Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
core/backup import: fix a bug with the removal of temporary files
Adorian Doranf0cd5eec668d
diff --git a/apps/server/src/services/database_restore.spec.ts b/apps/server/src/services/database_restore.spec.tsindex af43d23274..0ae2344041 100644--- a/apps/server/src/services/database_restore.spec.ts+++ b/apps/server/src/services/database_restore.spec.ts@@ -1,5 +1,5 @@import { writeBackupContainer } from "@triliumnext/backup-container";-import { app_info as appInfo, getSql } from "@triliumnext/core";+import { app_info as appInfo, getLog, getSql } from "@triliumnext/core";import Database from "better-sqlite3";import fs from "fs";import fsp from "fs/promises";@@ -11,6 +11,7 @@ import {exchangeDatabaseFiles,getRestoreProgress,readBackupFormat,+ removeQuietly,reportRestoreFailure,restoreDatabase,RestoreFailure,@@ -167,6 +168,39 @@ describe("staging a backup", () => {});});+describe("removing what is no longer wanted", () => {+ it("removes a file, and a directory when it is told to", () => {+ const file = fileWith("temporary");+ const directory = path.join(tempRoot, "staging");+ fs.mkdirSync(directory, { recursive: true });+ fs.writeFileSync(path.join(directory, "candidate.db"), "half a database");++ removeQuietly(file);+ removeQuietly(directory, { recursive: true });++ expect(fs.existsSync(file)).toBe(false);+ expect(fs.existsSync(directory)).toBe(false);+ });++ it("says nothing about something that was never there", () => {+ const errors = vi.spyOn(getLog(), "error");++ removeQuietly(path.join(tempRoot, "never-existed"));++ expect(errors).not.toHaveBeenCalled();+ });++ it("logs and carries on when the file will not go, rather than raising", () => {+ const errors = vi.spyOn(getLog(), "error").mockImplementation(() => {});+ vi.spyOn(fs, "rmSync").mockImplementation(() => {+ throw new Error("EPERM, Permission denied");+ });++ expect(() => removeQuietly(fileWith("locked"))).not.toThrow();+ expect(errors).toHaveBeenCalledWith(expect.stringContaining("EPERM"));+ });+});+describe("exchanging the database files", () => {it("puts the candidate in place, keeps the old database aside, and drops its sidecars", () => {const document = path.join(tempRoot, "document.db");diff --git a/apps/server/src/services/database_restore.ts b/apps/server/src/services/database_restore.tsindex 634c5343b6..e2e349c25d 100644--- a/apps/server/src/services/database_restore.ts+++ b/apps/server/src/services/database_restore.ts@@ -389,7 +389,10 @@ function report(stage: RestoreStage): void {*/export function removeQuietly(target: string, options: { recursive?: boolean } = {}): void {try {- fs.rmSync(target, { force: true, recursive: options.recursive });+ // `recursive` is spelled out rather than passed through: `fs.rmSync` merges the options over+ // its defaults, so a property that is present and undefined replaces the default rather than+ // leaving it alone, and every call that omitted it failed its own type check.+ fs.rmSync(target, { force: true, recursive: options.recursive === true });} catch (e) {getLog().error(`Could not remove a temporary file after a restore: ${messageOf(e)}`);}