Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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

The question is how to know this reliably?

like image 774
c00000fd Avatar asked Nov 01 '22 22:11

c00000fd


1 Answers

Use GetFileInformationByHandle to retrieve the volume serial number and file index for the destination directory and for each possible match. 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. (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.

like image 182
Harry Johnston Avatar answered Nov 09 '22 12:11

Harry Johnston