Home > Software design >  Is using fs::rename to move files between different file systems atomic?
Is using fs::rename to move files between different file systems atomic?

Time:02-03

On Linux, if we move (rename) a file from one file system to another like this:

fs::rename("/src/a", "/dest/a")?;

is it possible the file /dest/a to become visible/available to other potential readers (processes that scan /dest/ for example) before the whole file data (contents) is fully copied to the destination file system?

CodePudding user response:

From the docs

This will not work if the new name is on a different mount point.

So you shouldn't be using fs::rename to move between mount points. It's a simple filesystem rename and is not actually moving any data (which is why you can fs::rename a 2-terabyte file fairly quickly), so it only works if the source and destination are on the same filesystem.

If the source and destination are on the same mount point, then the answer is no. It's not possible for someone to read it before it's fully available, since, again, no data is actually transferred: it's just a single operation of "this pointer now points here and this one doesn't exist anymore".

  •  Tags:  
  • Related