Utils: Fix FileAccess if device returns nullptr

A device may return a nullptr if it does not support device access.

Change-Id: I302a2c63406268a2ff876c1bf408c5e26137fd9d
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-10-11 12:41:33 +02:00
parent a386b5467a
commit 2a84b9f02b
2 changed files with 9 additions and 4 deletions

View File

@@ -1179,13 +1179,13 @@ DeviceFileAccess *FilePath::fileAccess() const
static DeviceFileAccess dummy; static DeviceFileAccess dummy;
const expected_str<DeviceFileAccess *> access = getFileAccess(*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;
} }
bool FilePath::hasFileAccess() const bool FilePath::hasFileAccess() const
{ {
const expected_str<DeviceFileAccess *> access = getFileAccess(*this); const expected_str<DeviceFileAccess *> access = getFileAccess(*this);
return access && access.value(); return access.has_value();
} }
/*! /*!

View File

@@ -417,12 +417,17 @@ DeviceManager::DeviceManager(bool isInstance) : d(std::make_unique<DeviceManager
deviceHooks.fileAccess = [](const FilePath &filePath) -> expected_str<DeviceFileAccess *> { deviceHooks.fileAccess = [](const FilePath &filePath) -> expected_str<DeviceFileAccess *> {
if (!filePath.needsDevice()) if (!filePath.needsDevice())
return DesktopDeviceFileAccess::instance(); return DesktopDeviceFileAccess::instance();
auto device = DeviceManager::deviceForPath(filePath); IDevice::ConstPtr device = DeviceManager::deviceForPath(filePath);
if (!device) { if (!device) {
return make_unexpected( return make_unexpected(
QString("No device found for path \"%1\"").arg(filePath.toUserOutput())); QString("No device found for path \"%1\"").arg(filePath.toUserOutput()));
} }
return device->fileAccess(); DeviceFileAccess *fileAccess = device->fileAccess();
if (!fileAccess) {
return make_unexpected(
QString("No file access for device \"%1\"").arg(device->displayName()));
}
return fileAccess;
}; };
deviceHooks.environment = [](const FilePath &filePath) -> expected_str<Environment> { deviceHooks.environment = [](const FilePath &filePath) -> expected_str<Environment> {