Is there any function to retrieve the path associated with an inode?
I am writing a utility that walks a directory tree on Mac OS X (10.6 and higher) and tries to detect changes that have occurred since the directory was last synchronized with a back-up location.
When I initially synchronize the files and folders I obtain the inode number and store it in the database record for that 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 encounter a new file or folder I first do a database lookup using the inode number to find the original file, if any.
But in the case where a file has moved from a parent directory to a sub-directory, and I am trying to detect changes to the parent directory I would like to be able to use the saved inode number to identify the new path so that I can distinguish between a move and a delete.
4 Solutions Collect From Internet About “Is there any function to retrieve the path associated with an inode?”
inode numbers are only unique within a filesystem, so you need at least device and inode number to identify a file.
On the HFS+ file system, the inode number is in fact identical to the “Macintosh File Id”, and there is a special “/.vol” filesystem that allows you to find a directory by device and inode.
$ 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 of “/Volumes/Data”, 342711 is the inode number of “tmpwork/test20/test20.xcodeproj” within that filesystem, and
transferred you directly to that folder. You could now use
getcwd() to determine the real path to that folder.
The “/.vol” filesystem is documented in the legacy Technical Q&A QA1113.
Disclaimer: I tried this only on OS X 10.7 and I am fairly sure that it works on older systems. I have no idea if you can rely on this feature in future versions of OS X. Also it is very HFS specific.
On Mac the GetFileInfo command performs a reverse lookup of inode numbers.
file: "/path/to/file" ...
Martin R’s answer only works on directories.
On Unix-like systems, many filenames may reference the same inode, so you’d have to search the filesystem. I don’t know if MacOS provides a shortcut.
Note that, as explained above, the /.vol/ ‘magic’ directory needs the device ID for the volume, and the inode of the directory or file. You can get the device ID of the volume as the first number returned by
stat as explained in a different 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"
- UITextFields in UITableView, entered values reappearing in other cells
- Add Checkbox List to UIAlertController
- Cannot subscript a value of type ''
- Swift – Download a video from distant URL and save it in an photo album
- Do you need to delete imported files from Documents/Inbox?
- Objective-C SQLite join tables from multiple database
- getting an overview perspective of all methods in a class in Eclipse
- When would CBCentralManager's state ever be powered on but still give me a “not powered on” warning?
- How do I show multiple custom annotations (pin & left icon) loaded via plist?
- How does String.Index work in Swift
- Get an object properties list in Objective-C
- Generating ipa from xcode command-line
- Play/Pause with the same button
- self.navigationController.tabBarController.selectedIndex not working
- Extract path information from an black&white bitmap image