From 1e1c556bc83edbd5a2675e803bfe6df2f2ef9459 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 29 Jun 2021 09:41:53 +0200 Subject: [PATCH] Utils: Add a FilePath::ensureExistingFile Essentially a kind of 'touch', to be used in the CMake file API. Change-Id: Iaae62b441c0006b39d4bef5f06420e798c28c2a5 Reviewed-by: Christian Stenger --- src/libs/utils/fileutils.cpp | 14 ++++++++++++++ src/libs/utils/fileutils.h | 2 ++ src/plugins/docker/dockerdevice.cpp | 16 ++++++++++++++++ src/plugins/docker/dockerdevice.h | 1 + .../devicesupport/devicemanager.cpp | 6 ++++++ .../projectexplorer/devicesupport/idevice.cpp | 7 +++++++ .../projectexplorer/devicesupport/idevice.h | 1 + 7 files changed, 47 insertions(+) diff --git a/src/libs/utils/fileutils.cpp b/src/libs/utils/fileutils.cpp index c21a17d86b6..cc51b136971 100644 --- a/src/libs/utils/fileutils.cpp +++ b/src/libs/utils/fileutils.cpp @@ -898,6 +898,20 @@ bool FilePath::ensureWritableDir() const return QDir().mkpath(m_data); } +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(); +} + bool FilePath::isExecutableFile() const { if (needsDevice()) { diff --git a/src/libs/utils/fileutils.h b/src/libs/utils/fileutils.h index 3153b99bea9..9ef2ae21279 100644 --- a/src/libs/utils/fileutils.h +++ b/src/libs/utils/fileutils.h @@ -75,6 +75,7 @@ public: std::function isWritableDir; std::function isWritableFile; std::function ensureWritableDir; + std::function ensureExistingFile; std::function createDir; std::function exists; std::function removeFile; @@ -135,6 +136,7 @@ public: bool isWritableDir() const; bool isWritableFile() const; bool ensureWritableDir() const; + bool ensureExistingFile() const; bool isExecutableFile() const; bool isReadableFile() const; bool isReadableDir() const; diff --git a/src/plugins/docker/dockerdevice.cpp b/src/plugins/docker/dockerdevice.cpp index 3b1e9b1250a..478f96e91d5 100644 --- a/src/plugins/docker/dockerdevice.cpp +++ b/src/plugins/docker/dockerdevice.cpp @@ -877,6 +877,22 @@ bool DockerDevice::exists(const FilePath &filePath) const return exitCode == 0; } +bool DockerDevice::ensureExistingFile(const FilePath &filePath) const +{ + QTC_ASSERT(handlesFile(filePath), return false); + tryCreateLocalFileAccess(); + if (hasLocalFileAccess()) { + const FilePath localAccess = mapToLocalAccess(filePath); + const bool res = localAccess.ensureExistingFile(); + LOG("Ensure existing file? " << filePath.toUserOutput() << localAccess.toUserOutput() << res); + return res; + } + const QString path = filePath.path(); + const CommandLine cmd("touch", {path}); + const int exitCode = d->runSynchronously(cmd); + return exitCode == 0; +} + bool DockerDevice::removeFile(const FilePath &filePath) const { QTC_ASSERT(handlesFile(filePath), return false); diff --git a/src/plugins/docker/dockerdevice.h b/src/plugins/docker/dockerdevice.h index b299880904f..6d302d1815d 100644 --- a/src/plugins/docker/dockerdevice.h +++ b/src/plugins/docker/dockerdevice.h @@ -82,6 +82,7 @@ public: bool isWritableDirectory(const Utils::FilePath &filePath) const override; bool createDirectory(const Utils::FilePath &filePath) const override; bool exists(const Utils::FilePath &filePath) const override; + bool ensureExistingFile(const Utils::FilePath &filePath) const override; bool removeFile(const Utils::FilePath &filePath) const override; bool removeRecursively(const Utils::FilePath &filePath) const override; bool copyFile(const Utils::FilePath &filePath, const Utils::FilePath &target) const override; diff --git a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp index 5e1151c6b70..83eecf813a0 100644 --- a/src/plugins/projectexplorer/devicesupport/devicemanager.cpp +++ b/src/plugins/projectexplorer/devicesupport/devicemanager.cpp @@ -411,6 +411,12 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_uniqueensureWritableDirectory(filePath); }; + deviceHooks.ensureExistingFile = [](const FilePath &filePath) { + auto device = DeviceManager::deviceForPath(filePath); + QTC_ASSERT(device, return false); + return device->ensureExistingFile(filePath); + }; + deviceHooks.createDir = [](const FilePath &filePath) { auto device = DeviceManager::deviceForPath(filePath); QTC_ASSERT(device, return false); diff --git a/src/plugins/projectexplorer/devicesupport/idevice.cpp b/src/plugins/projectexplorer/devicesupport/idevice.cpp index ac36f7d3316..3adb3f2d305 100644 --- a/src/plugins/projectexplorer/devicesupport/idevice.cpp +++ b/src/plugins/projectexplorer/devicesupport/idevice.cpp @@ -260,6 +260,13 @@ bool IDevice::ensureWritableDirectory(const FilePath &filePath) const return createDirectory(filePath); } +bool IDevice::ensureExistingFile(const FilePath &filePath) const +{ + Q_UNUSED(filePath); + QTC_CHECK(false); + return false; +} + bool IDevice::createDirectory(const FilePath &filePath) const { Q_UNUSED(filePath); diff --git a/src/plugins/projectexplorer/devicesupport/idevice.h b/src/plugins/projectexplorer/devicesupport/idevice.h index a114f85c662..352ed64c67a 100644 --- a/src/plugins/projectexplorer/devicesupport/idevice.h +++ b/src/plugins/projectexplorer/devicesupport/idevice.h @@ -242,6 +242,7 @@ public: virtual bool isReadableDirectory(const Utils::FilePath &filePath) const; virtual bool isWritableDirectory(const Utils::FilePath &filePath) const; virtual bool ensureWritableDirectory(const Utils::FilePath &filePath) const; + virtual bool ensureExistingFile(const Utils::FilePath &filePath) const; virtual bool createDirectory(const Utils::FilePath &filePath) const; virtual bool exists(const Utils::FilePath &filePath) const; virtual bool removeFile(const Utils::FilePath &filePath) const;