Implement class for handling filesystem paths

PR #15915.
This commit is contained in:
Vladimir Golovnev
2022-02-08 06:03:48 +03:00
committed by GitHub
parent facfa26eed
commit dd1bd8ad10
131 changed files with 2252 additions and 1868 deletions

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -50,57 +51,17 @@
#include <unistd.h>
#endif
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QDirIterator>
#include <QFile>
#include <QFileInfo>
#include <QMimeDatabase>
#include <QStorageInfo>
#include <QRegularExpression>
#include "base/global.h"
QString Utils::Fs::toNativePath(const QString &path)
{
return QDir::toNativeSeparators(path);
}
QString Utils::Fs::toUniformPath(const QString &path)
{
return QDir::fromNativeSeparators(path);
}
QString Utils::Fs::resolvePath(const QString &relativePath, const QString &basePath)
{
Q_ASSERT(QDir::isRelativePath(relativePath));
Q_ASSERT(QDir::isAbsolutePath(basePath));
return (relativePath.isEmpty() ? basePath : QDir(basePath).absoluteFilePath(relativePath));
}
QString Utils::Fs::fileExtension(const QString &filename)
{
return QMimeDatabase().suffixForFileName(filename);
}
QString Utils::Fs::fileName(const QString &filePath)
{
const QString path = toUniformPath(filePath);
const int slashIndex = path.lastIndexOf('/');
if (slashIndex == -1)
return path;
return path.mid(slashIndex + 1);
}
QString Utils::Fs::folderName(const QString &filePath)
{
const QString path = toUniformPath(filePath);
const int slashIndex = path.lastIndexOf('/');
if (slashIndex == -1)
return {};
return path.left(slashIndex);
}
#include "base/path.h"
/**
* This function will first check if there are only system cache files, e.g. `Thumbs.db`,
@@ -111,9 +72,9 @@ QString Utils::Fs::folderName(const QString &filePath)
* that only the above mentioned "useless" files exist but before the whole folder is removed.
* In this case, the folder will not be removed but the "useless" files will be deleted.
*/
bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
bool Utils::Fs::smartRemoveEmptyFolderTree(const Path &path)
{
if (path.isEmpty() || !QDir(path).exists())
if (!path.exists())
return true;
const QStringList deleteFilesList =
@@ -128,8 +89,8 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
};
// travel from the deepest folder and remove anything unwanted on the way out.
QStringList dirList(path + '/'); // get all sub directories paths
QDirIterator iter(path, (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories);
QStringList dirList(path.data() + '/'); // get all sub directories paths
QDirIterator iter {path.data(), (QDir::AllDirs | QDir::NoDotAndDotDot), QDirIterator::Subdirectories};
while (iter.hasNext())
dirList << iter.next() + '/';
// sort descending by directory depth
@@ -138,7 +99,7 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
for (const QString &p : asConst(dirList))
{
const QDir dir(p);
const QDir dir {p};
// A deeper folder may have not been removed in the previous iteration
// so don't remove anything from this folder either.
if (!dir.isEmpty(QDir::Dirs | QDir::NoDotAndDotDot))
@@ -156,38 +117,22 @@ bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
continue;
for (const QString &f : tmpFileList)
forceRemove(p + f);
removeFile(Path(p + f));
// remove directory if empty
dir.rmdir(p);
}
return QDir(path).exists();
}
/**
* Removes the file with the given filePath.
*
* This function will try to fix the file permissions before removing it.
*/
bool Utils::Fs::forceRemove(const QString &filePath)
{
QFile f(filePath);
if (!f.exists())
return true;
// Make sure we have read/write permissions
f.setPermissions(f.permissions() | QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser);
// Remove the file
return f.remove();
return path.exists();
}
/**
* Removes directory and its content recursively.
*/
void Utils::Fs::removeDirRecursive(const QString &path)
void Utils::Fs::removeDirRecursively(const Path &path)
{
if (!path.isEmpty())
QDir(path).removeRecursively();
QDir(path.data()).removeRecursively();
}
/**
@@ -196,16 +141,16 @@ void Utils::Fs::removeDirRecursive(const QString &path)
*
* Returns -1 in case of error.
*/
qint64 Utils::Fs::computePathSize(const QString &path)
qint64 Utils::Fs::computePathSize(const Path &path)
{
// Check if it is a file
const QFileInfo fi(path);
const QFileInfo fi {path.data()};
if (!fi.exists()) return -1;
if (fi.isFile()) return fi.size();
// Compute folder size based on its content
qint64 size = 0;
QDirIterator iter(path, QDir::Files | QDir::Hidden | QDir::NoSymLinks, QDirIterator::Subdirectories);
QDirIterator iter {path.data(), QDir::Files | QDir::Hidden | QDir::NoSymLinks, QDirIterator::Subdirectories};
while (iter.hasNext())
{
iter.next();
@@ -217,9 +162,10 @@ qint64 Utils::Fs::computePathSize(const QString &path)
/**
* 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 Path &path1, const Path &path2)
{
QFile f1(path1), f2(path2);
QFile f1 {path1.data()};
QFile f2 {path2.data()};
if (!f1.exists() || !f2.exists()) return false;
if (f1.size() != f2.size()) return false;
if (!f1.open(QIODevice::ReadOnly)) return false;
@@ -234,120 +180,65 @@ bool Utils::Fs::sameFiles(const QString &path1, const QString &path2)
return true;
}
QString Utils::Fs::toValidFileSystemName(const QString &name, const bool allowSeparators, const QString &pad)
QString Utils::Fs::toValidFileName(const QString &name, const QString &pad)
{
const QRegularExpression regex(allowSeparators ? "[:?\"*<>|]+" : "[\\\\/:?\"*<>|]+");
const QRegularExpression regex {QLatin1String("[\\\\/:?\"*<>|]+")};
QString validName = name.trimmed();
validName.replace(regex, pad);
qDebug() << "toValidFileSystemName:" << name << "=>" << validName;
return validName;
}
bool Utils::Fs::isValidFileSystemName(const QString &name, const bool allowSeparators)
Path Utils::Fs::toValidPath(const QString &name, const QString &pad)
{
if (name.isEmpty()) return false;
const QRegularExpression regex {QLatin1String("[:?\"*<>|]+")};
#if defined(Q_OS_WIN)
const QRegularExpression regex
{allowSeparators
? QLatin1String("[:?\"*<>|]")
: QLatin1String("[\\\\/:?\"*<>|]")};
#elif defined(Q_OS_MACOS)
const QRegularExpression regex
{allowSeparators
? QLatin1String("[\\0:]")
: QLatin1String("[\\0/:]")};
#else
const QRegularExpression regex
{allowSeparators
? QLatin1String("[\\0]")
: QLatin1String("[\\0/]")};
#endif
return !name.contains(regex);
QString validPathStr = name;
validPathStr.replace(regex, pad);
return Path(validPathStr);
}
qint64 Utils::Fs::freeDiskSpaceOnPath(const QString &path)
qint64 Utils::Fs::freeDiskSpaceOnPath(const Path &path)
{
return QStorageInfo(path).bytesAvailable();
return QStorageInfo(path.data()).bytesAvailable();
}
QString Utils::Fs::branchPath(const QString &filePath, QString *removed)
Path Utils::Fs::tempPath()
{
QString ret = toUniformPath(filePath);
if (ret.endsWith('/'))
ret.chop(1);
const int slashIndex = ret.lastIndexOf('/');
if (slashIndex >= 0)
{
if (removed)
*removed = ret.mid(slashIndex + 1);
ret = ret.left(slashIndex);
}
return ret;
}
bool Utils::Fs::sameFileNames(const QString &first, const QString &second)
{
#if defined(Q_OS_UNIX) || defined(Q_WS_QWS)
return QString::compare(first, second, Qt::CaseSensitive) == 0;
#else
return QString::compare(first, second, Qt::CaseInsensitive) == 0;
#endif
}
QString Utils::Fs::expandPath(const QString &path)
{
const QString ret = path.trimmed();
if (ret.isEmpty())
return ret;
return QDir::cleanPath(ret);
}
QString Utils::Fs::expandPathAbs(const QString &path)
{
return QDir(expandPath(path)).absolutePath();
}
QString Utils::Fs::tempPath()
{
static const QString path = QDir::tempPath() + "/.qBittorrent/";
QDir().mkdir(path);
static const Path path = Path(QDir::tempPath()) / Path(".qBittorrent");
mkdir(path);
return path;
}
bool Utils::Fs::isRegularFile(const QString &path)
bool Utils::Fs::isRegularFile(const Path &path)
{
struct ::stat st;
if (::stat(path.toUtf8().constData(), &st) != 0)
if (::stat(path.toString().toUtf8().constData(), &st) != 0)
{
// analyse erno and log the error
const auto err = errno;
qDebug("Could not get file stats for path '%s'. Error: %s"
, qUtf8Printable(path), qUtf8Printable(strerror(err)));
, qUtf8Printable(path.toString()), qUtf8Printable(strerror(err)));
return false;
}
return (st.st_mode & S_IFMT) == S_IFREG;
}
#if !defined Q_OS_HAIKU
bool Utils::Fs::isNetworkFileSystem(const QString &path)
bool Utils::Fs::isNetworkFileSystem(const Path &path)
{
#if defined(Q_OS_WIN)
const std::wstring pathW {path.toStdWString()};
auto volumePath = std::make_unique<wchar_t[]>(path.length() + 1);
if (!::GetVolumePathNameW(pathW.c_str(), volumePath.get(), (path.length() + 1)))
#if defined Q_OS_HAIKU
return false;
#elif defined(Q_OS_WIN)
const std::wstring pathW = path.toString().toStdWString();
auto volumePath = std::make_unique<wchar_t[]>(pathW.length() + 1);
if (!::GetVolumePathNameW(pathW.c_str(), volumePath.get(), static_cast<DWORD>(pathW.length() + 1)))
return false;
return (::GetDriveTypeW(volumePath.get()) == DRIVE_REMOTE);
#else
QString file = path;
if (!file.endsWith('/'))
file += '/';
file += '.';
const QString file = path.toString() + QLatin1String("/.");
struct statfs buf {};
if (statfs(file.toLocal8Bit().constData(), &buf) != 0)
return false;
@@ -398,41 +289,77 @@ bool Utils::Fs::isNetworkFileSystem(const QString &path)
#endif
#endif
}
#endif // Q_OS_HAIKU
QString Utils::Fs::findRootFolder(const QStringList &filePaths)
bool Utils::Fs::copyFile(const Path &from, const Path &to)
{
QString rootFolder;
for (const QString &filePath : filePaths)
{
const auto filePathElements = QStringView(filePath).split(u'/');
// if at least one file has no root folder, no common root folder exists
if (filePathElements.count() <= 1)
return {};
if (rootFolder.isEmpty())
rootFolder = filePathElements.at(0).toString();
else if (rootFolder != filePathElements.at(0))
return {};
}
return rootFolder;
return QFile::copy(from.data(), to.data());
}
void Utils::Fs::stripRootFolder(QStringList &filePaths)
bool Utils::Fs::renameFile(const Path &from, const Path &to)
{
const QString commonRootFolder = findRootFolder(filePaths);
if (commonRootFolder.isEmpty())
return;
for (QString &filePath : filePaths)
filePath = filePath.mid(commonRootFolder.size() + 1);
return QFile::rename(from.data(), to.data());
}
void Utils::Fs::addRootFolder(QStringList &filePaths, const QString &rootFolder)
/**
* Removes the file with the given filePath.
*
* This function will try to fix the file permissions before removing it.
*/
bool Utils::Fs::removeFile(const Path &path)
{
Q_ASSERT(!rootFolder.isEmpty());
if (QFile::remove(path.data()))
return true;
for (QString &filePath : filePaths)
filePath = rootFolder + QLatin1Char('/') + filePath;
QFile file {path.data()};
if (!file.exists())
return true;
// Make sure we have read/write permissions
file.setPermissions(file.permissions() | QFile::ReadOwner | QFile::WriteOwner | QFile::ReadUser | QFile::WriteUser);
return file.remove();
}
bool Utils::Fs::isReadable(const Path &path)
{
return QFileInfo(path.data()).isReadable();
}
bool Utils::Fs::isWritable(const Path &path)
{
return QFileInfo(path.data()).isWritable();
}
QDateTime Utils::Fs::lastModified(const Path &path)
{
return QFileInfo(path.data()).lastModified();
}
bool Utils::Fs::isDir(const Path &path)
{
return QFileInfo(path.data()).isDir();
}
Path Utils::Fs::toCanonicalPath(const Path &path)
{
return Path(QFileInfo(path.data()).canonicalFilePath());
}
Path Utils::Fs::homePath()
{
return Path(QDir::homePath());
}
bool Utils::Fs::mkdir(const Path &dirPath)
{
return QDir().mkdir(dirPath.data());
}
bool Utils::Fs::mkpath(const Path &dirPath)
{
return QDir().mkpath(dirPath.data());
}
bool Utils::Fs::rmdir(const Path &dirPath)
{
return QDir().rmdir(dirPath.data());
}

View File

@@ -1,5 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org>
*
* This program is free software; you can redistribute it and/or
@@ -34,58 +35,36 @@
#include <QString>
#include "base/pathfwd.h"
class QDateTime;
namespace Utils::Fs
{
/**
* Converts a path to a string suitable for display.
* This function makes sure the directory separator used is consistent
* with the OS being run.
*/
QString toNativePath(const QString &path);
qint64 computePathSize(const Path &path);
qint64 freeDiskSpaceOnPath(const Path &path);
/**
* Converts a path to a string suitable for processing.
* This function makes sure the directory separator used is independent
* from the OS being run so it is the same on all supported platforms.
* Slash ('/') is used as "uniform" directory separator.
*/
QString toUniformPath(const QString &path);
bool isRegularFile(const Path &path);
bool isDir(const Path &path);
bool isReadable(const Path &path);
bool isWritable(const Path &path);
bool isNetworkFileSystem(const Path &path);
QDateTime lastModified(const Path &path);
bool sameFiles(const Path &path1, const Path &path2);
/**
* If `path is relative then resolves it against `basePath`, otherwise returns the `path` itself
*/
QString resolvePath(const QString &relativePath, const QString &basePath);
QString toValidFileName(const QString &name, const QString &pad = QLatin1String(" "));
Path toValidPath(const QString &name, const QString &pad = QLatin1String(" "));
Path toCanonicalPath(const Path &path);
/**
* Returns the file extension part of a file name.
*/
QString fileExtension(const QString &filename);
bool copyFile(const Path &from, const Path &to);
bool renameFile(const Path &from, const Path &to);
bool removeFile(const Path &path);
bool mkdir(const Path &dirPath);
bool mkpath(const Path &dirPath);
bool rmdir(const Path &dirPath);
void removeDirRecursively(const Path &path);
bool smartRemoveEmptyFolderTree(const Path &path);
QString fileName(const QString &filePath);
QString folderName(const QString &filePath);
qint64 computePathSize(const QString &path);
bool sameFiles(const QString &path1, const QString &path2);
QString toValidFileSystemName(const QString &name, bool allowSeparators = false
, const QString &pad = QLatin1String(" "));
bool isValidFileSystemName(const QString &name, bool allowSeparators = false);
qint64 freeDiskSpaceOnPath(const QString &path);
QString branchPath(const QString &filePath, QString *removed = nullptr);
bool sameFileNames(const QString &first, const QString &second);
QString expandPath(const QString &path);
QString expandPathAbs(const QString &path);
bool isRegularFile(const QString &path);
bool smartRemoveEmptyFolderTree(const QString &path);
bool forceRemove(const QString &filePath);
void removeDirRecursive(const QString &path);
QString tempPath();
QString findRootFolder(const QStringList &filePaths);
void stripRootFolder(QStringList &filePaths);
void addRootFolder(QStringList &filePaths, const QString &name);
#if !defined Q_OS_HAIKU
bool isNetworkFileSystem(const QString &path);
#endif
Path homePath();
Path tempPath();
}

View File

@@ -36,6 +36,8 @@
#include <QSaveFile>
#include <QString>
#include "base/path.h"
Utils::IO::FileDeviceOutputIterator::FileDeviceOutputIterator(QFileDevice &device, const int bufferSize)
: m_device {&device}
, m_buffer {std::make_shared<QByteArray>()}
@@ -66,17 +68,17 @@ Utils::IO::FileDeviceOutputIterator &Utils::IO::FileDeviceOutputIterator::operat
return *this;
}
nonstd::expected<void, QString> Utils::IO::saveToFile(const QString &path, const QByteArray &data)
nonstd::expected<void, QString> Utils::IO::saveToFile(const Path &path, const QByteArray &data)
{
QSaveFile file {path};
QSaveFile file {path.data()};
if (!file.open(QIODevice::WriteOnly) || (file.write(data) != data.size()) || !file.flush() || !file.commit())
return nonstd::make_unexpected(file.errorString());
return {};
}
nonstd::expected<void, QString> Utils::IO::saveToFile(const QString &path, const lt::entry &data)
nonstd::expected<void, QString> Utils::IO::saveToFile(const Path &path, const lt::entry &data)
{
QSaveFile file {path};
QSaveFile file {path.data()};
if (!file.open(QIODevice::WriteOnly))
return nonstd::make_unexpected(file.errorString());

View File

@@ -34,6 +34,7 @@
#include <libtorrent/fwd.hpp>
#include "base/3rdparty/expected.hpp"
#include "base/pathfwd.h"
class QByteArray;
class QFileDevice;
@@ -80,6 +81,6 @@ namespace Utils::IO
int m_bufferSize;
};
nonstd::expected<void, QString> saveToFile(const QString &path, const QByteArray &data);
nonstd::expected<void, QString> saveToFile(const QString &path, const lt::entry &data);
nonstd::expected<void, QString> saveToFile(const Path &path, const QByteArray &data);
nonstd::expected<void, QString> saveToFile(const Path &path, const lt::entry &data);
}

View File

@@ -61,6 +61,7 @@
#include <QDBusInterface>
#endif
#include "base/path.h"
#include "base/types.h"
#include "base/unicodestrings.h"
#include "base/utils/fs.h"
@@ -292,9 +293,9 @@ qlonglong Utils::Misc::sizeInBytes(qreal size, const Utils::Misc::SizeUnit unit)
return size;
}
bool Utils::Misc::isPreviewable(const QString &filename)
bool Utils::Misc::isPreviewable(const Path &filePath)
{
const QString mime = QMimeDatabase().mimeTypeForFile(filename, QMimeDatabase::MatchExtension).name();
const QString mime = QMimeDatabase().mimeTypeForFile(filePath.data(), QMimeDatabase::MatchExtension).name();
if (mime.startsWith(QLatin1String("audio"), Qt::CaseInsensitive)
|| mime.startsWith(QLatin1String("video"), Qt::CaseInsensitive))
@@ -304,50 +305,50 @@ bool Utils::Misc::isPreviewable(const QString &filename)
const QSet<QString> multimediaExtensions =
{
"3GP",
"AAC",
"AC3",
"AIF",
"AIFC",
"AIFF",
"ASF",
"AU",
"AVI",
"FLAC",
"FLV",
"M3U",
"M4A",
"M4P",
"M4V",
"MID",
"MKV",
"MOV",
"MP2",
"MP3",
"MP4",
"MPC",
"MPE",
"MPEG",
"MPG",
"MPP",
"OGG",
"OGM",
"OGV",
"QT",
"RA",
"RAM",
"RM",
"RMV",
"RMVB",
"SWA",
"SWF",
"TS",
"VOB",
"WAV",
"WMA",
"WMV"
".3GP",
".AAC",
".AC3",
".AIF",
".AIFC",
".AIFF",
".ASF",
".AU",
".AVI",
".FLAC",
".FLV",
".M3U",
".M4A",
".M4P",
".M4V",
".MID",
".MKV",
".MOV",
".MP2",
".MP3",
".MP4",
".MPC",
".MPE",
".MPEG",
".MPG",
".MPP",
".OGG",
".OGM",
".OGV",
".QT",
".RA",
".RAM",
".RM",
".RMV",
".RMVB",
".SWA",
".SWF",
".TS",
".VOB",
".WAV",
".WMA",
".WMV"
};
return multimediaExtensions.contains(Utils::Fs::fileExtension(filename).toUpper());
return multimediaExtensions.contains(filePath.extension().toUpper());
}
QString Utils::Misc::userFriendlyDuration(const qlonglong seconds, const qlonglong maxCap)

View File

@@ -37,6 +37,8 @@
#include <QString>
#include "base/pathfwd.h"
enum class ShutdownDialogAction;
/* Miscellaneous functions that can be useful */
@@ -77,7 +79,7 @@ namespace Utils::Misc
int friendlyUnitPrecision(SizeUnit unit);
qint64 sizeInBytes(qreal size, SizeUnit unit);
bool isPreviewable(const QString &filename);
bool isPreviewable(const Path &filePath);
// Take a number of seconds and return a user-friendly
// time duration like "1d 2h 10m".