forked from zach-klippenstein/goadb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_entries.go
99 lines (83 loc) · 2.04 KB
/
dir_entries.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
package adb
import (
"encoding/binary"
"io"
"os"
"path"
"time"
"github.com/pkg/errors"
)
// DirEntry holds information about a directory entry on a device.
type DirEntry struct {
FName string
FMode os.FileMode
FSize uint32
ModifiedAt time.Time
}
var _ os.FileInfo = DirEntry{}
func (de DirEntry) IsDir() bool {
return de.FMode.IsDir()
}
func (de DirEntry) ModTime() time.Time {
return de.ModifiedAt
}
func (de DirEntry) Mode() os.FileMode {
return de.FMode
}
func (de DirEntry) Name() string {
return path.Base(de.FName)
}
func (de DirEntry) Size() int64 {
return int64(de.FSize)
}
func (de DirEntry) Sys() interface{} {
return nil
}
// ReadAllDirEntries reads directory entries into a slice,
// closes self, and returns any error.
// If err is non-nil, result will contain any entries read until the error occurred.
func ReadAllDirEntries(r io.Reader) ([]DirEntry, error) {
result := make([]DirEntry, 0, 4)
de, err := readNextDirListEntry(r)
for err == nil {
result = append(result, de)
de, err = readNextDirListEntry(r)
}
if err != done {
return result, err
}
return result, nil
}
// done signals successful completion
var done = errors.New("DONE")
func readNextDirListEntry(r io.Reader) (DirEntry, error) {
header := make([]byte, 4*5)
n, err := io.ReadFull(r, header)
if err == io.ErrUnexpectedEOF {
if n == 4 && string(header[:4]) == statusSyncDone {
return DirEntry{}, done
} else {
return DirEntry{}, errors.New("unexpected status")
}
}
if string(header[:4]) != statusSyncDent {
return DirEntry{}, errors.New("unexpected status")
}
var (
mode = os.FileMode(binary.LittleEndian.Uint32(header[4:8]))
size = binary.LittleEndian.Uint32(header[8:12])
time = time.Unix(int64(binary.LittleEndian.Uint32(header[12:16])), 0)
length = binary.LittleEndian.Uint32(header[16:20])
)
body := make([]byte, length)
_, err = io.ReadFull(r, body)
if err != nil {
return DirEntry{}, nil
}
return DirEntry{
FName: string(body),
FMode: mode,
FSize: size,
ModifiedAt: time,
}, nil
}