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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.