Need help understanding a Perl statement that uses File :: stat

In the code below, what does the expression " $retMode & 0777" and " $retMode & 006" mean ?

 use File::stat;  

 my $fpath = "/home/xyz/abc.sh" ;  
 my $info ;  
 my $retMode ;  
 my $property = "File_Permission";  

 $info=stat($fpath) ;  
 if($info){  
     $retMode = $info->mode; # This field contain file mode info  
     $retMode = $retMode & 0777;  
     if(($retMode & 006)) {  
        printf "$property|%03o|$fpath\n",$retMode;  
     }  
 }
+3
source share
3 answers

$retMode & 0777means that you take the value of the return mode (which is the file resolution + file type) and Bitwise-And its using the octal number of 777 (for example, decimal number 511, for example binary 111111111).

What does this mean technically - removes any bits from an integer above the 9th bit, so if the binary representation of the mode was> 9 bits, after this operation it would leave only the last 9 bits, representing the main permissions (read / write / execute for others / group / user).

? ( perldoc stat, mode() stat:

" , , printf, " % o ", ".

, 9- , 9 , . , 12 ( & 07777), 10-12 , // // perms (, setuid, is_directory).


$retMode & 006 , . 006 110 , 2/3 . , , # , , 1.

, & 006 - BAD-, , perms ( , , 2/3 ). (S_IF *) (S_IS *) Fcntl:

use Fcntl ':mode';
$retMode = $retMode & 0777; # Ignoring setuid and directory bits
$other_read_or_write = $retMode & (S_IWOTH || S_IROTH); 
                                  # Bits 2/3 - Other read/write
if ($other_read_or_write) {
    printf "$property|%03o|$fpath\n",$retMode; 
}
+5

POSIX , , , .

. http://www.tuxfiles.org/linuxhelp/filepermissions.html google " Linux ".

if(($retMode & 006)) {
      printf "$property|%03o|$fpath\n",$retMode;
}

, .

+2

This is Perl bit-by- bit and .

$ retMode and 0777 clear all bits above the bottom 9 bits of $ retMode.

$ retMode and 006 checks the bottom two 3 bits and returns true (a number other than 0) if these bits correspond to numbers 2, 3, 4, 5, 6, 7

In the pictures (where x is 1 or 0 and does not change from the top line to the bottom line):

$ RetMode and 0777:

$RetMode:   xxxxxxxxxxxxxxxxxx
0777        000000000111111111
Result:     000000000xxxxxxxxx

$ RetMode and 006:

$RetMode:   000000000xxxxxxxxx
0777        000000000000000110
Result:     000000000000000xx0

As sent by graviton, you are checking file permissions.

+1
source

Source: https://habr.com/ru/post/1785274/


All Articles