develooper Front page | perl.beginners | Postings from December 2011

Re: File health check on *nix system using stat

Thread Previous
From:
John W. Krahn
Date:
December 10, 2011 12:55
Subject:
Re: File health check on *nix system using stat
Message ID:
4EE3C74E.3040507@shaw.ca
frank cui wrote:
> hi,

Hello,

> I'm reading the book of "automating system administration with perl" and
> having doubts about this code snippets about file health check.
>
> # attempt to read the directory entry for this file
> my @stat = stat($name);

That should probably be:

my @stat = stat($name) or return 0;


> if ( !$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8] ) {
> return 0;                        # according to the code context, here it
> is saying the file is NOT ok
> }
>
> here $name refers to the file name and it's checking the stat of this file.
>
> stat[4:8] refers to the following from the "perldoc -f stat"
>
> 4 uid      numeric user ID of file's owner
> 5 gid      numeric group ID of file's owner
> 6 rdev     the device identifier (special files only)
> 7 size     total size of file, in bytes
> 8 atime    last access time in seconds since the epoch
>
> so here the code is actually saying if all the above entries is 0 or
> undefined, then we declare this file to be NOT ok .
> and my question would be about this logic, why are we checking all of them
> with a AND relationship rather than OR, or maybe something else(like
> checking all the 0-12 entries) ?

That looks like a general heuristic that the author assumed would 
indicated a bad file.  As to whether it would work accurately is mute 
(there are a lot of files with a UID, GID and RDEV of 0, assuming he is 
reading from the whole file system.)



John
-- 
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About