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")
}
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 {
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" {
n, err := strconv.Atoi(*installUser)
if err != nil {
@ -58,10 +66,10 @@ func runInstall(args []string) error {
if n < 0 {
return fmt.Errorf("-user cannot have a negative number as USER_ID")
}
}
device, err := oneDevice()
if err != nil {
return err
allUids := adb.AllUserIds(inst)
if _, exists := allUids[n]; !exists {
return fmt.Errorf("user %d does not exist", n)
}
}
if *installUser == "current" || (*installUser == "" && !*installUpdates) {
uid, err := device.CurrentUserId()
@ -70,10 +78,6 @@ func runInstall(args []string) error {
}
*installUser = strconv.Itoa(uid)
}
inst, err := device.Installed()
if err != nil {
return err
}
if *installUpdates {
apps, err := loadIndexes()

4
search.go

@ -75,6 +75,10 @@ func runSearch(args []string) error {
if n < 0 {
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
} else {
if *installUser == "current" {

22
uninstall.go

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

Loading…
Cancel
Save