以编程方式复制文件夹而不解析 Windows 中的硬链接(Win32 API)
我想复制整个文件夹而不解析硬链接
示例:(
Folder1
|
+---File1
File2
HardLink3 -> File3
使用 fsutil Hardlink create
或 mklink
创建的 HardLink3)
我想复制此文件夹以
Folder2
|
+---File1
File2
HardLink3 -> File3
将Folder2\HardLink3 保留为指向 File3 的硬链接
是否有 Win32 API 调用来复制整个文件夹 这个语义,或者,如果我需要执行 CopyFile / CreateHardLink 逐个文件,检查给定文件是否存在的 API 调用是什么 是否有硬链接?
I want to copy an entire folder without resolving the hardlinks
example:
Folder1
|
+---File1
File2
HardLink3 -> File3
(HardLink3 created using fsutil hardlink create
or mklink
)
I want to copy this folder to
Folder2
|
+---File1
File2
HardLink3 -> File3
keeping Folder2\HardLink3 as a hardlink pointing to File3
Is there an Win32 API call to copy a entire folder with
this semantic, or, if I need to do CopyFile / CreateHardLink
file by file, what's the API call to check if a given file is
a hardlink or not?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您绝对确定这就是您想要做的,那么确定文件是否具有多个链接(即“是硬链接”)的最简单方法可能是
GetFileInformationByHandle
。对于普通文件,返回的 nNumberOfLinks 值将为 1;如果文件是(或具有)硬链接,则返回值大于 1。
如果我正确理解了您的情况,那么检查文件是否硬链接到一组特定文件(“共享文件夹”中的文件)之一可能比检查文件是否硬链接到任何文件更明智任何地方。为此,请查看硬链接的文件 ID(
nFileIndexHigh
和nFileIndexLow
),该文件 ID 与原始文件的文件 ID 相同。在后一种情况下,作为优化,您可以将
GetFileInformationByHandleEx
与FileIdBothDirectoryInfo
选项结合使用,在单个操作中读取给定目录中所有文件的名称和文件 ID。If you're absolutely sure that this is what you want to do, the easiest way to determine whether a file has multiple links (i.e., "is a hard link") is probably
GetFileInformationByHandle
.The
nNumberOfLinks
value returned will be 1 for a normal file and more than 1 if the file is (or has) a hard link.If I've understood your scenario correctly, it might be more sensible to check whether a file is hard linked to one of a specific set of files (the files in the "shared folder") rather than whether it is hard linked to any file anywhere. To do this, look at the File ID (
nFileIndexHigh
andnFileIndexLow
) which for a hard link is the same as for the original file.In the latter case, as an optimization you could use
GetFileInformationByHandleEx
with theFileIdBothDirectoryInfo
option to read the names and file IDs for all the files in a given directory in a single operation.我认为没有 Win32 API 调用可以一次性完成您想要的所有操作,因此您可能需要手动执行此操作。
检查文件是否是硬链接可能不是您想要做的。如果文件不是符号链接、目录(或重新分析点或其他一些模糊的东西),它实际上是硬链接,即文件名指向磁盘上存储的文件。因此,如果两个文件指向相同的数据,它们都是该文件的硬链接。
无论如何,枚举文件的所有硬链接的 Win32 方法是 FindFirstFileNameW 和 FindNextFileNameW。
I do not think there is a Win32 API call to do what you want all in one go, so you probably need to do this by hand.
Checking if a file is a hard-link or not is probably not what you want to do. If a file is not a symbolic link, directory (or reparse point or some other obscure thing) it is actually a hard link, i.e. the name of the file points to a stored file on disk. So if two files are pointing to the same data they are both hard links to that file.
Anyway, the Win32 methods to enumerate all hard links to a file are FindFirstFileNameW and FindNextFileNameW.