Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile): rename a finished save over its predecessor, not after it

Elian Doran6c949b2f562a
diff --git a/apps/standalone/src/services/capacitor_download.spec.ts b/apps/standalone/src/services/capacitor_download.spec.ts
index 5f04534449..0d309f8cd4 100644
--- a/apps/standalone/src/services/capacitor_download.spec.ts
+++ b/apps/standalone/src/services/capacitor_download.spec.ts
@@ -22,7 +22,12 @@ const RUN = "[a-z0-9]+-[a-z0-9]+";
* Models the bridge the module reaches through: neither Filesystem nor Share is registered by the
* injected runtime, so both arrive via `registerPlugin()` rather than from `Plugins`.
*/
-function installCapacitor({ share, runs }: { share?: ReturnType<typeof vi.fn>; runs?: string[] } = {}) {
+function installCapacitor({ share, runs, renameRefusesReplace }: {
+ share?: ReturnType<typeof vi.fn>;
+ runs?: string[];
+ /** Fail a rename whose destination has not been deleted, as a platform that cannot replace. */
+ renameRefusesReplace?: boolean;
+} = {}) {
const written: { path: string; data: string; directory: string; append: boolean }[] = [];
const removed: string[] = [];
const deleted: string[] = [];
@@ -44,6 +49,9 @@ function installCapacitor({ share, runs }: { share?: ReturnType<typeof vi.fn>; r
deleted.push(opts.path);
}),
rename: vi.fn(async (opts: { from: string; to: string }) => {
+ if (renameRefusesReplace && !deleted.includes(opts.to)) {
+ throw new Error("Destination file already exists");
+ }
renamed.push({ from: opts.from, to: opts.to });
}),
readdir: vi.fn(async () => {
@@ -350,14 +358,14 @@ describe("capacitor download", () => {
"db.tnbackup", inPieces(Uint8Array.from([1, 2, 3]), 3), BACKUP_TARGET
);
- // Backups keep their folder: nothing is pruned, and even the same-name predecessor
- // stands until its replacement is complete on disk.
+ // Backups keep their folder: nothing is pruned, and the same-name predecessor is
+ // replaced in one rename — at no moment is neither file on disk.
expect(removed).toEqual([]);
expect(filesystem.readdir).not.toHaveBeenCalled();
expect(written).toEqual([
expect.objectContaining({ path: "Trilium/db.tnbackup.part", directory: "DOCUMENTS" })
]);
- expect(deleted).toEqual(["Trilium/db.tnbackup"]);
+ expect(deleted).toEqual([]);
expect(renamed).toEqual([{ from: "Trilium/db.tnbackup.part", to: "Trilium/db.tnbackup" }]);
expect(result.location).toBe("/cache/Trilium/db.tnbackup");
expect(sharePlugin.share).toHaveBeenCalledWith({
@@ -366,6 +374,21 @@ describe("capacitor download", () => {
});
});
+ it("gives way before the rename only where the platform refuses to replace", async () => {
+ const { filesystem, deleted, renamed } = installCapacitor({ renameRefusesReplace: true });
+
+ const result = await saveChunksToDevice(
+ "db.tnbackup", inPieces(Uint8Array.from([1, 2, 3]), 3), BACKUP_TARGET
+ );
+
+ expect(result.status).toBe("saved");
+ // The old file falls only after the replacement refused to land over it, which keeps
+ // the gap to platforms whose rename cannot replace in place.
+ expect(filesystem.rename).toHaveBeenCalledTimes(2);
+ expect(deleted).toEqual(["Trilium/db.tnbackup"]);
+ expect(renamed).toEqual([{ from: "Trilium/db.tnbackup.part", to: "Trilium/db.tnbackup" }]);
+ });
+
it("streams raw bytes through the native sink when the shell provides one", async () => {
const { filesystem, written, sharePlugin } = installCapacitor();
const { posted, listeners } = installFileSink();
diff --git a/apps/standalone/src/services/capacitor_download.ts b/apps/standalone/src/services/capacitor_download.ts
index dd447f9547..1009aabc93 100644
--- a/apps/standalone/src/services/capacitor_download.ts
+++ b/apps/standalone/src/services/capacitor_download.ts
@@ -184,9 +184,15 @@ async function writeChunks(
throw e;
}
- // Only now, with its replacement complete on disk, does the previous file fall.
- await filesystem.deleteFile({ path, directory }).catch(() => undefined);
- await filesystem.rename({ from: partPath, to: path, directory });
+ // The previous file is replaced only now, when its successor is whole. Rename first: where
+ // the platform replaces in place, as POSIX rename does, no moment has neither file on disk.
+ // Only a platform that refuses to replace gets the delete-then-retry window.
+ try {
+ await filesystem.rename({ from: partPath, to: path, directory });
+ } catch {
+ await filesystem.deleteFile({ path, directory }).catch(() => undefined);
+ await filesystem.rename({ from: partPath, to: path, directory });
+ }
const { uri } = await filesystem.getUri({ path, directory });
return uri;