diff --git a/kernel/api/workspace.go b/kernel/api/workspace.go index be62245a8..6b07968c7 100644 --- a/kernel/api/workspace.go +++ b/kernel/api/workspace.go @@ -27,7 +27,7 @@ import ( "github.com/88250/gulu" "github.com/gin-gonic/gin" - "github.com/gofrs/flock" + "github.com/siyuan-note/logging" "github.com/siyuan-note/siyuan/kernel/model" "github.com/siyuan-note/siyuan/kernel/util" ) @@ -89,6 +89,11 @@ func removeWorkspaceDir(c *gin.Context) { path := arg["path"].(string) + if util.IsWorkspaceLocked(path) { + logging.LogWarnf("skip remove workspace [%s] because it is locked", path) + return + } + workspacePaths, err := util.ReadWorkspacePaths() if nil != err { ret.Code = -1 @@ -127,14 +132,7 @@ func getWorkspaces(c *gin.Context) { var workspaces []*Workspace for _, p := range workspacePaths { - closed := false - f := flock.New(filepath.Join(p, ".lock")) - ok, _ := f.TryLock() - if ok { - closed = true - } - f.Unlock() - + closed := !util.IsWorkspaceLocked(p) workspaces = append(workspaces, &Workspace{Path: p, Closed: closed}) } ret.Data = workspaces diff --git a/kernel/util/working.go b/kernel/util/working.go index 2b8270bf2..d0136e279 100644 --- a/kernel/util/working.go +++ b/kernel/util/working.go @@ -495,6 +495,25 @@ func tryLockWorkspace() { os.Exit(ExitCodeWorkspaceLocked) } +func IsWorkspaceLocked(workspacePath string) bool { + if !gulu.File.IsDir(workspacePath) { + return false + } + + lockFilePath := filepath.Join(workspacePath, ".lock") + if !gulu.File.IsExist(lockFilePath) { + return false + } + + f := flock.New(lockFilePath) + defer f.Unlock() + ok, _ := f.TryLock() + if ok { + return false + } + return true +} + func UnlockWorkspace() { if nil == WorkspaceLock { return