mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-22 09:30:14 +01:00
🎨 细化云端同步锁提升稳定性 https://github.com/siyuan-note/siyuan/issues/5887
This commit is contained in:
parent
963b409e4e
commit
11e4c88f5a
15 changed files with 110 additions and 106 deletions
|
|
@ -24,6 +24,7 @@ import (
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||||
"github.com/siyuan-note/siyuan/kernel/model"
|
"github.com/siyuan-note/siyuan/kernel/model"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
)
|
)
|
||||||
|
|
@ -85,7 +86,7 @@ func setFileAnnotation(c *gin.Context) {
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := util.WriteFileSafer(writePath, []byte(data)); nil != err {
|
if err := filesys.WriteFileSafer(writePath, []byte(data)); nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,7 @@ import (
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
|
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||||
"github.com/siyuan-note/siyuan/kernel/model"
|
"github.com/siyuan-note/siyuan/kernel/model"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
)
|
)
|
||||||
|
|
@ -106,7 +107,7 @@ func extensionCopy(c *gin.Context) {
|
||||||
}
|
}
|
||||||
fName = fName + "-" + ast.NewNodeID() + ext
|
fName = fName + "-" + ast.NewNodeID() + ext
|
||||||
writePath := filepath.Join(assets, fName)
|
writePath := filepath.Join(assets, fName)
|
||||||
if err = util.WriteFileSafer(writePath, data); nil != err {
|
if err = filesys.WriteFileSafer(writePath, data); nil != err {
|
||||||
ret.Code = -1
|
ret.Code = -1
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
break
|
break
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"github.com/imroc/req/v3"
|
"github.com/imroc/req/v3"
|
||||||
"github.com/siyuan-note/httpclient"
|
"github.com/siyuan-note/httpclient"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
|
"github.com/siyuan-note/siyuan/kernel/filesys"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
textUnicode "golang.org/x/text/encoding/unicode"
|
textUnicode "golang.org/x/text/encoding/unicode"
|
||||||
"golang.org/x/text/transform"
|
"golang.org/x/text/transform"
|
||||||
|
|
@ -374,7 +375,7 @@ func installPackage(data []byte, installPath string) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
srcPath := filepath.Join(unzipPath, dir)
|
srcPath := filepath.Join(unzipPath, dir)
|
||||||
if err = util.Copy(srcPath, installPath); nil != err {
|
if err = filesys.Copy(srcPath, installPath); nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
|
||||||
67
kernel/filesys/io.go
Normal file
67
kernel/filesys/io.go
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
package filesys
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
|
||||||
|
"github.com/88250/gulu"
|
||||||
|
"github.com/siyuan-note/filelock"
|
||||||
|
"github.com/siyuan-note/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
var writingFileLock = sync.Mutex{}
|
||||||
|
|
||||||
|
func LockWriteFile() {
|
||||||
|
writingFileLock.Lock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func UnlockWriteFile() {
|
||||||
|
writingFileLock.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteFileSaferByReader(writePath string, reader io.Reader) (err error) {
|
||||||
|
writingFileLock.Lock()
|
||||||
|
defer writingFileLock.Unlock()
|
||||||
|
|
||||||
|
if err = gulu.File.WriteFileSaferByReader(writePath, reader, 0644); nil != err {
|
||||||
|
logging.LogErrorf("write file [%s] failed: %s", writePath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteFileSafer(writePath string, data []byte) (err error) {
|
||||||
|
writingFileLock.Lock()
|
||||||
|
defer writingFileLock.Unlock()
|
||||||
|
|
||||||
|
if err = gulu.File.WriteFileSafer(writePath, data, 0644); nil != err {
|
||||||
|
logging.LogErrorf("write file [%s] failed: %s", writePath, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func Copy(source, dest string) (err error) {
|
||||||
|
writingFileLock.Lock()
|
||||||
|
defer writingFileLock.Unlock()
|
||||||
|
|
||||||
|
filelock.ReleaseFileLocks(source)
|
||||||
|
if err = gulu.File.Copy(source, dest); nil != err {
|
||||||
|
logging.LogErrorf("copy [%s] to [%s] failed: %s", source, dest, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoveAll(p string) (err error) {
|
||||||
|
writingFileLock.Lock()
|
||||||
|
defer writingFileLock.Unlock()
|
||||||
|
|
||||||
|
filelock.ReleaseFileLocks(p)
|
||||||
|
if err = os.RemoveAll(p); nil != err {
|
||||||
|
logging.LogErrorf("remove all [%s] failed: %s", p, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
@ -39,6 +39,7 @@ import (
|
||||||
"github.com/siyuan-note/httpclient"
|
"github.com/siyuan-note/httpclient"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
"github.com/siyuan-note/siyuan/kernel/cache"
|
"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/search"
|
||||||
"github.com/siyuan-note/siyuan/kernel/sql"
|
"github.com/siyuan-note/siyuan/kernel/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
|
|
@ -141,7 +142,7 @@ func NetImg2LocalAssets(rootID string) (err error) {
|
||||||
name = util.FilterFileName(name)
|
name = util.FilterFileName(name)
|
||||||
name = "net-img-" + name + "-" + ast.NewNodeID() + ext
|
name = "net-img-" + name + "-" + ast.NewNodeID() + ext
|
||||||
writePath := filepath.Join(util.DataDir, "assets", name)
|
writePath := filepath.Join(util.DataDir, "assets", name)
|
||||||
if err = util.WriteFileSafer(writePath, data); nil != err {
|
if err = filesys.WriteFileSafer(writePath, data); nil != err {
|
||||||
logging.LogErrorf("write downloaded net img [%s] to local assets [%s] failed: %s", u, writePath, err)
|
logging.LogErrorf("write downloaded net img [%s] to local assets [%s] failed: %s", u, writePath, err)
|
||||||
return ast.WalkSkipChildren
|
return ast.WalkSkipChildren
|
||||||
}
|
}
|
||||||
|
|
@ -383,7 +384,7 @@ func saveWorkspaceAssets(assets []string) {
|
||||||
logging.LogErrorf("create assets conf failed: %s", err)
|
logging.LogErrorf("create assets conf failed: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = util.WriteFileSafer(confPath, data); nil != err {
|
if err = filesys.WriteFileSafer(confPath, data); nil != err {
|
||||||
logging.LogErrorf("write assets conf failed: %s", err)
|
logging.LogErrorf("write assets conf failed: %s", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -478,7 +479,7 @@ func RenameAsset(oldPath, newName string) (err error) {
|
||||||
|
|
||||||
newName = util.AssetName(newName) + filepath.Ext(oldPath)
|
newName = util.AssetName(newName) + filepath.Ext(oldPath)
|
||||||
newPath := "assets/" + newName
|
newPath := "assets/" + newName
|
||||||
if err = util.Copy(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); nil != err {
|
if err = filesys.Copy(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); nil != err {
|
||||||
logging.LogErrorf("copy asset [%s] failed: %s", oldPath, err)
|
logging.LogErrorf("copy asset [%s] failed: %s", oldPath, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,7 @@ import (
|
||||||
"github.com/siyuan-note/filelock"
|
"github.com/siyuan-note/filelock"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
"github.com/siyuan-note/siyuan/kernel/conf"
|
"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/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
|
|
@ -202,7 +203,7 @@ func (box *Box) saveConf0(data []byte) {
|
||||||
if err := os.MkdirAll(filepath.Join(util.DataDir, box.ID, ".siyuan"), 0755); nil != err {
|
if err := os.MkdirAll(filepath.Join(util.DataDir, box.ID, ".siyuan"), 0755); nil != err {
|
||||||
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
|
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
|
||||||
}
|
}
|
||||||
if err := util.WriteFileSafer(confPath, data); nil != err {
|
if err := filesys.WriteFileSafer(confPath, data); nil != err {
|
||||||
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
|
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -292,8 +293,8 @@ func (box *Box) Move(oldPath, newPath string) error {
|
||||||
toPath := filepath.Join(boxLocalPath, newPath)
|
toPath := filepath.Join(boxLocalPath, newPath)
|
||||||
filelock.ReleaseFileLocks(fromPath)
|
filelock.ReleaseFileLocks(fromPath)
|
||||||
|
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
if err := os.Rename(fromPath, toPath); nil != err {
|
if err := os.Rename(fromPath, toPath); nil != err {
|
||||||
msg := fmt.Sprintf(Conf.Language(5), box.Name, fromPath, 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)
|
logging.LogErrorf("move [path=%s] in box [%s] failed: %s", fromPath, box.Name, err)
|
||||||
|
|
@ -313,7 +314,7 @@ func (box *Box) Move(oldPath, newPath string) error {
|
||||||
func (box *Box) Remove(path string) error {
|
func (box *Box) Remove(path string) error {
|
||||||
boxLocalPath := filepath.Join(util.DataDir, box.ID)
|
boxLocalPath := filepath.Join(util.DataDir, box.ID)
|
||||||
filePath := filepath.Join(boxLocalPath, path)
|
filePath := filepath.Join(boxLocalPath, path)
|
||||||
if err := util.RemoveAll(filePath); nil != err {
|
if err := filesys.RemoveAll(filePath); nil != err {
|
||||||
msg := fmt.Sprintf(Conf.Language(7), box.Name, path, 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)
|
logging.LogErrorf("remove [path=%s] in box [%s] failed: %s", path, box.ID, err)
|
||||||
return errors.New(msg)
|
return errors.New(msg)
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,7 @@ import (
|
||||||
"github.com/emirpasic/gods/stacks/linkedliststack"
|
"github.com/emirpasic/gods/stacks/linkedliststack"
|
||||||
"github.com/siyuan-note/filelock"
|
"github.com/siyuan-note/filelock"
|
||||||
"github.com/siyuan-note/logging"
|
"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/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
|
|
@ -158,8 +159,8 @@ func exportData(exportFolder string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
|
|
||||||
err = filelock.ReleaseAllFileLocks()
|
err = filelock.ReleaseAllFileLocks()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
|
|
||||||
|
|
@ -1203,7 +1203,7 @@ func RemoveDoc(boxID, p string) (err error) {
|
||||||
|
|
||||||
historyPath := filepath.Join(historyDir, boxID, p)
|
historyPath := filepath.Join(historyDir, boxID, p)
|
||||||
absPath := filepath.Join(util.DataDir, boxID, p)
|
absPath := filepath.Join(util.DataDir, boxID, p)
|
||||||
if err = util.Copy(absPath, historyPath); nil != err {
|
if err = filesys.Copy(absPath, historyPath); nil != err {
|
||||||
return errors.New(fmt.Sprintf(Conf.Language(70), box.Name, absPath, err))
|
return errors.New(fmt.Sprintf(Conf.Language(70), box.Name, absPath, err))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1534,8 +1534,8 @@ func ChangeFileTreeSort(boxID string, paths []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
WaitForWritingFiles()
|
WaitForWritingFiles()
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
|
|
||||||
box := Conf.Box(boxID)
|
box := Conf.Box(boxID)
|
||||||
sortIDs := map[string]int{}
|
sortIDs := map[string]int{}
|
||||||
|
|
|
||||||
|
|
@ -37,6 +37,7 @@ import (
|
||||||
"github.com/siyuan-note/filelock"
|
"github.com/siyuan-note/filelock"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
"github.com/siyuan-note/siyuan/kernel/conf"
|
"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/search"
|
||||||
"github.com/siyuan-note/siyuan/kernel/sql"
|
"github.com/siyuan-note/siyuan/kernel/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
|
|
@ -238,7 +239,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
WaitForWritingFiles()
|
WaitForWritingFiles()
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
|
|
||||||
srcPath := historyPath
|
srcPath := historyPath
|
||||||
var destPath string
|
var destPath string
|
||||||
|
|
@ -249,22 +250,22 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
||||||
workingDoc := treenode.GetBlockTree(id)
|
workingDoc := treenode.GetBlockTree(id)
|
||||||
if nil != workingDoc {
|
if nil != workingDoc {
|
||||||
if err = os.RemoveAll(filepath.Join(util.DataDir, boxID, workingDoc.Path)); nil != err {
|
if err = os.RemoveAll(filepath.Join(util.DataDir, boxID, workingDoc.Path)); nil != err {
|
||||||
util.UnlockWriteFile()
|
filesys.UnlockWriteFile()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
destPath, err = getRollbackDockPath(boxID, historyPath)
|
destPath, err = getRollbackDockPath(boxID, historyPath)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
util.UnlockWriteFile()
|
filesys.UnlockWriteFile()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = gulu.File.Copy(srcPath, destPath); nil != err {
|
if err = gulu.File.Copy(srcPath, destPath); nil != err {
|
||||||
util.UnlockWriteFile()
|
filesys.UnlockWriteFile()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
util.UnlockWriteFile()
|
filesys.UnlockWriteFile()
|
||||||
|
|
||||||
FullReindex()
|
FullReindex()
|
||||||
IncSync()
|
IncSync()
|
||||||
|
|
|
||||||
|
|
@ -259,7 +259,7 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
|
||||||
for _, assets := range assetsDirs {
|
for _, assets := range assetsDirs {
|
||||||
if gulu.File.IsDir(assets) {
|
if gulu.File.IsDir(assets) {
|
||||||
dataAssets := filepath.Join(util.DataDir, "assets")
|
dataAssets := filepath.Join(util.DataDir, "assets")
|
||||||
if err = util.Copy(assets, dataAssets); nil != err {
|
if err = filesys.Copy(assets, dataAssets); nil != err {
|
||||||
logging.LogErrorf("copy assets from [%s] to [%s] failed: %s", assets, dataAssets, err)
|
logging.LogErrorf("copy assets from [%s] to [%s] failed: %s", assets, dataAssets, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -267,8 +267,8 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
|
||||||
os.RemoveAll(assets)
|
os.RemoveAll(assets)
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
|
|
||||||
filelock.ReleaseAllFileLocks()
|
filelock.ReleaseAllFileLocks()
|
||||||
|
|
||||||
|
|
@ -331,8 +331,8 @@ func ImportData(zipPath string) (err error) {
|
||||||
return errors.New("invalid data.zip")
|
return errors.New("invalid data.zip")
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
|
|
||||||
filelock.ReleaseAllFileLocks()
|
filelock.ReleaseAllFileLocks()
|
||||||
tmpDataPath := filepath.Join(unzipPath, dirs[0].Name())
|
tmpDataPath := filepath.Join(unzipPath, dirs[0].Name())
|
||||||
|
|
|
||||||
|
|
@ -29,13 +29,12 @@ import (
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/siyuan-note/filelock"
|
"github.com/siyuan-note/filelock"
|
||||||
"github.com/siyuan-note/logging"
|
"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/treenode"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateBox(name string) (id string, err error) {
|
func CreateBox(name string) (id string, err error) {
|
||||||
WaitForWritingFiles()
|
|
||||||
|
|
||||||
id = ast.NewNodeID()
|
id = ast.NewNodeID()
|
||||||
boxLocalPath := filepath.Join(util.DataDir, id)
|
boxLocalPath := filepath.Join(util.DataDir, id)
|
||||||
err = os.MkdirAll(boxLocalPath, 0755)
|
err = os.MkdirAll(boxLocalPath, 0755)
|
||||||
|
|
@ -52,10 +51,6 @@ func CreateBox(name string) (id string, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func RenameBox(boxID, name string) (err error) {
|
func RenameBox(boxID, name string) (err error) {
|
||||||
WaitForWritingFiles()
|
|
||||||
util.LockWriteFile()
|
|
||||||
defer util.UnlockWriteFile()
|
|
||||||
|
|
||||||
box := Conf.Box(boxID)
|
box := Conf.Box(boxID)
|
||||||
if nil == box {
|
if nil == box {
|
||||||
return errors.New(Conf.Language(0))
|
return errors.New(Conf.Language(0))
|
||||||
|
|
@ -103,7 +98,7 @@ func RemoveBox(boxID string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
unmount0(boxID)
|
unmount0(boxID)
|
||||||
if err = os.RemoveAll(localPath); nil != err {
|
if err = filesys.RemoveAll(localPath); nil != err {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
IncSync()
|
IncSync()
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ import (
|
||||||
"github.com/siyuan-note/httpclient"
|
"github.com/siyuan-note/httpclient"
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
"github.com/siyuan-note/siyuan/kernel/cache"
|
"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/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
|
|
@ -221,8 +222,8 @@ func CheckoutRepo(id string) (err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
util.PushEndlessProgress(Conf.Language(63))
|
util.PushEndlessProgress(Conf.Language(63))
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
WaitForWritingFiles()
|
WaitForWritingFiles()
|
||||||
sql.WaitForWritingDatabase()
|
sql.WaitForWritingDatabase()
|
||||||
filelock.ReleaseAllFileLocks()
|
filelock.ReleaseAllFileLocks()
|
||||||
|
|
|
||||||
|
|
@ -32,6 +32,7 @@ import (
|
||||||
"github.com/dustin/go-humanize"
|
"github.com/dustin/go-humanize"
|
||||||
"github.com/siyuan-note/dejavu"
|
"github.com/siyuan-note/dejavu"
|
||||||
"github.com/siyuan-note/logging"
|
"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/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
|
|
@ -63,8 +64,8 @@ func SyncData(boot, exit, byHand bool) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
util.LockWriteFile()
|
filesys.LockWriteFile()
|
||||||
defer util.UnlockWriteFile()
|
defer filesys.UnlockWriteFile()
|
||||||
|
|
||||||
if util.IsMutexLocked(&syncLock) {
|
if util.IsMutexLocked(&syncLock) {
|
||||||
logging.LogWarnf("sync is in progress")
|
logging.LogWarnf("sync is in progress")
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import (
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/siyuan-note/logging"
|
"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/sql"
|
||||||
"github.com/siyuan-note/siyuan/kernel/treenode"
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
||||||
"github.com/siyuan-note/siyuan/kernel/util"
|
"github.com/siyuan-note/siyuan/kernel/util"
|
||||||
|
|
@ -85,7 +86,7 @@ func InsertLocalAssets(id string, assetPaths []string) (succMap map[string]inter
|
||||||
f.Close()
|
f.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err = util.WriteFileSaferByReader(writePath, f); nil != err {
|
if err = filesys.WriteFileSaferByReader(writePath, f); nil != err {
|
||||||
f.Close()
|
f.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -168,7 +169,7 @@ func Upload(c *gin.Context) {
|
||||||
f.Close()
|
f.Close()
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err = util.WriteFileSaferByReader(writePath, f); nil != err {
|
if err = filesys.WriteFileSaferByReader(writePath, f); nil != err {
|
||||||
errFiles = append(errFiles, fName)
|
errFiles = append(errFiles, fName)
|
||||||
ret.Msg = err.Error()
|
ret.Msg = err.Error()
|
||||||
f.Close()
|
f.Close()
|
||||||
|
|
|
||||||
|
|
@ -17,85 +17,17 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/88250/gulu"
|
"github.com/88250/gulu"
|
||||||
"github.com/88250/lute/ast"
|
"github.com/88250/lute/ast"
|
||||||
"github.com/siyuan-note/filelock"
|
|
||||||
"github.com/siyuan-note/logging"
|
"github.com/siyuan-note/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
var writingFileLock = sync.Mutex{}
|
|
||||||
|
|
||||||
func LockWriteFile() {
|
|
||||||
if IsMutexLocked(&writingFileLock) {
|
|
||||||
logging.LogWarnf("write file is locked")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
writingFileLock.Lock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func UnlockWriteFile() {
|
|
||||||
if !IsMutexLocked(&writingFileLock) {
|
|
||||||
logging.LogWarnf("write file is not locked")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
writingFileLock.Unlock()
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteFileSaferByReader(writePath string, reader io.Reader) (err error) {
|
|
||||||
writingFileLock.Lock()
|
|
||||||
defer writingFileLock.Unlock()
|
|
||||||
|
|
||||||
if err = gulu.File.WriteFileSaferByReader(writePath, reader, 0644); nil != err {
|
|
||||||
logging.LogErrorf("write file [%s] failed: %s", writePath, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func WriteFileSafer(writePath string, data []byte) (err error) {
|
|
||||||
writingFileLock.Lock()
|
|
||||||
defer writingFileLock.Unlock()
|
|
||||||
|
|
||||||
if err = gulu.File.WriteFileSafer(writePath, data, 0644); nil != err {
|
|
||||||
logging.LogErrorf("write file [%s] failed: %s", writePath, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func Copy(source, dest string) (err error) {
|
|
||||||
writingFileLock.Lock()
|
|
||||||
defer writingFileLock.Unlock()
|
|
||||||
|
|
||||||
filelock.ReleaseFileLocks(source)
|
|
||||||
if err = gulu.File.Copy(source, dest); nil != err {
|
|
||||||
logging.LogErrorf("copy [%s] to [%s] failed: %s", source, dest, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func RemoveAll(p string) (err error) {
|
|
||||||
writingFileLock.Lock()
|
|
||||||
defer writingFileLock.Unlock()
|
|
||||||
|
|
||||||
filelock.ReleaseFileLocks(p)
|
|
||||||
if err = os.RemoveAll(p); nil != err {
|
|
||||||
logging.LogErrorf("remove all [%s] failed: %s", p, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsEmptyDir(p string) bool {
|
func IsEmptyDir(p string) bool {
|
||||||
if !gulu.File.IsDir(p) {
|
if !gulu.File.IsDir(p) {
|
||||||
return false
|
return false
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue