linux - inode - 移动一个已经打开的文件

标签 linux filesystems inode

在 linux 上,ext4 分区上有一部电影,如果我打开电影并观看它,然后我移动或重命名电影。

在之前的缓存用完后,它需要从原始文件中读取更多的缓存,然后它仍然可以这样做。

问题是:

文件系统和 inode 是如何实现的?

最佳答案

使用 rename()在同一文件系统中的文件上只是更改指向 inode 的名称。您甚至可以使用 rename() 将该名称移动到另一个目录中 - 只要它在同一文件系统中:

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. ...

请注意 rename() 的 POSIX 规范比 C 标准 rename() 规范更具体:

7.21.4.2 The rename function

Synopsis

#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 by new . The file named old is no longer accessible by that name. If a file named by the string pointed to by new exists prior to the call to the rename 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.

(这是 rename() 的完整 C 标准规范。阅读 POSIX link 了解更多信息。)

那么如何在应用程序中观看文件时重命名文件呢?您的电影观看进程的打开文件描述符用于访问您刚刚 rename() 的文件的底层 inode 不会改变。

这就是为什么你可以 unlink() 的原因。 (删除)正在使用的文件 - 您所做的只是删除指向 inode 的条目 - 链接 到 inode 之一。文件使用的空间直到删除指向 inode 的 last 链接后​​才会释放 - 并且打开的文件描述符算作一个链接。这也是为什么删除文件目录条目的函数称为 unlink() - 这就是它所做的全部。是的,一个文件( inode )可以有多个指向它的链接。

关于linux - inode - 移动一个已经打开的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34904412/

相关文章:

c - 通过更改调度优先级来同步线程

python - apache 配置中的权限被拒绝 : [Errno 13] Permission denied

linux - Sed,在两行之间解析,但只打印有限的行

python - 图形用户界面 (GUI) 的 python 包的正确布局

linux - Linux 在哪里保存空闲 inode 的记录?

linux - 如何将 -I 和 -n 与 xargs 结合起来?

powershell - 我怎样才能找到递归操作的确切位置?

c - 没有 df -i 的 inode 使用

linux - 链接到特定的 inode

linux - Docker 容器的文件系统是什么?在此容器内运行的应用程序在哪个文件系统上运行?