🎨 不再自动从临时文件中恢复数据文件 Fix https://github.com/siyuan-note/siyuan/issues/7260

This commit is contained in:
Liang Ding 2023-02-05 11:11:32 +08:00
parent e7f2fb9c9c
commit ae9ba8496f
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 5 additions and 86 deletions

View file

@ -24,7 +24,6 @@ import (
"os"
"path/filepath"
"strings"
"time"
"github.com/88250/lute"
"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)
if nil == ret {
ret = recoverParseJSON2Tree(boxID, p, filePath, luteEngine)
if nil == ret {
return nil, errors.New("parse tree failed")
}
return nil, errors.New("parse tree failed")
}
ret.Path = p
ret.Root.Path = p
@ -185,47 +181,6 @@ func afterWriteTree(tree *parse.Tree) {
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) {
var err error
var needFix bool

View file

@ -245,7 +245,10 @@ func (box *Box) Ls(p string) (ret []*FileInfo, totals int, err error) {
}
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
}

View file

@ -22,7 +22,6 @@ import (
"os"
"path"
"path/filepath"
"sort"
"strings"
"unicode/utf8"
@ -79,44 +78,6 @@ func LastID(p string) (name, id string) {
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 {
if 64 > len(data) || '{' != data[0] {
return true