2015-05-06 14:53:27 +03:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2012 Christophe Dumez
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
2016-05-03 21:45:06 +02:00
|
|
|
#include "fs.h"
|
|
|
|
|
2015-05-06 14:53:27 +03:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QFileInfo>
|
2015-08-09 15:27:56 +08:00
|
|
|
#include <QDirIterator>
|
2015-05-06 14:53:27 +03:00
|
|
|
#include <QCoreApplication>
|
2017-05-14 23:57:24 +02:00
|
|
|
#include <QStorageInfo>
|
2015-05-06 14:53:27 +03:00
|
|
|
|
2017-02-07 14:07:59 +08:00
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
#include <Windows.h>
|
|
|
|
#elif defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
|
2015-05-06 14:53:27 +03:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#elif defined(Q_OS_HAIKU)
|
|
|
|
#include <kernel/fs_info.h>
|
|
|
|
#else
|
|
|
|
#include <sys/vfs.h>
|
|
|
|
#endif
|
2017-02-07 14:07:59 +08:00
|
|
|
|
|
|
|
#include "base/bittorrent/torrenthandle.h"
|
2015-05-06 14:53:27 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a path to a string suitable for display.
|
|
|
|
* This function makes sure the directory separator used is consistent
|
|
|
|
* with the OS being run.
|
|
|
|
*/
|
2017-08-15 16:17:57 +03:00
|
|
|
QString Utils::Fs::toNativePath(const QString &path)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
|
|
|
return QDir::toNativeSeparators(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utils::Fs::fromNativePath(const QString &path)
|
|
|
|
{
|
|
|
|
return QDir::fromNativeSeparators(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the file extension part of a file name.
|
|
|
|
*/
|
|
|
|
QString Utils::Fs::fileExtension(const QString &filename)
|
|
|
|
{
|
2017-02-07 14:07:59 +08:00
|
|
|
QString ext = QString(filename).remove(QB_EXT);
|
2017-08-15 16:17:57 +03:00
|
|
|
const int pointIndex = ext.lastIndexOf(".");
|
|
|
|
return (pointIndex >= 0) ? ext.mid(pointIndex + 1) : QString();
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:17:57 +03:00
|
|
|
QString Utils::Fs::fileName(const QString &filePath)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-08-15 16:17:57 +03:00
|
|
|
QString path = fromNativePath(filePath);
|
|
|
|
const int slashIndex = path.lastIndexOf("/");
|
|
|
|
if (slashIndex == -1)
|
2015-05-06 14:53:27 +03:00
|
|
|
return path;
|
2017-08-15 16:17:57 +03:00
|
|
|
return path.mid(slashIndex + 1);
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:17:57 +03:00
|
|
|
QString Utils::Fs::folderName(const QString &filePath)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-08-15 16:17:57 +03:00
|
|
|
QString path = fromNativePath(filePath);
|
|
|
|
const int slashIndex = path.lastIndexOf("/");
|
|
|
|
if (slashIndex == -1)
|
2015-05-06 14:53:27 +03:00
|
|
|
return path;
|
2017-08-15 16:17:57 +03:00
|
|
|
return path.left(slashIndex);
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-09 15:27:56 +08:00
|
|
|
* This function will first remove system cache files, e.g. `Thumbs.db`,
|
|
|
|
* `.DS_Store`. Then will try to remove the whole tree if the tree consist
|
|
|
|
* only of folders
|
2015-05-06 14:53:27 +03:00
|
|
|
*/
|
2017-08-15 16:17:57 +03:00
|
|
|
bool Utils::Fs::smartRemoveEmptyFolderTree(const QString &path)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-04-26 13:15:37 +03:00
|
|
|
if (path.isEmpty() || !QDir(path).exists())
|
2017-08-15 16:17:57 +03:00
|
|
|
return true;
|
2015-05-06 14:53:27 +03:00
|
|
|
|
2015-08-09 15:27:56 +08:00
|
|
|
static const QStringList deleteFilesList = {
|
|
|
|
// Windows
|
|
|
|
"Thumbs.db",
|
|
|
|
"desktop.ini",
|
|
|
|
// Linux
|
|
|
|
".directory",
|
|
|
|
// Mac OS
|
|
|
|
".DS_Store"
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
while (iter.hasNext())
|
|
|
|
dirList << iter.next() + "/";
|
2017-08-15 16:17:57 +03:00
|
|
|
// sort descending by directory depth
|
|
|
|
std::sort(dirList.begin(), dirList.end()
|
|
|
|
, [](const QString &l, const QString &r) { return l.count("/") > r.count("/"); });
|
2015-08-09 15:27:56 +08:00
|
|
|
|
|
|
|
for (const QString &p : dirList) {
|
|
|
|
// remove unwanted files
|
|
|
|
for (const QString &f : deleteFilesList) {
|
|
|
|
forceRemove(p + f);
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
2015-08-09 15:27:56 +08:00
|
|
|
|
|
|
|
// remove temp files on linux (file ends with '~'), e.g. `filename~`
|
|
|
|
QDir dir(p);
|
|
|
|
QStringList tmpFileList = dir.entryList(QDir::Files);
|
|
|
|
for (const QString &f : tmpFileList) {
|
|
|
|
if (f.endsWith("~"))
|
|
|
|
forceRemove(p + f);
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
2015-08-09 15:27:56 +08:00
|
|
|
|
|
|
|
// remove directory if empty
|
|
|
|
dir.rmdir(p);
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
2015-08-09 15:27:56 +08:00
|
|
|
|
|
|
|
return QDir(path).exists();
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes the file with the given file_path.
|
|
|
|
*
|
|
|
|
* This function will try to fix the file permissions before removing it.
|
|
|
|
*/
|
2017-08-15 16:17:57 +03:00
|
|
|
bool Utils::Fs::forceRemove(const QString &filePath)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-08-15 16:17:57 +03:00
|
|
|
QFile f(filePath);
|
2015-05-06 14:53:27 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-06-11 20:43:41 +03:00
|
|
|
/**
|
|
|
|
* Removes directory and its content recursively.
|
|
|
|
*
|
|
|
|
*/
|
2017-04-26 13:15:37 +03:00
|
|
|
void Utils::Fs::removeDirRecursive(const QString &path)
|
2015-06-17 19:18:35 +03:00
|
|
|
{
|
2017-04-26 13:15:37 +03:00
|
|
|
if (!path.isEmpty())
|
|
|
|
QDir(path).removeRecursively();
|
2015-06-11 20:43:41 +03:00
|
|
|
}
|
|
|
|
|
2015-05-06 14:53:27 +03:00
|
|
|
/**
|
|
|
|
* Returns the size of a file.
|
|
|
|
* If the file is a folder, it will compute its size based on its content.
|
|
|
|
*
|
|
|
|
* Returns -1 in case of error.
|
|
|
|
*/
|
2017-08-15 16:17:57 +03:00
|
|
|
qint64 Utils::Fs::computePathSize(const QString &path)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
|
|
|
// Check if it is a file
|
|
|
|
QFileInfo fi(path);
|
|
|
|
if (!fi.exists()) return -1;
|
|
|
|
if (fi.isFile()) return fi.size();
|
2016-04-28 19:04:51 +08:00
|
|
|
|
2015-05-06 14:53:27 +03:00
|
|
|
// Compute folder size based on its content
|
|
|
|
qint64 size = 0;
|
2016-04-28 19:04:51 +08:00
|
|
|
QDirIterator iter(path, QDir::Files | QDir::Hidden | QDir::NoSymLinks, QDirIterator::Subdirectories);
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
iter.next();
|
|
|
|
size += iter.fileInfo().size();
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes deep comparison of two files to make sure they are identical.
|
|
|
|
*/
|
2017-08-15 16:17:57 +03:00
|
|
|
bool Utils::Fs::sameFiles(const QString &path1, const QString &path2)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
|
|
|
QFile f1(path1), f2(path2);
|
|
|
|
if (!f1.exists() || !f2.exists()) return false;
|
|
|
|
if (f1.size() != f2.size()) return false;
|
|
|
|
if (!f1.open(QIODevice::ReadOnly)) return false;
|
2016-04-28 19:04:51 +08:00
|
|
|
if (!f2.open(QIODevice::ReadOnly)) return false;
|
|
|
|
|
|
|
|
const int readSize = 1024 * 1024; // 1 MiB
|
2015-05-06 14:53:27 +03:00
|
|
|
while(!f1.atEnd() && !f2.atEnd()) {
|
2016-04-28 19:04:51 +08:00
|
|
|
if (f1.read(readSize) != f2.read(readSize))
|
|
|
|
return false;
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
2016-04-28 19:04:51 +08:00
|
|
|
return true;
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-03-07 16:10:42 +03:00
|
|
|
QString Utils::Fs::toValidFileSystemName(const QString &name, bool allowSeparators, const QString &pad)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
QRegExp regex(allowSeparators ? "[:?\"*<>|]+" : "[\\\\/:?\"*<>|]+");
|
2015-12-01 17:41:56 +03:00
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
QString validName = name.trimmed();
|
2017-03-07 16:10:42 +03:00
|
|
|
validName.replace(regex, pad);
|
2016-02-09 11:56:48 +03:00
|
|
|
qDebug() << "toValidFileSystemName:" << name << "=>" << validName;
|
2015-12-01 17:41:56 +03:00
|
|
|
|
|
|
|
return validName;
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
bool Utils::Fs::isValidFileSystemName(const QString &name, bool allowSeparators)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
if (name.isEmpty()) return false;
|
|
|
|
|
|
|
|
QRegExp regex(allowSeparators ? "[:?\"*<>|]" : "[\\\\/:?\"*<>|]");
|
|
|
|
return !name.contains(regex);
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-05-14 23:57:24 +02:00
|
|
|
qint64 Utils::Fs::freeDiskSpaceOnPath(const QString &path)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-05-14 23:57:24 +02:00
|
|
|
if (path.isEmpty()) return -1;
|
2016-12-08 01:14:55 +08:00
|
|
|
|
2017-05-14 23:57:24 +02:00
|
|
|
return QStorageInfo(path).bytesAvailable();
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2017-08-15 16:17:57 +03:00
|
|
|
QString Utils::Fs::branchPath(const QString &filePath, QString *removed)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-08-15 16:17:57 +03:00
|
|
|
QString ret = fromNativePath(filePath);
|
2015-05-06 14:53:27 +03:00
|
|
|
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)
|
|
|
|
{
|
2017-08-15 16:17:57 +03:00
|
|
|
QString ret = path.trimmed();
|
2015-05-06 14:53:27 +03:00
|
|
|
if (ret.isEmpty())
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
return QDir::cleanPath(ret);
|
|
|
|
}
|
|
|
|
|
2017-08-15 16:17:57 +03:00
|
|
|
QString Utils::Fs::expandPathAbs(const QString &path)
|
2015-05-06 14:53:27 +03:00
|
|
|
{
|
2017-08-15 16:17:57 +03:00
|
|
|
return QDir(expandPath(path)).absolutePath();
|
2015-05-06 14:53:27 +03:00
|
|
|
}
|
|
|
|
|
2016-11-29 17:55:58 +08:00
|
|
|
QString Utils::Fs::tempPath()
|
|
|
|
{
|
|
|
|
static const QString path = QDir::tempPath() + "/.qBittorrent/";
|
|
|
|
QDir().mkdir(path);
|
|
|
|
return path;
|
|
|
|
}
|