Val*_*itz 2 c++ filesystems hash qt
有什么方法可以确定目录内容(包括深层子目录结构)自上次访问以来是否已更改?我正在寻找 C/C++ 中的可移植解决方案,最好是 Qt 中。
PS:如果相关,请说明问题的背景。在我的应用程序中,当某些条件成立时,我必须递归扫描许多目录并在数据库中导入一些数据。导入目录后,我用文件“.imported”对其进行标记,并在下次忽略。
现在我想标记也扫描但不导入的目录。为此,我将存储一个包含目录哈希的文件。因此,在扫描之前,我可以将计算出的哈希值与文件中的最后一个哈希值进行比较,如果相等则跳过扫描。
有一个QFileSystemWatcher类会通知您更改。
如果您想创建目录及其内容的加密哈希,我就是这样做的:-
void AddToHash(const QFileInfo& fileInf, QCryptographicHash& cryptHash)
{
QDir directory(fileInf.absoluteFilePath());
directory.setFilter(QDir::NoDotAndDotDot | QDir::AllDirs | QDir::Files);
QFileInfoList fileInfoList = directory.entryInfoList();
foreach(QFileInfo info, fileInfoList)
{
if(info.isDir())
{
// recurse through all directories
AddToHash(info, cryptHash);
continue;
}
// add all file contents to the hash
if(info.isFile())
{
QFile file(info.absoluteFilePath());
if(!file.open(QIODevice::ReadOnly))
{
// failed to open file, so skip
continue;
}
cryptHash.addData(&file);
file.close();
}
}
}
// create a fileInfo from the top-level directory
QFileInfo fileInfo(filePath);
QString hash;
// Choose an arbitrary hash, say Sha1
QCryptographicHash cryptHash(QCryptographicHash::Sha1);
// add all files to the hash
AddToHash(fileInfo, cryptHash);
// get a printable version of the hash
hash = cryptHash.result().toHex();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1972 次 |
| 最近记录: |