From f314761e4564e736e0ef279541f3463f18efe1af Mon Sep 17 00:00:00 2001 From: Jean-Luc Makiola Date: Fri, 13 Nov 2026 16:46:38 +0100 Subject: [PATCH] fix(#147, #411): review round 3 - serialised runs, per-series ledger saves, folder change resets ledger --- .../calendula/data/backup/AutoBackup.kt | 45 +++++++++---------- .../data/calendar/CalendarDataSource.kt | 5 ++- .../data/calendar/ReminderQueries.kt | 2 +- .../calendula/data/ics/IcsExporter.kt | 28 ++++++++---- .../calendula/data/prefs/SettingsPrefs.kt | 2 + .../calendula/domain/backup/BackupPlan.kt | 31 +++---------- .../calendula/domain/ics/IcsEvent.kt | 2 - .../calendula/ui/calendars/BackupScreen.kt | 2 +- .../calendula/domain/backup/BackupPlanTest.kt | 13 ------ floret-kit | 2 +- 10 files changed, 54 insertions(+), 78 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 ef16e031..35df7b27 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,13 +26,14 @@ 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.COMBINED_SERIES 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.ics.IcsWriter import de.jeanlucmakiola.floret.android.hasPermission import kotlinx.coroutines.flow.first +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import java.time.LocalDateTime import java.util.concurrent.TimeUnit import kotlin.coroutines.cancellation.CancellationException @@ -54,8 +55,8 @@ object BackupScheduler { * periodic request when backup is on and a folder is set, otherwise cancel * it. WorkManager persists the request across reboots on its own. * - * The first run is delayed by one interval so it never overlaps an immediate - * [runNow] (two simultaneous writes would race and leave a "(1)" duplicate). + * The first run is delayed by one interval so it doesn't follow an immediate + * [runNow] with a near-identical copy. */ fun apply(context: Context, enabled: Boolean, intervalMinutes: Long, hasFolder: Boolean) { val workManager = WorkManager.getInstance(context) @@ -123,7 +124,9 @@ class BackupWorker( ?: return Result.failure() // nothing to write to — leave scheduling to settings val now = System.currentTimeMillis() val outcome = try { - runBackup(deps, prefs, folder.toUri()) + // The periodic and the immediate run are separate unique works; one + // at a time, or each would overwrite the other's ledger. + runLock.withLock { runBackup(deps, prefs, folder.toUri()) } } catch (e: CancellationException) { throw e } catch (e: Exception) { @@ -152,45 +155,36 @@ class BackupWorker( val selection = prefs.autoBackupSelection.first() 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) + var ledger = prefs.autoBackupLedger.first() val plans = planBackupFiles( targets = targets, ledger = ledger, - existing = listing.keys, + // 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(), 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 liveKeys = all.map { it.id.toString() }.toSet() val writtenNames = mutableMapOf() - val undeleted = mutableSetOf() for (plan in plans) { try { 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) + val events = repository.exportEvents(target.calendarIds) + val content = IcsWriter().writeCalendar(events, stamp, target.calendarName) + val name = exporter.writeToFolder(folder, listing, plan.fileName, content) + writtenNames[target.key] = name + val undeleted = exporter.deleteFromFolder(listing, plan.prune) + // Saved per series, so a run cut short still knows what it wrote. + ledger = updatedBackupLedger(ledger, listOf(plan), mapOf(target.key to name), undeleted, liveKeys) + prefs.setAutoBackupLedger(ledger) } catch (e: CancellationException) { throw e } catch (e: Exception) { Log.w(TAG, "Backup of one series failed", e) } } - 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 @@ -237,5 +231,6 @@ class BackupWorker( private const val CHANNEL_ID = "backup" private const val NOTIFICATION_ID = 2 private const val TAG = "BackupWorker" + private val runLock = Mutex() } } 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 1d4f07f9..879bc77e 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 @@ -792,7 +792,8 @@ class AndroidCalendarDataSource @Inject constructor( // 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" + "${CalendarContract.Events.ORIGINAL_ID} IS NULL AND " + + "${CalendarContract.Events.ORIGINAL_SYNC_ID} IS NULL" val eventIds = resolver.query( CalendarContract.Events.CONTENT_URI, arrayOf(CalendarContract.Events._ID), @@ -815,7 +816,7 @@ class AndroidCalendarDataSource @Inject constructor( reminderMinutes = reminders[eventId].orEmpty(), calendarName = names[calendarId], allDayReminderTimeMinutes = allDayReminderTimeMinutes, - ).copy(calendarId = calendarId) + ) } } ?: emptyList() } 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 index 733167c2..6e517a59 100644 --- a/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/ReminderQueries.kt +++ b/app/src/main/java/de/jeanlucmakiola/calendula/data/calendar/ReminderQueries.kt @@ -4,7 +4,7 @@ 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 +private const val EVENT_ID_BATCH = 500 /** * Reminder minutes of [eventIds], keyed by event, a batched query at a time 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 be76fdc9..f159072c 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 @@ -36,7 +36,7 @@ class IcsExporter @Inject constructor( * a run over many calendars doesn't pay a lookup per file. Throws when the * folder is gone or not writable. */ - fun listFolder(folder: Uri): Map { + fun listFolder(folder: Uri): FolderListing { writableFolder(folder) val treeId = DocumentsContract.getTreeDocumentId(folder) val children = DocumentsContract.buildChildDocumentsUriUsingTree(folder, treeId) @@ -48,13 +48,14 @@ class IcsExporter @Inject constructor( val cursor = context.contentResolver.query(children, columns, null, null, null) ?: throw IOException("Backup folder can't be listed") return cursor.use { c -> - buildMap { + val files = buildMap { while (c.moveToNext()) { if (c.getString(2) == DocumentsContract.Document.MIME_TYPE_DIR) continue val name = c.getString(1) ?: continue put(name, DocumentsContract.buildDocumentUriUsingTree(folder, c.getString(0))) } } + FolderListing(files, complete = !c.extras.getBoolean(DocumentsContract.EXTRA_LOADING, false)) } } @@ -63,8 +64,8 @@ class IcsExporter @Inject constructor( * 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): String { - listing[fileName]?.let { uri -> + fun writeToFolder(folder: Uri, listing: FolderListing, fileName: String, content: String): String { + listing.files[fileName]?.let { uri -> writeDocument(uri, content) return fileName } @@ -74,14 +75,19 @@ class IcsExporter @Inject constructor( MIME_CALENDAR, fileName, ) ?: throw IOException("Could not create backup file in the chosen folder") - writeDocument(uri, content) + try { + writeDocument(uri, content) + } catch (e: IOException) { + runCatching { DocumentsContract.deleteDocument(context.contentResolver, uri) } + throw e + } return DocumentFile.fromSingleUri(context, uri)?.name ?: fileName } - /** Delete the [names] files of [listing]; returns the ones that wouldn't go. */ - fun deleteFromFolder(listing: Map, names: Collection): Set = + /** Delete the [names] files of [listing]; returns the ones that didn't go, or weren't listed. */ + fun deleteFromFolder(listing: FolderListing, names: Collection): Set = names.filterTo(mutableSetOf()) { name -> - val uri = listing[name] ?: return@filterTo false + val uri = listing.files[name] ?: return@filterTo true !runCatching { DocumentsContract.deleteDocument(context.contentResolver, uri) }.getOrDefault(false) } @@ -111,3 +117,9 @@ class IcsExporter @Inject constructor( const val MIME_CALENDAR = "text/calendar" } } + +/** + * A backup folder's files by name. A cloud provider may still be loading + * ([complete] false), so a name missing from [files] isn't proof it's gone. + */ +data class FolderListing(val files: Map, val complete: Boolean) 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..ed82b268 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 @@ -351,6 +351,8 @@ class SettingsPrefs @Inject constructor( if (it[SettingsKeys.autoBackupCombinedStored] == null) { it[SettingsKeys.autoBackupCombinedStored] = it[SettingsKeys.autoBackupFolderUri] != null } + // The ledger names files in the old folder; it means nothing in a new one. + if (it[SettingsKeys.autoBackupFolderUri] != uri) it[SettingsKeys.autoBackupLedger] = emptyMap() it[SettingsKeys.autoBackupFolderUri] = uri } } 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 edf3e8ff..231dffde 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 @@ -2,6 +2,7 @@ package de.jeanlucmakiola.calendula.domain.backup import de.jeanlucmakiola.calendula.domain.CalendarSource import de.jeanlucmakiola.calendula.domain.isNotSynced +import de.jeanlucmakiola.floret.android.safeFileName import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -90,33 +91,11 @@ val BACKUP_KEEP_OPTIONS: List = listOf(1, 2, 3, 5, 7, 10) const val DEFAULT_BACKUP_KEEP = 3 +/** Room for the id suffix and the stamp within a file system's 255-byte name limit. */ +private const val MAX_BASE_BYTES = 120 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 val FORBIDDEN_CHARS = Regex("[\\\\/:*?\"<>|\\p{Cntrl}]") -private val WHITESPACE = Regex("\\s+") - -/** - * [name] made safe as a file name on every storage provider: path separators, - * reserved and control characters become `_`, whitespace collapses, and leading - * dots go so the file can't turn hidden. Falls back to [fallback] when nothing - * is left. - */ -fun sanitizeBackupName(name: String, fallback: String = "calendar"): String { - val cleaned = name - .replace(FORBIDDEN_CHARS, "_") - .replace(WHITESPACE, " ") - .trim() - .trimStart('.') - // 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 { - cleaned.length - } - return cleaned.substring(0, end).trim().ifEmpty { fallback } -} /** * The file series the automatic backup writes: one combined file, or one per @@ -131,7 +110,9 @@ fun backupTargets(calendars: List, combined: Boolean): List