文件删除后handler是否失效的问题

今天发现一个有趣的现象:删除了一个文件后,文件对应的handler能够继续工作,仍然能够读取到数据,所以网上搜了一下,果然在StackOverflow找到了答案:

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.

delete操作并没有真的删除对应的inode节点,而只是调用了unlink函数。一个文件,必须得等到它的reference count = 0,才会被真正的清除。

A few apps (that don’t come to mind) rely on this behavior, by creating, opening and immediately deleting files, which then live exactly as long as the application – allowing other applications to be aware of the first app’s lifecycle without needing to look at process maps and such.

—END—

Advertisements
相册 | 此条目发表在技术杂项分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s