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 02977e18..954debe7 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 @@ -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( 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 cb1085a5..a2451e1f 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 @@ -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, combined: Boolean): List { +fun backupTargets( + calendars: List, + combined: Boolean, + ledger: BackupLedger = emptyMap(), +): List { if (calendars.isEmpty()) return emptyList() if (combined) { return listOf( @@ -136,12 +144,17 @@ fun backupTargets(calendars: List, combined: Boolean): List 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, combined: Boolean): List