Browse Source

-user USER_ID: make sure that the user actually exists

pull/70/head
Linus789 3 years ago
parent
commit
4bb6774156
  1. 10
      adb/device.go
  2. 20
      install.go
  3. 4
      search.go
  4. 22
      uninstall.go

10
adb/device.go

@ -336,3 +336,13 @@ func (d *Device) CurrentUserId() (int, error) {
} }
return -1, fmt.Errorf("could not get current user id") return -1, fmt.Errorf("could not get current user id")
} }
func AllUserIds(installed map[string]Package) map[int]struct{} {
uidMap := make(map[int]struct{})
for _, pkg := range installed {
for _, uid := range pkg.InstalledForUsers {
uidMap[uid] = struct{}{}
}
}
return uidMap
}

20
install.go

@ -50,6 +50,14 @@ func runInstall(args []string) error {
if *installUpdatesExclude != "" && !*installUpdates { if *installUpdatesExclude != "" && !*installUpdates {
return fmt.Errorf("-e can only be used for upgrading (i.e. -u)") return fmt.Errorf("-e can only be used for upgrading (i.e. -u)")
} }
device, err := oneDevice()
if err != nil {
return err
}
inst, err := device.Installed()
if err != nil {
return err
}
if *installUser != "" && *installUser != "all" && *installUser != "current" { if *installUser != "" && *installUser != "all" && *installUser != "current" {
n, err := strconv.Atoi(*installUser) n, err := strconv.Atoi(*installUser)
if err != nil { if err != nil {
@ -58,10 +66,10 @@ func runInstall(args []string) error {
if n < 0 { if n < 0 {
return fmt.Errorf("-user cannot have a negative number as USER_ID") return fmt.Errorf("-user cannot have a negative number as USER_ID")
} }
} allUids := adb.AllUserIds(inst)
device, err := oneDevice() if _, exists := allUids[n]; !exists {
if err != nil { return fmt.Errorf("user %d does not exist", n)
return err }
} }
if *installUser == "current" || (*installUser == "" && !*installUpdates) { if *installUser == "current" || (*installUser == "" && !*installUpdates) {
uid, err := device.CurrentUserId() uid, err := device.CurrentUserId()
@ -70,10 +78,6 @@ func runInstall(args []string) error {
} }
*installUser = strconv.Itoa(uid) *installUser = strconv.Itoa(uid)
} }
inst, err := device.Installed()
if err != nil {
return err
}
if *installUpdates { if *installUpdates {
apps, err := loadIndexes() apps, err := loadIndexes()

4
search.go

@ -75,6 +75,10 @@ func runSearch(args []string) error {
if n < 0 { if n < 0 {
return fmt.Errorf("-user cannot have a negative number as USER_ID") return fmt.Errorf("-user cannot have a negative number as USER_ID")
} }
allUids := adb.AllUserIds(inst)
if _, exists := allUids[n]; !exists {
return fmt.Errorf("user %d does not exist", n)
}
filterUser = &n filterUser = &n
} else { } else {
if *installUser == "current" { if *installUser == "current" {

22
uninstall.go

@ -7,6 +7,8 @@ import (
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"mvdan.cc/fdroidcl/adb"
) )
var cmdUninstall = &Command{ var cmdUninstall = &Command{
@ -26,6 +28,14 @@ func runUninstall(args []string) error {
if len(args) < 1 { if len(args) < 1 {
return fmt.Errorf("no package names given") return fmt.Errorf("no package names given")
} }
device, err := oneDevice()
if err != nil {
return err
}
inst, err := device.Installed()
if err != nil {
return err
}
if *uninstallUser != "all" && *uninstallUser != "current" { if *uninstallUser != "all" && *uninstallUser != "current" {
n, err := strconv.Atoi(*uninstallUser) n, err := strconv.Atoi(*uninstallUser)
if err != nil { if err != nil {
@ -34,10 +44,10 @@ func runUninstall(args []string) error {
if n < 0 { if n < 0 {
return fmt.Errorf("-user cannot have a negative number as USER_ID") return fmt.Errorf("-user cannot have a negative number as USER_ID")
} }
} allUids := adb.AllUserIds(inst)
device, err := oneDevice() if _, exists := allUids[n]; !exists {
if err != nil { return fmt.Errorf("user %d does not exist", n)
return err }
} }
if *uninstallUser == "current" { if *uninstallUser == "current" {
uid, err := device.CurrentUserId() uid, err := device.CurrentUserId()
@ -46,10 +56,6 @@ func runUninstall(args []string) error {
} }
*uninstallUser = strconv.Itoa(uid) *uninstallUser = strconv.Itoa(uid)
} }
inst, err := device.Installed()
if err != nil {
return err
}
for _, id := range args { for _, id := range args {
var err error var err error
fmt.Printf("Uninstalling %s\n", id) fmt.Printf("Uninstalling %s\n", id)

Loading…
Cancel
Save