简体   繁体   English

如何使用 WinApi / USN / MasterFileTable 为所有磁盘中的所有文件拥有唯一 ID?

[英]How to have a unique ID for all files in all disks, using WinApi / USN / MasterFileTable?

I'm using the NTFS MasterFileTable / USN journal of a few disk/partitions (C:, D:, E:, F:, etc), and I'd like to use a unique ID for each file/directory.我正在使用几个磁盘/分区(C:、D:、E:、F: 等)的 NTFS MasterFileTable/USN 日志,我想为每个文件/目录使用唯一的 ID。

While I'm reading the USN_RECORD (also called PUSN_RECORD), there is this int64:当我正在阅读USN_RECORD (也称为 PUSN_RECORD)时,有一个 int64:

DWORDLONG     FileReferenceNumber;

that is a unique file/directory identifier, unique at least in the current partition.这是一个唯一的文件/目录标识符,至少在当前分区中是唯一的。

But there could be collisions:但可能会发生冲突:

  • a file in C: could have FileReferenceNumber 1932847 C: 中的文件可能有 FileReferenceNumber 1932847
  • another file in D: could have FileReferenceNumber 1932847 too! D: 中的另一个文件也可以有 FileReferenceNumber 1932847!

I'd like to avoid having to use such a big thing as an int128 (that would be the 64 bits of FileReferenceNumber + 5 bits for the drive letter C:, D:, E:, ..., Z:).我想避免使用像int128这样大的东西(这将是FileReferenceNumber的 64 位 + 驱动器号 C:、D:、E:、...、Z: 的 5 位)。

I also would like to avoid having to use a pair (char DriveLetter, DWORDLONG FileReferenceNumber) to identify a file in the computer.我还想避免使用一对(char DriveLetter, DWORDLONG FileReferenceNumber)来识别计算机中的文件。

How to use a 64-bit int to code FileReferenceNumber + drive letter?如何使用 64 位 int 来编码FileReferenceNumber + 驱动器号?

Is it possible because FileReferenceNumber has a few free unused bits?是否可能因为FileReferenceNumber有一些空闲的未使用位?

If not, how would you handle this?如果不是,你会如何处理?

You must use a pair of FileReferenceNumber/FileID and "volume something".您必须使用一对 FileReferenceNumber/FileID 和“volume something”。 You can mount a volume in a folder so you cannot really use the drive letter.您可以在文件夹中安装卷,这样您就无法真正使用驱动器号。

Ideally "volume something" is the volume GUID path but you can use the volume serial number if size is important.理想情况下,“volume something”是卷 GUID 路径,但如果大小很重要,您可以使用卷序列号。 Note: Not all volumes have a GUID.注意:并非所有卷都有 GUID。

For NTFS you can get it from GetFileInformationByHandle and build a 32-bit+64-bit pair.对于 NTFS,您可以从GetFileInformationByHandle获取它并构建一个 32 位 + 64 位对。 For ReFS you need GetFileInformationByHandleEx and build a 64-bit+128-bit pair.对于 ReFS,您需要GetFileInformationByHandleEx并构建一个 64 位 + 128 位对。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM