diff --git a/exec.go b/exec.go index b7249ccf..2a97ae3c 100644 --- a/exec.go +++ b/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:]...) } diff --git a/parser.go b/parser.go index d4f444b0..3c7d51e5 100644 --- a/parser.go +++ b/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")