An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Prevent deleted notebooks from being recreated after data sync https://github.com/siyuan-note/siyuan/issues/19102
Daniel209c43147fbb
diff --git a/kernel/model/box.go b/kernel/model/box.goindex c3e12f29ba..219b5f988b 100644--- a/kernel/model/box.go+++ b/kernel/model/box.go@@ -119,6 +119,7 @@ func ListNotebooks() (ret []*Box, err error) {boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")isExistConf := filelock.IsExist(boxConfPath)missingEncryptedIdentity := false+ repairLockHeld := falseif !isExistConf {if !IsUserGuide(id) {// conf.json 缺失时检查加密备份,确认是否为加密笔记本@@ -146,6 +147,22 @@ func ListNotebooks() (ret []*Box, err error) {setEncryptedBoxState(id, EncryptedBoxStateError)logging.LogErrorf("encrypted notebook key identity is missing [%s]", boxDirPath)} else {+ if !syncLock.TryLock() {+ logging.LogWarnf("deferred repairing box without conf while sync lock is occupied [%s]", boxDirPath)+ continue+ }+ repairLockHeld = true+ hasDocuments, scanErr := hasLiveBoxDocuments(boxDirPath)+ if scanErr != nil {+ syncLock.Unlock()+ logging.LogErrorf("scan box without conf [%s] failed: %s", boxDirPath, scanErr)+ continue+ }+ if !hasDocuments {+ syncLock.Unlock()+ logging.LogWarnf("ignored a box without conf and documents [%s]", boxDirPath)+ continue+ }// 数据同步时展开文档树操作可能导致数据丢失 https://github.com/siyuan-note/siyuan/issues/7129logging.LogWarnf("found a corrupted box [%s]", boxDirPath)}@@ -223,6 +240,9 @@ func ListNotebooks() (ret []*Box, err error) {box.Unindex()logging.LogWarnf("fixed a corrupted box [%s]", boxDirPath)}+ if repairLockHeld {+ syncLock.Unlock()+ }ret = append(ret, box)}@@ -258,6 +278,30 @@ func ListNotebooks() (ret []*Box, err error) {return}+// hasLiveBoxDocuments 检查笔记本正文目录中是否存在文档,忽略仅用于保存笔记本元数据和历史数据的 .siyuan 目录。+func hasLiveBoxDocuments(boxDirPath string) (ret bool, err error) {+ err = filepath.WalkDir(boxDirPath, func(filePath string, entry fs.DirEntry, walkErr error) error {+ if walkErr != nil {+ return walkErr+ }+ if entry.IsDir() {+ if filePath != boxDirPath && ".siyuan" == entry.Name() {+ return fs.SkipDir+ }+ return nil+ }+ if entry.Type().IsRegular() && strings.HasSuffix(entry.Name(), ".sy") {+ ret = true+ return fs.SkipAll+ }+ return nil+ })+ if errors.Is(err, fs.SkipAll) {+ err = nil+ }+ return+}+func (box *Box) GetConf() (ret *conf.BoxConf) {ret = conf.NewBoxConf()diff --git a/kernel/model/box_list_test.go b/kernel/model/box_list_test.gonew file mode 100644index 0000000000..f939df6846--- /dev/null+++ b/kernel/model/box_list_test.go@@ -0,0 +1,135 @@+// SiYuan - From thought to insight, with agents+// Copyright (c) 2020-present, b3log.org+//+// This program is free software: you can redistribute it and/or modify+// it under the terms of the GNU Affero General Public License as published by+// the Free Software Foundation, either version 3 of the License, or+// (at your option) any later version.+//+// This program is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+// GNU Affero General Public License for more details.+//+// You should have received a copy of the GNU Affero General Public License+// along with this program. If not, see <https://www.gnu.org/licenses/>.++package model++import (+ "os"+ "path/filepath"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/conf"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestListNotebooksIgnoresBoxWithoutConfAndDocuments(t *testing.T) {+ oldConf, oldDataDir := Conf, util.DataDir+ util.DataDir = t.TempDir()+ Conf = NewAppConf()+ Conf.FileTree = conf.NewFileTree()+ Conf.NotebookCrypto = conf.NewNotebookCrypto()+ t.Cleanup(func() {+ Conf, util.DataDir = oldConf, oldDataDir+ })++ boxID := "20260903120000-abcdefg"+ boxDirPath := filepath.Join(util.DataDir, boxID)+ emptyDocDirPath := filepath.Join(boxDirPath, "20260903120100-hijklmn")+ if err := os.MkdirAll(emptyDocDirPath, 0755); err != nil {+ t.Fatal(err)+ }+ hiddenFilePath := filepath.Join(boxDirPath, ".DS_Store")+ if err := os.WriteFile(hiddenFilePath, nil, 0644); err != nil {+ t.Fatal(err)+ }++ boxes, err := ListNotebooks()+ if err != nil {+ t.Fatal(err)+ }+ if 0 != len(boxes) {+ t.Fatalf("expected orphan box directory to be ignored, got %+v", boxes)+ }+ confPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")+ if _, err = os.Stat(confPath); !os.IsNotExist(err) {+ t.Fatalf("expected notebook conf not to be recreated: %v", err)+ }+ if _, err = os.Stat(hiddenFilePath); err != nil {+ t.Fatalf("expected ignored local file to be preserved: %v", err)+ }+ if _, err = os.Stat(emptyDocDirPath); err != nil {+ t.Fatalf("expected residual directory to be preserved: %v", err)+ }+}++func TestListNotebooksDefersRepairWhileSyncLockIsOccupied(t *testing.T) {+ oldConf, oldDataDir := Conf, util.DataDir+ util.DataDir = t.TempDir()+ Conf = NewAppConf()+ Conf.FileTree = conf.NewFileTree()+ Conf.NotebookCrypto = conf.NewNotebookCrypto()+ t.Cleanup(func() {+ Conf, util.DataDir = oldConf, oldDataDir+ })++ boxID := "20260903120500-jklmnop"+ docPath := filepath.Join(util.DataDir, boxID, "20260903120600-qrstuvw.sy")+ if err := os.MkdirAll(filepath.Dir(docPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(docPath, []byte("{}"), 0644); err != nil {+ t.Fatal(err)+ }++ syncLock.Lock()+ boxes, err := ListNotebooks()+ syncLock.Unlock()+ if err != nil {+ t.Fatal(err)+ }+ if 0 != len(boxes) {+ t.Fatalf("expected box repair to be deferred, got %+v", boxes)+ }+ confPath := filepath.Join(util.DataDir, boxID, ".siyuan", "conf.json")+ if _, err = os.Stat(confPath); !os.IsNotExist(err) {+ t.Fatalf("expected notebook conf not to be recreated while syncing: %v", err)+ }+}++func TestHasLiveBoxDocuments(t *testing.T) {+ boxDirPath := t.TempDir()+ historyDocPath := filepath.Join(boxDirPath, ".siyuan", "history", "20260903120200-opqrstu.sy")+ if err := os.MkdirAll(filepath.Dir(historyDocPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(historyDocPath, nil, 0644); err != nil {+ t.Fatal(err)+ }++ hasDocuments, err := hasLiveBoxDocuments(boxDirPath)+ if err != nil {+ t.Fatal(err)+ }+ if hasDocuments {+ t.Fatal("expected notebook history not to be treated as a live document")+ }++ liveDocPath := filepath.Join(boxDirPath, "20260903120300-vwxyzab", "20260903120400-cdefghi.sy")+ if err = os.MkdirAll(filepath.Dir(liveDocPath), 0755); err != nil {+ t.Fatal(err)+ }+ if err = os.WriteFile(liveDocPath, nil, 0644); err != nil {+ t.Fatal(err)+ }++ hasDocuments, err = hasLiveBoxDocuments(boxDirPath)+ if err != nil {+ t.Fatal(err)+ }+ if !hasDocuments {+ t.Fatal("expected live notebook document to be detected")+ }+}