Skip to content

Commit

Permalink
Add debug option to add an alias remote for rclone's internal cache d…
Browse files Browse the repository at this point in the history
…irectory

This makes it easy to inspect rclone's vfsMeta directory, which contains
JSON files describing the state of pending uploads.

Signed-off-by: Andrew Gunnerson <accounts+github@chiller3.com>
  • Loading branch information
chenxiaolong committed Feb 16, 2025
1 parent aa0cf67 commit 1e5668c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/chiller3/rsaf/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ class Preferences(private val context: Context) {
const val PREF_EDIT_REMOTE_PREFIX = "edit_remote_"
const val PREF_IMPORT_CONFIGURATION = "import_configuration"
const val PREF_EXPORT_CONFIGURATION = "export_configuration"
const val PREF_SAVE_LOGS = "save_logs"
const val PREF_VERSION = "version"
const val PREF_SAVE_LOGS = "save_logs"
const val PREF_ADD_INTERNAL_CACHE_REMOTE = "add_internal_cache_remote"

// Edit remote UI actions
const val PREF_OPEN_REMOTE = "open_remote"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class SettingsFragment : PreferenceBaseFragment(), FragmentResultListener,
private lateinit var prefLockNow: Preference
private lateinit var prefVersion: LongClickablePreference
private lateinit var prefSaveLogs: Preference
private lateinit var prefAddInternalCacheRemote: Preference

private val requestEditRemote =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
Expand Down Expand Up @@ -169,6 +170,9 @@ class SettingsFragment : PreferenceBaseFragment(), FragmentResultListener,
prefSaveLogs = findPreference(Preferences.PREF_SAVE_LOGS)!!
prefSaveLogs.onPreferenceClickListener = this

prefAddInternalCacheRemote = findPreference(Preferences.PREF_ADD_INTERNAL_CACHE_REMOTE)!!
prefAddInternalCacheRemote.onPreferenceClickListener = this

// Call this once first to avoid UI jank from elements shifting. We call it again in
// onResume() because allowing the permissions does not restart the activity.
refreshPermissions()
Expand Down Expand Up @@ -424,6 +428,10 @@ class SettingsFragment : PreferenceBaseFragment(), FragmentResultListener,
requestSafSaveLogs.launch(Logcat.FILENAME_DEFAULT)
return true
}
preference === prefAddInternalCacheRemote -> {
viewModel.addInternalCacheRemote()
return true
}
}

return false
Expand Down
34 changes: 31 additions & 3 deletions app/src/main/java/com/chiller3/rsaf/settings/SettingsViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* SPDX-FileCopyrightText: 2023-2024 Andrew Gunnerson
* SPDX-FileCopyrightText: 2023-2025 Andrew Gunnerson
* SPDX-License-Identifier: GPL-3.0-only
*/

package com.chiller3.rsaf.settings

import android.app.Application
import android.net.Uri
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.chiller3.rsaf.Logcat
import com.chiller3.rsaf.rclone.RcloneConfig
Expand All @@ -19,6 +20,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File

data class Remote(
val name: String,
Expand Down Expand Up @@ -47,9 +49,11 @@ data class SettingsActivityActions(
val refreshRoots: Boolean,
)

class SettingsViewModel : ViewModel() {
class SettingsViewModel(application: Application) : AndroidViewModel(application) {
companion object {
private val TAG = SettingsViewModel::class.java.simpleName

private const val INTERNAL_CACHE_REMOTE_NAME = "rclone_internal_cache"
}

private val _remotes = MutableStateFlow<List<Remote>>(emptyList())
Expand Down Expand Up @@ -234,6 +238,30 @@ class SettingsViewModel : ViewModel() {
}
}

fun addInternalCacheRemote() {
viewModelScope.launch {
withContext(Dispatchers.IO) {
val cacheDir = File(getApplication<Application>().cacheDir, "rclone").path

val iq = RcloneRpc.InteractiveConfiguration(INTERNAL_CACHE_REMOTE_NAME)
while (true) {
val (_, option) = iq.question ?: break

when (option.name) {
"type" -> iq.submit("alias")
"remote" -> iq.submit(cacheDir)
"config_fs_advanced" -> iq.submit("false")
else -> throw IllegalStateException("Unexpected question: ${option.name}")
}
}
}

refreshRemotesInternal()
_alerts.update { it + SettingsAlert.RemoteAddSucceeded(INTERNAL_CACHE_REMOTE_NAME) }
_activityActions.update { it.copy(refreshRoots = true) }
}
}

fun activityActionCompleted() {
_activityActions.update { SettingsActivityActions(false) }
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
<string name="pref_verbose_rclone_logs_desc">Warning: The verbose logs may contain sensitive information, like authentication tokens.</string>
<string name="pref_save_logs_name">Save logs</string>
<string name="pref_save_logs_desc">Save logcat logs to a file. Note that the logs may contain names of remote files that have been accessed.</string>
<string name="pref_add_internal_cache_remote_name">Add rclone internal cache remote</string>
<string name="pref_add_internal_cache_remote_desc">Add a new <tt>alias</tt> remote that points to rclone\'s internal cache directory. This can be useful for troubleshooting VFS issues.</string>

<!-- Remote preferences -->
<string name="pref_edit_remote_open_name">Open remote</string>
Expand Down
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences_root.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,12 @@
app:title="@string/pref_save_logs_name"
app:summary="@string/pref_save_logs_desc"
app:iconSpaceReserved="false" />

<Preference
app:key="add_internal_cache_remote"
app:persistent="false"
app:title="@string/pref_add_internal_cache_remote_name"
app:summary="@string/pref_add_internal_cache_remote_desc"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit 1e5668c

Please sign in to comment.