Skip to content

Commit c041057

Browse files
committed
💚 fix portability failures
1 parent 22780f9 commit c041057

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

utils/filesystem/files_posix.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ import (
1010
"github.com/ARM-software/golang-utils/utils/commonerrors"
1111
)
1212

13+
func isPrivilegeError(err error) bool {
14+
return commonerrors.Any(err, syscall.EPERM)
15+
}
16+
17+
func isNotSupportedError(err error) bool {
18+
return commonerrors.Any(err, syscall.ENOTSUP, syscall.EOPNOTSUPP)
19+
}
20+
1321
func determineFileOwners(info os.FileInfo) (uid, gid int, err error) {
1422
if raw, ok := info.Sys().(*syscall.Stat_t); ok {
1523
gid = int(raw.Gid)

utils/filesystem/files_windows.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@ package filesystem
77
import (
88
"os"
99
"syscall"
10+
11+
"github.com/ARM-software/golang-utils/utils/commonerrors"
1012
)
1113

14+
func isPrivilegeError(err error) bool {
15+
return commonerrors.Any(err, syscall.EPERM, syscall.ERROR_PRIVILEGE_NOT_HELD)
16+
}
17+
18+
func isNotSupportedError(err error) bool {
19+
return commonerrors.Any(err, syscall.ENOTSUP, syscall.EOPNOTSUPP, syscall.EWINDOWS)
20+
}
21+
1222
func determineFileOwners(_ os.FileInfo) (uid, gid int, err error) {
1323
uid = syscall.Getuid()
1424
gid = syscall.Getgid()

utils/filesystem/filesystem.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"fmt"
1111
"io"
1212
"os"
13-
"syscall"
1413

1514
"github.com/spf13/afero"
1615

@@ -114,7 +113,7 @@ func ConvertFileSystemError(err error) error {
114113
return commonerrors.WrapError(commonerrors.ErrExists, err, "")
115114
case commonerrors.CorrespondTo(err, "bad file descriptor") || os.IsPermission(err) || commonerrors.Any(err, os.ErrPermission, os.ErrClosed, afero.ErrFileClosed, ErrPathNotExist, io.ErrClosedPipe):
116115
return commonerrors.WrapError(commonerrors.ErrConflict, err, "")
117-
case commonerrors.Any(err, syscall.EPERM, syscall.ERROR_PRIVILEGE_NOT_HELD) || commonerrors.CorrespondTo(err, "required privilege is not held", "operation not permitted"):
116+
case isPrivilegeError(err) || commonerrors.CorrespondTo(err, "required privilege is not held", "operation not permitted"):
118117
return commonerrors.WrapError(commonerrors.ErrForbidden, err, "")
119118
case os.IsNotExist(err) || commonerrors.Any(err, os.ErrNotExist, afero.ErrFileNotFound) || IsPathNotExist(err) || commonerrors.CorrespondTo(err, "No such file or directory"):
120119
return commonerrors.WrapError(commonerrors.ErrNotFound, err, "")
@@ -131,7 +130,7 @@ func ConvertFileSystemError(err error) error {
131130
case commonerrors.Any(err, io.ErrUnexpectedEOF):
132131
// Do not add io.EOF as it is used to read files
133132
return commonerrors.WrapError(commonerrors.ErrEOF, err, "")
134-
case commonerrors.Any(err, syscall.ENOTSUP, syscall.EOPNOTSUPP, syscall.EWINDOWS, afero.ErrNoSymlink, afero.ErrNoReadlink) || commonerrors.CorrespondTo(err, "not supported"):
133+
case isNotSupportedError(err) || commonerrors.Any(err, afero.ErrNoSymlink, afero.ErrNoReadlink) || commonerrors.CorrespondTo(err, "not supported"):
135134
return commonerrors.WrapError(commonerrors.ErrUnsupported, err, "")
136135
}
137136

utils/filesystem/links_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"strings"
66
"testing"
77

8-
"github.com/ARM-software/golang-utils/utils/commonerrors"
9-
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
10-
"github.com/ARM-software/golang-utils/utils/platform"
118
"github.com/go-faker/faker/v4"
129
"github.com/stretchr/testify/assert"
1310
"github.com/stretchr/testify/require"
11+
12+
"github.com/ARM-software/golang-utils/utils/commonerrors"
13+
"github.com/ARM-software/golang-utils/utils/commonerrors/errortest"
14+
"github.com/ARM-software/golang-utils/utils/platform"
1415
)
1516

1617
func printWarningOnWindows(t *testing.T) {

0 commit comments

Comments
 (0)