fix(#147, #411): review round 4 - settings read under the run lock, ledger saved before pruning, SAF helpers moved to floret-kit

This commit is contained in:
makiolaj committed 2026-11-13 16:54:48 +01:00
1 parent f314761e45
commit d86a09b744
9 files changed
+70 -117

No files matched your search

@@ -31,9 +31,11 @@ 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.NonCancellable
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import java.time.LocalDateTime
import java.util.concurrent.TimeUnit
import kotlin.coroutines.cancellation.CancellationException
@@ -117,21 +119,22 @@ class BackupWorker(
override suspend fun doWork(): Result {
val deps = EntryPointAccessors.fromApplication(applicationContext, Deps::class.java)
val prefs = deps.settingsPrefs()
// Respect the toggle even for already-queued work: if backup was turned
// off, no-op (and don't retry) so a lingering run can't revive itself.
if (!prefs.autoBackupEnabled.first()) return Result.success()
val folder = prefs.autoBackupFolderUri.first()
?: return Result.failure() // nothing to write to — leave scheduling to settings
val now = System.currentTimeMillis()
val outcome = try {
// 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) {
Log.w(TAG, "Automatic backup failed", e)
RunOutcome.Failed
// The periodic and the immediate run are separate unique works; one at
// a time, or each would overwrite the other's ledger.
val outcome = runLock.withLock {
// Respect the toggle even for already-queued work, so a lingering
// run can't revive itself.
if (!prefs.autoBackupEnabled.first()) return Result.success()
val folder = prefs.autoBackupFolderUri.first() ?: return Result.failure()
try {
runBackup(deps, prefs, folder.toUri())
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.w(TAG, "Automatic backup failed", e)
RunOutcome.Failed
}
}
if (outcome == RunOutcome.NothingSelected) return Result.success()
val success = outcome == RunOutcome.Written
@@ -149,9 +152,8 @@ class BackupWorker(
private suspend fun runBackup(deps: Deps, prefs: SettingsPrefs, folder: Uri): RunOutcome {
val repository = deps.repository()
val exporter = deps.exporter()
// Without the calendar permission the list comes back empty.
if (!applicationContext.hasPermission(Manifest.permission.READ_CALENDAR)) return RunOutcome.Failed
val all = repository.calendars().first()
if (all.isEmpty()) return RunOutcome.Failed
val selection = prefs.autoBackupSelection.first()
val targets = backupTargets(all.filter(selection::selects), combined = prefs.autoBackupCombined.first())
if (targets.isEmpty()) return RunOutcome.NothingSelected
@@ -167,25 +169,30 @@ class BackupWorker(
)
val stamp = Clock.System.now()
val liveKeys = all.map { it.id.toString() }.toSet()
val writtenNames = mutableMapOf<String, String>()
var written = 0
for (plan in plans) {
try {
val target = plan.target
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)
// Not cancellable once the file exists: it must reach the
// ledger, or nothing would ever prune it.
withContext(NonCancellable) {
val name = exporter.writeToFolder(folder, listing, plan.fileName, content)
ledger = updatedBackupLedger(ledger, plan, name, undeleted = plan.prune, liveKeys)
prefs.setAutoBackupLedger(ledger)
val undeleted = exporter.deleteFromFolder(listing, plan.prune)
ledger = updatedBackupLedger(ledger, plan, name, undeleted, liveKeys)
prefs.setAutoBackupLedger(ledger)
}
written++
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.w(TAG, "Backup of one series failed", e)
}
}
return when (writtenNames.size) {
return when (written) {
plans.size -> RunOutcome.Written
0 -> RunOutcome.Failed
else -> RunOutcome.PartlyFailed
@@ -821,7 +821,6 @@ class AndroidCalendarDataSource @Inject constructor(
} ?: emptyList()
}
override fun existingUids(calendarId: Long): Set<String> = resolver.query(
CalendarContract.Events.CONTENT_URI,
arrayOf(CalendarContract.Events.UID_2445),
@@ -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 = 500
private const val EVENT_ID_BATCH = 50
/**
* Reminder minutes of [eventIds], keyed by event, a batched query at a time
@@ -2,10 +2,13 @@ package de.jeanlucmakiola.calendula.data.ics
import android.content.Context
import android.net.Uri
import android.provider.DocumentsContract
import androidx.core.content.FileProvider
import androidx.documentfile.provider.DocumentFile
import dagger.hilt.android.qualifiers.ApplicationContext
import de.jeanlucmakiola.floret.android.TreeListing
import de.jeanlucmakiola.floret.android.deleteTreeFiles
import de.jeanlucmakiola.floret.android.listTreeFiles
import de.jeanlucmakiola.floret.android.writeTreeFile
import java.io.File
import java.io.IOException
import javax.inject.Inject
@@ -30,66 +33,25 @@ class IcsExporter @Inject constructor(
} ?: throw IOException("Could not open output stream for export (scheme=${uri.scheme})")
}
/**
* The files directly inside the persisted SAF tree [folder] (the
* automatic-backup destination), by display name — one provider query, so
* 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): FolderListing {
/** The files in the persisted SAF tree [folder], the automatic-backup destination. Throws when unusable. */
fun listFolder(folder: Uri): TreeListing {
writableFolder(folder)
val treeId = DocumentsContract.getTreeDocumentId(folder)
val children = DocumentsContract.buildChildDocumentsUriUsingTree(folder, treeId)
val columns = arrayOf(
DocumentsContract.Document.COLUMN_DOCUMENT_ID,
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
DocumentsContract.Document.COLUMN_MIME_TYPE,
return context.contentResolver.listTreeFiles(folder)
}
/** Write [content] as [fileName] into [folder]; returns the name the file ended up with. */
fun writeToFolder(folder: Uri, listing: TreeListing, fileName: String, content: String): String =
context.contentResolver.writeTreeFile(
tree = folder,
listing = listing,
name = fileName,
mimeType = MIME_CALENDAR,
bytes = content.toByteArray(Charsets.UTF_8),
)
val cursor = context.contentResolver.query(children, columns, null, null, null)
?: throw IOException("Backup folder can't be listed")
return cursor.use { c ->
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))
}
}
/**
* Write [content] to [fileName] in [folder], overwriting the file of that
* 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: FolderListing, fileName: String, content: String): String {
listing.files[fileName]?.let { uri ->
writeDocument(uri, content)
return fileName
}
val uri = DocumentsContract.createDocument(
context.contentResolver,
DocumentsContract.buildDocumentUriUsingTree(folder, DocumentsContract.getTreeDocumentId(folder)),
MIME_CALENDAR,
fileName,
) ?: throw IOException("Could not create backup file in the chosen folder")
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 didn't go, or weren't listed. */
fun deleteFromFolder(listing: FolderListing, names: Collection<String>): Set<String> =
names.filterTo(mutableSetOf()) { name ->
val uri = listing.files[name] ?: return@filterTo true
!runCatching { DocumentsContract.deleteDocument(context.contentResolver, uri) }.getOrDefault(false)
}
/** Delete [names] from the listed folder; returns the ones that didn't go. */
fun deleteFromFolder(listing: TreeListing, names: Collection<String>): Set<String> =
context.contentResolver.deleteTreeFiles(listing, names)
private fun writableFolder(folder: Uri): DocumentFile {
val dir = DocumentFile.fromTreeUri(context, folder)
@@ -117,9 +79,3 @@ 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,8 +351,6 @@ 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
}
}
@@ -169,24 +169,20 @@ fun planBackupFiles(
}
/**
* The ledger after a run: each written series records what it kept, what it
* failed to delete ([undeleted]) and the file it just wrote ([writtenNames],
* by key, as the provider named it). Series not written this run keep their
* entry while their calendar still exists ([liveKeys]), so a calendar taken
* out of the backup still has its files pruned if it's ever put back.
* The ledger once [plan]'s new file landed as [writtenName]: the series records
* what it kept, what it failed to delete ([undeleted]) and the new file. Other
* series keep their entry while their calendar still exists ([liveKeys]), so a
* calendar taken out of the backup still has its files pruned if it's put back.
*/
fun updatedBackupLedger(
previous: BackupLedger,
written: List<BackupFilePlan>,
writtenNames: Map<String, String>,
undeleted: Set<String>,
plan: BackupFilePlan,
writtenName: String,
undeleted: Collection<String>,
liveKeys: Set<String>,
): BackupLedger {
val next = previous.filterKeys { it in liveKeys || it == COMBINED_SERIES }.toMutableMap()
written.forEach { plan ->
val name = writtenNames[plan.target.key] ?: return@forEach
next[plan.target.key] = plan.prune.filter { it in undeleted } + plan.keep + name
}
next[plan.target.key] = (plan.prune.filter { it in undeleted } + plan.keep + writtenName).distinct()
return next.filterValues { it.isNotEmpty() }
}
@@ -7,6 +7,7 @@ import de.jeanlucmakiola.floret.reminders.ReminderOverride
import androidx.compose.ui.unit.dp
import androidx.datastore.preferences.core.stringPreferencesKey
import com.google.common.truth.Truth.assertThat
import de.jeanlucmakiola.calendula.domain.CalendarSource
import de.jeanlucmakiola.calendula.domain.EventFormField
import de.jeanlucmakiola.calendula.domain.FontRole
import de.jeanlucmakiola.calendula.domain.contacts.SpecialDateType
@@ -705,6 +706,7 @@ class SettingsPrefsTest {
prefs.recordSpecialDatesRun(2000L, null)
assertThat(prefs.specialDatesStatus.first()).isEqualTo(SpecialDatesStatus(2000L, null))
}
@Test
fun `a fresh install backs up one file per calendar, keeping three`(@TempDir tempDir: Path) = runTest {
val prefs = testSettings(tempDir)
@@ -757,7 +759,7 @@ class SettingsPrefsTest {
assertThat(prefs.autoBackupKeep.first()).isEqualTo(10)
}
private fun backupCalendar(id: Long, local: Boolean) = de.jeanlucmakiola.calendula.domain.CalendarSource(
private fun backupCalendar(id: Long, local: Boolean) = CalendarSource(
id = id,
displayName = "C$id",
accountName = "a",
@@ -156,8 +156,8 @@ class BackupPlanTest {
).single()
val next = updatedBackupLedger(
previous = mapOf("1" to listOf("Work_a.ics", "Work_b.ics"), "2" to listOf("Home_a.ics"), "7" to listOf("Gone.ics")),
written = listOf(plan),
writtenNames = mapOf("1" to "Work_new (1).ics"),
plan = plan,
writtenName = "Work_new (1).ics",
undeleted = setOf("Work_a.ics"),
liveKeys = setOf("1", "2"),
)
@@ -168,16 +168,11 @@ class BackupPlanTest {
}
@Test
fun `a series whose write failed keeps its old entry`() {
fun `a pruned file leaves the ledger once it is deleted`() {
val plan = planBackupFiles(listOf(work), mapOf("1" to listOf("Work_a.ics")), setOf("Work_a.ics"), at, keep = 1)
val next = updatedBackupLedger(
previous = mapOf("1" to listOf("Work_a.ics")),
written = plan,
writtenNames = emptyMap(),
undeleted = emptySet(),
liveKeys = setOf("1"),
)
assertThat(next).containsExactly("1", listOf("Work_a.ics"))
.single()
val next = updatedBackupLedger(mapOf("1" to listOf("Work_a.ics")), plan, "Work_new.ics", emptySet(), setOf("1"))
assertThat(next).containsExactly("1", listOf("Work_new.ics"))
}
@Test