forked from electricbubble/gidevice
-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
crashreportmover.go
167 lines (140 loc) · 3.75 KB
/
crashreportmover.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
package giDevice
import (
"fmt"
"github.com/SonicCloudOrg/sonic-gidevice/pkg/libimobiledevice"
"howett.net/plist"
"io"
"os"
"path"
"path/filepath"
"strings"
)
var _ CrashReportMover = (*crashReportMover)(nil)
func newCrashReportMover(client *libimobiledevice.CrashReportMoverClient) *crashReportMover {
return &crashReportMover{
client: client,
}
}
type crashReportMover struct {
client *libimobiledevice.CrashReportMoverClient
afc Afc
}
func (c *crashReportMover) readPing() (err error) {
var data []byte
if data, err = c.client.InnerConn().Read(4); err != nil {
return err
}
if string(data) != "ping" {
return fmt.Errorf("crashReportMover ping: %v", data)
}
return
}
func (c *crashReportMover) Move(hostDir string, opts ...CrashReportMoverOption) (err error) {
opt := defaultCrashReportMoverOption()
for _, fn := range opts {
fn(opt)
}
toExtract := make([]string, 0, 64)
fn := func(cwd string, info *AfcFileInfo) {
if info.IsDir() {
return
}
if cwd == "." {
cwd = ""
}
devFilename := path.Join(cwd, info.Name())
hostElem := strings.Split(devFilename, "/")
hostFilename := filepath.Join(hostDir, filepath.Join(hostElem...))
hostFilename = strings.TrimSuffix(hostFilename, ".synced")
if opt.extract && strings.HasSuffix(hostFilename, ".plist") {
toExtract = append(toExtract, hostFilename)
}
var afcFile *AfcFile
if afcFile, err = c.afc.Open(devFilename, AfcFileModeRdOnly); err != nil {
debugLog(fmt.Sprintf("crashReportMover open %s: %s", devFilename, err))
return
}
defer func() {
if err = afcFile.Close(); err != nil {
debugLog(fmt.Sprintf("crashReportMover device file close: %s", err))
}
}()
if err = os.MkdirAll(filepath.Dir(hostFilename), 0755); err != nil {
debugLog(fmt.Sprintf("crashReportMover mkdir %s: %s", filepath.Dir(hostFilename), err))
return
}
var hostFile *os.File
if hostFile, err = os.Create(hostFilename); err != nil {
debugLog(fmt.Sprintf("crashReportMover create %s: %s", hostFilename, err))
return
}
defer func() {
if err = hostFile.Close(); err != nil {
debugLog(fmt.Sprintf("crashReportMover host file close: %s", err))
}
}()
if _, err = io.Copy(hostFile, afcFile); err != nil {
debugLog(fmt.Sprintf("crashReportMover copy %s", err))
return
}
opt.whenDone(devFilename)
if opt.keep {
return
}
if err = c.afc.Remove(devFilename); err != nil {
debugLog(fmt.Sprintf("crashReportMover remove %s: %s", devFilename, err))
return
}
}
if err = c.walkDir(".", fn); err != nil {
return err
}
if !opt.extract {
return nil
}
for _, name := range toExtract {
data, err := os.ReadFile(name)
if err != nil {
debugLog(fmt.Sprintf("crashReportMover extract read %s: %s", name, err))
continue
}
m := make(map[string]interface{})
if _, err = plist.Unmarshal(data, &m); err != nil {
debugLog(fmt.Sprintf("crashReportMover extract plist %s: %s", name, err))
continue
}
desc, ok := m["description"]
if !ok {
continue
}
hostExtCrash := strings.TrimSuffix(name, ".plist") + ".crash"
if err = os.WriteFile(hostExtCrash, []byte(fmt.Sprintf("%v", desc)), 0755); err != nil {
debugLog(fmt.Sprintf("crashReportMover extract save %s: %s", name, err))
continue
}
}
return
}
func (c *crashReportMover) walkDir(dirname string, fn func(path string, info *AfcFileInfo)) (err error) {
var names []string
if names, err = c.afc.ReadDir(dirname); err != nil {
return err
}
cwd := dirname
for _, n := range names {
if n == "." || n == ".." {
continue
}
var info *AfcFileInfo
if info, err = c.afc.Stat(path.Join(cwd, n)); err != nil {
return err
}
if info.IsDir() {
if err = c.walkDir(path.Join(cwd, info.name), fn); err != nil {
return err
}
}
fn(cwd, info)
}
return
}