Sample Header Ad - 728x90

Does mv across filesystems flush the destination filesystem cache between copy and delete?

2 votes
0 answers
263 views
mv is implemented as a simple [rename()](https://pubs.opengroup.org/onlinepubs/000095399/functions/rename.html) whenever possible, falling back to copy-then-delete whenever renaming doesn't work. In practice ([but not in theory](https://github.com/coreutils/coreutils/blob/3a1c328cd55f427a74f16cda8513bedb7c153fd8/src/mv.c#L192-L202)) , these two cases correspond to source and destination being on the same or on different filesystems, respectively. **My question:** Does mv, as defined by the POSIX standard and implemented as part of the GNU coreutils, make any guarantee that in the latter case (i.e. rename not possible, fall back to copy-then-delete), filesystem caches are flushed (in the sense of [sync](https://linux.die.net/man/8/sync)) between the copy and delete operation so that disconnecting the destination filesystem at just the right moment won't lead to data loss? **What I've found so far:** The [POSIX spec for mv](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/mv.html) says that >termination at any point shall leave either source_file or the destination path complete which would suggest "yes", but then I'm not sure whether disconnecting a filesystem would be classed as simple "termination" or not, whether such statements in general only apply to the "surface" filesystem state, and so on. There is a [Hacker News comment](https://news.ycombinator.com/item?id=11804364) answering "no" but not citing any sources. Maybe the question is actually ill-defined because [POSIX sync()](https://pubs.opengroup.org/onlinepubs/009695299/functions/sync.html) does not even guarantee *completion* of the flush, only triggering it, so there is no way to implement a "completely safe" copy-then-delete across filesystems anyway? I tried to find out how it's actually implemented in [GNU coreutils' mv's source code](https://github.com/coreutils/coreutils/blob/3a1c328cd55f427a74f16cda8513bedb7c153fd8/src/mv.c#L153) , but before I waste any more time on this and in the interest of anyone else with the same question, I thought I'd ask here, too.
Asked by smheidrich (936 rep)
Aug 29, 2022, 09:01 PM