Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Fix AV history backup and unused asset scan for encrypted notebooks https://github.com/siyuan-note/siyuan/issues/18034

Daniel37fc5ea5c510
diff --git a/kernel/model/assets.go b/kernel/model/assets.go
index 1f32498a22..32afca16ea 100644
--- a/kernel/model/assets.go
+++ b/kernel/model/assets.go
@@ -1225,14 +1225,19 @@ func UnusedAssets(sorted bool) (ret []*UnusedItem) {
}
}
- // 排除数据库中引用的资源文件
- storageAvDir := filepath.Join(util.DataDir, "storage", "av")
- if gulu.File.IsDir(storageAvDir) {
+ // 排除数据库中引用的资源文件(含加密 box 的 AV,解密后扫描)
+ avDirs := []string{filepath.Join(util.DataDir, "storage", "av")}
+ for _, encBoxID := range treenode.GetOpenedEncryptedBoxIDs() {
+ avDirs = append(avDirs, filepath.Join(util.DataDir, encBoxID, "storage", "av"))
+ }
+ for _, storageAvDir := range avDirs {
+ if !gulu.File.IsDir(storageAvDir) {
+ continue
+ }
entries, readErr := os.ReadDir(storageAvDir)
if nil != readErr {
logging.LogErrorf("read dir [%s] failed: %s", storageAvDir, readErr)
- err = readErr
- return
+ continue
}
for _, entry := range entries {
@@ -1240,11 +1245,11 @@ func UnusedAssets(sorted bool) (ret []*UnusedItem) {
continue
}
- data, readDataErr := filelock.ReadFile(filepath.Join(util.DataDir, "storage", "av", entry.Name()))
- if nil != readDataErr {
- logging.LogErrorf("read file [%s] failed: %s", entry.Name(), readDataErr)
- err = readDataErr
- return
+ avID := strings.TrimSuffix(entry.Name(), ".json")
+ // 用 ReadAttributeViewData 解密读取(加密 box 的 AV 是密文)
+ data, readDataErr := av.ReadAttributeViewData(avID)
+ if readDataErr != nil || data == nil {
+ continue
}
for asset := range assetsPathMap {
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index a81bd789d9..a79dc71ec9 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -53,9 +53,6 @@ func RemoveUnusedAttributeView(id string) {
if !filelock.IsExist(absPath) {
return
}
- if !gulu.File.IsSubPath(base, absPath) {
- return
- }
historyDir, err := getHistoryDir(HistoryOpClean)
if err != nil {
@@ -258,6 +255,8 @@ func getAvIDs(tree *parse.Tree, allAvIDs []string) (ret []string) {
func getAllAvIDs() (ret []string, err error) {
ret = []string{}
+ // 只扫全局 AV 目录。加密 box 的 AV 存在笔记本级目录(密文),不参与全局枚举——
+ // 未引用清理功能在加密 box 锁定时无法确认引用关系(loadTree 失败),枚举加密 AV 有误删风险
entries, err := os.ReadDir(filepath.Join(util.DataDir, "storage", "av"))
if nil != err {
return
@@ -1923,6 +1922,7 @@ func SearchAttributeView(keyword string, excludeAvIDs []string) (ret []*AvSearch
keywords := strings.Fields(keyword)
var avSearchTmpResults []*AvSearchTempResult
+ // 只扫全局 AV 目录。加密 box 的 AV 不参与全局搜索——避免跨加密边界暴露数据库名等元信息
avDir := filepath.Join(util.DataDir, "storage", "av")
entries, err := os.ReadDir(avDir)
if err != nil {
@@ -4069,7 +4069,10 @@ func removeAttributeViewBlock(srcIDs []string, avID string, tx *Transaction) (er
}
}
- srcAvPath := filepath.Join(util.DataDir, "storage", "av", avID+".json")
+ srcAvPath, _ := av.FindAttributeViewPath(avID)
+ if srcAvPath == "" {
+ return
+ }
destAvPath := filepath.Join(historyDir, "storage", "av", avID+".json")
if copyErr := filelock.Copy(srcAvPath, destAvPath); nil != copyErr {
logging.LogErrorf("copy av [%s] failed: %s", srcAvPath, copyErr)