tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/engine/fs/readonlyfs/fs.go (about) 1 // Copyright © 2014 Steve Francia <spf@spf13.com>. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package readonlyfs 15 16 import ( 17 "io/fs" 18 "os" 19 "syscall" 20 "time" 21 ) 22 23 type FS struct { 24 fs.FS 25 } 26 27 func New(fsys fs.FS) *FS { 28 return &FS{FS: fsys} 29 } 30 31 func (r *FS) Chtimes(n string, a, m time.Time) error { 32 return fs.ErrPermission 33 } 34 35 func (r *FS) Chmod(n string, m fs.FileMode) error { 36 return fs.ErrPermission 37 } 38 39 func (r *FS) Chown(n string, uid, gid int) error { 40 return fs.ErrPermission 41 } 42 43 func (r *FS) Rename(o, n string) error { 44 return fs.ErrPermission 45 } 46 47 func (r *FS) RemoveAll(p string) error { 48 return fs.ErrPermission 49 } 50 51 func (r *FS) Remove(n string) error { 52 return fs.ErrPermission 53 } 54 55 func (r *FS) Mkdir(n string, p fs.FileMode) error { 56 return fs.ErrPermission 57 } 58 59 func (r *FS) MkdirAll(n string, p fs.FileMode) error { 60 return fs.ErrPermission 61 } 62 63 func (r *FS) Create(n string) (fs.File, error) { 64 return nil, fs.ErrPermission 65 } 66 67 func (r *FS) OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error) { 68 if flag&(os.O_WRONLY|syscall.O_RDWR|os.O_APPEND|os.O_CREATE|os.O_TRUNC) != 0 { 69 return nil, fs.ErrPermission 70 } 71 of, ok := r.FS.(interface { 72 OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error) 73 }) 74 if !ok { 75 return nil, fs.ErrPermission 76 } 77 return of.OpenFile(name, flag, perm) 78 }