已删除的文件仍报告为存在(仅限 Windows)
Deleted file still reported as existing (Windows only)
(注意这主要不是Qt问题)
在我看来 QFile::exists() 的 return 值有时不正确。
考虑以下两个类似单元测试的片段(每个片段我都循环执行了几千次)
// create file
QFile file("test.tmp");
QVERIFY(file.open(QIODevice::WriteOnly));
QVERIFY(file.write("some data") != -1);
file.close();
// delete file
QVERIFY(file.remove());
// assert file is gone
QVERIFY(!file.exists()); // <-- 5..10 % chance of failure
和
// create file
QFile file("test.tmp");
QVERIFY(file.open(QIODevice::WriteOnly));
QVERIFY(file.write("some data") != -1);
file.close();
// delete file
QVERIFY(file.remove());
// retry until file is gone (or until timeout)
for (auto i = 0; i < 10; i++)
{
if (!file.exists()) // <-- note that only the check is retried, not the actual delete
return;
QThread::yieldCurrentThread();
}
QFAIL("file is still reported as existing"); // <-- never reached in my tests
第一个单元测试大约有 100 次失败了 8 次。总是在最后一行代码(表示文件仍然存在)。第二个单元测试永远不会失败。
此行为是在 Windows 10 系统上使用 NTFS(Qt 5.2.1)观察到的。无法使用 ubuntu 16.04 LTS 在虚拟机上使用 ext4(使用 Qt 5.8.0)复制它
不确定这是否有帮助:
- Process Monitor (when it succeeds)
- Process Monitor (when it fails)
所以我的问题是:
- 发生了什么事?
- 我可能感兴趣的含义是什么?
更新:
澄清一下:我希望得到类似“这是由 NTFS 功能 'bills-fancy-caching-magic'”这样的答案。从那里我想知道 Qt 是否有意查看此功能。
根据Windows API documentation,定义行为:
The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.
它似乎是 Windows 内核的 属性,因此不限于 NTFS。
该行为似乎无法预测,因为其他服务(例如病毒扫描程序)可能会打开有问题的文件。
(注意这主要不是Qt问题)
在我看来 QFile::exists() 的 return 值有时不正确。
考虑以下两个类似单元测试的片段(每个片段我都循环执行了几千次)
// create file
QFile file("test.tmp");
QVERIFY(file.open(QIODevice::WriteOnly));
QVERIFY(file.write("some data") != -1);
file.close();
// delete file
QVERIFY(file.remove());
// assert file is gone
QVERIFY(!file.exists()); // <-- 5..10 % chance of failure
和
// create file
QFile file("test.tmp");
QVERIFY(file.open(QIODevice::WriteOnly));
QVERIFY(file.write("some data") != -1);
file.close();
// delete file
QVERIFY(file.remove());
// retry until file is gone (or until timeout)
for (auto i = 0; i < 10; i++)
{
if (!file.exists()) // <-- note that only the check is retried, not the actual delete
return;
QThread::yieldCurrentThread();
}
QFAIL("file is still reported as existing"); // <-- never reached in my tests
第一个单元测试大约有 100 次失败了 8 次。总是在最后一行代码(表示文件仍然存在)。第二个单元测试永远不会失败。
此行为是在 Windows 10 系统上使用 NTFS(Qt 5.2.1)观察到的。无法使用 ubuntu 16.04 LTS 在虚拟机上使用 ext4(使用 Qt 5.8.0)复制它
不确定这是否有帮助:
- Process Monitor (when it succeeds)
- Process Monitor (when it fails)
所以我的问题是:
- 发生了什么事?
- 我可能感兴趣的含义是什么?
更新:
澄清一下:我希望得到类似“这是由 NTFS 功能 'bills-fancy-caching-magic'”这样的答案。从那里我想知道 Qt 是否有意查看此功能。
根据Windows API documentation,定义行为:
The DeleteFile function marks a file for deletion on close. Therefore, the file deletion does not occur until the last handle to the file is closed. Subsequent calls to CreateFile to open the file fail with ERROR_ACCESS_DENIED.
它似乎是 Windows 内核的 属性,因此不限于 NTFS。
该行为似乎无法预测,因为其他服务(例如病毒扫描程序)可能会打开有问题的文件。