是否有任何功能可以检索与inode关联的路径? [英] Is there any function to retrieve the path associated with an inode?

查看:87
本文介绍了是否有任何功能可以检索与inode关联的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个实用程序,该实用程序可以在Mac OS X(10.6及更高版本)上遍历目录树,并尝试检测自上次将目录与备份位置同步以来发生的更改.

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] */

遇到新文件或文件夹时,我首先使用inode编号进行数据库查找以找到原始文件(如果有).

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.

但是,如果文件已从父目录移到子目录,并且我试图检测对父目录的更改,我希望能够使用保存的inode编号来标识新路径这样我就可以区分移动和删除.

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.

推荐答案

inode编号仅在文件系统内唯一,因此至少需要设备 inode编号来标识文件.

inode numbers are only unique within a filesystem, so you need at least device and inode number to identify a file.

在HFS +文件系统上,inode编号实际上与"Macintosh File Id"相同,并且有一个特殊的"/.vol"文件系统,可让您按设备和inode查找目录.

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 ......

如您所见,234881029是该文件系统中"/Volumes/Data"的设备号,342711是该文件系统中"tmpwork/test20/test20.xcodeproj"的索引号,并且

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

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

直接将您转移到该文件夹​​.现在,您可以使用getcwd()确定该文件夹的真实路径.

transferred you directly to that folder. You could now use getcwd() to determine the real path to that folder.

"/.vol"文件系统记录在旧版

The "/.vol" filesystem is documented in the legacy Technical Q&A QA1113.

免责声明:我仅在OS X 10.7上尝试过此操作,并且我相当确定它可以在较旧的系统上运行.我不知道您是否可以在将来的OS X版本中依赖此功能.而且它非常特定于HFS.

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.

这篇关于是否有任何功能可以检索与inode关联的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆