From 1f001dc30634ff67271dd1426e47d3bf89814522 Mon Sep 17 00:00:00 2001 From: avi Date: Sun, 4 Oct 2026 10:03:04 -0500 Subject: [PATCH] fix(updater): narrate the handoff to ~/.config/Folio/update.log + safe tree flatten - installer script now logs every step (wait, extract, flatten, swap, relaunch) to a persistent log so a failed update is diagnosable after the fact - flatten the electron-builder nested dir by adopting the dir itself; the old 'mv NEW/* ' glob missed dotfiles and could leave a half-moved tree (prime suspect for the two in-app update failures) - mkdir failure aborts with relaunch of the old app instead of falling through --- src/main/index.ts | 4 ++++ src/main/update.ts | 30 +++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/main/index.ts b/src/main/index.ts index 727e998..4bf7926 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -1254,6 +1254,9 @@ handleIpc("folio:performUpdate", async () => { app.getPath("temp"), `folio-update-${process.pid}.sh` ); + // Persistent, append-only handoff log: survives the swap and tells us + // exactly where a failed update stopped (temp files can vanish). + const updateLog = path.join(app.getPath("userData"), "update.log"); fs.writeFileSync( script, buildInstallerScript({ @@ -1261,6 +1264,7 @@ handleIpc("folio:performUpdate", async () => { tarball, installDir: cfg.installDir, relaunch: cfg.launcher, + log: updateLog, }), { mode: 0o700 } ); diff --git a/src/main/update.ts b/src/main/update.ts index 1382ab8..738245b 100644 --- a/src/main/update.ts +++ b/src/main/update.ts @@ -163,6 +163,8 @@ export interface InstallerSpec { tarball: string; installDir: string; relaunch: string; + /** Optional append-only log; the script narrates every step into it. */ + log?: string; } // The handoff script. Deliberately forgiving (no set -e): on any failure it @@ -170,7 +172,10 @@ export interface InstallerSpec { // user without an app. The old tree is kept as .old. and pruned only // after a successful swap. export function buildInstallerScript(spec: InstallerSpec): string { - const { pid, tarball, installDir, relaunch } = spec; + const { pid, tarball, installDir, relaunch, log } = spec; + const logLine = log + ? `say() { echo "$(date '+%F %T') $*" >> ${shQuote(log)} 2>/dev/null; }` + : "say() { :; }"; return [ "#!/bin/sh", "# Folio updater — waits for the app to exit, swaps in the new version,", @@ -181,29 +186,44 @@ export function buildInstallerScript(spec: InstallerSpec): string { `RELAUNCH=${shQuote(relaunch)}`, `NEW="$INSTALL.update-new.$$"`, `OLD="$INSTALL.old.$$"`, + logLine, + 'say "handoff start pid=$PID tarball=$TARBALL"', "# Wait (bounded to ~2 minutes) for the app process to exit.", "i=0", 'while [ "$PID" -gt 0 ] 2>/dev/null && kill -0 "$PID" 2>/dev/null && [ "$i" -lt 400 ]; do sleep 0.3; i=$((i+1)); done', + 'kill -0 "$PID" 2>/dev/null && say "WARN: app still alive after wait window" || say "app exited after ${i} polls"', 'rm -rf "$NEW"', - 'mkdir -p "$NEW"', + 'mkdir -p "$NEW" || { say "FAIL: mkdir $NEW"; "$RELAUNCH" >/dev/null 2>&1 & exit 1; }', 'if tar -xzf "$TARBALL" -C "$NEW"; then', - ' # electron-builder nests everything under a top-level dir; flatten it.', + ' say "extracted ok"', + ' # electron-builder nests everything under a top-level dir; flatten it', + " # by adopting the nested dir itself (mv of the dir, not a glob — globs", + " # miss dotfiles and a partial mv leaves a broken tree).", ' if [ ! -e "$NEW/folio" ]; then', ' ONLY=$(find "$NEW" -mindepth 1 -maxdepth 1 -type d | head -n 1)', - ' [ -n "$ONLY" ] && mv "$ONLY"/* "$NEW"/ 2>/dev/null', + ' REST=$(find "$NEW" -mindepth 1 -maxdepth 1 | wc -l)', + ' if [ -n "$ONLY" ] && [ "$REST" -eq 1 ]; then', + ' mv "$ONLY" "$NEW.new" && rm -rf "$NEW" && mv "$NEW.new" "$NEW" && say "flattened nested dir"', + " fi", " fi", + "else", + ' say "FAIL: tar extract failed"', "fi", 'if [ -x "$NEW/folio" ]; then', - ' [ -e "$INSTALL" ] && mv "$INSTALL" "$OLD"', + ' [ -e "$INSTALL" ] && mv "$INSTALL" "$OLD" && say "moved old install aside"', ' if mv "$NEW" "$INSTALL"; then', + ' say "SWAP OK — new version installed"', ' rm -rf "$OLD" "$INSTALL.update-new".* "$INSTALL.old".* 2>/dev/null', ' rm -f "$TARBALL"', " else", + ' say "FAIL: mv NEW->INSTALL; restoring old"', ' [ -e "$OLD" ] && mv "$OLD" "$INSTALL"', " fi", "else", + ' say "FAIL: no executable at $NEW/folio; keeping old install"', ' rm -rf "$NEW"', "fi", + 'say "relaunching"', '"$RELAUNCH" >/dev/null 2>&1 &', "", ].join("\n");