Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Do you know where your variables are?
 
PerlMonks  

state and file perms confusion

by neilwatson (Deacon)
on Aug 04, 2012 at 01:06 UTC ( #985371=perlquestion: print w/ replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Sisters and Brothers, Using stat, I'm not getting the results I expect. Could you please explain why.
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($file); ... Later $mode = $mode & 07777; print "mode=".$mode."\n";
In this example the target file is /etc/passwd. Here are my results.
$ ls -l /etc/passwd -rw-r--r--. 1 root root 1289 Jul 22 16:27 /etc/passwd $ ./perms-module.pl /etc/passwd 644 root root mode=420
Why is the mode 420 rather than 644?

Neil Watson
watson-wilson.ca

Comment on state and file perms confusion
Select or Download Code
Re: state and file perms confusion
by Kenosis (Priest) on Aug 04, 2012 at 01:20 UTC

    From perldoc:

    Because the mode contains both the file type and its permissions, you +should mask off the file type portion and (s)printf using a "%o" if y +ou want to see the real permissions. $mode = (stat($filename))[2]; printf "Permissions are %04o\n", $mode & 07777;

    You've masked off the file type with $mode = $mode & 07777;, so you'll get 0644 (octal) from the script above when your $mode equals 420 (decimal), since the printf above makes the conversion from decimal to octal.

      I'm missing something fundamental.
      print "mode=".$mode."\n"; printf "printf are %04o\n", $mode; printf "Permissions are %04o\n", $mode & 07777; ..produces mode=33188 printf are 100644 Permissions are 0644
      What am I seeing? How can store the number in a scalar rather than printing it?

      Neil Watson
      watson-wilson.ca

        What am I seeing?

        33188 decimal = 100644 octal (first printf) 07777 octal = 0000111111111111 binary (mask)-| +- & (bitwise AND) 33188 decimal = 1000000110100100 binary (mode)-| 0644 octal = 0000000110100100 binary (printf after mask) ^^^^ | + - file type masked out

        How can store the number in a scalar rather than printing it?

        use Modern::Perl; my $mode = 33188; my $modeResult = sprintf '%04o', $mode & 07777; say $modeResult; # prints 0644

        There aren't a lot of use cases for storing an octal "number" in a scalar. There are some, but they're all to do with formatting output, not actual use as a number.

        If your goal is to use this for chmod, you have to realise that it won't work. If you do this:

        $mode = sprintf "%o", 420; # $mode = 644 chmod $mode, $file;
        you'll actually be doing the same thing as this:
        chmod 01204, $file; # 01204 octal == 644 decimal
        Definitely not what you wanted. Leave the number as-is if you plan on using it in chmod. Note that the following lines are equivalent:
        # 1. octal number chmod 0644, $file; # 2. decimal number chmod 420, $file; # 3. number in a scalar $mode = 0644; chmod $mode, $file; # 4. number in a scalar (from decimal) $mode = 420; chmod $mode, $file; # 5. number with bitmasks $mode = 0666 & ~022; # like umask chmod $mode, $file; # 6. decimal with bitmasks $mode = 438 & ~022; # 438 == 0666 chmod $mode, $file;
        So, to answer your question, the number is already in the scalar. You don't actually need to do anything about it.

        And, if you use $mode = sprintf "%o", $mode;, remember what you just did: you took a number, you printf'd it into a string, and put that back in $mode. So $mode no longer carries a number but a string. Perl will try to force it back to a number if that's what you try to do, but it won't be the number you started with.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://985371]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-04-20 18:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (486 votes), past polls