cxbast has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to figure out how to use the 1st byte of the permissions string that is returned by stat. If I use the test -l operator it confirms that my file is a link, but when I use the technique below which I got from your site. It returns a - instead of an l for link. For directories and regular files it does return the correct value.
Now to be honest I don't really understand what the & 07777 and the 0170000 do as shown in the example below. Is it some sort of masking? I am very new to perl so any help would be greatly appreciated.
Code Excerpt:
Below is entire script...@ftypes = qw(. p c ? d ? b ? - ? l ? s ? ? ?); ($dev,$inode,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($source_file); $perms = $mode & 07777; $filetype = $ftypes[($mode & 0170000)>>12];
#!/usr/bin/perl # # get information about a file # # define source file parameter which is a link $source_file = "/temp/test/source/dir1/hello1_link.txt"; # define filetype array with potential first bits of mode @ftypes = qw(. p c ? d ? b ? - ? l ? s ? ? ?); $ftypes0 = ''; # Get File Statistics Using Scalar context ($dev,$inode,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($source_file); $perms = $mode & 07777; $octperms = sprintf("%lo", $perms); $filetype = $ftypes[($mode & 0170000)>>12]; # Try to Id a link using lstat $stat = lstat($source_file); octperms = sprintf("%lo", $perms); $filetype = $ftypes[($mode & 0170000)>>12]; # Try to Id a link using lstat $stat = lstat($source_file); if ( -l _) { print ("link identified using -l _ and lstat\n"); $link = "Y"; } # Try to Id a link with explicit -l test operator if ( -l $source_file) { print ("link identified using -l test operator \n"); $link = "Y"; + } print ("source_file = $source_file \n"); print ("uid = $uid \n"); print ("gid = $gid \n"); print ("mode = $mode \n"); print ("perms = $perms \n"); print ("octperms = $octperms \n"); print ("filetype = $filetype \n"); print ("link = $link \n"); print ("nlink = $nlink \n"); link identified using -l _ and lstat link identified using -l test operator source_file = /temp/test/source/dir1/hello1_link.txt uid = 14442 gid = 554 mode = 164349 perms = 509 octperms = 775 filetype = - link = Y nlink = 1
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unix File Type with stat
by Zaxo (Archbishop) on Sep 10, 2002 at 05:47 UTC | |
by Anonymous Monk on Sep 10, 2002 at 12:55 UTC | |
by Helter (Chaplain) on Sep 10, 2002 at 13:09 UTC | |
by Zaxo (Archbishop) on Sep 10, 2002 at 15:57 UTC | |
by Anonymous Monk on Sep 10, 2002 at 12:38 UTC | |
by Helter (Chaplain) on Sep 10, 2002 at 13:05 UTC | |
|
Re: Unix File Type with stat
by blm (Hermit) on Sep 10, 2002 at 05:13 UTC |