2022-10-10 17:32:56 +02:00
|
|
|
// Copyright (C) 2022 The Qt Company Ltd.
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
#include "devicefileaccess.h"
|
|
|
|
|
|
|
|
|
|
#include "algorithm.h"
|
2022-10-14 13:39:16 +02:00
|
|
|
#include "commandline.h"
|
2022-10-21 10:43:09 +02:00
|
|
|
#include "environment.h"
|
2022-11-24 08:52:47 +01:00
|
|
|
#include "expected.h"
|
2022-10-21 10:43:09 +02:00
|
|
|
#include "hostosinfo.h"
|
|
|
|
|
#include "qtcassert.h"
|
2022-11-24 08:52:47 +01:00
|
|
|
#include "utilstr.h"
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2022-10-13 11:11:29 +02:00
|
|
|
#include <QOperatingSystemVersion>
|
2022-10-10 17:32:56 +02:00
|
|
|
#include <QRegularExpression>
|
|
|
|
|
#include <QStorageInfo>
|
|
|
|
|
|
2022-10-13 11:11:29 +02:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
|
#ifdef QTCREATOR_PCH_H
|
|
|
|
|
#define CALLBACK WINAPI
|
|
|
|
|
#endif
|
|
|
|
|
#include <shlobj.h>
|
2022-11-24 08:52:47 +01:00
|
|
|
#include <qt_windows.h>
|
2022-10-13 11:11:29 +02:00
|
|
|
#else
|
|
|
|
|
#include <qplatformdefs.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
namespace Utils {
|
|
|
|
|
|
|
|
|
|
// DeviceFileAccess
|
|
|
|
|
|
|
|
|
|
DeviceFileAccess::~DeviceFileAccess() = default;
|
|
|
|
|
|
2022-11-16 15:07:25 +01:00
|
|
|
// This takes a \a hostPath, typically the same as used with QFileInfo
|
|
|
|
|
// and returns the path component of a universal FilePath. Host and scheme
|
|
|
|
|
// of the latter are added by a higher level to form a FilePath.
|
|
|
|
|
QString DeviceFileAccess::mapToDevicePath(const QString &hostPath) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
2022-11-16 15:07:25 +01:00
|
|
|
return hostPath;
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isExecutableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isReadableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isWritableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isReadableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isWritableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::isDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 10:11:13 +02:00
|
|
|
bool DeviceFileAccess::isSymLink(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
bool DeviceFileAccess::ensureWritableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
if (isWritableDirectory(filePath))
|
|
|
|
|
return true;
|
|
|
|
|
return createDirectory(filePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::ensureExistingFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::createDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::exists(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::removeFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::removeRecursively(const FilePath &filePath, QString *error) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
Q_UNUSED(error)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<void> DeviceFileAccess::copyFile(const FilePath &filePath, const FilePath &target) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
Q_UNUSED(target)
|
|
|
|
|
QTC_CHECK(false);
|
2022-11-24 08:52:47 +01:00
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("copyFile is not implemented for \"%1\"").arg(filePath.toUserOutput()));
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::renameFile(const FilePath &filePath, const FilePath &target) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
Q_UNUSED(target)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OsType DeviceFileAccess::osType(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
return OsTypeOther;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePath DeviceFileAccess::symLinkTarget(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void DeviceFileAccess::iterateDirectory(const FilePath &filePath,
|
|
|
|
|
const FilePath::IterateDirCallback &callBack,
|
|
|
|
|
const FileFilter &filter) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
Q_UNUSED(callBack)
|
|
|
|
|
Q_UNUSED(filter)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 13:07:10 +01:00
|
|
|
Environment DeviceFileAccess::deviceEnvironment() const
|
|
|
|
|
{
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<QByteArray> DeviceFileAccess::fileContents(const FilePath &filePath,
|
|
|
|
|
qint64 limit,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
Q_UNUSED(limit)
|
|
|
|
|
Q_UNUSED(offset)
|
|
|
|
|
QTC_CHECK(false);
|
2022-11-24 08:52:47 +01:00
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("fileContents is not implemented for \"%1\"").arg(filePath.toUserOutput()));
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<qint64> DeviceFileAccess::writeFileContents(const FilePath &filePath,
|
|
|
|
|
const QByteArray &data,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
Q_UNUSED(data)
|
|
|
|
|
Q_UNUSED(offset)
|
|
|
|
|
QTC_CHECK(false);
|
2022-11-24 08:52:47 +01:00
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("writeFileContents is not implemented for \"%1\"").arg(filePath.toUserOutput()));
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePathInfo DeviceFileAccess::filePathInfo(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDateTime DeviceFileAccess::lastModified(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile::Permissions DeviceFileAccess::permissions(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DeviceFileAccess::setPermissions(const FilePath &filePath, QFile::Permissions) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 DeviceFileAccess::fileSize(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 DeviceFileAccess::bytesAvailable(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 11:11:29 +02:00
|
|
|
QByteArray DeviceFileAccess::fileId(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-10-21 10:43:09 +02:00
|
|
|
Q_UNUSED(filePath)
|
2022-10-13 11:11:29 +02:00
|
|
|
QTC_CHECK(false);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 11:43:18 +01:00
|
|
|
std::optional<FilePath> DeviceFileAccess::refersToExecutableFile(
|
2022-11-24 08:52:47 +01:00
|
|
|
const FilePath &filePath, FilePath::MatchScope matchScope) const
|
2022-10-21 10:43:09 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(matchScope)
|
2022-11-28 11:43:18 +01:00
|
|
|
if (isExecutableFile(filePath))
|
|
|
|
|
return filePath;
|
|
|
|
|
return {};
|
2022-10-21 10:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void DeviceFileAccess::asyncFileContents(const FilePath &filePath,
|
|
|
|
|
const Continuation<expected_str<QByteArray>> &cont,
|
|
|
|
|
qint64 limit,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
cont(fileContents(filePath, limit, offset));
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void DeviceFileAccess::asyncWriteFileContents(const FilePath &filePath,
|
|
|
|
|
const Continuation<expected_str<qint64>> &cont,
|
|
|
|
|
const QByteArray &data,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
cont(writeFileContents(filePath, data, offset));
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void DeviceFileAccess::asyncCopyFile(const FilePath &filePath,
|
|
|
|
|
const Continuation<expected_str<void>> &cont,
|
|
|
|
|
const FilePath &target) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
cont(copyFile(filePath, target));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DesktopDeviceFileAccess
|
|
|
|
|
|
|
|
|
|
DesktopDeviceFileAccess::~DesktopDeviceFileAccess() = default;
|
|
|
|
|
|
|
|
|
|
DesktopDeviceFileAccess *DesktopDeviceFileAccess::instance()
|
|
|
|
|
{
|
|
|
|
|
static DesktopDeviceFileAccess theInstance;
|
|
|
|
|
return &theInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::isExecutableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.isExecutable() && !fi.isDir();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
static std::optional<FilePath> isWindowsExecutableHelper(const FilePath &filePath,
|
|
|
|
|
const QStringView suffix)
|
2022-10-21 10:43:09 +02:00
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path().append(suffix));
|
2022-11-28 11:43:18 +01:00
|
|
|
if (!fi.isExecutable() || fi.isDir())
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return filePath.withNewPath(FileUtils::normalizedPathName(fi.filePath()));
|
2022-10-21 10:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 11:43:18 +01:00
|
|
|
std::optional<FilePath> DesktopDeviceFileAccess::refersToExecutableFile(
|
2022-11-24 08:52:47 +01:00
|
|
|
const FilePath &filePath, FilePath::MatchScope matchScope) const
|
2022-10-21 10:43:09 +02:00
|
|
|
{
|
|
|
|
|
if (isExecutableFile(filePath))
|
2022-11-28 11:43:18 +01:00
|
|
|
return filePath;
|
2022-10-21 10:43:09 +02:00
|
|
|
|
|
|
|
|
if (HostOsInfo::isWindowsHost()) {
|
|
|
|
|
if (matchScope == FilePath::WithExeSuffix || matchScope == FilePath::WithExeOrBatSuffix) {
|
2022-11-28 11:43:18 +01:00
|
|
|
if (auto res = isWindowsExecutableHelper(filePath, u".exe"))
|
|
|
|
|
return res;
|
2022-10-21 10:43:09 +02:00
|
|
|
}
|
|
|
|
|
if (matchScope == FilePath::WithBatSuffix || matchScope == FilePath::WithExeOrBatSuffix) {
|
2022-11-28 11:43:18 +01:00
|
|
|
if (auto res = isWindowsExecutableHelper(filePath, u".bat"))
|
|
|
|
|
return res;
|
2022-10-21 10:43:09 +02:00
|
|
|
}
|
|
|
|
|
if (matchScope == FilePath::WithAnySuffix) {
|
|
|
|
|
// That's usually .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH,
|
2022-11-28 11:43:18 +01:00
|
|
|
static const QStringList exts = qtcEnvironmentVariable("PATHEXT").toLower().split(';');
|
2022-10-21 10:43:09 +02:00
|
|
|
for (const QString &ext : exts) {
|
2022-11-28 11:43:18 +01:00
|
|
|
if (auto res = isWindowsExecutableHelper(filePath, ext))
|
|
|
|
|
return res;
|
2022-10-21 10:43:09 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-28 11:43:18 +01:00
|
|
|
return {};
|
2022-10-21 10:43:09 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
bool DesktopDeviceFileAccess::isReadableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.exists() && fi.isFile() && fi.isReadable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::isWritableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.exists() && fi.isFile() && fi.isWritable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::isReadableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.exists() && fi.isDir() && fi.isReadable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::isWritableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.exists() && fi.isDir() && fi.isWritable();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::isFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.isFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::isDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.isDir();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 10:11:13 +02:00
|
|
|
bool DesktopDeviceFileAccess::isSymLink(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
return fi.isSymLink();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
bool DesktopDeviceFileAccess::ensureWritableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo fi(filePath.path());
|
|
|
|
|
if (fi.isDir() && fi.isWritable())
|
|
|
|
|
return true;
|
|
|
|
|
return QDir().mkpath(filePath.path());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::ensureExistingFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
QFile f(filePath.path());
|
|
|
|
|
if (f.exists())
|
|
|
|
|
return true;
|
|
|
|
|
f.open(QFile::WriteOnly);
|
|
|
|
|
f.close();
|
|
|
|
|
return f.exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::createDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
QDir dir(filePath.path());
|
|
|
|
|
return dir.mkpath(dir.absolutePath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::exists(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return !filePath.isEmpty() && QFileInfo::exists(filePath.path());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::removeFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return QFile::remove(filePath.path());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::removeRecursively(const FilePath &filePath, QString *error) const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(!filePath.needsDevice(), return false);
|
|
|
|
|
QFileInfo fileInfo = filePath.toFileInfo();
|
|
|
|
|
if (!fileInfo.exists() && !fileInfo.isSymLink())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
QFile::setPermissions(fileInfo.absoluteFilePath(), fileInfo.permissions() | QFile::WriteUser);
|
|
|
|
|
|
|
|
|
|
if (fileInfo.isDir()) {
|
|
|
|
|
QDir dir(fileInfo.absoluteFilePath());
|
|
|
|
|
dir.setPath(dir.canonicalPath());
|
|
|
|
|
if (dir.isRoot()) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
2022-11-24 08:52:47 +01:00
|
|
|
"Refusing to remove root directory.");
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (dir.path() == QDir::home().canonicalPath()) {
|
|
|
|
|
if (error) {
|
|
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
2022-11-24 08:52:47 +01:00
|
|
|
"Refusing to remove your home directory.");
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
const QStringList fileNames = dir.entryList(QDir::Files | QDir::Hidden | QDir::System
|
|
|
|
|
| QDir::Dirs | QDir::NoDotAndDotDot);
|
2022-10-10 17:32:56 +02:00
|
|
|
for (const QString &fileName : fileNames) {
|
|
|
|
|
if (!removeRecursively(filePath / fileName, error))
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (!QDir::root().rmdir(dir.path())) {
|
|
|
|
|
if (error) {
|
2022-11-24 08:52:47 +01:00
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
|
|
|
|
"Failed to remove directory \"%1\".")
|
|
|
|
|
.arg(filePath.toUserOutput());
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-11-24 17:34:31 +01:00
|
|
|
if (!QFile::remove(filePath.path())) {
|
2022-10-10 17:32:56 +02:00
|
|
|
if (error) {
|
2022-11-24 08:52:47 +01:00
|
|
|
*error = QCoreApplication::translate("Utils::FileUtils",
|
|
|
|
|
"Failed to remove file \"%1\".")
|
|
|
|
|
.arg(filePath.toUserOutput());
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<void> DesktopDeviceFileAccess::copyFile(const FilePath &filePath,
|
|
|
|
|
const FilePath &target) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
if (QFile::copy(filePath.path(), target.path()))
|
|
|
|
|
return {};
|
|
|
|
|
return make_unexpected(Tr::tr("Failed to copy file \"%1\" to \"%2\".")
|
2023-01-10 23:54:26 +01:00
|
|
|
.arg(filePath.toUserOutput(), target.toUserOutput()));
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::renameFile(const FilePath &filePath, const FilePath &target) const
|
|
|
|
|
{
|
|
|
|
|
return QFile::rename(filePath.path(), target.path());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePathInfo DesktopDeviceFileAccess::filePathInfo(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
FilePathInfo result;
|
|
|
|
|
|
|
|
|
|
QFileInfo fi(filePath.path());
|
|
|
|
|
result.fileSize = fi.size();
|
|
|
|
|
result.lastModified = fi.lastModified();
|
|
|
|
|
result.fileFlags = (FilePathInfo::FileFlag) int(fi.permissions());
|
|
|
|
|
|
|
|
|
|
if (fi.isDir())
|
|
|
|
|
result.fileFlags |= FilePathInfo::DirectoryType;
|
|
|
|
|
if (fi.isFile())
|
|
|
|
|
result.fileFlags |= FilePathInfo::FileType;
|
|
|
|
|
if (fi.exists())
|
|
|
|
|
result.fileFlags |= FilePathInfo::ExistsFlag;
|
|
|
|
|
if (fi.isSymbolicLink())
|
|
|
|
|
result.fileFlags |= FilePathInfo::LinkType;
|
|
|
|
|
if (fi.isBundle())
|
|
|
|
|
result.fileFlags |= FilePathInfo::BundleType;
|
|
|
|
|
if (fi.isHidden())
|
|
|
|
|
result.fileFlags |= FilePathInfo::HiddenFlag;
|
|
|
|
|
if (fi.isRoot())
|
|
|
|
|
result.fileFlags |= FilePathInfo::RootFlag;
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePath DesktopDeviceFileAccess::symLinkTarget(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QFileInfo info(filePath.path());
|
|
|
|
|
if (!info.isSymLink())
|
|
|
|
|
return {};
|
|
|
|
|
return FilePath::fromString(info.symLinkTarget());
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void DesktopDeviceFileAccess::iterateDirectory(const FilePath &filePath,
|
|
|
|
|
const FilePath::IterateDirCallback &callBack,
|
|
|
|
|
const FileFilter &filter) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
QDirIterator it(filePath.path(), filter.nameFilters, filter.fileFilters, filter.iteratorFlags);
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
const FilePath path = FilePath::fromString(it.next());
|
|
|
|
|
bool res = false;
|
|
|
|
|
if (callBack.index() == 0)
|
|
|
|
|
res = std::get<0>(callBack)(path);
|
|
|
|
|
else
|
|
|
|
|
res = std::get<1>(callBack)(path, path.filePathInfo());
|
|
|
|
|
if (!res)
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 13:07:10 +01:00
|
|
|
Environment DesktopDeviceFileAccess::deviceEnvironment() const
|
|
|
|
|
{
|
|
|
|
|
return Environment::systemEnvironment();
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<QByteArray> DesktopDeviceFileAccess::fileContents(const FilePath &filePath,
|
|
|
|
|
qint64 limit,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
|
|
|
|
QFile f(path);
|
|
|
|
|
if (!f.exists())
|
2022-11-24 08:52:47 +01:00
|
|
|
return make_unexpected(Tr::tr("File \"%1\" does not exist").arg(path));
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
if (!f.open(QFile::ReadOnly))
|
2022-11-24 08:52:47 +01:00
|
|
|
return make_unexpected(Tr::tr("Could not open File \"%1\"").arg(path));
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
if (offset != 0)
|
|
|
|
|
f.seek(offset);
|
|
|
|
|
|
|
|
|
|
if (limit != -1)
|
|
|
|
|
return f.read(limit);
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
const QByteArray data = f.readAll();
|
|
|
|
|
if (f.error() != QFile::NoError) {
|
|
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("Cannot read \"%1\": %2").arg(filePath.toUserOutput(), f.errorString()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<qint64> DesktopDeviceFileAccess::writeFileContents(const FilePath &filePath,
|
|
|
|
|
const QByteArray &data,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
QFile file(filePath.path());
|
2022-12-09 00:06:39 +01:00
|
|
|
const bool isOpened = file.open(QFile::WriteOnly | QFile::Truncate);
|
2022-11-24 08:52:47 +01:00
|
|
|
if (!isOpened)
|
|
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("Could not open file \"%1\" for writing").arg(filePath.toUserOutput()));
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
if (offset != 0)
|
|
|
|
|
file.seek(offset);
|
|
|
|
|
qint64 res = file.write(data);
|
2022-11-24 08:52:47 +01:00
|
|
|
if (res != data.size())
|
|
|
|
|
return make_unexpected(
|
|
|
|
|
Tr::tr("Could not write to file \"%1\" (only %2 of %3 bytes written)")
|
|
|
|
|
.arg(filePath.toUserOutput())
|
|
|
|
|
.arg(res)
|
|
|
|
|
.arg(data.size()));
|
|
|
|
|
return res;
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDateTime DesktopDeviceFileAccess::lastModified(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return QFileInfo(filePath.path()).lastModified();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile::Permissions DesktopDeviceFileAccess::permissions(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return QFileInfo(filePath.path()).permissions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool DesktopDeviceFileAccess::setPermissions(const FilePath &filePath,
|
|
|
|
|
QFile::Permissions permissions) const
|
|
|
|
|
{
|
|
|
|
|
return QFile(filePath.path()).setPermissions(permissions);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 DesktopDeviceFileAccess::fileSize(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return QFileInfo(filePath.path()).size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 DesktopDeviceFileAccess::bytesAvailable(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
return QStorageInfo(filePath.path()).bytesAvailable();
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 11:11:29 +02: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"];
|
2022-11-24 08:52:47 +01:00
|
|
|
qsnprintf(buffer,
|
|
|
|
|
sizeof(buffer),
|
|
|
|
|
"%lx:%08lx%08lx",
|
2022-10-13 11:11:29 +02:00
|
|
|
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,
|
2022-11-24 08:52:47 +01:00
|
|
|
static_cast<FILE_INFO_BY_HANDLE_CLASS>(
|
|
|
|
|
18), // FileIdInfo in Windows 8
|
|
|
|
|
&infoEx,
|
|
|
|
|
sizeof(FILE_ID_INFO))) {
|
2022-10-13 11:11:29 +02:00
|
|
|
result = QByteArray::number(infoEx.VolumeSerialNumber, 16);
|
|
|
|
|
result += ':';
|
|
|
|
|
// Note: MinGW-64's definition of FILE_ID_128 differs from the MSVC one.
|
2022-11-24 08:52:47 +01:00
|
|
|
result += QByteArray(reinterpret_cast<const char *>(&infoEx.FileId),
|
|
|
|
|
int(sizeof(infoEx.FileId)))
|
|
|
|
|
.toHex();
|
2022-10-13 11:11:29 +02:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static QByteArray fileIdWin(HANDLE fHandle)
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows8
|
|
|
|
|
? fileIdWin8(HANDLE(fHandle))
|
|
|
|
|
: fileIdWin7(HANDLE(fHandle));
|
2022-10-13 11:11:29 +02:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
QByteArray DesktopDeviceFileAccess::fileId(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
QByteArray result;
|
|
|
|
|
|
|
|
|
|
#ifdef Q_OS_WIN
|
2022-11-24 08:52:47 +01:00
|
|
|
const HANDLE handle = CreateFile((wchar_t *) filePath.toUserOutput().utf16(),
|
|
|
|
|
0,
|
|
|
|
|
FILE_SHARE_READ,
|
|
|
|
|
NULL,
|
|
|
|
|
OPEN_EXISTING,
|
|
|
|
|
FILE_FLAG_BACKUP_SEMANTICS,
|
|
|
|
|
NULL);
|
2022-10-13 11:11:29 +02:00
|
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
|
|
|
result = fileIdWin(handle);
|
|
|
|
|
CloseHandle(handle);
|
|
|
|
|
}
|
|
|
|
|
#else // Copied from qfilesystemengine_unix.cpp
|
|
|
|
|
if (Q_UNLIKELY(filePath.isEmpty()))
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
QT_STATBUF statResult;
|
2022-11-24 17:34:31 +01:00
|
|
|
if (QT_STAT(filePath.path().toLocal8Bit().constData(), &statResult))
|
2022-10-13 11:11:29 +02:00
|
|
|
return result;
|
|
|
|
|
result = QByteArray::number(quint64(statResult.st_dev), 16);
|
|
|
|
|
result += ':';
|
|
|
|
|
result += QByteArray::number(quint64(statResult.st_ino));
|
|
|
|
|
#endif
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
OsType DesktopDeviceFileAccess::osType(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath);
|
|
|
|
|
return HostOsInfo::hostOs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UnixDeviceAccess
|
|
|
|
|
|
|
|
|
|
UnixDeviceFileAccess::~UnixDeviceFileAccess() = default;
|
|
|
|
|
|
2022-10-14 13:39:16 +02:00
|
|
|
bool UnixDeviceFileAccess::runInShellSuccess(const CommandLine &cmdLine,
|
|
|
|
|
const QByteArray &stdInData) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
2022-10-14 13:39:16 +02:00
|
|
|
return runInShell(cmdLine, stdInData).exitCode == 0;
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isExecutableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-x", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isReadableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-r", path, "-a", "-f", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isWritableFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-w", path, "-a", "-f", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isReadableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-r", path, "-a", "-d", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isWritableDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-w", path, "-a", "-d", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-f", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::isDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-d", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-14 10:11:13 +02:00
|
|
|
bool UnixDeviceFileAccess::isSymLink(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-h", path}, OsType::OsTypeLinux});
|
2022-10-14 10:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
bool UnixDeviceFileAccess::ensureExistingFile(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"touch", {path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::createDirectory(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"mkdir", {"-p", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::exists(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
const QString path = filePath.path();
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"test", {"-e", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::removeFile(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"rm", {filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::removeRecursively(const FilePath &filePath, QString *error) const
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(filePath.path().startsWith('/'), return false);
|
|
|
|
|
|
|
|
|
|
const QString path = filePath.cleanPath().path();
|
|
|
|
|
// We are expecting this only to be called in a context of build directories or similar.
|
|
|
|
|
// Chicken out in some cases that _might_ be user code errors.
|
|
|
|
|
QTC_ASSERT(path.startsWith('/'), return false);
|
|
|
|
|
int levelsNeeded = path.startsWith("/home/") ? 4 : 3;
|
|
|
|
|
if (path.startsWith("/tmp/"))
|
|
|
|
|
levelsNeeded = 2;
|
|
|
|
|
QTC_ASSERT(path.count('/') >= levelsNeeded, return false);
|
|
|
|
|
|
2022-10-18 09:55:58 +02:00
|
|
|
RunResult result = runInShell({"rm", {"-rf", "--", path}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
if (error)
|
|
|
|
|
*error = QString::fromUtf8(result.stdErr);
|
|
|
|
|
return result.exitCode == 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<void> UnixDeviceFileAccess::copyFile(const FilePath &filePath,
|
|
|
|
|
const FilePath &target) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult result = runInShell(
|
|
|
|
|
{"cp", {filePath.path(), target.path()}, OsType::OsTypeLinux});
|
|
|
|
|
|
|
|
|
|
if (result.exitCode != 0) {
|
|
|
|
|
return make_unexpected(Tr::tr("Failed to copy file \"%1\" to \"%2\": %3")
|
2023-01-10 23:54:26 +01:00
|
|
|
.arg(filePath.toUserOutput(), target.toUserOutput(), QString::fromUtf8(result.stdErr)));
|
2022-11-24 08:52:47 +01:00
|
|
|
}
|
|
|
|
|
return {};
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::renameFile(const FilePath &filePath, const FilePath &target) const
|
|
|
|
|
{
|
2022-10-18 09:55:58 +02:00
|
|
|
return runInShellSuccess({"mv", {filePath.path(), target.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePath UnixDeviceFileAccess::symLinkTarget(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult result = runInShell(
|
|
|
|
|
{"readlink", {"-n", "-e", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
const QString out = QString::fromUtf8(result.stdOut);
|
|
|
|
|
return out.isEmpty() ? FilePath() : filePath.withNewPath(out);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<QByteArray> UnixDeviceFileAccess::fileContents(const FilePath &filePath,
|
|
|
|
|
qint64 limit,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
QStringList args = {"if=" + filePath.path(), "status=none"};
|
|
|
|
|
if (limit > 0 || offset > 0) {
|
|
|
|
|
const qint64 gcd = std::gcd(limit, offset);
|
|
|
|
|
args += QString("bs=%1").arg(gcd);
|
|
|
|
|
args += QString("count=%1").arg(limit / gcd);
|
|
|
|
|
args += QString("seek=%1").arg(offset / gcd);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-18 09:55:58 +02:00
|
|
|
const RunResult r = runInShell({"dd", args, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
if (r.exitCode != 0)
|
2022-11-24 08:52:47 +01:00
|
|
|
return make_unexpected(Tr::tr("Failed reading file \"%1\": %2")
|
2023-01-10 23:54:26 +01:00
|
|
|
.arg(filePath.toUserOutput(), QString::fromUtf8(r.stdErr)));
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
return r.stdOut;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
expected_str<qint64> UnixDeviceFileAccess::writeFileContents(const FilePath &filePath,
|
|
|
|
|
const QByteArray &data,
|
|
|
|
|
qint64 offset) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
QStringList args = {"of=" + filePath.path()};
|
|
|
|
|
if (offset != 0) {
|
|
|
|
|
args.append("bs=1");
|
|
|
|
|
args.append(QString("seek=%1").arg(offset));
|
|
|
|
|
}
|
2022-11-24 08:52:47 +01:00
|
|
|
RunResult result = runInShell({"dd", args, OsType::OsTypeLinux}, data);
|
|
|
|
|
|
|
|
|
|
if (result.exitCode != 0) {
|
|
|
|
|
return make_unexpected(Tr::tr("Failed writing file \"%1\": %2")
|
2023-01-10 23:54:26 +01:00
|
|
|
.arg(filePath.toUserOutput(), QString::fromUtf8(result.stdErr)));
|
2022-11-24 08:52:47 +01:00
|
|
|
}
|
|
|
|
|
return data.size();
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OsType UnixDeviceFileAccess::osType(const FilePath &filePath) const
|
|
|
|
|
{
|
|
|
|
|
Q_UNUSED(filePath)
|
|
|
|
|
return OsTypeLinux;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QDateTime UnixDeviceFileAccess::lastModified(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult result = runInShell(
|
|
|
|
|
{"stat", {"-L", "-c", "%Y", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
qint64 secs = result.stdOut.toLongLong();
|
|
|
|
|
const QDateTime dt = QDateTime::fromSecsSinceEpoch(secs, Qt::UTC);
|
|
|
|
|
return dt;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QFile::Permissions UnixDeviceFileAccess::permissions(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult result = runInShell(
|
|
|
|
|
{"stat", {"-L", "-c", "%a", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
const uint bits = result.stdOut.toUInt(nullptr, 8);
|
|
|
|
|
QFileDevice::Permissions perm = {};
|
2022-11-24 08:52:47 +01:00
|
|
|
#define BIT(n, p) \
|
|
|
|
|
if (bits & (1 << n)) \
|
|
|
|
|
perm |= QFileDevice::p
|
2022-10-10 17:32:56 +02:00
|
|
|
BIT(0, ExeOther);
|
|
|
|
|
BIT(1, WriteOther);
|
|
|
|
|
BIT(2, ReadOther);
|
|
|
|
|
BIT(3, ExeGroup);
|
|
|
|
|
BIT(4, WriteGroup);
|
|
|
|
|
BIT(5, ReadGroup);
|
|
|
|
|
BIT(6, ExeUser);
|
|
|
|
|
BIT(7, WriteUser);
|
|
|
|
|
BIT(8, ReadUser);
|
|
|
|
|
#undef BIT
|
|
|
|
|
return perm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool UnixDeviceFileAccess::setPermissions(const FilePath &filePath, QFile::Permissions perms) const
|
|
|
|
|
{
|
|
|
|
|
const int flags = int(perms);
|
2022-11-24 08:52:47 +01:00
|
|
|
return runInShellSuccess(
|
|
|
|
|
{"chmod", {QString::number(flags, 16), filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 UnixDeviceFileAccess::fileSize(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult result = runInShell(
|
|
|
|
|
{"stat", {"-L", "-c", "%s", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
return result.stdOut.toLongLong();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
qint64 UnixDeviceFileAccess::bytesAvailable(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-10-18 09:55:58 +02:00
|
|
|
const RunResult result = runInShell({"df", {"-k", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
return FileUtils::bytesAvailableFromDFOutput(result.stdOut);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-13 11:11:29 +02:00
|
|
|
QByteArray UnixDeviceFileAccess::fileId(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult result = runInShell(
|
|
|
|
|
{"stat", {"-L", "-c", "%D:%i", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-13 11:11:29 +02:00
|
|
|
if (result.exitCode != 0)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return result.stdOut;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-10 17:32:56 +02:00
|
|
|
FilePathInfo UnixDeviceFileAccess::filePathInfo(const FilePath &filePath) const
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const RunResult stat = runInShell(
|
|
|
|
|
{"stat", {"-L", "-c", "%f %Y %s", filePath.path()}, OsType::OsTypeLinux});
|
2022-10-10 17:32:56 +02:00
|
|
|
return FileUtils::filePathInfoFromTriple(QString::fromLatin1(stat.stdOut));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// returns whether 'find' could be used.
|
2022-11-24 08:52:47 +01:00
|
|
|
bool UnixDeviceFileAccess::iterateWithFind(const FilePath &filePath,
|
|
|
|
|
const FileFilter &filter,
|
|
|
|
|
const FilePath::IterateDirCallback &callBack) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
QTC_CHECK(filePath.isAbsolutePath());
|
|
|
|
|
|
2022-10-18 09:55:58 +02:00
|
|
|
CommandLine cmdLine{"find", filter.asFindArguments(filePath.path()), OsType::OsTypeLinux};
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
// TODO: Using stat -L will always return the link target, not the link itself.
|
|
|
|
|
// We may wan't to add the information that it is a link at some point.
|
|
|
|
|
if (callBack.index() == 1)
|
2022-11-24 08:52:47 +01:00
|
|
|
cmdLine.addArgs(R"(-exec echo -n \"{}\"" " \; -exec stat -L -c "%f %Y %s" "{}" \;)",
|
|
|
|
|
CommandLine::Raw);
|
2022-10-10 17:32:56 +02:00
|
|
|
|
2022-10-14 13:39:16 +02:00
|
|
|
const RunResult result = runInShell(cmdLine);
|
2022-10-10 17:32:56 +02:00
|
|
|
const QString out = QString::fromUtf8(result.stdOut);
|
|
|
|
|
if (result.exitCode != 0) {
|
|
|
|
|
// Find returns non-zero exit code for any error it encounters, even if it finds some files.
|
|
|
|
|
|
|
|
|
|
if (!out.startsWith('"' + filePath.path())) {
|
|
|
|
|
if (!filePath.exists()) // File does not exist, so no files to find.
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// If the output does not start with the path we are searching in, find has failed.
|
|
|
|
|
// Possibly due to unknown options.
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList entries = out.split("\n", Qt::SkipEmptyParts);
|
|
|
|
|
if (entries.isEmpty())
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const auto toFilePath = [&filePath, &callBack](const QString &entry) -> bool {
|
|
|
|
|
if (callBack.index() == 0)
|
|
|
|
|
return std::get<0>(callBack)(filePath.withNewPath(entry));
|
|
|
|
|
|
|
|
|
|
const QString fileName = entry.mid(1, entry.lastIndexOf('\"') - 1);
|
|
|
|
|
const QString infos = entry.mid(fileName.length() + 3);
|
|
|
|
|
|
|
|
|
|
const FilePathInfo fi = FileUtils::filePathInfoFromTriple(infos);
|
|
|
|
|
if (!fi.fileFlags)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const FilePath fp = filePath.withNewPath(fileName);
|
2022-10-19 09:35:14 +02:00
|
|
|
// Do not return the entry for the directory we are searching in.
|
|
|
|
|
if (fp.path() == filePath.path())
|
|
|
|
|
return true;
|
2022-10-10 17:32:56 +02:00
|
|
|
return std::get<1>(callBack)(fp, fi);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Remove the first line, this can be the directory we are searching in.
|
|
|
|
|
// as long as we do not specify "mindepth > 0"
|
|
|
|
|
if (entries.front() == filePath.path())
|
|
|
|
|
entries.pop_front();
|
|
|
|
|
|
|
|
|
|
for (const QString &entry : entries) {
|
|
|
|
|
if (!toFilePath(entry))
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void UnixDeviceFileAccess::findUsingLs(const QString ¤t,
|
|
|
|
|
const FileFilter &filter,
|
|
|
|
|
QStringList *found) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
2022-10-18 09:55:58 +02:00
|
|
|
const RunResult result = runInShell({"ls", {"-1", "-p", "--", current}, OsType::OsTypeLinux});
|
2022-11-24 08:52:47 +01:00
|
|
|
const QStringList entries = QString::fromUtf8(result.stdOut).split('\n', Qt::SkipEmptyParts);
|
2022-10-10 17:32:56 +02:00
|
|
|
for (QString entry : entries) {
|
|
|
|
|
const QChar last = entry.back();
|
|
|
|
|
if (last == '/') {
|
|
|
|
|
entry.chop(1);
|
|
|
|
|
if (filter.iteratorFlags.testFlag(QDirIterator::Subdirectories))
|
|
|
|
|
findUsingLs(current + '/' + entry, filter, found);
|
|
|
|
|
}
|
|
|
|
|
found->append(entry);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Used on 'ls' output on unix-like systems.
|
|
|
|
|
static void iterateLsOutput(const FilePath &base,
|
|
|
|
|
const QStringList &entries,
|
|
|
|
|
const FileFilter &filter,
|
|
|
|
|
const FilePath::IterateDirCallback &callBack)
|
|
|
|
|
{
|
2022-11-24 08:52:47 +01:00
|
|
|
const QList<QRegularExpression> nameRegexps
|
|
|
|
|
= transform(filter.nameFilters, [](const QString &filter) {
|
|
|
|
|
QRegularExpression re;
|
|
|
|
|
re.setPattern(QRegularExpression::wildcardToRegularExpression(filter));
|
|
|
|
|
QTC_CHECK(re.isValid());
|
|
|
|
|
return re;
|
|
|
|
|
});
|
2022-10-10 17:32:56 +02:00
|
|
|
|
|
|
|
|
const auto nameMatches = [&nameRegexps](const QString &fileName) {
|
|
|
|
|
for (const QRegularExpression &re : nameRegexps) {
|
|
|
|
|
const QRegularExpressionMatch match = re.match(fileName);
|
|
|
|
|
if (match.hasMatch())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return nameRegexps.isEmpty();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FIXME: Handle filters. For now bark on unsupported options.
|
|
|
|
|
QTC_CHECK(filter.fileFilters == QDir::NoFilter);
|
|
|
|
|
|
|
|
|
|
for (const QString &entry : entries) {
|
|
|
|
|
if (!nameMatches(entry))
|
|
|
|
|
continue;
|
|
|
|
|
const FilePath current = base.pathAppended(entry);
|
|
|
|
|
bool res = false;
|
|
|
|
|
if (callBack.index() == 0)
|
|
|
|
|
res = std::get<0>(callBack)(current);
|
|
|
|
|
else
|
|
|
|
|
res = std::get<1>(callBack)(current, current.filePathInfo());
|
|
|
|
|
if (!res)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 08:52:47 +01:00
|
|
|
void UnixDeviceFileAccess::iterateDirectory(const FilePath &filePath,
|
|
|
|
|
const FilePath::IterateDirCallback &callBack,
|
|
|
|
|
const FileFilter &filter) const
|
2022-10-10 17:32:56 +02:00
|
|
|
{
|
|
|
|
|
// We try to use 'find' first, because that can filter better directly.
|
|
|
|
|
// Unfortunately, it's not installed on all devices by default.
|
|
|
|
|
if (m_tryUseFind) {
|
|
|
|
|
if (iterateWithFind(filePath, filter, callBack))
|
|
|
|
|
return;
|
2022-11-24 08:52:47 +01:00
|
|
|
m_tryUseFind
|
|
|
|
|
= false; // remember the failure for the next time and use the 'ls' fallback below.
|
2022-10-10 17:32:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if we do not have find - use ls as fallback
|
|
|
|
|
QStringList entries;
|
|
|
|
|
findUsingLs(filePath.path(), filter, &entries);
|
|
|
|
|
iterateLsOutput(filePath, entries, filter, callBack);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 13:07:10 +01:00
|
|
|
Environment UnixDeviceFileAccess::deviceEnvironment() const
|
|
|
|
|
{
|
|
|
|
|
const RunResult result = runInShell({"env", {}, OsType::OsTypeLinux});
|
|
|
|
|
const QString out = QString::fromUtf8(result.stdOut);
|
|
|
|
|
return Environment(out.split('\n', Qt::SkipEmptyParts), OsTypeLinux);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // Utils
|