Skip to content

Commit fb69ec1

Browse files
committed
memfs: support for filemodes
1 parent 24ba1e5 commit fb69ec1

File tree

4 files changed

+122
-107
lines changed

4 files changed

+122
-107
lines changed

memfs/memory.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func New() *Memory {
3333

3434
// Create returns a new file in memory from a given filename.
3535
func (fs *Memory) Create(filename string) (billy.File, error) {
36-
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0)
36+
return fs.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
3737
}
3838

3939
// Open returns a readonly file from a given name.
@@ -51,15 +51,15 @@ func (fs *Memory) OpenFile(filename string, flag int, perm os.FileMode) (billy.F
5151
return nil, os.ErrNotExist
5252
}
5353

54-
fs.s.files[fullpath] = newFile(fs.base, fullpath, flag)
54+
fs.s.files[fullpath] = newFile(fs.base, fullpath, perm, flag)
5555
return fs.s.files[fullpath], nil
5656
}
5757

5858
if f.isDir {
5959
return nil, fmt.Errorf("cannot open directory: %s", filename)
6060
}
6161

62-
n := newFile(fs.base, fullpath, flag)
62+
n := newFile(fs.base, fullpath, perm, flag)
6363
n.content = f.content
6464

6565
if isAppend(flag) {
@@ -79,12 +79,12 @@ func (fs *Memory) Stat(filename string) (billy.FileInfo, error) {
7979

8080
f, ok := fs.s.files[fullpath]
8181
if ok && !f.isDir {
82-
return newFileInfo(fs.base, fullpath, fs.s.files[fullpath].content.Len()), nil
82+
return newFileInfo(fs.base, fullpath, f.mode, fs.s.files[fullpath].content.Len()), nil
8383
}
8484

8585
info, err := fs.ReadDir(fullpath)
8686
if err == nil && len(info) != 0 || f != nil && f.isDir {
87-
fi := newFileInfo(fs.base, fullpath, len(info))
87+
fi := newFileInfo(fs.base, fullpath, 0, len(info))
8888
fi.isDir = true
8989
return fi, nil
9090
}
@@ -110,15 +110,15 @@ func (fs *Memory) ReadDir(base string) (entries []billy.FileInfo, err error) {
110110
entries = append(entries, &fileInfo{name: parts[0], isDir: true})
111111
}
112112

113-
entries = append(entries, &fileInfo{name: parts[0], size: f.content.Len()})
113+
entries = append(entries, &fileInfo{name: parts[0], mode: f.mode, size: f.content.Len()})
114114
continue
115115
}
116116

117117
if _, ok := appendedDirs[parts[0]]; ok {
118118
continue
119119
}
120120

121-
entries = append(entries, &fileInfo{name: parts[0], isDir: true})
121+
entries = append(entries, &fileInfo{name: parts[0], mode: f.mode, isDir: true})
122122
appendedDirs[parts[0]] = true
123123
}
124124

@@ -232,15 +232,17 @@ type file struct {
232232
content *content
233233
position int64
234234
flag int
235+
mode os.FileMode
235236
isDir bool
236237
}
237238

238-
func newFile(base, fullpath string, flag int) *file {
239+
func newFile(base, fullpath string, mode os.FileMode, flag int) *file {
239240
filename, _ := filepath.Rel(base, fullpath)
240241

241242
return &file{
242243
BaseFile: billy.BaseFile{BaseFilename: filename},
243244
content: &content{},
245+
mode: mode,
244246
flag: flag,
245247
}
246248
}
@@ -318,14 +320,16 @@ func (f *file) Open() error {
318320
type fileInfo struct {
319321
name string
320322
size int
323+
mode os.FileMode
321324
isDir bool
322325
}
323326

324-
func newFileInfo(base, fullpath string, size int) *fileInfo {
327+
func newFileInfo(base, fullpath string, mode os.FileMode, size int) *fileInfo {
325328
filename, _ := filepath.Rel(base, fullpath)
326329

327330
return &fileInfo{
328331
name: filename,
332+
mode: mode,
329333
size: size,
330334
}
331335
}
@@ -339,7 +343,7 @@ func (fi *fileInfo) Size() int64 {
339343
}
340344

341345
func (fi *fileInfo) Mode() os.FileMode {
342-
return os.FileMode(0)
346+
return fi.mode
343347
}
344348

345349
func (*fileInfo) ModTime() time.Time {

memfs/memory_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ type MemorySuite struct {
1717
var _ = Suite(&MemorySuite{})
1818

1919
func (s *MemorySuite) SetUpTest(c *C) {
20-
s.FilesystemSuite.Fs = New()
20+
s.FilesystemSuite.FS = New()
2121
}
2222

2323
func (s *MemorySuite) TestTempFileMaxTempFiles(c *C) {
2424
for i := 0; i < maxTempFiles; i++ {
25-
f, err := s.FilesystemSuite.Fs.TempFile("", "")
25+
f, err := s.FilesystemSuite.FS.TempFile("", "")
2626
c.Assert(err, IsNil)
2727
c.Assert(f, NotNil)
2828
}
2929

30-
f, err := s.FilesystemSuite.Fs.TempFile("", "")
30+
f, err := s.FilesystemSuite.FS.TempFile("", "")
3131
c.Assert(err, NotNil)
3232
c.Assert(f, IsNil)
3333
}

osfs/os_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ var _ = Suite(&OSSuite{})
2121

2222
func (s *OSSuite) SetUpTest(c *C) {
2323
s.path, _ = ioutil.TempDir(os.TempDir(), "go-git-os-fs-test")
24-
s.FilesystemSuite.Fs = New(s.path)
24+
s.FilesystemSuite.FS = New(s.path)
2525
}
2626
func (s *OSSuite) TearDownTest(c *C) {
2727
err := os.RemoveAll(s.path)
2828
c.Assert(err, IsNil)
2929
}
3030

3131
func (s *OSSuite) TestOpenDoesNotCreateDir(c *C) {
32-
_, err := s.Fs.Open("dir/non-existent")
32+
_, err := s.FS.Open("dir/non-existent")
3333
c.Assert(err, NotNil)
3434
_, err = os.Stat(filepath.Join(s.path, "dir"))
3535
c.Assert(os.IsNotExist(err), Equals, true)

0 commit comments

Comments
 (0)