如何获取带有boost :: filesystem的inode? [英] How to get the inode with boost::filesystem?
问题描述
我要检测是否已经看到一个文件,并希望用唯一的标识。在Linux下,有inode编号和设备ID(参见 stat()
或 fstat()
)。我假设在Windows下我会找到类似的东西。
I want to detect if I saw a file already and would like to identify it with something unique. Under Linux there is the inode number together with the device id (see stat()
or fstat()
). I assume under Windows I would find something similar.
为了开始方便, boost :: filesystem
,例如我可以使用 boost :: filesystem :: recursive_directory_iterator
遍历目录树。 file_status
给我如果它是一个常规文件,但不是inode号。
To start easy, the boost::filesystem
offers convenient methods, e.g. I can use boost::filesystem::recursive_directory_iterator
to traverse the directory tree. The file_status
gives me if it is a regular file, but not the inode number.
是 boost :: filesystem :: equivalent()
采取两个路径。我想这也是最便携的设计。
The closest thing I found was boost::filesystem::equivalent()
taking two paths. I guess this is also the most portable design.
事情是,我想把inode数字到数据库中进行快速查找。我不能用这个功能,我必须调用等于()
所有的路径已经存在于数据库。
The thing is that I would like to put the inode numbers into a database to have a quick lookup. I cannot do this with this function, I would have to call equivalent()
with all paths already existing in the database.
由于可移植性原因,我的运气和提升不会提供此类信息吗?
Am I out of luck and boost will not provide me such information due to portability reasons?
(编辑)目的是在一次扫描期间通过硬链接检测重复项的文件夹树。 equivalent()
正是这样,但我必须做一个二次算法。
(edit) The intention is to detect duplicates via hardlinks during one scan of a folder tree. equivalent()
does exactly that, but I would have to do a quadratic algorithm.
推荐答案
stat
的Windows CRT实现始终为inode使用零,因此您必须自己滚动。这是因为在Windows上 FindFirstfile
比 GetFileInformationByHandle
更快,因此 stat
使用 FindFirstFile
,它不包括inode信息。如果你不需要inode,那是伟大的,性能胜利。
The Windows CRT implementation of stat
always uses zero for the inode, so you will have to roll your own. This is because on Windows FindFirstfile
is faster than GetFileInformationByHandle
, so stat
uses FindFirstFile
, which does not include the inode information. If you don't need the inode, that's great, performance win. But if you do, the following will help.
相当于INODE的NTFS是MFT记录号,也称为文件ID。它有稍微不同的属性,但在一个误差范围内可以用于与INODE相同的目的,即标识两个路径是否指向同一个文件。
The NTFS equivalent to the INODE is the MFT Record Number, otherwise known as the file ID. It has slightly different properties, but to within a margin of error can be used for the same purposes as the INODE, i.e. identifying whether two paths point to the same file.
您可以使用 GetFileInformationByHandle
或 GetFileInformationByHandleEx
检索此信息。您将首先调用 CreateFile
以获取文件句柄。
You can use GetFileInformationByHandle
or GetFileInformationByHandleEx
to retrieve this information. You will first have to call CreateFile
to obtain the file handle.
- 您需要
FILE_READ_ATTRIBUTES
权限才能获取文件ID。 - 您应指定
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE
- c> OPEN_EXISTING 作为处置。
- You need
FILE_READ_ATTRIBUTES
rights only to get the file ID. - You should specify
FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE
- You should specify
OPEN_EXISTING
as the disposition.
一旦你有句柄,使用GetFileInformation函数获取文件ID,然后关闭句柄。
Once you have the handle, use one of the GetFileInformation functions to obtain the file ID, then close the handle.
您需要的信息可在 BY_HANDLE_FILE_INFORMATION
nFileIndexLow
和 nFileIndexHigh
成员或如果使用ReFS,则可能正在使用128位文件ID。要获得此功能,您必须使用更新的功能。
This information you need is available in the BY_HANDLE_FILE_INFORMATION
nFileIndexLow
and nFileIndexHigh
members or if ReFS is in use, then a 128 bit file ID may be in use. To obtain this you must use the updated function.
这篇关于如何获取带有boost :: filesystem的inode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!