Sometimes you need to "commit" a directory to target location atomically. That is you don't want to have a partial directory where only a subset of files is stored.
Being able to atomically commit a directory relies on 2 conditions:
- Having an atomic rename operation where the source and target is on the same filesystem device.
- Being able to create a temporary directory descriptor similar to a temporary file descriptor.
Condition 2 can be relaxed if you can accept that garbage temporary files may be left on the filesystem device.
An external system would have to be used to clean up the garbage. Cleaning up garbage directories is not atomic.
You have to delete each individual file recursive within the garbage directory.
One way to achieve condition 1 for many programs is to set the TMPDIR
environment variable.