fix(#147, #411): review round 3 - serialised runs, per-series ledger saves, folder change resets ledger

This commit is contained in:
makiolaj committed 2026-11-13 16:46:38 +01:00
1 parent 46818cd0ae
commit f314761e45
10 files changed
+54 -78

No files matched your search

@@ -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<String, String>()
val undeleted = mutableSetOf<String>()
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()
}
}
@@ -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()
}
@@ -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
@@ -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<String, Uri> {
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<String, Uri>, 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<String, Uri>, names: Collection<String>): Set<String> =
/** Delete the [names] files of [listing]; returns the ones that didn't go, or weren't listed. */
fun deleteFromFolder(listing: FolderListing, names: Collection<String>): Set<String> =
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<String, Uri>, val complete: Boolean)
@@ -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
}
}
@@ -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<Int> = 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<CalendarSource>, combined: Boolean): List<Back
BackupTarget(COMBINED_SERIES, calendars.map { it.id }.toSet(), COMBINED_BACKUP_BASE, calendarName = null),
)
}
val bases = calendars.associateTo(LinkedHashMap()) { it.id to sanitizeBackupName(it.displayName) }
val bases = calendars.associateTo(LinkedHashMap()) {
it.id to safeFileName(it.displayName, MAX_BASE_BYTES, fallback = "calendar")
}
while (true) {
val counts = bases.values.groupingBy { it.lowercase() }.eachCount()
val clashing = bases.filterValues {
@@ -36,8 +36,6 @@ 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,
)
/**
@@ -233,7 +233,7 @@ private fun BackupLoading() {
private fun defaultBackupName(): String = "calendula-backup-${LocalDate.now()}.ics"
/**
* The working screen: export (when there are local calendars to export), restore,
* The working screen: export (when any calendar can be backed up), restore,
* and the automatic-backup block. Restore is always on screen — when nothing can
* receive the events it says so and routes to the calendar manager rather than
* disappearing, which is what #304 reported as an invisible button.
@@ -53,14 +53,6 @@ class BackupPlanTest {
assertThat(chosen.with(synced, false).with(local, true)).isEqualTo(BackupSelection())
}
@Test
fun `file names are sanitised calendar names`() {
assertThat(sanitizeBackupName("Work/Home: \"plans\"")).isEqualTo("Work_Home_ _plans_")
assertThat(sanitizeBackupName(" ..hidden ")).isEqualTo("hidden")
assertThat(sanitizeBackupName(" ")).isEqualTo("calendar")
assertThat(sanitizeBackupName("a\tb\nc")).isEqualTo("a_b_c")
}
@Test
fun `one target per calendar, clashing names told apart by id`() {
val targets = backupTargets(
@@ -212,9 +204,4 @@ class BackupPlanTest {
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"
assertThat(sanitizeBackupName(name)).isEqualTo("a".repeat(59) + "\uD83D\uDCC5")
}
}