浏览代码

Wait for db.lck to become available before starting a db operation (#573)

* Wait for db.lck to become available before starting a db operation

* Fix err!=nil issues and avoid spamming users

* Remove redundant cases

* Remove return
J Guerreiro 6 年之前
父节点
当前提交
f9972da763
共有 2 个文件被更改,包括 64 次插入6 次删除
  1. 20 0
      exec.go
  2. 44 6
      parser.go

+ 20 - 0
exec.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"os"
 	"os/exec"
+	"path/filepath"
 	"strings"
 	"time"
 )
@@ -53,6 +54,22 @@ func updateSudo() {
 	}
 }
 
+// waitLock will lock yay checking the status of db.lck until it does not exist
+func waitLock() {
+	if _, err := os.Stat(filepath.Join(alpmConf.DBPath, "db.lck")); err != nil {
+		return
+	}
+
+	fmt.Println(bold(yellow(smallArrow)), "db.lck is present. Waiting... ")
+
+	for {
+		time.Sleep(3 * time.Second)
+		if _, err := os.Stat(filepath.Join(alpmConf.DBPath, "db.lck")); err != nil {
+			return
+		}
+	}
+}
+
 func passToPacman(args *arguments) *exec.Cmd {
 	argArr := make([]string, 0)
 
@@ -71,6 +88,9 @@ func passToPacman(args *arguments) *exec.Cmd {
 
 	argArr = append(argArr, args.targets...)
 
+	if args.needWait() {
+		waitLock()
+	}
 	return exec.Command(argArr[0], argArr[1:]...)
 }
 

+ 44 - 6
parser.go

@@ -144,12 +144,6 @@ func (parser *arguments) needRoot() bool {
 	case "R", "remove":
 		return true
 	case "S", "sync":
-		if parser.existsArg("y", "refresh") {
-			return true
-		}
-		if parser.existsArg("u", "sysupgrade") {
-			return true
-		}
 		if parser.existsArg("s", "search") {
 			return false
 		}
@@ -177,6 +171,50 @@ func (parser *arguments) needRoot() bool {
 	}
 }
 
+//needWait checks if waitLock() should be called before calling pacman
+func (parser *arguments) needWait() bool {
+	if parser.existsArg("h", "help") {
+		return false
+	}
+
+	if parser.existsArg("p", "print") {
+		return false
+	}
+
+	switch parser.op {
+	case "D", "database":
+		return true
+	case "F", "files":
+		if parser.existsArg("y", "refresh") {
+			return true
+		}
+		return false
+	case "R", "remove":
+		return true
+	case "S", "sync":
+		if parser.existsArg("y", "refresh") {
+			return true
+		}
+		if parser.existsArg("u", "sysupgrade") {
+			return true
+		}
+		if parser.existsArg("s", "search") {
+			return false
+		}
+		if parser.existsArg("l", "list") {
+			return false
+		}
+		if parser.existsArg("i", "info") {
+			return false
+		}
+		return true
+	case "U", "upgrade":
+		return true
+	default:
+		return false
+	}
+}
+
 func (parser *arguments) addOP(op string) (err error) {
 	if parser.op != "" {
 		err = fmt.Errorf("only one operation may be used at a time")