forked from zach-klippenstein/adbfs
/
adb_filesystem.go
635 lines (510 loc) · 17.9 KB
/
adb_filesystem.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
package adbfs
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"
"github.com/hanwen/go-fuse/fuse"
"github.com/hanwen/go-fuse/fuse/nodefs"
"github.com/hanwen/go-fuse/fuse/pathfs"
"github.com/zach-klippenstein/adbfs/internal/cli"
"github.com/zach-klippenstein/goadb"
"github.com/zach-klippenstein/goadb/util"
)
// 64 symlinks ought to be deep enough for anybody.
const MaxLinkResolveDepth = 64
/*
AdbFileSystem is an implementation of fuse.pathfs.FileSystem that exposes the filesystem
on an adb device.
Since all operations go through a single adb server, short-lived connections are throttled by using a
fixed-size pool of device clients. The pool is initially filled by calling Config.ClientFactory.
The pool is not used for long-lived connections such as file transfers, which may be kept open
for arbitrary periods of time by processes using the filesystem.
*/
type AdbFileSystem struct {
config Config
// Client pool for short-lived connections (e.g. listing devices, running commands).
// Clients for long-lived connections like file transfers should be created as needed.
quickUseClientPool chan DeviceClient
openFiles *OpenFiles
}
// Config stores arguments used by AdbFileSystem.
type Config struct {
// Serial number of the device for which ClientFactory returns clients.
DeviceSerial string
// Absolute path to mountpoint, used to resolve symlinks.
Mountpoint string
// Directory on device to consider root.
DeviceRoot string
// Used to initially populate the device client pool, and create clients for open files.
ClientFactory DeviceClientFactory
// Maximum number of concurrent connections for short-lived connections (does not restrict
// the number of concurrently open files).
// Values <1 are treated as 1.
ConnectionPoolSize int
}
type DeviceClientFactory func() DeviceClient
var _ pathfs.FileSystem = &AdbFileSystem{}
func NewAdbFileSystem(config Config) (pathfs.FileSystem, error) {
if config.ConnectionPoolSize < 1 {
config.ConnectionPoolSize = 1
}
cli.Log.Infoln("connection pool size:", config.ConnectionPoolSize)
config.DeviceRoot = strings.TrimSuffix(config.DeviceRoot, "/")
cli.Log.Infoln("device root:", config.DeviceRoot)
clientPool := make(chan DeviceClient, config.ConnectionPoolSize)
clientPool <- config.ClientFactory()
fs := &AdbFileSystem{
config: config,
quickUseClientPool: clientPool,
openFiles: NewOpenFiles(OpenFilesOptions{
DeviceSerial: config.DeviceSerial,
ClientFactory: config.ClientFactory,
}),
}
if err := fs.initialize(); err != nil {
return nil, err
}
return fs, nil
}
func (fs *AdbFileSystem) initialize() error {
logEntry := StartOperation("Initialize", "")
defer logEntry.FinishOperation()
if fs.config.DeviceRoot != "" {
// The mountpoint can't report itself as a symlink (it couldn't have any meaningful target).
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
target, _, err := readLinkRecursively(device, fs.config.DeviceRoot, logEntry)
if err != nil {
logEntry.ErrorMsg(err, "reading link")
return err
}
logEntry.Result("resolved device root %s ➜ %s", fs.config.DeviceRoot, target)
fs.config.DeviceRoot = target
}
return nil
}
func readLinkRecursively(device DeviceClient, path string, logEntry *LogEntry) (string, *goadb.DirEntry, error) {
var result bytes.Buffer
currentDepth := 0
fmt.Fprintf(&result, "attempting to resolve %s if it's a symlink\n", path)
entry, err := device.Stat(path, logEntry)
if err != nil {
return "", nil, err
}
for entry.Mode&os.ModeSymlink == os.ModeSymlink {
if currentDepth > MaxLinkResolveDepth {
return "", nil, ErrLinkTooDeep
}
currentDepth++
fmt.Fprintln(&result, path)
path, err = readLink(device, path)
if err != nil {
return "", nil, util.WrapErrf(err, "reading link: %s", result.String())
}
fmt.Fprintln(&result, " ➜", path)
entry, err = device.Stat(path, logEntry)
if err != nil {
return "", nil, util.WrapErrf(err, "stating %s: %s", path, result.String())
}
}
return path, entry, nil
}
func (fs *AdbFileSystem) String() string {
return fmt.Sprintf("AdbFileSystem@%s", fs.config.Mountpoint)
}
func (fs *AdbFileSystem) StatFs(name string) *fuse.StatfsOut {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("StatFs", name)
defer logEntry.SuppressFinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
name, _, err := readLinkRecursively(device, name, logEntry)
if err != nil {
logEntry.Error(err)
return nil
}
output, err := device.RunCommand("stat", "-f", name)
if err != nil {
logEntry.ErrorMsg(err, "running statfs command")
return nil
}
statfs, err := parseStatfs(output)
if err != nil {
logEntry.ErrorMsg(err, "invalid stat command output:%v\n%s", err, output)
return nil
}
logEntry.Result("%+v", *statfs)
return statfs
}
func parseStatfs(output string) (stat *fuse.StatfsOut, err error) {
if output == "" {
return nil, errors.New("no output")
}
scanner := bufio.NewScanner(strings.NewReader(output))
scanner.Split(bufio.ScanWords)
/*
Sample output:
File: "/sdcard/Pictures"
ID: 0 Namelen: 255 Type: UNKNOWN
Block size: 4096
Blocks: Total: 1269664 Free: 1209578 Available: 1205482
Inodes: Total: 327680 Free: 326438
*/
stat = new(fuse.StatfsOut)
var scope, key, value string
for scanner.Scan() {
if !strings.HasSuffix(key, ":") {
// Keys end with :. If the key doesn't end with : yet, it's a multi-word key.
key += scanner.Text()
continue
} else if strings.HasSuffix(scanner.Text(), ":") {
// Handle the prefix keys (Blocks and Inodes).
scope = strings.TrimSuffix(key, ":")
key = scanner.Text()
continue
} else {
value = scanner.Text()
}
key = strings.TrimSuffix(key, ":")
intVal, err := strconv.Atoi(value)
// Don't return err immediately, we don't always need to parse an int.
switch key {
case "Namelen":
if err == nil {
stat.NameLen = uint32(intVal)
}
case "Blocksize":
if err == nil {
stat.Bsize = uint32(intVal)
}
case "Total":
if err == nil {
switch scope {
case "Blocks":
stat.Blocks = uint64(intVal)
case "Inodes":
stat.Files = uint64(intVal)
}
}
case "Free":
if err == nil {
switch scope {
case "Blocks":
stat.Bfree = uint64(intVal)
case "Inodes":
stat.Ffree = uint64(intVal)
}
}
case "Available":
if err == nil {
switch scope {
case "Blocks":
stat.Bavail = uint64(intVal)
}
}
default:
// Ignore other keys.
err = nil
}
if err != nil {
return nil, fmt.Errorf("invalid value for %s: %v", key, value)
}
key = ""
}
if scanner.Err() != nil {
return nil, err
}
return stat, nil
}
func (fs *AdbFileSystem) GetAttr(name string, _ *fuse.Context) (attr *fuse.Attr, status fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("GetAttr", name)
// This is a very noisy operation on OSX.
defer logEntry.SuppressFinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
attr = new(fuse.Attr)
err := getAttr(name, device, logEntry, attr)
return attr, toFuseStatusLog(err, logEntry)
}
// getAttr performs the actual stat call on a client, converts errors to status, and converts
// the DirEntry to a fuse.Attr. It also sets the LogEntry result.
func getAttr(name string, client DeviceClient, logEntry *LogEntry, attr *fuse.Attr) error {
entry, err := client.Stat(name, logEntry)
if err != nil {
return err
}
asFuseAttr(entry, attr)
logEntry.Result("entry=%v, attr=%v", entry, attr)
return nil
}
func (fs *AdbFileSystem) OpenDir(name string, _ *fuse.Context) ([]fuse.DirEntry, fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("OpenDir", name)
defer logEntry.FinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
entries, err := device.ListDirEntries(name, logEntry)
if err != nil {
return nil, toFuseStatusLog(err, logEntry)
}
result := asFuseDirEntries(entries)
return result, toFuseStatusLog(OK, logEntry)
}
func (fs *AdbFileSystem) Readlink(name string, context *fuse.Context) (target string, status fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Readlink", name)
defer logEntry.FinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
target, err := readLink(device, name)
if err == nil {
// Translate absolute links as relative to this mountpoint.
// Don't use path.Abs since we don't want to have platform-specific behavior.
if strings.HasPrefix(target, "/") {
target = filepath.Join(fs.config.Mountpoint, target)
}
logEntry.Result("%s", target)
}
return target, toFuseStatusLog(err, logEntry)
}
func readLink(client DeviceClient, path string) (string, error) {
// The sync protocol doesn't provide a way to read links.
// Some versions of Android have a readlink command that supports resolving recursively, but
// others (notably Marshmallow) don't, so don't try to do anything fancy (see issue #14).
// OSX Finder won't follow recursive symlinks in tree view, but it should resolve them if you
// open them.
result, err := client.RunCommand("readlink", path)
if err != nil {
return "", err
}
result = strings.TrimSuffix(result, "\r\n")
if result == ReadlinkInvalidArgument {
return "", ErrNotALink
} else if result == ReadlinkPermissionDenied {
return "", ErrNoPermission
}
return result, nil
}
func (fs *AdbFileSystem) Access(name string, mode uint32, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Access", name)
defer logEntry.SuppressFinishOperation()
if mode&fuse.W_OK == fuse.W_OK {
return toFuseStatusLog(ErrNotPermitted, logEntry)
}
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
// Access is required to resolve symlinks.
name, _, err := readLinkRecursively(device, name, logEntry)
if err != nil {
return toFuseStatusLog(err, logEntry)
}
logEntry.Result("target %s exists, read/execute access permitted", name)
return toFuseStatusLog(OK, logEntry)
}
func (fs *AdbFileSystem) Create(name string, rawFlags uint32, perms uint32, context *fuse.Context) (nodefs.File, fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Create", name)
defer logEntry.FinishOperation()
return nil, toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Open(name string, flags uint32, context *fuse.Context) (nodefs.File, fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Open", name)
defer logEntry.FinishOperation()
file, err := fs.createFile(name, FileOpenFlags(flags), logEntry)
if err == nil {
logEntry.Result("%s", file)
}
return file, toFuseStatusLog(err, logEntry)
}
func (fs *AdbFileSystem) createFile(name string, flags FileOpenFlags, logEntry *LogEntry) (nodefs.File, error) {
openFile, err := fs.openFiles.GetOrLoad(name, flags, logEntry)
if err != nil {
return nil, err
}
return NewAdbFile(AdbFileOpenOptions{
FileBuffer: openFile,
Flags: flags,
}), nil
}
// Mkdir creates name on the device with the default permissions.
// perms is ignored.
func (fs *AdbFileSystem) Mkdir(name string, perms uint32, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Mkdir", name)
defer logEntry.FinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
err := mkdir(device, name)
return toFuseStatusLog(err, logEntry)
}
func mkdir(client DeviceClient, path string) error {
result, err := client.RunCommand("mkdir", path)
if err != nil {
return err
}
if result != "" {
// TODO Be smarter about this.
return ErrNoPermission
}
return nil
}
func (fs *AdbFileSystem) Rename(oldName, newName string, context *fuse.Context) fuse.Status {
oldName = fs.convertClientPathToDevicePath(oldName)
newName = fs.convertClientPathToDevicePath(newName)
logEntry := StartOperation("Rename", fmt.Sprintf("%s→%s", oldName, newName))
defer logEntry.FinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
err := rename(device, oldName, newName)
return toFuseStatusLog(err, logEntry)
}
func rename(client DeviceClient, oldName, newName string) error {
result, err := client.RunCommand("mv", oldName, newName)
if err != nil {
return err
}
if result != "" {
// TODO Be smarter about this.
return ErrNoPermission
}
return nil
}
func (fs *AdbFileSystem) Rmdir(name string, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Rename", name)
defer logEntry.FinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
err := rmdir(device, name)
return toFuseStatusLog(err, logEntry)
}
func rmdir(client DeviceClient, name string) error {
result, err := client.RunCommand("rmdir", name)
if err != nil {
return err
}
if result != "" {
// TODO Be smarter about this.
return syscall.EINVAL
}
return nil
}
func (fs *AdbFileSystem) Unlink(name string, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Unlink", name)
defer logEntry.FinishOperation()
device := fs.getQuickUseClient()
defer fs.recycleQuickUseClient(device)
err := unlink(device, name)
return toFuseStatusLog(err, logEntry)
}
func unlink(client DeviceClient, name string) error {
result, err := client.RunCommand("rm", name)
if err != nil {
return err
}
if result != "" {
// TODO Be smarter about this error.
return ErrNoPermission
}
return nil
}
func (fs *AdbFileSystem) Chmod(name string, mode uint32, context *fuse.Context) (code fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Chmod", formatArgsListForLog(name, os.FileMode(mode)))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Chown(name string, uid uint32, gid uint32, context *fuse.Context) (code fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Chown", fmt.Sprintf("%s uid=%d, gid=%d", name, uid, gid))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) GetXAttr(name string, attribute string, context *fuse.Context) (data []byte, code fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("GetXAttr", formatArgsListForLog(name, attribute))
defer logEntry.FinishOperation()
return nil, toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) ListXAttr(name string, context *fuse.Context) (attributes []string, code fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("ListXAttr", formatArgsListForLog(name))
defer logEntry.FinishOperation()
return nil, toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) RemoveXAttr(name string, attr string, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("RemoveXAttr", formatArgsListForLog(name, attr))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) SetXAttr(name string, attr string, data []byte, flags int, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("SetXAttr", formatArgsListForLog(name, attr, data, flags))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Link(oldName string, newName string, context *fuse.Context) fuse.Status {
oldName = fs.convertClientPathToDevicePath(oldName)
newName = fs.convertClientPathToDevicePath(newName)
logEntry := StartOperation("Link", formatArgsListForLog(oldName, newName))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Symlink(oldName string, newName string, context *fuse.Context) fuse.Status {
oldName = fs.convertClientPathToDevicePath(oldName)
newName = fs.convertClientPathToDevicePath(newName)
logEntry := StartOperation("Symlink", formatArgsListForLog(oldName, newName))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Mknod(name string, mode uint32, dev uint32, context *fuse.Context) fuse.Status {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Mknod", formatArgsListForLog(name, mode, dev))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Truncate(name string, size uint64, context *fuse.Context) (code fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Truncate", formatArgsListForLog(name, size))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) Utimens(name string, Atime *time.Time, Mtime *time.Time, context *fuse.Context) (code fuse.Status) {
name = fs.convertClientPathToDevicePath(name)
logEntry := StartOperation("Utimens", formatArgsListForLog(name, Atime, Mtime))
defer logEntry.FinishOperation()
return toFuseStatusLog(syscall.ENOSYS, logEntry)
}
func (fs *AdbFileSystem) OnMount(nodeFs *pathfs.PathNodeFs) {
}
func (fs *AdbFileSystem) OnUnmount() {
}
func (fs *AdbFileSystem) SetDebug(debug bool) {
}
func (fs *AdbFileSystem) getNewClient() (client DeviceClient) {
client = fs.config.ClientFactory()
cli.Log.Debug("created device client:", client)
return
}
func (fs *AdbFileSystem) getQuickUseClient() DeviceClient {
return <-fs.quickUseClientPool
}
func (fs *AdbFileSystem) recycleQuickUseClient(client DeviceClient) {
fs.quickUseClientPool <- client
}
func (fs *AdbFileSystem) convertClientPathToDevicePath(name string) string {
return path.Join("/", fs.config.DeviceRoot, name)
}