i'm going crazy for this.
I'm trying to write an archive library which can store and extract files. An archive file looks like this:
<0,/home/user/file.txt,154,0755>
file contents (154 byte)
Each file is identified by an header ( <…> ), with four "tags" (separated by commas), file type (0 for file and 1 for directory), path, size in bytes, permissions (in octal).
I retrieve the size and the permissions with the stat system call (i'm on Linux).
My problem is that i have to convert the octal value from st_mode to a string, store it in the archive file (the fourth tag in the header),then extract it and use it with the chmod syscall.
To convert it to string i use:
char mode[6];
sprintf (mode, "%o", statr.st_mode);
and to retrieve it i use atoi, but it does not seem to work. For example, the value stored in the 4th tag is 100644, but chmod set the permissions wrong (file not readable by my user).
I'm not sure if i explained well, i wil post the whole code if is needed (but it don't think there are implementation problem, is just a problem between conversion from octal to string)
EDIT: Solved! Actually the strtol method worked, but i had forgotten to apply it to the directories too (so extracting a directory with files inside caused Segfault because of the bad folder's permission mask). Thanks everybody for help!
Best Answer
As far as I know, there's not a unix standard function to do this.
You will most likely have to mask
st_mode
to get each permissions bit that you want and translate into something that makes sense to you.related post
There's maybe an easier way to do this, but I've including a simple example which shows how to get each bit that you are interested in. This does compile and seemed to run as expected.
st_mode.c