From 0658f3bc0e2ee305e96988c56a8cb8ba2f37be73 Mon Sep 17 00:00:00 2001 From: Daniel <845765@qq.com> Date: Tue, 14 Nov 2023 11:08:39 +0800 Subject: [PATCH] :art: Automatically create notebook conf.json if not found it https://github.com/siyuan-note/siyuan/issues/9647 --- kernel/model/box.go | 38 +++++++++++++++++++++++--------------- kernel/model/conf.go | 7 ++++++- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/kernel/model/box.go b/kernel/model/box.go index d057cbbeb..4ee696cd8 100644 --- a/kernel/model/box.go +++ b/kernel/model/box.go @@ -111,32 +111,40 @@ func ListNotebooks() (ret []*Box, err error) { boxConf := conf.NewBoxConf() boxDirPath := filepath.Join(util.DataDir, dir.Name()) boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json") - if !filelock.IsExist(boxConfPath) { + isExistConf := filelock.IsExist(boxConfPath) + if !isExistConf { // 数据同步时展开文档树操作可能导致数据丢失 https://github.com/siyuan-note/siyuan/issues/7129 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) - os.RemoveAll(boxConfPath) - 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) + os.RemoveAll(boxConfPath) + continue + } } id := dir.Name() - ret = append(ret, &Box{ + box := &Box{ ID: id, Name: boxConf.Name, Icon: boxConf.Icon, Sort: boxConf.Sort, SortMode: boxConf.SortMode, Closed: boxConf.Closed, - }) + } + + if !isExistConf { + // Automatically create notebook conf.json if not found it https://github.com/siyuan-note/siyuan/issues/9647 + box.SaveConf(boxConf) + box.Unindex() + logging.LogWarnf("fixed a corrupted box [%s]", boxDirPath) + } + ret = append(ret, box) } switch Conf.FileTree.Sort { diff --git a/kernel/model/conf.go b/kernel/model/conf.go index fad41e45b..e2aa41709 100644 --- a/kernel/model/conf.go +++ b/kernel/model/conf.go @@ -902,7 +902,12 @@ func upgradeUserGuide() { boxConf := conf.NewBoxConf() boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json") if !filelock.IsExist(boxConfPath) { - logging.LogWarnf("found a corrupted box [%s]", boxDirPath) + logging.LogWarnf("found a corrupted user guide box [%s]", boxDirPath) + if removeErr := filelock.Remove(boxDirPath); nil != removeErr { + logging.LogErrorf("remove corrupted user guide box [%s] failed: %s", boxDirPath, removeErr) + } else { + logging.LogInfof("removed corrupted user guide box [%s]", boxDirPath) + } continue }