From 46818cd0ae52c7e2245adf9398a0913929e59611 Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 13 Nov 2026 16:38:06 +0100 Subject: [PATCH] fix(#147, #411): review round 2 - prune from a ledger of written files, one read per run --- .../calendula/data/backup/AutoBackup.kt | 55 ++++--- .../data/calendar/CalendarDataSource.kt | 29 +--- .../data/calendar/ReminderQueries.kt | 36 +++++ .../calendula/data/ics/IcsExporter.kt | 22 ++- .../calendula/data/prefs/SettingsPrefs.kt | 16 +- .../data/reminders/ReminderInstanceSource.kt | 32 +--- .../calendula/domain/backup/BackupPlan.kt | 136 ++++++++-------- .../calendula/domain/ics/IcsEvent.kt | 2 + .../calendula/ui/calendars/BackupScreen.kt | 13 +- .../calendula/ui/calendars/BackupUiState.kt | 9 +- .../ui/onboarding/OnboardingViewModel.kt | 3 +- app/src/main/res/values/strings.xml | 1 + .../calendula/domain/backup/BackupPlanTest.kt | 152 ++++++++++-------- docs/ARCHITECTURE.md | 6 +- 14 files changed, 268 insertions(+), 244 deletions(-) create mode 100644 app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/ReminderQueries.kt 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 3fa0ebbc..ef16e031 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 @@ -26,10 +26,10 @@ import de.jeanlucmakiola.calendula.R import de.jeanlucmakiola.calendula.data.calendar.CalendarRepository import de.jeanlucmakiola.calendula.data.ics.IcsExporter import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs -import de.jeanlucmakiola.calendula.domain.backup.BackupFilePlan +import de.jeanlucmakiola.calendula.domain.backup.COMBINED_SERIES import de.jeanlucmakiola.calendula.domain.backup.backupTargets import de.jeanlucmakiola.calendula.domain.backup.planBackupFiles -import de.jeanlucmakiola.calendula.domain.backup.updatedBackupBases +import de.jeanlucmakiola.calendula.domain.backup.updatedBackupLedger import de.jeanlucmakiola.calendula.domain.ics.IcsWriter import de.jeanlucmakiola.floret.android.hasPermission import kotlinx.coroutines.flow.first @@ -136,57 +136,62 @@ class BackupWorker( if (!success && prefs.autoBackupStatus.first().consecutiveFailures >= FAILURE_NOTIFY_THRESHOLD) { notifyFailure(applicationContext) } - // A retry rewrites every series, so only a run that wrote nothing is - // retried; a partial one waits for the next interval rather than - // rotating the healthy calendars' history out with near-identical copies. + // Only a run that wrote nothing retries: a retry rewrites every series. return if (outcome == RunOutcome.Failed) Result.retry() else Result.success() } private enum class RunOutcome { Written, PartlyFailed, Failed, NothingSelected } - /** - * Writes each series and prunes it only once its new file has landed, so - * a failed write never costs an older backup. - */ + /** Writes each series and prunes it only once its new file has landed. */ private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri): RunOutcome { val repository = deps.repository() val exporter = deps.exporter() + // Without the calendar permission the list comes back empty. val all = repository.calendars().first() - // Without the calendar permission the list comes back empty: a failure, - // not an empty selection. if (all.isEmpty()) return RunOutcome.Failed val selection = prefs.autoBackupSelection.first() - val previousBases = prefs.autoBackupSeriesNames.first() - val targets = backupTargets( - calendars = all.filter(selection::selects), - combined = prefs.autoBackupCombined.first(), - previousBases = previousBases, - ) + val targets = backupTargets(all.filter(selection::selects), combined = prefs.autoBackupCombined.first()) if (targets.isEmpty()) return RunOutcome.NothingSelected + val ledger = prefs.autoBackupLedger.first() val listing = exporter.listFolder(folder) val plans = planBackupFiles( targets = targets, + ledger = ledger, existing = listing.keys, at = LocalDateTime.now(), keep = prefs.autoBackupKeep.first(), ) + val events = repository.exportEvents(targets.flatMap { it.calendarIds }.toSet()) + val byCalendar = events.groupBy { it.calendarId } val stamp = Clock.System.now() - val written = mutableListOf() + val writtenNames = mutableMapOf() + val undeleted = mutableSetOf() for (plan in plans) { try { - val events = repository.exportEvents(plan.target.calendarIds) - val content = IcsWriter().writeCalendar(events, stamp, plan.target.calendarName) - exporter.writeToFolder(folder, listing, plan.fileName, content) - written += plan - exporter.deleteDocuments(plan.prune.mapNotNull(listing::get)) + val target = plan.target + val content = IcsWriter().writeCalendar( + events = if (target.key == COMBINED_SERIES) events else byCalendar[target.calendarIds.single()].orEmpty(), + dtStamp = stamp, + calendarName = target.calendarName, + ) + writtenNames[target.key] = exporter.writeToFolder(folder, listing, plan.fileName, content) + undeleted += exporter.deleteFromFolder(listing, plan.prune) } catch (e: CancellationException) { throw e } catch (e: Exception) { Log.w(TAG, "Backup of one series failed", e) } } - prefs.setAutoBackupSeriesNames(updatedBackupBases(previousBases, written)) - return when (written.size) { + prefs.setAutoBackupLedger( + updatedBackupLedger( + previous = ledger, + written = plans, + writtenNames = writtenNames, + undeleted = undeleted, + liveKeys = all.map { it.id.toString() }.toSet(), + ), + ) + return when (writtenNames.size) { plans.size -> RunOutcome.Written 0 -> RunOutcome.Failed else -> RunOutcome.PartlyFailed diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt index 9a73e571..1d4f07f9 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/CalendarDataSource.kt @@ -789,8 +789,7 @@ class AndroidCalendarDataSource @Inject constructor( if (names.isEmpty()) return emptyList() val idList = names.keys.joinToString(",") - // Skip soft-deleted rows and exception rows (modified occurrences / - // cancellations) — v1 exports masters + one-offs only. + // Masters + one-offs only: no soft-deleted or exception rows. val selection = "${CalendarContract.Events.CALENDAR_ID} IN ($idList) AND " + "${CalendarContract.Events.DELETED} = 0 AND " + "${CalendarContract.Events.ORIGINAL_ID} IS NULL" @@ -801,7 +800,7 @@ class AndroidCalendarDataSource @Inject constructor( null, null, )?.use { c -> c.mapRows { getLong(0) } }.orEmpty() - val reminders = reminderMinutesByEvent(eventIds) + val reminders = resolver.reminderMinutesByEvent(eventIds, alertsOnly = false) return resolver.query( CalendarContract.Events.CONTENT_URI, EventExportProjection.COLUMNS, @@ -816,30 +815,11 @@ class AndroidCalendarDataSource @Inject constructor( reminderMinutes = reminders[eventId].orEmpty(), calendarName = names[calendarId], allDayReminderTimeMinutes = allDayReminderTimeMinutes, - ) + ).copy(calendarId = calendarId) } } ?: emptyList() } - /** - * Reminder minutes of [eventIds], a chunked query at a time rather than one - * per event — a synced calendar can hold thousands (#147). - */ - private fun reminderMinutesByEvent(eventIds: List): Map> { - val byEvent = mutableMapOf>() - eventIds.chunked(REMINDER_QUERY_CHUNK).forEach { chunk -> - resolver.query( - CalendarContract.Reminders.CONTENT_URI, - arrayOf(CalendarContract.Reminders.EVENT_ID, CalendarContract.Reminders.MINUTES), - "${CalendarContract.Reminders.EVENT_ID} IN (${chunk.joinToString(",")})", - null, - null, - )?.use { c -> - while (c.moveToNext()) byEvent.getOrPut(c.getLong(0)) { mutableListOf() } += c.getInt(1) - } - } - return byEvent - } override fun existingUids(calendarId: Long): Set = resolver.query( CalendarContract.Events.CONTENT_URI, @@ -1775,9 +1755,6 @@ class AndroidCalendarDataSource @Inject constructor( private companion object { const val TAG = "CalendarDataSource" - /** Ids per `IN (…)` list, well under SQLite's bound-variable and statement limits. */ - const val REMINDER_QUERY_CHUNK = 500 - /** Normalised key an attendee is matched/deduped on (case-insensitive email). */ fun emailKey(email: String): String = email.trim().lowercase() diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/ReminderQueries.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/ReminderQueries.kt new file mode 100644 index 00000000..733167c2 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/ReminderQueries.kt @@ -0,0 +1,36 @@ +package de.jeanlucmakiola.calendula.data.calendar + +import android.content.ContentResolver +import android.provider.CalendarContract + +/** Ids per `IN (…)` list: they go into the SQL literally, which SQLite bounds. */ +private const val EVENT_ID_BATCH = 50 + +/** + * Reminder minutes of [eventIds], keyed by event, a batched query at a time + * rather than one per event. [alertsOnly] keeps just the notification ones. + */ +internal fun ContentResolver.reminderMinutesByEvent( + eventIds: Collection, + alertsOnly: Boolean, +): Map> { + if (eventIds.isEmpty()) return emptyMap() + val out = mutableMapOf>() + val method = if (alertsOnly) { + "${CalendarContract.Reminders.METHOD} = ${CalendarContract.Reminders.METHOD_ALERT} AND " + } else { + "" + } + eventIds.distinct().chunked(EVENT_ID_BATCH).forEach { batch -> + query( + CalendarContract.Reminders.CONTENT_URI, + arrayOf(CalendarContract.Reminders.EVENT_ID, CalendarContract.Reminders.MINUTES), + "$method${CalendarContract.Reminders.EVENT_ID} IN (${batch.joinToString(",")})", + null, + null, + )?.use { c -> + while (c.moveToNext()) out.getOrPut(c.getLong(0)) { mutableListOf() } += c.getInt(1) + } + } + return out +} 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 c619d6d2..be76fdc9 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 @@ -60,22 +60,30 @@ class IcsExporter @Inject constructor( /** * Write [content] to [fileName] in [folder], overwriting the file of that - * name if [listing] (from [listFolder]) has one. Throws on failure. + * name if [listing] (from [listFolder]) has one. Returns the name the file + * ended up with — a provider may de-duplicate it. Throws on failure. */ - fun writeToFolder(folder: Uri, listing: Map, fileName: String, content: String) { - val uri = listing[fileName] ?: DocumentsContract.createDocument( + fun writeToFolder(folder: Uri, listing: Map, fileName: String, content: String): String { + listing[fileName]?.let { uri -> + writeDocument(uri, content) + return fileName + } + val uri = DocumentsContract.createDocument( context.contentResolver, DocumentsContract.buildDocumentUriUsingTree(folder, DocumentsContract.getTreeDocumentId(folder)), MIME_CALENDAR, fileName, ) ?: throw IOException("Could not create backup file in the chosen folder") writeDocument(uri, content) + return DocumentFile.fromSingleUri(context, uri)?.name ?: fileName } - /** Delete [documents]; one that won't go is left behind. */ - fun deleteDocuments(documents: Collection) { - documents.forEach { runCatching { DocumentsContract.deleteDocument(context.contentResolver, it) } } - } + /** Delete the [names] files of [listing]; returns the ones that wouldn't go. */ + fun deleteFromFolder(listing: Map, names: Collection): Set = + names.filterTo(mutableSetOf()) { name -> + val uri = listing[name] ?: return@filterTo false + !runCatching { DocumentsContract.deleteDocument(context.contentResolver, uri) }.getOrDefault(false) + } private fun writableFolder(folder: Uri): DocumentFile { val dir = DocumentFile.fromTreeUri(context, folder) 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 743941af..05910def 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 @@ -8,8 +8,9 @@ import de.jeanlucmakiola.calendula.domain.FontRole import de.jeanlucmakiola.calendula.domain.backup.BackupSelection import de.jeanlucmakiola.calendula.domain.backup.DEFAULT_BACKUP_KEEP import de.jeanlucmakiola.calendula.domain.backup.normalizeBackupKeep -import de.jeanlucmakiola.calendula.domain.backup.parseBackupBases -import de.jeanlucmakiola.calendula.domain.backup.persistBackupBases +import de.jeanlucmakiola.calendula.domain.backup.BackupLedger +import de.jeanlucmakiola.calendula.domain.backup.parseBackupLedger +import de.jeanlucmakiola.calendula.domain.backup.persistBackupLedger import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType import de.jeanlucmakiola.calendula.ui.agenda.AgendaRange import de.jeanlucmakiola.calendula.ui.agenda.parseAgendaRange @@ -190,7 +191,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 autoBackupSeriesNames: Flow>> = prefs.flow(SettingsKeys.autoBackupSeriesNames) + val autoBackupLedger: Flow = prefs.flow(SettingsKeys.autoBackupLedger) val specialDatesEnabled: Flow = prefs.flow(SettingsKeys.specialDatesEnabled) val specialDatesTypes: Flow> = prefs.snapshot { it.specialDatesTypes } val specialDatesCalendars: Flow> = prefs.snapshot { it.specialDatesCalendars } @@ -359,8 +360,7 @@ class SettingsPrefs @Inject constructor( suspend fun setAutoBackupKeep(keep: Int) = prefs.set(SettingsKeys.autoBackupKeep, keep) - suspend fun setAutoBackupSeriesNames(names: Map>) = - prefs.set(SettingsKeys.autoBackupSeriesNames, names) + suspend fun setAutoBackupLedger(ledger: BackupLedger) = prefs.set(SettingsKeys.autoBackupLedger, ledger) /** Switch [calendar] in or out of the automatic backup (#147). */ suspend fun setAutoBackupCalendar(calendar: CalendarSource, included: Boolean) { @@ -757,9 +757,9 @@ object SettingsKeys { val autoBackupKeep: Pref = intPref("auto_backup_keep", DEFAULT_BACKUP_KEEP).coerced(::normalizeBackupKeep) - /** Each calendar's backup series name from its last run, so a rename still prunes the old files. */ - val autoBackupSeriesNames: Pref>> = - nullableStringPref("auto_backup_series").map(::parseBackupBases, ::persistBackupBases) + /** The files the automatic backup wrote, per series; the only ones it ever prunes (#411). */ + val autoBackupLedger: Pref = + nullableStringPref("auto_backup_files").map(::parseBackupLedger, ::persistBackupLedger) /** Master switch for the contact special-dates mirror (issue #15). Opt-in. */ val specialDatesEnabled: Pref = booleanPref("special_dates_enabled", false) diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderInstanceSource.kt b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderInstanceSource.kt index 29b140dc..71f7549d 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderInstanceSource.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/reminders/ReminderInstanceSource.kt @@ -4,6 +4,7 @@ import android.content.Context import android.provider.CalendarContract import dagger.hilt.android.qualifiers.ApplicationContext import de.jeanlucmakiola.calendula.data.calendar.instancesUri +import de.jeanlucmakiola.calendula.data.calendar.reminderMinutesByEvent import de.jeanlucmakiola.calendula.domain.reminders.ReminderEventInstance import javax.inject.Inject import javax.inject.Singleton @@ -72,28 +73,8 @@ class ProviderReminderInstanceSource @Inject constructor( } ?: emptyList() } - override fun reminderMinutes(eventIds: Collection): Map> { - if (eventIds.isEmpty()) return emptyMap() - val out = mutableMapOf>() - // Batched: the ids go into the selection literally, and an unbounded - // `IN (...)` would grow the SQL past what SQLite takes. - eventIds.distinct().chunked(EVENT_ID_BATCH).forEach { batch -> - context.contentResolver.query( - CalendarContract.Reminders.CONTENT_URI, - REMINDER_PROJECTION, - "${CalendarContract.Reminders.METHOD} = " + - "${CalendarContract.Reminders.METHOD_ALERT} AND " + - "${CalendarContract.Reminders.EVENT_ID} IN (${batch.joinToString(",")})", - null, - null, - )?.use { c -> - while (c.moveToNext()) { - out.getOrPut(c.getLong(0)) { mutableListOf() } += c.getInt(1) - } - } - } - return out - } + override fun reminderMinutes(eventIds: Collection): Map> = + context.contentResolver.reminderMinutesByEvent(eventIds, alertsOnly = true) override fun longestReminderMinutes(): Int = context.contentResolver.query( CalendarContract.Reminders.CONTENT_URI, @@ -105,8 +86,6 @@ class ProviderReminderInstanceSource @Inject constructor( )?.use { c -> if (c.moveToFirst()) c.getInt(0) else 0 } ?: 0 private companion object { - const val EVENT_ID_BATCH = 50 - val OCCURRENCE_PROJECTION = arrayOf( CalendarContract.Instances.EVENT_ID, CalendarContract.Instances.CALENDAR_ID, @@ -116,10 +95,5 @@ class ProviderReminderInstanceSource @Inject constructor( CalendarContract.Instances.EVENT_LOCATION, CalendarContract.Instances.ALL_DAY, ) - - val REMINDER_PROJECTION = arrayOf( - CalendarContract.Reminders.EVENT_ID, - CalendarContract.Reminders.MINUTES, - ) } } 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 54d25754..edf3e8ff 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 @@ -51,24 +51,34 @@ data class BackupSelection( /** One backup file series: the calendars it holds and the name its files start with. */ data class BackupTarget( + /** Stable identity of the series in the [BackupLedger]: the calendar id, or [COMBINED_SERIES]. */ + val key: String, val calendarIds: Set, val baseName: String, /** The calendar name written into the file, or null for a combined file. */ val calendarName: String?, - /** Names this calendar's series went by before a rename; their files are pruned with it. */ - val formerBaseNames: Set = emptySet(), ) /** What one run writes and which earlier files it removes afterwards. */ data class BackupFilePlan( val target: BackupTarget, val fileName: String, + /** Earlier files of this series that stay, oldest first. */ + val keep: List, /** Earlier files of this series beyond the retention count, oldest first. */ val prune: List, - /** Former names of the series that still have files after [prune], so they stay tracked. */ - val liveFormerBaseNames: Set = emptySet(), ) +/** + * The files the automatic backup has written, per series key, oldest first. + * Pruning only ever deletes names recorded here, so a series is its files, not + * whatever currently matches a calendar's name: renames, same-named calendars + * and the user's own files in the folder can't pull in the wrong ones. + */ +typealias BackupLedger = Map> + +const val COMBINED_SERIES = "combined" + /** The combined file's series name; also the fixed name used before #411. */ const val COMBINED_BACKUP_BASE = "calendula-backup" @@ -84,7 +94,6 @@ private const val ICS_EXTENSION = ".ics" private const val MAX_BASE_LENGTH = 60 private const val STAMP_PATTERN = "yyyy-MM-dd_HH-mm-ss" private val STAMP_FORMAT: DateTimeFormatter = DateTimeFormatter.ofPattern(STAMP_PATTERN) -private const val STAMP_REGEX = "\\d{4}-\\d{2}-\\d{2}_\\d{2}-\\d{2}-\\d{2}" private val FORBIDDEN_CHARS = Regex("[\\\\/:*?\"<>|\\p{Cntrl}]") private val WHITESPACE = Regex("\\s+") @@ -100,7 +109,7 @@ fun sanitizeBackupName(name: String, fallback: String = "calendar"): String { .replace(WHITESPACE, " ") .trim() .trimStart('.') - // Cut by code point, so an emoji at the limit isn't split into a lone surrogate. + // By code point, so an emoji at the limit isn't split into a lone surrogate. val end = if (cleaned.codePointCount(0, cleaned.length) > MAX_BASE_LENGTH) { cleaned.offsetByCodePoints(0, MAX_BASE_LENGTH) } else { @@ -111,21 +120,16 @@ fun sanitizeBackupName(name: String, fallback: String = "calendar"): String { /** * The file series the automatic backup writes: one combined file, or one per - * calendar named after it. Names are compared case-insensitively, as most - * storage is; calendars whose names clash (a local and a synced "Personal") - * take their id as a suffix until every series is distinct, so none prunes - * another's files. [previousBases] — the series names each calendar's files - * still carry from earlier runs — let a renamed calendar keep pruning its old - * files. + * calendar named after it. Calendars whose names clash case-insensitively (a + * local and a synced "Personal") take their id as a suffix until every name in + * the run is distinct, so no two series write the same file. */ -fun backupTargets( - calendars: List, - combined: Boolean, - previousBases: Map> = emptyMap(), -): List { +fun backupTargets(calendars: List, combined: Boolean): List { if (calendars.isEmpty()) return emptyList() if (combined) { - return listOf(BackupTarget(calendars.map { it.id }.toSet(), COMBINED_BACKUP_BASE, calendarName = null)) + return listOf( + BackupTarget(COMBINED_SERIES, calendars.map { it.id }.toSet(), COMBINED_BACKUP_BASE, calendarName = null), + ) } val bases = calendars.associateTo(LinkedHashMap()) { it.id to sanitizeBackupName(it.displayName) } while (true) { @@ -136,15 +140,12 @@ fun backupTargets( if (clashing.isEmpty()) break clashing.forEach { (id, base) -> bases[id] = "$base-$id" } } - val current = bases.values.map { it.lowercase() }.toSet() return calendars.map { calendar -> - val base = bases.getValue(calendar.id) BackupTarget( + key = calendar.id.toString(), calendarIds = setOf(calendar.id), - baseName = base, + baseName = bases.getValue(calendar.id), calendarName = calendar.displayName, - formerBaseNames = previousBases[calendar.id].orEmpty() - .filterTo(mutableSetOf()) { it.lowercase() !in current }, ) } } @@ -154,75 +155,74 @@ fun backupFileName(baseName: String, at: LocalDateTime): String = "${baseName}_${at.format(STAMP_FORMAT)}$ICS_EXTENSION" /** - * Plan one run: a new file per target, and which of that target's earlier files - * fall out of the newest [keep] (the new one included). Only names this app - * writes for the target count — `_.ics`, plus the pre-#411 - * [LEGACY_BACKUP_FILE] for the combined series, which ranks oldest — so a user's - * own files in the folder are never touched. + * Plan one run: a new 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. The pre-#411 + * [LEGACY_BACKUP_FILE] joins the combined series as its oldest file. */ fun planBackupFiles( targets: List, - existing: Collection, + ledger: BackupLedger, + existing: Set, at: LocalDateTime, keep: Int, ): List { val retain = keep.coerceAtLeast(1) return targets.map { target -> val fileName = backupFileName(target.baseName, at) - val series = (target.formerBaseNames + target.baseName).map { base -> - base to Regex("^${Regex.escape(base)}_($STAMP_REGEX)${Regex.escape(ICS_EXTENSION)}$") + val recorded = ledger[target.key].orEmpty() + val legacy = if (target.key == COMBINED_SERIES && LEGACY_BACKUP_FILE !in recorded) { + listOf(LEGACY_BACKUP_FILE) + } else { + emptyList() } - val earlier = existing - .filter { it != fileName } - .mapNotNull { name -> - series.firstNotNullOfOrNull { (base, regex) -> - regex.find(name)?.let { SeriesFile(name, base, it.groupValues[1]) } - } ?: if (target.baseName == COMBINED_BACKUP_BASE && name == LEGACY_BACKUP_FILE) { - SeriesFile(name, COMBINED_BACKUP_BASE, stamp = "") - } else { - null - } - } - .sortedByDescending { it.stamp } - val kept = earlier.take(retain - 1) + val earlier = (legacy + recorded).filter { it in existing && it != fileName }.distinct() + val kept = earlier.takeLast(retain - 1) BackupFilePlan( target = target, fileName = fileName, - prune = earlier.drop(retain - 1).reversed().map { it.name }, - liveFormerBaseNames = kept.map { it.base }.filterTo(mutableSetOf()) { it in target.formerBaseNames }, + keep = kept, + prune = earlier.dropLast(kept.size), ) } } -private data class SeriesFile(val name: String, val base: String, val stamp: String) - /** - * The series names each calendar's files carry after a run that wrote - * [written]: its current name plus former ones that still have files. Calendars - * not written this run keep what [previous] had. + * The ledger after a run: each written series records what it kept, what it + * failed to delete ([undeleted]) and the file it just wrote ([writtenNames], + * by key, as the provider named it). Series not written this run 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 ever put back. */ -fun updatedBackupBases(previous: Map>, written: List): Map> = - previous + written.filter { it.target.calendarName != null }.associate { - it.target.calendarIds.single() to (it.liveFormerBaseNames + it.target.baseName) +fun updatedBackupLedger( + previous: BackupLedger, + written: List, + writtenNames: Map, + undeleted: Set, + liveKeys: Set, +): BackupLedger { + val next = previous.filterKeys { it in liveKeys || it == COMBINED_SERIES }.toMutableMap() + written.forEach { plan -> + val name = writtenNames[plan.target.key] ?: return@forEach + next[plan.target.key] = plan.prune.filter { it in undeleted } + plan.keep + name } + return next.filterValues { it.isNotEmpty() } +} -private const val BASES_ENTRY_SEP = "|" -private const val BASES_KEY_SEP = "/" +private const val LEDGER_ENTRY_SEP = "|" +private const val LEDGER_KEY_SEP = "/" -/** Stores the id → series names map; both separators are characters a sanitised name can't hold. */ -fun persistBackupBases(bases: Map>): String? = - bases.filterValues { it.isNotEmpty() }.takeIf { it.isNotEmpty() } +/** Stores the ledger; both separators are characters no name this app writes can hold. */ +fun persistBackupLedger(ledger: BackupLedger): String? = + ledger.filterValues { it.isNotEmpty() }.takeIf { it.isNotEmpty() } ?.entries?.sortedBy { it.key } - ?.joinToString(BASES_ENTRY_SEP) { (id, names) -> - (listOf(id.toString()) + names.sorted()).joinToString(BASES_KEY_SEP) - } + ?.joinToString(LEDGER_ENTRY_SEP) { (key, names) -> (listOf(key) + names).joinToString(LEDGER_KEY_SEP) } -fun parseBackupBases(stored: String?): Map> = - stored?.split(BASES_ENTRY_SEP).orEmpty().mapNotNull { entry -> - val parts = entry.split(BASES_KEY_SEP) - val id = parts.first().toLongOrNull() ?: return@mapNotNull null - val names = parts.drop(1).filter { it.isNotEmpty() }.toSet() - names.takeIf { it.isNotEmpty() }?.let { id to it } +fun parseBackupLedger(stored: String?): BackupLedger = + stored?.split(LEDGER_ENTRY_SEP).orEmpty().mapNotNull { entry -> + val parts = entry.split(LEDGER_KEY_SEP) + val names = parts.drop(1).filter { it.isNotEmpty() } + parts.first().takeIf { it.isNotEmpty() && names.isNotEmpty() }?.let { it to names } }.toMap() /** Closest offered retention count to [stored], so a stray value still shows a row. */ diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/IcsEvent.kt b/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/IcsEvent.kt index 19da17bb..afddbb0b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/IcsEvent.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/domain/ics/IcsEvent.kt @@ -36,6 +36,8 @@ data class IcsEvent( val availability: Availability = Availability.Busy, /** Source calendar name, emitted as `X-CALENDULA-CALENDAR` so a combined backup can fan back out. */ val calendarName: String? = null, + /** The provider calendar the event was read from; not written to the file. */ + val calendarId: Long? = null, ) /** diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt index cf70f832..4abb715f 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupScreen.kt @@ -98,8 +98,6 @@ fun BackupScreen( val snackbarHostState = remember { SnackbarHostState() } // The one-calendar shortcut; the per-calendar selector owns its own launcher. - // The id is taken at the tap, so a result delivered into a recreated screen - // still knows what to export. var shortcutExportId by rememberSaveable { mutableStateOf(null) } val createBackup = rememberLauncherForActivityResult( contract = ActivityResultContracts.CreateDocument("text/calendar"), @@ -345,7 +343,14 @@ private fun BackupContent( position = Position.Bottom, onClick = onEditKeep, ) - GroupedHint(backupStatusText(autoBackup.status), style = MaterialTheme.typography.bodySmall) + GroupedHint( + if (selectedCount == 0) { + stringResource(R.string.calendars_auto_backup_nothing_selected) + } else { + backupStatusText(autoBackup.status) + }, + style = MaterialTheme.typography.bodySmall, + ) } } @@ -395,7 +400,7 @@ private fun ExportCalendarPicker( ) Button( onClick = { - runCatching { createBackup.launch("calendula-backup-${LocalDate.now()}.ics") } + runCatching { createBackup.launch(defaultBackupName()) } }, enabled = selected.isNotEmpty(), modifier = Modifier diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupUiState.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupUiState.kt index 2b61a618..13daf24b 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupUiState.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/calendars/BackupUiState.kt @@ -37,11 +37,10 @@ sealed interface BackupUiState { * * A failure is raised only when *neither* is possible — deliberately not * [calendarListFailure] on its own, which would call an all-hidden device a - * failure while its calendars are still perfectly exportable. When both - * halves are dead the list itself usually says why (no calendars, everything - * switched off); [FailureReason.NoImportTarget] covers the remaining case, where - * calendars exist and are visible but every one of them is read-only, managed or - * not synced to the device. + * failure while its calendars are still perfectly exportable. Since export + * reads hidden and read-only calendars too, both halves are dead only when there + * is no calendar at all, or every one is managed or not synced to the device + * ([FailureReason.NoImportTarget] unless the list itself says why). */ fun backupUiState(calendars: List): BackupUiState { val exportable = calendars.filter { it.isBackupCandidate } diff --git a/app/src/main/java/de/jeanlucmakiola/calendula/ui/onboarding/OnboardingViewModel.kt b/app/src/main/java/de/jeanlucmakiola/calendula/ui/onboarding/OnboardingViewModel.kt index 2a2894d4..3c7c24cd 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/ui/onboarding/OnboardingViewModel.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/ui/onboarding/OnboardingViewModel.kt @@ -15,6 +15,7 @@ import de.jeanlucmakiola.calendula.data.prefs.SettingsPrefs import de.jeanlucmakiola.calendula.data.prefs.WeekStartPref import de.jeanlucmakiola.calendula.data.reminders.ReminderScanner import de.jeanlucmakiola.calendula.domain.CalendarSource +import de.jeanlucmakiola.calendula.domain.backup.isBackedUpByDefault import de.jeanlucmakiola.calendula.domain.hasVisibilitySwitch import de.jeanlucmakiola.calendula.domain.orderedForManager import de.jeanlucmakiola.calendula.ui.common.CalendarView @@ -483,7 +484,7 @@ private fun List.allHidden(): Boolean = * A read-only subscription is not a backup, so it does not count. */ private fun List.backupApplies(): Boolean { - val exportable = any { it.isLocal && it.canModifyContents && !it.isManaged } + val exportable = any { it.isBackedUpByDefault } val syncedTarget = any { !it.isLocal && it.canModifyContents } return exportable && !syncedTarget } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 27a2ce7b..6eabe51f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -902,6 +902,7 @@ %d calendars No calendar selected + No calendar is selected, so nothing is backed up. Local calendars are backed up unless you turn them off. Synced calendars already live on their server, so add them here only if you want a copy in the backup folder too. Single file All calendars in one file 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 93a780d2..3e84a322 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 @@ -80,6 +80,7 @@ class BackupPlanTest { @Test fun `combined mode is one target holding every calendar`() { val target = backupTargets(listOf(cal(1L), cal(2L)), combined = true).single() + assertThat(target.key).isEqualTo(COMBINED_SERIES) assertThat(target.calendarIds).containsExactly(1L, 2L) assertThat(target.baseName).isEqualTo(COMBINED_BACKUP_BASE) assertThat(target.calendarName).isNull() @@ -91,114 +92,129 @@ class BackupPlanTest { assertThat(backupFileName("Work", at)).isEqualTo("Work_2026-11-13_14-30-05.ics") } + private val work = BackupTarget("1", setOf(1L), "Work", "Work") + @Test - fun `pruning keeps the newest files of the series and nothing else`() { - val target = BackupTarget(setOf(1L), "Work", "Work") - val existing = listOf( - "Work_2026-11-10_09-00-00.ics", - "Work_2026-11-12_09-00-00.ics", - "Work_2026-11-11_09-00-00.ics", - "Work notes.ics", - "Work_backup.ics", - "Workshop_2026-11-01_09-00-00.ics", - "Work-7_2026-11-01_09-00-00.ics", - ) - val plan = planBackupFiles(listOf(target), existing, at, keep = 3).single() + fun `pruning keeps the newest recorded files and nothing else`() { + val ledger = mapOf("1" to listOf("Work_a.ics", "Work_b.ics", "Work_c.ics")) + val existing = setOf("Work_a.ics", "Work_b.ics", "Work_c.ics", "Work_2026-11-01_09-00-00.ics", "Work notes.ics") + val plan = planBackupFiles(listOf(work), ledger, existing, at, keep = 3).single() assertThat(plan.fileName).isEqualTo("Work_2026-11-13_14-30-05.ics") - assertThat(plan.prune).containsExactly("Work_2026-11-10_09-00-00.ics") + assertThat(plan.keep).containsExactly("Work_b.ics", "Work_c.ics").inOrder() + assertThat(plan.prune).containsExactly("Work_a.ics") } @Test fun `keeping one replaces every earlier file`() { - val target = BackupTarget(setOf(1L), "Work", "Work") - val existing = listOf("Work_2026-11-11_09-00-00.ics", "Work_2026-11-12_09-00-00.ics") - val plan = planBackupFiles(listOf(target), existing, at, keep = 1).single() - assertThat(plan.prune).containsExactly("Work_2026-11-11_09-00-00.ics", "Work_2026-11-12_09-00-00.ics").inOrder() + val ledger = mapOf("1" to listOf("Work_a.ics", "Work_b.ics")) + val plan = planBackupFiles(listOf(work), ledger, setOf("Work_a.ics", "Work_b.ics"), at, keep = 1).single() + assertThat(plan.keep).isEmpty() + assertThat(plan.prune).containsExactly("Work_a.ics", "Work_b.ics").inOrder() + } + + @Test + fun `files gone from the folder are forgotten, not counted`() { + val ledger = mapOf("1" to listOf("Work_a.ics", "Work_b.ics")) + val plan = planBackupFiles(listOf(work), ledger, setOf("Work_b.ics"), at, keep = 2).single() + assertThat(plan.keep).containsExactly("Work_b.ics") + assertThat(plan.prune).isEmpty() + } + + @Test + fun `a same-named calendar never prunes another calendar's files`() { + // Local "Work" (id 1) was backed up; the user now backs up a synced "Work" (id 9) instead. + val ledger = mapOf("1" to listOf("Work_a.ics")) + val synced = backupTargets(listOf(cal(9L, "Work")), combined = false).single() + val plan = planBackupFiles(listOf(synced), ledger, setOf("Work_a.ics"), at, keep = 1).single() + assertThat(plan.prune).isEmpty() } @Test fun `a rerun in the same second never prunes the file it writes`() { - val target = BackupTarget(setOf(1L), "Work", "Work") - val plan = planBackupFiles(listOf(target), listOf("Work_2026-11-13_14-30-05.ics"), at, keep = 1).single() + val name = "Work_2026-11-13_14-30-05.ics" + val plan = planBackupFiles(listOf(work), mapOf("1" to listOf(name)), setOf(name), at, keep = 1).single() assertThat(plan.prune).isEmpty() } @Test fun `the pre-411 backup file is the combined series' oldest member`() { - val target = BackupTarget(setOf(1L, 2L), COMBINED_BACKUP_BASE, null) - val existing = listOf(LEGACY_BACKUP_FILE, "calendula-backup_2026-11-12_09-00-00.ics") - assertThat(planBackupFiles(listOf(target), existing, at, keep = 3).single().prune).isEmpty() - assertThat(planBackupFiles(listOf(target), existing, at, keep = 2).single().prune) + val target = BackupTarget(COMBINED_SERIES, setOf(1L, 2L), COMBINED_BACKUP_BASE, null) + val newer = "calendula-backup_2026-11-12_09-00-00.ics" + val ledger = mapOf(COMBINED_SERIES to listOf(newer)) + val existing = setOf(LEGACY_BACKUP_FILE, newer) + assertThat(planBackupFiles(listOf(target), ledger, existing, at, keep = 3).single().prune).isEmpty() + assertThat(planBackupFiles(listOf(target), ledger, existing, at, keep = 2).single().prune) .containsExactly(LEGACY_BACKUP_FILE) } @Test fun `per-calendar series leave the legacy combined file alone`() { - val target = BackupTarget(setOf(1L), "Work", "Work") - val plan = planBackupFiles(listOf(target), listOf(LEGACY_BACKUP_FILE), at, keep = 1).single() + val plan = planBackupFiles(listOf(work), emptyMap(), setOf(LEGACY_BACKUP_FILE), at, keep = 1).single() assertThat(plan.prune).isEmpty() } + @Test + fun `the ledger records what the run kept, failed to delete and wrote`() { + 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, + ).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")), + written = listOf(plan), + writtenNames = mapOf("1" to "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"), + ) + } + + @Test + fun `a series whose write failed keeps its old entry`() { + val plan = planBackupFiles(listOf(work), mapOf("1" to listOf("Work_a.ics")), setOf("Work_a.ics"), at, keep = 1) + val next = updatedBackupLedger( + previous = mapOf("1" to listOf("Work_a.ics")), + written = plan, + writtenNames = emptyMap(), + undeleted = emptySet(), + liveKeys = setOf("1"), + ) + assertThat(next).containsExactly("1", listOf("Work_a.ics")) + } + + @Test + fun `the ledger round-trips through storage`() { + val ledger = mapOf("3" to listOf("Work_b.ics", "Work_a (1).ics"), COMBINED_SERIES to listOf("calendula-backup_x.ics")) + assertThat(parseBackupLedger(persistBackupLedger(ledger))).isEqualTo(ledger) + assertThat(persistBackupLedger(emptyMap())).isNull() + assertThat(parseBackupLedger("garbage|/x|4/")).isEmpty() + } + @Test fun `retention snaps to the offered choices`() { assertThat(normalizeBackupKeep(0)).isEqualTo(1) assertThat(normalizeBackupKeep(3)).isEqualTo(3) assertThat(normalizeBackupKeep(50)).isEqualTo(10) } + @Test fun `a suffixed name that meets a real one is told apart again`() { val targets = backupTargets( listOf(cal(5L, "Work"), cal(6L, "Work"), cal(9L, "Work-5")), combined = false, ) - assertThat(targets.map { it.baseName }.toSet()).hasSize(3) assertThat(targets.map { it.baseName }).containsExactly("Work-5-5", "Work-6", "Work-5-9").inOrder() } @Test fun `an emoji at the length limit is not split`() { val name = "a".repeat(59) + "\uD83D\uDCC5" + "tail" - val cleaned = sanitizeBackupName(name) - assertThat(cleaned).isEqualTo("a".repeat(59) + "\uD83D\uDCC5") - assertThat(Character.isHighSurrogate(cleaned.last())).isFalse() - } - - @Test - fun `a renamed calendar keeps pruning the files of its old name`() { - val target = backupTargets( - listOf(cal(1L, "Job")), - combined = false, - previousBases = mapOf(1L to setOf("Work")), - ).single() - assertThat(target.formerBaseNames).containsExactly("Work") - val existing = listOf("Work_2026-11-10_09-00-00.ics", "Work_2026-11-12_09-00-00.ics") - - val keepTwo = planBackupFiles(listOf(target), existing, at, keep = 2).single() - assertThat(keepTwo.prune).containsExactly("Work_2026-11-10_09-00-00.ics") - assertThat(keepTwo.liveFormerBaseNames).containsExactly("Work") - assertThat(updatedBackupBases(emptyMap(), listOf(keepTwo))).containsExactly(1L, setOf("Work", "Job")) - - val keepOne = planBackupFiles(listOf(target), existing, at, keep = 1).single() - assertThat(keepOne.prune).hasSize(2) - assertThat(updatedBackupBases(mapOf(1L to setOf("Work"), 2L to setOf("X")), listOf(keepOne))) - .containsExactly(1L, setOf("Job"), 2L, setOf("X")) - } - - @Test - fun `a former name now used by another calendar is not claimed`() { - val targets = backupTargets( - listOf(cal(1L, "Job"), cal(2L, "Work")), - combined = false, - previousBases = mapOf(1L to setOf("Work")), - ) - assertThat(targets.first().formerBaseNames).isEmpty() - } - - @Test - fun `series names round-trip through storage`() { - val bases = mapOf(3L to setOf("Work", "Job"), 1L to setOf("Home")) - assertThat(parseBackupBases(persistBackupBases(bases))).isEqualTo(bases) - assertThat(persistBackupBases(emptyMap())).isNull() - assertThat(parseBackupBases("garbage|x/y|4/")).isEmpty() + assertThat(sanitizeBackupName(name)).isEqualTo("a".repeat(59) + "\uD83D\uDCC5") } } diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 36bb87e2..5991327c 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -471,9 +471,9 @@ restore runs through the normal import, whose targets stay `isEventTarget`. The automatic backup writes one `__