fix(#516): flag series the undated copy can't cover, back up when the interval returns
This commit is contained in:
1 parent
91cb57f940
commit
4d342c98ad
7 files changed
+59
-13
No files matched your search
@@ -32,6 +32,7 @@ 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.BackupTarget
|
||||
import de.jeanlucmakiola.calendula.domain.backup.backedUp
|
||||
import de.jeanlucmakiola.calendula.domain.backup.backupFingerprint
|
||||
import de.jeanlucmakiola.calendula.domain.backup.backupRunLatestOnly
|
||||
import de.jeanlucmakiola.calendula.domain.backup.backupTargets
|
||||
@@ -243,8 +244,7 @@ class BackupWorker(
|
||||
if (changeRun && !prefs.autoBackupOnChange.first()) return Result.success()
|
||||
val folder = prefs.autoBackupFolderUri.first() ?: return Result.failure()
|
||||
val outcome = try {
|
||||
val latestOnly = backupRunLatestOnly(changeRun, prefs.autoBackupScheduled.first())
|
||||
runBackup(deps, prefs, folder.toUri(), latestOnly = latestOnly)
|
||||
runBackup(deps, prefs, folder.toUri(), changeRun, prefs.autoBackupScheduled.first())
|
||||
} catch (e: CancellationException) {
|
||||
throw e
|
||||
} catch (e: Exception) {
|
||||
@@ -274,10 +274,19 @@ class BackupWorker(
|
||||
|
||||
/**
|
||||
* Writes each series and prunes it only once its new file has landed. A
|
||||
* [latestOnly] run (#516) leaves the dated files and their retention to the
|
||||
* change run (#516) leaves the dated files and their retention to the
|
||||
* schedule and rewrites only the stable copies whose calendars changed.
|
||||
* With the interval off, no run writes dated files, so a series whose
|
||||
* stable name a file of the user's holds counts as failed.
|
||||
*/
|
||||
private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri, latestOnly: Boolean): RunOutcome {
|
||||
private suspend fun runBackup(
|
||||
deps: Deps,
|
||||
prefs: SettingsPrefs,
|
||||
folder: Uri,
|
||||
changeRun: Boolean,
|
||||
intervalOn: Boolean,
|
||||
): RunOutcome {
|
||||
val latestOnly = backupRunLatestOnly(changeRun, intervalOn)
|
||||
val repository = deps.repository()
|
||||
val exporter = deps.exporter()
|
||||
if (!applicationContext.hasPermission(Manifest.permission.READ_CALENDAR)) return RunOutcome.Failed
|
||||
@@ -309,12 +318,12 @@ class BackupWorker(
|
||||
val trackHashes = prefs.autoBackupOnChange.first()
|
||||
if (!trackHashes && fingerprints.isNotEmpty()) prefs.updateAutoBackupFingerprints(folder.toString()) { emptyMap() }
|
||||
// A change run hashes first (null: unreadable) and leaves the folder alone when nothing moved.
|
||||
val preHashes: Map<String, String?> = if (latestOnly) {
|
||||
val preHashes: Map<String, String?> = if (changeRun) {
|
||||
targets.associate { target -> target.key to export(target)?.let { fingerprintOf(target, it) } }
|
||||
} else {
|
||||
emptyMap()
|
||||
}
|
||||
if (latestOnly && targets.all { preHashes[it.key].let { hash -> hash != null && hash == fingerprints[it.key] } }) {
|
||||
if (changeRun && targets.all { preHashes[it.key].let { hash -> hash != null && hash == fingerprints[it.key] } }) {
|
||||
return RunOutcome.NothingToDo
|
||||
}
|
||||
val plans = planBackupFiles(
|
||||
@@ -391,7 +400,11 @@ class BackupWorker(
|
||||
val landedLatest = mutableSetOf<String>()
|
||||
for (plan in plans) {
|
||||
val target = plan.target
|
||||
if (latestOnly) {
|
||||
if (!plan.backedUp(intervalOn)) {
|
||||
failed++
|
||||
continue
|
||||
}
|
||||
if (changeRun) {
|
||||
val hash = preHashes[target.key] ?: run {
|
||||
failed++
|
||||
continue
|
||||
@@ -408,7 +421,7 @@ class BackupWorker(
|
||||
continue
|
||||
}
|
||||
val fingerprint = fingerprintOf(target, text)
|
||||
if (!latestOnly) rememberBlocked(plan, fingerprint)
|
||||
if (!changeRun) rememberBlocked(plan, fingerprint)
|
||||
val bytes = text.toByteArray(Charsets.UTF_8)
|
||||
// Not cancellable once a file exists: it must reach the ledger.
|
||||
withContext(NonCancellable) {
|
||||
|
||||
@@ -529,7 +529,10 @@ class SettingsPrefs @Inject constructor(
|
||||
prefs.edit { applyAutoBackupOnChange(it, enabled) }
|
||||
}
|
||||
|
||||
suspend fun setAutoBackupScheduled(enabled: Boolean) = prefs.set(SettingsKeys.autoBackupScheduledStored, enabled)
|
||||
/** Off only sticks while backing up after changes is on; see [applyAutoBackupOnChange]. */
|
||||
suspend fun setAutoBackupScheduled(enabled: Boolean) {
|
||||
prefs.edit { if (enabled || it[SettingsKeys.autoBackupOnChange]) it[SettingsKeys.autoBackupScheduledStored] = enabled }
|
||||
}
|
||||
|
||||
/**
|
||||
* Atomic, so a run's ledger writes never undo one made meanwhile (pinning
|
||||
|
||||
@@ -317,6 +317,9 @@ fun backupIntervalOn(stored: Boolean, onChange: Boolean): Boolean = stored || !o
|
||||
/** Without the interval there are no dated files, so every run only refreshes the stable copies. */
|
||||
fun backupRunLatestOnly(changeRun: Boolean, intervalOn: Boolean): Boolean = changeRun || !intervalOn
|
||||
|
||||
/** Whether a run can back this series up at all: without dated files, only through its stable copy. */
|
||||
fun BackupFilePlan.backedUp(intervalOn: Boolean): Boolean = intervalOn || latestName != null
|
||||
|
||||
/** Closest offered retention count to [stored], so a stray value still shows a row. */
|
||||
fun normalizeBackupKeep(stored: Int): Int =
|
||||
BACKUP_KEEP_OPTIONS.minBy { kotlin.math.abs(it - stored) }
|
||||
@@ -375,7 +378,7 @@ fun parseBackupFingerprints(stored: String?): BackupFingerprints =
|
||||
* only when its content moved on from what the copy holds (a rename changes
|
||||
* the name written into it), or the copy is missing. Never when the name belongs to a file of the user's,
|
||||
* nor when a [complete]-less listing doesn't show the copy: writing then would
|
||||
* add a duplicate beside it, so that is left to the scheduled run.
|
||||
* add a duplicate beside it, so that is left to the next run that isn't a change run.
|
||||
*/
|
||||
fun BackupFilePlan.needsRefresh(
|
||||
fingerprint: String,
|
||||
|
||||
@@ -296,15 +296,20 @@ class CalendarsViewModel @Inject constructor(
|
||||
|
||||
fun setAutoBackupOnChange(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val wasScheduled = settingsPrefs.autoBackupScheduled.first()
|
||||
settingsPrefs.setAutoBackupOnChange(enabled)
|
||||
reschedule()
|
||||
if (!wasScheduled && settingsPrefs.autoBackupScheduled.first()) runBackupIfActive()
|
||||
}
|
||||
}
|
||||
|
||||
/** Back on, it backs up right away rather than an interval later. */
|
||||
fun setAutoBackupScheduled(enabled: Boolean) {
|
||||
viewModelScope.launch {
|
||||
val wasScheduled = settingsPrefs.autoBackupScheduled.first()
|
||||
settingsPrefs.setAutoBackupScheduled(enabled)
|
||||
reschedule()
|
||||
if (!wasScheduled && settingsPrefs.autoBackupScheduled.first()) runBackupIfActive()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -962,6 +962,15 @@ class SettingsPrefsTest {
|
||||
assertThat(prefs.autoBackupScheduled.first()).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `switching the interval off without after-change backups leaves nothing behind`(@TempDir tempDir: Path) = runTest {
|
||||
val prefs = testSettings(tempDir)
|
||||
prefs.setAutoBackupScheduled(false)
|
||||
|
||||
prefs.setAutoBackupOnChange(true)
|
||||
assertThat(prefs.autoBackupScheduled.first()).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a pre-411 install adopts its old file when the layout is pinned in place`(@TempDir tempDir: Path) = runTest {
|
||||
val store = testDataStore(tempDir, "test_settings")
|
||||
|
||||
@@ -418,4 +418,14 @@ class BackupPlanTest {
|
||||
assertThat(backupRunLatestOnly(changeRun = false, intervalOn = false)).isTrue()
|
||||
assertThat(backupRunLatestOnly(changeRun = true, intervalOn = false)).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `without the interval a series whose stable name is the user's goes unbacked`() {
|
||||
val free = planBackupFiles(listOf(work), emptyMap(), emptySet(), at, keep = 3).single()
|
||||
val blocked = planBackupFiles(listOf(work), emptyMap(), setOf("Work.ics"), at, keep = 3).single()
|
||||
|
||||
assertThat(free.backedUp(intervalOn = false)).isTrue()
|
||||
assertThat(blocked.backedUp(intervalOn = false)).isFalse()
|
||||
assertThat(blocked.backedUp(intervalOn = true)).isTrue()
|
||||
}
|
||||
}
|
||||
@@ -644,9 +644,12 @@ the app, is left to the next scheduled run.
|
||||
|
||||
While the option is on, the interval itself can be switched off
|
||||
(`auto_backup_scheduled`, read through `backupIntervalOn`, so it never counts as
|
||||
off without the option): the periodic work is cancelled and every run, including
|
||||
run-now, only refreshes the stable copies, so no dated files are written or
|
||||
pruned. Turning the option off sets the interval back on, at the interval it had.
|
||||
off without the option): the periodic work is cancelled and no run writes or
|
||||
prunes dated files. Run-now then rewrites every stable copy, change runs only
|
||||
the changed ones, and a series whose stable name a file of the user's holds
|
||||
counts as failed, since nothing else backs it up. Turning the option off sets
|
||||
the interval back on, at the interval it had, and turning the interval back on
|
||||
either way backs up right away.
|
||||
|
||||
**A backup carries the series' deleted and moved occurrences.** `EXDATE` lives on
|
||||
the master row and is part of `EventExportProjection`; `IcsWriter` marks an
|
||||
|
||||
Reference in new issue
Block a user