Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

check if file is executable by user/group/world?

by Anonymous Monk
on Aug 06, 2009 at 05:46 UTC ( [id://786317]=perlquestion: print w/replies, xml ) Need Help??

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

how would i -- from within perl -- check whether a given file is executable by world (chmod a+x myfile)?

how about whether it's executable by group (chmod g+x myfile)?

  • Comment on check if file is executable by user/group/world?

Replies are listed 'Best First'.
Re: check if file is executable by user/group/world?
by vinoth.ree (Monsignor) on Aug 06, 2009 at 07:08 UTC

    I assume that you want to check the permission of the file

    Fcntl':mode' module contains all the constants and functions you need to extract the file type and permissions. S_IMODE is the function which helps you extract the permissions: $permissions = S_IMODE($mode);

      Use "use Fcntl ':mode';" to import S_I* constants. They are just bitmasks that masks bits from portions of files permissions. In Unix systems they are grouped into 4 groups of 3 bits. First three are for setuid(4), setgid(2), stickybit(1). Other three groups of bits are for user, owner group and others. Permissions are read(4), write(2) and execute(1).

      Permissions are often represented in octal form, so for this permission "rwxr-x-w-" you will have octal one 0752.

      I recommend you reading "perldoc -f stat" document, you have it all there. Except some of the stuff about inner working, which you could find in "info glibc" if you are working on some Linux system.

      Code from stat doc page:
      #!/usr/bin/env perl use Fcntl ':mode'; use strict; use warnings; my $mode = (stat($ARGV[0]))[2]; my $user_rwx = ($mode & S_IRWXU) >> 6; my $group_read = ($mode & S_IRGRP) >> 3; my $other_execute = $mode & S_IXOTH; printf "Permissions are %04o\n", S_IMODE($mode), "\n"; my $is_setuid = $mode & S_ISUID;
      Just a quick clarification on the source of $mode in the ree's example above
      perl -e 'use Fcntl qw(:mode);$permissions=S_IMODE((stat("filename"))[2 +]);print $permissions,"\n";'

        Sorry!Utilitarian I just given the hint, and missed something,Below the code I just tried,

        use Data::Dumper; use Fcntl ':mode'; my $filename="pl_bak.pl"; my @array=lstat($filename); $permissions = sprintf "%04o", S_IMODE($array[2]); print "File permissions: $permissions\n";
        Update

        Removed a file type line.

Re: check if file is executable by user/group/world?
by Anonymous Monk on Aug 06, 2009 at 14:12 UTC
    so how would I, then, turn said bit off and on? I assumed it would be something like this, but it clearly does not work:
    use Fcntl ':mode'; $file = 'myfile'; $mode = (stat($file))[2]; chmod ($mode | S_IXOTH), $file;

      This is why you should use warnings and strict in your code. With warnings turned on you should get next warning:

      Useless use of array element in void context at ./file_change_mode.pl line 12.

      Reason why this is happening you can see in perlfunc.

      You could write:
      chmod(($mode | S_IXOTH), $ARGV[0]);

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-18 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found