Debian Codegraph @

helper/mount/mount.go

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