mirror of https://github.com/spf13/afero.git
Merge pull request #110 from SonOfBytes/memfs-perm-fix
Set perm in MemMapFS after OpenFile, MkDir and MkDirAll methods called
This commit is contained in:
commit
2f30b2a92c
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue