Browse Source

Fix the file size sum igonred files starting with a dot: ".name"

Make the computation iterative instead of recurisve to avoid stack overflow.

Enlarge read size to enable better performance.
Remove `QFile::close()` calls, QFile automatically close them in dtor.
adaptive-webui-19844
Chocobo1 9 years ago
parent
commit
386b93bb0f
  1. 28
      src/base/utils/fs.cpp

28
src/base/utils/fs.cpp

@ -180,14 +180,13 @@ qint64 Utils::Fs::computePathSize(const QString& path)
QFileInfo fi(path); QFileInfo fi(path);
if (!fi.exists()) return -1; if (!fi.exists()) return -1;
if (fi.isFile()) return fi.size(); if (fi.isFile()) return fi.size();
// Compute folder size based on its content // Compute folder size based on its content
qint64 size = 0; qint64 size = 0;
foreach (const QFileInfo &subfi, QDir(path).entryInfoList(QDir::Dirs|QDir::Files)) { QDirIterator iter(path, QDir::Files | QDir::Hidden | QDir::NoSymLinks, QDirIterator::Subdirectories);
if (subfi.fileName().startsWith(".")) continue; while (iter.hasNext()) {
if (subfi.isDir()) iter.next();
size += computePathSize(subfi.absoluteFilePath()); size += iter.fileInfo().size();
else
size += subfi.size();
} }
return size; return size;
} }
@ -201,19 +200,14 @@ bool Utils::Fs::sameFiles(const QString& path1, const QString& path2)
if (!f1.exists() || !f2.exists()) return false; if (!f1.exists() || !f2.exists()) return false;
if (f1.size() != f2.size()) return false; if (f1.size() != f2.size()) return false;
if (!f1.open(QIODevice::ReadOnly)) return false; if (!f1.open(QIODevice::ReadOnly)) return false;
if (!f2.open(QIODevice::ReadOnly)) { if (!f2.open(QIODevice::ReadOnly)) return false;
f1.close();
return false; const int readSize = 1024 * 1024; // 1 MiB
}
bool same = true;
while(!f1.atEnd() && !f2.atEnd()) { while(!f1.atEnd() && !f2.atEnd()) {
if (f1.read(1024) != f2.read(1024)) { if (f1.read(readSize) != f2.read(readSize))
same = false; return false;
break;
}
} }
f1.close(); f2.close(); return true;
return same;
} }
QString Utils::Fs::toValidFileSystemName(const QString &name, bool allowSeparators, const QString &pad) QString Utils::Fs::toValidFileSystemName(const QString &name, bool allowSeparators, const QString &pad)

Loading…
Cancel
Save