Hardlinks and Junction points on Win2K

Discuss various technical topics not related to Mozilla.
Post Reply
User avatar
asquithea
Posts: 1533
Joined: March 17th, 2003, 2:43 pm
Location: Guildford, UK

Hardlinks and Junction points on Win2K

Post by asquithea »

Ever wanted to make a files accessible from multiple locations? Windows 2000 and upwards have the ability to do just that.

Hardlinks are file-system level directory entries -- thus each file has at least one hardlink. To create a new hardlink, the syntax is:

Code: Select all

fsutil hardlink create <linkName> <targetName>
This is obviously a handful to type, but doskey comes in handy to shorten it.

I often use hardlinks to create multiple filenames in the same directory. For example, when testing my xhtml files, my webserver serves them as application/xhtml+xml if the file has a .xhtml extension, and as text/html when the extension is .html. I can use hardlinks to save effort when I'm testing in Mozilla and Internet Exploiter (which doesn't understand the xhtml mimetype);

Code: Select all

fsutil hardlink create testFile.xhtml testFile.html
...does the trick. I can edit the .html file quite happily and refresh both browsers without any screwing about copying and renaming files.

Hardlinks can only be used to link between files, however, which is a bit of a bummer if you've got a directory full of files to link. Junction points to the rescue! Windows 2000 doesn't have the necessary kit to create reparse points (on which junctions are based) out of the box, but there are freeware utilites that will do the job perfectly, like this one. Put it in System32 for convenience. The syntax is easy:

Code: Select all

junction <hostDir> <targetDir>
The host directory must be empty, if it exists.

I could use a junction point to share my divX files, like so:

Code: Select all

junction "E:\DivX Files" "E:\Documents and Settings\My Username\My Documents\My Videos"
Listing root E:\ shows the "DivX Files" entry as a <JUNCTION> rather than a <DIR>. You can delete the junction point using the same utility. Be careful not to delete the junction point using the DEL command or Windows Explorer, or chaos will start to break out. If you do make this mistake, just delete the host directory, and all should be well again.

Hardlinks and reparse points are fairly similar. Both are completely transparent to applications, so all the erratic behaviour that you get with Shortcuts is gone. However, whilst with a hardlink, a file is not deleted until all the hardlinks are gone, with a junction, the files are gone as soon as the junction point is deleted, and all your junctions that pointed there will be left dangling. That's a gotcha and a half, so be careful, and since deletions and modifications to a junction are mirrored at the junction point, set ACLs appropriately to safeguard your data.
Pikhq
Posts: 61
Joined: August 14th, 2003, 1:31 pm

Post by Pikhq »

In essence, Win 2000/XP has finnally gotten this feature a few decades after UNIX had it. Win 2000/XP has yet to get the softlink feature, the hardlink feature on directorys ( junction point sounds like softlink for dirs ), some slightly better LNF support, etc.
Mozilla Advocate
User avatar
asquithea
Posts: 1533
Joined: March 17th, 2003, 2:43 pm
Location: Guildford, UK

Post by asquithea »

Pikhq wrote:In essence, Win 2000/XP has finnally gotten this feature a few decades after UNIX had it. Win 2000/XP has yet to get the softlink feature, the hardlink feature on directorys ( junction point sounds like softlink for dirs ), some slightly better LNF support, etc.


Basically, yep. File systems were never really a strong point for windows, but the newer NTFS is pretty sweet in lots of ways. I'm kinda wondering why it isn't possible to hardlink directories on NTFS -- I would have thought that it ought to have been reasonably straightforward to implement.

Anyway, I posted this with the vague idea that it might be interesting to someone who found the various limitations of earlier windows filesystems frustrating -- I'm well aware that Unix-like systems have had this for years. I was glad when I found the data on junction points, at least, although I can't really figure out why M$ only puts a tool to create them in the Win2K resource kit. Maybe they think we'll break things... *sigh*
scragz
Posts: 2914
Joined: February 8th, 2003, 2:38 am
Location: San Diego, US
Contact:

Post by scragz »

What exactly is the difference between soft and hard links (no Viagra jokes please).
User avatar
GNU/Ben
Posts: 1557
Joined: November 5th, 2002, 1:45 pm
Location: 127.0.0.1
Contact:

Post by GNU/Ben »

Short answer: A soft link points to the path+filename, a hard link points to the inode.
"When you say 'I wrote a program that crashed Windows', people just stare at you blankly and say 'Hey, I got those with the system, *for free*'."
-- Linus Torvalds
Gentoo: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.5) Gecko/20041210 Firefox/1.0
Reason
Posts: 57
Joined: October 13th, 2003, 2:41 am
Contact:

Post by Reason »

Also, if I remember, if you delete a file that is softlinked the softlink will still remain, but if you delete a file that is hardlinked then the hardlink is also deleted.

You also have to be careful with hardlinks on NTFS because some software (defraggers, virus scanners etc.) can have problems with them and can get stuck in recursive loops.
User avatar
GNU/Ben
Posts: 1557
Joined: November 5th, 2002, 1:45 pm
Location: 127.0.0.1
Contact:

Post by GNU/Ben »

Well, I cannot speak for NTFS systems, but that's not the case on my Linux box. Only if all the hardlinks (all files you see are really hardlinks, even the "origional") to the inode are deleted, is the file actually gone. You can even delete the origional hardlink and the inode + other hardlinks will still exist.

Also, deleting a symbolic link does not delete the the file it is linked to.
"When you say 'I wrote a program that crashed Windows', people just stare at you blankly and say 'Hey, I got those with the system, *for free*'."
-- Linus Torvalds
Gentoo: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.5) Gecko/20041210 Firefox/1.0
User avatar
Thumper
Posts: 8037
Joined: November 4th, 2002, 5:42 pm
Location: Linlithgow, Scotland
Contact:

Post by Thumper »

edit: ignore me.
Last edited by Thumper on November 14th, 2003, 6:17 am, edited 1 time in total.
User avatar
asquithea
Posts: 1533
Joined: March 17th, 2003, 2:43 pm
Location: Guildford, UK

Post by asquithea »

GNU/Ben wrote:Well, I cannot speak for NTFS systems, but that's not the case on my Linux box. Only if all the hardlinks (all files you see are really hardlinks, even the "origional") to the inode are deleted, is the file actually gone. You can even delete the origional hardlink and the inode + other hardlinks will still exist.

Also, deleting a symbolic link does not delete the the file it is linked to.

That's the way it works on NTFS as well. If you have two hardlinks to a file, and delete one of them, the file still exists.

If you delete a softlink (an explorer shortcut is the nearest analogue on a windows box, I think), the file still exists, and if you delete the file, the softlink still exists, but doesn't point anywhere useful.
Reason
Posts: 57
Joined: October 13th, 2003, 2:41 am
Contact:

Post by Reason »

asquithea wrote:That's the way it works on NTFS as well. If you have two hardlinks to a file, and delete one of them, the file still exists.

Perhaps I wasn't clear, but on NTFS it depends how you delete the file :) The reason Microsoft never officialy supported hardlinks/junctions points in Win2K (and barely mentions them in XP) was that many Windows programs would delete the original file when trying to delete a hardlink.

Microsoft alude to this problem when they mention it in this support article :
Never delete a junction point by using Explorer, a del /s command, or other file system utilities that walk recursively into directory trees. These utilities affect the target directory and all subdirectories.


Sorry to be unclear, but the point I was trying to make is that you have to be very careful using these features on NTFS, as many Windows aps aren't 'hardlink' aware. For instance, Norton AV explicitly mentions in it's readme file that hardlinks can cause major problems for it. Very few Windows aps are tested against hardlinks, so be careful...
User avatar
asquithea
Posts: 1533
Joined: March 17th, 2003, 2:43 pm
Location: Guildford, UK

Post by asquithea »

AFAIK all you have to be careful not to do is to create a cycle of hardlinks. I can see that would screw up a lot of programs, but chkdsk will check for cycles when run if you're having problems. If you don't create a cycle, then applications won't have problems.

When I read the M$ warning originally, I couldn't resist finding out what happens if you delete a junction point. Answer: bugger all. If you delete the junction (the link part), all is well, and the target directory is unaffected. If you delete the target directory, then things go a little odd (as I originally mentioned), because the junction target is no longer valid. But all that happens is you get an error message "The system cannot find the file specified", just as if you had typed the path incorrectly. To make that clear; the junction (or directory shortcut if you prefer) is still visible, but it isn't pointing to anything anymore. You can simply delete the junction to set things aright again.

To avoid problems with junctions, it's best to delete them using the same utility with which they were created. fsutil will do this too;

Just for fun, try running

Code: Select all

fsutil reparsepoint query <junction name>
against a junction. It displays a hex listing and you can see from the ascii representation that the junction reparse data quite literally points to a path - hence the "The system cannot find the file specified" error if you delete the target. You can delete the junction point using

Code: Select all

fsutil reparsepoint delete <junction name>
Post Reply