From 34b42a477229bd2e57137909fcf9724f56c20068 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 8 Oct 2021 10:31:30 +0200 Subject: [PATCH] Utils: Introduce FilePath::mapTo{Device,Global}Path Re-directing via IDevice::mapTo{Device,Global}Path Change-Id: Ica957839479967790bbd93e90eced66aa0b122a8 Reviewed-by: David Schulz --- src/libs/utils/filepath.cpp | 18 ++++++++++++++++++ src/libs/utils/filepath.h | 3 +++ src/libs/utils/fileutils.h | 2 ++ src/plugins/docker/dockerdevice.cpp | 11 +++++++++++ src/plugins/docker/dockerdevice.h | 1 + .../devicesupport/devicemanager.cpp | 12 ++++++++++++ .../projectexplorer/devicesupport/idevice.cpp | 5 +++++ .../projectexplorer/devicesupport/idevice.h | 1 + 8 files changed, 53 insertions(+) diff --git a/src/libs/utils/filepath.cpp b/src/libs/utils/filepath.cpp index fd68683acee..ce6d0cc2cdd 100644 --- a/src/libs/utils/filepath.cpp +++ b/src/libs/utils/filepath.cpp @@ -819,6 +819,24 @@ FilePath FilePath::symLinkTarget() const return FilePath::fromString(info.symLinkTarget()); } +FilePath FilePath::mapToGlobalPath() const +{ + if (needsDevice()) { + QTC_ASSERT(s_deviceHooks.mapToGlobalPath, return {}); + return s_deviceHooks.mapToGlobalPath(*this); + } + return *this; +} + +QString FilePath::mapToDevicePath() const +{ + if (needsDevice()) { + QTC_ASSERT(s_deviceHooks.mapToDevicePath, return {}); + return s_deviceHooks.mapToDevicePath(*this); + } + return m_data; +} + FilePath FilePath::withExecutableSuffix() const { FilePath res = *this; diff --git a/src/libs/utils/filepath.h b/src/libs/utils/filepath.h index 409421eaf58..21ef3e12438 100644 --- a/src/libs/utils/filepath.h +++ b/src/libs/utils/filepath.h @@ -161,6 +161,9 @@ public: QDir::Filters filters = QDir::NoFilter, QDirIterator::IteratorFlags flags = QDirIterator::NoIteratorFlags) const; + [[nodiscard]] FilePath mapToGlobalPath() const; + [[nodiscard]] QString mapToDevicePath() const; + // makes sure that capitalization of directories is canonical // on Windows and macOS. This is rarely needed. [[nodiscard]] FilePath normalizedPathName() const; diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index a92017f7709..9fd0b6c2a03 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -78,6 +78,8 @@ public: std::function renameFile; std::function &)> searchInPath; std::function symLinkTarget; + std::function mapToGlobalPath; + std::function mapToDevicePath; std::function(const FilePath &, const QStringList &, QDir::Filters, QDir::SortFlags)> dirEntries; std::function fileContents; diff --git a/src/plugins/docker/dockerdevice.cpp b/src/plugins/docker/dockerdevice.cpp index 638fb91db0e..377b777d40b 100644 --- a/src/plugins/docker/dockerdevice.cpp +++ b/src/plugins/docker/dockerdevice.cpp @@ -1069,6 +1069,17 @@ FilePath DockerDevice::mapToGlobalPath(const FilePath &pathOnDevice) const return result; } +QString DockerDevice::mapToDevicePath(const Utils::FilePath &globalPath) const +{ + const FilePath normalized = globalPath.normalizedPathName(); + QString path = normalized.path(); + if (normalized.startsWithDriveLetter()) { + const QChar lowerDriveLetter = path.at(0).toLower(); + path = '/' + lowerDriveLetter + path.mid(2); // strip C: + } + return path; +} + bool DockerDevice::handlesFile(const FilePath &filePath) const { return filePath.scheme() == "docker" && filePath.host() == d->m_data.imageId; diff --git a/src/plugins/docker/dockerdevice.h b/src/plugins/docker/dockerdevice.h index ded9a93b063..c4d0efe703b 100644 --- a/src/plugins/docker/dockerdevice.h +++ b/src/plugins/docker/dockerdevice.h @@ -76,6 +76,7 @@ public: ProjectExplorer::DeviceEnvironmentFetcher::Ptr environmentFetcher() const override; Utils::FilePath mapToGlobalPath(const Utils::FilePath &pathOnDevice) const override; + QString mapToDevicePath(const Utils::FilePath &globalPath) const override; bool handlesFile(const Utils::FilePath &filePath) const override; bool isExecutableFile(const Utils::FilePath &filePath) const override; diff --git a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp index f7470cd079b..1e671e5fe31 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp +++ b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp @@ -484,6 +484,18 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_uniquesymLinkTarget(filePath); }; + deviceHooks.mapToGlobalPath = [](const FilePath &filePath) { + auto device = DeviceManager::deviceForPath(filePath); + QTC_ASSERT(device, return FilePath{}); + return device->mapToGlobalPath(filePath); + }; + + deviceHooks.mapToDevicePath = [](const FilePath &filePath) { + auto device = DeviceManager::deviceForPath(filePath); + QTC_ASSERT(device, return QString{}); + return device->mapToDevicePath(filePath); + }; + deviceHooks.dirEntries = [](const FilePath &filePath, const QStringList &nameFilters, QDir::Filters filters, QDir::SortFlags sort) { auto device = DeviceManager::deviceForPath(filePath); diff --git a/src/plugins/projectexplorer/devicesupport/idevice.cpp b/src/plugins/projectexplorer/devicesupport/idevice.cpp index 8c90a7fc146..f1bedcc4416 100644 --- a/src/plugins/projectexplorer/devicesupport/idevice.cpp +++ b/src/plugins/projectexplorer/devicesupport/idevice.cpp @@ -212,6 +212,11 @@ FilePath IDevice::mapToGlobalPath(const FilePath &pathOnDevice) const return pathOnDevice; } +QString IDevice::mapToDevicePath(const FilePath &globalPath) const +{ + return globalPath.path(); +} + bool IDevice::handlesFile(const FilePath &filePath) const { Q_UNUSED(filePath); diff --git a/src/plugins/projectexplorer/devicesupport/idevice.h b/src/plugins/projectexplorer/devicesupport/idevice.h index 873eb380bb3..56bdbb5bd28 100644 --- a/src/plugins/projectexplorer/devicesupport/idevice.h +++ b/src/plugins/projectexplorer/devicesupport/idevice.h @@ -237,6 +237,7 @@ public: bool isAnyUnixDevice() const; virtual Utils::FilePath mapToGlobalPath(const Utils::FilePath &pathOnDevice) const; + virtual QString mapToDevicePath(const Utils::FilePath &globalPath) const; virtual bool handlesFile(const Utils::FilePath &filePath) const; virtual bool isExecutableFile(const Utils::FilePath &filePath) const;