1

I need to create hard links at runtime for file at paths longer than 255 characters (this is a workaround for an infuriating Excel/Word 2011 limitation). But since the same file may be opened later again, I don't want to recreate the hard link if I already have it at the path I created it the first time (I have a scheme to create such a -short- path using a UUID). Which means I need to check wether the file already 'cached' as a hard link is still indeed a hard link to the file I am opening for the user. So I need to check wether 2 paths are hard links to the same file. And I realize there is a potential race condition when testing for this, but the hard links are entirely managed by my app.

charles
  • 11,212
  • 3
  • 31
  • 46

2 Answers2

2

Here's the modern way to do it:

NSError* error;
id fileID1, fileID2;
if (![url1 getResourceValue:&fileID1 forKey:NSURLFileResourceIdentifierKey error:&error])
    /* handle error */;
if (![url2 getResourceValue:&fileID2 forKey:NSURLFileResourceIdentifierKey error:&error])
    /* handle error */;
if ([fileID1 isEqual:fileID2])
    /* URLs point to the same file (inode) */;

NSURLFileResourceIdentifierKey is exactly made for this purpose.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
0

I found a solution:

NSDictionary *attr1 = [[NSFileManager defaultManager] attributesOfItemAtPath:url1.path error:NULL];
NSDictionary *attr2 = [[NSFileManager defaultManager] attributesOfItemAtPath:url2.path error:NULL];

NSLog(@"ino1: %@", attr1[NSFileSystemFileNumber]);
NSLog(@"ino2: %@", attr2[NSFileSystemFileNumber]);

 NSLog(@"fs1 : %@", attr1[NSFileSystemNumber]);
 NSLog(@"fs2 : %@", attr2[NSFileSystemNumber]);

If ino1 and ino2 are the same, and fs1 and fs2 are the same, the inode is the same, so the files are hard links:

BOOL hardLInks = [ino1 isEqual:ino2] && [fs1 isEqual:fs2];
charles
  • 11,212
  • 3
  • 31
  • 46
  • 1
    Inodes (`NSFileSystemFileNumber`) are only unique on a given volume. You should also compare the volume number, given by `NSFileSystemNumber`, to be absolutely sure you have the same file. – CRD May 23 '14 at 17:30