github.com/jspc/eggos@v0.5.1-0.20221028160421-556c75c878a5/fs/stripprefix/stripprefix.go (about) 1 // stripprefix strip the leading prefix of file name on access fs methods 2 // if file name is not an abs path, stripprefix do nothing 3 package stripprefix 4 5 import ( 6 "os" 7 "path/filepath" 8 "strings" 9 "time" 10 11 "github.com/spf13/afero" 12 ) 13 14 type fs struct { 15 prefix string 16 backend afero.Fs 17 } 18 19 func New(prefix string, backend afero.Fs) afero.Fs { 20 return &fs{ 21 prefix: prefix, 22 backend: backend, 23 } 24 } 25 26 func (f *fs) strip(name string) (string, error) { 27 if !filepath.IsAbs(name) { 28 return name, nil 29 } 30 p := strings.TrimPrefix(name, f.prefix) 31 if len(p) == len(name) { 32 return "", os.ErrNotExist 33 } 34 if p == "" { 35 return ".", nil 36 } 37 return p, nil 38 } 39 40 // Create creates a file in the filesystem, returning the file and an 41 // error, if any happens. 42 func (f *fs) Create(name string) (afero.File, error) { 43 p, err := f.strip(name) 44 if err != nil { 45 return nil, err 46 } 47 return f.backend.Create(p) 48 } 49 50 // Mkdir creates a directory in the filesystem, return an error if any 51 // happens. 52 func (f *fs) Mkdir(name string, perm os.FileMode) error { 53 p, err := f.strip(name) 54 if err != nil { 55 return err 56 } 57 return f.backend.Mkdir(p, perm) 58 } 59 60 // MkdirAll creates a directory path and all parents that does not exist 61 // yet. 62 func (f *fs) MkdirAll(path string, perm os.FileMode) error { 63 p, err := f.strip(path) 64 if err != nil { 65 return err 66 } 67 return f.backend.MkdirAll(p, perm) 68 } 69 70 // Open opens a file, returning it or an error, if any happens. 71 func (f *fs) Open(name string) (afero.File, error) { 72 p, err := f.strip(name) 73 if err != nil { 74 return nil, err 75 } 76 return f.backend.Open(p) 77 } 78 79 // OpenFile opens a file using the given flags and the given mode. 80 func (f *fs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) { 81 p, err := f.strip(name) 82 if err != nil { 83 return nil, err 84 } 85 return f.backend.OpenFile(p, flag, perm) 86 } 87 88 // Remove removes a file identified by name, returning an error, if any 89 // happens. 90 func (f *fs) Remove(name string) error { 91 p, err := f.strip(name) 92 if err != nil { 93 return err 94 } 95 return f.backend.Remove(p) 96 } 97 98 // RemoveAll removes a directory path and any children it contains. It 99 // does not fail if the path does not exist (return nil). 100 func (f *fs) RemoveAll(path string) error { 101 p, err := f.strip(path) 102 if err != nil { 103 return err 104 } 105 return f.backend.RemoveAll(p) 106 } 107 108 // Rename renames a file. 109 func (f *fs) Rename(oldname string, newname string) error { 110 p1, err := f.strip(oldname) 111 if err != nil { 112 return err 113 } 114 p2, err := f.strip(newname) 115 if err != nil { 116 return err 117 } 118 return f.backend.Rename(p1, p2) 119 } 120 121 // Stat returns a FileInfo describing the named file, or an error, if any 122 // happens. 123 func (f *fs) Stat(name string) (os.FileInfo, error) { 124 p, err := f.strip(name) 125 if err != nil { 126 return nil, err 127 } 128 return f.backend.Stat(p) 129 } 130 131 // The name of this FileSystem 132 func (f *fs) Name() string { 133 return "stripprefix" 134 } 135 136 //Chmod changes the mode of the named file to mode. 137 func (f *fs) Chmod(name string, mode os.FileMode) error { 138 p, err := f.strip(name) 139 if err != nil { 140 return err 141 } 142 return f.backend.Chmod(p, mode) 143 } 144 145 //Chtimes changes the access and modification times of the named file 146 func (f *fs) Chtimes(name string, atime time.Time, mtime time.Time) error { 147 p, err := f.strip(name) 148 if err != nil { 149 return err 150 } 151 return f.backend.Chtimes(p, atime, mtime) 152 }