Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
237
rated 0 times [  237] [ 0]  / answers: 1 / hits: 300951  / 2 Years ago, mon, june 27, 2022, 11:10:14

I am using Ubuntu 12.04. When I try to create a hard link for any directory, it fails. I can create hard links for files inside file system boundary. I know the reason why we cannot create hardlinks for files beyond file system.


I tried these commands:


$ ln /Some/Directory /home/nischay/Hard-Directory
hard link not allowed for directory
$ sudo ln /Some/Directory /home/nischay/Hard-Directory
[sudo] password for nischay:
hard link not allowed for directory

I just want to know the reason behind this. Is it same for all GNU/Linux distros and Unix flavours (BSD, Solaris, HP-UX, IBM AIX) or only in Ubuntu or Linux?


More From » filesystem

 Answers
1

Directory hardlinks break the filesystem in multiple ways


They allow you to create loops


A hard link to a directory can link to a parent of itself, which creates a file system loop. For example, these commands could create a loop with the back link l:


mkdir -p /tmp/a/b
cd /tmp/a/b
ln -d /tmp/a l

A filesystem with a directory loop has infinite depth:


cd /tmp/a/b/l/b/l/b/l/b/l/b

Avoiding an infinite loop when traversing such a directory structure is somewhat difficult (though for example POSIX requires find to avoid this).


A file system with this kind of hard link is no longer a tree, because a tree must not, by definition, contain a loop.


They break the unambiguity of parent directories


With a filesystem loop, multiple parent directories exist:


cd /tmp/a/b
cd /tmp/a/b/l/b

In the first case, /tmp/a is the parent directory of /tmp/a/b.

In the second case, /tmp/a/b/l is the parent directory of /tmp/a/b/l/b, which is the same as /tmp/a/b.

So it has two parent directories.

Even without loops, multiple hardlinks to the same directory will create ambiguous parent directories.


They multiply files


Files are identified by paths, after resolving symlinks. So


/tmp/a/b/foo.txt
/tmp/a/b/l/b/foo.txt

are different files.

There are infinitely many further paths of the file. They are the same in terms of their inode number of course. But if you do not explicitly expect loops, there is no reason to check for that.


A directory hardlink can also point to a child directory, or a directory that is neither child nor parent of any depth. In this case, a file that is a child of the link would be replicated to two files, identified by two paths.


Your example


$ ln /Some/Directory /home/nischay/Hard-Directory
$ echo foo > /home/nischay/Hard-Directory/foobar.txt
$ diff -s /Some/Directory/foobar.txt /home/nischay/Hard-Directory/foobar.txt
Files /Some/Directory/foobar.txt and /home/nischay/Hard-Directory/foobar.txt are identical
$ echo bar >> /Some/Directory/foobar.txt
$ diff -s /Some/Directory/foobar.txt /home/nischay/Hard-Directory/foobar.txt
Files /Some/Directory/foobar.txt and /home/nischay/Hard-Directory/foobar.txt are identical
$ cat /Some/Directory/foobar.txt
foo
bar

How can soft links to directories work then?


A path that may contain softlinks and even soft linked directory loops is often used just to identify and open a file. It can be used as a normal, linear path.


But there are other situations, when paths are used to compare files. In this case, symbolic links in the path can be resolved first, converting it to a minimal, and commonly agreed upon representation creating a canonical path:


This is possible, because the soft links can all be expanded to paths without the link. After doing that with all soft links in a path, the remaining path is part of a tree, where a path is always unambiguous.


The command readlink can resolve a path to its canonical path:


$ readlink -f /some/symlinked/path

Soft links are different from what the filesystem uses


A soft link cannot cause all the trouble because it is different from the links inside the filesystem. It can be distinguished from hard links, and resolved to a path without symlinks if needed.

In some sense, adding symlinks does not alter the basic file system structure - it keeps it, but adds more structure like an application layer.




From man readlink:


 NAME
readlink - print resolved symbolic links or canonical
file names

SYNOPSIS
readlink [OPTION]... FILE...

DESCRIPTION
Print value of a symbolic link or canonical file name

-f, --canonicalize
canonicalize by following every symlink in
every component of the given name recursively;
all but the last component must exist
[ ... ]

[#34537] Tuesday, June 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shionnky

Total Points: 276
Total Questions: 104
Total Answers: 108

Location: Montenegro
Member since Thu, Jun 16, 2022
2 Years ago
shionnky questions
;