| 1 | package mount |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "io" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strings" |
| 11 | |
| 12 | "github.com/go-git/go-billy/v6" |
| 13 | "github.com/go-git/go-billy/v6/helper/polyfill" |
| 14 | ) |
| 15 | |
| 16 | var separator = string(filepath.Separator) |
| 17 | |
| 18 | // Mount is a helper that allows to emulate the behavior of mount in memory. |
| 19 | // Very useful to create a temporal dir, on filesystem where is a performance |
| 20 | // penalty in doing so. |
| 21 | type Mount struct { |
| 22 | underlying billy.Filesystem |
| 23 | source billy.Filesystem |
| 24 | mountpoint string |
| 25 | } |
| 26 | |
| 27 | // New creates a new filesystem wrapping up 'fs' the intercepts all the calls |
| 28 | // made to `mountpoint` path and redirecting it to `source` filesystem. |
| 29 | func New(fs billy.Basic, mountpoint string, source billy.Basic) *Mount { |
| 30 | return &Mount{ |
| 31 | underlying: polyfill.New(fs), |
| 32 | source: polyfill.New(source), |
| 33 | mountpoint: cleanPath(mountpoint), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func (h *Mount) Create(path string) (billy.File, error) { |
| 38 | fs, fullpath := h.getBasicAndPath(path) |
| 39 | if fullpath == "." { |
| 40 | return nil, os.ErrInvalid |
| 41 | } |
| 42 | |
| 43 | f, err := fs.Create(fullpath) |
| 44 | return wrapFile(f, path), err |
| 45 | } |
| 46 | |
| 47 | func (h *Mount) Open(path string) (billy.File, error) { |
| 48 | fs, fullpath := h.getBasicAndPath(path) |
| 49 | if fullpath == "." { |
| 50 | return nil, os.ErrInvalid |
| 51 | } |
| 52 | |
| 53 | f, err := fs.Open(fullpath) |
| 54 | return wrapFile(f, path), err |
| 55 | } |
| 56 | |
| 57 | func (h *Mount) OpenFile(path string, flag int, mode fs.FileMode) (billy.File, error) { |
| 58 | fs, fullpath := h.getBasicAndPath(path) |
| 59 | if fullpath == "." { |
| 60 | return nil, os.ErrInvalid |
| 61 | } |
| 62 | |
| 63 | f, err := fs.OpenFile(fullpath, flag, mode) |
| 64 | return wrapFile(f, path), err |
| 65 | } |
| 66 | |
| 67 | func (h *Mount) Rename(from, to string) error { |
| 68 | fromInSource := h.isMountpoint(from) |
| 69 | toInSource := h.isMountpoint(to) |
| 70 | |
| 71 | var fromFS, toFS billy.Basic |
| 72 | |
| 73 | switch { |
| 74 | case fromInSource && toInSource: |
| 75 | from = h.mustRelToMountpoint(from) |
| 76 | to = h.mustRelToMountpoint(to) |
| 77 | return h.source.Rename(from, to) |
| 78 | case !fromInSource && !toInSource: |
| 79 | return h.underlying.Rename(from, to) |
| 80 | case fromInSource && !toInSource: |
| 81 | fromFS = h.source |
| 82 | from = h.mustRelToMountpoint(from) |
| 83 | toFS = h.underlying |
| 84 | to = cleanPath(to) |
| 85 | case !fromInSource && toInSource: |
| 86 | fromFS = h.underlying |
| 87 | from = cleanPath(from) |
| 88 | toFS = h.source |
| 89 | to = h.mustRelToMountpoint(to) |
| 90 | } |
| 91 | |
| 92 | if err := copyPath(fromFS, toFS, from, to); err != nil { |
| 93 | return err |
| 94 | } |
| 95 | |
| 96 | return fromFS.Remove(from) |
| 97 | } |
| 98 | |
| 99 | func (h *Mount) Stat(path string) (os.FileInfo, error) { |
| 100 | fs, fullpath := h.getBasicAndPath(path) |
| 101 | return fs.Stat(fullpath) |
| 102 | } |
| 103 | |
| 104 | func (h *Mount) Remove(path string) error { |
| 105 | fs, fullpath := h.getBasicAndPath(path) |
| 106 | if fullpath == "." { |
| 107 | return os.ErrInvalid |
| 108 | } |
| 109 | |
| 110 | return fs.Remove(fullpath) |
| 111 | } |
| 112 | |
| 113 | func (h *Mount) ReadDir(path string) ([]fs.DirEntry, error) { |
| 114 | fs, fullpath, err := h.getDirAndPath(path) |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |
| 118 | |
| 119 | return fs.ReadDir(fullpath) |
| 120 | } |
| 121 | |
| 122 | func (h *Mount) MkdirAll(filename string, perm fs.FileMode) error { |
| 123 | fs, fullpath, err := h.getDirAndPath(filename) |
| 124 | if err != nil { |
| 125 | return err |
| 126 | } |
| 127 | |
| 128 | return fs.MkdirAll(fullpath, perm) |
| 129 | } |
| 130 | |
| 131 | func (h *Mount) Symlink(target, link string) error { |
| 132 | fs, fullpath, err := h.getSymlinkAndPath(link) |
| 133 | if err != nil { |
| 134 | return err |
| 135 | } |
| 136 | |
| 137 | resolved := filepath.Join(filepath.Dir(link), target) |
| 138 | if h.isMountpoint(resolved) != h.isMountpoint(link) { |
| 139 | return fmt.Errorf("invalid symlink, target is crossing filesystems") |
| 140 | } |
| 141 | |
| 142 | return fs.Symlink(target, fullpath) |
| 143 | } |
| 144 | |
| 145 | func (h *Mount) Join(elem ...string) string { |
| 146 | return h.underlying.Join(elem...) |
| 147 | } |
| 148 | |
| 149 | func (h *Mount) Readlink(link string) (string, error) { |
| 150 | fs, fullpath, err := h.getSymlinkAndPath(link) |
| 151 | if err != nil { |
| 152 | return "", err |
| 153 | } |
| 154 | |
| 155 | return fs.Readlink(fullpath) |
| 156 | } |
| 157 | |
| 158 | func (h *Mount) Lstat(path string) (os.FileInfo, error) { |
| 159 | fs, fullpath, err := h.getSymlinkAndPath(path) |
| 160 | if err != nil { |
| 161 | return nil, err |
| 162 | } |
| 163 | |
| 164 | return fs.Lstat(fullpath) |
| 165 | } |
| 166 | |
| 167 | func (h *Mount) Underlying() billy.Basic { |
| 168 | return h.underlying |
| 169 | } |
| 170 | |
| 171 | // Capabilities implements the Capable interface. |
| 172 | func (h *Mount) Capabilities() billy.Capability { |
| 173 | return billy.Capabilities(h.underlying) & billy.Capabilities(h.source) |
| 174 | } |
| 175 | |
| 176 | func (h *Mount) getBasicAndPath(path string) (billy.Basic, string) { |
| 177 | path = cleanPath(path) |
| 178 | if !h.isMountpoint(path) { |
| 179 | return h.underlying, path |
| 180 | } |
| 181 | |
| 182 | return h.source, h.mustRelToMountpoint(path) |
| 183 | } |
| 184 | |
| 185 | func (h *Mount) getDirAndPath(path string) (billy.Dir, string, error) { |
| 186 | path = cleanPath(path) |
| 187 | if !h.isMountpoint(path) { |
| 188 | d, ok := h.underlying.(billy.Dir) |
| 189 | if !ok { |
| 190 | return nil, "", errors.New("underlying fs does not implement billy.Dir") |
| 191 | } |
| 192 | return d, path, nil |
| 193 | } |
| 194 | |
| 195 | d, ok := h.source.(billy.Dir) |
| 196 | if !ok { |
| 197 | return nil, "", errors.New("source fs does not implement billy.Dir") |
| 198 | } |
| 199 | return d, h.mustRelToMountpoint(path), nil |
| 200 | } |
| 201 | |
| 202 | func (h *Mount) getSymlinkAndPath(path string) (billy.Symlink, string, error) { |
| 203 | path = cleanPath(path) |
| 204 | if !h.isMountpoint(path) { |
| 205 | sl, ok := h.underlying.(billy.Symlink) |
| 206 | if !ok { |
| 207 | return nil, "", errors.New("underlying fs does not implement billy.Symlink") |
| 208 | } |
| 209 | |
| 210 | return sl, path, nil |
| 211 | } |
| 212 | |
| 213 | sl, ok := h.source.(billy.Symlink) |
| 214 | if !ok { |
| 215 | return nil, "", errors.New("source fs does not implement billy.Symlink") |
| 216 | } |
| 217 | |
| 218 | return sl, h.mustRelToMountpoint(path), nil |
| 219 | } |
| 220 | |
| 221 | func (h *Mount) mustRelToMountpoint(path string) string { |
| 222 | path = cleanPath(path) |
| 223 | fullpath, err := filepath.Rel(h.mountpoint, path) |
| 224 | if err != nil { |
| 225 | panic(err) |
| 226 | } |
| 227 | |
| 228 | return fullpath |
| 229 | } |
| 230 | |
| 231 | func (h *Mount) isMountpoint(path string) bool { |
| 232 | path = cleanPath(path) |
| 233 | return path == h.mountpoint || strings.HasPrefix(path, h.mountpoint+separator) |
| 234 | } |
| 235 | |
| 236 | func cleanPath(path string) string { |
| 237 | path = filepath.FromSlash(path) |
| 238 | rel, err := filepath.Rel(separator, path) |
| 239 | if err == nil { |
| 240 | path = rel |
| 241 | } |
| 242 | |
| 243 | return filepath.Clean(path) |
| 244 | } |
| 245 | |
| 246 | // copyPath copies a file across filesystems. |
| 247 | func copyPath(src, dst billy.Basic, srcPath, dstPath string) error { |
| 248 | dstFile, err := dst.Create(dstPath) |
| 249 | if err != nil { |
| 250 | return err |
| 251 | } |
| 252 | |
| 253 | srcFile, err := src.Open(srcPath) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 | |
| 258 | _, err = io.Copy(dstFile, srcFile) |
| 259 | if err != nil { |
| 260 | return err |
| 261 | } |
| 262 | |
| 263 | err = dstFile.Close() |
| 264 | if err != nil { |
| 265 | _ = srcFile.Close() |
| 266 | return err |
| 267 | } |
| 268 | |
| 269 | return srcFile.Close() |
| 270 | } |
| 271 | |
| 272 | type file struct { |
| 273 | billy.File |
| 274 | name string |
| 275 | } |
| 276 | |
| 277 | func wrapFile(f billy.File, filename string) billy.File { |
| 278 | if f == nil { |
| 279 | return nil |
| 280 | } |
| 281 | |
| 282 | return &file{ |
| 283 | File: f, |
| 284 | name: cleanPath(filename), |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | func (f *file) Name() string { |
| 289 | return f.name |
| 290 | } |