|
| 1 | +//go:build (aix || dragonfly || freebsd || (!android && linux) || netbsd || openbsd || solaris || darwin) && cgo && !osusergo |
| 2 | +// +build aix dragonfly freebsd !android,linux netbsd openbsd solaris darwin |
| 3 | +// +build cgo |
| 4 | +// +build !osusergo |
| 5 | + |
| 6 | +package user |
| 7 | + |
| 8 | +/* |
| 9 | +#include <unistd.h> |
| 10 | +#include <sys/types.h> |
| 11 | +#include <pwd.h> |
| 12 | +#include <grp.h> |
| 13 | +#include <stdlib.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <errno.h> |
| 16 | +
|
| 17 | +static void resetErrno(){ |
| 18 | + errno = 0; |
| 19 | +} |
| 20 | +*/ |
| 21 | +import "C" |
| 22 | + |
| 23 | +// usersHelper defines the methods used in users iteration process within |
| 24 | +// iterateUsers. This interface allows testing iterateUsers functionality. |
| 25 | +// iterate_test_fgetent.go file defines test related struct that implements |
| 26 | +// usersHelper. |
| 27 | +type usersHelper interface { |
| 28 | + // set sets up internal state before iteration |
| 29 | + set() |
| 30 | + |
| 31 | + // get sequentially returns a passwd structure which is later processed into *User entry |
| 32 | + get() (*C.struct_passwd, error) |
| 33 | + |
| 34 | + // end cleans up internal state after iteration is done |
| 35 | + end() |
| 36 | +} |
| 37 | + |
| 38 | +type iterateUsersHelper struct{} |
| 39 | + |
| 40 | +func (i iterateUsersHelper) set() { |
| 41 | + C.setpwent() |
| 42 | +} |
| 43 | + |
| 44 | +func (i iterateUsersHelper) get() (*C.struct_passwd, error) { |
| 45 | + var result *C.struct_passwd |
| 46 | + result, err := C.getpwent() |
| 47 | + return result, err |
| 48 | +} |
| 49 | + |
| 50 | +func (i iterateUsersHelper) end() { |
| 51 | + C.endpwent() |
| 52 | +} |
| 53 | + |
| 54 | +// This helper is used to retrieve users via c library call. A global |
| 55 | +// variable which implements usersHelper interface is needed in order to |
| 56 | +// separate testing logic from production. Since cgo can not be used directly |
| 57 | +// in tests, iterate_test_fgetent.go file provides iterateUsersHelperTest |
| 58 | +// structure which implements usersHelper interface and can substitute |
| 59 | +// default userIterator value. |
| 60 | +var userIterator usersHelper = iterateUsersHelper{} |
| 61 | + |
| 62 | +// iterateUsers iterates over users database via getpwent(3). If fn returns non |
| 63 | +// nil error, then iteration is terminated. A nil result from getpwent means |
| 64 | +// there were no more entries, or an error occurred, as such, iteration is |
| 65 | +// terminated, and if error was encountered it is returned. |
| 66 | +// |
| 67 | +// Since iterateUsers uses getpwent(3), which is not thread safe, iterateUsers |
| 68 | +// can not bet used concurrently. If concurrent usage is required, it is |
| 69 | +// recommended to use locking mechanism such as sync.Mutex when calling |
| 70 | +// iterateUsers from multiple goroutines. |
| 71 | +func iterateUsers(fn NextUserFunc) error { |
| 72 | + userIterator.set() |
| 73 | + defer userIterator.end() |
| 74 | + for { |
| 75 | + var result *C.struct_passwd |
| 76 | + C.resetErrno() |
| 77 | + result, err := userIterator.get() |
| 78 | + |
| 79 | + // If result is nil - getpwent iterated through entire users database or there was an error |
| 80 | + if result == nil { |
| 81 | + return err |
| 82 | + } |
| 83 | + |
| 84 | + if err = fn(buildUser(result)); err != nil { |
| 85 | + // User provided non-nil error means that iteration should be terminated |
| 86 | + return err |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// groupsHelper defines the methods used in groups iteration process within iterateGroups. This interface allows testing |
| 92 | +// iterateGroups functionality. iterate_test_fgetent.go file defines test related struct that implements groupsHelper. |
| 93 | +type groupsHelper interface { |
| 94 | + // set sets up internal state before iteration |
| 95 | + set() |
| 96 | + |
| 97 | + // get sequentially returns a group structure which is later processed into *Group entry |
| 98 | + get() (*C.struct_group, error) |
| 99 | + |
| 100 | + // end cleans up internal state after iteration is done |
| 101 | + end() |
| 102 | +} |
| 103 | + |
| 104 | +type iterateGroupsHelper struct{} |
| 105 | + |
| 106 | +func (i iterateGroupsHelper) set() { |
| 107 | + C.setgrent() |
| 108 | +} |
| 109 | + |
| 110 | +func (i iterateGroupsHelper) get() (*C.struct_group, error) { |
| 111 | + var result *C.struct_group |
| 112 | + result, err := C.getgrent() |
| 113 | + return result, err |
| 114 | +} |
| 115 | + |
| 116 | +func (i iterateGroupsHelper) end() { |
| 117 | + C.endgrent() |
| 118 | +} |
| 119 | + |
| 120 | +// This helper is used to retrieve groups via c library call. A global |
| 121 | +// variable which implements groupsHelper interface is needed in order to |
| 122 | +// separate testing logic from production. Since cgo can not be used directly |
| 123 | +// in tests, iterate_test_fgetent.go file provides iterateGroupsHelperTest |
| 124 | +// structure which implements groupsHelper interface and can substitute |
| 125 | +// default groupIterator value. |
| 126 | +var groupIterator groupsHelper = iterateGroupsHelper{} |
| 127 | + |
| 128 | +// iterateGroups iterates over groups database via getgrent(3). If fn returns |
| 129 | +// non nil error, then iteration is terminated. A nil result from getgrent means |
| 130 | +// there were no more entries, or an error occurred, as such, iteration is |
| 131 | +// terminated, and if error was encountered it is returned. |
| 132 | +// |
| 133 | +// Since iterateGroups uses getgrent(3), which is not thread safe, iterateGroups |
| 134 | +// can not bet used concurrently. If concurrent usage is required, it is |
| 135 | +// recommended to use locking mechanism such as sync.Mutex when calling |
| 136 | +// iterateGroups from multiple goroutines. |
| 137 | +func iterateGroups(fn NextGroupFunc) error { |
| 138 | + groupIterator.set() |
| 139 | + defer groupIterator.end() |
| 140 | + for { |
| 141 | + var result *C.struct_group |
| 142 | + C.resetErrno() |
| 143 | + result, err := groupIterator.get() |
| 144 | + |
| 145 | + // If result is nil - getgrent iterated through entire groups database or there was an error |
| 146 | + if result == nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + if err = fn(buildGroup(result)); err != nil { |
| 151 | + // User provided non-nil error means that iteration should be terminated |
| 152 | + return err |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments