diff --git a/memmap.go b/memmap.go index 26a7e53..735bdaf 100644 --- a/memmap.go +++ b/memmap.go @@ -134,6 +134,7 @@ func (m *MemMapFs) Mkdir(name string, perm os.FileMode) error { m.getData()[name] = item m.registerWithParent(item) m.mu.Unlock() + m.Chmod(name, perm) } return nil } @@ -203,9 +204,11 @@ func (m *MemMapFs) lockfreeOpen(name string) (*mem.FileData, error) { } func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) { + chmod := false file, err := m.openWrite(name) if os.IsNotExist(err) && (flag&os.O_CREATE > 0) { file, err = m.Create(name) + chmod = true } if err != nil { return nil, err @@ -227,6 +230,9 @@ func (m *MemMapFs) OpenFile(name string, flag int, perm os.FileMode) (File, erro return nil, err } } + if chmod { + m.Chmod(name, perm) + } return file, nil } diff --git a/memmap_test.go b/memmap_test.go index 9efd1f2..0bd10c9 100644 --- a/memmap_test.go +++ b/memmap_test.go @@ -102,6 +102,67 @@ func checkPathError(t *testing.T, err error, op string) { } } +// Ensure Permissions are set on OpenFile/Mkdir/MkdirAll +func TestPermSet(t *testing.T) { + const fileName = "/myFileTest" + const dirPath = "/myDirTest" + const dirPathAll = "/my/path/to/dir" + + const fileMode = os.FileMode(0765) + + fs := NewMemMapFs() + + // Test Openfile + f, err := fs.OpenFile(fileName, os.O_CREATE, fileMode) + if err != nil { + t.Errorf("OpenFile Create failed: %s", err) + return + } + f.Close() + + s, err := fs.Stat(fileName) + if err != nil { + t.Errorf("Stat failed: %s", err) + return + } + if s.Mode().String() != fileMode.String() { + t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), fileMode.String()) + return + } + + // Test Mkdir + err = fs.Mkdir(dirPath, fileMode) + if err != nil { + t.Errorf("MkDir Create failed: %s", err) + return + } + s, err = fs.Stat(dirPath) + if err != nil { + t.Errorf("Stat failed: %s", err) + return + } + if s.Mode().String() != fileMode.String() { + t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), fileMode.String()) + return + } + + // Test MkdirAll + err = fs.MkdirAll(dirPathAll, fileMode) + if err != nil { + t.Errorf("MkDir Create failed: %s", err) + return + } + s, err = fs.Stat(dirPathAll) + if err != nil { + t.Errorf("Stat failed: %s", err) + return + } + if s.Mode().String() != fileMode.String() { + t.Errorf("Permissions Incorrect: %s != %s", s.Mode().String(), fileMode.String()) + return + } +} + // Fails if multiple file objects use the same file.at counter in MemMapFs func TestMultipleOpenFiles(t *testing.T) { defer removeAllTestFiles(t)