I've written an extension method for the System.IO.FileInfo
class to create hard-link and it goes like this:
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
private static extern bool CreateHardLink(string lpFileName, string lpExistingFileName, IntPtr lpSecurityAttributes);
public static void CreateHardLink(this FileInfo file, string destination) {
CreateHardLink(destination, file.FullName, IntPtr.Zero);
}
// Usage:
fileInfo.CreateHardLink(@".\hardLinkCopy.txt");
The method works fine, but I'd like to make some unit tests just for the sake of it. So how can I assert that a file x and another file y are linked to the same data?
I came up with some ways to test it:
- Check if data is consistent throughout changes Since creating a hard-link copy is just giving a second name to a file, any modification done to the first instance will be reflected on the second one, and vice-versa. If the data stay consistent between two files despite modification, it's safe to assume that these files are both hard-linked to the same data.
- Assert that the creation of an hard-link doesn't affect the parent folder's size. Since a hard-link copy doesn't copy any data on disk, the parent directory shouldn't get any heavier. If upon calling the method a new file is created with the same content as the original file, and the parent folder didn't change in size (or gain less than what a normal copy would do), the new file must be a hard-link copy.
However, these methods smell. There's got to be at least one build-in method somewhere in the OS to check if two files point to the same data on disk!
Anyone could share a lead?