From 8f3dd9fbf44a39445411a4fbb1925194951e8dc9 Mon Sep 17 00:00:00 2001 From: Liang Ding Date: Fri, 9 Sep 2022 17:11:49 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E6=8D=9F=E5=9D=8F=E7=9A=84=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86?= =?UTF-8?q?=20Fix=20https://github.com/siyuan-note/siyuan/issues/5853?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/model/box.go | 4 ++-- kernel/model/file.go | 30 +++++++++++++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/kernel/model/box.go b/kernel/model/box.go index fab24fdff..5f9b5bc93 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -92,8 +92,8 @@ func ListNotebooks() (ret []*Box, err error) { continue } to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), dir.Name()) - if renameErr := gulu.File.CopyDir(boxDirPath, to); nil != renameErr { - logging.LogErrorf("copy corrupted box [%s] failed: %s", boxDirPath, renameErr) + if copyErr := gulu.File.CopyDir(boxDirPath, to); nil != copyErr { + logging.LogErrorf("copy corrupted box [%s] failed: %s", boxDirPath, copyErr) continue } if removeErr := os.RemoveAll(boxDirPath); nil != removeErr { diff --git a/kernel/model/file.go b/kernel/model/file.go index e8c09432a..220336f1c 100644 --- a/kernel/model/file.go +++ b/kernel/model/file.go @@ -137,12 +137,7 @@ func (box *Box) docIAL(p string) (ret map[string]string) { data, err := filelock.NoLockFileRead(filePath) if util.IsCorruptedSYData(data) { - filelock.UnlockFile(filePath) - if removeErr := os.RemoveAll(filePath); nil == removeErr { - logging.LogInfof("removed corrupted data file [path=%s, length=%d]", filePath, len(data)) - } else { - logging.LogWarnf("remove corrupted data file [path=%s, length=%d] failed: %s", filePath, len(data), removeErr) - } + box.moveCorruptedData(filePath) return nil } if nil != err { @@ -152,12 +147,28 @@ func (box *Box) docIAL(p string) (ret map[string]string) { ret = readDocIAL(data) if nil == ret { logging.LogWarnf("tree [%s] is corrupted", filePath) + box.moveCorruptedData(filePath) return nil } cache.PutDocIAL(p, ret) return ret } +func (box *Box) moveCorruptedData(filePath string) { + filelock.UnlockFile(filePath) + base := filepath.Base(filePath) + to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), box.ID, base) + if copyErr := gulu.File.CopyFile(filePath, to); nil != copyErr { + logging.LogErrorf("copy corrupted data file [%s] failed: %s", filePath, copyErr) + return + } + if removeErr := os.RemoveAll(filePath); nil != removeErr { + logging.LogErrorf("remove corrupted data file [%s] failed: %s", filePath, removeErr) + return + } + logging.LogWarnf("moved corrupted data file [%s] to [%s]", filePath, to) +} + func readDocIAL(data []byte) (ret map[string]string) { doc := map[string]interface{}{} if err := gulu.JSON.UnmarshalJSON(data, &doc); nil != err { @@ -165,7 +176,12 @@ func readDocIAL(data []byte) (ret map[string]string) { return nil } - props := doc["Properties"].(map[string]interface{}) + propsArg := doc["Properties"] + if nil == propsArg { + return nil + } + + props := propsArg.(map[string]interface{}) ret = map[string]string{} for k, v := range props { ret[k] = v.(string)