readonlyfs.go (2120B)
1 package afero 2 3 import ( 4 "os" 5 "syscall" 6 "time" 7 ) 8 9 var _ Lstater = (*ReadOnlyFs)(nil) 10 11 type ReadOnlyFs struct { 12 source Fs 13 } 14 15 func NewReadOnlyFs(source Fs) Fs { 16 return &ReadOnlyFs{source: source} 17 } 18 19 func (r *ReadOnlyFs) ReadDir(name string) ([]os.FileInfo, error) { 20 return ReadDir(r.source, name) 21 } 22 23 func (r *ReadOnlyFs) Chtimes(n string, a, m time.Time) error { 24 return syscall.EPERM 25 } 26 27 func (r *ReadOnlyFs) Chmod(n string, m os.FileMode) error { 28 return syscall.EPERM 29 } 30 31 func (r *ReadOnlyFs) Chown(n string, uid, gid int) error { 32 return syscall.EPERM 33 } 34 35 func (r *ReadOnlyFs) Name() string { 36 return "ReadOnlyFilter" 37 } 38 39 func (r *ReadOnlyFs) Stat(name string) (os.FileInfo, error) { 40 return r.source.Stat(name) 41 } 42 43 func (r *ReadOnlyFs) LstatIfPossible(name string) (os.FileInfo, bool, error) { 44 if lsf, ok := r.source.(Lstater); ok { 45 return lsf.LstatIfPossible(name) 46 } 47 fi, err := r.Stat(name) 48 return fi, false, err 49 } 50 51 func (r *ReadOnlyFs) SymlinkIfPossible(oldname, newname string) error { 52 return &os.LinkError{Op: "symlink", Old: oldname, New: newname, Err: ErrNoSymlink} 53 } 54 55 func (r *ReadOnlyFs) ReadlinkIfPossible(name string) (string, error) { 56 if srdr, ok := r.source.(LinkReader); ok { 57 return srdr.ReadlinkIfPossible(name) 58 } 59 60 return "", &os.PathError{Op: "readlink", Path: name, Err: ErrNoReadlink} 61 } 62 63 func (r *ReadOnlyFs) Rename(o, n string) error { 64 return syscall.EPERM 65 } 66 67 func (r *ReadOnlyFs) RemoveAll(p string) error { 68 return syscall.EPERM 69 } 70 71 func (r *ReadOnlyFs) Remove(n string) error { 72 return syscall.EPERM 73 } 74 75 func (r *ReadOnlyFs) OpenFile(name string, flag int, perm os.FileMode) (File, error) { 76 if flag&(os.O_WRONLY|syscall.O_RDWR|os.O_APPEND|os.O_CREATE|os.O_TRUNC) != 0 { 77 return nil, syscall.EPERM 78 } 79 return r.source.OpenFile(name, flag, perm) 80 } 81 82 func (r *ReadOnlyFs) Open(n string) (File, error) { 83 return r.source.Open(n) 84 } 85 86 func (r *ReadOnlyFs) Mkdir(n string, p os.FileMode) error { 87 return syscall.EPERM 88 } 89 90 func (r *ReadOnlyFs) MkdirAll(n string, p os.FileMode) error { 91 return syscall.EPERM 92 } 93 94 func (r *ReadOnlyFs) Create(n string) (File, error) { 95 return nil, syscall.EPERM 96 }