Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 897b685c70 | |||
| 4b20697bb1 |
@@ -146,13 +146,15 @@ class SyncEngine @Inject constructor(
|
|||||||
storeLocalMtime = false))
|
storeLocalMtime = false))
|
||||||
}
|
}
|
||||||
SyncDecision.DELETE_LOCAL -> {
|
SyncDecision.DELETE_LOCAL -> {
|
||||||
accessor.delete(rel)
|
val deleted = accessor.delete(rel)
|
||||||
|
if (!deleted) Timber.w("SyncEngine: DELETE_LOCAL failed (silent) for $rel")
|
||||||
fileStateDao.delete(pair.id, rel)
|
fileStateDao.delete(pair.id, rel)
|
||||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "local", 0)
|
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "local", 0)
|
||||||
FileOutcome(deleted = 1)
|
FileOutcome(deleted = 1)
|
||||||
}
|
}
|
||||||
SyncDecision.DELETE_REMOTE -> {
|
SyncDecision.DELETE_REMOTE -> {
|
||||||
provider.deleteFile("${pair.remotePath}/$rel")
|
runCatching { provider.deleteFile("${pair.remotePath}/$rel") }
|
||||||
|
.onFailure { e -> Timber.e(e, "SyncEngine: DELETE_REMOTE failed for $rel") }
|
||||||
fileStateDao.delete(pair.id, rel)
|
fileStateDao.delete(pair.id, rel)
|
||||||
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "remote", 0)
|
logEvent(pair.id, SyncEventType.FILE_DELETED, rel, "remote", 0)
|
||||||
FileOutcome(deleted = 1)
|
FileOutcome(deleted = 1)
|
||||||
@@ -283,21 +285,15 @@ internal fun syncDecide(
|
|||||||
}
|
}
|
||||||
|
|
||||||
!localExists && remoteExists -> when {
|
!localExists && remoteExists -> when {
|
||||||
known == null -> if (!hasPriorSyncState) {
|
known == null -> {
|
||||||
// Initial sync: no history at all — remote files are new, download them.
|
// No state record: could be a new remote file OR a file whose state was lost.
|
||||||
|
// Downloading is always safer than deleting — if the user deleted the local
|
||||||
|
// copy intentionally, the state record will still exist (known != null) and
|
||||||
|
// the else-branch below correctly deletes the remote copy.
|
||||||
when (direction) {
|
when (direction) {
|
||||||
SyncDirection.DOWNLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.DOWNLOAD
|
SyncDirection.DOWNLOAD_ONLY, SyncDirection.TWO_WAY -> SyncDecision.DOWNLOAD
|
||||||
else -> SyncDecision.SKIP
|
else -> SyncDecision.SKIP
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Pair has been synced before but this file has no state record
|
|
||||||
// (e.g. uploaded before state-tracking was fixed). Treat the same
|
|
||||||
// as a known remote-deletion: apply mirror/keep behavior.
|
|
||||||
when {
|
|
||||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
|
||||||
direction == SyncDirection.UPLOAD_ONLY || direction == SyncDirection.TWO_WAY -> SyncDecision.DELETE_REMOTE
|
|
||||||
else -> SyncDecision.SKIP
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else -> when {
|
else -> when {
|
||||||
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
deleteBehavior == DeleteBehavior.KEEP -> SyncDecision.SKIP
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ import com.syncflow.MainActivity
|
|||||||
import com.syncflow.R
|
import com.syncflow.R
|
||||||
import com.syncflow.data.db.SyncFileStateDao
|
import com.syncflow.data.db.SyncFileStateDao
|
||||||
import com.syncflow.data.db.SyncPairDao
|
import com.syncflow.data.db.SyncPairDao
|
||||||
|
import com.syncflow.data.db.entities.toDomain
|
||||||
import com.syncflow.domain.model.ScheduleType
|
import com.syncflow.domain.model.ScheduleType
|
||||||
|
import com.syncflow.domain.sync.LocalAccessor
|
||||||
import dagger.hilt.android.AndroidEntryPoint
|
import dagger.hilt.android.AndroidEntryPoint
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
@@ -44,8 +46,10 @@ class FileWatchService : Service() {
|
|||||||
private val fileObservers = mutableMapOf<Long, MutableList<FileObserver>>()
|
private val fileObservers = mutableMapOf<Long, MutableList<FileObserver>>()
|
||||||
private val contentObservers = mutableMapOf<Long, ContentObserver>()
|
private val contentObservers = mutableMapOf<Long, ContentObserver>()
|
||||||
private val debounceJobs = mutableMapOf<Long, Job>()
|
private val debounceJobs = mutableMapOf<Long, Job>()
|
||||||
// After a watcher-triggered sync completes, suppress FileObserver events for this long
|
// Persistent monitors that watch WorkManager for ANY sync (manual, catchup, onchange)
|
||||||
// to stop the feedback loop: sync writes files → FileObserver fires → another sync → repeat.
|
// so the cooldown is set regardless of who triggered the sync.
|
||||||
|
private val syncMonitorJobs = mutableMapOf<Long, Job>()
|
||||||
|
// After a sync completes, suppress FileObserver events for this long.
|
||||||
private val syncCooldownUntil = mutableMapOf<Long, Long>()
|
private val syncCooldownUntil = mutableMapOf<Long, Long>()
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -154,9 +158,36 @@ class FileWatchService : Service() {
|
|||||||
syncCooldownUntil[pairId] = System.currentTimeMillis() + 15_000
|
syncCooldownUntil[pairId] = System.currentTimeMillis() + 15_000
|
||||||
watchDirRecursive(dir, pairId, wifiOnly, chargingOnly)
|
watchDirRecursive(dir, pairId, wifiOnly, chargingOnly)
|
||||||
Timber.d("FileWatchService: watching pair $pairId at $path (${fileObservers[pairId]?.size} dirs)")
|
Timber.d("FileWatchService: watching pair $pairId at $path (${fileObservers[pairId]?.size} dirs)")
|
||||||
|
startSyncMonitor(pairId)
|
||||||
scope.launch { catchupScan(pairId, dir, wifiOnly, chargingOnly) }
|
scope.launch { catchupScan(pairId, dir, wifiOnly, chargingOnly) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Watches WorkManager for ANY sync tagged sync_$pairId (manual, catchup, onchange).
|
||||||
|
// Sets cooldown while running and for 60s after, so FileObserver events from our
|
||||||
|
// own file writes never trigger a re-sync regardless of what started the sync.
|
||||||
|
private fun startSyncMonitor(pairId: Long) {
|
||||||
|
syncMonitorJobs[pairId]?.cancel()
|
||||||
|
syncMonitorJobs[pairId] = scope.launch {
|
||||||
|
var wasSyncing = false
|
||||||
|
WorkManager.getInstance(applicationContext)
|
||||||
|
.getWorkInfosByTagFlow("sync_$pairId")
|
||||||
|
.collect { infos ->
|
||||||
|
val isSyncing = infos.any {
|
||||||
|
it.state == WorkInfo.State.RUNNING || it.state == WorkInfo.State.ENQUEUED
|
||||||
|
}
|
||||||
|
if (isSyncing) {
|
||||||
|
Timber.d("FileWatchService: sync active for pair $pairId — cooldown extended")
|
||||||
|
syncCooldownUntil[pairId] = System.currentTimeMillis() + 120_000
|
||||||
|
wasSyncing = true
|
||||||
|
} else if (wasSyncing) {
|
||||||
|
Timber.d("FileWatchService: sync finished for pair $pairId — 60s settle cooldown")
|
||||||
|
syncCooldownUntil[pairId] = System.currentTimeMillis() + 60_000
|
||||||
|
wasSyncing = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun watchDirRecursive(dir: File, pairId: Long, wifiOnly: Boolean, chargingOnly: Boolean) {
|
private fun watchDirRecursive(dir: File, pairId: Long, wifiOnly: Boolean, chargingOnly: Boolean) {
|
||||||
if (!dir.isDirectory) return
|
if (!dir.isDirectory) return
|
||||||
val mask = FileObserver.CREATE or FileObserver.DELETE or FileObserver.MODIFY or
|
val mask = FileObserver.CREATE or FileObserver.DELETE or FileObserver.MODIFY or
|
||||||
@@ -195,21 +226,25 @@ class FileWatchService : Service() {
|
|||||||
val known = fileStateDao.getForPair(pairId).associateBy { it.relativePath }
|
val known = fileStateDao.getForPair(pairId).associateBy { it.relativePath }
|
||||||
if (known.isEmpty()) return // Never synced — first sync will be triggered manually
|
if (known.isEmpty()) return // Never synced — first sync will be triggered manually
|
||||||
|
|
||||||
val current = mutableMapOf<String, Long>()
|
val pairEntity = syncPairDao.getById(pairId) ?: return
|
||||||
dir.walk().filter { it.isFile }.forEach { f ->
|
val pair = pairEntity.toDomain()
|
||||||
current[f.relativeTo(dir).path.replace('\\', '/')] = f.lastModified()
|
// Use the same accessor + filters as SyncEngine so hidden/excluded/size-filtered files
|
||||||
}
|
// don't appear as "new" in the catchup scan and trigger a perpetual sync loop.
|
||||||
|
val accessor = if (pair.localPath.startsWith("content://"))
|
||||||
|
LocalAccessor.Saf(Uri.parse(pair.localPath), contentResolver)
|
||||||
|
else
|
||||||
|
LocalAccessor.JavaFile(dir)
|
||||||
|
val current = accessor.walkFiles(pair)
|
||||||
|
|
||||||
val hasNew = current.any { (rel, _) -> rel !in known }
|
val hasNew = current.any { (rel, _) -> rel !in known }
|
||||||
val hasModified = current.any { (rel, mtime) ->
|
val hasModified = current.any { (rel, info) ->
|
||||||
val s = known[rel]; s != null && s.localModifiedAt != null &&
|
val s = known[rel]; s != null && s.localModifiedAt != null &&
|
||||||
s.localModifiedAt.toEpochMilli() != mtime
|
s.localModifiedAt.epochSecond != info.lastModifiedMs / 1000
|
||||||
}
|
}
|
||||||
val hasDeleted = known.keys.any { rel -> rel !in current }
|
val hasDeleted = known.keys.any { rel -> rel !in current }
|
||||||
|
|
||||||
if (hasNew || hasModified || hasDeleted) {
|
if (hasNew || hasModified || hasDeleted) {
|
||||||
Timber.d("FileWatchService: catchup detected changes for pair $pairId, scheduling sync")
|
Timber.d("FileWatchService: catchup detected changes for pair $pairId, scheduling sync")
|
||||||
val pair = syncPairDao.getById(pairId) ?: return
|
|
||||||
// Cancel any debounce that started before our startup cooldown was set
|
// Cancel any debounce that started before our startup cooldown was set
|
||||||
debounceJobs[pairId]?.cancel()
|
debounceJobs[pairId]?.cancel()
|
||||||
debounceJobs.remove(pairId)
|
debounceJobs.remove(pairId)
|
||||||
@@ -297,6 +332,8 @@ class FileWatchService : Service() {
|
|||||||
contentObservers.clear()
|
contentObservers.clear()
|
||||||
debounceJobs.values.forEach { it.cancel() }
|
debounceJobs.values.forEach { it.cancel() }
|
||||||
debounceJobs.clear()
|
debounceJobs.clear()
|
||||||
|
syncMonitorJobs.values.forEach { it.cancel() }
|
||||||
|
syncMonitorJobs.clear()
|
||||||
syncCooldownUntil.clear()
|
syncCooldownUntil.clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,2 +1,2 @@
|
|||||||
VERSION_NAME=1.0.31
|
VERSION_NAME=1.0.37
|
||||||
VERSION_CODE=32
|
VERSION_CODE=38
|
||||||
|
|||||||
Reference in New Issue
Block a user