Browse Source

Merge pull request #7282 from glassez/fsutils

Improve utils/fs.* and fix coding style
adaptive-webui-19844
sledgehammer999 7 years ago committed by GitHub
parent
commit
cccb22f0e3
  1. 57
      src/base/utils/fs.cpp
  2. 30
      src/base/utils/fs.h

57
src/base/utils/fs.cpp

@ -56,7 +56,7 @@
* This function makes sure the directory separator used is consistent * This function makes sure the directory separator used is consistent
* with the OS being run. * with the OS being run.
*/ */
QString Utils::Fs::toNativePath(const QString& path) QString Utils::Fs::toNativePath(const QString &path)
{ {
return QDir::toNativeSeparators(path); return QDir::toNativeSeparators(path);
} }
@ -72,26 +72,26 @@ QString Utils::Fs::fromNativePath(const QString &path)
QString Utils::Fs::fileExtension(const QString &filename) QString Utils::Fs::fileExtension(const QString &filename)
{ {
QString ext = QString(filename).remove(QB_EXT); QString ext = QString(filename).remove(QB_EXT);
const int point_index = ext.lastIndexOf("."); const int pointIndex = ext.lastIndexOf(".");
return (point_index >= 0) ? ext.mid(point_index + 1) : QString(); return (pointIndex >= 0) ? ext.mid(pointIndex + 1) : QString();
} }
QString Utils::Fs::fileName(const QString& file_path) QString Utils::Fs::fileName(const QString &filePath)
{ {
QString path = fromNativePath(file_path); QString path = fromNativePath(filePath);
const int slash_index = path.lastIndexOf("/"); const int slashIndex = path.lastIndexOf("/");
if (slash_index == -1) if (slashIndex == -1)
return path; return path;
return path.mid(slash_index + 1); return path.mid(slashIndex + 1);
} }
QString Utils::Fs::folderName(const QString& file_path) QString Utils::Fs::folderName(const QString &filePath)
{ {
QString path = fromNativePath(file_path); QString path = fromNativePath(filePath);
const int slash_index = path.lastIndexOf("/"); const int slashIndex = path.lastIndexOf("/");
if (slash_index == -1) if (slashIndex == -1)
return path; return path;
return path.left(slash_index); return path.left(slashIndex);
} }
/** /**
@ -99,10 +99,10 @@ QString Utils::Fs::folderName(const QString& file_path)
* `.DS_Store`. Then will try to remove the whole tree if the tree consist * `.DS_Store`. Then will try to remove the whole tree if the tree consist
* only of folders * only of folders
*/ */
bool Utils::Fs::smartRemoveEmptyFolderTree(const QString& path) bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
{ {
if (path.isEmpty() || !QDir(path).exists()) if (path.isEmpty() || !QDir(path).exists())
return false; return true;
static const QStringList deleteFilesList = { static const QStringList deleteFilesList = {
// Windows // Windows
@ -119,7 +119,9 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString& path)
QDirIterator iter(path, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories); QDirIterator iter(path, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories);
while (iter.hasNext()) while (iter.hasNext())
dirList << iter.next() + "/"; dirList << iter.next() + "/";
std::sort(dirList.begin(), dirList.end(), [](const QString &l, const QString &r) { return l.count("/") > r.count("/"); }); // sort descending by directory depth // sort descending by directory depth
std::sort(dirList.begin(), dirList.end()
, [](const QString &l, const QString &r) { return l.count("/") > r.count("/"); });
for (const QString &p : dirList) { for (const QString &p : dirList) {
// remove unwanted files // remove unwanted files
@ -147,9 +149,9 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString& path)
* *
* This function will try to fix the file permissions before removing it. * This function will try to fix the file permissions before removing it.
*/ */
bool Utils::Fs::forceRemove(const QString& file_path) bool Utils::Fs::forceRemove(const QString &filePath)
{ {
QFile f(file_path); QFile f(filePath);
if (!f.exists()) if (!f.exists())
return true; return true;
// Make sure we have read/write permissions // Make sure we have read/write permissions
@ -174,7 +176,7 @@ void Utils::Fs::removeDirRecursive(const QString &path)
* *
* Returns -1 in case of error. * Returns -1 in case of error.
*/ */
qint64 Utils::Fs::computePathSize(const QString& path) qint64 Utils::Fs::computePathSize(const QString &path)
{ {
// Check if it is a file // Check if it is a file
QFileInfo fi(path); QFileInfo fi(path);
@ -194,7 +196,7 @@ qint64 Utils::Fs::computePathSize(const QString& path)
/** /**
* Makes deep comparison of two files to make sure they are identical. * Makes deep comparison of two files to make sure they are identical.
*/ */
bool Utils::Fs::sameFiles(const QString& path1, const QString& path2) bool Utils::Fs::sameFiles(const QString &path1, const QString &path2)
{ {
QFile f1(path1), f2(path2); QFile f1(path1), f2(path2);
if (!f1.exists() || !f2.exists()) return false; if (!f1.exists() || !f2.exists()) return false;
@ -236,9 +238,9 @@ qint64 Utils::Fs::freeDiskSpaceOnPath(const QString &path)
return QStorageInfo(path).bytesAvailable(); return QStorageInfo(path).bytesAvailable();
} }
QString Utils::Fs::branchPath(const QString& file_path, QString* removed) QString Utils::Fs::branchPath(const QString &filePath, QString *removed)
{ {
QString ret = fromNativePath(file_path); QString ret = fromNativePath(filePath);
if (ret.endsWith("/")) if (ret.endsWith("/"))
ret.chop(1); ret.chop(1);
const int slashIndex = ret.lastIndexOf("/"); const int slashIndex = ret.lastIndexOf("/");
@ -261,21 +263,16 @@ bool Utils::Fs::sameFileNames(const QString &first, const QString &second)
QString Utils::Fs::expandPath(const QString &path) QString Utils::Fs::expandPath(const QString &path)
{ {
QString ret = fromNativePath(path.trimmed()); QString ret = path.trimmed();
if (ret.isEmpty()) if (ret.isEmpty())
return ret; return ret;
return QDir::cleanPath(ret); return QDir::cleanPath(ret);
} }
QString Utils::Fs::expandPathAbs(const QString& path) QString Utils::Fs::expandPathAbs(const QString &path)
{ {
QString ret = expandPath(path); return QDir(expandPath(path)).absolutePath();
if (!QDir::isAbsolutePath(ret))
ret = QDir(ret).absolutePath();
return ret;
} }
QString Utils::Fs::tempPath() QString Utils::Fs::tempPath()

30
src/base/utils/fs.h

@ -41,25 +41,25 @@ namespace Utils
{ {
namespace Fs namespace Fs
{ {
QString toNativePath(const QString& path); QString toNativePath(const QString &path);
QString fromNativePath(const QString& path); QString fromNativePath(const QString &path);
QString fileExtension(const QString& filename); QString fileExtension(const QString &filename);
QString fileName(const QString& file_path); QString fileName(const QString &filePath);
QString folderName(const QString& file_path); QString folderName(const QString &filePath);
qint64 computePathSize(const QString& path); qint64 computePathSize(const QString &path);
bool sameFiles(const QString& path1, const QString& path2); bool sameFiles(const QString &path1, const QString &path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" ")); , const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString& name, bool allowSeparators = false); bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
qint64 freeDiskSpaceOnPath(const QString &path); qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString& file_path, QString* removed = 0); QString branchPath(const QString &filePath, QString *removed = 0);
bool sameFileNames(const QString& first, const QString& second); bool sameFileNames(const QString &first, const QString &second);
QString expandPath(const QString& path); QString expandPath(const QString &path);
QString expandPathAbs(const QString& path); QString expandPathAbs(const QString &path);
bool smartRemoveEmptyFolderTree(const QString& path); bool smartRemoveEmptyFolderTree(const QString &path);
bool forceRemove(const QString& file_path); bool forceRemove(const QString &filePath);
void removeDirRecursive(const QString& path); void removeDirRecursive(const QString &path);
QString tempPath(); QString tempPath();
} }

Loading…
Cancel
Save