Skip to content

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

:sparkles: encrypted-notebook InBox: attribute view notebook-level storage #18034

Danielae89d62b8d49
diff --git a/kernel/av/av.go b/kernel/av/av.go
index 90c29593e8..08f4647f9c 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -500,7 +500,13 @@ func IsAttributeViewExist(avID string) bool {
}
func ParseAttributeView(avID string) (ret *AttributeView, err error) {
- avJSONPath := GetAttributeViewDataPath(avID)
+ // 加密笔记本的 AV 定义存笔记本级路径,通过 fallback 自动查找并解密
+ avJSONPath, _ := findAttributeViewPath(avID)
+ if avJSONPath == "" {
+ // 文件不存在,可能是首次创建,按全局路径返回(由调用方处理)
+ avJSONPath = GetAttributeViewDataPath(avID)
+ return ParseAttributeViewByPath(avJSONPath)
+ }
return ParseAttributeViewByPath(avJSONPath)
}
@@ -523,6 +529,14 @@ func ParseAttributeViewByPath(avJSONPath string) (ret *AttributeView, err error)
logging.LogErrorf("read attribute view [%s] failed: %s", avID, readErr)
return
}
+ // 加密笔记本的 AV 定义是密文,按路径反查 boxID 后解密
+ if boxID := avBoxIDFromPath(avJSONPath); boxID != "" {
+ data, readErr = decryptAVData(boxID, data)
+ if readErr != nil {
+ logging.LogErrorf("decrypt attribute view [%s] failed: %s", avID, readErr)
+ return
+ }
+ }
cache.SetAVData(avID, data)
}
@@ -652,20 +666,41 @@ func SaveAttributeView(av *AttributeView) (err error) {
}
// 缓存与待写入数据一致时跳过落盘;缓存未命中时再读盘比对,避免无变更的重复写入
- avJSONPath := GetAttributeViewDataPath(av.ID)
+ // 通过 fallback 查找 AV 定义的实际路径(普通 box 全局,加密 box 笔记本级)
+ avJSONPath, avBoxID := findAttributeViewPath(av.ID)
+ if avJSONPath == "" {
+ // 文件不存在(首次创建),使用全局路径,boxID 为空(普通 box)
+ // 加密 box 的首次创建由 handler 层通过 SetAVBoxID 预设路径
+ avJSONPath = GetAttributeViewDataPath(av.ID)
+ }
if cachedData, ok := cache.GetAVData(av.ID); ok {
if len(cachedData) == len(data) && bytes.Equal(cachedData, data) {
return
}
} else {
- if diskData, readErr := filelock.ReadFile(avJSONPath); nil == readErr && len(diskData) == len(data) && bytes.Equal(diskData, data) {
- cache.SetAVData(av.ID, data)
- return
+ if diskData, readErr := filelock.ReadFile(avJSONPath); nil == readErr {
+ // 加密 box 的磁盘数据是密文,需先解密再比对
+ if avBoxID != "" {
+ diskData, _ = decryptAVData(avBoxID, diskData)
+ }
+ if len(diskData) == len(data) && bytes.Equal(diskData, data) {
+ cache.SetAVData(av.ID, data)
+ return
+ }
}
}
- if err = util.WriteFileByMmap(avJSONPath, data); nil != err {
- if err = filelock.WriteFile(avJSONPath, data); nil != err {
+ // 加密 box 的数据需加密后再写盘
+ writeData := data
+ if avBoxID != "" {
+ writeData, err = encryptAVData(avBoxID, data)
+ if err != nil {
+ logging.LogErrorf("encrypt attribute view [%s] failed: %s", av.ID, err)
+ return
+ }
+ }
+ if err = util.WriteFileByMmap(avJSONPath, writeData); nil != err {
+ if err = filelock.WriteFile(avJSONPath, writeData); nil != err {
logging.LogErrorf("save attribute view [%s] failed: %s", av.ID, err)
return
}
diff --git a/kernel/av/encrypted_hook.go b/kernel/av/encrypted_hook.go
new file mode 100644
index 0000000000..6ad13d6880
--- /dev/null
+++ b/kernel/av/encrypted_hook.go
@@ -0,0 +1,224 @@
+// SiYuan - Refactor your thinking
+// Copyright (c) 2020-present, b3log.org
+//
+// 本文件为加密笔记本的 AV 定义提供笔记本级存储与 DEK 加解密支持。
+// 与 filesys/crypto_hook.go 同模式:av 包不直接 import model(避免循环依赖),
+// 由 model 层在 init 时注入回调函数。
+
+package av
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+
+ "github.com/siyuan-note/filelock"
+ "github.com/siyuan-note/logging"
+ "github.com/siyuan-note/siyuan/kernel/util"
+ "github.com/vmihailenco/msgpack/v5"
+)
+
+// AVDEKProvider 由 model 层注入,返回已解锁加密 box 的 DEK。
+// 返回 (nil, nil) 表示该 box 非加密或未解锁——此时 AV 定义明文存储,对普通笔记本透明。
+var AVDEKProvider func(boxID string) ([]byte, error)
+
+// AVEncryptedBoxIDs 由 model 层注入,返回所有已打开的加密 boxID 列表。
+// 供 AV 路径 fallback 时遍历查找。
+var AVEncryptedBoxIDs func() []string
+
+// AVIsEncryptedBox 由 model 层注入,判断 boxID 是否为加密 notebook。
+var AVIsEncryptedBox func(boxID string) bool
+
+// pendingAVBox 记录首次创建的 AV 归属哪个加密 box。
+// handler 层创建 AV 前调 SetAVBoxID(avID, boxID),SaveAttributeView 时
+// findAttributeViewPath 会先查 pending 映射,找到则写入对应加密 box 路径。
+var pendingAVBox = map[string]string{}
+var pendingAVBoxLock = sync.RWMutex{}
+
+// SetAVBoxID 预设 AV 定义的归属 box。加密 notebook 创建 AV 时调用。
+// 普通笔记本不需要调(AV 默认走全局路径)。
+func SetAVBoxID(avID, boxID string) {
+ if boxID != "" {
+ pendingAVBoxLock.Lock()
+ defer pendingAVBoxLock.Unlock()
+ pendingAVBox[avID] = boxID
+ }
+}
+
+// GetAVBoxID 查询 AV 定义的归属 box(供 handler 层判断)。
+func GetAVBoxID(avID string) string {
+ pendingAVBoxLock.RLock()
+ defer pendingAVBoxLock.RUnlock()
+ return pendingAVBox[avID]
+}
+
+// attributeViewDataPathByBox 返回指定 box 的 AV 定义路径。
+// 加密 box:<DataDir>/<boxID>/storage/av/<avID>.json
+// 普通 box(boxID 为空):<DataDir>/storage/av/<avID>.json
+func attributeViewDataPathByBox(avID, boxID string) string {
+ if boxID != "" {
+ return filepath.Join(util.DataDir, boxID, "storage", "av", avID+".json")
+ }
+ return filepath.Join(util.DataDir, "storage", "av", avID+".json")
+}
+
+// findAttributeViewPath 按 fallback 逻辑查找 AV 定义文件的实际路径。
+// 1. 先查 pendingAVBox(首次创建预设的 boxID)
+// 2. 查全局 storage/av/(普通 box)
+// 3. 遍历已打开的加密 box 查找
+// 返回找到的路径和对应的 boxID(普通 box 返回空 boxID)。找不到返回空串。
+func findAttributeViewPath(avID string) (path string, boxID string) {
+ // 先查 pendingAVBox(首次创建场景)
+ if pendingBoxID := GetAVBoxID(avID); pendingBoxID != "" {
+ encPath := attributeViewDataPathByBox(avID, pendingBoxID)
+ return encPath, pendingBoxID
+ }
+ // 查全局
+ globalPath := attributeViewDataPathByBox(avID, "")
+ if filelock.IsExist(globalPath) {
+ return globalPath, ""
+ }
+ // 遍历已打开的加密 box
+ if AVEncryptedBoxIDs != nil {
+ for _, encBoxID := range AVEncryptedBoxIDs() {
+ encPath := attributeViewDataPathByBox(avID, encBoxID)
+ if filelock.IsExist(encPath) {
+ return encPath, encBoxID
+ }
+ }
+ }
+ return "", ""
+}
+
+// readAttributeViewData 按 fallback 逻辑读取 AV 定义数据(自动解密)。
+func readAttributeViewData(avID string) ([]byte, error) {
+ path, boxID := findAttributeViewPath(avID)
+ if path == "" {
+ return nil, nil // 文件不存在,由调用方处理
+ }
+ data, err := filelock.ReadFile(path)
+ if err != nil {
+ return nil, err
+ }
+ // 加密 box 的数据需解密
+ if boxID != "" {
+ data, err = decryptAVData(boxID, data)
+ if err != nil {
+ return nil, err
+ }
+ }
+ return data, nil
+}
+
+// writeAttributeViewData 写入 AV 定义数据(自动加密)。
+// boxID 为空时写全局路径(普通 box),非空时写加密 box 路径并加密。
+func writeAttributeViewData(avID, boxID string, data []byte) error {
+ path := attributeViewDataPathByBox(avID, boxID)
+ // 确保目录存在
+ dir := filepath.Dir(path)
+ if err := os.MkdirAll(dir, 0755); err != nil {
+ return err
+ }
+ // 加密 box 的数据需加密
+ if boxID != "" {
+ var err error
+ data, err = encryptAVData(boxID, data)
+ if err != nil {
+ return err
+ }
+ }
+ return filelock.WriteFile(path, data)
+}
+
+// mirrorBlocksPath 返回镜像索引文件的路径。
+// 加密 box:<DataDir>/<boxID>/storage/av/blocks.msgpack
+// 普通 box:<DataDir>/storage/av/blocks.msgpack
+func mirrorBlocksPath(boxID string) string {
+ if boxID != "" {
+ return filepath.Join(util.DataDir, boxID, "storage", "av", "blocks.msgpack")
+ }
+ return filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
+}
+
+// mirrorBlocksPathByAvID 通过 AV 定义的归属 box 返回镜像索引路径。
+// 先查 findAttributeViewPath(含 pendingAVBox fallback),找到则返回对应 box 的镜像路径。
+// 找不到则返回全局路径。
+func mirrorBlocksPathByAvID(avID string) string {
+ _, boxID := findAttributeViewPath(avID)
+ return mirrorBlocksPath(boxID)
+}
+
+// readMirrorBlocks 按路径读取镜像索引(boxID 为空读全局,非空读加密 box)。
+func readMirrorBlocks(boxID string) (ret map[string][]string) {
+ ret = map[string][]string{}
+ p := mirrorBlocksPath(boxID)
+ if !filelock.IsExist(p) {
+ return
+ }
+ data, err := filelock.ReadFile(p)
+ if err != nil {
+ logging.LogErrorf("read attribute view blocks failed: %s", err)
+ return
+ }
+ if err = msgpack.Unmarshal(data, &ret); err != nil {
+ logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
+ return
+ }
+ return
+}
+
+// writeMirrorBlocks 按路径写入镜像索引。
+func writeMirrorBlocks(boxID string, data map[string][]string) error {
+ p := mirrorBlocksPath(boxID)
+ if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
+ return err
+ }
+ raw, err := msgpack.Marshal(data)
+ if err != nil {
+ return err
+ }
+ return filelock.WriteFile(p, raw)
+}
+
+// 路径形如 <DataDir>/<boxID>/storage/av/<avID>.json → 返回 boxID。
+// 全局路径 <DataDir>/storage/av/<avID>.json → 返回空串。
+func avBoxIDFromPath(absPath string) string {
+ rel, err := filepath.Rel(util.DataDir, absPath)
+ if err != nil {
+ return ""
+ }
+ rel = filepath.ToSlash(rel)
+ parts := strings.SplitN(rel, "/", 2)
+ if len(parts) < 2 {
+ return ""
+ }
+ boxID := parts[0]
+ // 全局路径的第一段是 "storage",加密 box 路径的第一段是 boxID(节点 ID 格式)
+ if boxID == "storage" {
+ return ""
+ }
+ return boxID
+}
+
+func encryptAVData(boxID string, data []byte) ([]byte, error) {
+ if AVDEKProvider == nil {
+ return data, nil
+ }
+ dek, err := AVDEKProvider(boxID)
+ if err != nil || dek == nil {
+ return data, nil
+ }
+ return util.Encrypt(dek, data)
+}
+
+func decryptAVData(boxID string, data []byte) ([]byte, error) {
+ if AVDEKProvider == nil {
+ return data, nil
+ }
+ dek, err := AVDEKProvider(boxID)
+ if err != nil || dek == nil {
+ return data, nil
+ }
+ return util.Decrypt(dek, data)
+}
diff --git a/kernel/av/mirror.go b/kernel/av/mirror.go
index 95a0795a00..21b4c866d0 100644
--- a/kernel/av/mirror.go
+++ b/kernel/av/mirror.go
@@ -1,16 +1,11 @@
package av
import (
- "os"
- "path/filepath"
"sync"
"github.com/88250/gulu"
"github.com/88250/lute/ast"
- "github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
- "github.com/siyuan-note/siyuan/kernel/util"
- "github.com/vmihailenco/msgpack/v5"
)
var (
@@ -22,21 +17,17 @@ func GetBlockRels() (ret map[string][]string) {
defer AttributeViewBlocksLock.Unlock()
ret = map[string][]string{}
-
- blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
- if !filelock.IsExist(blocks) {
- return
- }
-
- data, err := filelock.ReadFile(blocks)
- if err != nil {
- logging.LogErrorf("read attribute view blocks failed: %s", err)
- return
- }
-
- if err = msgpack.Unmarshal(data, &ret); err != nil {
- logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
- return
+ // 全局镜像索引(普通 box)
+ for k, v := range readMirrorBlocks("") {
+ ret[k] = v
+ }
+ // 加密 box 的镜像索引(已打开的)
+ if AVEncryptedBoxIDs != nil {
+ for _, encBoxID := range AVEncryptedBoxIDs() {
+ for k, v := range readMirrorBlocks(encBoxID) {
+ ret[k] = v
+ }
+ }
}
return
}
@@ -45,23 +36,8 @@ func IsMirror(avID string) bool {
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
- blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
- if !filelock.IsExist(blocks) {
- return false
- }
-
- data, err := filelock.ReadFile(blocks)
- if err != nil {
- logging.LogErrorf("read attribute view blocks failed: %s", err)
- return false
- }
-
- avBlocks := map[string][]string{}
- if err = msgpack.Unmarshal(data, &avBlocks); err != nil {
- logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
- return false
- }
-
+ _, boxID := findAttributeViewPath(avID)
+ avBlocks := readMirrorBlocks(boxID)
blockIDs := avBlocks[avID]
return nil != blockIDs && 1 < len(blockIDs)
}
@@ -70,22 +46,8 @@ func RemoveBlockRel(avID, blockID string, existBlockTree func(string) bool) (ret
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
- blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
- if !filelock.IsExist(blocks) {
- return
- }
-
- data, err := filelock.ReadFile(blocks)
- if err != nil {
- logging.LogErrorf("read attribute view blocks failed: %s", err)
- return
- }
-
- avBlocks := map[string][]string{}
- if err = msgpack.Unmarshal(data, &avBlocks); err != nil {
- logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
- return
- }
+ _, boxID := findAttributeViewPath(avID)
+ avBlocks := readMirrorBlocks(boxID)
blockIDs := avBlocks[avID]
if nil == blockIDs {
@@ -103,12 +65,7 @@ func RemoveBlockRel(avID, blockID string, existBlockTree func(string) bool) (ret
avBlocks[avID] = newBlockIDs
ret = len(newBlockIDs) != len(blockIDs)
- data, err = msgpack.Marshal(avBlocks)
- if err != nil {
- logging.LogErrorf("marshal attribute view blocks failed: %s", err)
- return
- }
- if err = filelock.WriteFile(blocks, data); err != nil {
+ if err := writeMirrorBlocks(boxID, avBlocks); err != nil {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
@@ -119,25 +76,8 @@ func BatchUpsertBlockRel(nodes []*ast.Node) {
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
- avBlocks := map[string][]string{}
- blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
- if !filelock.IsExist(blocks) {
- if err := os.MkdirAll(filepath.Dir(blocks), 0755); err != nil {
- logging.LogErrorf("create attribute view dir failed: %s", err)
- return
- }
- } else {
- data, err := filelock.ReadFile(blocks)
- if err != nil {
- logging.LogErrorf("read attribute view blocks failed: %s", err)
- return
- }
-
- if err = msgpack.Unmarshal(data, &avBlocks); err != nil {
- logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
- return
- }
- }
+ // 按 boxID 分桶:普通 box 的 avID 写全局镜像,加密 box 的 avID 写笔记本级镜像
+ boxAvBlocks := map[string]map[string][]string{} // boxID → avBlocks
for _, n := range nodes {
if ast.NodeAttributeView != n.Type {
@@ -148,20 +88,23 @@ func BatchUpsertBlockRel(nodes []*ast.Node) {
continue
}
+ _, boxID := findAttributeViewPath(n.AttributeViewID)
+ avBlocks, ok := boxAvBlocks[boxID]
+ if !ok {
+ avBlocks = readMirrorBlocks(boxID)
+ boxAvBlocks[boxID] = avBlocks
+ }
+
blockIDs := avBlocks[n.AttributeViewID]
blockIDs = append(blockIDs, n.ID)
blockIDs = gulu.Str.RemoveDuplicatedElem(blockIDs)
avBlocks[n.AttributeViewID] = blockIDs
}
- data, err := msgpack.Marshal(avBlocks)
- if err != nil {
- logging.LogErrorf("marshal attribute view blocks failed: %s", err)
- return
- }
- if err = filelock.WriteFile(blocks, data); err != nil {
- logging.LogErrorf("write attribute view blocks failed: %s", err)
- return
+ for boxID, avBlocks := range boxAvBlocks {
+ if err := writeMirrorBlocks(boxID, avBlocks); err != nil {
+ logging.LogErrorf("write attribute view blocks failed: %s", err)
+ }
}
}
@@ -169,25 +112,8 @@ func UpsertBlockRel(avID, blockID string) (ret bool) {
AttributeViewBlocksLock.Lock()
defer AttributeViewBlocksLock.Unlock()
- avBlocks := map[string][]string{}
- blocks := filepath.Join(util.DataDir, "storage", "av", "blocks.msgpack")
- if !filelock.IsExist(blocks) {
- if err := os.MkdirAll(filepath.Dir(blocks), 0755); err != nil {
- logging.LogErrorf("create attribute view dir failed: %s", err)
- return
- }
- } else {
- data, err := filelock.ReadFile(blocks)
- if err != nil {
- logging.LogErrorf("read attribute view blocks failed: %s", err)
- return
- }
-
- if err = msgpack.Unmarshal(data, &avBlocks); err != nil {
- logging.LogErrorf("unmarshal attribute view blocks failed: %s", err)
- return
- }
- }
+ _, boxID := findAttributeViewPath(avID)
+ avBlocks := readMirrorBlocks(boxID)
blockIDs := avBlocks[avID]
oldLen := len(blockIDs)
@@ -196,12 +122,7 @@ func UpsertBlockRel(avID, blockID string) (ret bool) {
avBlocks[avID] = blockIDs
ret = oldLen != len(blockIDs) && 0 != oldLen
- data, err := msgpack.Marshal(avBlocks)
- if err != nil {
- logging.LogErrorf("marshal attribute view blocks failed: %s", err)
- return
- }
- if err = filelock.WriteFile(blocks, data); err != nil {
+ if err := writeMirrorBlocks(boxID, avBlocks); err != nil {
logging.LogErrorf("write attribute view blocks failed: %s", err)
return
}
diff --git a/kernel/filesys/tree.go b/kernel/filesys/tree.go
index 788e8a5f93..2fe65e6356 100644
--- a/kernel/filesys/tree.go
+++ b/kernel/filesys/tree.go
@@ -49,6 +49,37 @@ func LoadTrees(ids []string) (ret map[string]*parse.Tree) {
}
bts := treenode.GetBlockTrees(ids)
+
+ // 全局 blocktree 未命中的 id,遍历已打开的加密 box 查找
+ foundSet := map[string]bool{}
+ for id := range bts {
+ foundSet[id] = true
+ }
+ var missing []string
+ for _, id := range ids {
+ if !foundSet[id] {
+ missing = append(missing, id)
+ }
+ }
+ if len(missing) > 0 {
+ for _, encBoxID := range treenode.GetOpenedEncryptedBoxIDs() {
+ if len(missing) == 0 {
+ break
+ }
+ encBTs := treenode.GetBlockTreesInBox(missing, encBoxID)
+ for id, bt := range encBTs {
+ bts[id] = bt
+ }
+ var stillMissing []string
+ for _, id := range missing {
+ if _, found := encBTs[id]; !found {
+ stillMissing = append(stillMissing, id)
+ }
+ }
+ missing = stillMissing
+ }
+ }
+
luteEngine := util.NewLute()
var boxIDs []string
var paths []string
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index 9b286819c7..9f5af4d6af 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -32,12 +32,30 @@ import (
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/sql"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
func RenderAttributeView(blockID, avID, viewID, query string, page, pageSize int, groupPaging map[string]any, createIfNotExist, ignoreRows bool) (viewable av.Viewable, attrView *av.AttributeView, err error) {
waitForSyncingStorages()
+ // 加密笔记本的 AV 定义存笔记本级路径,通过 blockID 反查 boxID 并预设归属
+ if "" != blockID {
+ bt := treenode.GetBlockTree(blockID)
+ if nil == bt {
+ // 全局 blocktree 找不到时,遍历已打开的加密 box
+ for _, encBoxID := range treenode.GetOpenedEncryptedBoxIDs() {
+ if encBT := treenode.GetBlockTreeInBox(blockID, encBoxID); nil != encBT {
+ bt = encBT
+ break
+ }
+ }
+ }
+ if nil != bt && IsEncryptedBox(bt.BoxID) {
+ av.SetAVBoxID(avID, bt.BoxID)
+ }
+ }
+
if avJSONPath := av.GetAttributeViewDataPath(avID); !filelock.IsExist(avJSONPath) {
if !createIfNotExist {
err = av.ErrAttributeViewNotFound
diff --git a/kernel/model/filesys_init.go b/kernel/model/filesys_init.go
index 55e59c6072..c2f69475f1 100644
--- a/kernel/model/filesys_init.go
+++ b/kernel/model/filesys_init.go
@@ -16,10 +16,18 @@
package model
-import "github.com/siyuan-note/siyuan/kernel/filesys"
+import (
+ "github.com/siyuan-note/siyuan/kernel/av"
+ "github.com/siyuan-note/siyuan/kernel/filesys"
+ "github.com/siyuan-note/siyuan/kernel/treenode"
+)
// init 把 GetDEKIfUnlocked 注入 filesys 包,让 filesys 的 .sy 读写插点能查询 box 的 DEK。
-// filesys 不能直接 import model(循环依赖),故通过此回调注入。
+// 同时把 AV 相关回调注入 av 包,让 AV 定义的笔记本级存储能查询 DEK 和已打开的加密 box。
+// filesys / av 不能直接 import model(循环依赖),故通过回调注入。
func init() {
filesys.DEKProvider = GetDEKIfUnlocked
+ av.AVDEKProvider = GetDEKIfUnlocked
+ av.AVEncryptedBoxIDs = treenode.GetOpenedEncryptedBoxIDs
+ av.AVIsEncryptedBox = IsEncryptedBox
}
diff --git a/kernel/treenode/blocktree.go b/kernel/treenode/blocktree.go
index 9d78316aee..41b639d464 100644
--- a/kernel/treenode/blocktree.go
+++ b/kernel/treenode/blocktree.go
@@ -315,6 +315,7 @@ func ExistBlockTrees(ids []string) (ret map[string]bool) {
ret[id] = false
}
+ // 先查全局 blocktree
sqlStmt := "SELECT id FROM blocktrees WHERE id IN ('" + strings.Join(ids, "','") + "')"
rows, err := query(sqlStmt)
if err != nil {
@@ -330,6 +331,31 @@ func ExistBlockTrees(ids []string) (ret map[string]bool) {
}
ret[id] = true
}
+
+ // 全局 blocktree 未命中的 id,遍历已打开的加密 box 查找
+ var missing []string
+ for id, found := range ret {
+ if !found {
+ missing = append(missing, id)
+ }
+ }
+ if len(missing) > 0 {
+ for _, encBoxID := range GetOpenedEncryptedBoxIDs() {
+ if len(missing) == 0 {
+ break
+ }
+ encRet := ExistBlockTreesInBox(missing, encBoxID)
+ var stillMissing []string
+ for _, id := range missing {
+ if encRet[id] {
+ ret[id] = true
+ } else {
+ stillMissing = append(stillMissing, id)
+ }
+ }
+ missing = stillMissing
+ }
+ }
return
}
@@ -976,6 +1002,33 @@ func ExistBlockTreeInBox(id, boxID string) bool {
return row.Scan(&tmp) == nil
}
+// ExistBlockTreesInBox 按 ids 在指定 box 的 db 里批量查块是否存在。
+func ExistBlockTreesInBox(ids []string, boxID string) (ret map[string]bool) {
+ ret = map[string]bool{}
+ if 1 > len(ids) {
+ return
+ }
+ for _, id := range ids {
+ ret[id] = false
+ }
+ sqlStmt := "SELECT id FROM blocktrees WHERE id IN ('" + strings.Join(ids, "','") + "')"
+ rows, err := queryForBox(boxID, sqlStmt)
+ if err != nil {
+ logging.LogErrorf("sql query [%s] failed: %s", sqlStmt, err)
+ return
+ }
+ defer rows.Close()
+ for rows.Next() {
+ var id string
+ if err = rows.Scan(&id); err != nil {
+ logging.LogErrorf("query scan field failed: %s", err)
+ return
+ }
+ ret[id] = true
+ }
+ return
+}
+
// GetBlockTreesByRootIDInBox 按 rootID 在指定 box 的 db 里查块树。
func GetBlockTreesByRootIDInBox(rootID, boxID string) (ret []*BlockTree) {
sqlStmt := "SELECT * FROM blocktrees WHERE root_id = ?"