Stephane Chazelas <stephane.chaze...@gmail.com> wrote, on 07 Nov 2016: > > 2016-11-02 13:32:44 +0000, Martijn Dekker: > [...] > > 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? > [...] > > I don't think you can use ln here. > > ln "$tempdir/file" "$tempfile" > > would create a "$tempfile/file" link if "$tempfile" existed and > was of type directory or a symlink eventually resolving to a > directory.
Yes, ln is unsuitable for this purpose if the destination file is in a world writable directory. > Same problem with "mv" (which I think would work just > as well (with LC_ALL=C mv -i < /dev/null 2> /dev/null)) No, mv -i doesn't work just as well - it has a race condition. If a file is created in between the existence check and the rename() call, mv will remove the file. > You could use "link" (Unix, not POSIX), or "ln -T" (GNU, not > POSIX) or "mv -Tn" (GNU) instead. The standard allows systems to make "link" available only to processes with appropriate privileges, so that solution might not be sufficiently portable. -- Geoff Clare <g.cl...@opengroup.org> The Open Group, Apex Plaza, Forbury Road, Reading, RG1 1AX, England