🎨 细化云端同步锁提升稳定性 https://github.com/siyuan-note/siyuan/issues/5887

This commit is contained in:
Liang Ding 2022-09-18 09:14:34 +08:00
parent 963b409e4e
commit 11e4c88f5a
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
15 changed files with 110 additions and 106 deletions

View file

@ -17,85 +17,17 @@
package util
import (
"io"
"os"
"path"
"path/filepath"
"sort"
"strings"
"sync"
"github.com/88250/gulu"
"github.com/88250/lute/ast"
"github.com/siyuan-note/filelock"
"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 {
if !gulu.File.IsDir(p) {
return false