From 71a8c4e31ba7d3608ad06376a84ac8dcad4dbd86 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 20 Jan 2023 00:11:22 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E6=95=B0=E6=8D=AE=E5=90=8C=E6=AD=A5?= =?UTF-8?q?=E6=97=B6=E5=B1=95=E5=BC=80=E6=96=87=E6=A1=A3=E6=A0=91=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E4=B8=A2=E5=A4=B1=20Fix=20https://github.com/siyuan-note/siyua?= =?UTF-8?q?n/issues/7129?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/box.go | 35 +++++++++++----------------------- kernel/model/conf.go | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/kernel/model/box.go b/kernel/model/box.go index 700cfea57..64caaf5c9 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -114,31 +114,18 @@ func ListNotebooks() (ret []*Box, err error) { boxDirPath := filepath.Join(util.DataDir, dir.Name()) boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json") if !gulu.File.IsExist(boxConfPath) { - if IsUserGuide(dir.Name()) { - filelock.Remove(boxDirPath) - continue - } - to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), dir.Name()) - if copyErr := filelock.Copy(boxDirPath, to); nil != copyErr { - logging.LogErrorf("copy corrupted box [%s] failed: %s", boxDirPath, copyErr) - continue - } - if removeErr := filelock.Remove(boxDirPath); nil != removeErr { - logging.LogErrorf("remove corrupted box [%s] failed: %s", boxDirPath, removeErr) - continue - } - logging.LogWarnf("moved corrupted box [%s] to [%s]", boxDirPath, to) + logging.LogWarnf("found a corrupted box [%s]", boxDirPath) + continue + } + + data, readErr := filelock.ReadFile(boxConfPath) + if nil != readErr { + logging.LogErrorf("read box conf [%s] failed: %s", boxConfPath, readErr) + continue + } + if readErr = gulu.JSON.UnmarshalJSON(data, boxConf); nil != readErr { + logging.LogErrorf("parse box conf [%s] failed: %s", boxConfPath, readErr) continue - } else { - data, readErr := filelock.ReadFile(boxConfPath) - if nil != readErr { - logging.LogErrorf("read box conf [%s] failed: %s", boxConfPath, readErr) - continue - } - if readErr = gulu.JSON.UnmarshalJSON(data, boxConf); nil != readErr { - logging.LogErrorf("parse box conf [%s] failed: %s", boxConfPath, readErr) - continue - } } id := dir.Name() diff --git a/kernel/model/conf.go b/kernel/model/conf.go index 9b9271475..17497bd3e 100644 --- a/kernel/model/conf.go +++ b/kernel/model/conf.go @@ -19,6 +19,7 @@ package model import ( "bytes" "fmt" + "github.com/88250/lute/ast" "os" "path/filepath" "runtime" @@ -432,6 +433,7 @@ func Close(force bool, execInstallPkg int) (exitCode int) { treenode.SaveBlockTree(false) SaveAssetsTexts() clearWorkspaceTemp() + clearCorruptedNotebooks() clearPortJSON() util.UnlockWorkspace() @@ -675,6 +677,49 @@ func clearPortJSON() { } } +func clearCorruptedNotebooks() { + // 数据同步时展开文档树操作可能导致数据丢失 https://github.com/siyuan-note/siyuan/issues/7129 + + dirs, err := os.ReadDir(util.DataDir) + if nil != err { + logging.LogErrorf("read dir [%s] failed: %s", util.DataDir, err) + return + } + for _, dir := range dirs { + if util.IsReservedFilename(dir.Name()) { + continue + } + + if !dir.IsDir() { + continue + } + + if !ast.IsNodeIDPattern(dir.Name()) { + continue + } + + boxDirPath := filepath.Join(util.DataDir, dir.Name()) + boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json") + if !gulu.File.IsExist(boxConfPath) { + if IsUserGuide(dir.Name()) { + filelock.Remove(boxDirPath) + continue + } + to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), dir.Name()) + if copyErr := filelock.Copy(boxDirPath, to); nil != copyErr { + logging.LogErrorf("copy corrupted box [%s] failed: %s", boxDirPath, copyErr) + continue + } + if removeErr := filelock.Remove(boxDirPath); nil != removeErr { + logging.LogErrorf("remove corrupted box [%s] failed: %s", boxDirPath, removeErr) + continue + } + logging.LogWarnf("moved corrupted box [%s] to [%s]", boxDirPath, to) + continue + } + } +} + func clearWorkspaceTemp() { os.RemoveAll(filepath.Join(util.TempDir, "bazaar")) os.RemoveAll(filepath.Join(util.TempDir, "export"))