feat: Add O_RDONLY, O_WRONLY and O_RDWR flag support

This commit is contained in:
Felicitas Pojtinger
2021-12-27 16:53:50 +01:00
parent a03d1e5016
commit 77060795e6
2 changed files with 30 additions and 6 deletions

View File

@@ -34,8 +34,8 @@ type WriteCache interface {
} }
type FileFlags struct { type FileFlags struct {
readOnly bool read bool
writeOnly bool write bool
append bool append bool
createIfNotExists bool createIfNotExists bool
@@ -326,6 +326,10 @@ func (f *File) Read(p []byte) (n int, err error) {
return -1, ErrIsDirectory return -1, ErrIsDirectory
} }
if !f.flags.read {
return -1, os.ErrPermission
}
f.ioLock.Lock() f.ioLock.Lock()
defer f.ioLock.Unlock() defer f.ioLock.Unlock()
@@ -387,6 +391,10 @@ func (f *File) ReadAt(p []byte, off int64) (n int, err error) {
return -1, ErrIsDirectory return -1, ErrIsDirectory
} }
if !f.flags.read {
return -1, os.ErrPermission
}
if _, err := f.Seek(off, io.SeekStart); err != nil { if _, err := f.Seek(off, io.SeekStart); err != nil {
return -1, err return -1, err
} }
@@ -481,6 +489,10 @@ func (f *File) Write(p []byte) (n int, err error) {
return -1, ErrIsDirectory return -1, ErrIsDirectory
} }
if !f.flags.write {
return -1, os.ErrPermission
}
f.ioLock.Lock() f.ioLock.Lock()
defer f.ioLock.Unlock() defer f.ioLock.Unlock()
@@ -505,6 +517,10 @@ func (f *File) WriteAt(p []byte, off int64) (n int, err error) {
return -1, ErrIsDirectory return -1, ErrIsDirectory
} }
if !f.flags.write {
return -1, os.ErrPermission
}
f.ioLock.Lock() f.ioLock.Lock()
defer f.ioLock.Unlock() defer f.ioLock.Unlock()
@@ -522,6 +538,10 @@ func (f *File) WriteString(s string) (ret int, err error) {
return -1, ErrIsDirectory return -1, ErrIsDirectory
} }
if !f.flags.write {
return -1, os.ErrPermission
}
f.ioLock.Lock() f.ioLock.Lock()
defer f.ioLock.Unlock() defer f.ioLock.Unlock()
@@ -539,6 +559,10 @@ func (f *File) Truncate(size int64) error {
return ErrIsDirectory return ErrIsDirectory
} }
if !f.flags.write {
return os.ErrPermission
}
f.ioLock.Lock() f.ioLock.Lock()
defer f.ioLock.Unlock() defer f.ioLock.Unlock()

View File

@@ -184,16 +184,16 @@ func (f *FileSystem) OpenFile(name string, flag int, perm os.FileMode) (afero.Fi
flags := &FileFlags{} flags := &FileFlags{}
if flag&os.O_RDONLY != 0 { if flag&os.O_RDONLY != 0 {
flags.readOnly = true flags.read = true
} }
if flag&os.O_WRONLY != 0 { if flag&os.O_WRONLY != 0 {
flags.writeOnly = true flags.write = true
} }
if flag&os.O_RDWR != 0 { if flag&os.O_RDWR != 0 {
flags.readOnly = true flags.read = true
flags.writeOnly = true flags.write = true
} }
if flag&os.O_APPEND != 0 { if flag&os.O_APPEND != 0 {