Linux 中的“陈旧文件句柄”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20105260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:24:17  来源:igfitidea点击:

What does 'stale file handle' in Linux mean?

linuxunix

提问by IDDQD

Say I have a terminal open, and through that terminal I have cd'ed to some directory. Through another terminal, I delete that directory and restore it back from an identical backup. When I try to vima file from the first terminal, in the same directory, why do I get an error about a stale file handle? What does it mean? (On a side note, I have found that it is possible to bypass this issue through cd $(pwd).)

假设我打开了一个终端,并通过该终端cd进入了某个目录。通过另一个终端,我删除该目录并从相同的备份中恢复它。当我尝试vim从第一个终端访问同一目录中的文件时,为什么会收到有关陈旧文件句柄的错误?这是什么意思?(顺便说一句,我发现可以通过 绕过这个问题cd $(pwd)。)

采纳答案by dg99

When the directory is deleted, the inodefor that directory (and the inodes for its contents) are recycled. The pointer your shell has to that directory's inode (and its contents's inodes) are now no longer valid. When the directory is restored from backup, the old inodes are not(necessarily) reused; the directory and its contents are stored on random inodes. The only thing that stays the same is that the parentdirectory reuses the same name for the restored directory (because you told it to).

当目录被删除时,索引节点对于该目录(和其内容的索引节点)被回收。您的 shell 指向该目录的 inode(及其内容的 inode)的指针现在不再有效。当目录从备份中恢复时,旧的 inode不会(必须)重用;目录及其内容存储在随机 inode 上。唯一保持不变的是目录对恢复的目录重用相同的名称(因为您告诉它)。

Now if you attempt to access the contents of the directory that your original shell is still pointing to, it communicates that request to the file system as a request for the original inode, which has since been recycled (and may even be in use for something entirely different now). So you get a stale file handlemessage because you asked for some nonexistent data.

现在,如果您尝试访问原始 shell 仍指向的目录的内容,它将将该请求作为对原始 inode 的请求传达给文件系统,该原始 inode 已被回收(甚至可能用于某些用途)现在完全不同了)。因此,您收到一条stale file handle消息,因为您要求提供一些不存在的数据。

When you perform a cdoperation, the shell reevaluates the inode location of whatever destination you give it. Now that your shell knows the new inode for the directory (and the new inodes for its contents), future requests for its contents will be valid.

当您执行cd操作时,shell 会重新评估您提供给它的任何目的地的 inode 位置。现在您的 shell 知道目录的新 inode(以及其内容的新 inode),未来对其内容的请求将是有效的。