Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Safely create new directory (mkdir, NFS)

by tye (Sage)
on Jul 25, 2012 at 19:37 UTC ( [id://983724]=note: print w/replies, xml ) Need Help??


in reply to Safely create new directory

mkdir is already atomic on local file systems. creat() with O_EXCL is not always atomic over NFS (depends on NFS version and kernel version, just when talking about Linux). I find indications in both directions as to whether mkdir() is atomic over NFS; so much so that I gave up trying to find the real answer.

Your approach can't work because there is no atomic "delete the file 'foo' and create the directory 'foo'" operation.

If it turns out that you have an NFS environment with atomic file creation semantics but mkdir isn't atomic (or you just can't determine whether it is or not), then you could use something similar to your approach.

  1. Increment $i
  2. Atomically create "reserve.$i" file
  3. next if that failed
  4. mkdir "$i" (which should never fail)

Note that you can't delete the "reserve.$i" file unless you know nobody is going to try to create a new directory after that. You could go with a more complicated algorithm if you want to delete the "reserve.$i" files.

  1. Increment $i
  2. next if -d "$i" (optional but avoids some thrash)
  3. Atomically create "reserve.$i" file
  4. next if that failed
  5. if -d "$i" then unlink "reserve.$i", next
  6. mkdir "$i" (which should never fail)
  7. unlink "reserve.$i"

- tye        

  • Comment on Re: Safely create new directory (mkdir, NFS)

Replies are listed 'Best First'.
Re^2: Safely create new directory (mkdir, NFS)
by elTriberium (Friar) on Jul 26, 2012 at 00:17 UTC
    Thanks, that helps! About your point that there's no atomic "delete the file and create the dir" operation. I understand that, but my point was to just leave the file there. Sure it's a dirty solution, that's why I was looking for better solution. Just using "mkdir" sounds good, if it's atomic then that will do the trick. I saw similar issues with O_EXCL not being guaranteed on older Linux kernels, but it was mentioned that it should be safe from 2.6.5 onwards.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://983724]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 12:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found