in reply to Is it possible to localize the stat/lstat cache?
Why not store the results based on the files themselves?
use strict; use warnings; my @files = glob('./*'); my %stat = map { $_ => { r => (-r $_), w => (-w $_), x => (-x $_), s => (-s $_), } } @files; # print out all sizes, as an example print $stat{$_}{s}, $/ for @files;
Now you can call stat or lstat and still have a lookup table for cached values that you can always overwrite if you wish.
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is it possible to localize the stat/lstat cache?
by Aldebaran (Curate) on Apr 18, 2015 at 06:10 UTC | |
Hi jeff, When I come to this site with some spare time, I try to work through some script that stretches my game a little bit. I had to add print statements to figure out your syntax but wanted to ask for clarification.
Q1) Why are directories always 4096 on my linux machine, regardless of whatever is in it? I really couldn't understand the map and resulting hash until I saw that the values were themselves hash references. I'm not suggesting that I added to your script in any way to improve it; rather it is simply more verbose:
Q2) Do I have it correct that the stat hash has an array reference as its value, where it references a hash with the letters for filetests as keys and their stat'ed values for any given file as values? Q3) How would I enumerate them, that is, display all their values for a directory? Thanks for your interesting post and comment, | [reply] [d/l] [select] |
by afoken (Chancellor) on Apr 18, 2015 at 06:47 UTC | |
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.
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 $_:
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:
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:
Alexander
-- Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-) | [reply] [d/l] [select] |
by Aldebaran (Curate) on Apr 18, 2015 at 23:22 UTC | |
Thanks for your comments and scripts, alexander. This might be a sheer banality to those with greater expereince and understanding than me, but this material is right at where I can tread the learning curve. Data::Dumper truly makes this version pretty (abridged for length):
This other version shows the same material but with blocks used and the (abridged) output from Dumper:
This shows that even the small files take up 8 blocks in 2 different ways. I've been scratching my head to figure out all these fields, and they are to be the eqivalent of stat(2):
The script:
Q1) Why does Data::Dumper bless this? I understand just enough about "bless" to be completely-miffed by it, much like in its religious context. As to which is "better," that would clearly depend on the user's needs. Maybe the user doesn't want certain information in a large hash. For me, it was a worthwhile exercise both ways. | [reply] [d/l] [select] |
by afoken (Chancellor) on Apr 19, 2015 at 00:44 UTC | |
by AnomalousMonk (Archbishop) on Apr 19, 2015 at 00:32 UTC | |
|
Re^2: Is it possible to localize the stat/lstat cache?
by Anonymous Monk on Apr 20, 2015 at 12:19 UTC | |
| [reply] |