forked from hanwen/go-mtpfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classic.go
366 lines (316 loc) · 7.48 KB
/
classic.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
package fs
import (
"fmt"
"io/ioutil"
"log"
"os"
"syscall"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-mtpfs/mtp"
)
type classicNode struct {
mtpNodeImpl
// local file containing the contents.
backing string
// If set, the backing file was changed.
dirty bool
// If set, there was some error writing to the backing store;
// don't flush file to device.
error fuse.Status
}
func (n *classicNode) send() error {
if !n.dirty {
return nil
}
if n.backing == "" {
log.Panicf("sending file without backing store: %q", n.obj.Filename)
}
f := n.obj
if !n.error.Ok() {
n.dirty = false
os.Remove(n.backing)
n.backing = ""
n.error = fuse.OK
n.obj.CompressedSize = 0
n.Size = 0
log.Printf("not sending file %q due to write errors", f.Filename)
return syscall.EIO // TODO - send back n.error
}
fi, err := os.Stat(n.backing)
if err != nil {
log.Printf("could not do stat for send: %v", err)
return err
}
if fi.Size() == 0 {
log.Printf("cannot send 0 byte file %q", f.Filename)
return syscall.EINVAL
}
if n.obj.Filename == "" {
return nil
}
if n.fs.mungeVfat[n.StorageID()] {
f.Filename = SanitizeDosName(f.Filename)
}
backing, err := os.Open(n.backing)
if err != nil {
return err
}
defer backing.Close()
log.Printf("sending file %q to device: %d bytes.", f.Filename, fi.Size())
if n.Handle() != 0 {
// Apparently, you can't overwrite things in MTP.
err := n.fs.dev.DeleteObject(n.Handle())
if err != nil {
return err
}
n.handle = 0
}
if fi.Size() > 0xFFFFFFFF {
f.CompressedSize = 0xFFFFFFFF
} else {
f.CompressedSize = uint32(fi.Size())
}
n.Size = fi.Size()
start := time.Now()
_, _, handle, err := n.fs.dev.SendObjectInfo(n.StorageID(), f.ParentObject, f)
if err != nil {
log.Printf("SendObjectInfo failed %v", err)
return syscall.EINVAL
}
err = n.fs.dev.SendObject(backing, fi.Size())
if err != nil {
log.Printf("SendObject failed %v", err)
return syscall.EINVAL
}
dt := time.Now().Sub(start)
log.Printf("sent %d bytes in %d ms. %.1f MB/s", fi.Size(),
dt.Nanoseconds()/1e6, 1e3*float64(fi.Size())/float64(dt.Nanoseconds()))
n.dirty = false
n.handle = handle
// We could leave the file for future reading, but the
// management of free space is a hassle when doing large
// copies.
if len(n.Inode().Files(0)) == 1 {
os.Remove(n.backing)
n.backing = ""
}
return err
}
// Drop backing data if unused. Returns freed up space.
func (n *classicNode) trim() int64 {
if n.dirty || n.backing == "" || n.Inode().AnyFile() != nil {
return 0
}
fi, err := os.Stat(n.backing)
if err != nil {
return 0
}
log.Printf("removing local cache for %q, %d bytes", n.obj.Filename, fi.Size())
err = os.Remove(n.backing)
if err != nil {
return 0
}
n.backing = ""
return fi.Size()
}
// PTP supports partial fetch (not exposed in libmtp), but we might as
// well get the whole thing.
func (n *classicNode) fetch() error {
if n.backing != "" {
return nil
}
sz := n.Size
if err := n.fs.ensureFreeSpace(sz); err != nil {
return err
}
f, err := ioutil.TempFile(n.fs.options.Dir, "")
if err != nil {
return err
}
defer f.Close()
start := time.Now()
err = n.fs.dev.GetObject(n.Handle(), f)
dt := time.Now().Sub(start)
if err == nil {
n.backing = f.Name()
n.dirty = false
log.Printf("fetched %q, %d bytes in %d ms. %.1f MB/s", n.obj.Filename, sz,
dt.Nanoseconds()/1e6, 1e3*float64(sz)/float64(dt.Nanoseconds()))
} else {
log.Printf("error fetching: %v", err)
err = syscall.EIO
}
return err
}
func (n *classicNode) Open(flags uint32, context *fuse.Context) (file nodefs.File, code fuse.Status) {
return &pendingFile{
File: nodefs.NewDefaultFile(),
node: n,
}, fuse.OK
}
func (n *classicNode) Truncate(file nodefs.File, size uint64, context *fuse.Context) (code fuse.Status) {
if file != nil {
return file.Truncate(size)
} else if n.backing != "" {
return fuse.ToStatus(os.Truncate(n.backing, int64(size)))
}
return fuse.OK
}
////////////////
// writing files.
type pendingFile struct {
nodefs.File
flags uint32
loopback nodefs.File
node *classicNode
}
func (p *pendingFile) rwLoopback() (nodefs.File, fuse.Status) {
if p.loopback == nil {
err := p.node.fetch()
if err != nil {
return nil, fuse.ToStatus(err)
}
f, err := os.OpenFile(p.node.backing, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return nil, fuse.ToStatus(err)
}
p.loopback = nodefs.NewLoopbackFile(f)
}
return p.loopback, fuse.OK
}
func (p *pendingFile) Read(data []byte, off int64) (fuse.ReadResult, fuse.Status) {
if p.loopback == nil {
if err := p.node.fetch(); err != nil {
log.Printf("fetch failed: %v", err)
return nil, fuse.EIO
}
f, err := os.OpenFile(p.node.backing, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return nil, fuse.ToStatus(err)
}
p.loopback = nodefs.NewLoopbackFile(f)
}
return p.loopback.Read(data, off)
}
func (p *pendingFile) Write(data []byte, off int64) (uint32, fuse.Status) {
p.node.dirty = true
f, code := p.rwLoopback()
if !code.Ok() {
return 0, code
}
n, code := f.Write(data, off)
if !code.Ok() {
p.node.error = code
}
return n, code
}
func (p *pendingFile) Truncate(size uint64) fuse.Status {
f, code := p.rwLoopback()
if !code.Ok() {
return code
}
code = f.Truncate(size)
if !code.Ok() {
return code
}
p.node.dirty = true
if code.Ok() && size == 0 {
p.node.error = fuse.OK
}
return code
}
func (p *pendingFile) Flush() fuse.Status {
if p.loopback == nil {
return fuse.OK
}
code := p.loopback.Flush()
if !code.Ok() {
return code
}
s := fuse.ToStatus(p.node.send())
if s == fuse.ENOSYS {
return fuse.EIO
}
return s
}
func (p *pendingFile) Release() {
if p.loopback != nil {
p.loopback.Release()
}
}
////////////////////////////////////////////////////////////////
func (fs *deviceFS) trimUnused(todo int64, node *nodefs.Inode) (done int64) {
for _, ch := range node.Children() {
if done > todo {
break
}
if fn, ok := ch.Node().(*classicNode); ok {
done += fn.trim()
} else if ch.IsDir() {
done += fs.trimUnused(todo-done, ch)
}
}
return
}
func (fs *deviceFS) freeBacking() (int64, error) {
t := syscall.Statfs_t{}
err := syscall.Statfs(fs.options.Dir, &t)
if err != nil {
return 0, err
}
return int64(t.Bfree * uint64(t.Bsize)), nil
}
func (fs *deviceFS) ensureFreeSpace(want int64) error {
free, err := fs.freeBacking()
if err != nil {
return err
}
if free > want {
return nil
}
todo := want - free + 10*1024
fs.trimUnused(todo, fs.root.Inode())
free, err = fs.freeBacking()
if err != nil {
return err
}
if free > want {
return nil
}
return fmt.Errorf("not enough space in %s. Have %d, want %d", fs.options.Dir, free, want)
}
func (fs *deviceFS) setupClassic() error {
if fs.options.Dir == "" {
var err error
fs.options.Dir, err = ioutil.TempDir(os.TempDir(), "go-mtpfs")
if err != nil {
return err
}
fs.delBackingDir = true
}
if fi, err := os.Lstat(fs.options.Dir); err != nil || !fi.IsDir() {
return fmt.Errorf("%s is not a directory")
}
return nil
}
func (fs *deviceFS) createClassicFile(obj mtp.ObjectInfo) (file nodefs.File, node nodefs.Node, err error) {
backingFile, err := ioutil.TempFile(fs.options.Dir, "")
cl := &classicNode{
mtpNodeImpl: mtpNodeImpl{
Node: nodefs.NewDefaultNode(),
obj: &obj,
fs: fs,
},
dirty: true,
backing: backingFile.Name(),
}
file = &pendingFile{
loopback: nodefs.NewLoopbackFile(backingFile),
node: cl,
File: nodefs.NewDefaultFile(),
}
node = cl
return
}