Skip to content

Commit

Permalink
fix: use FileMode.IsDir() (#18)
Browse files Browse the repository at this point in the history
* fix: use FileMode.IsDir()
  • Loading branch information
yktakaha4 authored Jan 24, 2023
1 parent b382a6f commit 7b46b09
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 19 deletions.
20 changes: 10 additions & 10 deletions cleanse.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package memoryfs

import (
"path/filepath"
"strings"
"path/filepath"
"strings"
)

func cleanse(path string) string {
path = strings.ReplaceAll(path, "/", separator)
path = filepath.Clean(path)
path = strings.TrimPrefix(path, "."+separator)
path = strings.TrimPrefix(path, separator)
if path == "." {
return ""
}
return path
path = strings.ReplaceAll(path, "/", separator)
path = filepath.Clean(path)
path = strings.TrimPrefix(path, "."+separator)
path = strings.TrimPrefix(path, separator)
if path == "." {
return ""
}
return path
}
15 changes: 12 additions & 3 deletions dir.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package memoryfs

import (
"fmt"
"io"
"io/fs"
"path/filepath"
Expand Down Expand Up @@ -189,13 +190,15 @@ func (d *dir) MkdirAll(path string, perm fs.FileMode) error {
}

d.Lock()
if perm&fs.ModeDir == 0 {
perm |= fs.ModeDir
}
if _, ok := d.dirs[parts[0]]; !ok {
d.dirs[parts[0]] = &dir{
info: fileinfo{
name: parts[0],
size: 0x100,
modified: time.Now(),
isDir: true,
mode: perm,
},
dirs: map[string]*dir{},
Expand All @@ -217,6 +220,10 @@ func (d *dir) MkdirAll(path string, perm fs.FileMode) error {
func (d *dir) WriteFile(path string, data []byte, perm fs.FileMode) error {
parts := strings.Split(path, separator)

if perm&fs.ModeDir != 0 {
return fmt.Errorf("invalid perm: %v", perm)
}

if len(parts) == 1 {
max := bufferSize
if len(data) > max {
Expand All @@ -236,7 +243,6 @@ func (d *dir) WriteFile(path string, data []byte, perm fs.FileMode) error {
name: parts[0],
size: int64(len(buffer)),
modified: time.Now(),
isDir: false,
mode: perm,
},
content: buffer,
Expand Down Expand Up @@ -304,6 +310,10 @@ func (d *dir) glob(pattern string) ([]string, error) {
func (d *dir) WriteLazyFile(path string, opener LazyOpener, perm fs.FileMode) error {
parts := strings.Split(path, separator)

if perm&fs.ModeDir != 0 {
return fmt.Errorf("invalid perm: %v", perm)
}

if len(parts) == 1 {
d.Lock()
defer d.Unlock()
Expand All @@ -312,7 +322,6 @@ func (d *dir) WriteLazyFile(path string, opener LazyOpener, perm fs.FileMode) er
name: parts[0],
size: 0,
modified: time.Now(),
isDir: false,
mode: perm,
},
opener: opener,
Expand Down
1 change: 0 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type fileAccess struct {
reader io.Reader
}


// LazyOpener provides an io.Reader that can be used to access the content of a file, whatever the actual storage medium.
// If the LazyOpener returns an io.ReadCloser, it will be closed after each read.
type LazyOpener func() (io.Reader, error)
Expand Down
3 changes: 1 addition & 2 deletions fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ type fileinfo struct {
name string
size int64
modified time.Time
isDir bool
mode fs.FileMode
}

Expand Down Expand Up @@ -46,7 +45,7 @@ func (f fileinfo) ModTime() time.Time {

// IsDir reports whether the entry describes a directory.
func (f fileinfo) IsDir() bool {
return f.isDir
return f.Mode().IsDir()
}

// Sys is the underlying data source of the file (always nil)
Expand Down
3 changes: 1 addition & 2 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func New() *FS {
name: ".",
size: 0x100,
modified: time.Now(),
isDir: true,
mode: 0o700,
mode: 0o0700 | fs.ModeDir,
},
dirs: map[string]*dir{},
files: map[string]*file{},
Expand Down
21 changes: 20 additions & 1 deletion fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func Test_AllOperations(t *testing.T) {
require.NoError(t, memfs.WriteFile("files/a/b/c/.secret", []byte("secret file!"), 0o644))
require.NoError(t, memfs.WriteFile("files/a/b/c/note.txt", []byte(":)"), 0o644))
require.NoError(t, memfs.WriteFile("files/a/middle.txt", []byte(":("), 0o644))
require.NoError(t, memfs.MkdirAll("files/xyz", 0o700|fs.ModeDir))

require.Error(t, memfs.WriteFile("test.txt", []byte("hello world"), 0o644|fs.ModeDir))
require.Error(t, memfs.WriteLazyFile("test.txt", func() (io.Reader, error) {
return strings.NewReader("hello"), nil
}, 0o644|fs.ModeDir))

t.Run("Open file", func(t *testing.T) {
f, err := memfs.Open("test.txt")
Expand Down Expand Up @@ -78,6 +84,7 @@ func Test_AllOperations(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, "test.txt", info.Name())
assert.Equal(t, fs.FileMode(0o644), info.Mode())
assert.Equal(t, false, info.Mode().IsDir())
assert.Equal(t, false, info.IsDir())
assert.Equal(t, int64(11), info.Size())
})
Expand All @@ -87,6 +94,7 @@ func Test_AllOperations(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, ".secret", info.Name())
assert.Equal(t, fs.FileMode(0o644), info.Mode())
assert.Equal(t, false, info.Mode().IsDir())
assert.Equal(t, false, info.IsDir())
assert.Equal(t, int64(12), info.Size())
})
Expand All @@ -97,6 +105,16 @@ func Test_AllOperations(t *testing.T) {
assert.Nil(t, info)
})

t.Run("Stat directory", func(t *testing.T) {
info, err := memfs.Stat("files/xyz")
require.NoError(t, err)
assert.Equal(t, "xyz", info.Name())
assert.Equal(t, fs.FileMode(0o700|fs.ModeDir), info.Mode())
assert.Equal(t, true, info.IsDir())
assert.Equal(t, true, info.Mode().IsDir())
assert.Equal(t, int64(256), info.Size())
})

t.Run("List directory at root", func(t *testing.T) {
entries, err := fs.ReadDir(memfs, ".")
require.NoError(t, err)
Expand Down Expand Up @@ -125,8 +143,9 @@ func Test_AllOperations(t *testing.T) {
info, err := memfs.Stat(".")
require.NoError(t, err)
assert.Equal(t, ".", info.Name())
assert.Equal(t, fs.FileMode(0o700), info.Mode())
assert.Equal(t, fs.FileMode(0o700|fs.ModeDir), info.Mode())
assert.Equal(t, true, info.IsDir())
assert.Equal(t, true, info.Mode().IsDir())
})

t.Run("ReadFile", func(t *testing.T) {
Expand Down

0 comments on commit 7b46b09

Please sign in to comment.