如果尖头文件被移动发生在Linux上打开的文件处理程序是什么,删除 [英] What happens to an open file handler on Linux if the pointed file gets moved, delete

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

问题描述

发生在Linux上打开的文件处理程序是什么,如果有针对性的文件同时获得:


  • 搬走了? - >请问文件句柄保持有效

  • 删除 - >这是否导致EBADF,指示无效的文件句柄

  • 将一个新的文件替换? - >是否文件处理程序指向这个新文件

  • 由硬链接到一个新的文件替换 - >请问我的文件处理程序跟随这个链接

  • 将一个软链接到一个新的文件替换? - >我的文件处理程序打到现在这个软链接文件

为什么我要问这样的问题:我使用的还是热插拔硬件(如USB设备等)。它可以发生,该设备(以及它的/ dev /文件)获取由用户或另一个的Gremlin复位。

什么是最好的做法处理这个?

感谢您分享您的体验!


解决方案

如果文件移动了(在同一个文件系统)或重命名,然后将文件句柄保持打开状态,仍然可以用于读取和写入文件。

如果文件被删除,文件句柄保持打开状态,仍然可以使用(这不是某些人期望的那样)。该文件将直到最后句柄关闭没有真正被删除。

如果该文件是由一个新的文件替换,这取决于究竟如何。任一文件被覆盖,在这种情况下,文件句柄仍是有效和访问新的文件,或在现有的文件是未链接的和一个新的具有相同名称创建的,在这种情况下,它是相同缺失(见上文)

在一般情况下,一旦文件被打开,该文件是开放的,没有人改变目录结构可以改变 - 他们可以移动,重命名文件,或者把别的东西在它的地方,它只是保持打开

在Unix的没有删除,仅仅取消连结(),它是有意义的,因为它不一定删除该文件。 - 只是删除从目录的链路


如果另一方面底层设备消失(例如,USB拔下),则文件句柄将无效一个以上,且可能给任何操作IO /错误。你还是得虽然关闭它。这将是真实的,即使该设备在重新插入,因为它不是明智的保留文件在这种情况下打开。

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

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

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

What's the best practice dealing with this?

Thanks for sharing your experience!

解决方案

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. Either the file is overwritten, in which case the file handle will still be valid and access the new file, or the existing file is unlinked and a new one created with the same name, in which case it's the same as deletion (see above).

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 an 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天全站免登陆