取消链接究竟是如何工作的? [英] How exactly does unlink work?

查看:22
本文介绍了取消链接究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点难以理解这究竟是如何运作的.

I'm having a little difficulty understanding how exactly this works.

看起来 unlink() 会删除引用文件数据的 inode,但实际上不会删除数据.如果是这样,

It seems that unlink() will remove the inode which refers to the file's data, but won't actually delete the data. If this is the case,

a) 数据会发生什么变化?大概它不会永远存在,否则人们会一直耗尽磁盘空间.是否有其他东西最终会在没有关联 inode 的情况下删除数据,或者什么?

a) what happens to the data? Presumably it doesn't stick around forever, or people would be running out of disk space all the time. Does something else eventually get around to deleting data without associated inodes, or what?

b) 如果数据没有任何变化:我如何才能真正删除它?如果某些事情自动发生在它身上:我怎样才能根据命令做到这一点?

b) if nothing happens to the data: how can I actually delete it? If something automatically happens to it: how can I make that happen on command?

(辅助问题:如果 shell 命令 rmunlink 基本上做了同样的事情,就像我在这里读到的其他问题一样,而 Perl unlink 只是对它的另一个调用,那么像 File::Remove 这样的模块有什么意义,它似乎又做了同样的事情?我意识到有不止一种方法可以do it",但这似乎是以多种方式 it"的情况,it"总是指相同的操作.)

(Auxiliary question: if the shell commands rm and unlink do essentially the same thing, as I've read on other questions here, and Perl unlink is just another call to that, then what's the point of a module like File::Remove, which seems to do exactly the same thing again? I realize "there's more than one way to do it", but this seems to be a case of "more than one way to say it", with "it" always referring to the same operation.)

简而言之:我能否确保删除文件确实会立即释放其磁盘空间?

In short: can I make sure deleting a file actually results in its disk space being freed up immediately?

推荐答案

磁盘上的每个 inode 都有一个引用计数——它知道有多少地方引用了它.目录条目是一个参考.可以存在对同一 inode 的多个引用.unlink 删除引用.当引用计数为零时,inode 不再使用并且可能会被删除.这就是许多事情的工作方式,例如硬链接和快照.

Each inode on your disk has a reference count - it knows how many places refer to it. A directory entry is a reference. Multiple references to the same inode can exist. unlink removes a reference. When the reference count is zero, then the inode is no longer in use and may be deleted. This is how many things work, such as hard linking and snap shots.

特别是 - 一个打开的文件句柄是一个参考.所以你可以打开一个文件,取消它的链接,然后继续使用它——它只会在文件句柄关闭后被实际删除(假设引用计数降为零,并且它没有在其他任何地方打开/硬链接).

In particular - an open file handle is a reference. So you can open a file, unlink it, and continue to use it - it'll only be actually removed after the file handle is closed (provided the reference count drops to zero, and it's not open/hard linked anywhere else).

这篇关于取消链接究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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