Add error handling

in case directory could not be created or permissions could not be set
This commit is contained in:
Stefan Forstenlechner 2024-06-19 17:57:41 +02:00
parent 5c865bed99
commit 52b6586594
3 changed files with 32 additions and 11 deletions

View file

@ -177,25 +177,27 @@ void DatabaseSettingsWidgetRemote::testDownload()
params->downloadCommand = m_ui->downloadCommand->text();
params->downloadInput = m_ui->inputForDownload->toPlainText();
QScopedPointer<RemoteHandler> remoteHandler(new RemoteHandler(this));
if (params->downloadCommand.isEmpty()) {
m_ui->messageWidget->showMessage(tr("Download command cannot be empty."), MessageWidget::Warning);
return;
}
QScopedPointer<RemoteHandler> remoteHandler(new RemoteHandler(this));
RemoteHandler::RemoteResult result = remoteHandler->download(params);
if (!result.success) {
m_ui->messageWidget->showMessage(tr("Download failed with error: %1").arg(result.errorMessage),
MessageWidget::Error);
remoteHandler->cleanup(result.filePath);
return;
}
if (!QFile::exists(result.filePath)) {
m_ui->messageWidget->showMessage(tr("Download finished, but file %1 could not be found.").arg(result.filePath),
MessageWidget::Error);
remoteHandler->cleanup(result.filePath);
return;
}
remoteHandler->cleanup(result.filePath);
m_ui->messageWidget->showMessage(tr("Download successful."), MessageWidget::Positive);
}
}

View file

@ -47,7 +47,15 @@ RemoteHandler::RemoteResult RemoteHandler::download(const RemoteParams* params)
return result;
}
auto filePath = getTempFileLocation();
QString error;
auto filePath = getTempFileLocation(&error);
result.filePath = filePath;
if (!error.isEmpty()) {
result.success = false;
result.errorMessage = error;
return result;
}
auto remoteProcess = m_createRemoteProcess(nullptr); // use nullptr parent, otherwise there is a warning
remoteProcess->setTempFileLocation(filePath);
remoteProcess->start(params->downloadCommand);
@ -72,7 +80,6 @@ RemoteHandler::RemoteResult RemoteHandler::download(const RemoteParams* params)
result.errorMessage = tr("Command `%1` failed to download database.").arg(params->downloadCommand);
} else {
result.success = true;
result.filePath = filePath;
}
} else if (finished) {
result.success = false;
@ -135,7 +142,7 @@ RemoteHandler::RemoteResult RemoteHandler::upload(const QString& filePath, const
});
}
QString RemoteHandler::getTempFileLocation()
QString RemoteHandler::getTempFileLocation(QString* error)
{
QString uuid = QUuid::createUuid().toString().remove(0, 1);
uuid.chop(1);
@ -143,12 +150,24 @@ QString RemoteHandler::getTempFileLocation()
if (writableLocation.isEmpty()) {
writableLocation = QStandardPaths::writableLocation(QStandardPaths::TempLocation);
}
QString tempLocation = QDir(writableLocation).absoluteFilePath(PREFIX + uuid);
QDir().mkdir(tempLocation);
QDir uuidPath(tempLocation);
QFile(uuidPath.path()).setPermissions(QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner);
return QDir::toNativeSeparators(uuidPath.absoluteFilePath("RemoteDatabase-" + uuid + ".kdbx"));
QString tempDirLocation = QDir(writableLocation).absoluteFilePath(PREFIX + uuid);
QString tempFileLocation =
QDir::toNativeSeparators(QDir(tempDirLocation).absoluteFilePath("RemoteDatabase-" + uuid + ".kdbx"));
if (!QDir().mkdir(tempDirLocation)) {
*error = tr("Could not create temporary directory '%1'").arg(tempDirLocation);
return "";
}
if (!QFile::setPermissions(tempDirLocation,
QFileDevice::ReadOwner | QFileDevice::WriteOwner | QFileDevice::ExeOwner)) {
QDir(tempDirLocation).removeRecursively();
*error = tr("Could not change permissions of temporary directory '%1' to owner").arg(tempDirLocation);
return "";
}
return tempFileLocation;
}
void RemoteHandler::cleanup(QString& tempFileLocation)

View file

@ -50,7 +50,7 @@ public:
static void setRemoteProcessFunc(std::function<QScopedPointer<RemoteProcess>(QObject*)> func);
private:
static QString getTempFileLocation();
static QString getTempFileLocation(QString* error);
static std::function<QScopedPointer<RemoteProcess>(QObject*)> m_createRemoteProcess;
inline static const QString PREFIX = "KPXC-Sync-";