70

从两个方面来阐述下linux为什么不允许创建文件夹的硬链接

 6 years ago
source link: https://www.linuxprobe.com/csx-linux-wjjykj.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用方面考虑,会产生递归黑洞,举例:

文件夹 a,b,在a下面创建b文件夹得硬链接c,在b下面创建a文件夹的硬链接d,

ls a ,会看到c,ls c,看到b下的文件夹d,再ls d又看到c,这样可以无限ls下去

软连接在访问时readlink有递归次数的限制,硬链接就是普通inode,所以没办法记录递归次数

代码实现角度

代码实现方面应该是可以做到的。首先pwd不是由文件系统拼接路径的,而是记录在一个全局变量中,所以cd 。。 的时候只是需要获得上一级目录inode的属性来检查权限,或者显示属性信息。路径只需要简单运算就可以得到。

每个路径都对应一个dentry,即使是相同的inode,如果是不同path,那么dentry是不同的,dentry在内存中的hashtable是通过父目录的dentry来hash,而不是父目录inode,

path_walk的过程中,如果父目录是软连接,则会readlink找到被连接的父目录inode,再用该inode来获取名字空间。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK