fsutil hardlink doesn't work?

3.6k views Asked by At

I was looking for a way to create hard links under Windows and I found this page: http://technet.microsoft.com/en-us/library/cc788097.aspx

To try it out, I created a file (1.txt) on the root of my C: drive with 100 lines of the following content:

C:\1.txt (2.598 bytes):

test test test test test

Then I open the command prompt and typed:

fsutil hardlink create C:\2.txt C:\1.txt

Success, 2.txt was created but when I go to see it's size it has exactly 2.598 bytes and also noticed some strange behaviours (as far as my understanding of hard links goes):

  1. If I delete 2.txt (the hard linked file) 1.txt is not deleted, and vice-versa.
  2. If I open 2.txt after I delete 1.txt (the original file) the content is still the same.

How does the fsutil hardlink create command differs from the copy command? And how can I create a true hard link under Windows?

I'm using Windows XP SP 3, and my file system is NTFS.

3

There are 3 answers

0
Adam Rosenfield On BEST ANSWER

That's exactly the behavior that supposed to happen. Are you sure you understand how hard links work? Hard links are really just multiple entries in the file system that refer to the exact same file. If you create a hard link to a file and modify one of the instances of the file, the other file will show up with your changes because they both refer to the same blocks of data on disk.

When you delete one of the hard links, you're really just deleting one of the file system entries referring to that file. The file doesn't get deleted until you delete all of its hard links (including the original one). After you delete 1.txt, 2.txt still exists and refers to the same file that was originally there, only now there is only a single file system entry referring to that file (namely, 2.txt). 2.txt is that file.

1
paxdiablo On

That is a true hard link. In UNIX (I'm not familiar with the Windows variant) a file is just a set of data on the disk (simplistically). It's the directory entry itself that gives that file a name.

When you have two directory entries pointing at the same underlying file, deleting one of them does not delete the file unless it's the last directory entry attached to it (and the file is not still open by a process, but that's a different matter).

4
SingleNegationElimination On

How does the fsutil hardlink create command differs from the copy command? And how can I create a true hard link under Windows?

After creating the hardlinked file, try editing either. You will discover that the changes are reflected in both files.

Try creating a hard-link of a very large file (say a video or disk-image). Notice that the available space on the disk has not changed (or not changed by more than a few bytes, if additional metadata had to be written to disk to reflect the new dir entry).