Utils: Add FilePath.hasFileAccess()

FilePath::hasFileAccess allows a user to test if a specific device
can access files. This is faster than calling "exists()" as it does
not have to actually check anything on the device.

Task-number: QTCREATORBUG-28531
Change-Id: I94b5b9e0ae020b81f126c61fd06bb25f3d4a88cb
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-01-12 13:08:23 +01:00
parent b3bc499ebd
commit 21ef25a0f5
3 changed files with 17 additions and 5 deletions

View File

@@ -932,9 +932,9 @@ void FilePath::setFromString(const QStringView fileNameView)
setParts({}, {}, fileNameView); setParts({}, {}, fileNameView);
} }
DeviceFileAccess *FilePath::fileAccess() const expected_str<DeviceFileAccess *> getFileAccess(const FilePath &filePath)
{ {
if (!needsDevice()) if (!filePath.needsDevice())
return DesktopDeviceFileAccess::instance(); return DesktopDeviceFileAccess::instance();
if (!s_deviceHooks.fileAccess) { if (!s_deviceHooks.fileAccess) {
@@ -943,12 +943,23 @@ DeviceFileAccess *FilePath::fileAccess() const
return DesktopDeviceFileAccess::instance(); return DesktopDeviceFileAccess::instance();
} }
return s_deviceHooks.fileAccess(filePath);
}
DeviceFileAccess *FilePath::fileAccess() const
{
static DeviceFileAccess dummy; static DeviceFileAccess dummy;
const expected_str<DeviceFileAccess *> access = s_deviceHooks.fileAccess(*this); const expected_str<DeviceFileAccess *> access = getFileAccess(*this);
QTC_ASSERT_EXPECTED(access, return &dummy); QTC_ASSERT_EXPECTED(access, return &dummy);
return *access ? *access : &dummy; return *access ? *access : &dummy;
} }
bool FilePath::hasFileAccess() const
{
const expected_str<DeviceFileAccess *> access = getFileAccess(*this);
return access && access.value();
}
/*! /*!
Constructs a FilePath from \a filePath. The \a defaultExtension is appended Constructs a FilePath from \a filePath. The \a defaultExtension is appended
to \a filePath if that does not have an extension already. to \a filePath if that does not have an extension already.

View File

@@ -212,6 +212,7 @@ public:
// There are usually other means available. E.g. distinguishing based // There are usually other means available. E.g. distinguishing based
// on FilePath::osType(). // on FilePath::osType().
bool needsDevice() const; bool needsDevice() const;
bool hasFileAccess() const;
bool isSameDevice(const FilePath &other) const; bool isSameDevice(const FilePath &other) const;
bool isSameFile(const FilePath &other) const; bool isSameFile(const FilePath &other) const;

View File

@@ -36,7 +36,7 @@ FilePaths FSEngine::registeredDeviceRoots()
void FSEngine::addDevice(const FilePath &deviceRoot) void FSEngine::addDevice(const FilePath &deviceRoot)
{ {
if (deviceRoot.exists()) if (deviceRoot.hasFileAccess())
deviceRoots().append(deviceRoot); deviceRoots().append(deviceRoot);
} }
@@ -53,7 +53,7 @@ FilePaths &FSEngine::deviceRoots()
QStringList &FSEngine::deviceSchemes() QStringList &FSEngine::deviceSchemes()
{ {
static QStringList g_deviceSchemes {"device"}; static QStringList g_deviceSchemes{"device"};
return g_deviceSchemes; return g_deviceSchemes;
} }