From 030e8c054a1536c5b30e55ac84015f6e24bd0de2 Mon Sep 17 00:00:00 2001 From: SonOfBytes Date: Tue, 22 Nov 2016 16:24:52 +0000 Subject: [PATCH] Set Permissions after OpenFile, MkDir and MkDirAll methods called. Added tests to validate. --- memmap.go | 7 ++++++ memmap_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/memmap.go b/memmap.go index 2e259b8..bdce2cc 100644 --- a/memmap.go +++ b/memmap.go @@ -136,6 +136,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 } @@ -205,9 +206,12 @@ 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 we create a new file } if err != nil { return nil, err @@ -229,6 +233,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)