The first thing that comes to mind for me is 'locking' the directory. Do you get this error quite often? If this is happening because the files in this directory are changing quite rapidly, then I would suggest doing a lock of some kind on the directory while you fiddle around with it.
Note that this method only works if any and all programs that modify files in this directory follow this locking mechanism. For example, if this were a directory that users FTP files to and can delete files (from FTP) from this directory, this method simply would not work as desired because the users' FTP clients are not going to know that your lock exists, let alone how to go about gaining this lock. Having said that, here's how you'd do it:
Use a semaphore file to 'lock' the directory. Note the quotes around the word 'lock', because this does not deny any program access to the directory. If a program wants to walk in and change files, it is free to do so. The thought behind using a semaphore file is this: every single time you plan on modifying a file within the directory, you open a file for writing, lock it with Perl flock() command and then go about your business. Example code (untested):
#!/usr/bin/perl -w use strict; use Fcntl ':flock'; my $dir_path = '/home/snam/subdir'; my $lock_file = $dir_path . '/lock.lck'; # Get a 'directory lock' (note the quotes again) :) open LOCK, '>', $lock_file or die "Could not lock directory '$dir_path': $!"; flock LOCK, LOCK_EX; # Now do whatever you want with this directory. # If all programs use the locking mechanism, they # won't have access to this directory until you execute # a line that says "close LOCK;" (or when the script exits) opendir DIR, $dir_path or die "Could not open directory '$dir_path': $!"; my @files = readdir DIR; closedir DIR; for my $d (@files) { next if $d !~ /^[0-9]+$/; my $procdir = "/proc/$d"; open IN, '<', $procdir . '/status' or die "Can't open status with $procdir: $!"; while (<IN>) { # Fill this in yourself... } } # We're done with the directory! Let someone else access it close LOCK;
Update: As soon as I posted, I realized I left out the actual flock() statement to place an exclusive lock on the semaphore file {grin}
C:\>shutdown -s
>> Could not shut down computer:
>> Microsoft is logged in remotely.
In reply to Advisory locking to 'lock' a directory
by Coruscate
in thread fastest way to open a file and storing it?
by snam
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |