mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-16 22:50:13 +01:00
🎨 不再自动从临时文件中恢复数据文件 Fix https://github.com/siyuan-note/siyuan/issues/7260
This commit is contained in:
parent
e7f2fb9c9c
commit
ae9ba8496f
3 changed files with 5 additions and 86 deletions
|
|
@ -24,7 +24,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/88250/lute"
|
"github.com/88250/lute"
|
||||||
"github.com/88250/lute/parse"
|
"github.com/88250/lute/parse"
|
||||||
|
|
@ -45,10 +44,7 @@ func LoadTree(boxID, p string, luteEngine *lute.Lute) (ret *parse.Tree, err erro
|
||||||
}
|
}
|
||||||
ret = parseJSON2Tree(boxID, p, data, luteEngine)
|
ret = parseJSON2Tree(boxID, p, data, luteEngine)
|
||||||
if nil == ret {
|
if nil == ret {
|
||||||
ret = recoverParseJSON2Tree(boxID, p, filePath, luteEngine)
|
return nil, errors.New("parse tree failed")
|
||||||
if nil == ret {
|
|
||||||
return nil, errors.New("parse tree failed")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ret.Path = p
|
ret.Path = p
|
||||||
ret.Root.Path = p
|
ret.Root.Path = p
|
||||||
|
|
@ -185,47 +181,6 @@ func afterWriteTree(tree *parse.Tree) {
|
||||||
cache.PutDocIAL(tree.Path, docIAL)
|
cache.PutDocIAL(tree.Path, docIAL)
|
||||||
}
|
}
|
||||||
|
|
||||||
func recoverParseJSON2Tree(boxID, p, filePath string, luteEngine *lute.Lute) (ret *parse.Tree) {
|
|
||||||
// 尝试从临时文件恢复
|
|
||||||
tmp := util.LatestTmpFile(filePath)
|
|
||||||
if "" == tmp {
|
|
||||||
logging.LogWarnf("recover tree [%s] not found tmp", filePath)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
stat, err := os.Stat(filePath)
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("stat tmp [%s] failed: %s", tmp, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if stat.ModTime().Before(time.Now().Add(-time.Hour * 24)) {
|
|
||||||
logging.LogWarnf("tmp [%s] is too old, remove it", tmp)
|
|
||||||
os.RemoveAll(tmp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
data, err := filelock.ReadFile(tmp)
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("recover tree read from tmp [%s] failed: %s", tmp, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err = filelock.WriteFile(filePath, data); nil != err {
|
|
||||||
logging.LogErrorf("recover tree write [%s] from tmp [%s] failed: %s", filePath, tmp, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = parseJSON2Tree(boxID, p, data, luteEngine)
|
|
||||||
if nil == ret {
|
|
||||||
logging.LogErrorf("recover tree from tmp [%s] parse failed, remove it", tmp)
|
|
||||||
os.RemoveAll(tmp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
logging.LogInfof("recovered tree [%s] from [%s]", filePath, tmp)
|
|
||||||
os.RemoveAll(tmp)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (ret *parse.Tree) {
|
func parseJSON2Tree(boxID, p string, jsonData []byte, luteEngine *lute.Lute) (ret *parse.Tree) {
|
||||||
var err error
|
var err error
|
||||||
var needFix bool
|
var needFix bool
|
||||||
|
|
|
||||||
|
|
@ -245,7 +245,10 @@ func (box *Box) Ls(p string) (ret []*FileInfo, totals int, err error) {
|
||||||
}
|
}
|
||||||
if strings.HasSuffix(name, ".tmp") {
|
if strings.HasSuffix(name, ".tmp") {
|
||||||
// 移除写入失败时产生的临时文件
|
// 移除写入失败时产生的临时文件
|
||||||
os.Remove(filepath.Join(util.DataDir, box.ID, p, name))
|
removePath := filepath.Join(util.DataDir, box.ID, p, name)
|
||||||
|
if removeErr := os.Remove(removePath); nil != removeErr {
|
||||||
|
logging.LogWarnf("remove tmp file [%s] failed: %s", removePath, removeErr)
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,6 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
|
|
||||||
|
|
@ -79,44 +78,6 @@ func LastID(p string) (name, id string) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func LatestTmpFile(p string) string {
|
|
||||||
dir, base := filepath.Split(p)
|
|
||||||
files, err := os.ReadDir(dir)
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read dir [%s] failed: %s", dir, err)
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
var tmps []os.DirEntry
|
|
||||||
for _, f := range files {
|
|
||||||
if f.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(f.Name(), ".tmp") && strings.HasPrefix(f.Name(), base) && len(base)+7+len(".tmp") == len(f.Name()) {
|
|
||||||
tmps = append(tmps, f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if 1 > len(tmps) {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(tmps, func(i, j int) bool {
|
|
||||||
info1, err := tmps[i].Info()
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read file info [%s] failed: %s", tmps[i].Name(), err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
info2, err := tmps[j].Info()
|
|
||||||
if nil != err {
|
|
||||||
logging.LogErrorf("read file info [%s] failed: %s", tmps[j].Name(), err)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return info1.ModTime().After(info2.ModTime())
|
|
||||||
})
|
|
||||||
return filepath.Join(dir, tmps[0].Name())
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsCorruptedSYData(data []byte) bool {
|
func IsCorruptedSYData(data []byte) bool {
|
||||||
if 64 > len(data) || '{' != data[0] {
|
if 64 > len(data) || '{' != data[0] {
|
||||||
return true
|
return true
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue