为什么 UNIX/Linux 中不允许硬链接到目录

发布于 2024-12-08 21:38:29 字数 1459 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-12-15 21:38:29

不允许使用硬链接,因为它们会导致循环。一旦允许循环形成,您必须执行标记和清除垃圾收集,以检测何时可以最终删除目录的孤立循环(不再可从根访问) - 这在磁盘上非常昂贵。

软链接不会导致这个问题,因为它们不会增加目标目录的引用计数;因此,您仍然可以摆脱引用计数(最多一个引用:)。

另一个问题是遍历文件系统的程序(例如,find)需要避免循环。他们可以通过记住他们见过的每个索引节点号来做到这一点,但这很昂贵 - 如果他们能够区分可能导致循环的链接(即软链接)和不会导致循环的链接(普通目录条目),并跳过软链接,它们不再需要跟踪 inode 编号。

Hardlinks are not permitted because they would lead to cycles. Once you allow cycles to form, you must perform a mark-and-sweep garbage collection to detect when isolated cycles of directories (no longer reachable from the root) can be finally deleted - this is extremely expensive on disk.

Soft links do not cause this problem because they do not raise the reference count of the targeted directory; thus you can still get away with reference counting (with a maximum of one reference :).

The other issue is that programs which traverse the file system (eg, find) need to avoid cycles. They could do this by remembering every inode number they've seen, but this is expensive - if they can distinguish between links which could lead to cycles (ie, softlinks) and links which will not lead to cycles (normal directory entries), and skip the softlinks, they don't need to track inode numbers anymore.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文