Is moving file from tmpfs
to ext4
atomic?
No. Renames as such only work within a filesystem. The manual page for rename(2)
explicitly mentions the error that is returned if trying to rename across mount points:
EXDEV
oldpath and newpath are not on the same mounted filesystem.
Moves across file systems need to be done as a combination of a copy and a delete. mv
will do this for you if the rename()
doesn't work, but it will not be atomic in that case.
The simple way to work around that would indeed be to first copy the file to a temporary location on the same filesystem. In general, it's simplest to place the temporary file in the same directory as the final destination, since that's the only place that's guaranteed to be on the same filesystem.
Of course that requires that any process working on the files there will have some logic to ignore the temporary based on its name.
Roughly, something like this should work for one file:
cp /src/filename /dst/filename.tmp &&
mv /dst/filename.tmp /dst/filename &&
rm /src/filename
Note that the process you describe for a directory is essentially this:
cp -r /src/dir /dst/dir.tmp &&
mv /dst/dir /dst/dir.bak &&
mv /dst/dir.tmp /dst/dir &&
rm -r /dst/dir.bak
Which is not bad, but is not atomic. There's a moment of time between the two runs of mv
(or calls to rename()
), when /dst/dir
does not exist. That could be worked around by accessing the directory through a symlink, since the link can be atomically replaced with a rename.
IMHO this all seems to be made overly complicated. When tar alone seems like the obvious solution. tar can create just about any file system, including cdfs (--options cd9660:*). It will also allow you to time stamp the output file to any of that of the most recent -m || --modification-time
, --gid id || --gname name
, --acls || --no-acls
, --same-owner || --no-same-owner
, ...
Or you could create your filesystem. Perform a chown -Rh someone:somegroup .
within your file tree, and chmod
it to your liking and use either tar
, or rsync to place the file tree into your prepared filesystem. Then everything would be consistent -- same date, same owner/group && perms.
Well that's the way I'd approach something like this. :)
HTH
Best Answer
Yes. It is not only possible to mount directories, but also files. This enables to mount files residing on different file systems into one mutual directory.
Renaming the file results in EBUSY, when
mv
is used, this usually prints the message “Device or resource busy”. The destination can be renamed without throwing an error. The changes are applied to the new name instead.