Skip to content

Commit 723dd31

Browse files
committed
builtin: spring cleaning
1 parent 9d89a00 commit 723dd31

4 files changed

Lines changed: 44 additions & 113 deletions

File tree

builtin/builtin.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import (
1515
// - visitedItems := map[string]Void{}
1616
type Void struct{}
1717

18-
// ignores error in situations where context was canceled and thus errors are to be expected
19-
// and thus are non-interesting
18+
// ignores error in situations where context was canceled and thus errors are to be expected / normal.
2019
func IgnoreErrorIfCanceled(ctx context.Context, err error) error {
2120
select {
2221
case <-ctx.Done():
@@ -34,17 +33,6 @@ func Pointer[T any](val T) *T {
3433
return &val
3534
}
3635

37-
// helper for wrapping an error with error prefix.
38-
// if error is nil, nil is returned.
39-
// Deprecated: this doesn't carry its own weight - just use `fmt.Errorf()`
40-
func ErrorWrap(prefix string, err error) error {
41-
if err != nil {
42-
return fmt.Errorf("%s: %w", prefix, err)
43-
} else {
44-
return nil
45-
}
46-
}
47-
4836
// errors if a field is unset. it is up to the caller to evaluate if the field is unset
4937
func ErrorIfUnset(isUnset bool, fieldName string) error {
5038
if isUnset {
@@ -54,18 +42,6 @@ func ErrorIfUnset(isUnset bool, fieldName string) error {
5442
}
5543
}
5644

57-
// returns the first non-empty value.
58-
// Deprecated: use `cmp.Or()`
59-
func FirstNonEmpty[T comparable](values ...T) T {
60-
var empty T
61-
for _, value := range values {
62-
if value != empty {
63-
return value
64-
}
65-
}
66-
return empty
67-
}
68-
6945
// for a function that returns two values, you can get the value with `Must(fn())` to convert it to
7046
// the value, panicking if producing the value caused an error
7147
func Must[T any](value T, err error) T {
@@ -76,13 +52,6 @@ func Must[T any](value T, err error) T {
7652
return value
7753
}
7854

79-
// returns first error, or nil if no errors
80-
// Deprecated: use `FirstNonEmpty()`
81-
func FirstError(errs ...error) error {
82-
return FirstNonEmpty(errs...)
83-
}
84-
85-
8655
// append to a slice without needing the "assign to same variable" boilerplate
8756
func Append[T any](slice *[]T, item T) {
8857
*slice = append(*slice, item)

builtin/builtin_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -34,50 +34,6 @@ func TestPointerToInt(t *testing.T) {
3434
assert.Assert(t, *Pointer(num) == 314)
3535
}
3636

37-
func TestFirstError(t *testing.T) {
38-
var errA = errors.New("error A")
39-
var errB = errors.New("error B")
40-
41-
assert.Assert(t, FirstError() == nil)
42-
assert.Assert(t, FirstError(nil) == nil)
43-
assert.Assert(t, FirstError(errA, nil) == errA)
44-
assert.Assert(t, FirstError(nil, errA) == errA)
45-
assert.Assert(t, FirstError(errA, errB) == errA)
46-
assert.Assert(t, FirstError(errB, errA) == errB)
47-
}
48-
49-
func TestErrorWrap(t *testing.T) {
50-
assert.EqualString(t, ErrorWrap("myprefix", errors.New("unknown error occurred")).Error(), "myprefix: unknown error occurred")
51-
assert.Assert(t, ErrorWrap("myprefix", nil) == nil)
52-
}
53-
54-
func TestErrorIfUnset(t *testing.T) {
55-
assert.Assert(t, ErrorIfUnset(false, "foo") == nil)
56-
assert.EqualString(t, ErrorIfUnset(true, "foo").Error(), "'foo' is required")
57-
}
58-
59-
func TestFirstNonEmptyWithString(t *testing.T) {
60-
assert.EqualString(t, FirstNonEmpty("foo", "bar"), "foo")
61-
assert.EqualString(t, FirstNonEmpty("", "bar"), "bar")
62-
assert.EqualString(t, FirstNonEmpty(""), "")
63-
}
64-
65-
func TestFirstNonEmptyWithInt(t *testing.T) {
66-
var zero int
67-
pi := int(314)
68-
69-
assert.Assert(t, FirstNonEmpty(zero, pi) == 314)
70-
assert.Assert(t, FirstNonEmpty(pi, zero) == 314)
71-
}
72-
73-
func TestFirstNonEmptyWithError(t *testing.T) {
74-
var nilError error = nil
75-
actualError := errors.New("actual")
76-
77-
assert.EqualString(t, FirstNonEmpty(nilError, actualError).Error(), "actual")
78-
assert.Assert(t, FirstNonEmpty(nilError, nilError) == nil)
79-
}
80-
8137
func TestAppend(t *testing.T) {
8238
favoriteThings := []string{"hamburger"}
8339

os/osutil/createemptyfile.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ import (
88

99
// does the same as "$ touch"
1010
func CreateEmptyFile(path string) error {
11-
return ErrorWrap("CreateEmptyFile", func() error {
12-
file, err := os.Create(path)
13-
if err != nil {
14-
return err
15-
}
11+
withErr := FuncWrapErr
1612

17-
return file.Close()
18-
}())
13+
file, err := os.Create(path)
14+
if err != nil {
15+
return withErr(err)
16+
}
17+
18+
if err := file.Close(); err != nil {
19+
return withErr(err)
20+
}
21+
22+
return nil
1923
}

os/user/userutil/privileges_linux.go

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type UnprivilegedUser interface {
7474
// WARNING: this alters global process state, so you shouldn't be doing anything concurrent.
7575
// (at least where the different operations would be bothered by running in different security context)
7676
func DropToUnprivilegedUserIfPossible() (PrivilegedWork, error) {
77+
withErr := func(err error) (PrivilegedWork, error) { return nil, FuncWrapErr(err) }
78+
7779
if _, err := RequireRoot(); err != nil {
7880
return nil, err
7981
}
@@ -83,10 +85,10 @@ func DropToUnprivilegedUserIfPossible() (PrivilegedWork, error) {
8385
userDetails, err := getInvokingUserUidAndGidIfRunningInSudoWithSupplementary()
8486
switch {
8587
case err != nil: // should only happen if sudo ENV var numbers fail to parse
86-
return nil, ErrorWrap("PrivilegesDropTemporarily", err)
88+
return withErr(err)
8789
case userDetails != nil:
8890
if err := setEffectiveUIDAndGID(*userDetails); err != nil {
89-
return nil, ErrorWrap("PrivilegesDropTemporarily", err)
91+
return withErr(err)
9092
}
9193

9294
return &runningUnderSudo{*userDetails}, nil
@@ -138,12 +140,12 @@ func (r *runningUnderSudo) UnprivilegedUser() UserAndGroup {
138140

139141
func (r *runningUnderSudo) AsRoot(work func(ProofOfRunningAsRoot) error) error {
140142
if err := regainRoot(); err != nil {
141-
return ErrorWrap("AsRoot", err)
143+
return fmt.Errorf("AsRoot: %w", err)
142144
}
143145

144146
returnAfterDroppingPrivileges := func(errWork error) error {
145147
if errDrop := setEffectiveUIDAndGID(r.unprivileged); errDrop != nil {
146-
if errDrop != nil {
148+
if errWork != nil {
147149
return fmt.Errorf("%w; additionally privilege drop failed: %v", errWork, errDrop)
148150
} else {
149151
return fmt.Errorf("AsRoot: work succeeded but privilege drop failed: %w", errDrop)
@@ -188,61 +190,61 @@ func (r *runningUnderSudo) AsRoot(work func(ProofOfRunningAsRoot) error) error {
188190
//
189191
// After regaining root (seteuid(0) and setegid(0)) the effect is reversed, i.e. back to starting situation.
190192
func setEffectiveUIDAndGID(user UserAndGroup) error {
191-
return ErrorWrap("setEffectiveUIDAndGID", func() error {
192-
// gid has to be set first (otherwise we'd not be authorized to change it)
193-
if err := syscall.Setegid(user.gid); err != nil {
194-
return ErrorWrap("Setegid", err)
195-
}
193+
// gid has to be set first (otherwise we'd not be authorized to change it)
194+
if err := syscall.Setegid(user.gid); err != nil {
195+
return fmt.Errorf("Setegid: %w", err)
196+
}
196197

197-
if err := syscall.Setgroups(user.gidsSupplementary); err != nil {
198-
return ErrorWrap("Setgroups", err)
199-
}
198+
if err := syscall.Setgroups(user.gidsSupplementary); err != nil {
199+
return fmt.Errorf("Setgroups: %w", err)
200+
}
200201

201-
if err := syscall.Seteuid(user.uid); err != nil {
202-
return ErrorWrap("Seteuid", err)
203-
}
202+
if err := syscall.Seteuid(user.uid); err != nil {
203+
return fmt.Errorf("Seteuid: %w", err)
204+
}
204205

205-
return nil
206-
}())
206+
return nil
207207
}
208208

209209
func regainRoot() error {
210-
return ErrorWrap("regainRoot", func() error {
211-
// uid has to be set first (otherwise we'd not be authorized to change it)
212-
if err := syscall.Seteuid(0); err != nil {
213-
return fmt.Errorf("Seteuid root: %w", err)
214-
}
210+
withErr := FuncWrapErr
215211

216-
if err := syscall.Setegid(0); err != nil {
217-
return fmt.Errorf("Setegid root: %w", err)
218-
}
212+
// uid has to be set first (otherwise we'd not be authorized to change it)
213+
if err := syscall.Seteuid(0); err != nil {
214+
return withErr(fmt.Errorf("Seteuid root: %w", err))
215+
}
219216

220-
return nil
221-
}())
217+
if err := syscall.Setegid(0); err != nil {
218+
return withErr(fmt.Errorf("Setegid root: %w", err))
219+
}
220+
221+
return nil
222222
}
223223

224224
// if running under '$ sudo', return invoking user's uid:gid pair.
225225
// returns nil, nil if not running under sudo
226226
func getInvokingUserUidAndGidIfRunningInSudoWithSupplementary() (*UserAndGroup, error) {
227+
withErr := func(err error) (*UserAndGroup, error) { return nil, FuncWrapErr(err) }
228+
227229
// documented in https://www.sudo.ws/docs/man/sudo.man/#SUDO_UID
228230
if os.Getenv("SUDO_UID") == "" { // not running under sudo
229231
return nil, nil
230232
}
231233

232234
uidSudo, err := strconv.Atoi(os.Getenv("SUDO_UID"))
233235
if err != nil {
234-
return nil, ErrorWrap("getInvokingUserUidAndGidIfRunningInSudoWithoutSupplementary", err)
236+
return withErr(err)
235237
}
236238

237239
gidSudo, err := strconv.Atoi(os.Getenv("SUDO_GID"))
238240
if err != nil {
239-
return nil, ErrorWrap("getInvokingUserUidAndGidIfRunningInSudoWithoutSupplementary", err)
241+
return withErr(err)
240242
}
241243

242244
// resolve also supplementary gids
243245
gidsSupplementarySudo, err := resolveSupplementaryGids(os.Getenv("SUDO_USER"))
244246
if err != nil {
245-
return nil, ErrorWrap("getInvokingUserUidAndGidIfRunningInSudoWithSupplementary", err)
247+
return withErr(err)
246248
}
247249

248250
return &UserAndGroup{uidSudo, gidSudo, gidsSupplementarySudo}, nil

0 commit comments

Comments
 (0)