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
This commit is contained in:
J Guerreiro 2018-07-21 16:18:19 +01:00 committed by GitHub
parent 1d463d1e3f
commit f9972da763
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 6 deletions

20
exec.go
View File

@ -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:]...)
}

View File

@ -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")