Skip to content

Commit

Permalink
Use fs pkg in favor of os
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Jan 18, 2024
1 parent 187900c commit 9f99086
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 54 deletions.
29 changes: 15 additions & 14 deletions internal/unpackinfo/filemode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package unpackinfo

import (
"fmt"
"io/fs"
"os"
)

Expand All @@ -20,7 +21,7 @@ const (
Symlink FileMode = 0120000
)

func NewFileMode(mode os.FileMode) (FileMode, error) {
func NewFileMode(mode fs.FileMode) (FileMode, error) {
if mode.IsRegular() {
// disallow pipes, I/O, temporary files etc
if isCharDevice(mode) || isTemporary(mode) {
Expand All @@ -45,21 +46,21 @@ func NewFileMode(mode os.FileMode) (FileMode, error) {
return Empty, fmt.Errorf("invalid file mode: %s", mode)
}

// Maps a FileMode integer to an os.FileMode type, normalizing
// Maps a FileMode integer to an fs.FileMode type, normalizing
// permissions for regular and executable files.
func (m FileMode) ToOSFileMode() (os.FileMode, error) {
func (m FileMode) ToFsFileMode() (fs.FileMode, error) {
switch m {
case Regular:
return os.FileMode(0644), nil
return fs.FileMode(0644), nil
case Dir:
return os.ModePerm | os.ModeDir, nil
return fs.ModePerm | fs.ModeDir, nil
case Executable:
return os.FileMode(0755), nil
return fs.FileMode(0755), nil
case Symlink:
return os.ModePerm | os.ModeSymlink, nil
return fs.ModePerm | fs.ModeSymlink, nil
}

return os.FileMode(0), fmt.Errorf("malformed file mode: %s", m)
return fs.FileMode(0), fmt.Errorf("malformed file mode: %s", m)
}

func (m FileMode) IsRegular() bool {
Expand All @@ -76,18 +77,18 @@ func (m FileMode) String() string {
return fmt.Sprintf("%07o", uint32(m))
}

func isCharDevice(m os.FileMode) bool {
func isCharDevice(m fs.FileMode) bool {
return m&os.ModeCharDevice != 0
}

func isExecutable(m os.FileMode) bool {
func isExecutable(m fs.FileMode) bool {
return m&0100 != 0
}

func isSymlink(m os.FileMode) bool {
return m&os.ModeSymlink != 0
func isSymlink(m fs.FileMode) bool {
return m&fs.ModeSymlink != 0
}

func isTemporary(m os.FileMode) bool {
return m&os.ModeTemporary != 0
func isTemporary(m fs.FileMode) bool {
return m&fs.ModeTemporary != 0
}
78 changes: 39 additions & 39 deletions internal/unpackinfo/filemode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package unpackinfo

import (
"fmt"
"os"
"io/fs"
"strings"
"testing"
)
Expand All @@ -13,44 +13,44 @@ func assertFileMode(t *testing.T, expected FileMode, got FileMode) {
}
}

func assertOSFileMode(t *testing.T, expected os.FileMode, got os.FileMode) {
func assertFsFileMode(t *testing.T, expected fs.FileMode, got fs.FileMode) {
if expected != got {
t.Fatalf("expected OS filemode: %s, got: %s", expected, got)
}
}

func TestFileMode_New(t *testing.T) {
for _, c := range []struct {
mode os.FileMode
mode fs.FileMode
expected FileMode
}{
{os.FileMode(0755) | os.ModeDir, Dir},
{os.FileMode(0700) | os.ModeDir, Dir},
{os.FileMode(0500) | os.ModeDir, Dir},
{fs.FileMode(0755) | fs.ModeDir, Dir},
{fs.FileMode(0700) | fs.ModeDir, Dir},
{fs.FileMode(0500) | fs.ModeDir, Dir},
// dirs with a sticky bit are just dirs
{os.FileMode(0755) | os.ModeDir | os.ModeSticky, Dir},
{os.FileMode(0644), Regular},
{fs.FileMode(0755) | fs.ModeDir | fs.ModeSticky, Dir},
{fs.FileMode(0644), Regular},
// append only files are regular
{os.FileMode(0644) | os.ModeAppend, Regular},
{fs.FileMode(0644) | fs.ModeAppend, Regular},
// exclusive only files are regular
{os.FileMode(0644) | os.ModeExclusive, Regular},
{fs.FileMode(0644) | fs.ModeExclusive, Regular},
// depending on owner perms, setguid can be regular
{os.FileMode(0644) | os.ModeSetgid, Regular},
{os.FileMode(0660), Regular},
{os.FileMode(0640), Regular},
{os.FileMode(0600), Regular},
{os.FileMode(0400), Regular},
{os.FileMode(0000), Regular},
{os.FileMode(0755), Executable},
{fs.FileMode(0644) | fs.ModeSetgid, Regular},
{fs.FileMode(0660), Regular},
{fs.FileMode(0640), Regular},
{fs.FileMode(0600), Regular},
{fs.FileMode(0400), Regular},
{fs.FileMode(0000), Regular},
{fs.FileMode(0755), Executable},
// setuid and setguid are executables
{os.FileMode(0755) | os.ModeSetuid, Executable},
{os.FileMode(0755) | os.ModeSetgid, Executable},
{os.FileMode(0700), Executable},
{os.FileMode(0500), Executable},
{os.FileMode(0744), Executable},
{os.FileMode(0540), Executable},
{os.FileMode(0550), Executable},
{os.FileMode(0777) | os.ModeSymlink, Symlink},
{fs.FileMode(0755) | fs.ModeSetuid, Executable},
{fs.FileMode(0755) | fs.ModeSetgid, Executable},
{fs.FileMode(0700), Executable},
{fs.FileMode(0500), Executable},
{fs.FileMode(0744), Executable},
{fs.FileMode(0540), Executable},
{fs.FileMode(0550), Executable},
{fs.FileMode(0777) | fs.ModeSymlink, Symlink},
} {
m, err := NewFileMode(c.mode)
if err != nil {
Expand All @@ -62,18 +62,18 @@ func TestFileMode_New(t *testing.T) {

func TestFileMode_NewWithErrors(t *testing.T) {
for _, c := range []struct {
mode os.FileMode
mode fs.FileMode
expected FileMode
expectedErr string
}{
// temporary files are ignored
{os.FileMode(0644) | os.ModeTemporary, Empty, "invalid file mode"},
{fs.FileMode(0644) | fs.ModeTemporary, Empty, "invalid file mode"},
// device files are ignored
{os.FileMode(0644) | os.ModeCharDevice, Empty, "invalid file mode"},
{fs.FileMode(0644) | fs.ModeCharDevice, Empty, "invalid file mode"},
// named pipes are ignored
{os.FileMode(0644) | os.ModeNamedPipe, Empty, "invalid file mode"},
{fs.FileMode(0644) | fs.ModeNamedPipe, Empty, "invalid file mode"},
// sockets are ignored
{os.FileMode(0644) | os.ModeSocket, Empty, "invalid file mode"},
{fs.FileMode(0644) | fs.ModeSocket, Empty, "invalid file mode"},
} {
m, err := NewFileMode(c.mode)
if err == nil {
Expand All @@ -92,18 +92,18 @@ func TestFileMode_NewWithErrors(t *testing.T) {
func TestFileMode_ToOSFileMode(t *testing.T) {
for _, c := range []struct {
mode FileMode
expected os.FileMode
expected fs.FileMode
}{
{Regular, os.FileMode(0644)},
{Dir, os.ModePerm | os.ModeDir},
{Symlink, os.ModePerm | os.ModeSymlink},
{Executable, os.FileMode(0755)},
{Regular, fs.FileMode(0644)},
{Dir, fs.ModePerm | fs.ModeDir},
{Symlink, fs.ModePerm | fs.ModeSymlink},
{Executable, fs.FileMode(0755)},
} {
m, err := c.mode.ToOSFileMode()
m, err := c.mode.ToFsFileMode()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
assertOSFileMode(t, c.expected, m)
assertFsFileMode(t, c.expected, m)
}
}

Expand All @@ -117,7 +117,7 @@ func TestFileMode_ToOSFileModeWithErrors(t *testing.T) {
FileMode(010000),
FileMode(0100000),
} {
m, err := mode.ToOSFileMode()
m, err := mode.ToFsFileMode()
if err == nil {
t.Fatalf("expected an error, got nil")
}
Expand All @@ -128,7 +128,7 @@ func TestFileMode_ToOSFileModeWithErrors(t *testing.T) {
t.Fatalf("expected error: %s, got: %s", expectedErr, gotErr)
}

if m != os.FileMode(0) {
if m != fs.FileMode(0) {
t.Fatalf("expected file mode 0, got: %s", m)
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/unpackinfo/unpackinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (i UnpackInfo) FileMode() (fs.FileMode, error) {
mode := i.OriginalMode

if i.NormalizedMode != Empty {
mode, err = i.NormalizedMode.ToOSFileMode()
mode, err = i.NormalizedMode.ToFsFileMode()
if err != nil {
return fs.FileMode(0), err
}
Expand Down

0 comments on commit 9f99086

Please sign in to comment.