spf13 / afero

A FileSystem Abstraction System for Go
Apache License 2.0
5.79k stars 498 forks source link

[MemMapFs] Will MemMapFs support creating read-only files? #401

Open kizuna-lek opened 10 months ago

kizuna-lek commented 10 months ago

When conducting tests, there are times when I want to create a read-only file, But i found that there is no option available to create a read-only file. I know there is ReadOnlyFs already, but new a fs seems unnecessary to me.

I found func Create in memmap.go

func (m *MemMapFs) Create(name string) (File, error) {
    name = normalizePath(name)
    m.mu.Lock()
    file := mem.CreateFile(name)
    m.getData()[name] = file
    m.registerWithParent(file, 0)
    m.mu.Unlock()
    return mem.NewFileHandle(file), nil
}

and NewReadOnlyFileHandle in file.go

func NewReadOnlyFileHandle(data *FileData) *File {
    return &File{fileData: data, readOnly: true}
}

Will it possible to support create read-only file with adding an additional parameter to the Create function like

func (m *MemMapFs) Create(name string, readOnly bool) (File, error) 
......
if readOnly {
    return NewReadOnlyFileHandle(file), nil
}
......

Or there might already be a way to create a read-only file. I have tried func (m *MemMapFs) Chmod, but it seems useless. If there are any better methods, i would appreciate to learn it.