From 5c05563785f89ed6571c85cec874d94f166e147e Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 13 Nov 2026 22:06:46 +0100 Subject: [PATCH] fix(#147, #411): review round 5 - stable copy ownership per series, best-effort writes, layout-switch cleanup --- .../calendula/data/backup/AutoBackup.kt | 80 ++++++++++------ .../calendula/data/ics/IcsExporter.kt | 12 +-- .../calendula/data/prefs/SettingsPrefs.kt | 39 ++++++-- .../calendula/domain/backup/BackupPlan.kt | 89 ++++++++++-------- .../calendula/data/prefs/SettingsPrefsTest.kt | 8 +- .../calendula/domain/backup/BackupPlanTest.kt | 91 ++++++++++--------- docs/ARCHITECTURE.md | 12 ++- 7 files changed, 204 insertions(+), 127 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 2f114deb..79c3a5a5 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 @@ -28,8 +28,9 @@ import de.jeanlucmakiola.calendula.data.ics.IcsExporter import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.domain.backup.backupTargets import de.jeanlucmakiola.calendula.domain.backup.planBackupFiles -import de.jeanlucmakiola.calendula.domain.backup.updatedBackupLedger -import de.jeanlucmakiola.calendula.domain.backup.updatedLatestLedger +import de.jeanlucmakiola.calendula.domain.backup.latestLedgerKey +import de.jeanlucmakiola.calendula.domain.backup.replacedLatestCopies +import de.jeanlucmakiola.calendula.domain.backup.withLedgerEntry import de.jeanlucmakiola.calendula.domain.ics.IcsWriter import de.jeanlucmakiola.floret.android.hasPermission import kotlinx.coroutines.NonCancellable @@ -160,44 +161,69 @@ class BackupWorker( if (targets.isEmpty()) return RunOutcome.NothingSelected val listing = exporter.listFolder(folder) var ledger = prefs.autoBackupLedger.first() + // A listing still loading can't tell a gone file from a late one. + val existing = if (listing.complete) listing.files.keys else listing.files.keys + ledger.values.flatten() val plans = planBackupFiles( targets = targets, ledger = ledger, - // A listing still loading can't tell a gone file from a late one. - existing = if (listing.complete) listing.files.keys else listing.files.keys + ledger.values.flatten(), + existing = existing, at = LocalDateTime.now(), keep = prefs.autoBackupKeep.first(), + legacyOwned = prefs.autoBackupLegacyInstall.first(), ) val stamp = Clock.System.now() val liveKeys = all.map { it.id.toString() }.toSet() + suspend fun record(key: String, files: List) { + ledger = withLedgerEntry(ledger, key, files, liveKeys) + prefs.setAutoBackupLedger(ledger) + } var written = 0 for (plan in plans) { - try { - val target = plan.target - val events = repository.exportEvents(target.calendarIds) - val content = IcsWriter().writeCalendar(events, stamp, target.calendarName) - // Not cancellable once the file exists: it must reach the - // ledger, or nothing would ever prune it. - withContext(NonCancellable) { - val name = exporter.writeToFolder(folder, listing, plan.fileName, content) - ledger = updatedBackupLedger(ledger, plan, name, undeleted = plan.prune, liveKeys) - prefs.setAutoBackupLedger(ledger) - plan.latestName?.let { latest -> - val latestName = exporter.writeToFolder(folder, listing, latest, content) - ledger = updatedLatestLedger(ledger, plan, latestName, plan.staleLatest, liveKeys) - prefs.setAutoBackupLedger(ledger) - val staleLeft = exporter.deleteFromFolder(listing, plan.staleLatest) - ledger = updatedLatestLedger(ledger, plan, latestName, staleLeft, liveKeys) - } - val undeleted = exporter.deleteFromFolder(listing, plan.prune) - ledger = updatedBackupLedger(ledger, plan, name, undeleted, liveKeys) - prefs.setAutoBackupLedger(ledger) - } - written++ + val target = plan.target + val bytes = try { + IcsWriter() + .writeCalendar(repository.exportEvents(target.calendarIds), stamp, target.calendarName) + .toByteArray(Charsets.UTF_8) } catch (e: CancellationException) { throw e } catch (e: Exception) { - Log.w(TAG, "Backup of one series failed", e) + Log.w(TAG, "Reading one series failed", e) + continue + } + // Not cancellable once a file exists: it must reach the ledger, or + // nothing would ever prune it. + withContext(NonCancellable) { + try { + val name = exporter.writeToFolder(folder, listing, plan.fileName, bytes) + record(target.key, plan.prune + plan.keep + name) + val undeleted = exporter.deleteFromFolder(listing, plan.prune) + record(target.key, plan.prune.filter { it in undeleted } + plan.keep + name) + written++ + } catch (e: Exception) { + Log.w(TAG, "Backup of one series failed", e) + } + // The stable copy is best-effort: it never holds up the dated + // backup, its pruning or the run's status. + try { + val latestKey = latestLedgerKey(target.key) + val latest = plan.latestName?.let { intended -> + val landed = exporter.writeToFolder(folder, listing, intended, bytes) + // A provider that renamed it leaves a copy to clean up next run. + listOf(landed, intended).distinct() + }.orEmpty() + val owned = ledger[latestKey].orEmpty().filter { it !in plan.staleLatest } + record(latestKey, plan.staleLatest + (latest.ifEmpty { owned })) + val staleLeft = exporter.deleteFromFolder(listing, plan.staleLatest) + record(latestKey, plan.staleLatest.filter { it in staleLeft } + latest.ifEmpty { owned }) + } catch (e: Exception) { + Log.w(TAG, "Stable copy of one series failed", e) + } + } + } + replacedLatestCopies(targets, ledger, existing).forEach { (key, names) -> + withContext(NonCancellable) { + val left = exporter.deleteFromFolder(listing, names) + record(latestLedgerKey(key), names.filter { it in left }) } } return when (written) { diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/ics/IcsExporter.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/ics/IcsExporter.kt index 775eb20d..479dcb87 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/ics/IcsExporter.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/ics/IcsExporter.kt @@ -39,15 +39,9 @@ class IcsExporter @Inject constructor( return context.contentResolver.listTreeFiles(folder) } - /** Write [content] as [fileName] into [folder]; returns the name the file ended up with. */ - fun writeToFolder(folder: Uri, listing: TreeListing, fileName: String, content: String): String = - context.contentResolver.writeTreeFile( - tree = folder, - listing = listing, - name = fileName, - mimeType = MIME_CALENDAR, - bytes = content.toByteArray(Charsets.UTF_8), - ) + /** Write the `.ics` [bytes] as [fileName] into [folder]; returns the name the file ended up with. */ + fun writeToFolder(folder: Uri, listing: TreeListing, fileName: String, bytes: ByteArray): String = + context.contentResolver.writeTreeFile(folder, listing, fileName, MIME_CALENDAR, bytes) /** Delete [names] from the listed folder; returns the ones that didn't go. */ fun deleteFromFolder(listing: TreeListing, names: Collection): Set = diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt index 05910def..1fa01e6a 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefs.kt @@ -1,12 +1,16 @@ package de.jeanlucmakiola.calendula.data.prefs import androidx.datastore.core.DataStore +import androidx.datastore.preferences.core.MutablePreferences import androidx.datastore.preferences.core.Preferences import de.jeanlucmakiola.calendula.domain.CalendarSource import de.jeanlucmakiola.calendula.domain.EventFormField import de.jeanlucmakiola.calendula.domain.FontRole import de.jeanlucmakiola.calendula.domain.backup.BackupSelection +import de.jeanlucmakiola.calendula.domain.backup.COMBINED_SERIES import de.jeanlucmakiola.calendula.domain.backup.DEFAULT_BACKUP_KEEP +import de.jeanlucmakiola.calendula.domain.backup.LEGACY_BACKUP_FILE +import de.jeanlucmakiola.calendula.domain.backup.latestLedgerKey import de.jeanlucmakiola.calendula.domain.backup.normalizeBackupKeep import de.jeanlucmakiola.calendula.domain.backup.BackupLedger import de.jeanlucmakiola.calendula.domain.backup.parseBackupLedger @@ -191,6 +195,7 @@ class SettingsPrefs @Inject constructor( val autoBackupSelection: Flow = prefs.snapshot { it.autoBackupSelection } val autoBackupCombined: Flow = prefs.snapshot { it.autoBackupCombined } val autoBackupKeep: Flow = prefs.flow(SettingsKeys.autoBackupKeep) + val autoBackupLegacyInstall: Flow = prefs.snapshot { it.autoBackupLegacyInstall } val autoBackupLedger: Flow = prefs.flow(SettingsKeys.autoBackupLedger) val specialDatesEnabled: Flow = prefs.flow(SettingsKeys.specialDatesEnabled) val specialDatesTypes: Flow> = prefs.snapshot { it.specialDatesTypes } @@ -348,15 +353,17 @@ class SettingsPrefs @Inject constructor( */ suspend fun setAutoBackupFolderUri(uri: String?) { prefs.edit { - if (it[SettingsKeys.autoBackupCombinedStored] == null) { - it[SettingsKeys.autoBackupCombinedStored] = it[SettingsKeys.autoBackupFolderUri] != null - } + it.pinBackupLayout() it[SettingsKeys.autoBackupFolderUri] = uri } } - suspend fun setAutoBackupCombined(combined: Boolean) = - prefs.set(SettingsKeys.autoBackupCombinedStored, combined) + suspend fun setAutoBackupCombined(combined: Boolean) { + prefs.edit { + it.pinBackupLayout() + it[SettingsKeys.autoBackupCombinedStored] = combined + } + } suspend fun setAutoBackupKeep(keep: Int) = prefs.set(SettingsKeys.autoBackupKeep, keep) @@ -822,7 +829,27 @@ val Preferences.autoBackupSelection: BackupSelection * files. Choosing a folder pins the answer (see [SettingsPrefs.setAutoBackupFolderUri]). */ val Preferences.autoBackupCombined: Boolean - get() = this[SettingsKeys.autoBackupCombinedStored] ?: (this[SettingsKeys.autoBackupFolderUri] != null) + get() = this[SettingsKeys.autoBackupCombinedStored] ?: autoBackupLegacyInstall + +/** + * Store the layout the install is on, if it's still derived. Pinning forgets + * it was a pre-#411 install, so its old file is recorded as the combined + * series' own stable copy at the same time. + */ +private fun MutablePreferences.pinBackupLayout() { + if (this[SettingsKeys.autoBackupCombinedStored] != null) return + val legacy = autoBackupLegacyInstall + this[SettingsKeys.autoBackupCombinedStored] = legacy + if (legacy) { + val key = latestLedgerKey(COMBINED_SERIES) + val ledger = this[SettingsKeys.autoBackupLedger] + this[SettingsKeys.autoBackupLedger] = ledger + (key to (ledger[key].orEmpty() + LEGACY_BACKUP_FILE).distinct()) + } +} + +/** An install that set up automatic backup before #411 and hasn't been pinned since. */ +val Preferences.autoBackupLegacyInstall: Boolean + get() = this[SettingsKeys.autoBackupCombinedStored] == null && this[SettingsKeys.autoBackupFolderUri] != null val Preferences.specialDatesTypes: Set get() = SettingsKeys.specialDateTypeEnabled.filterValues { this[it] }.keys 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 81a09e66..d98b9265 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 @@ -96,7 +96,7 @@ const val COMBINED_BACKUP_BASE = "calendula-backup" * The single file every automatic backup overwrote before #411: the combined * series' stable copy now, so an old setup keeps its path. */ -const val LEGACY_BACKUP_FILE = "$COMBINED_BACKUP_BASE.ics" +val LEGACY_BACKUP_FILE: String = latestFileName(COMBINED_BACKUP_BASE) private const val LATEST_PREFIX = "latest-" @@ -156,9 +156,11 @@ fun backupFileName(baseName: String, at: LocalDateTime): String = * Plan one run: a new dated file per target, and which of the series' recorded * files ([ledger]) fall out of the newest [keep], the new one included. Recorded * files no longer in the folder ([existing]) are forgotten. Each target also - * gets its stable copy, which retention never touches; a renamed series drops - * its old one. The pre-#411 [LEGACY_BACKUP_FILE] is the combined series' - * stable copy, so it is overwritten rather than left to go stale. + * gets its stable copy, which retention never touches. A stable name is only + * written when the file is absent or this series owns it — or a series of this + * run that is moving off it (a rename hand-off). [legacyOwned]: this install + * wrote the pre-#411 [LEGACY_BACKUP_FILE], which is the combined series' stable + * copy now. */ fun planBackupFiles( targets: List, @@ -166,21 +168,31 @@ fun planBackupFiles( existing: Set, at: LocalDateTime, keep: Int, + legacyOwned: Boolean = false, ): List { val retain = keep.coerceAtLeast(1) - val ownLatest = ledger.filterKeys { it.startsWith(LATEST_PREFIX) }.values.flatten().toSet() + LEGACY_BACKUP_FILE + val runKeys = targets.map { it.key }.toSet() + val owners = ledger.filterKeys { it.startsWith(LATEST_PREFIX) } + .flatMap { (key, names) -> names.map { it to key.removePrefix(LATEST_PREFIX) } } + .toMap() val currentLatest = targets.map { latestFileName(it.baseName) }.toSet() return targets.map { target -> val fileName = backupFileName(target.baseName, at) - val earlier = ledger[target.key].orEmpty().filter { it in existing && it != fileName }.distinct() - val kept = earlier.takeLast(retain - 1) val latest = latestFileName(target.baseName) + val earlier = ledger[target.key].orEmpty() + .filter { it in existing && it != fileName && it != latest && it != LEGACY_BACKUP_FILE } + .distinct() + val kept = earlier.takeLast(retain - 1) + val owner = owners[latest] + val writable = latest !in existing || + (owner != null && owner in runKeys) || + (latest == LEGACY_BACKUP_FILE && target.key == COMBINED_SERIES && legacyOwned) BackupFilePlan( target = target, fileName = fileName, keep = kept, prune = earlier.dropLast(kept.size), - latestName = latest.takeIf { it !in existing || it in ownLatest }, + latestName = latest.takeIf { writable }, staleLatest = ledger[latestLedgerKey(target.key)].orEmpty() .filter { it in existing && it != latest && it !in currentLatest }, ) @@ -188,45 +200,48 @@ fun planBackupFiles( } /** - * The ledger once [plan]'s new file landed as [writtenName]: the series records - * what it kept, what it failed to delete ([undeleted]) and the new file. Other - * series keep their entry while their calendar still exists ([liveKeys]), so a - * calendar taken out of the backup still has its files pruned if it's put back. + * Stable copies the current layout replaced, per ledger key: a calendar's own + * copy once the combined file holds it, or the combined copy once each calendar + * has its own. Left in place they would keep serving old data under a path + * something may follow. A calendar taken out of the backup keeps its copy. */ -fun updatedBackupLedger( - previous: BackupLedger, - plan: BackupFilePlan, - writtenName: String, - undeleted: Collection, - liveKeys: Set, -): BackupLedger { - val next = previous.livePart(liveKeys) - next[plan.target.key] = (plan.prune.filter { it in undeleted } + plan.keep + writtenName).distinct() - return next.filterValues { it.isNotEmpty() } +fun replacedLatestCopies( + targets: List, + ledger: BackupLedger, + existing: Set, +): Map> { + val runKeys = targets.map { it.key }.toSet() + val covered = targets.flatMap { it.calendarIds }.map { it.toString() }.toSet() + val currentLatest = targets.map { latestFileName(it.baseName) }.toSet() + val combinedRun = COMBINED_SERIES in runKeys + return ledger.filterKeys { it.startsWith(LATEST_PREFIX) } + .mapKeys { it.key.removePrefix(LATEST_PREFIX) } + .filterKeys { key -> + key !in runKeys && (if (key == COMBINED_SERIES) !combinedRun else combinedRun && key in covered) + } + .mapValues { (_, names) -> names.filter { it in existing && it !in currentLatest } } + .filterValues { it.isNotEmpty() } } /** - * The ledger once [plan]'s stable copy landed as [writtenName]: recorded with - * any stale copy that wouldn't delete ([undeleted]), so a later run retries it. + * The ledger with [key]'s files set to [files] (empty drops the entry). Entries + * of deleted calendars go ([liveKeys]); their files stay, unrecorded. Others + * stay, so a calendar taken out of the backup still has its files pruned if it + * is put back. */ -fun updatedLatestLedger( +fun withLedgerEntry( previous: BackupLedger, - plan: BackupFilePlan, - writtenName: String, - undeleted: Collection, + key: String, + files: List, liveKeys: Set, ): BackupLedger { - val next = previous.livePart(liveKeys) - next[latestLedgerKey(plan.target.key)] = (plan.staleLatest.filter { it in undeleted } + writtenName).distinct() - return next.filterValues { it.isNotEmpty() } -} - -/** Entries of calendars that still exist; a deleted calendar's files stay, unrecorded. */ -private fun BackupLedger.livePart(liveKeys: Set): MutableMap> = - filterKeys { key -> - val series = key.removePrefix(LATEST_PREFIX) + val next = previous.filterKeys { entry -> + val series = entry.removePrefix(LATEST_PREFIX) series in liveKeys || series == COMBINED_SERIES }.toMutableMap() + next[key] = files.distinct() + return next.filterValues { it.isNotEmpty() } +} /** `Work.ics`: the series' stable copy. */ fun latestFileName(baseName: String): String = "$baseName$ICS_EXTENSION" diff --git a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt index 4dac5569..5e5a4360 100644 --- a/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt +++ b/app/src/test/java/de/jeanlucmakiola/calendula/data/prefs/SettingsPrefsTest.kt @@ -726,9 +726,15 @@ class SettingsPrefsTest { } assertThat(prefs.autoBackupCombined.first()).isTrue() - // Picking another folder pins the layout instead of flipping it. + assertThat(prefs.autoBackupLegacyInstall.first()).isTrue() + + // Picking another folder pins the layout instead of flipping it, and + // keeps the old file recognised as this install's own. prefs.setAutoBackupFolderUri("content://tree/new") assertThat(prefs.autoBackupCombined.first()).isTrue() + assertThat(prefs.autoBackupLegacyInstall.first()).isFalse() + assertThat(prefs.autoBackupLedger.first()) + .containsExactly("latest-combined", listOf("calendula-backup.ics")) prefs.setAutoBackupCombined(false) assertThat(prefs.autoBackupCombined.first()).isFalse() 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 e18d05fd..7d6135fa 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 @@ -131,10 +131,22 @@ class BackupPlanTest { private val combined = BackupTarget(COMBINED_SERIES, setOf(1L, 2L), COMBINED_BACKUP_BASE, null) @Test - fun `the pre-411 backup file becomes the combined series' stable copy`() { - val plan = planBackupFiles(listOf(combined), emptyMap(), setOf(LEGACY_BACKUP_FILE), at, keep = 1).single() - assertThat(plan.latestName).isEqualTo(LEGACY_BACKUP_FILE) - assertThat(plan.prune).isEmpty() + fun `the pre-411 file is the combined stable copy only for the install that wrote it`() { + val owned = planBackupFiles(listOf(combined), emptyMap(), setOf(LEGACY_BACKUP_FILE), at, 1, legacyOwned = true) + assertThat(owned.single().latestName).isEqualTo(LEGACY_BACKUP_FILE) + assertThat(owned.single().prune).isEmpty() + + val foreign = planBackupFiles(listOf(combined), emptyMap(), setOf(LEGACY_BACKUP_FILE), at, 1) + assertThat(foreign.single().latestName).isNull() + } + + @Test + fun `a stable name recorded as a dated file by an earlier build is never pruned`() { + val ledger = mapOf(COMBINED_SERIES to listOf(LEGACY_BACKUP_FILE, "calendula-backup_a.ics")) + val plan = planBackupFiles( + listOf(combined), ledger, setOf(LEGACY_BACKUP_FILE, "calendula-backup_a.ics"), at, 1, legacyOwned = true, + ).single() + assertThat(plan.prune).containsExactly("calendula-backup_a.ics") } @Test @@ -173,12 +185,10 @@ class BackupPlanTest { ).single() assertThat(plan.latestName).isEqualTo("Job.ics") assertThat(plan.staleLatest).containsExactly("Work.ics") - assertThat(updatedLatestLedger(emptyMap(), plan, "Job.ics", emptySet(), setOf("1"))) - .containsExactly(latestLedgerKey("1"), listOf("Job.ics")) } @Test - fun `a stable copy another calendar took over is not deleted, but rewritten by it`() { + fun `a stable copy is handed over to the calendar that takes its name in the same run`() { // Calendar 1 was "Work", is now "Job"; calendar 2 is now called "Work". val ledger = mapOf(latestLedgerKey("1") to listOf("Work.ics")) val plans = planBackupFiles( @@ -193,46 +203,43 @@ class BackupPlanTest { } @Test - fun `a deleted calendar's ledger entries are dropped, its files left`() { - val plan = planBackupFiles(listOf(work), emptyMap(), emptySet(), at, keep = 1).single() - val next = updatedLatestLedger( - previous = mapOf(latestLedgerKey("7") to listOf("Gone.ics"), latestLedgerKey(COMBINED_SERIES) to listOf(LEGACY_BACKUP_FILE)), - plan = plan, - writtenName = "Work.ics", - undeleted = emptySet(), - liveKeys = setOf("1"), - ) - assertThat(next.keys).containsExactly(latestLedgerKey("1"), latestLedgerKey(COMBINED_SERIES)) - } - - @Test - fun `the ledger records what the run kept, failed to delete and wrote`() { + fun `a deselected calendar's stable copy is not taken over by a same-named one`() { + // Calendar 3 "Personal" is out of the backup; calendar 9, also "Personal", is in. + val ledger = mapOf(latestLedgerKey("3") to listOf("Personal.ics")) val plan = planBackupFiles( - listOf(work), - mapOf("1" to listOf("Work_a.ics", "Work_b.ics")), - setOf("Work_a.ics", "Work_b.ics"), - at, - keep = 2, + listOf(BackupTarget("9", setOf(9L), "Personal", "Personal")), ledger, setOf("Personal.ics"), at, 1, ).single() - val next = updatedBackupLedger( - previous = mapOf("1" to listOf("Work_a.ics", "Work_b.ics"), "2" to listOf("Home_a.ics"), "7" to listOf("Gone.ics")), - plan = plan, - writtenName = "Work_new (1).ics", - undeleted = setOf("Work_a.ics"), - liveKeys = setOf("1", "2"), - ) - assertThat(next).containsExactly( - "1", listOf("Work_a.ics", "Work_b.ics", "Work_new (1).ics"), - "2", listOf("Home_a.ics"), - ) + assertThat(plan.latestName).isNull() } @Test - fun `a pruned file leaves the ledger once it is deleted`() { - val plan = planBackupFiles(listOf(work), mapOf("1" to listOf("Work_a.ics")), setOf("Work_a.ics"), at, keep = 1) - .single() - val next = updatedBackupLedger(mapOf("1" to listOf("Work_a.ics")), plan, "Work_new.ics", emptySet(), setOf("1")) - assertThat(next).containsExactly("1", listOf("Work_new.ics")) + fun `a layout switch replaces the other layout's stable copies, a deselected calendar keeps its own`() { + val ledger = mapOf( + latestLedgerKey("1") to listOf("Work.ics"), + latestLedgerKey("5") to listOf("Old.ics"), + latestLedgerKey(COMBINED_SERIES) to listOf(LEGACY_BACKUP_FILE), + ) + val existing = setOf("Work.ics", "Old.ics", LEGACY_BACKUP_FILE) + assertThat(replacedLatestCopies(listOf(combined), ledger, existing)) + .containsExactly("1", listOf("Work.ics")) + assertThat(replacedLatestCopies(listOf(work), ledger, existing)) + .containsExactly(COMBINED_SERIES, listOf(LEGACY_BACKUP_FILE)) + } + + @Test + fun `a ledger entry is set, emptied away, and dropped with its calendar`() { + val previous = mapOf( + "1" to listOf("Work_a.ics"), + latestLedgerKey("7") to listOf("Gone.ics"), + latestLedgerKey(COMBINED_SERIES) to listOf(LEGACY_BACKUP_FILE), + ) + val next = withLedgerEntry(previous, "1", listOf("Work_a.ics", "Work_b.ics", "Work_b.ics"), setOf("1")) + assertThat(next).containsExactly( + "1", listOf("Work_a.ics", "Work_b.ics"), + latestLedgerKey(COMBINED_SERIES), listOf(LEGACY_BACKUP_FILE), + ) + assertThat(withLedgerEntry(next, "1", emptyList(), setOf("1")).keys) + .containsExactly(latestLedgerKey(COMBINED_SERIES)) } @Test diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 10d331d8..ebb333c8 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -473,13 +473,15 @@ calendar, or one `calendula-backup_…` series in combined mode (#411), and prun each series to the retention count after its new file has landed. Pruning only deletes names recorded in a per-series ledger of files the backup wrote (`auto_backup_files`, keyed by calendar id), never whatever happens to match a -name, so renames, same-named calendars and the user's own files are safe; the +name, so renames, same-named calendars and the user's own files are safe. Each series also keeps a stable undated copy (`.ics`, or `calendula-backup.ics` combined) overwritten every run, so sync tools and -subscriptions can follow one path; retention never touches it, it never -overwrites a file the backup didn't write, and a renamed calendar's old copy is -deleted while a deselected one's is kept. The pre-#411 fixed -`calendula-backup.ics` simply is the combined series' stable copy, and an +subscriptions can follow one path. Retention never touches it, and it is only +written over a file the series itself (or a series renaming away from that name +in the same run) recorded. A renamed calendar's old copy is deleted, and so is a +copy the layout switch replaced (per-calendar ↔ combined); a calendar taken out +of the backup keeps its last one. The pre-#411 fixed `calendula-backup.ics` is +the combined series' stable copy for the install that wrote it, and an install that already had a folder stays on the combined layout. **A backup carries the series' deleted occurrences.** `EXDATE` lives on the