Is there any function to get the inode related path?

I am writing a utility that scans the directory tree on Mac OS X (10.6 and higher) and tries to detect changes that have occurred since the last synchronization of the directory with the backup location.

When I initially synchronize files and folders, I get the inode number and save it in the database entry for this file or folder:

NSString *oldFilePath = /* ... */; NSError *error = nil; NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:oldFilePath error:&error]; /* set database record for oldFilePath to [attributes fileSystemFileNumber] */ 

When I come across a new file or folder, I first search the database using the inode number to find the source file, if any.

But in the case where the file has moved from the parent directory to a subdirectory, and I'm trying to detect changes in the parent directory, I would like to be able to use the stored inode number to determine the new path so that I can distinguish between moving and deleting.

+2
source share
4 answers

inodes are unique only in the file system, so at least the device and inode numbers are required to identify the file.

On the HFS + file system, the inode number is actually identical to the “Macintosh File Identifier”, and there is a special “/.vol” file system that allows you to find the directory using the device and inode.

Example:

 $ cd /.vol/234881029/342711 $ pwd /Volumes/Data/tmpwork/test20/test20.xcodeproj $ stat . 234881029 342711 drwxr-xr-x 5 martin staff 0 170 ...... 

As you can see, 234881029 is the device number "/ Volumes / Data", 342711 is the inode number "tmpwork / test20 / test20.xcodeproj" inside this file system and

 cd /.vol/<deviceNo>/<inodeNo> 

transferred you directly to this folder. Now you can use getcwd() to determine the real path to this folder.

The "/.vol" file system is documented in the obsolete Technical Q & A QA1113 .

Disclaimer: I have only tried this on OS X 10.7, and I'm sure it works on older systems. I have no idea if you can rely on this feature in future versions of OS X. It is also very specific to HFS.

+4
source

On a Mac, the GetFileInfo command performs a reverse lookup of index numbers.

 GetFileInfo /.vol/234881029/344711 

Should be made:

 file: "/path/to/file" ... 

Martin R only responds to directories.

+6
source

On Unix-like systems, many file names can refer to the same index, so you have to look for the file system. I don’t know if MacOS has a shortcut.

+1
source

Note that, as explained above, the /.vol/'magic 'directory needs a device identifier for the volume and inode of the directory or file. You can get the volume device identifier as the first number returned by stat , as described in another answer here .

 # stat returns device ID as '234881026' and confirms inode is '32659974' ~$ stat /Volumes/Foo 234881026 32659974 lrwxr-xr-x 1 root admin 0 1 ... /Volumes/Foo # access file using ./vol/<device ID>/<inode> ~$ cd /.vol/234881026/1017800 :../Prague 2011 March$ ~$ GetFileInfo /.vol/234881026/1017800/IMG_3731.JPG file: "/Users/roger/Pictures/Prague 2011 March/IMG_3731.JPG" 
0
source

Source: https://habr.com/ru/post/953841/


All Articles