[OT] Hardlinks and directories
vvmarko at gmail.com
Sat Feb 13 13:44:28 UTC 2010
On Saturday 13 February 2010 06:30:55 jdow wrote:
> From: "Marko Vojinovic" <vvmarko at gmail.com>
> > Deleting directories is a textbook example. In order to delete a
> > directory,
> > you first have to delete all files and subdirectories that it contains,
> > and once
> > it is empty, delete the directory itself. So deletion goes on via a
> > recursive
> > algorithm:
> > 1) check whether there are files or dirs in target dir
> > 2) delete any files present
> > 3) execute yourself (from step 1) for every subdir as target dir
> > 4) target dir is now empty, unlink it
> > 5) done
> > Now consider the directory structure of the form where inside dir a/ you
> > have
> > dir b/, and inside it dir c/ which is a hard link back to a/ (this is a
> > simplest loop situation, much more complicated are possible). IOW, the
> > directory a/ contains itself as a subdirectory two levels down. Now
> > execute
> > the above algorithm in order to delete a/ --- the algorithm will try to
> > delete
> > all subdirectories, namely b/, and execute itself over b/ in step 3. But
> > to
> > delete b/, it needs to execute itself over c/ which is actually a/. But
> > to delete a/ it needs to execute itself over b/...
> It's even worse than that, Marko.
> You have a directory tree /a/b/c/d. You create a hard link to directory
> /a/b inside of d. You get /a/b/c/d/b/c/d/b/c/d....
> NOW you unlink /a/b. Now when you do an ls on /a you get . and .. only.
> Now, I challenge you to delete the /b/c/d loop and everything it contains
> without reformatting the disk or using low level disk edit functions.
I'm not sure I understand your point. The system will not allow you to unlink
/a/b before it is empty. And it never is, since it contains a loop to itself.
OTOH, if you manage to persuade the kernel to remove it regardless, then yes,
the b/c/d loop gets cut off from the rest of the filesystem, and there is no way
to claim back that disk space.
But unlinking nonempty directories leaks disk space even if there are no loops
inside, so you always need to have the "unlink only empty dirs" rule
implemented in the kernel. This is analogous to losing a pointer before
free()-ing it in, say, C --- memory leaks. :-)
More information about the users