mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 07:00:12 +01:00
🐛 数据同步时展开文档树操作可能导致数据丢失 Fix https://github.com/siyuan-note/siyuan/issues/7129
This commit is contained in:
parent
25852c3e2c
commit
71a8c4e31b
2 changed files with 56 additions and 24 deletions
|
|
@ -114,31 +114,18 @@ func ListNotebooks() (ret []*Box, err error) {
|
||||||
boxDirPath := filepath.Join(util.DataDir, dir.Name())
|
boxDirPath := filepath.Join(util.DataDir, dir.Name())
|
||||||
boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")
|
boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")
|
||||||
if !gulu.File.IsExist(boxConfPath) {
|
if !gulu.File.IsExist(boxConfPath) {
|
||||||
if IsUserGuide(dir.Name()) {
|
logging.LogWarnf("found a corrupted box [%s]", boxDirPath)
|
||||||
filelock.Remove(boxDirPath)
|
continue
|
||||||
continue
|
}
|
||||||
}
|
|
||||||
to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), dir.Name())
|
data, readErr := filelock.ReadFile(boxConfPath)
|
||||||
if copyErr := filelock.Copy(boxDirPath, to); nil != copyErr {
|
if nil != readErr {
|
||||||
logging.LogErrorf("copy corrupted box [%s] failed: %s", boxDirPath, copyErr)
|
logging.LogErrorf("read box conf [%s] failed: %s", boxConfPath, readErr)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if removeErr := filelock.Remove(boxDirPath); nil != removeErr {
|
if readErr = gulu.JSON.UnmarshalJSON(data, boxConf); nil != readErr {
|
||||||
logging.LogErrorf("remove corrupted box [%s] failed: %s", boxDirPath, removeErr)
|
logging.LogErrorf("parse box conf [%s] failed: %s", boxConfPath, readErr)
|
||||||
continue
|
|
||||||
}
|
|
||||||
logging.LogWarnf("moved corrupted box [%s] to [%s]", boxDirPath, to)
|
|
||||||
continue
|
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()
|
id := dir.Name()
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ package model
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/88250/lute/ast"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
@ -432,6 +433,7 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
|
||||||
treenode.SaveBlockTree(false)
|
treenode.SaveBlockTree(false)
|
||||||
SaveAssetsTexts()
|
SaveAssetsTexts()
|
||||||
clearWorkspaceTemp()
|
clearWorkspaceTemp()
|
||||||
|
clearCorruptedNotebooks()
|
||||||
clearPortJSON()
|
clearPortJSON()
|
||||||
util.UnlockWorkspace()
|
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() {
|
func clearWorkspaceTemp() {
|
||||||
os.RemoveAll(filepath.Join(util.TempDir, "bazaar"))
|
os.RemoveAll(filepath.Join(util.TempDir, "bazaar"))
|
||||||
os.RemoveAll(filepath.Join(util.TempDir, "export"))
|
os.RemoveAll(filepath.Join(util.TempDir, "export"))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue