Skip to content

Commit

Permalink
RcloneProvider: Only trigger background upload monitor for writable f…
Browse files Browse the repository at this point in the history
…iles

Read-only files do not use the VFS cache, so we were previously spawning
and immediately killing the service for no reason.

Signed-off-by: Andrew Gunnerson <[email protected]>
  • Loading branch information
chenxiaolong committed Oct 24, 2024
1 parent e7099dd commit c29e543
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/src/main/java/com/chiller3/rsaf/rclone/RcloneProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -488,12 +488,12 @@ class RcloneProvider : DocumentsProvider(), SharedPreferences.OnSharedPreference
(pfdMode and flags) == flags
}

var fcntlMode = if (pfdModeHasFlags(ParcelFileDescriptor.MODE_READ_WRITE)) {
OsConstants.O_RDWR
var (fcntlMode, isWrite) = if (pfdModeHasFlags(ParcelFileDescriptor.MODE_READ_WRITE)) {
OsConstants.O_RDWR to true
} else if (pfdModeHasFlags(ParcelFileDescriptor.MODE_WRITE_ONLY)) {
OsConstants.O_WRONLY
OsConstants.O_WRONLY to true
} else if (pfdModeHasFlags(ParcelFileDescriptor.MODE_READ_ONLY)) {
OsConstants.O_RDONLY
OsConstants.O_RDONLY to false
} else {
throw IllegalArgumentException("Invalid mode: $mode")
}
Expand All @@ -517,7 +517,7 @@ class RcloneProvider : DocumentsProvider(), SharedPreferences.OnSharedPreference

try {
return storageManager.openProxyFileDescriptor(
pfdMode, ProxyFd(documentId, handle), ioHandler)
pfdMode, ProxyFd(documentId, handle, isWrite), ioHandler)
} catch (e: Exception) {
Log.e(TAG, "Failed to open proxy file descriptor", e)
// openProxyFileDescriptor can throw an exception without invoking onRelease
Expand Down Expand Up @@ -735,8 +735,11 @@ class RcloneProvider : DocumentsProvider(), SharedPreferences.OnSharedPreference
}
}

private inner class ProxyFd(private val documentId: String, private val handle: RbFile) :
ProxyFileDescriptorCallback() {
private inner class ProxyFd(
private val documentId: String,
private val handle: RbFile,
private val isWrite: Boolean
) : ProxyFileDescriptorCallback() {
private fun debugLog(msg: String) {
this@RcloneProvider.debugLog("ProxyFd[$documentId].$msg")
}
Expand Down Expand Up @@ -794,7 +797,7 @@ class RcloneProvider : DocumentsProvider(), SharedPreferences.OnSharedPreference

val context = context!!

if (Permissions.isInhibitingBatteryOpt(context)) {
if (isWrite && Permissions.isInhibitingBatteryOpt(context)) {
context.startForegroundService(
BackgroundUploadMonitorService.createAddIntent(context, documentId),
)
Expand All @@ -810,7 +813,7 @@ class RcloneProvider : DocumentsProvider(), SharedPreferences.OnSharedPreference
notifications.notifyBackgroundUploadFailed(documentId, exception.toSingleLineString())
}

if (Permissions.isInhibitingBatteryOpt(context)) {
if (isWrite && Permissions.isInhibitingBatteryOpt(context)) {
context.startForegroundService(
BackgroundUploadMonitorService.createRemoveIntent(context, documentId),
)
Expand Down

0 comments on commit c29e543

Please sign in to comment.