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: Clean SiYuan archive export staging folders https://github.com/siyuan-note/siyuan/issues/20410
Daniel786760be04b4
diff --git a/kernel/model/export.go b/kernel/model/export.goindex 3c216944af..9a9307566a 100644--- a/kernel/model/export.go+++ b/kernel/model/export.go@@ -2676,11 +2676,20 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string, iexportDir := filepath.Join(util.TempDir, "export", normalExportTempName(baseFolderName))if encrypted {exportDir = filepath.Join(util.TempDir, "export", boxID, "sy", exportID)+ } else {+ // 同名导出共享暂存路径,串行处理以免清理其他请求正在写入的文件。+ filelock.Lock(exportDir)+ defer filelock.Unlock(exportDir)+ if err := os.RemoveAll(exportDir); err != nil {+ logging.LogErrorf("clear export temp folder [%s] failed: %s", exportDir, err)+ return+ }}if err := os.MkdirAll(exportDir, 0755); err != nil {logging.LogErrorf("create export temp folder failed: %s", err)return}+ defer os.RemoveAll(exportDir)if includeBoxConf {boxConf := box.GetConf()boxConf.Sort = 0@@ -2979,11 +2988,13 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string, izipPath = filepath.Join(util.TempDir, "export", boxID, "sy", exportID+"-"+zipBaseName)zipPartialPath = zipPath + ".partial"}+ defer os.Remove(zipPartialPath)zip, err := gulu.Zip.Create(zipPartialPath)if err != nil {logging.LogErrorf("create export .sy.zip [%s] failed: %s", exportDir, err)return ""}+ defer zip.Close()zipCallback := func(filename string) {util.PushEndlessProgress(Conf.language(65) + " " + fmt.Sprintf(Conf.language(253), filename))@@ -3003,7 +3014,6 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string, ireturn ""}- os.RemoveAll(exportDir)if encrypted {zipPath = "/export/" + registerManagedEncryptedExport(boxID, "sy", zipPath)} else {diff --git a/kernel/model/export_sy_temp_test.go b/kernel/model/export_sy_temp_test.gonew file mode 100644index 0000000000..36e15ea7f3--- /dev/null+++ b/kernel/model/export_sy_temp_test.go@@ -0,0 +1,98 @@+package model++import (+ "os"+ "path/filepath"+ "testing"+ "time"++ "github.com/siyuan-note/siyuan/kernel/treenode"+ "github.com/siyuan-note/siyuan/kernel/util"+)++func TestExportSYClearsStaleTempFiles(t *testing.T) {+ const boxID = "20261010000000-box0001"+ const docID = "20261010000001-doc0001"+ setupExportRelatedTest(t, boxID)+ tree := treenode.NewTree(boxID, "/"+docID+".sy", "/Notes", "Notes")+ writeExportRelatedTestTree(t, tree)+ exportDir := filepath.Join(util.TempDir, "export", "Notes")+ for _, stale := range []string{+ "20261010000002-old0001.sy", "assets/deleted.txt", "storage/av/deleted.json",+ "storage/riff/deleted.json", ".siyuan/sort.json",+ } {+ stalePath := filepath.Join(exportDir, stale)+ if err := os.MkdirAll(filepath.Dir(stalePath), 0755); err != nil {+ t.Fatal(err)+ }+ if err := os.WriteFile(stalePath, []byte("stale"), 0644); err != nil {+ t.Fatal(err)+ }+ }+ archivePath := exportSYZip(boxID, "/", "Notes", []string{tree.Path}, false)+ if archivePath != "/export/Notes.sy.zip" {+ t.Fatalf("download filename changed: %q", archivePath)+ }+ archive := openExportArchive(t, archivePath)+ if findArchiveFile(archive.File, "Notes/"+docID+".sy") == nil {+ t.Fatal("current document missing from export")+ }+ for _, entry := range archive.File {+ if entry.Name != "Notes/" && entry.Name != "Notes/"+docID+".sy" {+ t.Fatalf("stale entry included in archive: %s", entry.Name)+ }+ }+ if _, err := os.Stat(exportDir); !os.IsNotExist(err) {+ t.Fatalf("successful export retained temp files: %v", err)+ }+}++func TestExportSYConcurrentSameName(t *testing.T) {+ const boxID = "20261010000000-box0001"+ const docID = "20261010000001-doc0001"+ setupExportRelatedTest(t, boxID)+ tree := treenode.NewTree(boxID, "/"+docID+".sy", "/Notes", "Notes")+ writeExportRelatedTestTree(t, tree)+ results := make(chan string, 4)+ for i := 0; i < cap(results); i++ {+ go func() {+ results <- exportSYZip(boxID, "/", "Notes", []string{tree.Path}, false)+ }()+ }+ for i := 0; i < cap(results); i++ {+ select {+ case archivePath := <-results:+ if archivePath != "/export/Notes.sy.zip" {+ t.Errorf("concurrent export failed: %q", archivePath)+ }+ case <-time.After(10 * time.Second):+ t.Fatal("concurrent exports did not complete")+ }+ }+ archive := openExportArchive(t, "/export/Notes.sy.zip")+ if findArchiveFile(archive.File, "Notes/"+docID+".sy") == nil {+ t.Fatal("concurrent export archive is incomplete")+ }+}++func TestExportSYFailureCleansTempFilesAndAllowsRetry(t *testing.T) {+ const boxID = "20261010000000-box0001"+ const docID = "20261010000001-doc0001"+ setupExportRelatedTest(t, boxID)+ tree := treenode.NewTree(boxID, "/"+docID+".sy", "/Notes", "Notes")+ writeExportRelatedTestTree(t, tree)+ exportDir := filepath.Join(util.TempDir, "export", "Notes")+ partialPath := exportDir + ".sy.zip.partial"+ if err := os.MkdirAll(partialPath, 0755); err != nil {+ t.Fatal(err)+ }+ if archivePath := exportSYZip(boxID, "/", "Notes", []string{tree.Path}, false); archivePath != "" {+ t.Fatalf("expected zip creation failure, got %q", archivePath)+ }+ if _, err := os.Stat(exportDir); !os.IsNotExist(err) {+ t.Fatalf("failed export retained temp files: %v", err)+ }+ if archivePath := exportSYZip(boxID, "/", "Notes", []string{tree.Path}, false); archivePath == "" {+ t.Fatal("export did not recover after failure")+ }+}