Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Describe funky Android file system behavior again

Carsten Hartenfelse4df97e38192
diff --git a/libs/resources/KisStorageModel.cpp b/libs/resources/KisStorageModel.cpp
index 0e063f6285..293ec7d61e 100644
--- a/libs/resources/KisStorageModel.cpp
+++ b/libs/resources/KisStorageModel.cpp
@@ -471,6 +471,12 @@ bool KisStorageModel::importStorageInternal(const QString &filename,
QSaveFile f(newLocation);
// If the user selects a custom resource location, atomic renames may
// not be available, so a direct write ends up being the only option.
+ // On Android, we don't do the same writing to a temporary file and
+ // then copying it into the destination as in KisImportExportManager
+ // because we already have the file contents in memory, so we're
+ // already writing it as fast and atomically as we can in the face of
+ // a non-atomic external storage file system. The internal file system
+ // supports atomic renames, so there we get proper QSaveFile behavior.
f.setDirectWriteFallback(true);
if (!f.open(QIODevice::WriteOnly) || f.write(data) != data.size() || !f.flush()) {
diff --git a/libs/ui/KisImportExportManager.cpp b/libs/ui/KisImportExportManager.cpp
index 55f8bbaa2a..eab6b307ce 100644
--- a/libs/ui/KisImportExportManager.cpp
+++ b/libs/ui/KisImportExportManager.cpp
@@ -789,6 +789,16 @@ KisImportExportErrorCode KisImportExportManager::doExport(const QString &locatio
// It can work if user gives access to the container dir, but
// we cannot guarantee the user gave us permission.
// 12-05-2025 update: Also Android because we gotta play in the sandbox.
+// External storage does not support atomic renames, so
+// QSaveFile does not work properly and will always fall back
+// to a direct write, which means it would truncate the file
+// as the first step, which leaves a very large time window
+// for Krita to get terminated by Android, leaving the user
+// with an incomplete file. Instead, we write to a temporary
+// file and then copy the contents over. This is still not
+// atomic and can end up with a partial file, but the time
+// window is much shorter and the likelihood of data loss
+// significantly lower because of it.
#if !(defined(Q_OS_WIN) || defined(Q_OS_MACOS) || defined(Q_OS_ANDROID))
#define USE_QSAVEFILE
#endif