[英]How to tell if folder is a subfolder for recursive folder copy?
I'm trying to implement a folder copy method that calls FindFirstFile
and FindNextFile
in a loop, that may call itself recursively on any subfolders. 我正在尝试实现一个在循环中调用
FindFirstFile
和FindNextFile
的文件夹复制方法,它可以在任何子文件夹上递归调用自身。
To prevent an obvious infinite loop I need to make sure that the destination folder is not a subfolder of a source folder. 为了防止明显的无限循环,我需要确保目标文件夹不是源文件夹的子文件夹。 The question is how to do that?
问题是如何做到这一点? My thinking was to translate a DOS path into a device specific path (need to find out how) but there seems to be more to it.
我的想法是将DOS路径转换为特定于设备的路径(需要找出方法),但似乎还有更多。
So I'm testing it for this situation: 所以我正在测试它的情况:
I set up My Documents
folder to be redirected to a network share to \\\\Server\\Home\\UserA\\Documents
, plus that folder is also mapped to the drive R:
on the client machine. 我将
My Documents
文件夹设置为重定向到网络共享到\\\\Server\\Home\\UserA\\Documents
,此外该文件夹也映射到客户端计算机上的驱动器R:
\\\\Server\\Home\\UserA\\Documents
So that means that all of the following folders: 这意味着所有以下文件夹:
"R:\Documents\Subfolder1"
"\\Server\Home\UserA\Documents\Subfolder1"
"C:\Users\UserA\Documents\Subfolder1"
point technically to the same physical location, that is a subfolder of My Documents
. 从技术上讲,指向同一物理位置,即“
My Documents
的子文件夹。
The question is how to know this reliably? 问题是如何可靠地了解这一点?
Use GetFileInformationByHandle
to retrieve the volume serial number and file index for the destination directory and for each possible match. 使用
GetFileInformationByHandle
检索目标目录和每个可能匹配的卷序列号和文件索引。 If both the serial number and the file index are the same, they are the same directory. 如果序列号和文件索引都相同,则它们是同一目录。
Note that you will need to use the FILE_FLAG_BACKUP_SEMANTICS
flag in CreateFile
in order to open a handle to a directory. 请注意,您需要在
CreateFile
中使用FILE_FLAG_BACKUP_SEMANTICS
标志才能打开目录句柄。 (You do not need backup privilege to do so.) (您不需要备份权限。)
It may be possible for cloned volumes to have the same serial number (I'm not sure offhand whether Windows forces a serial number change or not) so it might be wise to provide an option to the user that disables this check. 克隆卷可能具有相同的序列号(我不确定Windows是否强制更改序列号),因此向用户提供禁用此检查的选项可能是明智之举。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.