forked from qt-creator/qt-creator
Utils: Introduce FilePath::ensureWritableDir()
Combine the test and potential creation in one operation to potentially reduce the number of remote calls. Change-Id: I06ffa1256593afec430eb35605ba3b249f1da97c Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
@@ -793,6 +793,18 @@ bool FilePath::isWritableDir() const
|
||||
return exists() && fi.isDir() && fi.isWritable();
|
||||
}
|
||||
|
||||
bool FilePath::ensureWritableDir() const
|
||||
{
|
||||
if (needsDevice()) {
|
||||
QTC_ASSERT(s_deviceHooks.ensureWritableDir, return false);
|
||||
return s_deviceHooks.ensureWritableDir(*this);
|
||||
}
|
||||
const QFileInfo fi{m_data};
|
||||
if (exists() && fi.isDir() && fi.isWritable())
|
||||
return true;
|
||||
return QDir().mkpath(m_data);
|
||||
}
|
||||
|
||||
bool FilePath::isExecutableFile() const
|
||||
{
|
||||
if (needsDevice()) {
|
||||
|
@@ -73,6 +73,7 @@ public:
|
||||
std::function<bool(const FilePath &)> isReadableFile;
|
||||
std::function<bool(const FilePath &)> isReadableDir;
|
||||
std::function<bool(const FilePath &)> isWritableDir;
|
||||
std::function<bool(const FilePath &)> ensureWritableDir;
|
||||
std::function<bool(const FilePath &)> createDir;
|
||||
std::function<QList<FilePath>(const FilePath &, const QStringList &, QDir::Filters)> dirEntries;
|
||||
std::function<QByteArray(const FilePath &, int)> fileContents;
|
||||
@@ -117,6 +118,7 @@ public:
|
||||
|
||||
bool isWritablePath() const { return isWritableDir(); } // Remove.
|
||||
bool isWritableDir() const;
|
||||
bool ensureWritableDir() const;
|
||||
bool isExecutableFile() const;
|
||||
bool isReadableFile() const;
|
||||
bool isReadableDir() const;
|
||||
|
@@ -389,6 +389,12 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
|
||||
return device->isWritableDirectory(filePath);
|
||||
};
|
||||
|
||||
deviceHooks.ensureWritableDir = [](const FilePath &filePath) {
|
||||
auto device = DeviceManager::deviceForPath(filePath);
|
||||
QTC_ASSERT(device, return false);
|
||||
return device->ensureWritableDirectory(filePath);
|
||||
};
|
||||
|
||||
deviceHooks.createDir = [](const FilePath &filePath) {
|
||||
auto device = DeviceManager::deviceForPath(filePath);
|
||||
QTC_ASSERT(device, return false);
|
||||
|
@@ -245,6 +245,13 @@ bool IDevice::isWritableDirectory(const FilePath &filePath) const
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDevice::ensureWritableDirectory(const FilePath &filePath) const
|
||||
{
|
||||
if (isWritableDirectory(filePath))
|
||||
return true;
|
||||
return createDirectory(filePath);
|
||||
}
|
||||
|
||||
bool IDevice::createDirectory(const Utils::FilePath &filePath) const
|
||||
{
|
||||
Q_UNUSED(filePath);
|
||||
|
@@ -240,6 +240,7 @@ public:
|
||||
virtual bool isReadableFile(const Utils::FilePath &filePath) const;
|
||||
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 createDirectory(const Utils::FilePath &filePath) const;
|
||||
virtual QList<Utils::FilePath> directoryEntries(const Utils::FilePath &filePath,
|
||||
const QStringList &nameFilters,
|
||||
|
Reference in New Issue
Block a user