ReadFile 在物理驱动器末尾不会发出 EOF 信号
我正在尝试在 Windows 中实现 dd 等效项。 [澄清:我正在尝试复制 dd 的 if=/dev/hda of=/dev/hdb 功能,以便将 Windows 安装迁移到更大的硬盘。奇怪的是,我已经成功地使用过这种方法几次了。 - G]
程序可以工作(我最终将源数据复制到目标磁盘),但不会自行终止 - 一旦我确定整个源磁盘已被读取,我就必须终止它。
我在 CreateFile 调用中使用“\\.\PhysicalDriveX”语法打开和锁定源和目标物理驱动器没有问题。
我的问题是成功检测源驱动器数据的结尾。
目前我正在使用以下方法读取 32K 块:
// 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(在同步 io 期间)由 ReadFile 返回 TRUE 发出信号,但将读取的字节数 (nRead) 设置为 0。这就是我在笨拙的 while( ) 陈述。
一旦 ReadFile 读取了源 PhysicalDrive 上的有效数据末尾,它似乎就会返回空字节块。
知道何时停止阅读的正确方法是什么? - 在开始之前我应该使用 IOCTL_DISK_GET_LENGTH_INFO 吗?它看起来有点多余,因为 ReadFile 在读取超过磁盘末尾时应该报告 EOF(或失败)。
提前致谢。
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 number 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MSDN 你必须检查使用 GetLastError 来获取
ERROR_HANDLE_EOF
。也许这有帮助。但在这种情况下它也应该返回 0。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.