读书人

为啥linux的硬连接(hard link)不能指向

发布时间: 2013-08-01 15:23:18 作者: rapoo

为什么linux的硬连接(hard link)不能指向目录

So why can du deal with symlinks easily and not hard links? We were able to see above that hard links are indistinguishable from normal directory entries. Symlinks however are special, detectable, and skippable! Du notices that the symlink is a symlink, and skips it completely!

% ls -l total 4drwxr-xr-x  3 danny  staff  102 Oct 13 18:14 test1/lrwxr-xr-x  1 danny  staff    5 Oct 13 18:13 test2@ -> test1% du -ah242M    ./test1/bigfile242M    ./test14.0K    ./test2242M    .

With the exception of mount points, each directory has one and only parent:?...

One way to do?pwd?is to check the device:inode for '.' and '..'. If they are the same, you have reached the root of the file system. Otherwise, find the name of the current directory in the parent, push that on a stack, and start comparing '../.' with '../..', then '../../.' with '../../..', etc. Once you've hit the root, start popping and printing the names from the stack. This algorithm relies on the fact that each directory has one and only one parent.

If hard links to directories were allowed, which one of the multiple parents should?..?point to? That is one compelling reason why hardlinks to directories are not allowed.

Symlinks to directories don't cause that problem. If a program wants to, it could do an?lstat()?on each part of the pathname and detect when a symlink is encountered. The?pwd?algorithm will return the true absolute pathname for a target directory. The fact that there is a piece of text somewhere (the symlink) that points to the target directory is pretty much irrelevant. The existence of such a symlink does not create a loop in the graph.

读书人网 >UNIXLINUX

热点推荐