v1.0.1 — Fix SAF content URI access and foreground service type
- SyncEngine now handles content:// URIs via ContentResolver/DocumentsContract alongside regular file paths; fixes ENOENT on all SAF-backed sync pairs - ForegroundInfo now passes FOREGROUND_SERVICE_TYPE_DATA_SYNC on API 29+ - Declare foregroundServiceType=dataSync on WorkManager service in manifest Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package com.syncflow.domain.sync
|
||||
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import com.syncflow.data.db.SyncConflictDao
|
||||
import com.syncflow.data.db.SyncEventDao
|
||||
import com.syncflow.data.db.SyncFileStateDao
|
||||
import com.syncflow.data.db.SyncPairDao
|
||||
import com.syncflow.data.db.entities.SyncConflictEntity
|
||||
import com.syncflow.data.db.entities.SyncEventEntity
|
||||
import com.syncflow.data.db.entities.SyncFileStateEntity
|
||||
import com.syncflow.data.providers.CloudProvider
|
||||
import com.syncflow.domain.model.ConflictStrategy
|
||||
import com.syncflow.domain.model.DeleteBehavior
|
||||
@@ -15,12 +16,12 @@ import com.syncflow.domain.model.SyncDirection
|
||||
import com.syncflow.domain.model.SyncEventType
|
||||
import com.syncflow.domain.model.SyncPair
|
||||
import com.syncflow.domain.model.SyncStatus
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.sync.Semaphore
|
||||
import kotlinx.coroutines.sync.withPermit
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import java.security.MessageDigest
|
||||
import java.time.Instant
|
||||
import javax.inject.Inject
|
||||
|
||||
@@ -29,6 +30,7 @@ class SyncEngine @Inject constructor(
|
||||
private val fileStateDao: SyncFileStateDao,
|
||||
private val conflictDao: SyncConflictDao,
|
||||
private val eventDao: SyncEventDao,
|
||||
@ApplicationContext private val context: Context,
|
||||
) {
|
||||
suspend fun sync(pair: SyncPair, provider: CloudProvider): SyncResult {
|
||||
syncPairDao.updateStatus(pair.id, SyncStatus.SYNCING)
|
||||
@@ -53,20 +55,26 @@ class SyncEngine @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeAccessor(localPath: String): LocalAccessor =
|
||||
if (localPath.startsWith("content://"))
|
||||
LocalAccessor.Saf(Uri.parse(localPath), context.contentResolver)
|
||||
else
|
||||
LocalAccessor.JavaFile(File(localPath))
|
||||
|
||||
private suspend fun performSync(pair: SyncPair, provider: CloudProvider): SyncResult {
|
||||
val localRoot = File(pair.localPath)
|
||||
val accessor = makeAccessor(pair.localPath)
|
||||
val knownStates = fileStateDao.getForPair(pair.id).associateBy { it.relativePath }
|
||||
val remoteFiles = provider.listFiles(pair.remotePath).getOrThrow().associateBy { it.path.removePrefix(pair.remotePath).trimStart('/') }
|
||||
val localFiles = localRoot.walkFiles(pair)
|
||||
val remoteFiles = provider.listFiles(pair.remotePath).getOrThrow()
|
||||
.associateBy { it.path.removePrefix(pair.remotePath).trimStart('/') }
|
||||
val localFiles = accessor.walkFiles(pair)
|
||||
|
||||
var uploaded = 0; var downloaded = 0; var deleted = 0; var skipped = 0; var failed = 0; var conflicts = 0
|
||||
var bytesTransferred = 0L
|
||||
val newStates = mutableListOf<SyncFileStateEntity>()
|
||||
val newStates = mutableListOf<com.syncflow.data.db.entities.SyncFileStateEntity>()
|
||||
|
||||
val allPaths = (localFiles.keys + remoteFiles.keys + knownStates.keys).toSet()
|
||||
|
||||
// Fan out with bounded parallelism
|
||||
val semaphore = Semaphore(4)
|
||||
|
||||
coroutineScope {
|
||||
allPaths.map { rel ->
|
||||
async {
|
||||
@@ -78,12 +86,11 @@ class SyncEngine @Inject constructor(
|
||||
|
||||
when (decision) {
|
||||
SyncDecision.UPLOAD -> {
|
||||
val file = File(localRoot, rel)
|
||||
val bytes = runCatching {
|
||||
file.inputStream().use { stream ->
|
||||
provider.uploadFile(stream, "${pair.remotePath}/$rel", file.length()) { }
|
||||
accessor.openInputStream(rel)?.use { stream ->
|
||||
provider.uploadFile(stream, "${pair.remotePath}/$rel", local!!.sizeBytes) { }
|
||||
}
|
||||
file.length()
|
||||
local!!.sizeBytes
|
||||
}.getOrElse { e ->
|
||||
Timber.e(e, "Upload failed: $rel")
|
||||
failed++
|
||||
@@ -92,13 +99,12 @@ class SyncEngine @Inject constructor(
|
||||
}
|
||||
uploaded++
|
||||
bytesTransferred += bytes
|
||||
newStates += buildState(pair.id, rel, file, remote)
|
||||
newStates += buildState(pair.id, rel, local!!, remote)
|
||||
logEvent(pair.id, SyncEventType.FILE_UPLOADED, rel, null, bytes)
|
||||
}
|
||||
SyncDecision.DOWNLOAD -> {
|
||||
val dest = File(localRoot, rel).also { it.parentFile?.mkdirs() }
|
||||
val bytes = runCatching {
|
||||
dest.outputStream().use { stream ->
|
||||
accessor.createOutputStream(rel)?.use { stream ->
|
||||
provider.downloadFile("${pair.remotePath}/$rel", stream) { }
|
||||
}
|
||||
remote!!.sizeBytes
|
||||
@@ -110,11 +116,11 @@ class SyncEngine @Inject constructor(
|
||||
}
|
||||
downloaded++
|
||||
bytesTransferred += bytes
|
||||
newStates += buildState(pair.id, rel, dest, remote)
|
||||
newStates += buildState(pair.id, rel, null, remote)
|
||||
logEvent(pair.id, SyncEventType.FILE_DOWNLOADED, rel, null, bytes)
|
||||
}
|
||||
SyncDecision.DELETE_LOCAL -> {
|
||||
File(localRoot, rel).delete()
|
||||
accessor.delete(rel)
|
||||
fileStateDao.delete(pair.id, rel)
|
||||
deleted++
|
||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "local", 0)
|
||||
@@ -130,8 +136,8 @@ class SyncEngine @Inject constructor(
|
||||
conflictDao.insert(SyncConflictEntity(
|
||||
syncPairId = pair.id,
|
||||
relativePath = rel,
|
||||
localModifiedAt = local?.lastModified()?.let { Instant.ofEpochMilli(it) } ?: Instant.EPOCH,
|
||||
localSizeBytes = local?.length() ?: 0L,
|
||||
localModifiedAt = local?.lastModifiedMs?.let { Instant.ofEpochMilli(it) } ?: Instant.EPOCH,
|
||||
localSizeBytes = local?.sizeBytes ?: 0L,
|
||||
remoteModifiedAt = remote?.modifiedAt ?: Instant.EPOCH,
|
||||
remoteSizeBytes = remote?.sizeBytes ?: 0L,
|
||||
resolution = null,
|
||||
@@ -154,26 +160,24 @@ class SyncEngine @Inject constructor(
|
||||
direction: SyncDirection,
|
||||
conflictStrategy: ConflictStrategy,
|
||||
deleteBehavior: DeleteBehavior,
|
||||
local: File?,
|
||||
local: LocalFileInfo?,
|
||||
remote: RemoteFile?,
|
||||
known: SyncFileStateEntity?,
|
||||
known: com.syncflow.data.db.entities.SyncFileStateEntity?,
|
||||
): SyncDecision {
|
||||
val localExists = local?.exists() == true
|
||||
val localExists = local != null
|
||||
val remoteExists = remote != null
|
||||
|
||||
val localChanged = known == null || (localExists && local!!.lastModified() != known.localModifiedAt?.toEpochMilli())
|
||||
val localChanged = known == null || (localExists && local!!.lastModifiedMs != known.localModifiedAt?.toEpochMilli())
|
||||
val remoteChanged = known == null || (remoteExists && remote!!.etag != known.remoteEtag && remote.modifiedAt != known.remoteModifiedAt)
|
||||
|
||||
return when {
|
||||
!localExists && !remoteExists -> SyncDecision.SKIP
|
||||
|
||||
// File only exists locally
|
||||
localExists && !remoteExists -> when {
|
||||
known == null -> when (direction) {
|
||||
SyncDirection.UPLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.UPLOAD
|
||||
else -> SyncDecision.SKIP
|
||||
}
|
||||
// Remote was deleted — respect deleteBehavior
|
||||
else -> when {
|
||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
||||
direction == SyncDirection.DOWNLOAD_ONLY || direction == SyncDirection.TWO_WAY -> SyncDecision.DELETE_LOCAL
|
||||
@@ -181,13 +185,11 @@ class SyncEngine @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
// File only exists remotely
|
||||
!localExists && remoteExists -> when {
|
||||
known == null -> when (direction) {
|
||||
SyncDirection.DOWNLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.DOWNLOAD
|
||||
else -> SyncDecision.SKIP
|
||||
}
|
||||
// Local was deleted — respect deleteBehavior
|
||||
else -> when {
|
||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
||||
direction == SyncDirection.UPLOAD_ONLY || direction == SyncDirection.TWO_WAY -> SyncDecision.DELETE_REMOTE
|
||||
@@ -195,16 +197,15 @@ class SyncEngine @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
// Both changed — conflict
|
||||
localChanged && remoteChanged -> when (direction) {
|
||||
SyncDirection.UPLOAD_ONLY -> SyncDecision.UPLOAD
|
||||
SyncDirection.DOWNLOAD_ONLY -> SyncDecision.DOWNLOAD
|
||||
SyncDirection.TWO_WAY -> when (conflictStrategy) {
|
||||
ConflictStrategy.KEEP_LOCAL -> SyncDecision.UPLOAD
|
||||
ConflictStrategy.KEEP_REMOTE -> SyncDecision.DOWNLOAD
|
||||
ConflictStrategy.KEEP_NEWEST -> if ((local?.lastModified() ?: 0L) >= (remote?.modifiedAt?.toEpochMilli() ?: 0L)) SyncDecision.UPLOAD else SyncDecision.DOWNLOAD
|
||||
ConflictStrategy.KEEP_LARGEST -> if ((local?.length() ?: 0L) >= (remote?.sizeBytes ?: 0L)) SyncDecision.UPLOAD else SyncDecision.DOWNLOAD
|
||||
ConflictStrategy.KEEP_BOTH -> SyncDecision.CONFLICT // engine keeps both via rename
|
||||
ConflictStrategy.KEEP_NEWEST -> if ((local?.lastModifiedMs ?: 0L) >= (remote?.modifiedAt?.toEpochMilli() ?: 0L)) SyncDecision.UPLOAD else SyncDecision.DOWNLOAD
|
||||
ConflictStrategy.KEEP_LARGEST -> if ((local?.sizeBytes ?: 0L) >= (remote?.sizeBytes ?: 0L)) SyncDecision.UPLOAD else SyncDecision.DOWNLOAD
|
||||
ConflictStrategy.KEEP_BOTH -> SyncDecision.CONFLICT
|
||||
ConflictStrategy.ASK -> SyncDecision.CONFLICT
|
||||
}
|
||||
}
|
||||
@@ -221,11 +222,16 @@ class SyncEngine @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildState(pairId: Long, rel: String, local: File, remote: RemoteFile?) = SyncFileStateEntity(
|
||||
private fun buildState(
|
||||
pairId: Long,
|
||||
rel: String,
|
||||
local: LocalFileInfo?,
|
||||
remote: RemoteFile?,
|
||||
) = com.syncflow.data.db.entities.SyncFileStateEntity(
|
||||
syncPairId = pairId,
|
||||
relativePath = rel,
|
||||
localModifiedAt = if (local.exists()) Instant.ofEpochMilli(local.lastModified()) else null,
|
||||
localSizeBytes = local.length(),
|
||||
localModifiedAt = local?.lastModifiedMs?.let { Instant.ofEpochMilli(it) },
|
||||
localSizeBytes = local?.sizeBytes ?: 0L,
|
||||
localHash = null,
|
||||
remoteModifiedAt = remote?.modifiedAt,
|
||||
remoteSizeBytes = remote?.sizeBytes ?: 0L,
|
||||
@@ -237,31 +243,6 @@ class SyncEngine @Inject constructor(
|
||||
private suspend fun logEvent(pairId: Long, type: SyncEventType, file: String?, msg: String?, bytes: Long) {
|
||||
eventDao.insert(SyncEventEntity(syncPairId = pairId, timestamp = Instant.now(), eventType = type, filePath = file, message = msg, bytesTransferred = bytes))
|
||||
}
|
||||
|
||||
private fun File.walkFiles(pair: SyncPair): Map<String, File> {
|
||||
if (!exists()) return emptyMap()
|
||||
val includeExts = pair.includeExtensions.map { it.lowercase().trimStart('.') }.toSet()
|
||||
val excludeExts = pair.excludeExtensions.map { it.lowercase().trimStart('.') }.toSet()
|
||||
val minBytes = pair.minFileSizeKb * 1024
|
||||
val maxBytes = if (pair.maxFileSizeKb > 0) pair.maxFileSizeKb * 1024 else Long.MAX_VALUE
|
||||
|
||||
return walkTopDown()
|
||||
.onEnter { dir ->
|
||||
pair.recursive || dir == this
|
||||
}
|
||||
.filter { it.isFile }
|
||||
.filter { f -> !pair.skipHiddenFiles || !f.name.startsWith('.') }
|
||||
.filter { f -> pair.excludePatterns.none { pat -> f.name.matches(pat.toGlob()) } }
|
||||
.filter { f ->
|
||||
val ext = f.extension.lowercase()
|
||||
(includeExts.isEmpty() || ext in includeExts) && ext !in excludeExts
|
||||
}
|
||||
.filter { f -> f.length() >= minBytes && f.length() <= maxBytes }
|
||||
.associate { f -> f.relativeTo(this).path to f }
|
||||
}
|
||||
|
||||
private fun String.toGlob(): Regex =
|
||||
Regex(replace(".", "\\.").replace("*", ".*").replace("?", "."), RegexOption.IGNORE_CASE)
|
||||
}
|
||||
|
||||
enum class SyncDecision { UPLOAD, DOWNLOAD, DELETE_LOCAL, DELETE_REMOTE, CONFLICT, SKIP }
|
||||
|
||||
Reference in New Issue
Block a user