@@ -536,7 +536,7 @@ func WithUser(userstr string) SpecOpts {
536536 }
537537 f := func (root string ) error {
538538 if username != "" {
539- user , err := getUserFromPath (root , func (u user.User ) bool {
539+ user , err := UserFromPath (root , func (u user.User ) bool {
540540 return u .Name == username
541541 })
542542 if err != nil {
@@ -545,7 +545,7 @@ func WithUser(userstr string) SpecOpts {
545545 uid = uint32 (user .Uid )
546546 }
547547 if groupname != "" {
548- gid , err = getGIDFromPath (root , func (g user.Group ) bool {
548+ gid , err = GIDFromPath (root , func (g user.Group ) bool {
549549 return g .Name == groupname
550550 })
551551 if err != nil {
@@ -600,11 +600,11 @@ func WithUserID(uid uint32) SpecOpts {
600600 if ! isRootfsAbs (s .Root .Path ) {
601601 return errors .Errorf ("rootfs absolute path is required" )
602602 }
603- user , err := getUserFromPath (s .Root .Path , func (u user.User ) bool {
603+ user , err := UserFromPath (s .Root .Path , func (u user.User ) bool {
604604 return u .Uid == int (uid )
605605 })
606606 if err != nil {
607- if os .IsNotExist (err ) || err == errNoUsersFound {
607+ if os .IsNotExist (err ) || err == ErrNoUsersFound {
608608 s .Process .User .UID , s .Process .User .GID = uid , 0
609609 return nil
610610 }
@@ -626,11 +626,11 @@ func WithUserID(uid uint32) SpecOpts {
626626 return err
627627 }
628628 return mount .WithTempMount (ctx , mounts , func (root string ) error {
629- user , err := getUserFromPath (root , func (u user.User ) bool {
629+ user , err := UserFromPath (root , func (u user.User ) bool {
630630 return u .Uid == int (uid )
631631 })
632632 if err != nil {
633- if os .IsNotExist (err ) || err == errNoUsersFound {
633+ if os .IsNotExist (err ) || err == ErrNoUsersFound {
634634 s .Process .User .UID , s .Process .User .GID = uid , 0
635635 return nil
636636 }
@@ -654,7 +654,7 @@ func WithUsername(username string) SpecOpts {
654654 if ! isRootfsAbs (s .Root .Path ) {
655655 return errors .Errorf ("rootfs absolute path is required" )
656656 }
657- user , err := getUserFromPath (s .Root .Path , func (u user.User ) bool {
657+ user , err := UserFromPath (s .Root .Path , func (u user.User ) bool {
658658 return u .Name == username
659659 })
660660 if err != nil {
@@ -675,7 +675,7 @@ func WithUsername(username string) SpecOpts {
675675 return err
676676 }
677677 return mount .WithTempMount (ctx , mounts , func (root string ) error {
678- user , err := getUserFromPath (root , func (u user.User ) bool {
678+ user , err := UserFromPath (root , func (u user.User ) bool {
679679 return u .Name == username
680680 })
681681 if err != nil {
@@ -707,11 +707,11 @@ func WithAdditionalGIDs(userstr string) SpecOpts {
707707 var username string
708708 uid , err := strconv .Atoi (userstr )
709709 if err == nil {
710- user , err := getUserFromPath (root , func (u user.User ) bool {
710+ user , err := UserFromPath (root , func (u user.User ) bool {
711711 return u .Uid == uid
712712 })
713713 if err != nil {
714- if os .IsNotExist (err ) || err == errNoUsersFound {
714+ if os .IsNotExist (err ) || err == ErrNoUsersFound {
715715 return nil
716716 }
717717 return err
@@ -869,9 +869,12 @@ func WithAmbientCapabilities(caps []string) SpecOpts {
869869 }
870870}
871871
872- var errNoUsersFound = errors .New ("no users found" )
872+ // ErrNoUsersFound can be returned from UserFromPath
873+ var ErrNoUsersFound = errors .New ("no users found" )
873874
874- func getUserFromPath (root string , filter func (user.User ) bool ) (user.User , error ) {
875+ // UserFromPath inspects the user object using /etc/passwd in the specified rootfs.
876+ // filter can be nil.
877+ func UserFromPath (root string , filter func (user.User ) bool ) (user.User , error ) {
875878 ppath , err := fs .RootPath (root , "/etc/passwd" )
876879 if err != nil {
877880 return user.User {}, err
@@ -881,14 +884,17 @@ func getUserFromPath(root string, filter func(user.User) bool) (user.User, error
881884 return user.User {}, err
882885 }
883886 if len (users ) == 0 {
884- return user.User {}, errNoUsersFound
887+ return user.User {}, ErrNoUsersFound
885888 }
886889 return users [0 ], nil
887890}
888891
889- var errNoGroupsFound = errors .New ("no groups found" )
892+ // ErrNoGroupsFound can be returned from GIDFromPath
893+ var ErrNoGroupsFound = errors .New ("no groups found" )
890894
891- func getGIDFromPath (root string , filter func (user.Group ) bool ) (gid uint32 , err error ) {
895+ // GIDFromPath inspects the GID using /etc/passwd in the specified rootfs.
896+ // filter can be nil.
897+ func GIDFromPath (root string , filter func (user.Group ) bool ) (gid uint32 , err error ) {
892898 gpath , err := fs .RootPath (root , "/etc/group" )
893899 if err != nil {
894900 return 0 , err
@@ -898,7 +904,7 @@ func getGIDFromPath(root string, filter func(user.Group) bool) (gid uint32, err
898904 return 0 , err
899905 }
900906 if len (groups ) == 0 {
901- return 0 , errNoGroupsFound
907+ return 0 , ErrNoGroupsFound
902908 }
903909 g := groups [0 ]
904910 return uint32 (g .Gid ), nil
0 commit comments