Home » Blog
date 19.Jan.2025

■ Preserve hard link integrity when saving files


Hard links are a smart and space-efficient way to place the same file in multiple folders (on the same NTFS partition). For example I place the skin file X2SKIN.PNG in 5 different locations to cater for various installers and debugging needs. See the pic below how the file Links column shows 5, the number of hard links associated with this particular file:

sample hard links

To see all hard links, send the file to a scrap container then use Tools > Find targets menu command.

So I have a "master" file and 4 shadow copies of it spread with hard linking. When I make changes to one file (it doesn't have to be the "original", any file will do) then the contents of the remaining 4 links are immediately updated. Unlike symbolic links, hard links are immune to relocations and renaming. Sweet!

Whilst magnificent in theory, the reality is that lots of programs destroy hard linkage when saving files. Paint.NET the editor I use to manage xplorer² skins is a culprit. If you modify and save one of the 5 X2SKIN.PNG files in our example, you will find that the new version has a link count of 1, and the rest drop down to 4; saving has messed up the connection, which defeats the purpose of using hard links in the first place!

Paint.NET uses an arcane "safe" save mechanism where it first saves to a temp file, deletes the original file if all is well, then renames the temp file with the original name — hence explaining the unlinking of the modified file

There is a workaround. If you know your editor is careless with hard links (Paint.NET, Visual studio, the list is long) follow these steps when you want to modify a file:

  1. Open the file and edit it
     
  2. Instead of direct saving, use Save As command and write the file to a different folder (e.g. on the Desktop)
     
  3. Copy the modified file and <CTRL+V> Paste it in a folder that contains the original

In this roundabout way, the link count will remain unmolested. Not perfect but a tolerable hassle if you only modify files infrequently. Alternatively, find another editor program that is link-friendly — or forget about hard links and use symbolic links for the "shadow copies" instead.

ps. the internal <F4> editor² is careful with hard links, so it's safe to edit text documents with it.

Post a comment on this topic »


©2002-2025 ZABKAT LTD, all rights reserved | Privacy policy | Sitemap