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