Skip to content

Commit

Permalink
feat: support to FileInfo.Sys() (#19)
Browse files Browse the repository at this point in the history
Co-authored-by: Liam Galvin <liamgalvin@protonmail.com>
  • Loading branch information
yktakaha4 and liamg committed Jan 24, 2023
1 parent 7b46b09 commit 82830c8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
5 changes: 3 additions & 2 deletions fileinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type fileinfo struct {
size int64
modified time.Time
mode fs.FileMode
sys interface{}
}

// Name is the base name of the file (without directory)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (f fileinfo) IsDir() bool {
return f.Mode().IsDir()
}

// Sys is the underlying data source of the file (always nil)
// Sys is the underlying data source of the file (can return nil)
func (f fileinfo) Sys() interface{} {
return nil
return f.sys
}
14 changes: 14 additions & 0 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,17 @@ func (m *FS) SetModified(name string, modified time.Time) error {
}
return &fs.PathError{Op: "set modified", Path: name, Err: fs.ErrNotExist}
}

// SetSys set underlying data source to file or directory
func (m *FS) SetSys(name string, sys interface{}) error {
name = cleanse(name)
if f, err := m.dir.getFile(name); err == nil {
f.info.sys = sys
return nil
}
if f, err := m.dir.getDir(name); err == nil {
f.info.sys = sys
return nil
}
return &fs.PathError{Op: "set sys", Path: name, Err: fs.ErrNotExist}
}
49 changes: 49 additions & 0 deletions fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,55 @@ func Test_AllOperations(t *testing.T) {
_, err = memfs.Stat("not_found.txt")
assert.Error(t, err)
})

t.Run("Set sys to directory", func(t *testing.T) {
type sysStat struct {
Gid int
Uid int
}

sys := sysStat{
Uid: 123,
Gid: 456,
}

stat, err := memfs.Stat("files/a/b/c")
assert.NoError(t, err)
assert.Nil(t, stat.Sys())

err = memfs.SetSys("files/a/b/c", sys)
assert.NoError(t, err)

stat, err = memfs.Stat("files/a/b/c")
assert.NoError(t, err)
assert.Equal(t, sys, stat.Sys())
})

t.Run("Set sys to file", func(t *testing.T) {
type sysStat struct {
Gid int
Uid int
}

sys := sysStat{
Uid: 123,
Gid: 456,
}

stat, err := memfs.Stat("test.txt")
assert.NoError(t, err)
assert.Nil(t, stat.Sys())

err = memfs.SetSys("test.txt", sys)
assert.NoError(t, err)

stat, err = memfs.Stat("test.txt")
assert.NoError(t, err)
assert.Equal(t, sys, stat.Sys())

err = memfs.SetSys("not_found.txt", sys)
assert.Error(t, err)
})
}

func Test_ConcurrentWritesToDirectory(t *testing.T) {
Expand Down

0 comments on commit 82830c8

Please sign in to comment.