开发者

ReadFile doesn't signal EOF at the end of a PhysicalDrive

I'm trying to implement a dd equivalent in Windows. [Clarification: I'm trying to replicate the if=/dev/hda of=/dev/hdb functionality of dd, in order to migrate a windows installation to a larger HD. Bizarrely enough, I have used this approach successfully a couple of times now. - G]

The program works (I end up with the source data copied to the destination disk) but does not terminate by itself - I have to tskill it once I'm certain the whole source disk has been read.

I have no problem with opening and locking the source and destination physical drives using the "\\.\PhysicalDriveX" syntax in the CreateFile call.

My problem is in successfully detecting the end of the source drive data.

Currently I'm reading 32K chunks using:

// doesn't work - won't detect End-of-valid drive data and reads endlessly
while (( success = ReadFile(hInfile, buffer, BUFSIZE, &nRead, NULL )) && nRead != 0) {
    // ... write the data to the target drive; break if it fails.
    // ... write a progress indicator to the console
}

// should execute but never does
if (! success) {
    // an error occurred, do cleanup.
}
else {
    // all done, unlock & close filehandles
    puts("ta da!");
}

EOF for a 'normal' file (during synchronous io) is signalled by ReadFile returning TRUE but setting the nu开发者_开发知识库mber of bytes read (nRead) to 0. This is what I attempt in my clumsy while() statement.

ReadFile seems to be returning blocks of null bytes once it has read past the end of the valid data on the source PhysicalDrive.

What's the right way of knowing when to stop reading? - should I use IOCTL_DISK_GET_LENGTH_INFO before I start? It just seems a bit redundant, as ReadFile should report EOF (or fail) on reading past the end of the disk.

Thanks in advance.


According to MSDN you have to check with GetLastError for ERROR_HANDLE_EOF. Maybe this helps. Altough it should also return 0 in this case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜