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"
|
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 <QDir>
|
2012-04-17 08:01:25 +02:00
|
|
|
#include <QDebug>
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QDateTime>
|
2017-03-09 23:02:32 +01:00
|
|
|
#include <QRegExp>
|
2014-07-07 16:16:49 +02:00
|
|
|
#include <QTimer>
|
2014-07-09 18:21:39 +02:00
|
|
|
#include <QUrl>
|
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
|
|
|
|
|
#include <qt_windows.h>
|
2013-12-04 07:37:07 +01: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
|
|
|
|
|
QDebug operator<<(QDebug dbg, const Utils::FileName &c)
|
|
|
|
|
{
|
|
|
|
|
return dbg << c.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
|
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
|
|
|
*/
|
2012-09-05 15:52:53 +02:00
|
|
|
bool FileUtils::removeRecursively(const FileName &filePath, QString *error)
|
2011-07-05 12:59:14 +02:00
|
|
|
{
|
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());
|
2011-07-21 14:49:16 +02:00
|
|
|
dir = dir.canonicalPath();
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-05 12:59:14 +02:00
|
|
|
QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden
|
|
|
|
|
| QDir::System | QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
foreach (const QString &fileName, fileNames) {
|
2012-09-05 15:52:53 +02:00
|
|
|
if (!removeRecursively(FileName(filePath).appendPath(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
|
|
|
*/
|
2012-09-05 15:52:53 +02:00
|
|
|
bool FileUtils::copyRecursively(const FileName &srcFilePath, const FileName &tgtFilePath,
|
2014-09-22 16:20:51 +03:00
|
|
|
QString *error, const std::function<bool (QFileInfo, QFileInfo, QString *)> ©Helper)
|
2011-07-05 12:59:14 +02:00
|
|
|
{
|
2012-09-05 15:52:53 +02:00
|
|
|
QFileInfo srcFileInfo = srcFilePath.toFileInfo();
|
2011-07-05 12:59:14 +02:00
|
|
|
if (srcFileInfo.isDir()) {
|
2014-10-24 13:15:54 +02:00
|
|
|
if (!tgtFilePath.exists()) {
|
2014-09-22 16:20:51 +03:00
|
|
|
QDir targetDir(tgtFilePath.toString());
|
|
|
|
|
targetDir.cdUp();
|
2015-01-10 23:40:32 +02:00
|
|
|
if (!targetDir.mkdir(tgtFilePath.fileName())) {
|
2014-09-22 16:20:51 +03:00
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils", "Failed to create directory \"%1\".")
|
|
|
|
|
.arg(tgtFilePath.toUserOutput());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2011-07-05 12:59:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-09-05 15:52:53 +02:00
|
|
|
QDir sourceDir(srcFilePath.toString());
|
|
|
|
|
QStringList fileNames = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot
|
|
|
|
|
| QDir::Hidden | QDir::System);
|
2011-07-05 12:59:14 +02:00
|
|
|
foreach (const QString &fileName, fileNames) {
|
2012-09-05 15:52:53 +02:00
|
|
|
FileName newSrcFilePath = srcFilePath;
|
|
|
|
|
newSrcFilePath.appendPath(fileName);
|
|
|
|
|
FileName newTgtFilePath = tgtFilePath;
|
|
|
|
|
newTgtFilePath.appendPath(fileName);
|
2014-09-22 16:20:51 +03:00
|
|
|
if (!copyRecursively(newSrcFilePath, newTgtFilePath, error, copyHelper))
|
2011-07-05 12:59:14 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2014-09-22 16:20:51 +03:00
|
|
|
if (copyHelper) {
|
|
|
|
|
if (!copyHelper(srcFileInfo, tgtFilePath.toFileInfo(), error))
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
if (!QFile::copy(srcFilePath.toString(), tgtFilePath.toString())) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils", "Could not copy file \"%1\" to \"%2\".")
|
|
|
|
|
.arg(srcFilePath.toUserOutput(), tgtFilePath.toUserOutput());
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2011-07-05 12:59:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 22:56:26 +02:00
|
|
|
/*!
|
|
|
|
|
If \a filePath is a directory, the function will recursively check all files and return
|
|
|
|
|
true if one of them is newer than \a timeStamp. If \a filePath 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
|
|
|
*/
|
2012-09-05 15:52:53 +02:00
|
|
|
bool FileUtils::isFileNewerThan(const FileName &filePath, const QDateTime &timeStamp)
|
2011-07-05 12:59:14 +02:00
|
|
|
{
|
2012-09-05 15:52:53 +02:00
|
|
|
QFileInfo fileInfo = filePath.toFileInfo();
|
2011-07-05 12:59:14 +02:00
|
|
|
if (!fileInfo.exists() || fileInfo.lastModified() >= timeStamp)
|
|
|
|
|
return true;
|
|
|
|
|
if (fileInfo.isDir()) {
|
2012-09-05 15:52:53 +02:00
|
|
|
const QStringList dirContents = QDir(filePath.toString())
|
2011-07-05 12:59:14 +02:00
|
|
|
.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
|
|
|
|
|
foreach (const QString &curFileName, dirContents) {
|
2012-09-05 15:52:53 +02:00
|
|
|
if (isFileNewerThan(FileName(filePath).appendPath(curFileName), timeStamp))
|
2011-07-05 12:59:14 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
2012-09-05 15:52:53 +02:00
|
|
|
FileName FileUtils::resolveSymlinks(const FileName &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)
|
2012-09-05 15:52:53 +02:00
|
|
|
return FileName();
|
|
|
|
|
return FileName::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
|
|
|
/*!
|
|
|
|
|
Recursively resolves possibly present symlinks in \a filePath.
|
|
|
|
|
Unlike QFileInfo::canonicalFilePath(), this function will not return an empty
|
|
|
|
|
string if path doesn't exist.
|
|
|
|
|
|
|
|
|
|
Returns the canonical path.
|
|
|
|
|
*/
|
|
|
|
|
FileName FileUtils::canonicalPath(const FileName &path)
|
|
|
|
|
{
|
2017-08-24 08:10:27 +03:00
|
|
|
const QString result = path.toFileInfo().canonicalFilePath();
|
2016-06-18 15:11:12 +02:00
|
|
|
if (result.isEmpty())
|
|
|
|
|
return path;
|
|
|
|
|
return FileName::fromString(result);
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
QString FileUtils::shortNativePath(const FileName &path)
|
|
|
|
|
{
|
|
|
|
|
if (HostOsInfo::isAnyUnixHost()) {
|
|
|
|
|
const FileName home = FileName::fromString(QDir::cleanPath(QDir::homePath()));
|
|
|
|
|
if (path.isChildOf(home)) {
|
|
|
|
|
return QLatin1Char('~') + QDir::separator()
|
|
|
|
|
+ QDir::toNativeSeparators(path.relativeChildPath(home).toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return path.toUserOutput();
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-16 17:10:45 +02:00
|
|
|
QString FileUtils::fileSystemFriendlyName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
QString result = name;
|
|
|
|
|
result.replace(QRegExp(QLatin1String("\\W")), QLatin1String("_"));
|
|
|
|
|
result.replace(QRegExp(QLatin1String("_+")), QLatin1String("_")); // compact _
|
|
|
|
|
result.remove(QRegExp(QLatin1String("^_*"))); // remove leading _
|
|
|
|
|
result.remove(QRegExp(QLatin1String("_+$"))); // remove trailing _
|
|
|
|
|
if (result.isEmpty())
|
|
|
|
|
result = QLatin1String("unknown");
|
|
|
|
|
return result;
|
2013-12-09 12:56:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int FileUtils::indexOfQmakeUnfriendly(const QString &name, int startpos)
|
|
|
|
|
{
|
|
|
|
|
static QRegExp checkRegExp(QLatin1String("[^a-zA-Z0-9_.-]"));
|
|
|
|
|
return checkRegExp.indexIn(name, startpos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2012-10-15 11:53:22 +02:00
|
|
|
bool FileUtils::makeWritable(const FileName &path)
|
|
|
|
|
{
|
|
|
|
|
const QString fileName = path.toString();
|
|
|
|
|
return QFile::setPermissions(fileName, QFile::permissions(fileName) | QFile::WriteUser);
|
|
|
|
|
}
|
|
|
|
|
|
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));
|
|
|
|
|
const LPCTSTR nameC = reinterpret_cast<LPCTSTR>(nativeSeparatorName.utf16()); // MinGW
|
|
|
|
|
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];
|
|
|
|
|
if (!SHGetPathFromIDList(file, buffer))
|
2013-10-23 22:07:46 +03:00
|
|
|
return name;
|
2013-12-04 07:37:07 +01:00
|
|
|
return QDir::fromNativeSeparators(QString::fromUtf16(reinterpret_cast<const ushort *>(buffer)));
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString FileUtils::resolvePath(const QString &baseDir, const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
|
return QString();
|
|
|
|
|
if (isAbsolutePath(fileName))
|
|
|
|
|
return QDir::cleanPath(fileName);
|
|
|
|
|
return QDir::cleanPath(baseDir + QLatin1Char('/') + fileName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
QByteArray FileReader::fetchQrc(const QString &fileName)
|
|
|
|
|
{
|
2012-04-17 08:01:25 +02:00
|
|
|
QTC_ASSERT(fileName.startsWith(QLatin1Char(':')), 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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileReader::fetch(const QString &fileName, QIODevice::OpenMode mode)
|
|
|
|
|
{
|
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
|
|
|
|
|
|
|
|
QFile file(fileName);
|
|
|
|
|
if (!file.open(QIODevice::ReadOnly | mode)) {
|
|
|
|
|
m_errorString = tr("Cannot open %1 for reading: %2").arg(
|
|
|
|
|
QDir::toNativeSeparators(fileName), file.errorString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
m_data = file.readAll();
|
|
|
|
|
if (file.error() != QFile::NoError) {
|
|
|
|
|
m_errorString = tr("Cannot read %1: %2").arg(
|
|
|
|
|
QDir::toNativeSeparators(fileName), file.errorString());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileReader::fetch(const QString &fileName, QIODevice::OpenMode mode, QString *errorString)
|
|
|
|
|
{
|
|
|
|
|
if (fetch(fileName, mode))
|
|
|
|
|
return true;
|
|
|
|
|
if (errorString)
|
|
|
|
|
*errorString = m_errorString;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-18 14:32:39 +02:00
|
|
|
#ifdef QT_GUI_LIB
|
2011-03-30 12:06:05 +02:00
|
|
|
bool FileReader::fetch(const QString &fileName, QIODevice::OpenMode mode, QWidget *parent)
|
|
|
|
|
{
|
|
|
|
|
if (fetch(fileName, mode))
|
|
|
|
|
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
|
|
|
|
|
|
|
|
FileSaverBase::FileSaverBase()
|
|
|
|
|
: m_hasError(false)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
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()) {
|
|
|
|
|
m_errorString = tr("Cannot write file %1: %2").arg(
|
|
|
|
|
QDir::toNativeSeparators(m_fileName), m_file->errorString());
|
|
|
|
|
} else {
|
|
|
|
|
m_errorString = tr("Cannot write file %1. Disk full?").arg(
|
|
|
|
|
QDir::toNativeSeparators(m_fileName));
|
|
|
|
|
}
|
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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FileSaver::FileSaver(const QString &filename, QIODevice::OpenMode mode)
|
|
|
|
|
{
|
|
|
|
|
m_fileName = filename;
|
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"};
|
2016-10-07 10:34:40 +02:00
|
|
|
const QString fn = QFileInfo(filename).baseName().toUpper();
|
|
|
|
|
for (const QString &rn : reservedNames) {
|
|
|
|
|
if (fn == rn) {
|
|
|
|
|
m_errorString = tr("%1: Is a reserved filename on Windows. Cannot save.").arg(filename);
|
|
|
|
|
m_hasError = true;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-03-30 12:06:05 +02:00
|
|
|
if (mode & (QIODevice::ReadOnly | QIODevice::Append)) {
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset(new QFile{filename});
|
2011-03-30 12:06:05 +02:00
|
|
|
m_isSafe = false;
|
|
|
|
|
} else {
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset(new SaveFile{filename});
|
2011-03-30 12:06:05 +02:00
|
|
|
m_isSafe = true;
|
|
|
|
|
}
|
|
|
|
|
if (!m_file->open(QIODevice::WriteOnly | mode)) {
|
|
|
|
|
QString err = QFile::exists(filename) ?
|
|
|
|
|
tr("Cannot overwrite file %1: %2") : tr("Cannot create file %1: %2");
|
|
|
|
|
m_errorString = err.arg(QDir::toNativeSeparators(filename), m_file->errorString());
|
|
|
|
|
m_hasError = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileSaver::finalize()
|
|
|
|
|
{
|
|
|
|
|
if (!m_isSafe)
|
|
|
|
|
return FileSaverBase::finalize();
|
|
|
|
|
|
2017-06-29 15:04:01 +02:00
|
|
|
SaveFile *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)
|
|
|
|
|
: m_autoRemove(true)
|
|
|
|
|
{
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset(new QTemporaryFile{});
|
|
|
|
|
QTemporaryFile *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;
|
|
|
|
|
}
|
|
|
|
|
m_fileName = tempFile->fileName();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TempFileSaver::~TempFileSaver()
|
|
|
|
|
{
|
2017-06-29 15:04:01 +02:00
|
|
|
m_file.reset();
|
2011-03-30 12:06:05 +02:00
|
|
|
if (m_autoRemove)
|
|
|
|
|
QFile::remove(m_fileName);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/*! \class Utils::FileName
|
|
|
|
|
|
2013-06-05 14:29:24 +02:00
|
|
|
\brief The FileName class is a light-weight convenience class for filenames.
|
2011-11-25 13:17:52 +01:00
|
|
|
|
|
|
|
|
On windows filenames are compared case insensitively.
|
|
|
|
|
*/
|
|
|
|
|
|
2011-11-08 18:06:45 +01:00
|
|
|
FileName::FileName()
|
|
|
|
|
: QString()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// Constructs a FileName from \a info
|
2011-11-08 18:06:45 +01:00
|
|
|
FileName::FileName(const QFileInfo &info)
|
|
|
|
|
: QString(info.absoluteFilePath())
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns a QFileInfo
|
|
|
|
|
QFileInfo FileName::toFileInfo() const
|
|
|
|
|
{
|
|
|
|
|
return QFileInfo(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// \returns a QString for passing on to QString based APIs
|
2015-02-27 09:42:48 +02:00
|
|
|
const QString &FileName::toString() const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2015-02-27 09:42:48 +02:00
|
|
|
return *this;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns a QString to display to the user
|
|
|
|
|
/// Converts the separators to the native format
|
|
|
|
|
QString FileName::toUserOutput() const
|
|
|
|
|
{
|
|
|
|
|
return QDir::toNativeSeparators(toString());
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-10 23:23:15 +02:00
|
|
|
QString FileName::fileName(int pathComponents) const
|
|
|
|
|
{
|
|
|
|
|
if (pathComponents < 0)
|
|
|
|
|
return *this;
|
|
|
|
|
const QChar slash = QLatin1Char('/');
|
|
|
|
|
int i = lastIndexOf(slash);
|
|
|
|
|
if (pathComponents == 0 || i == -1)
|
|
|
|
|
return mid(i + 1);
|
|
|
|
|
int component = i + 1;
|
|
|
|
|
// skip adjacent slashes
|
|
|
|
|
while (i > 0 && at(--i) == slash);
|
|
|
|
|
while (i >= 0 && --pathComponents >= 0) {
|
|
|
|
|
i = lastIndexOf(slash, i);
|
|
|
|
|
component = i + 1;
|
|
|
|
|
while (i > 0 && at(--i) == slash);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If there are no more slashes before the found one, return the entire string
|
|
|
|
|
if (i > 0 && lastIndexOf(slash, i) != -1)
|
|
|
|
|
return mid(component);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-24 13:15:54 +02:00
|
|
|
/// \returns a bool indicating whether a file with this
|
|
|
|
|
/// FileName exists.
|
|
|
|
|
bool FileName::exists() const
|
|
|
|
|
{
|
2017-08-28 11:29:15 +02:00
|
|
|
return !isEmpty() && QFileInfo::exists(*this);
|
2014-10-24 13:15:54 +02:00
|
|
|
}
|
|
|
|
|
|
2012-01-19 15:26:54 +01:00
|
|
|
/// Find the parent directory of a given directory.
|
|
|
|
|
|
2014-03-04 00:12:11 +01:00
|
|
|
/// Returns an empty FileName if the current directory is already
|
2012-01-19 15:26:54 +01:00
|
|
|
/// a root level directory.
|
|
|
|
|
|
|
|
|
|
/// \returns \a FileName with the last segment removed.
|
|
|
|
|
FileName FileName::parentDir() const
|
|
|
|
|
{
|
|
|
|
|
const QString basePath = toString();
|
|
|
|
|
if (basePath.isEmpty())
|
|
|
|
|
return FileName();
|
|
|
|
|
|
|
|
|
|
const QDir base(basePath);
|
|
|
|
|
if (base.isRoot())
|
|
|
|
|
return FileName();
|
|
|
|
|
|
|
|
|
|
const QString path = basePath + QLatin1String("/..");
|
|
|
|
|
const QString parent = QDir::cleanPath(path);
|
|
|
|
|
|
|
|
|
|
return FileName::fromString(parent);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-16 15:58:32 +02:00
|
|
|
/// Constructs a FileName from \a filename
|
|
|
|
|
/// \a filename is not checked for validity.
|
2011-11-08 18:06:45 +01:00
|
|
|
FileName FileName::fromString(const QString &filename)
|
|
|
|
|
{
|
|
|
|
|
return FileName(filename);
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-16 15:58:32 +02:00
|
|
|
/// Constructs a FileName from \a fileName. The \a defaultExtension is appended
|
|
|
|
|
/// to \a filename if that does not have an extension already.
|
|
|
|
|
/// \a fileName is not checked for validity.
|
|
|
|
|
FileName FileName::fromString(const QString &filename, const QString &defaultExtension)
|
|
|
|
|
{
|
|
|
|
|
if (filename.isEmpty() || defaultExtension.isEmpty())
|
|
|
|
|
return filename;
|
|
|
|
|
|
|
|
|
|
QString rc = filename;
|
|
|
|
|
QFileInfo fi(filename);
|
|
|
|
|
// Add extension unless user specified something else
|
|
|
|
|
const QChar dot = QLatin1Char('.');
|
|
|
|
|
if (!fi.fileName().contains(dot)) {
|
|
|
|
|
if (!defaultExtension.startsWith(dot))
|
|
|
|
|
rc += dot;
|
|
|
|
|
rc += defaultExtension;
|
|
|
|
|
}
|
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-07 15:46:36 +01:00
|
|
|
/// Constructs a FileName from \a fileName
|
|
|
|
|
/// \a fileName is not checked for validity.
|
|
|
|
|
FileName FileName::fromLatin1(const QByteArray &filename)
|
|
|
|
|
{
|
|
|
|
|
return FileName(QString::fromLatin1(filename));
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// Constructs a FileName from \a fileName
|
|
|
|
|
/// \a fileName is only passed through QDir::cleanPath
|
|
|
|
|
FileName FileName::fromUserInput(const QString &filename)
|
|
|
|
|
{
|
2013-06-25 23:32:04 +03:00
|
|
|
QString clean = QDir::cleanPath(filename);
|
|
|
|
|
if (clean.startsWith(QLatin1String("~/")))
|
2013-06-27 14:15:19 +02:00
|
|
|
clean = QDir::homePath() + clean.mid(1);
|
2013-06-25 23:32:04 +03:00
|
|
|
return FileName(clean);
|
2011-11-25 13:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-27 12:11:46 +01:00
|
|
|
/// Constructs a FileName from \a fileName, which is encoded as UTF-8.
|
|
|
|
|
/// \a fileName is not checked for validity.
|
|
|
|
|
FileName FileName::fromUtf8(const char *filename, int filenameSize)
|
|
|
|
|
{
|
|
|
|
|
return FileName(QString::fromUtf8(filename, filenameSize));
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 18:06:45 +01:00
|
|
|
FileName::FileName(const QString &string)
|
|
|
|
|
: QString(string)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2011-11-25 13:17:52 +01:00
|
|
|
|
2011-11-08 18:06:45 +01:00
|
|
|
bool FileName::operator==(const FileName &other) const
|
|
|
|
|
{
|
2012-08-31 16:39:20 +02:00
|
|
|
return QString::compare(*this, other, HostOsInfo::fileNameCaseSensitivity()) == 0;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
bool FileName::operator!=(const FileName &other) const
|
|
|
|
|
{
|
|
|
|
|
return !(*this == other);
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-08 18:06:45 +01:00
|
|
|
bool FileName::operator<(const FileName &other) const
|
|
|
|
|
{
|
2012-08-31 16:39:20 +02:00
|
|
|
return QString::compare(*this, other, HostOsInfo::fileNameCaseSensitivity()) < 0;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileName::operator<=(const FileName &other) const
|
|
|
|
|
{
|
2012-08-31 16:39:20 +02:00
|
|
|
return QString::compare(*this, other, HostOsInfo::fileNameCaseSensitivity()) <= 0;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileName::operator>(const FileName &other) const
|
|
|
|
|
{
|
|
|
|
|
return other < *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool FileName::operator>=(const FileName &other) const
|
|
|
|
|
{
|
|
|
|
|
return other <= *this;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 15:07:54 +02:00
|
|
|
FileName FileName::operator+(const QString &s) const
|
|
|
|
|
{
|
|
|
|
|
FileName result(*this);
|
|
|
|
|
result.appendString(s);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns whether FileName is a child of \a s
|
|
|
|
|
bool FileName::isChildOf(const FileName &s) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2012-10-04 06:06:09 +02:00
|
|
|
if (s.isEmpty())
|
|
|
|
|
return false;
|
2012-08-31 16:39:20 +02:00
|
|
|
if (!QString::startsWith(s, HostOsInfo::fileNameCaseSensitivity()))
|
2011-11-25 13:17:52 +01:00
|
|
|
return false;
|
|
|
|
|
if (size() <= s.size())
|
|
|
|
|
return false;
|
2012-10-04 06:06:09 +02:00
|
|
|
// s is root, '/' was already tested in startsWith
|
|
|
|
|
if (s.QString::endsWith(QLatin1Char('/')))
|
|
|
|
|
return true;
|
|
|
|
|
// s is a directory, next character should be '/' (/tmpdir is NOT a child of /tmp)
|
2012-01-04 17:34:08 +01:00
|
|
|
return at(s.size()) == QLatin1Char('/');
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2012-07-18 14:46:05 +04:00
|
|
|
/// \overload
|
|
|
|
|
bool FileName::isChildOf(const QDir &dir) const
|
|
|
|
|
{
|
2014-07-15 23:33:17 +03:00
|
|
|
return isChildOf(FileName::fromString(dir.absolutePath()));
|
2012-07-18 14:46:05 +04:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns whether FileName endsWith \a s
|
2011-11-08 18:06:45 +01:00
|
|
|
bool FileName::endsWith(const QString &s) const
|
|
|
|
|
{
|
2012-08-31 16:39:20 +02:00
|
|
|
return QString::endsWith(s, HostOsInfo::fileNameCaseSensitivity());
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// \returns the relativeChildPath of FileName to parent if FileName is a child of parent
|
|
|
|
|
/// \note returns a empty FileName if FileName is not a child of parent
|
|
|
|
|
/// That is, this never returns a path starting with "../"
|
|
|
|
|
FileName FileName::relativeChildPath(const FileName &parent) const
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2011-11-25 13:17:52 +01:00
|
|
|
if (!isChildOf(parent))
|
2014-07-15 23:33:17 +03:00
|
|
|
return FileName();
|
2011-11-25 13:17:52 +01:00
|
|
|
return FileName(QString::mid(parent.size() + 1, -1));
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2011-11-25 13:17:52 +01:00
|
|
|
/// Appends \a s, ensuring a / between the parts
|
2011-11-25 13:19:58 +01:00
|
|
|
FileName &FileName::appendPath(const QString &s)
|
2011-11-08 18:06:45 +01:00
|
|
|
{
|
2014-11-06 17:03:21 +01:00
|
|
|
if (s.isEmpty())
|
|
|
|
|
return *this;
|
2012-07-07 16:35:15 +03:00
|
|
|
if (!isEmpty() && !QString::endsWith(QLatin1Char('/')))
|
2013-10-30 16:24:58 +01:00
|
|
|
appendString(QLatin1Char('/'));
|
|
|
|
|
appendString(s);
|
2011-11-25 13:19:58 +01:00
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-30 16:24:58 +01:00
|
|
|
FileName &FileName::appendString(const QString &str)
|
2011-11-25 13:19:58 +01:00
|
|
|
{
|
|
|
|
|
QString::append(str);
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-30 16:24:58 +01:00
|
|
|
FileName &FileName::appendString(QChar str)
|
2011-11-25 13:19:58 +01:00
|
|
|
{
|
|
|
|
|
QString::append(str);
|
|
|
|
|
return *this;
|
2011-11-08 18:06:45 +01:00
|
|
|
}
|
|
|
|
|
|
2014-11-27 10:49:18 +01:00
|
|
|
QTextStream &operator<<(QTextStream &s, const FileName &fn)
|
|
|
|
|
{
|
|
|
|
|
return s << fn.toString();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
|
2011-03-30 12:06:05 +02:00
|
|
|
} // namespace Utils
|
2011-11-08 18:06:45 +01:00
|
|
|
|
2011-11-09 15:08:53 +01:00
|
|
|
QT_BEGIN_NAMESPACE
|
2011-11-08 18:06:45 +01:00
|
|
|
uint qHash(const Utils::FileName &a)
|
|
|
|
|
{
|
2014-06-12 13:33:51 +02:00
|
|
|
if (Utils::HostOsInfo::fileNameCaseSensitivity() == Qt::CaseInsensitive)
|
2012-08-23 15:53:58 +02:00
|
|
|
return qHash(a.toString().toUpper());
|
2011-11-08 18:06:45 +01:00
|
|
|
return qHash(a.toString());
|
|
|
|
|
}
|
2011-11-09 15:08:53 +01:00
|
|
|
QT_END_NAMESPACE
|