2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-03-30 12:06:05 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-03-30 12:06:05 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-03-30 12:06:05 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:58:39 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2011-03-30 12:06:05 +02:00
|
|
|
**
|
2016-01-15 14:58:39 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-03-30 12:06:05 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-03-30 12:06:05 +02:00
|
|
|
|
|
|
|
|
#include "fileutils.h"
|
|
|
|
|
#include "savefile.h"
|
|
|
|
|
|
2014-09-10 14:00:12 +02:00
|
|
|
#include "algorithm.h"
|
2021-05-11 14:34:56 +02:00
|
|
|
#include "commandline.h"
|
2021-06-07 15:47:06 +02:00
|
|
|
#include "environment.h"
|
2011-03-30 12:06:05 +02:00
|
|
|
#include "qtcassert.h"
|
|
|
|
|
|
2017-08-18 14:32:39 +02:00
|
|
|
#include <QDataStream>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDateTime>
|
2020-06-11 15:41:26 +02:00
|
|
|
#include <QDebug>
|
2019-04-04 23:03:03 +03:00
|
|
|
#include <QOperatingSystemVersion>
|
2020-05-27 11:07:43 +02:00
|
|
|
#include <QRegularExpression>
|
2014-07-07 16:16:49 +02:00
|
|
|
#include <QTimer>
|
2014-07-09 18:21:39 +02:00
|
|
|
#include <QUrl>
|
2019-04-04 23:03:03 +03:00
|
|
|
#include <qplatformdefs.h>
|
2011-03-30 12:06:05 +02:00
|
|
|
|
2017-08-18 14:32:39 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#endif
|
|
|
|
|
|
2013-10-23 22:07:46 +03:00
|
|
|
#ifdef Q_OS_WIN
|
2021-05-05 10:28:07 +02:00
|
|
|
#ifdef QTCREATOR_PCH_H
|
2021-05-01 21:04:48 +02:00
|
|
|
#define CALLBACK WINAPI
|
2021-05-05 10:28:07 +02:00
|
|
|
#endif
|
2013-10-23 22:07:46 +03:00
|
|
|
#include <qt_windows.h>
|
2018-07-19 00:07:58 +03:00
|
|
|
#include <shlobj.h>
|
2013-10-23 22:07:46 +03:00
|
|
|
#endif
|
|
|
|
|
|
2015-09-01 15:59:27 +02:00
|
|
|
#ifdef Q_OS_OSX
|
|
|
|
|
#include "fileutils_mac.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2014-02-13 11:49:13 +01:00
|
|
|
QT_BEGIN_NAMESPACE
|
2019-05-28 13:49:26 +02:00
|
|
|
QDebug operator<<(QDebug dbg, const Utils::FilePath &c)
|
2014-02-13 11:49:13 +01:00
|
|
|
{
|
2021-05-17 15:15:50 +02:00
|
|
|
return dbg << c.toUserOutput();
|
2014-02-13 11:49:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
|
2021-04-27 12:05:13 +02:00
|
|
|
static DeviceFileHooks s_deviceHooks;
|
|
|
|
|
|
2011-07-05 12:59:14 +02:00
|
|
|
/*! \class Utils::FileUtils
|
|
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The FileUtils class contains file and directory related convenience
|
|
|
|
|
functions.
|
2011-07-05 12:59:14 +02:00
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
Removes the directory \a filePath and its subdirectories recursively.
|
|
|
|
|
|
|
|
|
|
\note The \a error parameter is optional.
|
|
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
Returns whether the operation succeeded.
|
2011-07-05 12:59:14 +02:00
|
|
|
*/
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FileUtils::removeRecursively(const FilePath &filePath, QString *error)
|
2011-07-05 12:59:14 +02:00
|
|
|
{
|
2021-06-28 15:26:44 +02:00
|
|
|
QTC_ASSERT(!filePath.needsDevice(), return false);
|
2012-09-05 15:52:53 +02:00
|
|
|
QFileInfo fileInfo = filePath.toFileInfo();
|
2012-03-27 12:00:59 +02:00
|
|
|
if (!fileInfo.exists() && !fileInfo.isSymLink())
|
2011-07-05 12:59:14 +02:00
|
|
|
return true;
|
2012-09-05 15:52:53 +02:00
|
|
|
QFile::setPermissions(filePath.toString(), fileInfo.permissions() | QFile::WriteUser);
|
2011-07-05 12:59:14 +02:00
|
|
|
if (fileInfo.isDir()) {
|
2012-09-05 15:52:53 +02:00
|
|
|
QDir dir(filePath.toString());
|
2019-02-11 10:17:53 +01:00
|
|
|
dir.setPath(dir.canonicalPath());
|
2011-07-21 14:49:16 +02:00
|
|
|
if (dir.isRoot()) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
|
|
|
|
"Refusing to remove root directory.");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (dir.path() == QDir::home().canonicalPath()) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
|
|
|
|
"Refusing to remove your home directory.");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 23:39:57 +03:00
|
|
|
const QStringList fileNames = dir.entryList(
|
|
|
|
|
QDir::Files | QDir::Hidden | QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
for (const QString &fileName : fileNames) {
|
2020-02-19 17:35:56 +01:00
|
|
|
if (!removeRecursively(filePath / fileName, error))
|
2011-07-05 12:59:14 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2011-07-21 14:49:16 +02:00
|
|
|
if (!QDir::root().rmdir(dir.path())) {
|
2011-07-05 12:59:14 +02:00
|
|
|
if (error) {
|
2014-04-17 14:09:47 +02:00
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove directory \"%1\".")
|
2012-09-05 15:52:53 +02:00
|
|
|
.arg(filePath.toUserOutput());
|
2011-07-05 12:59:14 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2012-09-05 15:52:53 +02:00
|
|
|
if (!QFile::remove(filePath.toString())) {
|
2011-07-05 12:59:14 +02:00
|
|
|
if (error) {
|
2014-04-17 14:09:47 +02:00
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to remove file \"%1\".")
|
2012-09-05 15:52:53 +02:00
|
|
|
.arg(filePath.toUserOutput());
|
2011-07-05 12:59:14 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 22:56:26 +02:00
|
|
|
/*!
|
|
|
|
|
Copies the directory specified by \a srcFilePath recursively to \a tgtFilePath. \a tgtFilePath will contain
|
|
|
|
|
the target directory, which will be created. Example usage:
|
|
|
|
|
|
|
|
|
|
\code
|
|
|
|
|
QString error;
|
|
|
|
|
book ok = Utils::FileUtils::copyRecursively("/foo/bar", "/foo/baz", &error);
|
|
|
|
|
if (!ok)
|
|
|
|
|
qDebug() << error;
|
|
|
|
|
\endcode
|
|
|
|
|
|
|
|
|
|
This will copy the contents of /foo/bar into to the baz directory under /foo, which will be created in the process.
|
|
|
|
|
|
|
|
|
|
\note The \a error parameter is optional.
|
|
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
Returns whether the operation succeeded.
|
2011-10-20 22:56:26 +02:00
|
|
|
*/
|
2020-07-07 10:11:53 +02:00
|
|
|
|
|
|
|
|
bool FileUtils::copyRecursively(const FilePath &srcFilePath, const FilePath &tgtFilePath, QString *error)
|
2011-07-05 12:59:14 +02:00
|
|
|
{
|
2020-07-07 10:11:53 +02:00
|
|
|
return copyRecursively(
|
|
|
|
|
srcFilePath, tgtFilePath, error, [](const QFileInfo &src, const QFileInfo &dest, QString *error) {
|
|
|
|
|
if (!QFile::copy(src.filePath(), dest.filePath())) {
|
2014-09-22 16:20:51 +03:00
|
|
|
if (error) {
|
2020-07-07 10:11:53 +02:00
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
|
|
|
|
"Could not copy file \"%1\" to \"%2\".")
|
|
|
|
|
.arg(FilePath::fromFileInfo(src).toUserOutput(),
|
|
|
|
|
FilePath::fromFileInfo(dest).toUserOutput());
|
2014-09-22 16:20:51 +03:00
|
|
|
}
|
|
|
|
|
return false;
|
2011-07-05 12:59:14 +02:00
|
|
|
}
|
2020-07-07 10:11:53 +02:00
|
|
|
return true;
|
|
|
|
|
});
|
2011-07-05 12:59:14 +02:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 15:57:16 +01:00
|
|
|
/*!
|
|
|
|
|
Copies a file specified by \a srcFilePath to \a tgtFilePath only if \a srcFilePath is different
|
2021-06-09 15:40:06 +02:00
|
|
|
(file contents and last modification time).
|
2021-01-11 15:57:16 +01:00
|
|
|
|
|
|
|
|
Returns whether the operation succeeded.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
bool FileUtils::copyIfDifferent(const FilePath &srcFilePath, const FilePath &tgtFilePath)
|
|
|
|
|
{
|
2021-06-09 15:40:06 +02:00
|
|
|
QTC_ASSERT(srcFilePath.exists(), return false);
|
|
|
|
|
QTC_ASSERT(srcFilePath.scheme() == tgtFilePath.scheme(), return false);
|
|
|
|
|
QTC_ASSERT(srcFilePath.host() == tgtFilePath.host(), return false);
|
|
|
|
|
|
|
|
|
|
if (tgtFilePath.exists()) {
|
|
|
|
|
const QDateTime srcModified = srcFilePath.lastModified();
|
|
|
|
|
const QDateTime tgtModified = tgtFilePath.lastModified();
|
|
|
|
|
if (srcModified == tgtModified) {
|
|
|
|
|
const QByteArray srcContents = srcFilePath.fileContents();
|
|
|
|
|
const QByteArray tgtContents = srcFilePath.fileContents();
|
|
|
|
|
if (srcContents == tgtContents)
|
|
|
|
|
return true;
|
2021-01-11 15:57:16 +01:00
|
|
|
}
|
2021-06-09 15:40:06 +02:00
|
|
|
tgtFilePath.removeFile();
|
2021-01-11 15:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-09 15:40:06 +02:00
|
|
|
return srcFilePath.copyFile(tgtFilePath);
|
2021-01-11 15:57:16 +01:00
|
|
|
}
|
|
|
|
|
|
2011-10-20 22:56:26 +02:00
|
|
|
/*!
|
2019-05-17 13:36:48 +02:00
|
|
|
If this is a directory, the function will recursively check all files and return
|
|
|
|
|
true if one of them is newer than \a timeStamp. If this is a single file, true will
|
2013-09-25 18:19:45 +02:00
|
|
|
be returned if the file is newer than \a timeStamp.
|
2011-10-20 22:56:26 +02:00
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
Returns whether at least one file in \a filePath has a newer date than
|
|
|
|
|
\a timeStamp.
|
2011-10-20 22:56:26 +02:00
|
|
|
*/
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::isNewerThan(const QDateTime &timeStamp) const
|
2011-07-05 12:59:14 +02:00
|
|
|
{
|
2019-05-17 13:36:48 +02:00
|
|
|
const QFileInfo fileInfo = toFileInfo();
|
2011-07-05 12:59:14 +02:00
|
|
|
if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp)
|
|
|
|
|
return true;
|
|
|
|
|
if (fileInfo.isDir()) {
|
2019-05-17 13:36:48 +02:00
|
|
|
const QStringList dirContents = QDir(toString())
|
2011-07-05 12:59:14 +02:00
|
|
|
.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
|
2019-05-17 13:36:48 +02:00
|
|
|
for (const QString &curFileName : dirContents) {
|
|
|
|
|
if (pathAppended(curFileName).isNewerThan(timeStamp))
|
2011-07-05 12:59:14 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 09:28:57 +02:00
|
|
|
Qt::CaseSensitivity FilePath::caseSensitivity() const
|
|
|
|
|
{
|
|
|
|
|
if (m_scheme.isEmpty())
|
|
|
|
|
return HostOsInfo::fileNameCaseSensitivity();
|
|
|
|
|
|
|
|
|
|
// FIXME: This could or possibly should the target device's file name case sensitivity
|
|
|
|
|
// into account by diverting to IDevice. However, as this is expensive and we are
|
|
|
|
|
// in time-critical path here, we go with "good enough" for now:
|
|
|
|
|
// The first approximation is "Anything unusual is not case sensitive"
|
|
|
|
|
return Qt::CaseSensitive;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 22:56:26 +02:00
|
|
|
/*!
|
2016-06-18 15:11:12 +02:00
|
|
|
Recursively resolves symlinks if \a filePath is a symlink.
|
2017-08-24 08:09:37 +03:00
|
|
|
To resolve symlinks anywhere in the path, see canonicalPath.
|
|
|
|
|
Unlike QFileInfo::canonicalFilePath(), this function will still return the expected deepest
|
|
|
|
|
target file even if the symlink is dangling.
|
2011-10-20 22:56:26 +02:00
|
|
|
|
|
|
|
|
\note Maximum recursion depth == 16.
|
|
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
Returns the symlink target file path.
|
2011-10-20 22:56:26 +02:00
|
|
|
*/
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FileUtils::resolveSymlinks(const FilePath &path)
|
2011-10-20 22:33:54 +02:00
|
|
|
{
|
2012-09-05 15:52:53 +02:00
|
|
|
QFileInfo f = path.toFileInfo();
|
2011-10-20 22:33:54 +02:00
|
|
|
int links = 16;
|
|
|
|
|
while (links-- && f.isSymLink())
|
2017-08-24 07:53:15 +03:00
|
|
|
f.setFile(f.dir(), f.symLinkTarget());
|
2011-10-20 22:33:54 +02:00
|
|
|
if (links <= 0)
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath();
|
|
|
|
|
return FilePath::fromString(f.filePath());
|
2011-10-20 22:33:54 +02:00
|
|
|
}
|
2011-07-05 12:59:14 +02:00
|
|
|
|
2016-06-18 15:11:12 +02:00
|
|
|
/*!
|
2019-05-17 13:44:09 +02:00
|
|
|
Recursively resolves possibly present symlinks in this file name.
|
2016-06-18 15:11:12 +02:00
|
|
|
Unlike QFileInfo::canonicalFilePath(), this function will not return an empty
|
|
|
|
|
string if path doesn't exist.
|
|
|
|
|
|
|
|
|
|
Returns the canonical path.
|
|
|
|
|
*/
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::canonicalPath() const
|
2016-06-18 15:11:12 +02:00
|
|
|
{
|
2021-06-01 14:15:06 +02:00
|
|
|
if (needsDevice()) {
|
|
|
|
|
// FIXME: Not a full solution, but it stays on the right device.
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
2019-05-17 13:44:09 +02:00
|
|
|
const QString result = toFileInfo().canonicalFilePath();
|
2016-06-18 15:11:12 +02:00
|
|
|
if (result.isEmpty())
|
2019-05-17 13:44:09 +02:00
|
|
|
return *this;
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromString(result);
|
2016-06-18 15:11:12 +02:00
|
|
|
}
|
|
|
|
|
|
2020-02-19 17:35:56 +01:00
|
|
|
FilePath FilePath::operator/(const QString &str) const
|
|
|
|
|
{
|
|
|
|
|
return pathAppended(str);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 12:05:13 +02:00
|
|
|
void FilePath::clear()
|
|
|
|
|
{
|
|
|
|
|
m_data.clear();
|
2021-05-18 18:03:10 +02:00
|
|
|
m_host.clear();
|
|
|
|
|
m_scheme.clear();
|
2021-04-27 12:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FilePath::isEmpty() const
|
|
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
return m_data.isEmpty();
|
2021-04-27 12:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
2012-08-24 15:00:44 +02:00
|
|
|
/*!
|
|
|
|
|
Like QDir::toNativeSeparators(), but use prefix '~' instead of $HOME on unix systems when an
|
|
|
|
|
absolute path is given.
|
|
|
|
|
|
2013-09-06 13:23:11 +02:00
|
|
|
Returns the possibly shortened path with native separators.
|
2012-08-24 15:00:44 +02:00
|
|
|
*/
|
2019-05-28 13:49:26 +02:00
|
|
|
QString FilePath::shortNativePath() const
|
2012-08-24 15:00:44 +02:00
|
|
|
{
|
|
|
|
|
if (HostOsInfo::isAnyUnixHost()) {
|
2021-06-08 13:51:34 +02:00
|
|
|
const FilePath home = FileUtils::homePath();
|
2019-05-17 13:54:27 +02:00
|
|
|
if (isChildOf(home)) {
|
2012-08-24 15:00:44 +02:00
|
|
|
return QLatin1Char('~') + QDir::separator()
|
2019-05-17 13:54:27 +02:00
|
|
|
+ QDir::toNativeSeparators(relativeChildPath(home).toString());
|
2012-08-24 15:00:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-05-17 13:54:27 +02:00
|
|
|
return toUserOutput();
|
2012-08-24 15:00:44 +02:00
|
|
|
}
|
|
|
|
|
|
2013-08-16 17:10:45 +02:00
|
|
|
QString FileUtils::fileSystemFriendlyName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
QString result = name;
|
2020-05-27 11:07:43 +02:00
|
|
|
result.replace(QRegularExpression(QLatin1String("\\W")), QLatin1String("_"));
|
|
|
|
|
result.replace(QRegularExpression(QLatin1String("_+")), QLatin1String("_")); // compact _
|
|
|
|
|
result.remove(QRegularExpression(QLatin1String("^_*"))); // remove leading _
|
|
|
|
|
result.remove(QRegularExpression(QLatin1String("_+$"))); // remove trailing _
|
2013-08-16 17:10:45 +02:00
|
|
|
if (result.isEmpty())
|
|
|
|
|
result = QLatin1String("unknown");
|
|
|
|
|
return result;
|
2013-12-09 12:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileUtils::indexOfQmakeUnfriendly(const QString &name, int startpos)
|
|
|
|
|
{
|
2020-05-27 11:07:43 +02:00
|
|
|
static const QRegularExpression checkRegExp(QLatin1String("[^a-zA-Z0-9_.-]"));
|
|
|
|
|
return checkRegExp.match(name, startpos).capturedStart();
|
2013-12-09 12:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileUtils::qmakeFriendlyName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
QString result = name;
|
|
|
|
|
|
|
|
|
|
// Remove characters that might trip up a build system (especially qmake):
|
|
|
|
|
int pos = indexOfQmakeUnfriendly(result);
|
|
|
|
|
while (pos >= 0) {
|
|
|
|
|
result[pos] = QLatin1Char('_');
|
|
|
|
|
pos = indexOfQmakeUnfriendly(result, pos);
|
|
|
|
|
}
|
|
|
|
|
return fileSystemFriendlyName(result);
|
2013-08-16 17:10:45 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FileUtils::makeWritable(const FilePath &path)
|
2012-10-15 11:53:22 +02:00
|
|
|
{
|
2019-05-28 15:55:45 +02:00
|
|
|
const QString filePath = path.toString();
|
|
|
|
|
return QFile::setPermissions(filePath, QFile::permissions(filePath) | QFile::WriteUser);
|
2012-10-15 11:53:22 +02:00
|
|
|
}
|
|
|
|
|
|
2015-09-01 15:59:27 +02:00
|
|
|
// makes sure that capitalization of directories is canonical on Windows and OS X.
|
2013-10-23 22:07:46 +03:00
|
|
|
// This mimics the logic in QDeclarative_isFileCaseCorrect
|
|
|
|
|
QString FileUtils::normalizePathName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
#ifdef Q_OS_WIN
|
2013-12-04 07:37:07 +01:00
|
|
|
const QString nativeSeparatorName(QDir::toNativeSeparators(name));
|
2018-07-19 16:39:41 +02:00
|
|
|
const auto nameC = reinterpret_cast<LPCTSTR>(nativeSeparatorName.utf16()); // MinGW
|
2013-12-04 07:37:07 +01:00
|
|
|
PIDLIST_ABSOLUTE file;
|
|
|
|
|
HRESULT hr = SHParseDisplayName(nameC, NULL, &file, 0, NULL);
|
|
|
|
|
if (FAILED(hr))
|
2013-10-23 22:07:46 +03:00
|
|
|
return name;
|
2013-12-04 07:37:07 +01:00
|
|
|
TCHAR buffer[MAX_PATH];
|
2019-03-06 09:18:32 +01:00
|
|
|
const bool success = SHGetPathFromIDList(file, buffer);
|
|
|
|
|
ILFree(file);
|
|
|
|
|
return success ? QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const ushort *>(buffer)))
|
|
|
|
|
: name;
|
2015-09-01 15:59:27 +02:00
|
|
|
#elif defined(Q_OS_OSX)
|
|
|
|
|
return Internal::normalizePathName(name);
|
|
|
|
|
#else // do not try to handle case-insensitive file systems on Linux
|
2013-10-23 22:07:46 +03:00
|
|
|
return name;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-11 17:45:11 +02:00
|
|
|
bool FileUtils::isRelativePath(const QString &path)
|
|
|
|
|
{
|
|
|
|
|
if (path.startsWith(QLatin1Char('/')))
|
|
|
|
|
return false;
|
2014-05-02 14:47:39 +02:00
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
|
if (path.startsWith(QLatin1Char('\\')))
|
|
|
|
|
return false;
|
|
|
|
|
// Unlike QFileInfo, this won't accept a relative path with a drive letter.
|
|
|
|
|
// Such paths result in a royal mess anyway ...
|
|
|
|
|
if (path.length() >= 3 && path.at(1) == QLatin1Char(':') && path.at(0).isLetter()
|
|
|
|
|
&& (path.at(2) == QLatin1Char('/') || path.at(2) == QLatin1Char('\\')))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-04-11 17:45:11 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-04 13:36:04 +01:00
|
|
|
FilePath FilePath::resolvePath(const QString &fileName) const
|
2014-04-11 17:45:11 +02:00
|
|
|
{
|
|
|
|
|
if (fileName.isEmpty())
|
2019-12-04 13:36:04 +01:00
|
|
|
return {}; // FIXME: Isn't this odd?
|
|
|
|
|
if (FileUtils::isAbsolutePath(fileName))
|
|
|
|
|
return FilePath::fromString(QDir::cleanPath(fileName));
|
|
|
|
|
return FilePath::fromString(QDir::cleanPath(toString() + QLatin1Char('/') + fileName));
|
2014-04-11 17:45:11 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 12:05:13 +02:00
|
|
|
FilePath FilePath::resolveSymlinkTarget() const
|
|
|
|
|
{
|
|
|
|
|
// FIXME: implement
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-10 08:07:32 +02:00
|
|
|
FilePath FilePath::cleanPath() const
|
|
|
|
|
{
|
|
|
|
|
FilePath result = *this;
|
|
|
|
|
result.setPath(QDir::cleanPath(result.path()));
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
FilePath FileUtils::commonPath(const FilePath &oldCommonPath, const FilePath &filePath)
|
2018-10-30 14:21:34 +01:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath newCommonPath = oldCommonPath;
|
2019-05-28 15:55:45 +02:00
|
|
|
while (!newCommonPath.isEmpty() && !filePath.isChildOf(newCommonPath))
|
2018-10-30 14:21:34 +01:00
|
|
|
newCommonPath = newCommonPath.parentDir();
|
2019-05-17 13:44:09 +02:00
|
|
|
return newCommonPath.canonicalPath();
|
2018-10-30 14:21:34 +01:00
|
|
|
}
|
2014-04-11 17:45:11 +02:00
|
|
|
|
2019-04-04 23:03:03 +03:00
|
|
|
// Copied from qfilesystemengine_win.cpp
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
|
|
|
|
|
// File ID for Windows up to version 7.
|
|
|
|
|
static inline QByteArray fileIdWin7(HANDLE handle)
|
|
|
|
|
{
|
|
|
|
|
BY_HANDLE_FILE_INFORMATION info;
|
|
|
|
|
if (GetFileInformationByHandle(handle, &info)) {
|
|
|
|
|
char buffer[sizeof "01234567:0123456701234567\0"];
|
|
|
|
|
qsnprintf(buffer, sizeof(buffer), "%lx:%08lx%08lx",
|
|
|
|
|
info.dwVolumeSerialNumber,
|
|
|
|
|
info.nFileIndexHigh,
|
|
|
|
|
info.nFileIndexLow);
|
|
|
|
|
return QByteArray(buffer);
|
|
|
|
|
}
|
|
|
|
|
return QByteArray();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// File ID for Windows starting from version 8.
|
|
|
|
|
static QByteArray fileIdWin8(HANDLE handle)
|
|
|
|
|
{
|
|
|
|
|
QByteArray result;
|
|
|
|
|
FILE_ID_INFO infoEx;
|
|
|
|
|
if (GetFileInformationByHandleEx(handle,
|
|
|
|
|
static_cast<FILE_INFO_BY_HANDLE_CLASS>(18), // FileIdInfo in Windows 8
|
|
|
|
|
&infoEx, sizeof(FILE_ID_INFO))) {
|
|
|
|
|
result = QByteArray::number(infoEx.VolumeSerialNumber, 16);
|
|
|
|
|
result += ':';
|
|
|
|
|
// Note: MinGW-64's definition of FILE_ID_128 differs from the MSVC one.
|
|
|
|
|
result += QByteArray(reinterpret_cast<const char *>(&infoEx.FileId), int(sizeof(infoEx.FileId))).toHex();
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QByteArray fileIdWin(HANDLE fHandle)
|
|
|
|
|
{
|
|
|
|
|
return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows8 ?
|
|
|
|
|
fileIdWin8(HANDLE(fHandle)) : fileIdWin7(HANDLE(fHandle));
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QByteArray FileUtils::fileId(const FilePath &fileName)
|
2019-04-04 23:03:03 +03:00
|
|
|
{
|
|
|
|
|
QByteArray result;
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
const HANDLE handle =
|
|
|
|
|
CreateFile((wchar_t*)fileName.toUserOutput().utf16(), 0,
|
|
|
|
|
FILE_SHARE_READ, NULL, OPEN_EXISTING,
|
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
|
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
|
result = fileIdWin(handle);
|
|
|
|
|
CloseHandle(handle);
|
|
|
|
|
}
|
|
|
|
|
#else // Copied from qfilesystemengine_unix.cpp
|
|
|
|
|
if (Q_UNLIKELY(fileName.isEmpty()))
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
QT_STATBUF statResult;
|
|
|
|
|
if (QT_STAT(fileName.toString().toLocal8Bit().constData(), &statResult))
|
|
|
|
|
return result;
|
|
|
|
|
result = QByteArray::number(quint64(statResult.st_dev), 16);
|
|
|
|
|
result += ':';
|
|
|
|
|
result += QByteArray::number(quint64(statResult.st_ino));
|
|
|
|
|
#endif
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 13:51:34 +02:00
|
|
|
FilePath FileUtils::homePath()
|
|
|
|
|
{
|
|
|
|
|
return FilePath::fromString(QDir::cleanPath(QDir::homePath()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-11 14:33:58 +02:00
|
|
|
bool FileUtils::renameFile(const FilePath &srcFilePath, const FilePath &tgtFilePath)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!srcFilePath.needsDevice(), return false);
|
|
|
|
|
QTC_ASSERT(srcFilePath.scheme() == tgtFilePath.scheme(), return false);
|
|
|
|
|
return QFile::rename(srcFilePath.path(), tgtFilePath.path());
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
QByteArray FileReader::fetchQrc(const QString &fileName)
|
|
|
|
|
{
|
2021-05-18 09:47:07 +02:00
|
|
|
QTC_ASSERT(fileName.startsWith(':'), return QByteArray());
|
2011-03-30 12:06:05 +02:00
|
|
|
QFile file(fileName);
|
|
|
|
|
bool ok = file.open(QIODevice::ReadOnly);
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(ok, qWarning() << fileName << "not there!"; return QByteArray());
|
2011-03-30 12:06:05 +02:00
|
|
|
return file.readAll();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 09:47:07 +02:00
|
|
|
bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode)
|
2011-03-30 12:06:05 +02:00
|
|
|
{
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(!(mode & ~(QIODevice::ReadOnly | QIODevice::Text)), return false);
|
2011-03-30 12:06:05 +02:00
|
|
|
|
2021-05-18 09:47:07 +02:00
|
|
|
if (filePath.needsDevice()) {
|
|
|
|
|
// TODO: add error handling to FilePath::fileContents
|
|
|
|
|
m_data = filePath.fileContents();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile file(filePath.toString());
|
2011-03-30 12:06:05 +02:00
|
|
|
if (!file.open(QIODevice::ReadOnly | mode)) {
|
|
|
|
|
m_errorString = tr("Cannot open %1 for reading: %2").arg(
|
2021-05-18 09:47:07 +02:00
|
|
|
filePath.toUserOutput(), file.errorString());
|
2011-03-30 12:06:05 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_data = file.readAll();
|
|
|
|
|
if (file.error() != QFile::NoError) {
|
|
|
|
|
m_errorString = tr("Cannot read %1: %2").arg(
|
2021-05-18 09:47:07 +02:00
|
|
|
filePath.toUserOutput(), file.errorString());
|
2011-03-30 12:06:05 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 09:47:07 +02:00
|
|
|
bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode, QString *errorString)
|
2011-03-30 12:06:05 +02:00
|
|
|
{
|
2021-05-18 09:47:07 +02:00
|
|
|
if (fetch(filePath, mode))
|
2011-03-30 12:06:05 +02:00
|
|
|
return true;
|
|
|
|
|
if (errorString)
|
|
|
|
|
*errorString = m_errorString;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 14:32:39 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
2021-05-18 09:47:07 +02:00
|
|
|
bool FileReader::fetch(const FilePath &filePath, QIODevice::OpenMode mode, QWidget *parent)
|
2011-03-30 12:06:05 +02:00
|
|
|
{
|
2021-05-18 09:47:07 +02:00
|
|
|
if (fetch(filePath, mode))
|
2011-03-30 12:06:05 +02:00
|
|
|
return true;
|
|
|
|
|
if (parent)
|
|
|
|
|
QMessageBox::critical(parent, tr("File Error"), m_errorString);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-18 14:32:39 +02:00
|
|
|
#endif // QT_GUI_LIB
|
2011-03-30 12:06:05 +02:00
|
|
|
|
2018-07-23 10:45:40 +02:00
|
|
|
FileSaverBase::FileSaverBase() = default;
|
2011-03-30 12:06:05 +02:00
|
|
|
|
2017-07-03 12:02:26 +02:00
|
|
|
FileSaverBase::~FileSaverBase() = default;
|
|
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
bool FileSaverBase::finalize()
|
|
|
|
|
{
|
|
|
|
|
m_file->close();
|
|
|
|
|
setResult(m_file->error() == QFile::NoError);
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset();
|
2011-03-30 12:06:05 +02:00
|
|
|
return !m_hasError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaverBase::finalize(QString *errStr)
|
|
|
|
|
{
|
|
|
|
|
if (finalize())
|
|
|
|
|
return true;
|
|
|
|
|
if (errStr)
|
|
|
|
|
*errStr = errorString();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 14:32:39 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
2011-03-30 12:06:05 +02:00
|
|
|
bool FileSaverBase::finalize(QWidget *parent)
|
|
|
|
|
{
|
|
|
|
|
if (finalize())
|
|
|
|
|
return true;
|
|
|
|
|
QMessageBox::critical(parent, tr("File Error"), errorString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2017-08-18 14:32:39 +02:00
|
|
|
#endif // QT_GUI_LIB
|
2011-03-30 12:06:05 +02:00
|
|
|
|
|
|
|
|
bool FileSaverBase::write(const char *data, int len)
|
|
|
|
|
{
|
|
|
|
|
if (m_hasError)
|
|
|
|
|
return false;
|
|
|
|
|
return setResult(m_file->write(data, len) == len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaverBase::write(const QByteArray &bytes)
|
|
|
|
|
{
|
|
|
|
|
if (m_hasError)
|
|
|
|
|
return false;
|
|
|
|
|
return setResult(m_file->write(bytes) == bytes.count());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaverBase::setResult(bool ok)
|
|
|
|
|
{
|
|
|
|
|
if (!ok && !m_hasError) {
|
2017-06-29 15:16:26 +02:00
|
|
|
if (!m_file->errorString().isEmpty()) {
|
2021-05-18 09:47:07 +02:00
|
|
|
m_errorString = tr("Cannot write file %1: %2")
|
|
|
|
|
.arg(m_filePath.toUserOutput(), m_file->errorString());
|
2017-06-29 15:16:26 +02:00
|
|
|
} else {
|
2021-05-18 09:47:07 +02:00
|
|
|
m_errorString = tr("Cannot write file %1. Disk full?").arg(m_filePath.toUserOutput());
|
2017-06-29 15:16:26 +02:00
|
|
|
}
|
2011-03-30 12:06:05 +02:00
|
|
|
m_hasError = true;
|
|
|
|
|
}
|
|
|
|
|
return ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaverBase::setResult(QTextStream *stream)
|
|
|
|
|
{
|
|
|
|
|
stream->flush();
|
|
|
|
|
return setResult(stream->status() == QTextStream::Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaverBase::setResult(QDataStream *stream)
|
|
|
|
|
{
|
2011-04-18 15:40:10 +02:00
|
|
|
return setResult(stream->status() == QDataStream::Ok);
|
2011-03-30 12:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaverBase::setResult(QXmlStreamWriter *stream)
|
|
|
|
|
{
|
|
|
|
|
return setResult(!stream->hasError());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-05-18 09:47:07 +02:00
|
|
|
FileSaver::FileSaver(const FilePath &filePath, QIODevice::OpenMode mode)
|
2011-03-30 12:06:05 +02:00
|
|
|
{
|
2021-05-18 09:47:07 +02:00
|
|
|
m_filePath = filePath;
|
2016-10-07 10:34:40 +02:00
|
|
|
// Workaround an assert in Qt -- and provide a useful error message, too:
|
|
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
|
// Taken from: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx
|
|
|
|
|
static const QStringList reservedNames
|
2017-02-22 15:09:35 +01:00
|
|
|
= {"CON", "PRN", "AUX", "NUL",
|
|
|
|
|
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9",
|
|
|
|
|
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"};
|
2021-06-03 12:53:40 +02:00
|
|
|
const QString fn = filePath.baseName().toUpper();
|
2021-05-06 16:42:02 +02:00
|
|
|
if (reservedNames.contains(fn)) {
|
2021-05-18 09:47:07 +02:00
|
|
|
m_errorString = tr("%1: Is a reserved filename on Windows. Cannot save.")
|
|
|
|
|
.arg(filePath.toString());
|
2021-05-06 16:42:02 +02:00
|
|
|
m_hasError = true;
|
|
|
|
|
return;
|
2016-10-07 10:34:40 +02:00
|
|
|
}
|
|
|
|
|
}
|
2011-03-30 12:06:05 +02:00
|
|
|
if (mode & (QIODevice::ReadOnly | QIODevice::Append)) {
|
2021-05-18 09:47:07 +02:00
|
|
|
m_file.reset(new QFile{filePath.toString()});
|
2011-03-30 12:06:05 +02:00
|
|
|
m_isSafe = false;
|
|
|
|
|
} else {
|
2021-05-18 09:47:07 +02:00
|
|
|
m_file.reset(new SaveFile{filePath.toString()});
|
2011-03-30 12:06:05 +02:00
|
|
|
m_isSafe = true;
|
|
|
|
|
}
|
|
|
|
|
if (!m_file->open(QIODevice::WriteOnly | mode)) {
|
2021-05-18 09:47:07 +02:00
|
|
|
QString err = filePath.exists() ?
|
2011-03-30 12:06:05 +02:00
|
|
|
tr("Cannot overwrite file %1: %2") : tr("Cannot create file %1: %2");
|
2021-05-18 09:47:07 +02:00
|
|
|
m_errorString = err.arg(filePath.toUserOutput(), m_file->errorString());
|
2011-03-30 12:06:05 +02:00
|
|
|
m_hasError = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaver::finalize()
|
|
|
|
|
{
|
|
|
|
|
if (!m_isSafe)
|
|
|
|
|
return FileSaverBase::finalize();
|
|
|
|
|
|
2018-07-19 16:39:41 +02:00
|
|
|
auto sf = static_cast<SaveFile *>(m_file.get());
|
2013-06-04 18:00:42 +02:00
|
|
|
if (m_hasError) {
|
|
|
|
|
if (sf->isOpen())
|
|
|
|
|
sf->rollback();
|
|
|
|
|
} else {
|
2011-03-30 12:06:05 +02:00
|
|
|
setResult(sf->commit());
|
2013-06-04 18:00:42 +02:00
|
|
|
}
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset();
|
2011-03-30 12:06:05 +02:00
|
|
|
return !m_hasError;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TempFileSaver::TempFileSaver(const QString &templ)
|
|
|
|
|
{
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset(new QTemporaryFile{});
|
2018-07-19 16:39:41 +02:00
|
|
|
auto tempFile = static_cast<QTemporaryFile *>(m_file.get());
|
2011-03-30 12:06:05 +02:00
|
|
|
if (!templ.isEmpty())
|
|
|
|
|
tempFile->setFileTemplate(templ);
|
|
|
|
|
tempFile->setAutoRemove(false);
|
|
|
|
|
if (!tempFile->open()) {
|
|
|
|
|
m_errorString = tr("Cannot create temporary file in %1: %2").arg(
|
|
|
|
|
QDir::toNativeSeparators(QFileInfo(tempFile->fileTemplate()).absolutePath()),
|
|
|
|
|
tempFile->errorString());
|
|
|
|
|
m_hasError = true;
|
|
|
|
|
}
|
2021-05-18 09:47:07 +02:00
|
|
|
m_filePath = FilePath::fromString(tempFile->fileName());
|
2011-03-30 12:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TempFileSaver::~TempFileSaver()
|
|
|
|
|
{
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset();
|
2011-03-30 12:06:05 +02:00
|
|
|
if (m_autoRemove)
|
2021-05-18 09:47:07 +02:00
|
|
|
QFile::remove(m_filePath.toString());
|
2011-03-30 12:06:05 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/*! \class Utils::FilePath
|
2011-11-25 13:17:52 +01:00
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
\brief The FilePath class is a light-weight convenience class for filenames.
|
2011-11-25 13:17:52 +01:00
|
|
|
|
|
|
|
|
On windows filenames are compared case insensitively.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath::FilePath()
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// Constructs a FilePath from \a info
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::fromFileInfo(const QFileInfo &info)
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromString(info.absoluteFilePath());
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns a QFileInfo
|
2019-05-28 13:49:26 +02:00
|
|
|
QFileInfo FilePath::toFileInfo() const
|
2011-11-25 13:17:52 +01:00
|
|
|
{
|
2019-05-10 12:23:27 +02:00
|
|
|
return QFileInfo(m_data);
|
2011-11-25 13:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::fromUrl(const QUrl &url)
|
2019-05-09 16:35:25 +02:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath fn;
|
2021-05-18 18:03:10 +02:00
|
|
|
fn.m_scheme = url.scheme();
|
|
|
|
|
fn.m_host = url.host();
|
2019-05-09 16:35:25 +02:00
|
|
|
fn.m_data = url.path();
|
|
|
|
|
return fn;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns a QString for passing on to QString based APIs
|
2021-04-23 15:01:08 +02:00
|
|
|
QString FilePath::toString() const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
if (m_scheme.isEmpty())
|
|
|
|
|
return m_data;
|
|
|
|
|
if (m_data.startsWith('/'))
|
|
|
|
|
return m_scheme + "://" + m_host + m_data;
|
|
|
|
|
return m_scheme + "://" + m_host + "/./" + m_data;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QUrl FilePath::toUrl() const
|
2019-05-09 16:35:25 +02:00
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
QUrl url;
|
|
|
|
|
url.setScheme(m_scheme);
|
|
|
|
|
url.setHost(m_host);
|
|
|
|
|
url.setPath(m_data);
|
|
|
|
|
return url;
|
2019-05-09 16:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-27 12:05:13 +02:00
|
|
|
void FilePath::setDeviceFileHooks(const DeviceFileHooks &hooks)
|
|
|
|
|
{
|
|
|
|
|
s_deviceHooks = hooks;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns a QString to display to the user
|
|
|
|
|
/// Converts the separators to the native format
|
2019-05-28 13:49:26 +02:00
|
|
|
QString FilePath::toUserOutput() const
|
2011-11-25 13:17:52 +01:00
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
if (m_scheme.isEmpty())
|
|
|
|
|
return QDir::toNativeSeparators(m_data);
|
|
|
|
|
return toString();
|
2011-11-25 13:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
2019-08-14 10:49:09 +02:00
|
|
|
QString FilePath::fileName() const
|
|
|
|
|
{
|
|
|
|
|
const QChar slash = QLatin1Char('/');
|
|
|
|
|
return m_data.mid(m_data.lastIndexOf(slash) + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FilePath::fileNameWithPathComponents(int pathComponents) const
|
2015-01-10 23:23:15 +02:00
|
|
|
{
|
|
|
|
|
if (pathComponents < 0)
|
2019-05-10 12:23:27 +02:00
|
|
|
return m_data;
|
2015-01-10 23:23:15 +02:00
|
|
|
const QChar slash = QLatin1Char('/');
|
2019-05-10 12:23:27 +02:00
|
|
|
int i = m_data.lastIndexOf(slash);
|
2015-01-10 23:23:15 +02:00
|
|
|
if (pathComponents == 0 || i == -1)
|
2019-05-10 12:23:27 +02:00
|
|
|
return m_data.mid(i + 1);
|
2015-01-10 23:23:15 +02:00
|
|
|
int component = i + 1;
|
|
|
|
|
// skip adjacent slashes
|
2019-05-10 12:23:27 +02:00
|
|
|
while (i > 0 && m_data.at(--i) == slash)
|
|
|
|
|
;
|
2015-01-10 23:23:15 +02:00
|
|
|
while (i >= 0 && --pathComponents >= 0) {
|
2019-05-10 12:23:27 +02:00
|
|
|
i = m_data.lastIndexOf(slash, i);
|
2015-01-10 23:23:15 +02:00
|
|
|
component = i + 1;
|
2019-05-10 12:23:27 +02:00
|
|
|
while (i > 0 && m_data.at(--i) == slash)
|
|
|
|
|
;
|
2015-01-10 23:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are no more slashes before the found one, return the entire string
|
2019-05-10 12:23:27 +02:00
|
|
|
if (i > 0 && m_data.lastIndexOf(slash, i) != -1)
|
|
|
|
|
return m_data.mid(component);
|
|
|
|
|
return m_data;
|
2015-01-10 23:23:15 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-04 07:59:00 +02:00
|
|
|
/// \returns the base name of the file without the path.
|
|
|
|
|
///
|
|
|
|
|
/// The base name consists of all characters in the file up to
|
|
|
|
|
/// (but not including) the first '.' character.
|
|
|
|
|
|
2021-06-03 12:53:40 +02:00
|
|
|
QString FilePath::baseName() const
|
|
|
|
|
{
|
|
|
|
|
const QString &name = fileName();
|
|
|
|
|
return name.left(name.indexOf('.'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 07:59:00 +02:00
|
|
|
/// \returns the complete base name of the file without the path.
|
|
|
|
|
///
|
|
|
|
|
/// The complete base name consists of all characters in the file up to
|
|
|
|
|
/// (but not including) the last '.' character
|
|
|
|
|
|
|
|
|
|
QString FilePath::completeBaseName() const
|
|
|
|
|
{
|
|
|
|
|
const QString &name = fileName();
|
|
|
|
|
return name.left(name.lastIndexOf('.'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-04 09:13:49 +02:00
|
|
|
/// \returns the suffix (extension) of the file.
|
|
|
|
|
///
|
|
|
|
|
/// The suffix consists of all characters in the file after
|
|
|
|
|
/// (but not including) the last '.'.
|
|
|
|
|
|
|
|
|
|
QString FilePath::suffix() const
|
|
|
|
|
{
|
|
|
|
|
const QString &name = fileName();
|
|
|
|
|
const int index = name.lastIndexOf('.');
|
|
|
|
|
if (index >= 0)
|
|
|
|
|
return name.mid(index + 1);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \returns the complete suffix (extension) of the file.
|
|
|
|
|
///
|
|
|
|
|
/// The complete suffix consists of all characters in the file after
|
|
|
|
|
/// (but not including) the first '.'.
|
|
|
|
|
|
|
|
|
|
QString FilePath::completeSuffix() const
|
|
|
|
|
{
|
|
|
|
|
const QString &name = fileName();
|
|
|
|
|
const int index = name.indexOf('.');
|
|
|
|
|
if (index >= 0)
|
|
|
|
|
return name.mid(index + 1);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:03:10 +02:00
|
|
|
void FilePath::setScheme(const QString &scheme)
|
2021-04-27 12:05:13 +02:00
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
QTC_CHECK(!scheme.contains('/'));
|
|
|
|
|
m_scheme = scheme;
|
2021-04-27 12:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-18 18:03:10 +02:00
|
|
|
void FilePath::setHost(const QString &host)
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(!host.contains('/'));
|
|
|
|
|
m_host = host;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2014-10-24 13:15:54 +02:00
|
|
|
/// \returns a bool indicating whether a file with this
|
2019-05-28 15:55:45 +02:00
|
|
|
/// FilePath exists.
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::exists() const
|
2014-10-24 13:15:54 +02:00
|
|
|
{
|
2021-06-01 09:40:17 +02:00
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.exists, return false);
|
|
|
|
|
return s_deviceHooks.exists(*this);
|
|
|
|
|
}
|
2019-05-10 12:23:27 +02:00
|
|
|
return !isEmpty() && QFileInfo::exists(m_data);
|
2014-10-24 13:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-12 01:41:41 +02:00
|
|
|
/// \returns a bool indicating whether a path is writable.
|
2021-04-27 12:05:13 +02:00
|
|
|
bool FilePath::isWritableDir() const
|
2019-08-12 01:41:41 +02:00
|
|
|
{
|
2021-04-27 12:05:13 +02:00
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.isWritableDir, return false);
|
2021-05-31 18:20:40 +02:00
|
|
|
return s_deviceHooks.isWritableDir(*this);
|
2021-04-27 12:05:13 +02:00
|
|
|
}
|
2019-08-12 01:41:41 +02:00
|
|
|
const QFileInfo fi{m_data};
|
|
|
|
|
return exists() && fi.isDir() && fi.isWritable();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-22 07:09:00 +02:00
|
|
|
bool FilePath::isWritableFile() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.isWritableFile, return false);
|
|
|
|
|
return s_deviceHooks.isWritableFile(*this);
|
|
|
|
|
}
|
|
|
|
|
const QFileInfo fi{m_data};
|
|
|
|
|
return fi.exists() && fi.isWritable() && !fi.isDir();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 18:20:46 +02:00
|
|
|
bool FilePath::ensureWritableDir() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.ensureWritableDir, return false);
|
|
|
|
|
return s_deviceHooks.ensureWritableDir(*this);
|
|
|
|
|
}
|
|
|
|
|
const QFileInfo fi{m_data};
|
|
|
|
|
if (exists() && fi.isDir() && fi.isWritable())
|
|
|
|
|
return true;
|
|
|
|
|
return QDir().mkpath(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 09:41:53 +02:00
|
|
|
bool FilePath::ensureExistingFile() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.ensureExistingFile, return false);
|
|
|
|
|
return s_deviceHooks.ensureExistingFile(*this);
|
|
|
|
|
}
|
|
|
|
|
QFile f(m_data);
|
|
|
|
|
if (f.exists())
|
|
|
|
|
return true;
|
|
|
|
|
f.open(QFile::WriteOnly);
|
|
|
|
|
f.close();
|
|
|
|
|
return f.exists();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 12:05:13 +02:00
|
|
|
bool FilePath::isExecutableFile() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.isExecutableFile, return false);
|
|
|
|
|
return s_deviceHooks.isExecutableFile(*this);
|
|
|
|
|
}
|
|
|
|
|
const QFileInfo fi{m_data};
|
|
|
|
|
return fi.exists() && fi.isExecutable() && !fi.isDir();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FilePath::isReadableFile() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.isReadableFile, return false);
|
|
|
|
|
return s_deviceHooks.isReadableFile(*this);
|
|
|
|
|
}
|
|
|
|
|
const QFileInfo fi{m_data};
|
|
|
|
|
return fi.exists() && fi.isReadable() && !fi.isDir();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FilePath::isReadableDir() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.isReadableDir, return false);
|
|
|
|
|
return s_deviceHooks.isReadableDir(*this);
|
|
|
|
|
}
|
|
|
|
|
const QFileInfo fi{m_data};
|
|
|
|
|
return fi.exists() && fi.isReadable() && fi.isDir();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FilePath::createDir() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.createDir, return false);
|
|
|
|
|
return s_deviceHooks.createDir(*this);
|
|
|
|
|
}
|
|
|
|
|
QDir dir(m_data);
|
|
|
|
|
return dir.mkpath(dir.absolutePath());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 11:10:27 +02:00
|
|
|
QList<FilePath> FilePath::dirEntries(const QStringList &nameFilters,
|
|
|
|
|
QDir::Filters filters,
|
|
|
|
|
QDir::SortFlags sort) const
|
2021-04-27 12:05:13 +02:00
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.dirEntries, return {});
|
2021-06-29 11:10:27 +02:00
|
|
|
return s_deviceHooks.dirEntries(*this, nameFilters, filters, sort);
|
2021-04-27 12:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
2021-06-29 11:10:27 +02:00
|
|
|
const QFileInfoList entryInfoList = QDir(m_data).entryInfoList(nameFilters, filters, sort);
|
2021-04-27 12:05:13 +02:00
|
|
|
return Utils::transform(entryInfoList, &FilePath::fromFileInfo);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QList<FilePath> FilePath::dirEntries(QDir::Filters filters) const
|
|
|
|
|
{
|
|
|
|
|
return dirEntries({}, filters);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QByteArray FilePath::fileContents(int maxSize) const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.fileContents, return {});
|
|
|
|
|
return s_deviceHooks.fileContents(*this, maxSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QString path = toString();
|
|
|
|
|
QFile f(path);
|
|
|
|
|
if (!f.exists())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
if (!f.open(QFile::ReadOnly))
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
if (maxSize != -1)
|
|
|
|
|
return f.read(maxSize);
|
|
|
|
|
|
|
|
|
|
return f.readAll();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-15 18:29:51 +02:00
|
|
|
bool FilePath::writeFileContents(const QByteArray &data) const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.writeFileContents, return {});
|
|
|
|
|
return s_deviceHooks.writeFileContents(*this, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile file(path());
|
|
|
|
|
QTC_ASSERT(file.open(QFile::WriteOnly | QFile::Truncate), return false);
|
|
|
|
|
qint64 res = file.write(data);
|
|
|
|
|
return res == data.size();
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 12:05:13 +02:00
|
|
|
bool FilePath::needsDevice() const
|
|
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
return !m_scheme.isEmpty();
|
2021-04-27 12:05:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-01-19 15:26:54 +01:00
|
|
|
/// Find the parent directory of a given directory.
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// Returns an empty FilePath if the current directory is already
|
2012-01-19 15:26:54 +01:00
|
|
|
/// a root level directory.
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// \returns \a FilePath with the last segment removed.
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::parentDir() const
|
2012-01-19 15:26:54 +01:00
|
|
|
{
|
2021-06-09 17:35:40 +02:00
|
|
|
const QString basePath = path();
|
2012-01-19 15:26:54 +01:00
|
|
|
if (basePath.isEmpty())
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath();
|
2012-01-19 15:26:54 +01:00
|
|
|
|
|
|
|
|
const QDir base(basePath);
|
|
|
|
|
if (base.isRoot())
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath();
|
2012-01-19 15:26:54 +01:00
|
|
|
|
|
|
|
|
const QString path = basePath + QLatin1String("/..");
|
|
|
|
|
const QString parent = QDir::cleanPath(path);
|
2019-05-28 13:49:26 +02:00
|
|
|
QTC_ASSERT(parent != path, return FilePath());
|
2012-01-19 15:26:54 +01:00
|
|
|
|
2021-06-09 17:35:40 +02:00
|
|
|
FilePath result = *this;
|
|
|
|
|
result.setPath(parent);
|
|
|
|
|
return result;
|
2012-01-19 15:26:54 +01:00
|
|
|
}
|
|
|
|
|
|
2019-12-18 18:55:27 +01:00
|
|
|
FilePath FilePath::absolutePath() const
|
|
|
|
|
{
|
|
|
|
|
FilePath result = *this;
|
|
|
|
|
result.m_data = QFileInfo(m_data).absolutePath();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-18 12:43:48 +02:00
|
|
|
FilePath FilePath::absoluteFilePath() const
|
|
|
|
|
{
|
|
|
|
|
FilePath result = *this;
|
|
|
|
|
result.m_data = QFileInfo(m_data).absoluteFilePath();
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-09 17:05:50 +02:00
|
|
|
FilePath FilePath::absoluteFilePath(const FilePath &tail) const
|
|
|
|
|
{
|
|
|
|
|
if (FileUtils::isRelativePath(tail.m_data))
|
|
|
|
|
return pathAppended(tail.m_data);
|
|
|
|
|
return tail;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 10:46:25 +02:00
|
|
|
/// Constructs an absolute FilePath from this path which
|
|
|
|
|
/// is interpreted as being relative to \a anchor.
|
|
|
|
|
FilePath FilePath::absoluteFromRelativePath(const FilePath &anchor) const
|
|
|
|
|
{
|
|
|
|
|
QDir anchorDir = QFileInfo(anchor.m_data).absoluteDir();
|
|
|
|
|
QString absoluteFilePath = QFileInfo(anchorDir, m_data).canonicalFilePath();
|
|
|
|
|
return FilePath::fromString(absoluteFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// Constructs a FilePath from \a filename
|
2014-09-16 15:58:32 +02:00
|
|
|
/// \a filename is not checked for validity.
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::fromString(const QString &filename)
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath fn;
|
2021-05-18 18:03:10 +02:00
|
|
|
if (filename.startsWith('/')) {
|
|
|
|
|
fn.m_data = filename; // fast track: absolute local paths
|
|
|
|
|
} else {
|
|
|
|
|
int pos1 = filename.indexOf("://");
|
|
|
|
|
if (pos1 >= 0) {
|
|
|
|
|
fn.m_scheme = filename.left(pos1);
|
|
|
|
|
pos1 += 3;
|
|
|
|
|
int pos2 = filename.indexOf('/', pos1);
|
|
|
|
|
if (pos2 == -1) {
|
|
|
|
|
fn.m_data = filename.mid(pos1);
|
|
|
|
|
} else {
|
|
|
|
|
fn.m_host = filename.mid(pos1, pos2 - pos1);
|
|
|
|
|
fn.m_data = filename.mid(pos2);
|
|
|
|
|
}
|
|
|
|
|
if (fn.m_data.startsWith("/./"))
|
|
|
|
|
fn.m_data = fn.m_data.mid(3);
|
|
|
|
|
} else {
|
|
|
|
|
fn.m_data = filename; // treat everything else as local, too.
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-10 12:23:27 +02:00
|
|
|
return fn;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// Constructs a FilePath from \a filePath. The \a defaultExtension is appended
|
2014-09-16 15:58:32 +02:00
|
|
|
/// to \a filename if that does not have an extension already.
|
2019-05-28 15:55:45 +02:00
|
|
|
/// \a filePath is not checked for validity.
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::fromStringWithExtension(const QString &filepath, const QString &defaultExtension)
|
2014-09-16 15:58:32 +02:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
if (filepath.isEmpty() || defaultExtension.isEmpty())
|
|
|
|
|
return FilePath::fromString(filepath);
|
2014-09-16 15:58:32 +02:00
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QString rc = filepath;
|
|
|
|
|
QFileInfo fi(filepath);
|
2014-09-16 15:58:32 +02:00
|
|
|
// Add extension unless user specified something else
|
|
|
|
|
const QChar dot = QLatin1Char('.');
|
|
|
|
|
if (!fi.fileName().contains(dot)) {
|
|
|
|
|
if (!defaultExtension.startsWith(dot))
|
|
|
|
|
rc += dot;
|
|
|
|
|
rc += defaultExtension;
|
|
|
|
|
}
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromString(rc);
|
2014-09-16 15:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// Constructs a FilePath from \a filePath
|
2020-05-28 15:57:35 +02:00
|
|
|
/// \a filePath is only passed through QDir::fromNativeSeparators
|
2019-05-28 15:55:45 +02:00
|
|
|
FilePath FilePath::fromUserInput(const QString &filePath)
|
2011-11-25 13:17:52 +01:00
|
|
|
{
|
2020-05-28 15:57:35 +02:00
|
|
|
QString clean = QDir::fromNativeSeparators(filePath);
|
2013-06-25 23:32:04 +03:00
|
|
|
if (clean.startsWith(QLatin1String("~/")))
|
2021-06-08 13:51:34 +02:00
|
|
|
return FileUtils::homePath().pathAppended(clean.mid(2));
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromString(clean);
|
2011-11-25 13:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// Constructs a FilePath from \a filePath, which is encoded as UTF-8.
|
|
|
|
|
/// \a filePath is not checked for validity.
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::fromUtf8(const char *filename, int filenameSize)
|
2014-11-27 12:11:46 +01:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromString(QString::fromUtf8(filename, filenameSize));
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
2011-11-25 13:17:52 +01:00
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::fromVariant(const QVariant &variant)
|
2019-05-09 16:35:25 +02:00
|
|
|
{
|
|
|
|
|
if (variant.type() == QVariant::Url)
|
2019-05-28 13:49:26 +02:00
|
|
|
return FilePath::fromUrl(variant.toUrl());
|
|
|
|
|
return FilePath::fromString(variant.toString());
|
2019-05-09 16:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QVariant FilePath::toVariant() const
|
2019-05-09 16:35:25 +02:00
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
return toString();
|
2019-05-09 16:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 16:15:26 +02:00
|
|
|
QDir FilePath::toDir() const
|
|
|
|
|
{
|
|
|
|
|
return QDir(m_data);
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::operator==(const FilePath &other) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2021-05-19 09:28:57 +02:00
|
|
|
return QString::compare(m_data, other.m_data, caseSensitivity()) == 0
|
|
|
|
|
&& m_host == other.m_host
|
|
|
|
|
&& m_scheme == other.m_scheme;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::operator!=(const FilePath &other) const
|
2011-11-25 13:17:52 +01:00
|
|
|
{
|
2019-05-20 13:39:27 +02:00
|
|
|
return !(*this == other);
|
2011-11-25 13:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::operator<(const FilePath &other) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2021-05-19 09:28:57 +02:00
|
|
|
const int cmp = QString::compare(m_data, other.m_data, caseSensitivity());
|
|
|
|
|
if (cmp != 0)
|
|
|
|
|
return cmp < 0;
|
2021-05-18 18:03:10 +02:00
|
|
|
if (m_host != other.m_host)
|
|
|
|
|
return m_host < other.m_host;
|
|
|
|
|
return m_scheme < other.m_scheme;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::operator<=(const FilePath &other) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2019-05-20 13:39:27 +02:00
|
|
|
return !(other < *this);
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::operator>(const FilePath &other) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2019-05-20 13:39:27 +02:00
|
|
|
return other < *this;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::operator>=(const FilePath &other) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2019-05-20 13:39:27 +02:00
|
|
|
return !(*this < other);
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::operator+(const QString &s) const
|
2018-03-27 15:07:54 +02:00
|
|
|
{
|
2021-05-19 10:35:44 +02:00
|
|
|
FilePath res = *this;
|
|
|
|
|
res.m_data += s;
|
|
|
|
|
return res;
|
2018-03-27 15:07:54 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// \returns whether FilePath is a child of \a s
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::isChildOf(const FilePath &s) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2012-10-04 06:06:09 +02:00
|
|
|
if (s.isEmpty())
|
|
|
|
|
return false;
|
2021-05-19 09:28:57 +02:00
|
|
|
if (!m_data.startsWith(s.m_data, caseSensitivity()))
|
2011-11-25 13:17:52 +01:00
|
|
|
return false;
|
2019-05-10 12:23:27 +02:00
|
|
|
if (m_data.size() <= s.m_data.size())
|
2011-11-25 13:17:52 +01:00
|
|
|
return false;
|
2012-10-04 06:06:09 +02:00
|
|
|
// s is root, '/' was already tested in startsWith
|
2019-05-10 12:23:27 +02:00
|
|
|
if (s.m_data.endsWith(QLatin1Char('/')))
|
2012-10-04 06:06:09 +02:00
|
|
|
return true;
|
|
|
|
|
// s is a directory, next character should be '/' (/tmpdir is NOT a child of /tmp)
|
2019-05-10 12:23:27 +02:00
|
|
|
return m_data.at(s.m_data.size()) == QLatin1Char('/');
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2012-07-18 14:46:05 +04:00
|
|
|
/// \overload
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::isChildOf(const QDir &dir) const
|
2012-07-18 14:46:05 +04:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
return isChildOf(FilePath::fromString(dir.absolutePath()));
|
2012-07-18 14:46:05 +04:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 15:06:28 +02:00
|
|
|
/// \returns whether FilePath startsWith \a s
|
|
|
|
|
bool FilePath::startsWith(const QString &s) const
|
|
|
|
|
{
|
2021-05-19 09:28:57 +02:00
|
|
|
return m_data.startsWith(s, caseSensitivity());
|
2019-10-21 15:06:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// \returns whether FilePath endsWith \a s
|
2019-05-28 13:49:26 +02:00
|
|
|
bool FilePath::endsWith(const QString &s) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2021-05-19 09:28:57 +02:00
|
|
|
return m_data.endsWith(s, caseSensitivity());
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 16:55:43 +02:00
|
|
|
bool FilePath::isDir() const
|
|
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
QTC_CHECK(m_scheme.isEmpty()); // FIXME: Not implemented yet.
|
2019-05-28 16:55:43 +02:00
|
|
|
return QFileInfo(m_data).isDir();
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 15:55:45 +02:00
|
|
|
/// \returns the relativeChildPath of FilePath to parent if FilePath is a child of parent
|
|
|
|
|
/// \note returns a empty FilePath if FilePath is not a child of parent
|
2011-11-25 13:17:52 +01:00
|
|
|
/// That is, this never returns a path starting with "../"
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::relativeChildPath(const FilePath &parent) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2021-05-19 10:36:34 +02:00
|
|
|
FilePath res;
|
|
|
|
|
if (isChildOf(parent))
|
|
|
|
|
res.m_data = m_data.mid(parent.m_data.size() + 1, -1);
|
|
|
|
|
return res;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-06 10:46:25 +02:00
|
|
|
/// \returns the relativePath of FilePath to given \a anchor.
|
|
|
|
|
/// Both, FilePath and anchor may be files or directories.
|
|
|
|
|
/// Example usage:
|
|
|
|
|
///
|
|
|
|
|
/// \code
|
|
|
|
|
/// FilePath filePath("/foo/b/ar/file.txt");
|
|
|
|
|
/// FilePath relativePath = filePath.relativePath("/foo/c");
|
|
|
|
|
/// qDebug() << relativePath
|
|
|
|
|
/// \endcode
|
|
|
|
|
///
|
|
|
|
|
/// The debug output will be "../b/ar/file.txt".
|
|
|
|
|
///
|
|
|
|
|
FilePath FilePath::relativePath(const FilePath &anchor) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fileInfo(m_data);
|
|
|
|
|
QString absolutePath;
|
|
|
|
|
QString filename;
|
|
|
|
|
if (fileInfo.isFile()) {
|
|
|
|
|
absolutePath = fileInfo.absolutePath();
|
|
|
|
|
filename = fileInfo.fileName();
|
|
|
|
|
} else if (fileInfo.isDir()) {
|
|
|
|
|
absolutePath = fileInfo.absoluteFilePath();
|
|
|
|
|
} else {
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
const QFileInfo anchorInfo(anchor.m_data);
|
|
|
|
|
QString absoluteAnchorPath;
|
|
|
|
|
if (anchorInfo.isFile())
|
|
|
|
|
absoluteAnchorPath = anchorInfo.absolutePath();
|
|
|
|
|
else if (anchorInfo.isDir())
|
|
|
|
|
absoluteAnchorPath = anchorInfo.absoluteFilePath();
|
|
|
|
|
else
|
|
|
|
|
return {};
|
|
|
|
|
QString relativeFilePath = calcRelativePath(absolutePath, absoluteAnchorPath);
|
|
|
|
|
if (!filename.isEmpty()) {
|
|
|
|
|
if (!relativeFilePath.isEmpty())
|
|
|
|
|
relativeFilePath += '/';
|
|
|
|
|
relativeFilePath += filename;
|
|
|
|
|
}
|
|
|
|
|
return FilePath::fromString(relativeFilePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \returns the relativePath of \a absolutePath to given \a absoluteAnchorPath.
|
|
|
|
|
/// Both paths must be an absolute path to a directory. Example usage:
|
|
|
|
|
///
|
|
|
|
|
/// \code
|
|
|
|
|
/// qDebug() << FilePath::calcRelativePath("/foo/b/ar", "/foo/c");
|
|
|
|
|
/// \endcode
|
|
|
|
|
///
|
|
|
|
|
/// The debug output will be "../b/ar".
|
|
|
|
|
///
|
|
|
|
|
/// \see FilePath::relativePath
|
|
|
|
|
///
|
|
|
|
|
QString FilePath::calcRelativePath(const QString &absolutePath, const QString &absoluteAnchorPath)
|
|
|
|
|
{
|
|
|
|
|
if (absolutePath.isEmpty() || absoluteAnchorPath.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
// TODO using split() instead of parsing the strings by char index is slow
|
|
|
|
|
// and needs more memory (but the easiest implementation for now)
|
|
|
|
|
const QStringList splits1 = absolutePath.split('/');
|
|
|
|
|
const QStringList splits2 = absoluteAnchorPath.split('/');
|
|
|
|
|
int i = 0;
|
|
|
|
|
while (i < splits1.count() && i < splits2.count() && splits1.at(i) == splits2.at(i))
|
|
|
|
|
++i;
|
|
|
|
|
QString relativePath;
|
|
|
|
|
int j = i;
|
|
|
|
|
bool addslash = false;
|
|
|
|
|
while (j < splits2.count()) {
|
|
|
|
|
if (!splits2.at(j).isEmpty()) {
|
|
|
|
|
if (addslash)
|
|
|
|
|
relativePath += '/';
|
|
|
|
|
relativePath += "..";
|
|
|
|
|
addslash = true;
|
|
|
|
|
}
|
|
|
|
|
++j;
|
|
|
|
|
}
|
|
|
|
|
while (i < splits1.count()) {
|
|
|
|
|
if (!splits1.at(i).isEmpty()) {
|
|
|
|
|
if (addslash)
|
|
|
|
|
relativePath += '/';
|
|
|
|
|
relativePath += splits1.at(i);
|
|
|
|
|
addslash = true;
|
|
|
|
|
}
|
|
|
|
|
++i;
|
|
|
|
|
}
|
|
|
|
|
return relativePath;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-27 15:08:03 +02:00
|
|
|
/*!
|
|
|
|
|
Returns a path corresponding to the current object on the
|
|
|
|
|
same device as \a deviceTemplate.
|
|
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
\code
|
|
|
|
|
localDir = FilePath::fromString("/tmp/workingdir");
|
|
|
|
|
executable = FilePath::fromUrl("docker://123/bin/ls")
|
|
|
|
|
realDir = localDir.onDevice(executable)
|
|
|
|
|
assert(realDir == FilePath::fromUrl("docker://123/tmp/workingdir"))
|
|
|
|
|
\endcode
|
|
|
|
|
*/
|
2021-04-27 12:05:13 +02:00
|
|
|
FilePath FilePath::onDevice(const FilePath &deviceTemplate) const
|
|
|
|
|
{
|
2021-05-18 18:03:10 +02:00
|
|
|
FilePath res;
|
|
|
|
|
res.m_data = m_data;
|
|
|
|
|
res.m_host = deviceTemplate.m_host;
|
|
|
|
|
res.m_scheme = deviceTemplate.m_scheme;
|
2021-04-27 12:05:13 +02:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-07 15:47:06 +02:00
|
|
|
/*!
|
|
|
|
|
Searched a binary corresponding to this object in the PATH of
|
|
|
|
|
the device implied by this object's scheme and host.
|
|
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
\code
|
|
|
|
|
binary = FilePath::fromUrl("docker://123/./make);
|
|
|
|
|
fullPath = binary.onDeviceSearchInPath();
|
|
|
|
|
assert(fullPath == FilePath::fromUrl("docker://123/usr/bin/make"))
|
|
|
|
|
\endcode
|
|
|
|
|
*/
|
|
|
|
|
FilePath FilePath::onDeviceSearchInPath() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.searchInPath, return {});
|
|
|
|
|
return s_deviceHooks.searchInPath(*this);
|
|
|
|
|
}
|
|
|
|
|
return Environment::systemEnvironment().searchInPath(path());
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-28 12:37:35 +02:00
|
|
|
FilePath FilePath::pathAppended(const QString &path) const
|
2019-05-15 08:15:19 +02:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath fn = *this;
|
2021-05-28 12:37:35 +02:00
|
|
|
if (path.isEmpty())
|
2019-05-15 08:15:19 +02:00
|
|
|
return fn;
|
2021-05-18 18:03:10 +02:00
|
|
|
if (!fn.m_data.isEmpty() && !fn.m_data.endsWith(QLatin1Char('/')))
|
|
|
|
|
fn.m_data.append('/');
|
2021-05-28 12:37:35 +02:00
|
|
|
fn.m_data.append(path);
|
2019-05-15 08:15:19 +02:00
|
|
|
return fn;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath FilePath::stringAppended(const QString &str) const
|
2011-11-25 13:19:58 +01:00
|
|
|
{
|
2019-05-28 13:49:26 +02:00
|
|
|
FilePath fn = *this;
|
2019-05-15 08:01:06 +02:00
|
|
|
fn.m_data.append(str);
|
|
|
|
|
return fn;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
uint FilePath::hash(uint seed) const
|
2019-05-10 12:23:27 +02:00
|
|
|
{
|
|
|
|
|
if (Utils::HostOsInfo::fileNameCaseSensitivity() == Qt::CaseInsensitive)
|
|
|
|
|
return qHash(m_data.toUpper(), seed);
|
|
|
|
|
return qHash(m_data, seed);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 15:30:01 +02:00
|
|
|
QDateTime FilePath::lastModified() const
|
|
|
|
|
{
|
2021-06-14 08:33:44 +02:00
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.lastModified, return {});
|
|
|
|
|
return s_deviceHooks.lastModified(*this);
|
|
|
|
|
}
|
2021-06-08 15:30:01 +02:00
|
|
|
return toFileInfo().lastModified();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-17 10:39:31 +02:00
|
|
|
QFile::Permissions FilePath::permissions() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.permissions, return {});
|
|
|
|
|
return s_deviceHooks.permissions(*this);
|
|
|
|
|
}
|
|
|
|
|
return toFileInfo().permissions();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 08:33:44 +02:00
|
|
|
bool FilePath::removeFile() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.removeFile, return false);
|
|
|
|
|
return s_deviceHooks.removeFile(*this);
|
|
|
|
|
}
|
|
|
|
|
return QFile::remove(path());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 15:26:44 +02:00
|
|
|
bool FilePath::removeRecursively() const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.removeRecursively, return false);
|
|
|
|
|
return s_deviceHooks.removeRecursively(*this);
|
|
|
|
|
}
|
|
|
|
|
return FileUtils::removeRecursively(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 08:33:44 +02:00
|
|
|
bool FilePath::copyFile(const FilePath &target) const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.copyFile, return false);
|
|
|
|
|
return s_deviceHooks.copyFile(*this, target);
|
|
|
|
|
}
|
|
|
|
|
return QFile::copy(path(), target.path());
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 13:57:10 +02:00
|
|
|
bool FilePath::renameFile(const FilePath &target) const
|
|
|
|
|
{
|
|
|
|
|
if (needsDevice()) {
|
|
|
|
|
QTC_ASSERT(s_deviceHooks.renameFile, return false);
|
|
|
|
|
return s_deviceHooks.renameFile(*this, target);
|
|
|
|
|
}
|
|
|
|
|
return QFile::rename(path(), target.path());
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-28 13:49:26 +02:00
|
|
|
QTextStream &operator<<(QTextStream &s, const FilePath &fn)
|
2014-11-27 10:49:18 +01:00
|
|
|
{
|
|
|
|
|
return s << fn.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 15:41:26 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
2021-02-12 16:06:24 +01:00
|
|
|
FileUtils::CopyAskingForOverwrite::CopyAskingForOverwrite(
|
|
|
|
|
QWidget *dialogParent, const std::function<void(QFileInfo)> &postOperation)
|
2020-06-11 15:41:26 +02:00
|
|
|
: m_parent(dialogParent)
|
2021-02-12 16:06:24 +01:00
|
|
|
, m_postOperation(postOperation)
|
2020-06-11 15:41:26 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
bool FileUtils::CopyAskingForOverwrite::operator()(const QFileInfo &src,
|
|
|
|
|
const QFileInfo &dest,
|
|
|
|
|
QString *error)
|
|
|
|
|
{
|
|
|
|
|
bool copyFile = true;
|
|
|
|
|
if (dest.exists()) {
|
|
|
|
|
if (m_skipAll)
|
|
|
|
|
copyFile = false;
|
|
|
|
|
else if (!m_overwriteAll) {
|
|
|
|
|
const int res = QMessageBox::question(
|
|
|
|
|
m_parent,
|
|
|
|
|
QCoreApplication::translate("Utils::FileUtils", "Overwrite File?"),
|
|
|
|
|
QCoreApplication::translate("Utils::FileUtils", "Overwrite existing file \"%1\"?")
|
|
|
|
|
.arg(FilePath::fromFileInfo(dest).toUserOutput()),
|
|
|
|
|
QMessageBox::Yes | QMessageBox::YesToAll | QMessageBox::No | QMessageBox::NoToAll
|
|
|
|
|
| QMessageBox::Cancel);
|
|
|
|
|
if (res == QMessageBox::Cancel) {
|
|
|
|
|
return false;
|
|
|
|
|
} else if (res == QMessageBox::No) {
|
|
|
|
|
copyFile = false;
|
|
|
|
|
} else if (res == QMessageBox::NoToAll) {
|
|
|
|
|
m_skipAll = true;
|
|
|
|
|
copyFile = false;
|
|
|
|
|
} else if (res == QMessageBox::YesToAll) {
|
|
|
|
|
m_overwriteAll = true;
|
|
|
|
|
}
|
|
|
|
|
if (copyFile)
|
|
|
|
|
QFile::remove(dest.filePath());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (copyFile) {
|
|
|
|
|
if (!dest.absoluteDir().exists())
|
|
|
|
|
dest.absoluteDir().mkpath(dest.absolutePath());
|
|
|
|
|
if (!QFile::copy(src.filePath(), dest.filePath())) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
|
|
|
|
"Could not copy file \"%1\" to \"%2\".")
|
|
|
|
|
.arg(FilePath::fromFileInfo(src).toUserOutput(),
|
|
|
|
|
FilePath::fromFileInfo(dest).toUserOutput());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-02-12 16:06:24 +01:00
|
|
|
if (m_postOperation)
|
|
|
|
|
m_postOperation(dest);
|
2020-06-11 15:41:26 +02:00
|
|
|
}
|
|
|
|
|
m_files.append(dest.absoluteFilePath());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList FileUtils::CopyAskingForOverwrite::files() const
|
|
|
|
|
{
|
|
|
|
|
return m_files;
|
|
|
|
|
}
|
|
|
|
|
#endif // QT_GUI_LIB
|
|
|
|
|
|
2018-02-19 15:37:39 +01:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
template <>
|
2018-06-05 14:22:26 +02:00
|
|
|
void withNtfsPermissions(const std::function<void()> &task)
|
2018-02-19 15:37:39 +01:00
|
|
|
{
|
|
|
|
|
qt_ntfs_permission_lookup++;
|
|
|
|
|
task();
|
|
|
|
|
qt_ntfs_permission_lookup--;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2021-04-27 12:05:13 +02:00
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
} // namespace Utils
|
2019-12-17 14:18:14 +01:00
|
|
|
|
|
|
|
|
std::hash<Utils::FilePath>::result_type
|
|
|
|
|
std::hash<Utils::FilePath>::operator()(const std::hash<Utils::FilePath>::argument_type &fn) const
|
|
|
|
|
{
|
2021-05-19 09:28:57 +02:00
|
|
|
if (fn.caseSensitivity() == Qt::CaseInsensitive)
|
2019-12-17 14:18:14 +01:00
|
|
|
return hash<string>()(fn.toString().toUpper().toStdString());
|
|
|
|
|
return hash<string>()(fn.toString().toStdString());
|
|
|
|
|
}
|