Here’s something I don’t understand. In perl, let’s say I have a file(handle) $file then
my $mode = (stat $file)[2];
gives the mode of the file, described as the type and permissions. Then, in the docs for chmod, it is recommended to mask off the file type when sending the mode from stat to chmod like, e.g.
my $perm = (stat $file)[2] & 07777;
chmod($perm | 0600, $file);
Similarly, the docs for mkdir indicate that mkdir(FILENAME,MASK) “creates the directory specified by FILENAME, with permissions specified by MASK (as modified by umask).”
Alrighty. Here’s what I don’t get. On the command line, I make a dir temp in my home directory, with default permissions drwxr-xr-x (octal 0755). Then
- perl’s stat tells me that the mode of this directory is 16877.
$perms = (stat 'temp')[2] & 07777;returns 493.$perms = sprintf("%04o",(stat 'temp')[2] & 07777);returns 0755.
I expected chmod(x,'temp') with x = (1) and (2) to change the permissions on temp/ to something screwy. But, all three give drwxr-xr-x (admittedly with (3) you must do chmod(oct($perms),'temp);) Similarly, mkdir(temp,16877), mkdir(temp,493), mkdir(temp,0755) all give permissions drwxr-xr-x on temp/.
So, my question: what’s really going on? How is it that chmod and mkdir take these three different values and set the same permissions on temp/? I realize one answer might be ‘there’s more than one way to do it’, and that’s fine as far as it goes, but I’d like to understand what’s happening here. Also, what is the recommended way to pass permissions values to these functions? Thanks!
493 decimal = 0755 octal, so this is a non issue – they are identical.
16877 decimal = 40755 octal.
Looks like chmod only uses the bits that it needs and ignores anything in the higher order bits.