Skip to content

Commit

Permalink
memfs: Handle error checks with error.Is
Browse files Browse the repository at this point in the history
Signed-off-by: Paulo Gomes <[email protected]>
  • Loading branch information
pjbgf committed Apr 19, 2024
1 parent cd9eb92 commit adc7a06
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
14 changes: 8 additions & 6 deletions memfs/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ import (

const separator = filepath.Separator

// Memory a very convenient filesystem based on memory files
var errNotLink = errors.New("not a link")

// Memory a very convenient filesystem based on memory files.
type Memory struct {
s *storage

Expand Down Expand Up @@ -72,8 +74,6 @@ func (fs *Memory) OpenFile(filename string, flag int, perm os.FileMode) (billy.F
return f.Duplicate(filename, perm, flag), nil
}

var errNotLink = errors.New("not a link")

func (fs *Memory) resolveLink(fullpath string, f *file) (target string, isLink bool) {
if !isSymlink(f.mode) {
return fullpath, false
Expand All @@ -91,7 +91,7 @@ func (fs *Memory) resolveLink(fullpath string, f *file) (target string, isLink b
// unit (eg.: `C:\`) to assert that is absolute, but in this mem implementation
// any path starting by `separator` is also considered absolute.
func isAbs(path string) bool {
return filepath.IsAbs(path) || strings.HasPrefix(path, string(separator))
return filepath.IsAbs(path) || strings.HasPrefix(path, string(filepath.Separator))
}

func (fs *Memory) Stat(filename string) (os.FileInfo, error) {
Expand Down Expand Up @@ -177,6 +177,8 @@ func (fs *Memory) Remove(filename string) error {
return fs.s.Remove(filename)
}

// Falls back to Go's filepath.Join, which works differently depending on the
// OS where the code is being executed.
func (fs *Memory) Join(elem ...string) string {
return filepath.Join(elem...)
}
Expand All @@ -187,7 +189,7 @@ func (fs *Memory) Symlink(target, link string) error {
return os.ErrExist
}

if !os.IsNotExist(err) {
if !errors.Is(err, os.ErrNotExist) {
return err
}

Expand Down Expand Up @@ -238,7 +240,7 @@ func (f *file) Read(b []byte) (int, error) {
n, err := f.ReadAt(b, f.position)
f.position += int64(n)

if err == io.EOF && n != 0 {
if errors.Is(err, io.EOF) && n != 0 {
err = nil
}

Expand Down
3 changes: 2 additions & 1 deletion memfs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"os"
"path/filepath"
"strings"
"sync"
)

Expand Down Expand Up @@ -112,7 +113,7 @@ func (s *storage) Rename(from, to string) error {
move := [][2]string{{from, to}}

for pathFrom := range s.files {
if pathFrom == from || !filepath.HasPrefix(pathFrom, from) {
if pathFrom == from || !strings.HasPrefix(pathFrom, from) {
continue
}

Expand Down
19 changes: 10 additions & 9 deletions util/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"errors"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -33,14 +34,14 @@ func removeAll(fs billy.Basic, path string) error {

// Simple case: if Remove works, we're done.
err := fs.Remove(path)
if err == nil || os.IsNotExist(err) {
if err == nil || errors.Is(err, os.ErrNotExist) {
return nil
}

// Otherwise, is this a directory we need to recurse into?
dir, serr := fs.Stat(path)
if serr != nil {
if os.IsNotExist(serr) {
if errors.Is(serr, os.ErrNotExist) {
return nil
}

Expand All @@ -60,7 +61,7 @@ func removeAll(fs billy.Basic, path string) error {
// Directory.
fis, err := dirfs.ReadDir(path)
if err != nil {
if os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
// Race. It was deleted between the Lstat and Open.
// Return nil per RemoveAll's docs.
return nil
Expand All @@ -81,7 +82,7 @@ func removeAll(fs billy.Basic, path string) error {

// Remove directory.
err1 := fs.Remove(path)
if err1 == nil || os.IsNotExist(err1) {
if err1 == nil || errors.Is(err1, os.ErrNotExist) {
return nil
}

Expand Down Expand Up @@ -154,7 +155,7 @@ func TempFile(fs billy.Basic, dir, prefix string) (f billy.File, err error) {
for i := 0; i < 10000; i++ {
name := filepath.Join(dir, prefix+nextSuffix())
f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
if errors.Is(err, os.ErrExist) {
if nconflict++; nconflict > 10 {
randmu.Lock()
rand = reseed()
Expand Down Expand Up @@ -185,16 +186,16 @@ func TempDir(fs billy.Dir, dir, prefix string) (name string, err error) {
for i := 0; i < 10000; i++ {
try := filepath.Join(dir, prefix+nextSuffix())
err = fs.MkdirAll(try, 0700)
if os.IsExist(err) {
if errors.Is(err, os.ErrExist) {
if nconflict++; nconflict > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
if os.IsNotExist(err) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
if errors.Is(err, os.ErrNotExist) {
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
return "", err
}
}
Expand Down Expand Up @@ -272,7 +273,7 @@ func ReadFile(fs billy.Basic, name string) ([]byte, error) {
data = data[:len(data)+n]

if err != nil {
if err == io.EOF {
if errors.Is(err, io.EOF) {
err = nil
}

Expand Down

0 comments on commit adc7a06

Please sign in to comment.