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

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

问题描述




  • 移动 - >文件处理程序是否保留有效?

  • 已删除 - >这是否导致EBADF,表示无效的文件处理程序?

  • 替换为新文件 - >文件指向这个新文件的处理程序?

  • 将硬链接替换为新文件 - >我的文件处理程序是否遵循此链接?

  • 替换为一个新的文件的软链接 - >我的文件处理程序是否现在这个软链接文件?



为什么我问这样的问题:我使用热插拔硬件(如USB设备等)。可能会发生,设备(也是/ dev / file)被用户或另一个Gremlin重新附加。



处理此事的最佳做法是什么?

解决方案

文件被移动(在相同的文件系统中)或重命名,然后文件句柄保持打开,仍可用于读取和写入文件。



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



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



一般来说,一旦文件打开,文件打开,没有人更改目录结构可以改变 - 他们可以移动,重命名文件或放置其他东西,它只是保持打开。



在Unix中没有删除,只有 unlink(),这是有道理的,因为它不一定会删除文件 - 只需从目录中删除链接






如果另一方面底层设备消失(例如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 hot-plugged 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?

解决方案

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