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: Avoid treating concurrently deleted documents as corrupted
Daniel20b2fd281864
diff --git a/kernel/model/file.go b/kernel/model/file.goindex 894be1a196..5ef8161f8f 100644--- a/kernel/model/file.go+++ b/kernel/model/file.go@@ -164,6 +164,10 @@ func (box *Box) docIAL(p string) (ret map[string]string) {filePath := filepath.Join(util.DataDir, box.ID, p)ret = filesys.DocIAL(filePath)if 1 > len(ret) {+ // 目录枚举后文档可能已被删除,不将不存在的文件视为损坏。+ if _, statErr := os.Stat(filePath); errors.Is(statErr, os.ErrNotExist) {+ return nil+ }// 加密笔记本的 .sy 解密失败(DEK 未缓存或 box 未解锁)时不应视为损坏,// 否则文件会被 moveCorruptedData 移走导致数据丢失。// 使用解析后的文件路径反查实际 boxID(可能因 symlink 或路径穿越指向加密 box)@@ -184,6 +188,10 @@ func (box *Box) moveCorruptedData(filePath string) {base := filepath.Base(filePath)to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), box.ID, base)if copyErr := filelock.Copy(filePath, to); nil != copyErr {+ // 属性读取与备份之间发生删除时,无需再备份源文件。+ if _, statErr := os.Stat(filePath); errors.Is(copyErr, os.ErrNotExist) && errors.Is(statErr, os.ErrNotExist) {+ return+ }logging.LogErrorf("copy corrupted data file [%s] failed: %s", filePath, copyErr)return}diff --git a/kernel/model/file_corrupted_test.go b/kernel/model/file_corrupted_test.gonew file mode 100644index 0000000000..4a58f28f4c--- /dev/null+++ b/kernel/model/file_corrupted_test.go@@ -0,0 +1,58 @@+package model++import (+ "bytes"+ "os"+ "os/exec"+ "path/filepath"+ "testing"++ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestDocIALMissingFileIsNotCorruption(t *testing.T) {+ const child = "SIYUAN_TEST_MISSING_DOC_IAL"+ if os.Getenv(child) != "1" {+ cmd := exec.Command(os.Args[0], "-test.run=^TestDocIALMissingFileIsNotCorruption$", "-test.v")+ cmd.Env = append(os.Environ(), child+"=1")+ output, err := cmd.CombinedOutput()+ if err != nil {+ t.Fatalf("missing document subprocess failed: %v\n%s", err, output)+ }+ if bytes.Contains(output, []byte("properties not found")) || bytes.Contains(output, []byte("copy corrupted data file")) {+ t.Fatalf("deleted document was treated as corrupted:\n%s", output)+ }+ return+ }+ fixture := setupFileOperationTest(t)+ util.WorkspaceDir = t.TempDir()+ const missing = "/20260718000009-abcdefg.sy"+ if attrs := fixture.box.docIAL(missing); attrs != nil {+ t.Fatalf("missing document returned attributes: %v", attrs)+ }+ fixture.box.moveCorruptedData(filepath.Join(util.DataDir, fixture.box.ID, missing))+}++func TestDocIALCorruptedFileRemainsRecoverable(t *testing.T) {+ fixture := setupFileOperationTest(t)+ originalWorkspace := util.WorkspaceDir+ util.WorkspaceDir = t.TempDir()+ t.Cleanup(func() { util.WorkspaceDir = originalWorkspace })+ const document = "/20260718000009-abcdefg.sy"+ filePath := filepath.Join(util.DataDir, fixture.box.ID, document)+ raw := []byte(`{"Type":"NodeDocument","broken":`)+ if err := os.WriteFile(filePath, raw, 0644); err != nil {+ t.Fatal(err)+ }+ if attrs := fixture.box.docIAL(document); attrs != nil {+ t.Fatalf("corrupted document returned attributes: %v", attrs)+ }+ backups, err := filepath.Glob(filepath.Join(util.WorkspaceDir, "corrupted", "*", fixture.box.ID, filepath.Base(document)))+ if err != nil || len(backups) != 1 {+ t.Fatalf("expected one recoverable backup, got %v, %v", backups, err)+ }+ backup, err := os.ReadFile(backups[0])+ if err != nil || !bytes.Equal(backup, raw) {+ t.Fatalf("original corrupted bytes were not preserved: %q, %v", backup, err)+ }+}