in reply to oldfiles

- first note on your code is that you blatantly waste CPU cycles with double-quotes interpolation. heh, this is habit seen to many people that came to Perl from other programming language like C/C++ where a string should always be limited by double quotes, but many other languages (and Perl is one of them) offer extended functionality through these semantic sugarcubes :)
e.g.:
"." -> '.'
"*END*" -> '*END*'
"support\@mydomain.com" -> 'support@mydomain.com'
... etc ...

- check Perl FAQ for a perlish way of getting your hostname
- i'd rather consider using getpwuid and a caching system instead of pushing all users information into memory
- run this script on a cron basis scanning user writeable filesystems and you'll soon get headaches (considering of course that users are always b.a.d.) - they could create a heck lotta dumb files to make your script trash your system (heh, my first thought was to tell you to switch to a more efficient storage method for the %files data structure)
- presuming a MB equals '1000000' is wrong. indeed, HDD manufacturers inherited this darn habit, but this is not true on the filesystem thay lies onto that storage space ;-)
- you waste again a lots of CPU cycles performing a lot of useless stats: file used in your wanted routine as $_ has already been stat-ed by the File::Find module, so now you can rely on the information in _;
- as a principle: restrict the context of each variable as much as possible to avoid problems ;-] (i'm talking about most of the variables on this line: my ($uid, %files, $size, @pw, %unames, $str);)
 
Here is a trimmed down code that implements that cache I told you above, eliminates all useless stats and switches %files to hash of arrays ;-]
use Data::Dumper; use Cache::MemoryCache (); my $cache = new Cache::MemoryCache; find({wanted => \&ab_wanted, no_chdir => 1}, $opt{d}); print Dumper \%files; sub ab_wanted { if (-r _ && -f _){ my $size = int ((-s _) / 2**20); if ($size > $opt{s} && $opt{a} < -A _){ my $uid = (lstat(_))[4]; my $name = $cache->get($uid); unless (defined $name) { $name = getpwuid($uid); $cache->set($uid, $name); } push @{$files{$name}}, [ $_, $size, int -A _ ]; } } }
--
AltBlue.

Replies are listed 'Best First'.
Re: Re: oldfiles
by neilwatson (Priest) on Nov 15, 2002 at 17:53 UTC
    I find parts of this hard to follow. What is the purpose of Cache::MemoryCache? What is being cached and how is that better than letting the OS handle RAM?

    How does the information get stored using a hash of arrays?

    What does the no_chdir => 1 mean?

    Neil Watson
    watson-wilson.ca

      What does the no_chdir => 1 mean?
      from File::Find manual:
      "no_chdir"
                Does not "chdir()" to each directory as it recurses. The wanted()
                function will need to be aware of this, of course. In this case, $_
                will be the same as $File::Find::name.
      
      In other words, it just means that your program will skip the chdir part which is useless for your program (so you gain again some CPU cycles) and, as a bonus, $File::Find::name equals $_, so you'll be able to use:
      push @{$files{$name}}, [ $_, $size, int -A _ ];
      instead
      push @{$files{$name}}, [ $File::Find::name, $size, int -A _ ];

      What is the purpose of Cache::MemoryCache?
      heh, i told you in my previous post to consider using getpwuid and a caching system instead of pushing all users information into memory. Cache::MemoryCache is just a wellknown module that could be useful for such things. OFC, it your specific case, this could be overworking as your program is too simple and maybe a simple hash cache would be enough. Anyway, I was trying to prepare you for more complicate stuff ;-)
      What is being cached? The correspondance between UserIDs and UserNames.
      Let's see an example using a simple hash-cache.
      my %cache; find({wanted => \&ab_wanted, no_chdir => 1}, $opt{d}); use Data::Dumper; print Dumper \%files, \%cache; sub ab_wanted { if (-r _ && -f _){ $size = int ((-s _) / 2**20); if ($size > $opt{s} && $opt{a} < -A _){ $uid = (lstat(_))[4]; my $name = $cache{$uid}; unless (defined $name) { $name = getpwuid($uid); $cache{$uid} = $name; } push @{$files{$name}}, [ $_, $size, int -A _ ]; } } }

      --
      AltBlue.