forked from qt-creator/qt-creator
Utils: Add/fix FilePath::isFile and FilePath::isDir
In general, the more specific isReadableFile etc are preferred, but for porting a 1:1 match to QFileInfo functionality is helpful. Change-Id: I5b575ca9a5053f13f63c0fbe0e3dcc222494280f Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -939,6 +939,26 @@ bool FilePath::isReadableDir() const
|
||||
return fi.exists() && fi.isReadable() && fi.isDir();
|
||||
}
|
||||
|
||||
bool FilePath::isFile() const
|
||||
{
|
||||
if (needsDevice()) {
|
||||
QTC_ASSERT(s_deviceHooks.isFile, return false);
|
||||
return s_deviceHooks.isFile(*this);
|
||||
}
|
||||
const QFileInfo fi{m_data};
|
||||
return fi.exists() && fi.isFile();
|
||||
}
|
||||
|
||||
bool FilePath::isDir() const
|
||||
{
|
||||
if (needsDevice()) {
|
||||
QTC_ASSERT(s_deviceHooks.isDir, return false);
|
||||
return s_deviceHooks.isDir(*this);
|
||||
}
|
||||
const QFileInfo fi{m_data};
|
||||
return fi.exists() && fi.isDir();
|
||||
}
|
||||
|
||||
bool FilePath::createDir() const
|
||||
{
|
||||
if (needsDevice()) {
|
||||
@@ -1244,12 +1264,6 @@ bool FilePath::endsWith(const QString &s) const
|
||||
return m_data.endsWith(s, caseSensitivity());
|
||||
}
|
||||
|
||||
bool FilePath::isDir() const
|
||||
{
|
||||
QTC_CHECK(m_scheme.isEmpty()); // FIXME: Not implemented yet.
|
||||
return QFileInfo(m_data).isDir();
|
||||
}
|
||||
|
||||
/// \returns the relativeChildPath of FilePath to parent if FilePath is a child of parent
|
||||
/// \note returns a empty FilePath if FilePath is not a child of parent
|
||||
/// That is, this never returns a path starting with "../"
|
||||
|
@@ -77,6 +77,8 @@ public:
|
||||
std::function<bool(const FilePath &)> isReadableDir;
|
||||
std::function<bool(const FilePath &)> isWritableDir;
|
||||
std::function<bool(const FilePath &)> isWritableFile;
|
||||
std::function<bool(const FilePath &)> isFile;
|
||||
std::function<bool(const FilePath &)> isDir;
|
||||
std::function<bool(const FilePath &)> ensureWritableDir;
|
||||
std::function<bool(const FilePath &)> ensureExistingFile;
|
||||
std::function<bool(const FilePath &)> createDir;
|
||||
@@ -149,6 +151,8 @@ public:
|
||||
bool isReadableDir() const;
|
||||
bool isRelativePath() const;
|
||||
bool isAbsolutePath() const { return !isRelativePath(); }
|
||||
bool isFile() const;
|
||||
bool isDir() const;
|
||||
|
||||
bool createDir() const;
|
||||
QList<FilePath> dirEntries(const QStringList &nameFilters,
|
||||
@@ -177,7 +181,6 @@ public:
|
||||
bool startsWith(const QString &s) const;
|
||||
bool endsWith(const QString &s) const;
|
||||
|
||||
bool isDir() const;
|
||||
bool isNewerThan(const QDateTime &timeStamp) const;
|
||||
QDateTime lastModified() const;
|
||||
QFile::Permissions permissions() const;
|
||||
|
@@ -925,6 +925,38 @@ bool DockerDevice::isWritableDirectory(const FilePath &filePath) const
|
||||
return exitCode == 0;
|
||||
}
|
||||
|
||||
bool DockerDevice::isFile(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return false);
|
||||
tryCreateLocalFileAccess();
|
||||
if (hasLocalFileAccess()) {
|
||||
const FilePath localAccess = mapToLocalAccess(filePath);
|
||||
const bool res = localAccess.isFile();
|
||||
LOG("IsFile? " << filePath.toUserOutput() << localAccess.toUserOutput() << res);
|
||||
return res;
|
||||
}
|
||||
const QString path = filePath.path();
|
||||
const CommandLine cmd("test", {"-f", path});
|
||||
const int exitCode = d->runSynchronously(cmd);
|
||||
return exitCode == 0;
|
||||
}
|
||||
|
||||
bool DockerDevice::isDirectory(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return false);
|
||||
tryCreateLocalFileAccess();
|
||||
if (hasLocalFileAccess()) {
|
||||
const FilePath localAccess = mapToLocalAccess(filePath);
|
||||
const bool res = localAccess.isDir();
|
||||
LOG("IsDirectory? " << filePath.toUserOutput() << localAccess.toUserOutput() << res);
|
||||
return res;
|
||||
}
|
||||
const QString path = filePath.path();
|
||||
const CommandLine cmd("test", {"-d", path});
|
||||
const int exitCode = d->runSynchronously(cmd);
|
||||
return exitCode == 0;
|
||||
}
|
||||
|
||||
bool DockerDevice::createDirectory(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return false);
|
||||
|
@@ -81,6 +81,8 @@ public:
|
||||
bool isWritableFile(const Utils::FilePath &filePath) const override;
|
||||
bool isReadableDirectory(const Utils::FilePath &filePath) const override;
|
||||
bool isWritableDirectory(const Utils::FilePath &filePath) const override;
|
||||
bool isFile(const Utils::FilePath &filePath) const override;
|
||||
bool isDirectory(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;
|
||||
|
@@ -225,6 +225,18 @@ bool DesktopDevice::isWritableDirectory(const FilePath &filePath) const
|
||||
return filePath.isWritableDir();
|
||||
}
|
||||
|
||||
bool DesktopDevice::isFile(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return false);
|
||||
return filePath.isFile();
|
||||
}
|
||||
|
||||
bool DesktopDevice::isDirectory(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return false);
|
||||
return filePath.isDir();
|
||||
}
|
||||
|
||||
bool DesktopDevice::createDirectory(const FilePath &filePath) const
|
||||
{
|
||||
QTC_ASSERT(handlesFile(filePath), return false);
|
||||
|
@@ -66,6 +66,8 @@ public:
|
||||
bool isWritableFile(const Utils::FilePath &filePath) const override;
|
||||
bool isReadableDirectory(const Utils::FilePath &filePath) const override;
|
||||
bool isWritableDirectory(const Utils::FilePath &filePath) const override;
|
||||
bool isFile(const Utils::FilePath &filePath) const override;
|
||||
bool isDirectory(const Utils::FilePath &filePath) const override;
|
||||
bool ensureExistingFile(const Utils::FilePath &filePath) const override;
|
||||
bool createDirectory(const Utils::FilePath &filePath) const override;
|
||||
bool exists(const Utils::FilePath &filePath) const override;
|
||||
|
@@ -410,6 +410,18 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
|
||||
return device->isWritableFile(filePath);
|
||||
};
|
||||
|
||||
deviceHooks.isFile = [](const FilePath &filePath) {
|
||||
auto device = DeviceManager::deviceForPath(filePath);
|
||||
QTC_ASSERT(device, return false);
|
||||
return device->isFile(filePath);
|
||||
};
|
||||
|
||||
deviceHooks.isDir = [](const FilePath &filePath) {
|
||||
auto device = DeviceManager::deviceForPath(filePath);
|
||||
QTC_ASSERT(device, return false);
|
||||
return device->isDirectory(filePath);
|
||||
};
|
||||
|
||||
deviceHooks.ensureWritableDir = [](const FilePath &filePath) {
|
||||
auto device = DeviceManager::deviceForPath(filePath);
|
||||
QTC_ASSERT(device, return false);
|
||||
|
@@ -253,6 +253,20 @@ bool IDevice::isWritableDirectory(const FilePath &filePath) const
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDevice::isFile(const FilePath &filePath) const
|
||||
{
|
||||
Q_UNUSED(filePath);
|
||||
QTC_CHECK(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDevice::isDirectory(const FilePath &filePath) const
|
||||
{
|
||||
Q_UNUSED(filePath);
|
||||
QTC_CHECK(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IDevice::ensureWritableDirectory(const FilePath &filePath) const
|
||||
{
|
||||
if (isWritableDirectory(filePath))
|
||||
|
@@ -241,6 +241,8 @@ public:
|
||||
virtual bool isWritableFile(const Utils::FilePath &filePath) const;
|
||||
virtual bool isReadableDirectory(const Utils::FilePath &filePath) const;
|
||||
virtual bool isWritableDirectory(const Utils::FilePath &filePath) const;
|
||||
virtual bool isFile(const Utils::FilePath &filePath) const;
|
||||
virtual bool isDirectory(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;
|
||||
|
Reference in New Issue
Block a user