Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Jul 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
Expand Down Expand Up @@ -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)

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

Expand Down
50 changes: 44 additions & 6 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are redundant, they'll be caught by the default return true at the end.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is return false though, I did notice needRoot does have a lot of redundancy

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default inside of sync is true though.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. 😇

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe needRoot needs a touch up as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, been a long time since I've even looked at it.

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")
Expand Down