Skip to content

Commit 5a9dad3

Browse files
author
Fastace
committed
fix(cloud): 云端仓库初始化统一前置校验,WebDAV 端口改为独立输入
一、现象与核对 用户反馈:新建 SMB 账户(威联通 NAS 192.168.10.10),userA 在根目录只有列共享 权限、folderA 才有写权限。URL 栏填 192.168.10.10 时「测试连接」成功,初始化 云端仓库却报「init 报成功但仓库 config 未写入 SMB(疑似 rclone serve /认证失败)」; URL 栏填 192.168.10.10/folderA 时「测试连接」直接失败。 横向核对 8 种云账户页(FTP / SFTP / SMB / WebDAV / S3 / AWS S3 / Aliyun OSS / Huawei OBS)与 8 个云端 Restic 后端后确认:这不是权限误判,而是「URL 只承载 主机、目标目录必须由 高级 → 远程路径 单独选择」这一既有设计缺少校验与提示。 上述现象为用户反馈;本次修改完成后已由用户实机测试,确认符合预期。 二、根因 1. URL 输入框的 smb:// / ftp:// / sftp:// 只是 OutlinedTextField 的 prefix 装饰, 不进入值;IndexViewModel.UpdateEntity 原样 host = intent.url。所有连接串拼接 只做 removePrefix(scheme) + removeSuffix("/"),不解析路径,故把 /folderA 写进 URL 栏会整段塞进 rclone 的 host 参数,newConnection 拼出 192.168.10.10/folderA:445 后拨号失败。 2. 测试连接只做只读列举:IndexViewModel.TestConnection 对所有协议统一 skipRemoteCheck = true,各 testConnection() 分别是 remote=""(SMB/FTP)、 remote="."(SFTP)、opendalList("/")(WebDAV / 对象存储),既不验证写权限, 也不要求 remote 已选择。CloudRepository.withClient 里已有的 entity.remote.isEmpty() 校验被初始化链路(ResticRepository* → RcloneServe / opendal → rcloneRpc)完全绕过。 3. 空 remote 时各后端行为不一致:formatSftpRoot("") = "",rclone SMB 后端把空 root 当成 share 枚举层(NewFs 直接返回、Mkdir 空 share 静默 return nil、 Object.Update 因 filename 为空返回 fs.ErrorIsDir),config 根本无处落地; FTP/SFTP 会静默把仓库建到登录家目录,WebDAV / 对象存储经 formatOpenDALRoot("") = "/" 建到根。因此 SMB 必然触发那条误导性的兜底文案 (共 4 份:SMB / FTP / SFTP / WebDAV),把用户引向「认证失败」的猜测。 4. 校验缺口:8 个「继续」按钮都带 remote.isNotEmpty(),但 8 个「初始化状态」 Clickable 全部不校验、8 个「初始化」按钮仅 SFTP 校验;8 个 XxxResticViewModel.initializeXxxRepository 内部也没有 remotePath 守卫。 即 16 个初始化入口里 15 个可以在未选远程路径时发起建库。 5. 选完远程路径后 remote = uiState.cloudEntity!!.remote 读的是 Compose 捕获的 旧状态快照(甚至可能为 null),会把刚选中的路径写回空值,新增校验会被卡住。 6. WebDAV 配置页把「地址 + 端口」塞在同一个 URL 栏:updateWebDAVEntity 用 $scheme://$cleanHost 直接落库;WebDAVCertUtil.verifyPublicCa 本来就有 port: Int = 443,调用处却从不传,非标准 HTTPS 端口永远去连 443。 三、修复 1. 新增 core/restic/ResticPreflight.kt:集中 remotePath 非空校验、纯主机校验 (仅 FTP/SFTP/SMB,大小写敏感剥离 scheme,与各后端 removePrefix 对齐), 以及 config 复核兜底文案。core/common 的 values / values-zh-rCN 新增 4 个 双语字符串:restic_remote_path_required、restic_host_only、 restic_config_missing_hint、connection_established_no_remote。 2. 8 个云端后端建库入口统一接入 ResticPreflight.checkInit:SMB/FTP/SFTP/WebDAV 的 initXxxRepository 与 COS/AWS S3/Aliyun OSS/Huawei OBS 的 initRepository; 前 4 个 override 都委托到旧名方法,8 处即覆盖全部入口。4 条「init 报成功但 仓库 config 未写入 XXX(疑似 rclone serve/认证失败)」改为同一条可操作提示 「未检测到仓库 config:请确认远程路径指向的目录可写…」。 3. UI 全协议对齐:7 个初始化按钮补齐 remote.isNotEmpty()(对齐唯一已有的 SFTP),8 个初始化状态 Clickable 补齐;8 个「高级 → 远程路径」在未选时 显示明确提示文案;8 处 remote 回填改读 viewModel.uiState.value,修掉 旧快照 / NPE 隐患。 4. 测试连接统一加 host 校验(ResticPreflight.hostError,禁止把路径或大写 scheme 写进 URL 栏),并在未选 remote 时把成功 toast 改为「连接成功(仅验证 列目录)。初始化仓库前请先选择远程路径」。 5. WebDAV 端口拆分:IndexViewModel 新增 splitWebdavEndpoint / buildWebdavEndpoint(端口插在 authority 之后、路径之前,兼容 nas/dav 子路径 与 [::1]:8080;非法或越界端口回落协议默认值),updateWebDAVEntity 增 port: String = ""。WebDAVSetup 新增独立端口输入框(Number 键盘、 R.string.port)、默认 HTTPS=443 / HTTP=80、切协议时仅在当前端口为默认值时 联动、编辑旧账户时从完整 host 拆出地址与端口。测试连接的证书预校验按新端口 握手;测试连接 / 远程路径 / 继续 / 两处初始化共 5 个入口统一走 buildWebdavEndpoint,保证建库 host 与落库 host 完全一致。 四、验证 - 真机复测:用户已实机测试,确认本次修改符合预期。 - 编译::core:common:processDebugResources、:core:restic:compileDebugKotlin、 :feature:main:cloud:compileDebugKotlin / processDebugResources、 :app:compileArm64-v8aFossDebugKotlin --offline 全部 BUILD SUCCESSFUL。 - lint::core:common:lintDebug、:feature:main:cloud:lintDebug 在线执行 BUILD SUCCESSFUL,新增双语字符串无 MissingTranslation;read_lints 0 诊断。 - git diff --check 干净;提交统计 20 files +237 -80(19 个修改 + 新增 ResticPreflight.kt 60 行);ResticPreflight.class 实际产出。 - 计数核对:8 个 checkInit、4 条 configMissingMessage、每个 Setup 3 处 remote.isNotEmpty()(共 24)、8 处状态同步;边界行为 192.168.10.10:8080 → host+8080、nas.example.com/dav → 无端口保留路径、 [::1]:8080 → IPv6 正确、纯主机 → 无端口。 - 签名变更:新增 2 个 helper;updateWebDAVEntity 仅新增带默认值的 port 参数 (全仓 3 个调用点均为命名参数,文本搜索确认),其余零签名变更。 - 门禁:impact CloudResticBackend.initRepository = MEDIUM(12 impacted)、 initSmbRepository = LOW(2 direct)、PageWebDAVSetup = LOW(1 direct); TestConnection 与 updateWebDAVEntity 报 UNKNOWN,已用文本搜索补证 8 处 emitIntent(TestConnection) 与 3 处 updateWebDAVEntity 调用。 detect_changes scope=all = 19 files / 37 symbols / 6 processes / risk high, 来自 IndexViewModel.onEvent 中枢符号,6 个执行流均为 OnEvent → …,无无关 模块扩散,判为中枢符号虚报,按源码与全量编译放行。 - 索引:提交前已用 analyze --force --pdg 将索引对齐到当时的 HEAD 43bb2b3 (本次提交的父提交),status up-to-date (268,282 nodes / 708,614 edges / 1872 clusters / 825 flows), check --cycles = cycleCount 104 / componentCount 10,与历史基线一致; MCP 响应仍带 staleness behind 属进程缓存旧元数据,以 CLI status 为准。 五、有意不改的部分 - 测试连接写探针(mkdir + delete)未实现:SMB/FTP/SFTP 的 removeDirectory / deleteRecursively 均抛 UnsupportedOperationException, 需要新增 rclone RPC 路径,风险大于收益;改为 toast 明确标注仅验证列目录。 - URL 字段语义不变:仍只填主机 / endpoint,不自动把 host/folderA 拆成两段, 避免误伤 WebDAV 与对象存储的路径语义。 - WebDAV 端口不写进 WebDAVExtra:host 始终保存完整 endpoint,避免旧配置 反序列化兼容问题与 core/network、core/restic 的跨模块改动。 - SFTP/FTP 的 remote 仍为登录家目录相对路径(无法表达绝对路径), SMBExtra.share 仍只是元数据不参与连接串,均未动。
1 parent 43bb2b3 commit 5a9dad3

20 files changed

Lines changed: 237 additions & 80 deletions

File tree

‎source/core/common/src/main/res/values-zh-rCN/strings.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
<string name="native_error_smb_share">SMB 共享名无效或不可访问</string>
1313
<string name="native_error_obscure">密码解码失败</string>
1414
<string name="native_error_unknown">操作失败:%1$s</string>
15+
16+
<!-- 云端仓库建库前置校验(主机 / 远程路径 / config 复核)。 -->
17+
<string name="restic_remote_path_required">未选择远程路径:请先在「高级 → 远程路径」中选择共享文件夹或仓库目录,再初始化仓库。</string>
18+
<string name="restic_host_only">服务器地址只能填主机地址(不要带协议前缀或路径),目录请在「高级 → 远程路径」中选择。</string>
19+
<string name="restic_config_missing_hint">未检测到仓库 config:请确认「远程路径」指向的目录可写,且 rclone serve / 云端认证正常。</string>
20+
<string name="connection_established_no_remote">连接成功(仅验证列目录)。初始化仓库前请先选择远程路径。</string>
1521
</resources>

‎source/core/common/src/main/res/values/strings.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
<string name="native_error_smb_share">The SMB share name is invalid or unreachable</string>
1414
<string name="native_error_obscure">Failed to decode the password</string>
1515
<string name="native_error_unknown">Operation failed: %1$s</string>
16+
17+
<!-- Cloud repository preflight (host / remote path / config verification). -->
18+
<string name="restic_remote_path_required">No remote path selected. Open Advanced > Remote path and choose a share folder or repository directory before initializing.</string>
19+
<string name="restic_host_only">Server address must be a host only (no scheme or path). Put the directory in Advanced > Remote path.</string>
20+
<string name="restic_config_missing_hint">Repository config was not found. Check that the remote path points to a writable directory and that rclone serve / cloud credentials are valid.</string>
21+
<string name="connection_established_no_remote">Connection OK (listing only). Select a remote path before initializing the repository.</string>
1622
</resources>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.xayah.core.restic
2+
3+
import android.content.Context
4+
import com.xayah.core.common.R
5+
import com.xayah.core.model.CloudType
6+
import com.xayah.core.model.database.CloudEntity
7+
8+
/**
9+
* 云端账户建库前的统一前置校验。
10+
*
11+
* URL / endpoint 输入框只承载主机(WebDAV 与对象存储为完整 endpoint),目标目录
12+
* 必须走「高级 → 远程路径」写入 CloudEntity.remote。空 remote 时各后端行为不一致:
13+
* SMB 的 rclone 后端把空 root 当成 share 枚举层,config 根本写不进去;FTP/SFTP
14+
* 会静默把仓库建到登录家目录;WebDAV/对象存储建到根。因此所有 CloudResticBackend
15+
* 的建库入口必须先过这里,避免走到「init 报成功但仓库 config 未写入 …」兜底分支。
16+
*/
17+
object ResticPreflight {
18+
19+
/** URL 栏只接受主机地址的协议;它们的 host 会被整段塞进 rclone 的 host 参数。 */
20+
private val PLAIN_HOST_TYPES = setOf(CloudType.FTP, CloudType.SFTP, CloudType.SMB)
21+
22+
/** 返回可直接展示的失败原因;null 表示校验通过。 */
23+
fun error(context: Context, entity: CloudEntity, remotePath: String): String? {
24+
if (remotePath.isBlank()) return context.getString(R.string.restic_remote_path_required)
25+
return hostError(context, entity)
26+
}
27+
28+
/** 只校验主机字段;测试连接阶段按设计不要求 remote 已选择。 */
29+
fun hostError(context: Context, entity: CloudEntity): String? =
30+
if (entity.type in PLAIN_HOST_TYPES && !isPlainHost(entity.host)) {
31+
context.getString(R.string.restic_host_only)
32+
} else {
33+
null
34+
}
35+
36+
/** 供各后端 init 入口使用:非 null 时调用方应直接 `return@withContext it`。 */
37+
fun checkInit(context: Context, entity: CloudEntity, remotePath: String): Result<String>? =
38+
error(context, entity, remotePath)?.let { Result.failure(Exception(it)) }
39+
40+
/** init 未抛异常但 config 读不到时的兜底文案,不再把用户引向「认证失败」的猜测。 */
41+
fun configMissingMessage(context: Context): String =
42+
context.getString(R.string.restic_config_missing_hint)
43+
44+
/**
45+
* 兼容历史/导入值里残留的小写 scheme。这里必须与各后端
46+
* `removePrefix("smb://")` / `removePrefix("ftp://")` 的大小写敏感行为保持一致:
47+
* 大写 scheme 后端并不会剥离,放行反而会变成底层 rclone 的拨号错误,故直接判非法。
48+
*/
49+
private fun isPlainHost(raw: String): Boolean {
50+
val host = raw.trim().removeSuffix("/")
51+
val withoutScheme = when {
52+
host.startsWith("smb://") -> host.substring(6)
53+
host.startsWith("sftp://") -> host.substring(7)
54+
host.startsWith("ftps://") -> host.substring(7)
55+
host.startsWith("ftp://") -> host.substring(6)
56+
else -> host
57+
}
58+
return withoutScheme.isNotEmpty() && !withoutScheme.contains('/') && !withoutScheme.contains("://")
59+
}
60+
}

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositoryAliyunOss.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class ResticRepositoryAliyunOss @Inject constructor(
2525
override suspend fun initRepository(
2626
cloudEntity: CloudEntity, remotePath: String, password: String
2727
): Result<String> = withContext(Dispatchers.IO) {
28+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
2829
try {
2930
val extra = ResticShared.json.decodeFromString<AliyunOssExtra>(cloudEntity.extra)
3031
val options = buildAliyunOssBackendOptions(extra, remotePath) + shared.context.resticCompressionOptions() + resticExtraVerifyOptions()

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositoryAwsS3.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class ResticRepositoryAwsS3 @Inject constructor(
3636
override suspend fun initRepository(
3737
cloudEntity: CloudEntity, remotePath: String, password: String
3838
): Result<String> = withContext(Dispatchers.IO) {
39+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
3940
try {
4041
val extra = ResticShared.json.decodeFromString<AwsS3Extra>(cloudEntity.extra)
4142
// 仅在 init 这一处合并压缩 key(与 COS 一致)

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositoryCos.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ResticRepositoryCos @Inject constructor(
4343
override suspend fun initRepository(
4444
cloudEntity: CloudEntity, remotePath: String, password: String
4545
): Result<String> = withContext(Dispatchers.IO) {
46+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
4647
try {
4748
val extra = ResticShared.json.decodeFromString<S3Extra>(cloudEntity.extra)
4849
// 仅在 init 这一处合并压缩 key;buildS3BackendOptions 本身不改(被 backup/restore/prune/list 共用)

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositoryFtp.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ class ResticRepositoryFtp @Inject constructor(
115115
suspend fun initFtpRepository(
116116
cloudEntity: CloudEntity, remotePath: String, password: String
117117
): Result<String> = withContext(Dispatchers.IO) {
118+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
118119
val session = startServe(cloudEntity, remotePath)
119120
try {
120121
Log.i("ResticFtpRoot", "initFtp restUrl=${session.restUrl} remotePath=$remotePath")
@@ -132,7 +133,7 @@ class ResticRepositoryFtp @Inject constructor(
132133
if (!exists) {
133134
Log.e(ResticShared.TAG, "FTP init 报成功但 repositoryExists=false")
134135
return@withContext Result.failure(
135-
Exception("init 报成功但仓库 config 未写入 FTP(疑似 rclone serve/认证失败)")
136+
Exception(ResticPreflight.configMissingMessage(shared.context))
136137
)
137138
}
138139

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositoryHuaweiObs.kt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class ResticRepositoryHuaweiObs @Inject constructor(
2424
override suspend fun initRepository(
2525
cloudEntity: CloudEntity, remotePath: String, password: String
2626
): Result<String> = withContext(Dispatchers.IO) {
27+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
2728
try {
2829
val extra = ResticShared.json.decodeFromString<HuaweiObsExtra>(cloudEntity.extra)
2930
val options = buildHuaweiObsBackendOptions(extra, remotePath) + shared.context.resticCompressionOptions() + resticExtraVerifyOptions()

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositorySftp.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class ResticRepositorySftp @Inject constructor(
114114
suspend fun initSftpRepository(
115115
cloudEntity: CloudEntity, remotePath: String, password: String
116116
): Result<String> = withContext(Dispatchers.IO) {
117+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
117118
val session = startServe(cloudEntity, remotePath)
118119
try {
119120
Log.i("ResticSftpRoot", "initSftp restUrl=${session.restUrl} remotePath=$remotePath")
@@ -131,7 +132,7 @@ class ResticRepositorySftp @Inject constructor(
131132
if (!exists) {
132133
Log.e(ResticShared.TAG, "SFTP init 报成功但 repositoryExists=false")
133134
return@withContext Result.failure(
134-
Exception("init 报成功但仓库 config 未写入 SFTP(疑似 rclone serve/认证失败)")
135+
Exception(ResticPreflight.configMissingMessage(shared.context))
135136
)
136137
}
137138

‎source/core/restic/src/main/kotlin/com/xayah/core/restic/ResticRepositorySmb.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class ResticRepositorySmb @Inject constructor(
111111
suspend fun initSmbRepository(
112112
cloudEntity: CloudEntity, remotePath: String, password: String
113113
): Result<String> = withContext(Dispatchers.IO) {
114+
ResticPreflight.checkInit(shared.context, cloudEntity, remotePath)?.let { return@withContext it }
114115
val session = startServe(cloudEntity, remotePath)
115116
try {
116117
Log.i("ResticSmbRoot", "initSmb restUrl=${session.restUrl} remotePath=$remotePath")
@@ -128,7 +129,7 @@ class ResticRepositorySmb @Inject constructor(
128129
if (!exists) {
129130
Log.e(ResticShared.TAG, "SMB init 报成功但 repositoryExists=false")
130131
return@withContext Result.failure(
131-
Exception("init 报成功但仓库 config 未写入 SMB(疑似 rclone serve/认证失败)")
132+
Exception(ResticPreflight.configMissingMessage(shared.context))
132133
)
133134
}
134135

0 commit comments

Comments
 (0)