This commit is contained in:
1 parent
c6901c9c63
commit
b1ff5912fc
3 files changed
+61
-16
No files matched your search
@@ -158,11 +158,12 @@ class BackupWorker(
|
||||
if (!applicationContext.hasPermission(Manifest.permission.READ_CALENDAR)) return RunOutcome.Failed
|
||||
val all = repository.calendars().first()
|
||||
val selection = prefs.autoBackupSelection.first()
|
||||
val targets = backupTargets(all.filter(selection::selects), combined = prefs.autoBackupCombined.first())
|
||||
if (targets.isEmpty()) return RunOutcome.NothingSelected
|
||||
val combined = prefs.autoBackupCombined.first()
|
||||
if (all.none(selection::selects)) return RunOutcome.NothingSelected
|
||||
prefs.adoptLegacyBackupFile()
|
||||
val listing = exporter.listFolder(folder)
|
||||
val ledger = prefs.autoBackupLedger.first()
|
||||
val targets = backupTargets(all.filter(selection::selects), combined, ledger)
|
||||
// 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(
|
||||
|
||||
@@ -122,10 +122,18 @@ private val STAMP_FORMAT: DateTimeFormatter = DateTimeFormatter.ofPattern(STAMP_
|
||||
/**
|
||||
* The file series the automatic backup writes: one combined file, or one per
|
||||
* 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.
|
||||
* local and a synced "Personal") must not write the same file: the one whose
|
||||
* series already holds the name in [ledger] keeps it, so a path something
|
||||
* follows doesn't move when another calendar takes the same name; with no
|
||||
* owner the lowest id keeps it. The others take their id as a suffix until
|
||||
* every name in the run is distinct. A calendar named like the combined file
|
||||
* is always suffixed.
|
||||
*/
|
||||
fun backupTargets(calendars: List<CalendarSource>, combined: Boolean): List<BackupTarget> {
|
||||
fun backupTargets(
|
||||
calendars: List<CalendarSource>,
|
||||
combined: Boolean,
|
||||
ledger: BackupLedger = emptyMap(),
|
||||
): List<BackupTarget> {
|
||||
if (calendars.isEmpty()) return emptyList()
|
||||
if (combined) {
|
||||
return listOf(
|
||||
@@ -136,12 +144,17 @@ fun backupTargets(calendars: List<CalendarSource>, combined: Boolean): List<Back
|
||||
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 {
|
||||
counts.getValue(it.lowercase()) > 1 || it.equals(COMBINED_BACKUP_BASE, ignoreCase = true)
|
||||
}
|
||||
val clashing = bases.entries.groupBy({ it.value.lowercase() }, { it.key })
|
||||
.filter { (lower, ids) -> ids.size > 1 || lower == COMBINED_BACKUP_BASE }
|
||||
if (clashing.isEmpty()) break
|
||||
clashing.forEach { (id, base) -> bases[id] = "$base-$id" }
|
||||
clashing.forEach { (lower, ids) ->
|
||||
val keeper = if (lower == COMBINED_BACKUP_BASE) {
|
||||
null
|
||||
} else {
|
||||
ids.filter { ledger.holdsBase(it.toString(), bases.getValue(it)) }.minOrNull() ?: ids.min()
|
||||
}
|
||||
ids.filter { it != keeper }.forEach { id -> bases[id] = "${bases.getValue(id)}-$id" }
|
||||
}
|
||||
}
|
||||
return calendars.map { calendar ->
|
||||
BackupTarget(
|
||||
@@ -153,6 +166,17 @@ fun backupTargets(calendars: List<CalendarSource>, combined: Boolean): List<Back
|
||||
}
|
||||
}
|
||||
|
||||
/** Whether series [key] has written files under [base]: its stable copy, or a dated file. */
|
||||
private fun BackupLedger.holdsBase(key: String, base: String): Boolean {
|
||||
val latest = latestFileName(base)
|
||||
val datedLength = base.length + 1 + STAMP_PATTERN.length + ICS_EXTENSION.length
|
||||
return this[latestLedgerKey(key)].orEmpty().any { it.equals(latest, ignoreCase = true) } ||
|
||||
this[key].orEmpty().any {
|
||||
it.length == datedLength && it.startsWith("${base}_", ignoreCase = true) &&
|
||||
it.endsWith(ICS_EXTENSION, ignoreCase = true)
|
||||
}
|
||||
}
|
||||
|
||||
/** `Work_2026-11-13_14-30-05.ics`: the series name and the local time of the run. */
|
||||
fun backupFileName(baseName: String, at: LocalDateTime): String =
|
||||
"${baseName}_${at.format(STAMP_FORMAT)}$ICS_EXTENSION"
|
||||
|
||||
@@ -54,13 +54,33 @@ class BackupPlanTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `one target per calendar, clashing names told apart by id`() {
|
||||
fun `one target per calendar, with no owner the lowest id keeps a clashing name`() {
|
||||
val targets = backupTargets(
|
||||
listOf(cal(1L, "Personal", local = true), cal(2L, "personal"), cal(3L, "Work")),
|
||||
listOf(cal(2L, "personal"), cal(1L, "Personal", local = true), cal(3L, "Work")),
|
||||
combined = false,
|
||||
)
|
||||
assertThat(targets.map { it.baseName }).containsExactly("Personal-1", "personal-2", "Work").inOrder()
|
||||
assertThat(targets.map { it.calendarName }).containsExactly("Personal", "personal", "Work").inOrder()
|
||||
assertThat(targets.map { it.baseName }).containsExactly("personal-2", "Personal", "Work").inOrder()
|
||||
assertThat(targets.map { it.calendarName }).containsExactly("personal", "Personal", "Work").inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the calendar already owning a name keeps it when a new one shares it`() {
|
||||
val calendars = listOf(cal(9L, "Fitness"), cal(20L, "Fitness"), cal(4L, "Fitness"))
|
||||
val byStable = backupTargets(calendars, combined = false, mapOf(latestLedgerKey("9") to listOf("Fitness.ics")))
|
||||
assertThat(byStable.map { it.baseName }).containsExactly("Fitness", "Fitness-20", "Fitness-4").inOrder()
|
||||
// A dated file counts too, for a series whose stable copy was blocked by the user's own file.
|
||||
val byDated = backupTargets(calendars, combined = false, mapOf("20" to listOf("Fitness_2026-11-13_14-30-05.ics")))
|
||||
assertThat(byDated.map { it.baseName }).containsExactly("Fitness-9", "Fitness", "Fitness-4").inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a suffixed series doesn't count as owning the plain name`() {
|
||||
val ledger = mapOf(
|
||||
latestLedgerKey("9") to listOf("Fitness-9.ics"),
|
||||
"9" to listOf("Fitness-9_2026-11-13_14-30-05.ics"),
|
||||
)
|
||||
val targets = backupTargets(listOf(cal(20L, "Fitness"), cal(9L, "Fitness")), combined = false, ledger)
|
||||
assertThat(targets.map { it.baseName }).containsExactly("Fitness-20", "Fitness").inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -262,10 +282,10 @@ class BackupPlanTest {
|
||||
@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")),
|
||||
listOf(cal(5L, "Work"), cal(6L, "Work"), cal(9L, "Work-6")),
|
||||
combined = false,
|
||||
)
|
||||
assertThat(targets.map { it.baseName }).containsExactly("Work-5-5", "Work-6", "Work-5-9").inOrder()
|
||||
assertThat(targets.map { it.baseName }).containsExactly("Work", "Work-6", "Work-6-9").inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user