6

I'm wondering if this is considered safe. I know the file handles work just fine as long as a link remains, and I know the identifier is the inode rather than the name, but I am not sure how it works across different FS.

For example copying from an ext4 harddrive to a NTFS USB stick, or copying from a FAT stick to an ext4 drive.

I was just copying over a bunch of large media files, and renamed them before the copy was done. The checksums match. I wonder if it is always safe, will it work in the opposite direction, are there quirks I should know about or reasons to avoid doing this?

The OS/Distro is Ubuntu with the 5.0.0-15 Linux kernel.

5

I am not sure how it works across different FS.

The rename operation itself doesn’t operate across different file systems; there is no difference between writing to a file from say a text editor and writing to a file using cp with a source file on another file system.

On Linux, the rename system call is transparent to other links to the file, which include other hard links and open file descriptions (and descriptors). The manpage explicitly states

Open file descriptors for oldpath are also unaffected.

(I’m qualifying with “on Linux” only because I couldn’t find a reference in POSIX; I think this is common across POSIX-style operating systems.)

So when you’re copying a file across file systems, cp opens the source for reading, the target for writing, and starts copying. Rename operations don’t affect the file descriptors it’s using; you can rename the source and/or the target without affecting cp.

Another way to think of this is that the file’s name in its containing directory is part of its directory entry, which points at its inode; open file descriptions are other pointers to the inode, as are other hard links. Changing the file name doesn’t affect any other existing pointers.

The caveats to watch out for are that tools such as mv don’t limit themselves to what the rename system call can do; if you mv files across file systems, the rename will fail (or mv will figure out that the operation is across file systems and won’t even attempt it), and mv will then resort to manually copying the file contents and deleting the original. This won’t give good results if the file being renamed is being changed simultaneously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.