Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

how to check permissions

by Anonymous Monk
on May 01, 2003 at 17:19 UTC ( [id://254717]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi everyone ! I need some help on writing a script which will give me an output saying wether the user has permissions to access a directory or not, I am have been trying to this using ls -la , ls -las but looks like it's not working .. can anyone help me out on that .. thanks

Replies are listed 'Best First'.
Re: how to check permissions
by derby (Abbot) on May 01, 2003 at 17:33 UTC
    What type of access? Read? Write? Both? Try the -x, -r, -w operators:

    $ perl -e 'print "Can read\n" if -r "/home"' $ perl -e 'print "Can write\n" if -w "/home"' $ perl -e 'print "Can execute (or cd into if dir)\n" if -x "/home"'

    -derby

      how can i combine all there in one go instead of in three ways thanks
      THANK YOU SOOOOOOOOO MUCH I LOVE THE HELP YOU GAVE ME
Re: how to check permissions
by graff (Chancellor) on May 02, 2003 at 03:18 UTC
    The example given by draconis only provides the "owner/group/other" mode flags for the file in question. It looks like the OP was asking how to learn what sort of access is available to the person running the perl script.

    In order to do that, you would need to combine the mode field from stat with knowledge of the current user's group membership(s) and whether the current user happens to be the owner of the file, so you'll know which set of bits from the mode flag are relevant for determining file access.

    A better way for the task at hand would be:

    ($r,$w,$x) = (-r $path, -w _, -x _); print "Access: read $r, write $w, execute (search dir) $x\n";
    A few notes:
    • using underscore as the argument for "-w" and "-x" tells perl to use the same "stat" data obtained by "-r"
    • as written above, any permission not granted will be "undef", so the printed message in this case would look like "read 1, write , execute (search dir) 1" when there is no write permission
    • contrary to derby's comment, "execute" permission on a directory does not control whether or not you can "cd" into that directory; it does determine whether you can "search" the directory contents -- i.e. you need execute permission to run "ls" with no args, or "find", or do any sort of operation using "*" or "?" as part of a file name within the directory.
Re: how to check permissions
by pzbagel (Chaplain) on May 01, 2003 at 17:29 UTC

    You can use stat() to get the mode of the file and check it with your code:

    $filemode=(stat($filename))[2]; #check permissions here with slick XOR

    Cheers

      i am very new with perl and get a lot of syntax trouble if possible could u sum it up for me ... if it's not too much trouble for you Thanks .. I am reading the perldoc and they have an example in it .. but it's kind of confusing ..and i need the permissions for the directories not files .. thanks again
Re: how to check permissions
by defyance (Curate) on May 01, 2003 at 17:28 UTC
    You could try stat or even File::Stat

    -- Can't never could do anything, so give me and inch, I'll make it a mile.

Re: how to check permissions
by draconis (Scribe) on May 01, 2003 at 17:57 UTC
    If you have the camel book see page 801. If not, here is the code:

    $mode = (stat($filename))[2]; printf "permissions are %04o\n", $mode &07777
    This will send back to you the usual UNIX-style permissions - once you have them - you can then interrogate them as you wish.
      how to factor in the sticky bit while getting the permissions?

        Did you try the code which brother draconis helpfully copied out for you? In what way are you unable to determine the sticky bit from that?

Re: how to check permissions
by Huele-pedos (Acolyte) on May 01, 2003 at 17:26 UTC
    perldoc -f stat
      could show me a script that would do that , if Possible thank you very much

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-24 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found