mirror of
https://github.com/siyuan-note/siyuan.git
synced 2025-12-17 23:20:13 +01:00
🎨 细化云端同步锁提升稳定性 https://github.com/siyuan-note/siyuan/issues/5887
This commit is contained in:
parent
3468fb0976
commit
9ab7030a81
10 changed files with 50 additions and 41 deletions
|
|
@ -478,7 +478,7 @@ func RenameAsset(oldPath, newName string) (err error) {
|
|||
|
||||
newName = util.AssetName(newName) + filepath.Ext(oldPath)
|
||||
newPath := "assets/" + newName
|
||||
if err = gulu.File.Copy(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); nil != err {
|
||||
if err = util.Copy(filepath.Join(util.DataDir, oldPath), filepath.Join(util.DataDir, newPath)); nil != err {
|
||||
logging.LogErrorf("copy asset [%s] failed: %s", oldPath, err)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -202,7 +202,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 := filelock.NoLockFileWrite(confPath, data); nil != err {
|
||||
if err := util.WriteFileSafer(confPath, data); nil != err {
|
||||
logging.LogErrorf("save box conf [%s] failed: %s", confPath, err)
|
||||
}
|
||||
}
|
||||
|
|
@ -292,8 +292,8 @@ func (box *Box) Move(oldPath, newPath string) error {
|
|||
toPath := filepath.Join(boxLocalPath, newPath)
|
||||
filelock.ReleaseFileLocks(fromPath)
|
||||
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
if err := os.Rename(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)
|
||||
|
|
|
|||
|
|
@ -158,8 +158,8 @@ func exportData(exportFolder string) (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
|
||||
err = filelock.ReleaseAllFileLocks()
|
||||
if nil != err {
|
||||
|
|
|
|||
|
|
@ -1534,8 +1534,8 @@ func ChangeFileTreeSort(boxID string, paths []string) {
|
|||
}
|
||||
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
|
||||
box := Conf.Box(boxID)
|
||||
sortIDs := map[string]int{}
|
||||
|
|
|
|||
|
|
@ -238,7 +238,7 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
|||
}
|
||||
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
util.LockWriteFile()
|
||||
|
||||
srcPath := historyPath
|
||||
var destPath string
|
||||
|
|
@ -249,22 +249,22 @@ func RollbackDocHistory(boxID, historyPath string) (err error) {
|
|||
workingDoc := treenode.GetBlockTree(id)
|
||||
if nil != workingDoc {
|
||||
if err = os.RemoveAll(filepath.Join(util.DataDir, boxID, workingDoc.Path)); nil != err {
|
||||
util.WritingFileLock.Unlock()
|
||||
util.UnlockWriteFile()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
destPath, err = getRollbackDockPath(boxID, historyPath)
|
||||
if nil != err {
|
||||
util.WritingFileLock.Unlock()
|
||||
util.UnlockWriteFile()
|
||||
return
|
||||
}
|
||||
|
||||
if err = gulu.File.Copy(srcPath, destPath); nil != err {
|
||||
util.WritingFileLock.Unlock()
|
||||
util.UnlockWriteFile()
|
||||
return
|
||||
}
|
||||
util.WritingFileLock.Unlock()
|
||||
util.UnlockWriteFile()
|
||||
|
||||
FullReindex()
|
||||
IncSync()
|
||||
|
|
|
|||
|
|
@ -259,7 +259,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 = gulu.File.Copy(assets, dataAssets); nil != err {
|
||||
if err = util.Copy(assets, dataAssets); nil != err {
|
||||
logging.LogErrorf("copy assets from [%s] to [%s] failed: %s", assets, dataAssets, err)
|
||||
return
|
||||
}
|
||||
|
|
@ -267,8 +267,8 @@ func ImportSY(zipPath, boxID, toPath string) (err error) {
|
|||
os.RemoveAll(assets)
|
||||
}
|
||||
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
|
||||
filelock.ReleaseAllFileLocks()
|
||||
|
||||
|
|
@ -331,8 +331,8 @@ func ImportData(zipPath string) (err error) {
|
|||
return errors.New("invalid data.zip")
|
||||
}
|
||||
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
|
||||
filelock.ReleaseAllFileLocks()
|
||||
tmpDataPath := filepath.Join(unzipPath, dirs[0].Name())
|
||||
|
|
|
|||
|
|
@ -35,8 +35,6 @@ import (
|
|||
|
||||
func CreateBox(name string) (id string, err error) {
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
|
||||
id = ast.NewNodeID()
|
||||
boxLocalPath := filepath.Join(util.DataDir, id)
|
||||
|
|
@ -55,8 +53,8 @@ func CreateBox(name string) (id string, err error) {
|
|||
|
||||
func RenameBox(boxID, name string) (err error) {
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
|
||||
box := Conf.Box(boxID)
|
||||
if nil == box {
|
||||
|
|
@ -73,8 +71,6 @@ func RenameBox(boxID, name string) (err error) {
|
|||
|
||||
func RemoveBox(boxID string) (err error) {
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
|
||||
if util.IsReservedFilename(boxID) {
|
||||
return errors.New(fmt.Sprintf("can not remove [%s] caused by it is a reserved file", boxID))
|
||||
|
|
@ -116,8 +112,6 @@ func RemoveBox(boxID string) (err error) {
|
|||
|
||||
func Unmount(boxID string) {
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
|
||||
unmount0(boxID)
|
||||
evt := util.NewCmdResult("unmount", 0, util.PushModeBroadcast, 0)
|
||||
|
|
@ -142,8 +136,6 @@ func unmount0(boxID string) {
|
|||
|
||||
func Mount(boxID string) (alreadyMount bool, err error) {
|
||||
WaitForWritingFiles()
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
|
||||
localPath := filepath.Join(util.DataDir, boxID)
|
||||
|
||||
|
|
|
|||
|
|
@ -221,8 +221,8 @@ func CheckoutRepo(id string) (err error) {
|
|||
}
|
||||
|
||||
util.PushEndlessProgress(Conf.Language(63))
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
WaitForWritingFiles()
|
||||
sql.WaitForWritingDatabase()
|
||||
filelock.ReleaseAllFileLocks()
|
||||
|
|
|
|||
|
|
@ -63,8 +63,8 @@ func SyncData(boot, exit, byHand bool) {
|
|||
return
|
||||
}
|
||||
|
||||
util.WritingFileLock.Lock()
|
||||
defer util.WritingFileLock.Unlock()
|
||||
util.LockWriteFile()
|
||||
defer util.UnlockWriteFile()
|
||||
|
||||
if util.IsMutexLocked(&syncLock) {
|
||||
logging.LogWarnf("sync is in progress")
|
||||
|
|
|
|||
|
|
@ -31,11 +31,28 @@ import (
|
|||
"github.com/siyuan-note/logging"
|
||||
)
|
||||
|
||||
var WritingFileLock = sync.Mutex{}
|
||||
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()
|
||||
writingFileLock.Lock()
|
||||
defer writingFileLock.Unlock()
|
||||
|
||||
if err = gulu.File.WriteFileSaferByReader(writePath, reader, 0644); nil != err {
|
||||
logging.LogErrorf("write file [%s] failed: %s", writePath, err)
|
||||
|
|
@ -45,8 +62,8 @@ func WriteFileSaferByReader(writePath string, reader io.Reader) (err error) {
|
|||
}
|
||||
|
||||
func WriteFileSafer(writePath string, data []byte) (err error) {
|
||||
WritingFileLock.Lock()
|
||||
defer WritingFileLock.Unlock()
|
||||
writingFileLock.Lock()
|
||||
defer writingFileLock.Unlock()
|
||||
|
||||
if err = gulu.File.WriteFileSafer(writePath, data, 0644); nil != err {
|
||||
logging.LogErrorf("write file [%s] failed: %s", writePath, err)
|
||||
|
|
@ -56,8 +73,8 @@ func WriteFileSafer(writePath string, data []byte) (err error) {
|
|||
}
|
||||
|
||||
func Copy(source, dest string) (err error) {
|
||||
WritingFileLock.Lock()
|
||||
defer WritingFileLock.Unlock()
|
||||
writingFileLock.Lock()
|
||||
defer writingFileLock.Unlock()
|
||||
|
||||
filelock.ReleaseFileLocks(source)
|
||||
if err = gulu.File.Copy(source, dest); nil != err {
|
||||
|
|
@ -68,8 +85,8 @@ func Copy(source, dest string) (err error) {
|
|||
}
|
||||
|
||||
func RemoveAll(p string) (err error) {
|
||||
WritingFileLock.Lock()
|
||||
defer WritingFileLock.Unlock()
|
||||
writingFileLock.Lock()
|
||||
defer writingFileLock.Unlock()
|
||||
|
||||
filelock.ReleaseFileLocks(p)
|
||||
if err = os.RemoveAll(p); nil != err {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue