forked from hanwen/go-fuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autounion.go
370 lines (313 loc) · 8.07 KB
/
autounion.go
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package unionfs
import (
"fmt"
"github.com/hanwen/go-fuse/fuse"
"log"
"os"
"path/filepath"
"strings"
"syscall"
"sync"
"time"
)
// Creates unions for all files under a given directory,
// walking the tree and looking for directories D which have a
// D/READONLY symlink.
//
// A union for A/B/C will placed under directory A-B-C.
type AutoUnionFs struct {
fuse.DefaultFileSystem
lock sync.RWMutex
knownFileSystems map[string]*UnionFs
nameRootMap map[string]string
root string
connector *fuse.FileSystemConnector
options *AutoUnionFsOptions
}
type AutoUnionFsOptions struct {
UnionFsOptions
fuse.FileSystemOptions
// If set, run updateKnownFses() after mounting.
UpdateOnMount bool
}
const (
_READONLY = "READONLY"
_STATUS = "status"
_CONFIG = "config"
_ROOT = "root"
_VERSION = "gounionfs_version"
_SCAN_CONFIG = ".scan_config"
)
func NewAutoUnionFs(directory string, options AutoUnionFsOptions) *AutoUnionFs {
a := new(AutoUnionFs)
a.knownFileSystems = make(map[string]*UnionFs)
a.nameRootMap = make(map[string]string)
a.options = &options
directory, err := filepath.Abs(directory)
if err != nil {
panic("filepath.Abs returned err")
}
a.root = directory
return a
}
func (me *AutoUnionFs) Mount(connector *fuse.FileSystemConnector) fuse.Status {
me.connector = connector
if me.options.UpdateOnMount {
time.AfterFunc(0.1e9, func() { me.updateKnownFses() })
}
return fuse.OK
}
func (me *AutoUnionFs) addAutomaticFs(roots []string) {
relative := strings.TrimLeft(strings.Replace(roots[0], me.root, "", -1), "/")
name := strings.Replace(relative, "/", "-", -1)
if me.getUnionFs(name) == nil {
me.addFs(name, roots)
}
}
func (me *AutoUnionFs) createFs(name string, roots []string) (*UnionFs, fuse.Status) {
me.lock.Lock()
defer me.lock.Unlock()
for workspace, root := range me.nameRootMap {
if root == roots[0] && workspace != name {
log.Printf("Already have a union FS for directory %s in workspace %s",
roots[0], workspace)
return nil, fuse.EBUSY
}
}
ufs := me.knownFileSystems[name]
if ufs != nil {
log.Println("Already have a workspace:", name)
return nil, fuse.EBUSY
}
ufs, err := NewUnionFsFromRoots(roots, &me.options.UnionFsOptions)
if err != nil {
log.Println("Could not create UnionFs:", err)
return nil, fuse.EPERM
}
log.Printf("Adding workspace %v for roots %v", name, ufs.Name())
me.knownFileSystems[name] = ufs
me.nameRootMap[name] = roots[0]
return ufs, fuse.OK
}
func (me *AutoUnionFs) rmFs(name string) (code fuse.Status) {
me.lock.Lock()
defer me.lock.Unlock()
fs := me.knownFileSystems[name]
if fs == nil {
return fuse.ENOENT
}
code = me.connector.Unmount(name)
if code.Ok() {
me.knownFileSystems[name] = nil, false
me.nameRootMap[name] = "", false
} else {
log.Printf("Unmount failed for %s. Code %v", name, code)
}
return code
}
func (me *AutoUnionFs) addFs(name string, roots []string) (code fuse.Status) {
if name == _CONFIG || name == _STATUS || name == _SCAN_CONFIG {
log.Println("Illegal name for overlay", roots)
return fuse.EINVAL
}
fs, code := me.createFs(name, roots)
if code.Ok() && fs != nil {
code = me.connector.Mount("/"+name, fs, &me.options.FileSystemOptions)
}
return code
}
// TODO - should hide these methods.
func (me *AutoUnionFs) VisitDir(path string, f *os.FileInfo) bool {
roots := me.getRoots(path)
if roots != nil {
me.addAutomaticFs(roots)
}
return true
}
func (me *AutoUnionFs) getRoots(path string) []string {
ro := filepath.Join(path, _READONLY)
fi, err := os.Lstat(ro)
fiDir, errDir := os.Stat(ro)
if err == nil && errDir == nil && fi.IsSymlink() && fiDir.IsDirectory() {
// TODO - should recurse and chain all READONLYs
// together.
return []string{path, ro}
}
return nil
}
func (me *AutoUnionFs) VisitFile(path string, f *os.FileInfo) {
}
func (me *AutoUnionFs) updateKnownFses() {
log.Println("Looking for new filesystems")
filepath.Walk(me.root, me, nil)
log.Println("Done looking")
}
func (me *AutoUnionFs) Readlink(path string) (out string, code fuse.Status) {
comps := strings.Split(path, filepath.SeparatorString, -1)
if comps[0] == _STATUS && comps[1] == _ROOT {
return me.root, fuse.OK
}
if comps[0] != _CONFIG {
return "", fuse.ENOENT
}
name := comps[1]
me.lock.RLock()
defer me.lock.RUnlock()
root, ok := me.nameRootMap[name]
if ok {
return root, fuse.OK
}
return "", fuse.ENOENT
}
func (me *AutoUnionFs) getUnionFs(name string) *UnionFs {
me.lock.RLock()
defer me.lock.RUnlock()
return me.knownFileSystems[name]
}
func (me *AutoUnionFs) Symlink(pointedTo string, linkName string) (code fuse.Status) {
comps := strings.Split(linkName, "/", -1)
if len(comps) != 2 {
return fuse.EPERM
}
if comps[0] == _CONFIG {
roots := me.getRoots(pointedTo)
if roots == nil {
return syscall.ENOTDIR
}
name := comps[1]
return me.addFs(name, roots)
}
return fuse.EPERM
}
func (me *AutoUnionFs) Unlink(path string) (code fuse.Status) {
comps := strings.Split(path, "/", -1)
if len(comps) != 2 {
return fuse.EPERM
}
if comps[0] == _CONFIG && comps[1] != _SCAN_CONFIG {
code = me.rmFs(comps[1])
} else {
code = fuse.ENOENT
}
return code
}
// Must define this, because ENOSYS will suspend all GetXAttr calls.
func (me *AutoUnionFs) GetXAttr(name string, attr string) ([]byte, fuse.Status) {
return nil, syscall.ENODATA
}
func (me *AutoUnionFs) GetAttr(path string) (*os.FileInfo, fuse.Status) {
if path == "" || path == _CONFIG || path == _STATUS {
a := &os.FileInfo{
Mode: fuse.S_IFDIR | 0755,
}
return a, fuse.OK
}
if path == filepath.Join(_STATUS, _VERSION) {
a := &os.FileInfo{
Mode: fuse.S_IFREG | 0644,
Size: int64(len(fuse.Version())),
}
return a, fuse.OK
}
if path == filepath.Join(_STATUS, _ROOT) {
a := &os.FileInfo{
Mode: syscall.S_IFLNK | 0644,
}
return a, fuse.OK
}
if path == filepath.Join(_CONFIG, _SCAN_CONFIG) {
a := &os.FileInfo{
Mode: fuse.S_IFREG | 0644,
}
return a, fuse.OK
}
comps := strings.Split(path, filepath.SeparatorString, -1)
if len(comps) > 1 && comps[0] == _CONFIG {
fs := me.getUnionFs(comps[1])
if fs == nil {
return nil, fuse.ENOENT
}
a := &os.FileInfo{
Mode: syscall.S_IFLNK | 0644,
}
return a, fuse.OK
}
if me.getUnionFs(path) != nil {
return &os.FileInfo{
Mode: fuse.S_IFDIR | 0755,
},fuse.OK
}
return nil, fuse.ENOENT
}
func (me *AutoUnionFs) StatusDir() (stream chan fuse.DirEntry, status fuse.Status) {
stream = make(chan fuse.DirEntry, 10)
stream <- fuse.DirEntry{
Name: _VERSION,
Mode: fuse.S_IFREG | 0644,
}
stream <- fuse.DirEntry{
Name: _ROOT,
Mode: syscall.S_IFLNK | 0644,
}
close(stream)
return stream, fuse.OK
}
func (me *AutoUnionFs) Open(path string, flags uint32) (fuse.File, fuse.Status) {
if path == filepath.Join(_STATUS, _VERSION) {
if flags & fuse.O_ANYWRITE != 0 {
return nil, fuse.EPERM
}
return fuse.NewReadOnlyFile([]byte(fuse.Version())), fuse.OK
}
if path == filepath.Join(_CONFIG, _SCAN_CONFIG) {
if flags & fuse.O_ANYWRITE != 0 {
me.updateKnownFses()
}
return fuse.NewDevNullFile(), fuse.OK
}
return nil, fuse.ENOENT
}
func (me *AutoUnionFs) Truncate(name string, offset uint64) (code fuse.Status) {
if name != filepath.Join(_CONFIG, _SCAN_CONFIG) {
log.Println("Huh? Truncating unsupported write file", name)
return fuse.EPERM
}
return fuse.OK
}
func (me *AutoUnionFs) OpenDir(name string) (stream chan fuse.DirEntry, status fuse.Status) {
switch name {
case _STATUS:
return me.StatusDir()
case _CONFIG:
case "/":
name = ""
case "":
default:
panic(fmt.Sprintf("Don't know how to list dir %v", name))
}
me.lock.RLock()
defer me.lock.RUnlock()
stream = make(chan fuse.DirEntry, len(me.knownFileSystems)+5)
for k, _ := range me.knownFileSystems {
mode := fuse.S_IFDIR | 0755
if name == _CONFIG {
mode = syscall.S_IFLNK | 0644
}
stream <- fuse.DirEntry{
Name: k,
Mode: uint32(mode),
}
}
if name == "" {
stream <- fuse.DirEntry{
Name: _CONFIG,
Mode: uint32(fuse.S_IFDIR | 0755),
}
stream <- fuse.DirEntry{
Name: _STATUS,
Mode: uint32(fuse.S_IFDIR | 0755),
}
}
close(stream)
return stream, status
}