forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mount.go
265 lines (231 loc) · 6.15 KB
/
mount.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
package main
import (
"os"
"fmt"
"strings"
"strconv"
"bazil.org/fuse"
"bazil.org/fuse/fs"
. "github.com/claudetech/loggo/default"
"golang.org/x/net/context"
)
// Mount the fuse volume
func Mount(client *Drive, mountpoint string, mountOptions []string, uid, gid uint32, umask os.FileMode) error {
Log.Infof("Mounting path %v", mountpoint)
if _, err := os.Stat(mountpoint); os.IsNotExist(err) {
Log.Debugf("Mountpoint doesn't exist, creating...")
if err := os.MkdirAll(mountpoint, 0644); nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not create mount directory %v", mountpoint)
}
}
fuse.Debug = func(msg interface{}) {
Log.Tracef("FUSE %v", msg)
}
// Set mount options
options := []fuse.MountOption{
fuse.NoAppleDouble(),
fuse.NoAppleXattr(),
}
for _, option := range mountOptions {
if "allow_other" == option {
options = append(options, fuse.AllowOther())
} else if "allow_root" == option {
options = append(options, fuse.AllowRoot())
} else if "allow_dev" == option {
options = append(options, fuse.AllowDev())
} else if "allow_non_empty_mount" == option {
options = append(options, fuse.AllowNonEmptyMount())
} else if "allow_suid" == option {
options = append(options, fuse.AllowSUID())
} else if strings.Contains(option, "max_readahead=") {
data := strings.Split(option, "=")
value, err := strconv.ParseUint(data[1], 10, 32)
if nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Could not parse max_readahead value")
}
options = append(options, fuse.MaxReadahead(uint32(value)))
} else if "default_permissions" == option {
options = append(options, fuse.DefaultPermissions())
} else if "excl_create" == option {
options = append(options, fuse.ExclCreate())
} else if strings.Contains(option, "fs_name") {
data := strings.Split(option, "=")
options = append(options, fuse.FSName(data[1]))
} else if "local_volume" == option {
options = append(options, fuse.LocalVolume())
} else if "writeback_cache" == option {
options = append(options, fuse.WritebackCache())
} else if strings.Contains(option, "volume_name") {
data := strings.Split(option, "=")
options = append(options, fuse.VolumeName(data[1]))
} else if "read_only" == option {
options = append(options, fuse.ReadOnly())
} else {
Log.Warningf("Fuse option %v is not supported, yet", option)
}
}
c, err := fuse.Mount(mountpoint, options...)
if err != nil {
return err
}
defer c.Close()
filesys := &FS{
client: client,
uid: uid,
gid: gid,
umask: umask,
}
if err := fs.Serve(c, filesys); err != nil {
return err
}
// check if the mount process has an error to report
<-c.Ready
if err := c.MountError; nil != err {
Log.Debugf("%v", err)
return fmt.Errorf("Error mounting FUSE")
}
return Unmount(mountpoint, true)
}
// Unmount unmounts the mountpoint
func Unmount(mountpoint string, notify bool) error {
if notify {
Log.Infof("Unmounting path %v", mountpoint)
}
fuse.Unmount(mountpoint)
return nil
}
// FS the fuse filesystem
type FS struct {
client *Drive
uid uint32
gid uint32
umask os.FileMode
}
// Root returns the root path
func (f *FS) Root() (fs.Node, error) {
object, err := f.client.GetRoot()
if nil != err {
Log.Warningf("%v", err)
return nil, fmt.Errorf("Could not get root object")
}
return &Object{
client: f.client,
object: object,
uid: f.uid,
gid: f.gid,
umask: f.umask,
}, nil
}
// Object represents one drive object
type Object struct {
client *Drive
object *APIObject
buffer *Buffer
uid uint32
gid uint32
umask os.FileMode
}
// Attr returns the attributes for a directory
func (o *Object) Attr(ctx context.Context, attr *fuse.Attr) error {
if o.object.IsDir {
if o.umask > 0 {
attr.Mode = os.ModeDir | o.umask
} else {
attr.Mode = os.ModeDir | 0755
}
attr.Size = 0
} else {
if o.umask > 0 {
attr.Mode = o.umask
} else {
attr.Mode = 0644
}
attr.Size = o.object.Size
}
attr.Uid = uint32(o.uid)
attr.Gid = uint32(o.gid)
attr.Mtime = o.object.LastModified
attr.Crtime = o.object.LastModified
attr.Ctime = o.object.LastModified
return nil
}
// ReadDirAll shows all files in the current directory
func (o *Object) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
objects, err := o.client.GetObjectsByParent(o.object.ObjectID)
if nil != err {
Log.Debugf("%v", err)
return nil, fuse.ENOENT
}
dirs := []fuse.Dirent{}
for _, object := range objects {
dirs = append(dirs, fuse.Dirent{
Name: object.Name,
Type: fuse.DT_File,
})
}
return dirs, nil
}
// Lookup tests if a file is existent in the current directory
func (o *Object) Lookup(ctx context.Context, name string) (fs.Node, error) {
object, err := o.client.GetObjectByParentAndName(o.object.ObjectID, name)
if nil != err {
Log.Debugf("%v", err)
return nil, fuse.ENOENT
}
return &Object{
client: o.client,
object: object,
uid: o.uid,
gid: o.gid,
umask: o.umask,
}, nil
}
// Open opens a file for reading
func (o *Object) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
if req.Dir {
return o, nil
}
buffer, err := o.client.Open(o.object)
if nil != err {
Log.Warningf("%v", err)
return o, fuse.ENOENT
}
o.buffer = buffer
return o, nil
}
// Release a stream
func (o *Object) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
if nil != o.buffer {
if err := o.buffer.Close(); nil != err {
Log.Debugf("%v", err)
Log.Warningf("Could not close buffer stream")
}
}
return nil
}
// Read reads some bytes or the whole file
func (o *Object) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
buf, err := o.buffer.ReadBytes(req.Offset, int64(req.Size), false, 0)
if nil != err {
Log.Warningf("%v", err)
return fuse.EIO
}
resp.Data = buf[:]
return nil
}
// Remove deletes an element
func (o *Object) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
obj, err := o.client.GetObjectByParentAndName(o.object.ObjectID, req.Name)
if nil != err {
Log.Warningf("%v", err)
return fuse.EIO
}
err = o.client.Remove(obj)
if nil != err {
Log.Warningf("%v", err)
return fuse.EIO
}
return nil
}