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