From 830e48c5e7898bfa9234dbf56be0e287cdd8e7c8 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Wed, 25 Nov 2026 15:31:55 +0100 Subject: [PATCH] fix(#516): review round 4 - id suffix sticks, run-now rewrites every copy again, unchanged blocked series don't count as success --- .../calendula/data/backup/AutoBackup.kt | 26 +++++++++---------- .../calendula/domain/backup/BackupPlan.kt | 6 +++-- .../calendula/domain/backup/BackupPlanTest.kt | 8 ++++++ docs/ARCHITECTURE.md | 12 ++++----- docs/SETTINGS_BACKUP.md | 2 +- 5 files changed, 31 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt index 14835d9e..2852e9b7 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/backup/AutoBackup.kt @@ -279,7 +279,7 @@ class BackupWorker( * schedule and rewrites only the stable copies whose calendars changed. * With the interval off, no run writes dated files, so a series whose * stable name a file of the user's holds counts as failed (a change run - * only when its content changed), and run-now skips current copies too. + * only when its content changed). */ private suspend fun runBackup( deps: Deps, @@ -325,10 +325,12 @@ class BackupWorker( } else { emptyMap() } - // Unless a stable copy went missing: with the interval off, nothing else would restore it. + // With the interval off, unless a stable copy went missing: no scheduled run would restore it. + fun copyListed(target: BackupTarget) = latestFileName(target.baseName) in listing.files || + ledger[latestLedgerKey(target.key)].orEmpty().any { it in listing.files } if (changeRun && targets.all { preHashes[it.key].let { hash -> hash != null && hash == fingerprints[it.key] } } && - (!listing.complete || targets.all { latestFileName(it.baseName) in listing.files }) + (intervalOn || !listing.complete || targets.all(::copyListed)) ) { return RunOutcome.NothingToDo } @@ -404,6 +406,7 @@ class BackupWorker( var written = 0 var failed = 0 var blocked = 0 + var stillBlocked = 0 val landedLatest = mutableSetOf() for (plan in plans) { val target = plan.target @@ -419,8 +422,11 @@ class BackupWorker( // Its hash still goes on record, so a later change run can stop early. val blockedHash = hash ?: if (trackHashes) export(target)?.let { fingerprintOf(target, it) } else null blockedHash?.let { rememberBlocked(plan, it) } - // Reported once per content: a change run doesn't fail again on one that didn't change. - if (changeRun && hash == fingerprints[target.key]) continue + // A change run doesn't fail again on one that didn't change, nor report the run a success. + if (changeRun && hash == fingerprints[target.key]) { + stillBlocked++ + continue + } failed++ blocked++ continue @@ -437,14 +443,6 @@ class BackupWorker( } val fingerprint = fingerprintOf(target, text) if (!changeRun) rememberBlocked(plan, fingerprint) - // Without the interval, run-now leaves a stable copy that is already current alone. - if (!changeRun && latestOnly && trackHashes && plan.latestName in listing.files && - fingerprints[target.key] == fingerprint - ) { - landedLatest += target.key - written++ - continue - } val bytes = text.toByteArray(Charsets.UTF_8) // Not cancellable once a file exists: it must reach the ledger. withContext(NonCancellable) { @@ -468,7 +466,7 @@ class BackupWorker( } } return when { - failed == 0 && written == 0 -> RunOutcome.NothingToDo + failed == 0 && (written == 0 || stillBlocked > 0) -> RunOutcome.NothingToDo failed == 0 -> RunOutcome.Written written == 0 && failed == blocked -> RunOutcome.Blocked written == 0 -> RunOutcome.Failed diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt b/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt index cbb4fb90..dbed2c78 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlan.kt @@ -129,7 +129,7 @@ private val STAMP_FORMAT: DateTimeFormatter = DateTimeFormatter.ofPattern(STAMP_ * something follows shouldn't move because another calendar took the same * name. So the series holding the stable copy of the name in [ledger] keeps * it, then one holding dated files under it; series that already write under - * their id suffix keep that; with no claim at all the lowest id keeps the + * their id suffix keep that, clash or not; with no claim at all the lowest id keeps the * name. The others take their id as a suffix until every name in the run is * distinct. A stable copy a series outside the run still holds (a calendar * taken out of the backup) is kept, so a calendar now named like it is @@ -150,7 +150,9 @@ fun backupTargets( } val present = if (existing == null) ledger else ledger.mapValues { (_, names) -> names.filter { it in existing } } val bases = calendars.associateTo(LinkedHashMap()) { - it.id to safeFileName(it.displayName, MAX_BASE_BYTES, fallback = "calendar") + val base = safeFileName(it.displayName, MAX_BASE_BYTES, fallback = "calendar") + val suffixed = "$base-${it.id}" + it.id to if (present.holdsLatest(it.id.toString(), suffixed)) suffixed else base } val runKeys = calendars.map { latestLedgerKey(it.id.toString()) }.toSet() val heldOutside = present.filterKeys { it.startsWith(LATEST_PREFIX) && it !in runKeys } diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt index 7817b709..1cf4b383 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/domain/backup/BackupPlanTest.kt @@ -119,6 +119,14 @@ class BackupPlanTest { assertThat(gone.baseName).isEqualTo("Fitness") } + @Test + fun `a series writing under its id suffix keeps it once the clash is gone`() { + val ledger = mapOf(latestLedgerKey("20") to listOf("Fitness-20.ics")) + val target = backupTargets(listOf(cal(20L, "Fitness")), combined = false, ledger, setOf("Fitness.ics", "Fitness-20.ics")) + .single() + assertThat(target.baseName).isEqualTo("Fitness-20") + } + @Test fun `a calendar named like the combined file doesn't share its series`() { val target = backupTargets(listOf(cal(4L, COMBINED_BACKUP_BASE)), combined = false).single() diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 628e41ad..7349f0ad 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -645,12 +645,12 @@ the app, is left to the next scheduled run. While the option is on, the interval itself can be switched off (`auto_backup_scheduled`, read through `backupIntervalOn`, so it never counts as off without the option): the periodic work is cancelled and no run writes or -prunes dated files. Every run then rewrites only the stable copies that changed -or are missing from a complete listing (a change run checks for missing ones -even when no hash moved, since no scheduled run would restore them), and a -series whose stable name a file of the user's holds counts as failed (not -retried), since nothing else backs it up; a change run reports it again only -once its content changed. Turning the option on or off sets the interval back +prunes dated files. Run-now then rewrites every stable copy, change runs the +changed ones and any missing from a complete listing (checked even when no hash +moved, since no scheduled run would restore them), and a series whose stable +name a file of the user's holds counts as failed (not retried), since nothing +else backs it up. A change run fails on it again only once its content changed, +and while it stays unchanged doesn't record the run as a success either. Turning the option on or off sets the interval back on, at the interval it had; any switch of the interval backs up right away. What the text above leaves to the next scheduled run then waits for the next run-now. diff --git a/docs/SETTINGS_BACKUP.md b/docs/SETTINGS_BACKUP.md index 4e0c9bf0..367a1cda 100644 --- a/docs/SETTINGS_BACKUP.md +++ b/docs/SETTINGS_BACKUP.md @@ -58,7 +58,7 @@ e.g. `"theme_mode": "DARK"`, `"multi_day_count": 4`, `"default_reminder_minutes": "10,60"`. Only keys that have been set are written. `SettingsBackupSchema.plain` lists them: appearance, fonts, views, the agenda screen, the event form and its default calendar, reminders -and the daily summary, automatic backup (switch, interval, layout, retention, +and the daily summary, automatic backup (switch, interval and whether it runs, layout, retention, backup on change) and contact special dates (switch, types, title templates, year).