/
fs.go
534 lines (451 loc) · 11.6 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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
// Copyright 2012 Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package fs
import (
"bytes"
"fmt"
"log"
"strings"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-mtpfs/mtp"
)
type DeviceFsOptions struct {
// Assume removable volumes are VFAT and munge filenames
// accordingly.
RemovableVFat bool
// Backing directory.
Dir string
// Use android extensions if available.
Android bool
}
// DeviceFS implements a fuse.NodeFileSystem that mounts multiple
// storages.
type DeviceFs struct {
fuse.DefaultNodeFileSystem
backingDir string
delBackingDir bool
root *rootNode
dev *mtp.Device
devInfo mtp.DeviceInfo
storages []uint32
mungeVfat map[uint32]bool
options *DeviceFsOptions
}
// DeviceFs is a simple filesystem interface to an MTP device. It
// should be wrapped in a Locking(Raw)FileSystem to make sure it is
// threadsafe. The file system assumes the device does not touch the
// storage. Arguments are the opened mtp device and a directory for the
// backing store.
func NewDeviceFs(d *mtp.Device, storages []uint32, options DeviceFsOptions) (*DeviceFs, error) {
root := rootNode{}
fs := &DeviceFs{root: &root, dev: d, options: &options}
root.fs = fs
fs.storages = storages
err := d.GetDeviceInfo(&fs.devInfo)
if err != nil {
return fs, nil
}
if !strings.Contains(fs.devInfo.MTPExtension, "android.com") {
fs.options.Android = false
}
if !options.Android {
err = fs.setupClassic()
if err != nil {
return nil, err
}
}
fs.mungeVfat = make(map[uint32]bool)
for _, sid := range fs.storages {
var info mtp.StorageInfo
if err != nil {
return nil, err
}
fs.mungeVfat[sid] = info.IsRemovable() && fs.options.RemovableVFat
}
return fs, nil
}
func (fs *DeviceFs) Root() fuse.FsNode {
return fs.root
}
func (fs *DeviceFs) String() string {
return fmt.Sprintf("DeviceFs(%s)", fs.devInfo.Model)
}
// TODO - this should be per storage and return just the free space in
// the storage.
func (fs *DeviceFs) statFs() *fuse.StatfsOut {
total := uint64(0)
free := uint64(0)
for _, sid := range fs.storages {
var info mtp.StorageInfo
if err := fs.dev.GetStorageInfo(sid, &info); err != nil {
log.Printf("GetStorageInfo %x: %v", sid, err)
continue
}
total += uint64(info.MaxCapability)
free += uint64(info.FreeSpaceInBytes)
}
bs := uint64(1024)
return &fuse.StatfsOut{
Bsize: uint32(bs),
Blocks: total / bs,
Bavail: free / bs,
Bfree: free / bs,
}
}
func (fs *DeviceFs) newFile(obj mtp.ObjectInfo, size int64, id uint32) (node fuse.FsNode) {
if obj.CompressedSize != 0xFFFFFFFF {
size = int64(obj.CompressedSize)
}
mNode := mtpNodeImpl{
obj: &obj,
handle: id,
fs: fs,
Size: size,
}
if fs.options.Android {
node = &androidNode{
mtpNodeImpl: mNode,
}
} else {
node = &classicNode{
mtpNodeImpl: mNode,
}
}
return node
}
type rootNode struct {
fuse.DefaultFsNode
fs *DeviceFs
}
func (n *rootNode) StatFs() *fuse.StatfsOut {
return n.fs.statFs()
}
const NOPARENT_ID = 0xFFFFFFFF
func (fs *DeviceFs) OnMount(conn *fuse.FileSystemConnector) {
for _, sid := range fs.storages {
var info mtp.StorageInfo
if err := fs.dev.GetStorageInfo(sid, &info); err != nil {
log.Printf("GetStorageInfo %x: %v", sid, err)
continue
}
obj := mtp.ObjectInfo{
ParentObject: NOPARENT_ID,
StorageID: sid,
Filename: info.StorageDescription,
}
folder := fs.newFolder(obj, NOPARENT_ID)
inode := fs.root.Inode().New(true, folder)
fs.root.Inode().AddChild(info.StorageDescription, inode)
}
}
const forbidden = ":*?\"<>|"
func SanitizeDosName(name string) string {
if strings.IndexAny(name, forbidden) == -1 {
return name
}
dest := make([]byte, len(name))
for i := 0; i < len(name); i++ {
if strings.Contains(forbidden, string(name[i])) {
dest[i] = '_'
} else {
dest[i] = name[i]
}
}
return string(dest)
}
////////////////
// mtpNode
type mtpNode interface {
fuse.FsNode
Handle() uint32
StorageID() uint32
SetName(string)
}
type mtpNodeImpl struct {
fuse.DefaultFsNode
// MTP handle.
handle uint32
obj *mtp.ObjectInfo
fs *DeviceFs
// This is needed because obj.CompressedSize only goes to
// 0xFFFFFFFF
Size int64
}
func (n *mtpNodeImpl) GetAttr(out *fuse.Attr, file fuse.File, context *fuse.Context) (code fuse.Status) {
out.Mode = fuse.S_IFREG | 0644
f := n.obj
if f != nil {
out.Size = uint64(n.Size)
t := f.ModificationDate
out.SetTimes(&t, &t, &t)
}
return fuse.OK
}
func (n *mtpNodeImpl) Chown(file fuse.File, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status) {
// Get rid of pesky messages from cp -a.
return fuse.OK
}
func (n *mtpNodeImpl) Chmod(file fuse.File, perms uint32, context *fuse.Context) (code fuse.Status) {
// Get rid of pesky messages from cp -a.
return fuse.OK
}
func (n *mtpNodeImpl) Utimens(file fuse.File, aTime *time.Time, mTime *time.Time, context *fuse.Context) (code fuse.Status) {
// Unfortunately, we can't set the modtime; it's READONLY in
// the Android MTP implementation. We just change the time in
// the mount, but this is not persisted.
if mTime != nil {
n.obj.ModificationDate = *mTime
}
return fuse.OK
}
func (n *mtpNodeImpl) Handle() uint32 {
return n.handle
}
func (n *mtpNodeImpl) SetName(nm string) {
n.obj.Filename = nm
}
func (n *mtpNodeImpl) StatFs() *fuse.StatfsOut {
return n.fs.statFs()
}
func (n *mtpNodeImpl) StorageID() uint32 {
return n.obj.StorageID
}
var _ = mtpNode((*folderNode)(nil))
////////////////
// files
//////////////////
// folders
type folderNode struct {
mtpNodeImpl
fetched bool
}
func (fs *DeviceFs) newFolder(obj mtp.ObjectInfo, h uint32) *folderNode {
obj.AssociationType = mtp.OFC_Association
return &folderNode{
mtpNodeImpl: mtpNodeImpl{
handle: h,
obj: &obj,
fs: fs,
},
}
}
// Keep the root nodes for all device storages alive.
func (n *folderNode) Deletable() bool {
return n.Handle() != NOPARENT_ID
}
// Fetches data from device returns false on failure.
func (n *folderNode) fetch() bool {
if n.fetched {
return true
}
handles := mtp.Uint32Array{}
err := n.fs.dev.GetObjectHandles(n.StorageID(), 0x0,
n.Handle(), &handles)
if err != nil {
log.Printf("GetObjectHandles failed: %v", err)
return false
}
infos := []*mtp.ObjectInfo{}
sizes := map[uint32]int64{}
for _, handle := range handles.Values {
obj := mtp.ObjectInfo{}
err := n.fs.dev.GetObjectInfo(handle, &obj)
if err != nil {
log.Printf("GetObjectInfo failed: %v", err)
return false
}
if obj.Filename == "" {
log.Printf("ignoring handle 0x%x with empty name in dir 0x%x",
handle, n.Handle())
infos = append(infos, nil)
continue
}
if obj.CompressedSize == 0xFFFFFFFF {
var val mtp.Uint64Value
err := n.fs.dev.GetObjectPropValue(handle, mtp.OPC_ObjectSize, &val)
if err != nil {
log.Printf("GetObjectPropValue handle %d failed: %v", handle, err)
return false
}
sizes[handle] = int64(val.Value)
}
infos = append(infos, &obj)
}
for i, handle := range handles.Values {
var node fuse.FsNode
if infos[i] == nil {
continue
}
obj := *infos[i]
obj.ParentObject = n.Handle()
isdir := obj.ObjectFormat == mtp.OFC_Association
if isdir {
fNode := n.fs.newFolder(obj, handle)
node = fNode
} else {
sz := sizes[handle]
node = n.fs.newFile(obj, sz, handle)
}
n.Inode().AddChild(obj.Filename, n.Inode().New(isdir, node))
}
n.fetched = true
return true
}
func (n *folderNode) OpenDir(context *fuse.Context) (stream []fuse.DirEntry, status fuse.Status) {
if !n.fetch() {
return nil, fuse.EIO
}
return n.DefaultFsNode.OpenDir(context)
}
func (n *folderNode) GetAttr(out *fuse.Attr, file fuse.File, context *fuse.Context) (code fuse.Status) {
out.Mode = fuse.S_IFDIR | 0755
return fuse.OK
}
func (n *folderNode) basenameRename(oldName string, newName string) error {
ch := n.Inode().GetChild(oldName)
mFile := ch.FsNode().(mtpNode)
if mFile.Handle() != 0 {
// Only rename on device if it was sent already.
v := mtp.StringValue{newName}
err := n.fs.dev.SetObjectPropValue(mFile.Handle(), mtp.OPC_ObjectFileName, &v)
if err != nil {
return err
}
}
n.Inode().RmChild(oldName)
n.Inode().AddChild(newName, ch)
return nil
}
func (n *folderNode) Rename(oldName string, newParent fuse.FsNode, newName string, context *fuse.Context) (code fuse.Status) {
fn, ok := newParent.(*folderNode)
if !ok {
return fuse.ENOSYS
}
fn.fetch()
n.fetch()
if f := n.Inode().GetChild(newName); f != nil {
if fn != n {
// TODO - delete destination?
log.Printf("old folder already has child %q", newName)
return fuse.ENOSYS
} else {
// does mtp overwrite the destination?
}
}
if fn != n {
return fuse.ENOSYS
}
if newName != oldName {
err := n.basenameRename(oldName, newName)
if err != nil {
log.Printf("basenameRename failed: %v", err)
return fuse.EIO
}
}
return fuse.OK
}
func (n *folderNode) Lookup(out *fuse.Attr, name string, context *fuse.Context) (node fuse.FsNode, code fuse.Status) {
if !n.fetch() {
return nil, fuse.EIO
}
ch := n.Inode().GetChild(name)
if ch == nil {
return nil, fuse.ENOENT
}
s := ch.FsNode().GetAttr(out, nil, context)
return ch.FsNode(), s
}
func (n *folderNode) Mkdir(name string, mode uint32, context *fuse.Context) (fuse.FsNode, fuse.Status) {
if !n.fetch() {
return nil, fuse.EIO
}
obj := mtp.ObjectInfo{
Filename: name,
ObjectFormat: mtp.OFC_Association,
ModificationDate: time.Now(),
ParentObject: n.Handle(),
StorageID: n.StorageID(),
}
_, _, newId, err := n.fs.dev.SendObjectInfo(n.StorageID(), n.Handle(), &obj)
if err != nil {
log.Printf("CreateFolder failed: %v", err)
return nil, fuse.EIO
}
f := n.fs.newFolder(obj, newId)
n.Inode().AddChild(name, n.Inode().New(true, f))
return f, fuse.OK
}
func (n *folderNode) Unlink(name string, c *fuse.Context) fuse.Status {
if !n.fetch() {
return fuse.EIO
}
ch := n.Inode().GetChild(name)
if ch == nil {
return fuse.ENOENT
}
f := ch.FsNode().(mtpNode)
if f.Handle() != 0 {
err := n.fs.dev.DeleteObject(f.Handle())
if err != nil {
log.Printf("DeleteObject failed: %v", err)
return fuse.EIO
}
} else {
f.SetName("")
}
n.Inode().RmChild(name)
return fuse.OK
}
func (n *folderNode) Rmdir(name string, c *fuse.Context) fuse.Status {
return n.Unlink(name, c)
}
func (n *folderNode) Create(name string, flags uint32, mode uint32, context *fuse.Context) (file fuse.File, node fuse.FsNode, code fuse.Status) {
if !n.fetch() {
return nil, nil, fuse.EIO
}
obj := mtp.ObjectInfo{
StorageID: n.StorageID(),
Filename: name,
ObjectFormat: mtp.OFC_Undefined,
ModificationDate: time.Now(),
ParentObject: n.Handle(),
CompressedSize: 0,
}
if n.fs.options.Android {
_, _, handle, err := n.fs.dev.SendObjectInfo(n.StorageID(), n.Handle(), &obj)
if err != nil {
log.Println("SendObjectInfo failed", err)
return nil, nil, fuse.EIO
}
err = n.fs.dev.SendObject(&bytes.Buffer{}, 0)
if err != nil {
log.Println("SendObject failed:", err)
return nil, nil, fuse.EIO
}
if err := n.fs.dev.AndroidBeginEditObject(handle); err != nil {
log.Println("AndroidBeginEditObject failed:", err)
return nil, nil, fuse.EIO
}
node = &androidNode{
mtpNodeImpl: mtpNodeImpl{
obj: &obj,
fs: n.fs,
handle: handle,
},
write: true,
}
file = &androidFile{
node: node.(*androidNode),
}
} else {
var err error
file, node, err = n.fs.createClassicFile(obj)
if err != nil {
return nil, nil, fuse.ToStatus(err)
}
}
n.Inode().AddChild(name, n.Inode().New(false, node))
return file, node, fuse.OK
}