Y
yapd
I saw a particular use of stat in a book. The snippet was:
if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){
return 0;
}
It would return from a subroutine that was determining if a file was
readable (else it was deemed damaged). I was wondering what, if anything
this particular snippet was checking. I would be grateful if someone
would reveal what I may be missing.
The entire program as it appeared in the text, for purposes of context,
is below.
A second issue I was hoping someone could comment on:
this error occuring on the bounds checking in the for loop:
"# read the file one byte at a time""
Argument "\x{4a}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{59}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{75}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{71}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{62}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
I get the message when perl is run with '-w'. However, it is quiet
otherwise.
Thanks for your comments!
###############
#*
#* search a filesystem "by hand" for damaged files
#*
use Cwd; # module for finding the current working directory
$|=1; # turn off I/O buffering
sub ScanDirectory {
my ($workdir) = shift;
my($startdir) = &cwd; # keep track of where we began
chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
my @names = readdir(DIR);
closedir(DIR);
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
if (-d $name){ # is this a directory?
&ScanDirectory($name);
next;
}
unless (&CheckFile($name)){
print &cwd."/".$name."\n"; # print the bad filename
}
}
chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
}
sub CheckFile{
my($name) = shift;
print STDERR "Scanning ". &cwd."/".$name."\n";
# attempt to read the directory entry for this file
my @stat = stat($name);
if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){
return 0;
}
# attempt to open this file
unless (open(T,"$name")){
return 0;
}
# read the file one byte at a time
for (my $i=0;$i< $stat[7];$i++){
my $r=sysread(T,$i,1);
if ($r !=1) {
close(T);
return 0;
}
}
close(T);
return 1;
}
&ScanDirectory(".");
###############
from chapter 2 of ORA's
Perl for System Administration
if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){
return 0;
}
It would return from a subroutine that was determining if a file was
readable (else it was deemed damaged). I was wondering what, if anything
this particular snippet was checking. I would be grateful if someone
would reveal what I may be missing.
The entire program as it appeared in the text, for purposes of context,
is below.
A second issue I was hoping someone could comment on:
this error occuring on the bounds checking in the for loop:
"# read the file one byte at a time""
Argument "\x{4a}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{59}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{75}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{71}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{62}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6e}" isn't numeric in numeric lt (<) at line 61.
Argument "\x{6f}" isn't numeric in numeric lt (<) at line 61.
I get the message when perl is run with '-w'. However, it is quiet
otherwise.
Thanks for your comments!
###############
#*
#* search a filesystem "by hand" for damaged files
#*
use Cwd; # module for finding the current working directory
$|=1; # turn off I/O buffering
sub ScanDirectory {
my ($workdir) = shift;
my($startdir) = &cwd; # keep track of where we began
chdir($workdir) or die "Unable to enter dir $workdir:$!\n";
opendir(DIR, ".") or die "Unable to open $workdir:$!\n";
my @names = readdir(DIR);
closedir(DIR);
foreach my $name (@names){
next if ($name eq ".");
next if ($name eq "..");
if (-d $name){ # is this a directory?
&ScanDirectory($name);
next;
}
unless (&CheckFile($name)){
print &cwd."/".$name."\n"; # print the bad filename
}
}
chdir($startdir) or die "Unable to change to dir $startdir:$!\n";
}
sub CheckFile{
my($name) = shift;
print STDERR "Scanning ". &cwd."/".$name."\n";
# attempt to read the directory entry for this file
my @stat = stat($name);
if (!$stat[4] && !$stat[5] && !$stat[6] && !$stat[7] && !$stat[8]){
return 0;
}
# attempt to open this file
unless (open(T,"$name")){
return 0;
}
# read the file one byte at a time
for (my $i=0;$i< $stat[7];$i++){
my $r=sysread(T,$i,1);
if ($r !=1) {
close(T);
return 0;
}
}
close(T);
return 1;
}
&ScanDirectory(".");
###############
from chapter 2 of ORA's
Perl for System Administration