forked from airnandez/cluefs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.go
95 lines (82 loc) · 1.88 KB
/
fs.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
package main
import (
"fmt"
"os"
"path/filepath"
"bazil.org/fuse"
fusefs "bazil.org/fuse/fs"
"golang.org/x/net/context"
)
type ClueFS struct {
shadowDir string
mountDir string
root *Dir
}
var trace func(op FsOperTracer)
func FuseDebug(msg interface{}) {
Debug(4, "*** FUSE: %s", msg)
}
func NewClueFS(shadowDir string, tracer Tracer) (*ClueFS, error) {
if !filepath.IsAbs(shadowDir) {
return nil, fmt.Errorf("'%s' is not an absolute path", shadowDir)
}
dir, err := os.Open(shadowDir)
if err != nil {
return nil, err
}
defer dir.Close()
// Initialize the trace function this file system will use to
// emit file I/O events
trace = func(op FsOperTracer) {
op.SetTimeEnd()
tracer.Trace(op)
}
return &ClueFS{shadowDir: shadowDir}, nil
}
func (fs *ClueFS) MountAndServe(mountpoint string, readonly bool) error {
// Mount the file system
fs.mountDir = mountpoint
if IsDebugActive() {
fuse.Debug = FuseDebug
}
mountOpts := []fuse.MountOption{
fuse.FSName(programName),
fuse.Subtype(programName),
fuse.VolumeName(programName),
fuse.LocalVolume(),
}
if readonly {
mountOpts = append(mountOpts, fuse.ReadOnly())
}
conn, err := fuse.Mount(mountpoint, mountOpts...)
if err != nil {
return err
}
defer conn.Close()
// Start serving requests
if err = fusefs.Serve(conn, fs); err != nil {
return err
}
// Check for errors when mounting the file system
<-conn.Ready
if err = conn.MountError; err != nil {
return err
}
return nil
}
func (fs *ClueFS) Root() (fusefs.Node, error) {
if fs.root == nil {
fs.root = NewDir("", fs.shadowDir, fs)
}
return fs.root, nil
}
func (fs *ClueFS) Statfs(ctx context.Context, req *fuse.StatfsRequest, resp *fuse.StatfsResponse) error {
defer trace(NewStatFsOp(req, fs.mountDir))
return statfsToFuse(fs.shadowDir, resp)
}
func (fs *ClueFS) Destroy() {
if fs.root != nil {
fs.root.doClose()
fs.root = nil
}
}