Op 02-11-16 om 10:51 schreef Stephane Chazelas: > At the moment, there's no way (that I know) to create a temp file > reliably with POSIX utilities (though it may be possible to create a > temp dir reliably, which is generally a better idea than creating a temp > file in a world writable directory anyway).
Hmmm. Interesting. (Just for clarity: below I'm still talking about creating temporary files for any use as in 'mktemp', and *not* about locking.) If both 'mkdir' and 'ln' operate atomically, there could be a safe workaround for creating a regular file directly under /tmp. It would involve creating a (very) temporary directory under /tmp using 'mkdir -m700', then creating the file inside there, setting the mode, etc. with no need for atomicity, then attempting to 'ln' that file back to /tmp until we've got an available name. Do you think this could work? The lack of a good POSIX shell way to create a temporary file has irked me for years. So, as part of my cross-platform POSIX shell library "modernish", I've created a shell implementation of 'mktemp' which is a superset of currently available 'mktemp' implementations. To create regular files, it currently uses the 'set -C'-plus-output-redirection technique discussed earlier. At the time of writing it I was assuming it must be safe, but it isn't, and (given the discussions here) it seems uncertain if it will ever be safe in the future[*]. If the workaround described above is good, I will have to implement it. It also has an -F flag to create a temporary FIFO. Should 'mkfifo' be considered atomic? Thanks, - Martijn [*] I don't get all the objections. Is there any good reason for *not* ensuring that noclobber is atomic? It's not as if modern operating systems make this hard to implement. Why not err on the side of safety?