On Sat, Oct 25, 2003 at 12:17:37PM +0200, Kevin Pfeiffer wrote:
> In article <[EMAIL PROTECTED]>, David wrote:
>> 4. you have:
>> 
>> unless (-e $path) {
>>      print "Creating trash directory: $path\n";
>>      mkdir $path or die "Couldn't create $path: $!\n";
>> }
>> 
>> don't do this for a general utility program where your program might be
>> involved very often from different scripts. you have a race condition. do
>> something similar to the following instead:
>> 
>> [panda]$ perl -MErrno -e 'die $! unless(mkdir('foo') || $!{EEXIST});'
                 ^^^^^^^

[ FWIW: in newer perls Errno is loaded automatically when you use %!. ]

> This says "die unless you can mkdir or you get an error message that it
> already exists"? I understand your point, but in this situation (creating
> a trash directory) I don't see the danger.

I wouldn't call it "dangerous".  You're doing something in two steps
that could easily be done in one, so it's maybe "redundant".  And I
don't think it makes sense for your script to die if another process
creates the directory between the file test and the mkdir()...  But this
isn't serious; the really harmful race conditions take two or more steps
to complete an action that *must* be completed atomically -- like 
reading-and-updating a shared variable or opening-and-locking a file.

When mkdir() fails, you'd usually want to proceed if a) it failed with
EEXIST and b) the file that exists is actually a directory.

    mkdir $path or do {
      die "mkdir: $path: $!" unless $!{EEXIST} and -d $path;
    };

There are still potential timing problems, but this seems more likely
to do the right thing.

-- 
Steve

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to