Skip to content

Go 文件与路径平台抽象

系统概览

跨平台文件访问与路径管理抽象(fileaccess*.go + pathmgr*.go)。FileAccessor 接口统一桌面(os.*)与安卓(shared 模式 os.ReadDir 直读 /sdcard)的文件 IO;PathManager 抽象配置/缓存目录位置。安卓 SAF 相关细节与能力矩阵见 android-file-access 卡

核心职责

  • fileaccess.goFileAccessor 接口 + isContentUri 判定 + 应用层 ReadTextFile/WriteTextFile/ReadFileBytes/ListDir*/FileExists
  • fileaccess_desktop.go — os.* 实现(桌面)。
  • fileaccess_android.go — 安卓实现;content:// 直接返回 ErrContentUriNotSupported
  • pathmgr.go + pathmgr_desktop.go / pathmgr_android.goPathManager 平台路径(UserConfigDir/UserCacheDir / 私有目录)。

对外 API(节选)

  • ReadTextFile(path) / WriteTextFile(path, content) / ReadFileBytes(path) — 文件读写绑定。
  • ListDirRecursive(dir) — 递归列目录(返回 FileInfo 列表)。
  • FileExists(path) — 存在性判断。

与其他子系统关系

  • go-app.md(配置读写)、go-watch.mdgo-library.md 统一调用。
  • 前端契约对齐:core-backend.mdreadFileBytes 与 Go ReadFileBytes base64 编解码。
  • 安卓 shared/SAF 决策见 android-file-access.md(ADR-017/180/183)。

前端接入入口

  • 后端适配层:frontend/src/core/backend/go-adapter.ts 的能力矩阵(fsSelectDir 等)。

不变量

  • 新增平台文件操作必须落在 FileAccessor 实现内,禁止在业务文件散落 os.* 分支。
  • 安卓禁止引入 content:// 依赖(后端 ErrContentUriNotSupported 兜底)。
  • 前端能力矩阵(go-adapter.ts)是平台能力唯一真相源,与后端实现双向核对。

验证入口

  • 测试:internal/app/fs_test.go
  • 命令:go test ./internal/app/ -run "Fs|File"