[xplorer˛] — Write files with TLC
home » blog » 21 September 2008 [programming]

The "New Technology File System" (NTFS) has many tricks over the older FAT32 found in Win9x hard disks, like hard links and alternate data streams (ADS). Hard links save you disk space and ADS offers file comments (summary information) among other benefits. However many programs can destroy hard links and file comments when writing changes to a file.

It is not just programs made in some guy's basement that misbehave. Text editors written with microsoft's MFC most probably save documents using the little known CMirrorFile class, which instead of saving straight to the target filename, it creates a temporary file, writes in it, deletes the original and then renames the temporary to the actual filename. As you can imagine, any streams or hard link information in the original file is lost forever. WordPad and visual studio 6 are two known culprits.

editor˛, a fine notepad replacement included with xplorer˛, was always careful with hard links, since it uses straight CreateFile calls without mirror files (what's the point?). But I received reports that it ruined ADS when overwriting existing files with comments.

As it turns out the problem was with the dwCreationDisposition argument I was using with CreateFile API. For convenience I was passing CREATE_ALWAYS that doesn't care whether the file being written to exists or not, but nukes the old file in case it does exist, including any parallel streams — thus destroying the summary information stream.

The message is do not use CREATE_ALWAYS! A little extra effort is required to examine if the file we are about to write exists or not. If it does, then use TRUNCATE_EXISTING which will preserve ADS. If it doesn't then pass CREATE_NEW as the disposition parameter. Bug sorted.

Here is a patched version of editor˛ that doesn't harm file comments: Download build Unicode

Post a comment on this topic



What would you like to do next?

Reclaim control of your files!
  • browse
  • preview
  • manage
  • locate
  • organize
Download xplorer2 free trial
"This powerhouse file manager beats the pants off Microsoft's built-in utility..."

© 2002—2008 Nikos Bozinis, all rights reserved