group_unix.go 916 B

12345678910111213141516171819202122232425262728293031323334
  1. //go:build !windows
  2. package localapi
  3. import (
  4. "fmt"
  5. "os/user"
  6. "strconv"
  7. )
  8. // lookupGroupID resolves a group's name, or a numeric gid, to the gid as a string. A
  9. // gid that the group database does not list is accepted as chown would accept it: the
  10. // operator may be naming a group the host will learn about later, or one that only a
  11. // directory service knows.
  12. func lookupGroupID(name string) (string, error) {
  13. if g, err := user.LookupGroup(name); err == nil {
  14. return g.Gid, nil
  15. }
  16. if _, err := strconv.ParseUint(name, 10, 32); err == nil {
  17. return name, nil
  18. }
  19. return "", fmt.Errorf("group %q does not exist on this host", name)
  20. }
  21. // gidOf returns the group's gid as the number chown takes.
  22. func gidOf(g *Group) (int, error) {
  23. gid, err := strconv.ParseUint(g.ID, 10, 32)
  24. if err != nil {
  25. return 0, fmt.Errorf("group %q resolved to %q, which is not a gid", g.Name, g.ID)
  26. }
  27. return int(gid), nil
  28. }