in reply to Re^2: Is it possible to localize the stat/lstat cache?
in thread Is it possible to localize the stat/lstat cache?
subroutine says this is your hash: key: ./stat1.pl, value: HASH(0xa1519ac)
Use Data::Dumper or similar to dump the hash content.
Q1) Why are directories always 4096 on my linux machine, regardless of whatever is in it?
They aren't. Directories on ext2/3/4 filesystems have a minimal size, 1 block, which is 4096 bytes on typical large filesystems. Smaller filesystems may use block sizes of 1024 or 2048. Directories filled with many files grow larger than one block. Removing the files will NOT make the directory shrink. Other filesystems may give completely different results. Unless you are writing low-level code to check, repair, or backup filesystems, it is best to completely ignore any size value for anything but plain files.
my %stat = map { $_ => { r => (-r $_), w => (-w $_), x => (-x $_), s => (-s $_), } } @files;
Note that this code is not as efficient as it may seem. It hides four (l)stat calls per file, and so it may cause race conditions. To really reduce the number of (l)stat calls, use one explicit (l)stat and the special file handle _ instead of $_:
my %stat = map { lstat($_) or die "Can't lstat $_: $!"; $_ => { r => (-r _), w => (-w _), x => (-x _), s => (-s _), } } @files;
fishmonger gave a much better hint: File::stat's stat and lstat functions both return an object that could be stored in the hash, allowing you to run all tests that you need without storing each tests result in the %stat hash:
use v5.12; use File::stat 1.02 qw( stat lstat ); # ... my %stat = map { $_ => lstat($_) } @files; # ... for my $fn (@files) { say $fn,' is ',(-d $stat{$fn} ? 'a directory' : -x $stat{$fn} ? 'exe +cutable' : 'not executable'); say $fn,' has a size of ',$stat{$fn}->size(),' bytes, uses ',$stat{$ +fn}->blocks(),' "blocks" of 512 bytes, the filesystem uses a block si +ze of ',$stat{$fn}->blksize(),' bytes'; }
Update: Note that stat and lstat often return st_blocks for the historic block size of 512, even if the filesystem uses a different block size. This conforms to POSIX:
The unit for the st_blocks member of the stat structure is not defined within IEEE Std 1003.1-2001. In some implementations it is 512 bytes. It may differ on a file system basis. There is no correlation between values of the st_blocks and st_blksize, and the f_bsize (from <sys/statvfs.h>) structure members.
Traditionally, some implementations defined the multiplier for st_blocks in <sys/param.h> as the symbol DEV_BSIZE.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Is it possible to localize the stat/lstat cache?
by Aldebaran (Curate) on Apr 18, 2015 at 23:22 UTC | |
by afoken (Chancellor) on Apr 19, 2015 at 00:44 UTC | |
by AnomalousMonk (Archbishop) on Apr 19, 2015 at 00:32 UTC |