Is there any function to retrieve the path associated with an NSFileSystemFileNumber?

安稳与你 提交于 2020-01-07 04:22:42

问题


I am writing a utility that walks a directory tree on Cocoa 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 NSFileSystemFileNumber 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 NSFileSystemFileNumber to find the original file, if any.

But in the case where a file has edited and 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 NSFileSystemFileNumber to identify the new path so that I can distinguish between a move and a delete and updated.

Any alternate solutions to solve out this problem?

Any help is appreciated..!!


回答1:


There is no direct way to do this. The NSFileSystemFileNumber is just the file's inode. There can me multiple directory entries, in multiple directories, to the same inode - i.e. a file can appear in more than one place, and have different names in those places. So a mapping from inode to path is not one-to-one, it may produce a collection of paths.

The typical solutions you will find to your problem are using the command find(1), or library functions such as the fts family, to walk the file system and produce a mapping. However for your situation it sounds like your database should be searchable by inode number, something you are already storing in the records, so you don't need to walk the tree to produce a mapping each time.

HTH



来源:https://stackoverflow.com/questions/30191016/is-there-any-function-to-retrieve-the-path-associated-with-an-nsfilesystemfilenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!