Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Mkdir #21

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,55 @@ func (d *dir) MkdirAll(path string, perm fs.FileMode) error {
return d.dirs[parts[0]].MkdirAll(strings.Join(parts[1:], separator), perm)
}

func (d *dir) Mkdir(path string, perm fs.FileMode) error {
parts := strings.Split(path, separator)

if path == "" {
return fs.ErrExist
}

d.RLock()
_, fOk := d.files[parts[0]]
_, dOk := d.dirs[parts[0]]
d.RUnlock()
if fOk {
return fs.ErrExist
}
if !dOk && len(parts) > 1 {
return fs.ErrNotExist
}

if len(parts) == 1 {
d.Lock()
if perm&fs.ModeDir == 0 {
perm |= fs.ModeDir
}
_, ok := d.dirs[parts[0]]
if !ok {
d.dirs[parts[0]] = &dir{
info: fileinfo{
name: parts[0],
size: 0x100,
modified: time.Now(),
mode: perm,
},
dirs: map[string]*dir{},
files: map[string]*file{},
}
}
d.Unlock()
if ok {
return fs.ErrExist
}

return nil
}

d.RLock()
defer d.RUnlock()
return d.dirs[parts[0]].Mkdir(strings.Join(parts[1:], separator), perm)
}

func (d *dir) WriteFile(path string, data []byte, perm fs.FileMode) error {
parts := strings.Split(path, separator)

Expand Down
7 changes: 7 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ func (m *FS) MkdirAll(path string, perm fs.FileMode) error {
return m.dir.MkdirAll(cleanse(path), perm)
}

// Mkdir create a directory named path.
// Parent attributes are preserved.
// If path is already exists, returns an error.
func (m *FS) Mkdir(path string, perm fs.FileMode) error {
return m.dir.Mkdir(cleanse(path), perm)
}

// ReadFile reads the named file and returns its contents.
// A successful call returns a nil error, not io.EOF.
// (Because ReadFile reads the whole file, the expected EOF
Expand Down
78 changes: 78 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,84 @@ func Test_MkdirAllRoot(t *testing.T) {
require.NoError(t, err)
}

func Test_Mkdir(t *testing.T) {
memfs := New()
rootStat, err := memfs.Stat("/")
require.NoError(t, err)

rootMode := rootStat.Mode()
rootModTime := rootStat.ModTime()

t.Run("create directory on root", func(t *testing.T) {

err = memfs.Mkdir("/dir", 0o444)
assert.NoError(t, err)

stat, err := memfs.Stat("/dir")
assert.NoError(t, err)
assert.Equal(t, 0o444|fs.ModeDir, stat.Mode())

rootStat, err := memfs.Stat("/")
assert.NoError(t, err)
assert.Equal(t, rootMode, rootStat.Mode())
assert.Equal(t, rootModTime, rootStat.ModTime())
})

t.Run("create directory on sub directory", func(t *testing.T) {
err := memfs.Mkdir("/sub", 0o444)
assert.NoError(t, err)

subStat, err := memfs.Stat("/sub")
require.NoError(t, err)
subMode := subStat.Mode()
subModTime := subStat.ModTime()

err = memfs.Mkdir("/sub/dir", 0o666)
assert.NoError(t, err)

stat, err := memfs.Stat("/sub/dir")
assert.NoError(t, err)
assert.Equal(t, 0o666|fs.ModeDir, stat.Mode())

subStat, err = memfs.Stat("/sub")
assert.NoError(t, err)
assert.Equal(t, subMode, subStat.Mode())
assert.Equal(t, subModTime, subStat.ModTime())

rootStat, err := memfs.Stat("/")
assert.NoError(t, err)
assert.Equal(t, rootMode, rootStat.Mode())
assert.Equal(t, rootModTime, rootStat.ModTime())
})

t.Run("file already exists", func(t *testing.T) {
err := memfs.WriteFile("/file", []byte{}, 0o777)
assert.NoError(t, err)

err = memfs.Mkdir("/file", 0o644)
assert.Error(t, err)

err = memfs.Mkdir("/file/dir", 0o644)
assert.Error(t, err)
})

t.Run("directory already exists", func(t *testing.T) {
err := memfs.Mkdir("/", 0o644)
assert.Error(t, err)

err = memfs.Mkdir("/exists", 0o444)
assert.NoError(t, err)

err = memfs.Mkdir("/exists", 0o444)
assert.Error(t, err)
})

t.Run("parent not exists", func(t *testing.T) {
err := memfs.Mkdir("/not_exists/dir", 0o644)
assert.Error(t, err)
})
}

type entry struct {
path string
info fs.DirEntry
Expand Down