Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Hard links to directories

On unix systems, you aren't supposed to be able to hard link to a directory. If I try, I get this:

<pre>% ln foo bar
ln: foo: Is a directory</pre>


But Apple's "Time Machine" backup apparently is able to do this, according to Arse Technica.

How?

G5 2x2.5 GHZ and a few others, Mac OS X (10.5.1)

Posted on Nov 30, 2007 9:26 AM

Reply
Question marked as Best reply

Posted on Nov 30, 2007 10:49 AM

I was wondering the same thing a couple of weeks ago. I still don't know if there is anything preinstalled that lets you make hard links to directories, but I did find some code for a little c programme here:
http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/ index.html

The relevant part is down near the bottom, "hlink.c". I haven't tested it extensively, but it seems to work.

Or did I misunderstand the question? If by asking "how" you meant how is it possible when on unix systems you aren't supposed to be able to, then I don't know...
3 replies
Question marked as Best reply

Nov 30, 2007 10:49 AM in response to Bill Scott

I was wondering the same thing a couple of weeks ago. I still don't know if there is anything preinstalled that lets you make hard links to directories, but I did find some code for a little c programme here:
http://www.mactech.com/articles/mactech/Vol.23/23.11/ExploringLeopardwithDTrace/ index.html

The relevant part is down near the bottom, "hlink.c". I haven't tested it extensively, but it seems to work.

Or did I misunderstand the question? If by asking "how" you meant how is it possible when on unix systems you aren't supposed to be able to, then I don't know...

Dec 3, 2007 4:59 AM in response to biovizier

look at the following kernel source code (xnu-1228)

from: bsd/vfs/vfs_syscalls.c:
int
link(__unused proc_t p, struct link_args *uap, __unused register_t *retval)
{
.
.
.
/*
* Normally, linking to directories is not supported.
* However, some file systems may have limited support.
*/
if (vp->v_type == VDIR) {
if (!(vp->v mount->mnt_vtable->vfcvfsflags & VFC_VFSDIRLINKS)) {
error = EPERM; /* POSIX */
goto out;
}

check HFS code:

from: "bsd/hfs/hfs_vfsops.c"
if (!(hfsmp->hfs_flags & HFS_STANDARD)) {
/* Tell VFS that we support directory hard links. */ <<<<<
mp->mnt vtable->vfcvfsflags |= VFC_VFSDIRLINKS;

Dec 3, 2007 5:57 AM in response to Bill Scott

There is nothing that would stop a filesystem implementation from supporting directory hard-links. However, this feature can easily lead to undetectable loops that could send path look-ups into infinite spins, and is therefore considered harmful and usually forbidden in general purpose filesystems.

TimeMachine makes use of directory hard-links for space efficiency reasons. Remember, it brings forward unchanged files into the most recent backup by hard-linking them. If you have large directories full of unchanged files creating a new directory file for every hourly backup and populating it with hard-links for all the files in there is vastly less efficient than simply creating a hard-link for the directory.

In general, directory hard-links are a dangerous feature, but under controlled circumstances (like TimeMachine use) lifting the ban can prove to be beneficial.

Cheers
Steffen.

Hard links to directories

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.