On linux, on a ext4 partition there is a movie, if I open the movie & watch it, then I move or rename the movie.
After the previous cache use up, it need to read more cache from original file, then it still could do that.
The question is:
How file system & inode achieve this?
Using rename()
on a file within the same file system just changes the name that points to the inode. You can even use rename()
to move that name into another directory - as long as it's in the same file system:
The rename() function shall change the name of a file. The old argument points to the pathname of the file to be renamed. The new argument points to the new pathname of the file. ...
Note that the POSIX specification for rename()
is a lot more specific than the C Standard rename()
specification:
7.21.4.2 The
rename
functionSynopsis
#include <stdio.h> int rename(const char *old, const char *new);
Description
The rename function causes the file whose name is the string pointed to by
old
to be henceforth known by the name given by the string pointed to bynew
. The file namedold
is no longer accessible by that name. If a file named by the string pointed to bynew
exists prior to the call to therename
function, the behavior is implementation-defined.Returns
The
rename
function returns zero if the operation succeeds, nonzero if it fails, 269) in which case if the file existed previously it is still known by its original name.
(That's the entire C Standard specification for rename()
. Read the POSIX link for a lot more information.)
So how can you rename a file while your watching it in an application? The underlying inode that your movie-watching process's open file descriptor uses to access the file that you just rename()
'd doesn't change.
That's the same reason why you can unlink()
(delete) a file while it's in use - all you're doing is removing the entry that points to the inode - one of the links to the inode. The space used by the file isn't freed until the last link to the inode is removed - and an open file descriptor counts as a link. That's also why the function that deletes the directory entry for a file is called unlink()
- that's all it does. And yes, a file (inode) can have more than one link pointing to it.