语境:
我有一个应用程序可以搜索目录中的文件并复制特定文件。
问题:
使用FileCopyExW
,我成功地将文件复制到新位置,但是文件的日期和时间不匹配,它可能会偏离一秒或更长时间,如下例所示。
尝试QDateTime
用源文件(有时)修改覆盖目标文件无法写入正确的日期,如下面的示例所示。
调试器截图
此文件的日志输出
09:34:50.731 Warning: File times do not match
09:34:50.731 Warning: Birth time differene: 0
09:34:50.731 Warning: Last Access time differene: 483
09:42:02.660 Warning: Last Modified time differene: 483
09:42:02.660 Debug:
失败的随机其他文件的输出
09:43:48.831 Warning: File times do not match
09:43:48.831 Warning: Birth time differene: -7
09:43:48.831 Warning: Last Access time differene: 1462
09:44:53.602 Warning: Last Modified time differene: 0
09:44:53.602 Debug:
任何想法为什么会发生这种情况以及我该如何解决?
代码示例:
// File copied successfully at this point, now adjust file times as these may not be accurate (why?)
QFile newFile(destination);
if (newFile.open(QIODevice::OpenModeFlag::WriteOnly | QIODevice::OpenModeFlag::Append)) {
// set file attributes, create, last write and last modified dates the same as original file
QFileInfo srcFi(storageFile.location);
// Set last modified date - most important
QDateTime localLastModified = srcFi.lastModified().toLocalTime();
if (!newFile.setFileTime(localLastModified, QFileDevice::FileModificationTime)) {
printWarningFileError(TAG, QString("Failed to set lastModified date for file at [%1]").arg(destination), &newFile);
}
// Set last accessed date
QDateTime localLastRead = srcFi.lastRead().toLocalTime();
if (!newFile.setFileTime(localLastRead, QFileDevice::FileAccessTime)) {
printWarningFileError(TAG, QString("Failed to set lastAccessed date for file at [%1]").arg(destination), &newFile);
}
// Set created date
QDateTime localBirthTime = srcFi.birthTime().toLocalTime();
if (!newFile.setFileTime(localBirthTime, QFileDevice::FileBirthTime)) {
printWarningFileError(TAG, QString("Failed to set created date for file at [%1]").arg(destination), &newFile);
}
// Ensure we write out content before closing file (not really necessary due to close() )
newFile.flush();
newFile.close();
// Read file & refresh meta data for checking
QFileInfo fiDest(destination);
fiDest.refresh();
// Read destination file date data
QDateTime destFileBirth = fiDest.birthTime();
QDateTime destFileAccess = fiDest.lastRead();
QDateTime destFileModified = fiDest.lastModified();
// Compare dates
bool birthTimeMatch = (destFileBirth != localBirthTime);
bool accessTimeMatch = (destFileAccess != localLastRead);
bool modifiedTimeMatch = (destFileModified != localLastModified);
if (!(birthTimeMatch && accessTimeMatch && modifiedTimeMatch)) {
qWarning() << "File times do not match";
qWarning() << "Birth time differene: " << (destFileBirth.toMSecsSinceEpoch() - localBirthTime.toMSecsSinceEpoch());
qWarning() << "Last Access time differene: " << (destFileAccess.toMSecsSinceEpoch() - localLastRead.toMSecsSinceEpoch());
qWarning() << "Last Modified time differene: " << (destFileModified.toMSecsSinceEpoch() - localLastModified.toMSecsSinceEpoch());
qDebug() << "";
}
else {
qDebug() << "All good";
}
}
else {
printWarningFileError(TAG, QString("Failed to open file for adjust dates [%1]").arg(destination), &newFile);
}