如果指向的文件被移动或删除,Linux上的打开文件句柄会发生什么? [英] What happens to an open file handle on Linux if the pointed file gets moved or deleted

查看:391
本文介绍了如果指向的文件被移动或删除,Linux上的打开文件句柄会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果指向的文件同时获取,在Linux上打开文件句柄会发生什么:

  • 已移开->文件句柄保持有效吗?
  • 已删除->是否导致EBADF,表明文件句柄无效?
  • 由新文件替换->该文件是否处理指向该新文件的位置?
  • 由指向新文件的硬链接代替->我的文件可以跟随"该链接吗?
  • 通过软链接替换为新文件->我的文件句柄现在是否命中了该软链接文件?

为什么我问这样的问题:我正在使用热插拔硬件(例如USB设备等).可能会发生设备(以及它的/dev/file)被用户或另一个Gremlin重新连接的情况.

处理此问题的最佳实践是什么?

解决方案

如果文件已移动(在同一文件系统中)或已重命名,则文件句柄将保持打开状态,并且仍可用于读取和写入文件. /p>

如果删除了文件,则文件句柄将保持打开状态,并且仍可以使用(这不是某些人所期望的).在关闭最后一个句柄之前,不会真正删除该文件.

如果文件被新文件替换,则完全取决于方式.如果文件内容被覆盖,则文件句柄将仍然有效并访问新内容.如果取消现有文件的链接并使用相同的名称创建一个新文件,或者如果使用rename()将新文件移动到现有文件中,则该操作与删除操作相同(请参见上文)-也就是说,文件句柄将继续引用文件的原始版本.

通常,一旦打开文件,文件就会打开,并且没有人更改目录结构可以更改它-他们可以移动,重命名文件或将其他地方放到文件中,只是保持打开状态.

在Unix中,没有删除,只有unlink(),这很有意义,因为它不一定删除文件-只需从目录中删除链接.


如果另一方面,基础设备消失了(例如,USB拔出),则文件句柄将不再有效,并且可能在任何操作中产生IO/错误.您仍然必须关闭它.即使重新插入设备,也是如此,因为在这种情况下保持文件打开不明智.

What happens to an open file handle on Linux if the pointed file meanwhile gets:

  • Moved away -> Does the file handle stay valid?
  • Deleted -> Does this lead to an EBADF, indicating an invalid file handle?
  • Replaced by a new file -> Does the file handle pointing to this new file?
  • Replaced by a hard link to a new file -> Does my file handle "follow" this link?
  • Replaced by a soft link to a new file -> Does my file handle hit this soft link file now?

Why I'm asking such questions: I'm using hot-plugged hardware (such as USB devices etc.). It can happen, that the device (and also its /dev/file) gets reattached by the user or another Gremlin.

What's the best practice dealing with this?

解决方案

If the file is moved (in the same filesystem) or renamed, then the file handle remains open and can still be used to read and write the file.

If the file is deleted, the file handle remains open and can still be used (This is not what some people expect). The file will not really be deleted until the last handle is closed.

If the file is replaced by a new file, it depends exactly how. If the file's contents are overwritten, the file handle will still be valid and access the new content. If the existing file is unlinked and a new one created with the same name or, if a new file is moved onto the existing file using rename(), it's the same as deletion (see above) - that is, the file handle will continue to refer to the original version of the file.

In general, once the file is open, the file is open, and nobody changing the directory structure can change that - they can move, rename the file, or put something else in its place, it simply remains open.

In Unix there is no delete, only unlink(), which makes sense as it doesn't necessarily delete the file - just removes the link from the directory.


If on the other hand the underlying device disappears (e.g. USB unplug) then the file handle won't be valid any more and is likely to give IO/error on any operation. You still have to close it though. This is going to be true even if the device is plugged back in, as it's not sensible to keep a file open in this case.

这篇关于如果指向的文件被移动或删除,Linux上的打开文件句柄会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆