Friday, February 13, 2009

Hard Links in Windows Do Not Update Immediately

I was pretty excited when I saw mklink appear. Filesystem links can solve a lot of annoying problems, which is why you see them used all over the place in Unixy systems. Although, like salt, the key is to use them sparingly.

Which is why I’m so annoyed that the Windows version is pretty much completely useless. Soft links are only recognized in some contexts, but hard links are the real offender. Try this from an elevated command prompt:

> cd C:\temp
> echo foo > a.txt
> mklink /h C:\temp\hard-a.txt C:\temp\a.txt
> more hard-a.txt
foo

So far, so good. Now try this:

> echo more >> a.txt
> more hard-a.txt
foo
> more a.txt
foo
more
> more hard-a.txt
foo
more

WTF? My hard link doesn’t update immediately? That sucks!

From experimentation, it seems that it only updates after you’ve actually accessed the link once. Since one of the things I’d like to do is hardlink files into my Dropbox synchronization directory, this is pretty much a non-starter – they’ll never appear to be updated, and therefore will never sync. Unless I visit every file, which is pointless, because if I was going to do that, I could just copy them. Which is what I’m trying to avoid.

I’m more than willing to believe I’m missing something here, but if so I don’t know what it is. My best guess is that this is a bug in the filesystem caching code (I’m on Windows 2008).

Days like this make me want to switch full-time to my Ubuntu machine. Also not perfect, but at least the novelty makes me less sensitive to its shortcomings.

5 comments:

  1. Wow, weird. Repro-d on Windows 7 also. Weird thing is that if I use TYPE to print the file instead of MORE, you see the up-to-date content.

    ReplyDelete
  2. Weird indeed. It's only more.com that behaves that way; if you use type or notepad, the correct content is displayed.

    I tried to understand what more.com does via Process Monitor, but I'm not sure I understand what's going on. According to procmon, more doesn't even read the file? It only does a QueryDirectory, CreateFile, QueryStandardInformationFile, and CloseFile.

    Type, on the other hand, causes cmd.exe to do a ReadFile (in addition), How does more.com get the file contents without a ReadFile operation? I guess therein lies the answer about why it won't show the most recent data.

    ReplyDelete
  3. mklink creates a Symbolic link, not a Hard link.

    That's an opposite entities.

    ReplyDelete
  4. There's a switch /h that controls which one you get, actually. You'll notice I'm specifying it, indicating I want a hard link. Symbolic links don't work for crap under windows.

    ReplyDelete
  5. Wonder if it's related to the fact that NTFS stores very small files in the MFT? Does it also happen with a large file and hard link ?

    ReplyDelete