forked from qt-creator/qt-creator
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 <christian.stenger@qt.io>
This commit is contained in:
@@ -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()) {
|
||||
|
||||
@@ -75,6 +75,7 @@ public:
|
||||
std::function<bool(const FilePath &)> isWritableDir;
|
||||
std::function<bool(const FilePath &)> isWritableFile;
|
||||
std::function<bool(const FilePath &)> ensureWritableDir;
|
||||
std::function<bool(const FilePath &)> ensureExistingFile;
|
||||
std::function<bool(const FilePath &)> createDir;
|
||||
std::function<bool(const FilePath &)> exists;
|
||||
std::function<bool(const FilePath &)> 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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -411,6 +411,12 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
|
||||
return device->ensureWritableDirectory(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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user