When a file is moved and the destination file already exists and is currently being read, will the operation succeed? Will it depend on how long the reading operation on the destination file lasts?
1 Answers
The reading operation will succeed, regardless from the time it takes to complete the reading operation.
Why and how does this work?
When the reading operation starts, the file's Inode is used as a handle from which the file's content is read.
When moving another file to the target file, the result will be a new inode, which means the physical content of the file on the disk will be placed somewhere else and the original content of the file which is being read won't be touched.
The only thing they have in common, is their path/filename, while the underlying inode and phyiscal location on the disk changes.
Once the reading operation finishes (given no other process still has an open file handle on the old file and there are no other hardlinks referencing its inode), the old data will be discarded.
Once the moving operation is completed, the file will have a new inode index number.
You can display the files inode index number using ls -i /tmp/some-file.
For the same reason as described above, it is possible to delete files which are still in use by an application, as the applications using the file will just read from the inode (pointing to the actual file content on disk) while the files' reference in the filesystem is deleted.
- 1,053
- 7
- 13
-
1You forgot to mention hard links: When the reading operation finishes, if that inode is linked from another directory entry, then the data won't be removed (because it still has a name). – cjm Dec 01 '13 at 17:51
-
Thanks for pointing that out, edited my answer and added it. – Elias Probst Dec 01 '13 at 19:13