This commit is contained in:
Liang Ding 2022-09-29 21:52:01 +08:00
parent 56129699b9
commit 69a9713776
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
38 changed files with 193 additions and 438 deletions

View file

@ -39,7 +39,6 @@ import (
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
@ -142,7 +141,7 @@ func NetImg2LocalAssets(rootID string) (err error) {
name = util.FilterFileName(name)
name = "net-img-" + name + "-" + ast.NewNodeID() + ext
writePath := filepath.Join(util.DataDir, "assets", name)
if err = filesys.WriteFileSafer(writePath, data); nil != err {
if err = filelock.WriteFile(writePath, data); nil != err {
logging.LogErrorf("write downloaded net img [%s] to local assets [%s] failed: %s", u, writePath, err)
return ast.WalkSkipChildren
}
@ -384,7 +383,7 @@ func saveWorkspaceAssets(assets []string) {
logging.LogErrorf("create assets conf failed: %s", err)
return
}
if err = filesys.WriteFileSafer(confPath, data); nil != err {
if err = filelock.WriteFile(confPath, data); nil != err {
logging.LogErrorf("write assets conf failed: %s", err)
return
}
@ -479,7 +478,7 @@ func RenameAsset(oldPath, newName string) (err error) {
newName = util.AssetName(newName) + filepath.Ext(oldPath)
newPath := "assets/" + newName
if err = filesys.Copy(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); nil != err {
if err = filelock.Copy(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); nil != err {
logging.LogErrorf("copy asset [%s] failed: %s", oldPath, err)
return
}
@ -493,7 +492,7 @@ func RenameAsset(oldPath, newName string) (err error) {
pages := pagedPaths(filepath.Join(util.DataDir, notebook.ID), 32)
for _, paths := range pages {
for _, treeAbsPath := range paths {
data, readErr := filelock.NoLockFileRead(treeAbsPath)
data, readErr := filelock.ReadFile(treeAbsPath)
if nil != readErr {
logging.LogErrorf("get data [path=%s] failed: %s", treeAbsPath, readErr)
err = readErr
@ -505,7 +504,7 @@ func RenameAsset(oldPath, newName string) (err error) {
}
data = bytes.Replace(data, []byte(oldName), []byte(newName), -1)
if writeErr := filelock.NoLockFileWrite(treeAbsPath, data); nil != writeErr {
if writeErr := filelock.WriteFile(treeAbsPath, data); nil != writeErr {
logging.LogErrorf("write data [path=%s] failed: %s", treeAbsPath, writeErr)
err = writeErr
return
@ -835,8 +834,6 @@ func copyDocAssetsToDataAssets(boxID, parentDocPath string) {
}
func copyAssetsToDataAssets(rootPath string) {
filelock.ReleaseFileLocks(rootPath)
var assetsDirPaths []string
filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
if rootPath == path || nil == info {
@ -861,7 +858,7 @@ func copyAssetsToDataAssets(rootPath string) {
dataAssetsPath := filepath.Join(util.DataDir, "assets")
for _, assetsDirPath := range assetsDirPaths {
if err := gulu.File.Copy(assetsDirPath, dataAssetsPath); nil != err {
if err := filelock.Copy(assetsDirPath, dataAssetsPath); nil != err {
logging.LogErrorf("copy tree assets from [%s] to [%s] failed: %s", assetsDirPaths, dataAssetsPath, err)
}
}

View file

@ -35,7 +35,6 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -87,24 +86,23 @@ func ListNotebooks() (ret []*Box, err error) {
boxDirPath := filepath.Join(util.DataDir, dir.Name())
boxConfPath := filepath.Join(boxDirPath, ".siyuan", "conf.json")
if !gulu.File.IsExist(boxConfPath) {
filelock.ReleaseAllFileLocks()
if IsUserGuide(dir.Name()) {
os.RemoveAll(boxDirPath)
filelock.Remove(boxDirPath)
continue
}
to := filepath.Join(util.WorkspaceDir, "corrupted", time.Now().Format("2006-01-02-150405"), dir.Name())
if copyErr := gulu.File.CopyDir(boxDirPath, to); nil != copyErr {
if copyErr := filelock.Copy(boxDirPath, to); nil != copyErr {
logging.LogErrorf("copy corrupted box [%s] failed: %s", boxDirPath, copyErr)
continue
}
if removeErr := os.RemoveAll(boxDirPath); nil != removeErr {
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
} else {
data, readErr := filelock.NoLockFileRead(boxConfPath)
data, readErr := filelock.ReadFile(boxConfPath)
if nil != readErr {
logging.LogErrorf("read box conf [%s] failed: %s", boxConfPath, readErr)
continue
@ -164,7 +162,7 @@ func (box *Box) GetConf() (ret *conf.BoxConf) {
return
}
data, err := filelock.NoLockFileRead(confPath)
data, err := filelock.ReadFile(confPath)
if nil != err {
logging.LogErrorf("read box conf [%s] failed: %s", confPath, err)
return
@ -185,7 +183,7 @@ func (box *Box) SaveConf(conf *conf.BoxConf) {
return
}
oldData, err := filelock.NoLockFileRead(confPath)
oldData, err := filelock.ReadFile(confPath)
if nil != err {
box.saveConf0(newData)
return
@ -203,7 +201,7 @@ func (box *Box) saveConf0(data []byte) {
if err := os.MkdirAll(filepath.Join(util.DataDir, box.ID, ".siyuan"), 0755); nil != err {
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
}
if err := filesys.WriteFileSafer(confPath, data); nil != err {
if err := filelock.WriteFile(confPath, data); nil != err {
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
}
}
@ -226,16 +224,22 @@ func (box *Box) Ls(p string) (ret []*FileInfo, totals int, err error) {
}
for _, f := range files {
if util.IsReservedFilename(f.Name()) {
name := f.Name()
if util.IsReservedFilename(name) {
continue
}
if strings.HasSuffix(name, ".tmp") {
// 移除写入失败时产生的临时文件
os.Remove(filepath.Join(util.DataDir, box.ID, p, name))
continue
}
totals += 1
fi := &FileInfo{}
fi.name = f.Name()
fi.name = name
fi.isdir = f.IsDir()
fi.size = f.Size()
fPath := path.Join(p, f.Name())
fPath := path.Join(p, name)
if f.IsDir() {
fPath += "/"
}
@ -291,11 +295,8 @@ func (box *Box) Move(oldPath, newPath string) error {
boxLocalPath := filepath.Join(util.DataDir, box.ID)
fromPath := filepath.Join(boxLocalPath, oldPath)
toPath := filepath.Join(boxLocalPath, newPath)
filelock.ReleaseFileLocks(fromPath)
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
if err := os.Rename(fromPath, toPath); nil != err {
if err := filelock.Move(fromPath, toPath); nil != err {
msg := fmt.Sprintf(Conf.Language(5), box.Name, fromPath, err)
logging.LogErrorf("move [path=%s] in box [%s] failed: %s", fromPath, box.Name, err)
return errors.New(msg)
@ -304,7 +305,7 @@ func (box *Box) Move(oldPath, newPath string) error {
if oldDir := path.Dir(oldPath); util.IsIDPattern(path.Base(oldDir)) {
fromDir := filepath.Join(boxLocalPath, oldDir)
if util.IsEmptyDir(fromDir) {
os.Remove(fromDir)
filelock.Remove(fromDir)
}
}
IncSync()
@ -314,7 +315,7 @@ func (box *Box) Move(oldPath, newPath string) error {
func (box *Box) Remove(path string) error {
boxLocalPath := filepath.Join(util.DataDir, box.ID)
filePath := filepath.Join(boxLocalPath, path)
if err := filesys.RemoveAll(filePath); nil != err {
if err := filelock.Remove(filePath); nil != err {
msg := fmt.Sprintf(Conf.Language(7), box.Name, path, err)
logging.LogErrorf("remove [path=%s] in box [%s] failed: %s", path, box.ID, err)
return errors.New(msg)
@ -331,7 +332,6 @@ func (box *Box) Unindex() {
sql.RemoveBoxHash(tx, box.ID)
sql.DeleteByBoxTx(tx, box.ID)
sql.CommitTx(tx)
filelock.ReleaseFileLocks(filepath.Join(util.DataDir, box.ID))
treenode.RemoveBlockTreesByBoxID(box.ID)
}
@ -533,7 +533,7 @@ func (box *Box) UpdateHistoryGenerated() {
boxLatestHistoryTime[box.ID] = time.Now()
}
func LockFileByBlockID(id string) (locked bool, filePath string) {
func TryAccessFileByBlockID(id string) (ok bool) {
bt := treenode.GetBlockTree(id)
if nil == bt {
return
@ -541,7 +541,7 @@ func LockFileByBlockID(id string) (locked bool, filePath string) {
p := filepath.Join(util.DataDir, bt.BoxID, bt.Path)
if !gulu.File.IsExist(p) {
return true, ""
return false
}
return nil == filelock.LockFile(p), p
return true
}

View file

@ -362,7 +362,6 @@ func Close(force bool, execInstallPkg int) (exitCode int) {
logging.LogInfof("exiting kernel [force=%v, execInstallPkg=%d]", force, execInstallPkg)
treenode.CloseBlockTree()
util.PushMsg(Conf.Language(95), 10000*60)
WaitForWritingFiles()
if !force {
@ -435,7 +434,7 @@ func (conf *AppConf) Save() {
newData, _ := gulu.JSON.MarshalIndentJSON(Conf, "", " ")
confPath := filepath.Join(util.ConfDir, "conf.json")
oldData, err := filelock.NoLockFileRead(confPath)
oldData, err := filelock.ReadFile(confPath)
if nil != err {
conf.save0(newData)
return
@ -450,7 +449,7 @@ func (conf *AppConf) Save() {
func (conf *AppConf) save0(data []byte) {
confPath := filepath.Join(util.ConfDir, "conf.json")
if err := filelock.NoLockFileWrite(confPath, data); nil != err {
if err := filelock.WriteFile(confPath, data); nil != err {
logging.LogFatalf("write conf [%s] failed: %s", confPath, err)
}
}

View file

@ -42,7 +42,6 @@ import (
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -159,16 +158,8 @@ func exportData(exportFolder string) (err error) {
return
}
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
err = filelock.ReleaseAllFileLocks()
if nil != err {
return
}
data := filepath.Join(util.WorkspaceDir, "data")
if err = stableCopy(data, exportFolder); nil != err {
if err = filelock.RoboCopy(data, exportFolder); nil != err {
logging.LogErrorf("copy data dir from [%s] to [%s] failed: %s", data, baseFolderName, err)
err = errors.New(fmt.Sprintf(Conf.Language(14), formatErrorMsg(err)))
return
@ -224,7 +215,7 @@ func ExportDocx(id, savePath string, removeAssets bool) (err error) {
}
pandoc := exec.Command(Conf.Export.PandocBin, args...)
util.CmdAttr(pandoc)
gulu.CmdAttr(pandoc)
pandoc.Stdin = bytes.NewBufferString(content)
output, err := pandoc.CombinedOutput()
if nil != err {
@ -819,7 +810,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
// 按文件夹结构复制选择的树
for _, tree := range trees {
readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
data, readErr := filelock.NoLockFileRead(readPath)
data, readErr := filelock.ReadFile(readPath)
if nil != readErr {
logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
continue
@ -841,7 +832,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
// 引用树放在导出文件夹根路径下
for treeID, tree := range refTrees {
readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
data, readErr := filelock.NoLockFileRead(readPath)
data, readErr := filelock.ReadFile(readPath)
if nil != readErr {
logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
continue
@ -903,7 +894,7 @@ func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (
var sortData []byte
var sortErr error
if gulu.File.IsExist(sortPath) {
sortData, sortErr = filelock.NoLockFileRead(sortPath)
sortData, sortErr = filelock.ReadFile(sortPath)
if nil != sortErr {
logging.LogErrorf("read sort conf failed: %s", sortErr)
}

View file

@ -134,7 +134,7 @@ func (box *Box) docIAL(p string) (ret map[string]string) {
filePath := filepath.Join(util.DataDir, box.ID, p)
data, err := filelock.NoLockFileRead(filePath)
data, err := filelock.ReadFile(filePath)
if util.IsCorruptedSYData(data) {
box.moveCorruptedData(filePath)
return nil
@ -154,14 +154,13 @@ func (box *Box) docIAL(p string) (ret map[string]string) {
}
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 {
if copyErr := filelock.Copy(filePath, to); nil != copyErr {
logging.LogErrorf("copy corrupted data file [%s] failed: %s", filePath, copyErr)
return
}
if removeErr := os.RemoveAll(filePath); nil != removeErr {
if removeErr := filelock.Remove(filePath); nil != removeErr {
logging.LogErrorf("remove corrupted data file [%s] failed: %s", filePath, removeErr)
return
}
@ -1126,12 +1125,10 @@ func MoveDoc(fromBoxID, fromPath, toBoxID, toPath string) (newPath string, err e
} else {
absFromPath := filepath.Join(util.DataDir, fromBoxID, fromFolder)
absToPath := filepath.Join(util.DataDir, toBoxID, newFolder)
filelock.ReleaseFileLocks(absFromPath)
if gulu.File.IsExist(absToPath) {
filelock.ReleaseFileLocks(absToPath)
os.RemoveAll(absToPath)
filelock.Remove(absToPath)
}
if err = os.Rename(absFromPath, absToPath); nil != err {
if err = filelock.Move(absFromPath, absToPath); nil != err {
msg := fmt.Sprintf(Conf.Language(5), fromBox.Name, fromPath, err)
logging.LogErrorf("move [path=%s] in box [%s] failed: %s", fromPath, fromBoxID, err)
err = errors.New(msg)
@ -1156,8 +1153,7 @@ func MoveDoc(fromBoxID, fromPath, toBoxID, toPath string) (newPath string, err e
} else {
absFromPath := filepath.Join(util.DataDir, fromBoxID, fromPath)
absToPath := filepath.Join(util.DataDir, toBoxID, newPath)
filelock.ReleaseFileLocks(absFromPath)
if err = os.Rename(absFromPath, absToPath); nil != err {
if err = filelock.Move(absFromPath, absToPath); nil != err {
msg := fmt.Sprintf(Conf.Language(5), fromBox.Name, fromPath, err)
logging.LogErrorf("move [path=%s] in box [%s] failed: %s", fromPath, fromBoxID, err)
err = errors.New(msg)
@ -1198,7 +1194,7 @@ func RemoveDoc(boxID, p string) (err error) {
historyPath := filepath.Join(historyDir, boxID, p)
absPath := filepath.Join(util.DataDir, boxID, p)
if err = filesys.Copy(absPath, historyPath); nil != err {
if err = filelock.Copy(absPath, historyPath); nil != err {
return errors.New(fmt.Sprintf(Conf.Language(70), box.Name, absPath, err))
}
@ -1211,7 +1207,7 @@ func RemoveDoc(boxID, p string) (err error) {
if existChildren {
absChildrenDir := filepath.Join(util.DataDir, tree.Box, childrenDir)
historyPath = filepath.Join(historyDir, tree.Box, childrenDir)
if err = gulu.File.Copy(absChildrenDir, historyPath); nil != err {
if err = filelock.Copy(absChildrenDir, historyPath); nil != err {
return
}
}
@ -1456,7 +1452,7 @@ func moveSorts(rootID, fromBox, toBox string) {
fromConfPath := filepath.Join(util.DataDir, fromBox, ".siyuan", "sort.json")
fromFullSortIDs := map[string]int{}
if gulu.File.IsExist(fromConfPath) {
data, err := filelock.NoLockFileRead(fromConfPath)
data, err := filelock.ReadFile(fromConfPath)
if nil != err {
logging.LogErrorf("read sort conf failed: %s", err)
return
@ -1473,7 +1469,7 @@ func moveSorts(rootID, fromBox, toBox string) {
toConfPath := filepath.Join(util.DataDir, toBox, ".siyuan", "sort.json")
toFullSortIDs := map[string]int{}
if gulu.File.IsExist(toConfPath) {
data, err := filelock.NoLockFileRead(toConfPath)
data, err := filelock.ReadFile(toConfPath)
if nil != err {
logging.LogErrorf("read sort conf failed: %s", err)
return
@ -1494,7 +1490,7 @@ func moveSorts(rootID, fromBox, toBox string) {
logging.LogErrorf("marshal sort conf failed: %s", err)
return
}
if err = filelock.NoLockFileWrite(toConfPath, data); nil != err {
if err = filelock.WriteFile(toConfPath, data); nil != err {
logging.LogErrorf("write sort conf failed: %s", err)
return
}
@ -1529,9 +1525,6 @@ func ChangeFileTreeSort(boxID string, paths []string) {
}
WaitForWritingFiles()
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
box := Conf.Box(boxID)
sortIDs := map[string]int{}
max := 0
@ -1575,7 +1568,7 @@ func ChangeFileTreeSort(boxID string, paths []string) {
fullSortIDs := map[string]int{}
var data []byte
if gulu.File.IsExist(confPath) {
data, err = filelock.NoLockFileRead(confPath)
data, err = filelock.ReadFile(confPath)
if nil != err {
logging.LogErrorf("read sort conf failed: %s", err)
return
@ -1595,7 +1588,7 @@ func ChangeFileTreeSort(boxID string, paths []string) {
logging.LogErrorf("marshal sort conf failed: %s", err)
return
}
if err = filelock.NoLockFileWrite(confPath, data); nil != err {
if err = filelock.WriteFile(confPath, data); nil != err {
logging.LogErrorf("write sort conf failed: %s", err)
return
}
@ -1609,7 +1602,7 @@ func (box *Box) fillSort(files *[]*File) {
return
}
data, err := filelock.NoLockFileRead(confPath)
data, err := filelock.ReadFile(confPath)
if nil != err {
logging.LogErrorf("read sort conf failed: %s", err)
return
@ -1656,7 +1649,7 @@ func (box *Box) removeSort(rootID, path string) {
return
}
data, err := filelock.NoLockFileRead(confPath)
data, err := filelock.ReadFile(confPath)
if nil != err {
logging.LogErrorf("read sort conf failed: %s", err)
return
@ -1677,7 +1670,7 @@ func (box *Box) removeSort(rootID, path string) {
logging.LogErrorf("marshal sort conf failed: %s", err)
return
}
if err = filelock.NoLockFileWrite(confPath, data); nil != err {
if err = filelock.WriteFile(confPath, data); nil != err {
logging.LogErrorf("write sort conf failed: %s", err)
return
}
@ -1685,16 +1678,6 @@ func (box *Box) removeSort(rootID, path string) {
func ServeFile(c *gin.Context, filePath string) (err error) {
WaitForWritingFiles()
if filelock.IsLocked(filePath) {
if err = filelock.UnlockFile(filePath); nil == err {
logging.LogInfof("unlocked file [%s]", filePath)
} else {
msg := fmt.Sprintf("unlock file [%s] failed: %s", filePath, err)
logging.LogErrorf(msg)
return errors.New(msg)
}
}
c.File(filePath)
return
}

View file

@ -95,7 +95,7 @@ func generateFormatHistory(tree *parse.Tree) {
}
var data []byte
if data, err = filelock.NoLockFileRead(filepath.Join(util.DataDir, tree.Box, tree.Path)); err != nil {
if data, err = filelock.ReadFile(filepath.Join(util.DataDir, tree.Box, tree.Path)); err != nil {
logging.LogErrorf("generate history failed: %s", err)
return
}

View file

@ -116,9 +116,9 @@ func Doc2Heading(srcID, targetID string, after bool) (srcTreeBox, srcTreePath st
if !util.IsEmptyDir(subDir) {
err = errors.New(Conf.Language(20))
return
} else {
os.Remove(subDir) // 移除空文件夹不会有副作用
}
os.Remove(subDir) // 移除空文件夹不会有副作用
}
targetTree, _ := loadTreeByBlockID(targetID)

View file

@ -37,7 +37,6 @@ import (
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/conf"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
@ -147,7 +146,7 @@ func GetDocHistoryContent(historyPath, keyword string) (id, rootID, content stri
return
}
data, err := filelock.NoLockFileRead(historyPath)
data, err := filelock.ReadFile(historyPath)
if nil != err {
logging.LogErrorf("read file [%s] failed: %s", historyPath, err)
return
@ -234,33 +233,27 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
}
WaitForWritingFiles()
filesys.LockWriteFile()
srcPath := historyPath
var destPath string
baseName := filepath.Base(historyPath)
id := strings.TrimSuffix(baseName, ".sy")
filelock.ReleaseFileLocks(filepath.Join(util.DataDir, boxID))
workingDoc := treenode.GetBlockTree(id)
if nil != workingDoc {
if err = os.RemoveAll(filepath.Join(util.DataDir, boxID, workingDoc.Path)); nil != err {
filesys.UnlockWriteFile()
if err = filelock.Remove(filepath.Join(util.DataDir, boxID, workingDoc.Path)); nil != err {
return
}
}
destPath, err = getRollbackDockPath(boxID, historyPath)
if nil != err {
filesys.UnlockWriteFile()
return
}
if err = gulu.File.Copy(srcPath, destPath); nil != err {
filesys.UnlockWriteFile()
if err = filelock.Copy(srcPath, destPath); nil != err {
return
}
filesys.UnlockWriteFile()
FullReindex()
IncSync()
@ -446,7 +439,7 @@ func (box *Box) generateDocHistory0() {
}
var data []byte
if data, err = filelock.NoLockFileRead(file); err != nil {
if data, err = filelock.ReadFile(file); err != nil {
logging.LogErrorf("generate history failed: %s", err)
return
}

View file

@ -168,7 +168,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
return
}
newSyPath := filepath.Join(filepath.Dir(syPath), tree.ID+".sy")
if err = os.Rename(syPath, newSyPath); nil != err {
if err = filelock.Move(syPath, newSyPath); nil != err {
logging.LogErrorf("rename .sy from [%s] to [%s] failed: %s", syPath, newSyPath, err)
return
}
@ -181,7 +181,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
var sortErr error
sortPath := filepath.Join(unzipRootPath, ".siyuan", "sort.json")
if gulu.File.IsExist(sortPath) {
sortData, sortErr = filelock.NoLockFileRead(sortPath)
sortData, sortErr = filelock.ReadFile(sortPath)
if nil != sortErr {
logging.LogErrorf("read import sort conf failed: %s", sortErr)
}
@ -192,7 +192,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
sortPath = filepath.Join(util.DataDir, boxID, ".siyuan", "sort.json")
if gulu.File.IsExist(sortPath) {
sortData, sortErr = filelock.NoLockFileRead(sortPath)
sortData, sortErr = filelock.ReadFile(sortPath)
if nil != sortErr {
logging.LogErrorf("read box sort conf failed: %s", sortErr)
}
@ -212,7 +212,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
if nil != sortErr {
logging.LogErrorf("marshal box sort conf failed: %s", sortErr)
} else {
sortErr = filelock.NoLockFileWrite(sortPath, sortData)
sortErr = filelock.WriteFile(sortPath, sortData)
if nil != sortErr {
logging.LogErrorf("write box sort conf failed: %s", sortErr)
}
@ -265,7 +265,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
})
for i, oldPath := range oldPaths {
newPath := renamePaths[oldPath]
if err = os.Rename(oldPath, newPath); nil != err {
if err = filelock.Move(oldPath, newPath); nil != err {
logging.LogErrorf("rename path from [%s] to [%s] failed: %s", oldPath, renamePaths[oldPath], err)
return errors.New("rename path failed")
}
@ -304,7 +304,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
for _, assets := range assetsDirs {
if gulu.File.IsDir(assets) {
dataAssets := filepath.Join(util.DataDir, "assets")
if err = filesys.Copy(assets, dataAssets); nil != err {
if err = filelock.Copy(assets, dataAssets); nil != err {
logging.LogErrorf("copy assets from [%s] to [%s] failed: %s", assets, dataAssets, err)
return
}
@ -312,11 +312,6 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
os.RemoveAll(assets)
}
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
filelock.ReleaseAllFileLocks()
var baseTargetPath string
if "/" == toPath {
baseTargetPath = "/"
@ -334,7 +329,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
return
}
if err = stableCopy(unzipRootPath, targetDir); nil != err {
if err = filelock.RoboCopy(unzipRootPath, targetDir); nil != err {
logging.LogErrorf("copy data dir from [%s] to [%s] failed: %s", unzipRootPath, util.DataDir, err)
err = errors.New("copy data failed")
return
@ -376,12 +371,8 @@ func ImportData(zipPath string) (err error) {
return errors.New("invalid data.zip")
}
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
filelock.ReleaseAllFileLocks()
tmpDataPath := filepath.Join(unzipPath, dirs[0].Name())
if err = stableCopy(tmpDataPath, util.DataDir); nil != err {
if err = filelock.RoboCopy(tmpDataPath, util.DataDir); nil != err {
logging.LogErrorf("copy data dir from [%s] to [%s] failed: %s", tmpDataPath, util.DataDir, err)
err = errors.New("copy data failed")
return
@ -533,7 +524,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
name = filepath.Base(fullPath)
name = util.AssetName(name)
assetTargetPath := filepath.Join(assetDirPath, name)
if err = gulu.File.Copy(fullPath, assetTargetPath); nil != err {
if err = filelock.Copy(fullPath, assetTargetPath); nil != err {
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", fullPath, assetTargetPath, err)
return ast.WalkContinue
}
@ -622,7 +613,7 @@ func ImportFromLocalPath(boxID, localPath string, toPath string) (err error) {
name := filepath.Base(absolutePath)
name = util.AssetName(name)
assetTargetPath := filepath.Join(assetDirPath, name)
if err = gulu.File.CopyFile(absolutePath, assetTargetPath); nil != err {
if err = filelock.Copy(absolutePath, assetTargetPath); nil != err {
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", absolutePath, assetTargetPath, err)
return ast.WalkContinue
}

View file

@ -32,7 +32,6 @@ import (
"github.com/dustin/go-humanize"
"github.com/emirpasic/gods/sets/hashset"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
@ -95,7 +94,6 @@ func (box *Box) Index(fullRebuildIndex bool) (treeCount int, treeSize int64) {
idHashMap[tree.ID] = tree.Hash
if 1 < i && 0 == i%64 {
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(88), i, len(files)-i))
filelock.ReleaseAllFileLocks()
}
i++
}
@ -175,7 +173,6 @@ func (box *Box) Index(fullRebuildIndex bool) (treeCount int, treeSize int64) {
}
if 1 < i && 0 == i%64 {
util.PushEndlessProgress(fmt.Sprintf("["+box.Name+"] "+Conf.Language(53), i, treeCount-i))
filelock.ReleaseAllFileLocks()
}
i++
}
@ -314,7 +311,6 @@ func IndexRefs() {
}
if 1 < i && 0 == i%64 {
util.PushEndlessProgress(fmt.Sprintf(Conf.Language(55), i))
filelock.ReleaseAllFileLocks()
}
i++
}

View file

@ -29,7 +29,6 @@ import (
"github.com/88250/lute/ast"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
)
@ -79,7 +78,6 @@ func RemoveBox(boxID string) (err error) {
return errors.New(fmt.Sprintf("can not remove [%s] caused by it is not a dir", boxID))
}
filelock.ReleaseFileLocks(localPath)
if !IsUserGuide(boxID) {
var historyDir string
historyDir, err = GetHistoryDir(HistoryOpDelete)
@ -89,7 +87,7 @@ func RemoveBox(boxID string) (err error) {
}
p := strings.TrimPrefix(localPath, util.DataDir)
historyPath := filepath.Join(historyDir, p)
if err = gulu.File.Copy(localPath, historyPath); nil != err {
if err = filelock.Copy(localPath, historyPath); nil != err {
logging.LogErrorf("gen sync history failed: %s", err)
return
}
@ -98,7 +96,7 @@ func RemoveBox(boxID string) (err error) {
}
unmount0(boxID)
if err = filesys.RemoveAll(localPath); nil != err {
if err = filelock.Remove(localPath); nil != err {
return
}
IncSync()
@ -144,12 +142,12 @@ func Mount(boxID string) (alreadyMount bool, err error) {
reMountGuide = true
}
if err = os.RemoveAll(localPath); nil != err {
if err = filelock.Remove(localPath); nil != err {
return
}
p := filepath.Join(util.WorkingDir, "guide", boxID)
if err = gulu.File.Copy(p, localPath); nil != err {
if err = filelock.Copy(p, localPath); nil != err {
return
}

View file

@ -36,11 +36,9 @@ import (
"github.com/siyuan-note/dejavu/entity"
"github.com/siyuan-note/encryption"
"github.com/siyuan-note/eventbus"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/httpclient"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/cache"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -222,11 +220,8 @@ func CheckoutRepo(id string) (err error) {
}
util.PushEndlessProgress(Conf.Language(63))
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
WaitForWritingFiles()
sql.WaitForWritingDatabase()
filelock.ReleaseAllFileLocks()
CloseWatchAssets()
defer WatchAssets()
@ -451,7 +446,6 @@ func IndexRepo(memo string) (err error) {
start := time.Now()
latest, _ := repo.Latest()
WaitForWritingFiles()
filelock.ReleaseAllFileLocks()
index, err := repo.Index(memo, map[string]interface{}{
eventbus.CtxPushMsg: eventbus.CtxPushMsgToStatusBarAndProgress,
})

View file

@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
@ -32,7 +31,6 @@ import (
"github.com/dustin/go-humanize"
"github.com/siyuan-note/dejavu"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -64,9 +62,6 @@ func SyncData(boot, exit, byHand bool) {
return
}
filesys.LockWriteFile()
defer filesys.UnlockWriteFile()
if util.IsMutexLocked(&syncLock) {
logging.LogWarnf("sync is in progress")
planSyncAfter(30 * time.Second)
@ -411,38 +406,6 @@ func IncSync() {
planSyncAfter(30 * time.Second)
}
func stableCopy(src, dest string) (err error) {
if gulu.OS.IsWindows() {
robocopy := "robocopy"
cmd := exec.Command(robocopy, src, dest, "/DCOPY:T", "/E", "/IS", "/R:0", "/NFL", "/NDL", "/NJH", "/NJS", "/NP", "/NS", "/NC")
util.CmdAttr(cmd)
var output []byte
output, err = cmd.CombinedOutput()
if strings.Contains(err.Error(), "exit status 16") {
// 某些版本的 Windows 无法同步 https://github.com/siyuan-note/siyuan/issues/4197
return gulu.File.Copy(src, dest)
}
if nil != err && strings.Contains(err.Error(), exec.ErrNotFound.Error()) {
robocopy = os.Getenv("SystemRoot") + "\\System32\\" + "robocopy"
cmd = exec.Command(robocopy, src, dest, "/DCOPY:T", "/E", "/IS", "/R:0", "/NFL", "/NDL", "/NJH", "/NJS", "/NP", "/NS", "/NC")
util.CmdAttr(cmd)
output, err = cmd.CombinedOutput()
}
if nil == err ||
strings.Contains(err.Error(), "exit status 3") ||
strings.Contains(err.Error(), "exit status 1") ||
strings.Contains(err.Error(), "exit status 2") ||
strings.Contains(err.Error(), "exit status 5") ||
strings.Contains(err.Error(), "exit status 6") ||
strings.Contains(err.Error(), "exit status 7") {
return nil
}
logging.LogErrorf("robocopy data from [%s] to [%s] failed: %s %s", src, dest, string(output), err)
}
return gulu.File.Copy(src, dest)
}
func planSyncAfter(d time.Duration) {
syncPlanTime = time.Now().Add(d)
}

View file

@ -20,7 +20,6 @@ import (
"bytes"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sync"
@ -123,7 +122,7 @@ func flushTx() {
case TxErrCodeBlockNotFound:
util.PushTxErr("Transaction failed", txErr.code, nil)
return
case TxErrCodeUnableLockFile:
case TxErrCodeUnableAccessFile:
util.PushTxErr(Conf.Language(76), txErr.code, txErr.id)
return
default:
@ -175,9 +174,9 @@ func PerformTransactions(transactions *[]*Transaction) (err error) {
}
const (
TxErrCodeBlockNotFound = 0
TxErrCodeUnableLockFile = 1
TxErrCodeWriteTree = 2
TxErrCodeBlockNotFound = 0
TxErrCodeUnableAccessFile = 1
TxErrCodeWriteTree = 2
)
type TxErr struct {
@ -244,6 +243,10 @@ func performTx(tx *Transaction) (ret *TxErr) {
}
if cr := tx.commit(); nil != cr {
if errors.Is(cr, filelock.ErrUnableAccessFile) {
return &TxErr{code: TxErrCodeUnableAccessFile, msg: cr.Error()}
}
logging.LogErrorf("commit tx failed: %s", cr)
return &TxErr{msg: cr.Error()}
}
@ -415,8 +418,8 @@ func (tx *Transaction) doPrependInsert(operation *Operation) (ret *TxErr) {
return &TxErr{code: TxErrCodeBlockNotFound, id: operation.ParentID}
}
tree, err := tx.loadTree(block.ID)
if errors.Is(err, filelock.ErrUnableLockFile) {
return &TxErr{code: TxErrCodeUnableLockFile, msg: err.Error(), id: block.ID}
if errors.Is(err, filelock.ErrUnableAccessFile) {
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: block.ID}
}
if nil != err {
msg := fmt.Sprintf("load tree [id=%s] failed: %s", block.ID, err)
@ -503,8 +506,8 @@ func (tx *Transaction) doAppendInsert(operation *Operation) (ret *TxErr) {
return &TxErr{code: TxErrCodeBlockNotFound, id: operation.ParentID}
}
tree, err := tx.loadTree(block.ID)
if errors.Is(err, filelock.ErrUnableLockFile) {
return &TxErr{code: TxErrCodeUnableLockFile, msg: err.Error(), id: block.ID}
if errors.Is(err, filelock.ErrUnableAccessFile) {
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: block.ID}
}
if nil != err {
msg := fmt.Sprintf("load tree [id=%s] failed: %s", block.ID, err)
@ -661,8 +664,8 @@ func (tx *Transaction) doDelete(operation *Operation) (ret *TxErr) {
var err error
id := operation.ID
tree, err := tx.loadTree(id)
if errors.Is(err, filelock.ErrUnableLockFile) {
return &TxErr{code: TxErrCodeUnableLockFile, msg: err.Error(), id: id}
if errors.Is(err, filelock.ErrUnableAccessFile) {
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: id}
}
if ErrBlockNotFound == err {
return nil // move 以后这里会空,算作正常情况
@ -712,8 +715,8 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
}
}
tree, err := tx.loadTree(block.ID)
if errors.Is(err, filelock.ErrUnableLockFile) {
return &TxErr{code: TxErrCodeUnableLockFile, msg: err.Error(), id: block.ID}
if errors.Is(err, filelock.ErrUnableAccessFile) {
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: block.ID}
}
if nil != err {
msg := fmt.Sprintf("load tree [id=%s] failed: %s", block.ID, err)
@ -750,7 +753,7 @@ func (tx *Transaction) doInsert(operation *Operation) (ret *TxErr) {
// 只有全局 assets 才移动到相对 assets
targetP := filepath.Join(assets, filepath.Base(assetPath))
if e = os.Rename(assetPath, targetP); nil != err {
if e = filelock.Move(assetPath, targetP); nil != err {
logging.LogErrorf("copy path of asset from [%s] to [%s] failed: %s", assetPath, targetP, err)
return ast.WalkContinue
}
@ -846,8 +849,8 @@ func (tx *Transaction) doUpdate(operation *Operation) (ret *TxErr) {
id := operation.ID
tree, err := tx.loadTree(id)
if errors.Is(err, filelock.ErrUnableLockFile) {
return &TxErr{code: TxErrCodeUnableLockFile, msg: err.Error(), id: id}
if errors.Is(err, filelock.ErrUnableAccessFile) {
return &TxErr{code: TxErrCodeUnableAccessFile, msg: err.Error(), id: id}
}
if nil != err {
logging.LogErrorf("load tree [id=%s] failed: %s", id, err)

View file

@ -122,7 +122,7 @@ func pagedPaths(localPath string, pageSize int) (ret map[int][]string) {
}
func loadTree(localPath string, luteEngine *lute.Lute) (ret *parse.Tree, err error) {
data, err := filelock.NoLockFileRead(localPath)
data, err := filelock.ReadFile(localPath)
if nil != err {
logging.LogErrorf("get data [path=%s] failed: %s", localPath, err)
return

View file

@ -45,7 +45,7 @@ func execNewVerInstallPkg(newVerInstallPkgPath string) {
} else if gulu.OS.IsLinux() {
cmd = exec.Command("sh", "-c", newVerInstallPkgPath)
}
util.CmdAttr(cmd)
gulu.CmdAttr(cmd)
cmdErr := cmd.Start()
if nil != cmdErr {
logging.LogErrorf("exec install new version failed: %s", cmdErr)

View file

@ -27,8 +27,8 @@ import (
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/gin-gonic/gin"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/filesys"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
"github.com/siyuan-note/siyuan/kernel/util"
@ -95,7 +95,7 @@ func InsertLocalAssets(id string, assetPaths []string, isUpload bool) (succMap m
f.Close()
return
}
if err = filesys.WriteFileSaferByReader(writePath, f); nil != err {
if err = filelock.WriteFileByReader(writePath, f); nil != err {
f.Close()
return
}
@ -180,7 +180,7 @@ func Upload(c *gin.Context) {
f.Close()
break
}
if err = filesys.WriteFileSaferByReader(writePath, f); nil != err {
if err = filelock.WriteFileByReader(writePath, f); nil != err {
errFiles = append(errFiles, fName)
ret.Msg = err.Error()
f.Close()