-
Notifications
You must be signed in to change notification settings - Fork 69
/
afc.go
551 lines (463 loc) · 14.7 KB
/
afc.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
package giDevice
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"github.com/electricbubble/gidevice/pkg/libimobiledevice"
"io"
"path"
"strconv"
"time"
)
var ErrAfcStatNotExist = errors.New("afc stat: no such file or directory")
var _ Afc = (*afc)(nil)
func newAfc(client *libimobiledevice.AfcClient) *afc {
return &afc{client: client}
}
type afc struct {
client *libimobiledevice.AfcClient
}
func (c *afc) DiskInfo() (info *AfcDiskInfo, err error) {
if err = c.client.Send(libimobiledevice.AfcOperationGetDeviceInfo, nil, nil); err != nil {
return nil, fmt.Errorf("afc send 'DiskInfo': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return nil, fmt.Errorf("afc receive 'DiskInfo': %w", err)
}
m := respMsg.Map()
info = &AfcDiskInfo{
Model: m["Model"],
}
if info.TotalBytes, err = strconv.ParseUint(m["FSTotalBytes"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'DiskInfo': %w", err)
}
if info.FreeBytes, err = strconv.ParseUint(m["FSFreeBytes"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'DiskInfo': %w", err)
}
if info.BlockSize, err = strconv.ParseUint(m["FSBlockSize"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'DiskInfo': %w", err)
}
return
}
func (c *afc) ReadDir(dirname string) (names []string, err error) {
if err = c.client.Send(libimobiledevice.AfcOperationReadDir, toCString(dirname), nil); err != nil {
return nil, fmt.Errorf("afc send 'ReadDir': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return nil, fmt.Errorf("afc receive 'ReadDir': %w", err)
}
if err = respMsg.Err(); err != nil {
return nil, fmt.Errorf("afc 'ReadDir': %w", err)
}
names = respMsg.Strings()
return
}
func (c *afc) Stat(filename string) (info *AfcFileInfo, err error) {
if err = c.client.Send(libimobiledevice.AfcOperationGetFileInfo, toCString(filename), nil); err != nil {
return nil, fmt.Errorf("afc send 'Stat': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return nil, fmt.Errorf("afc receive 'Stat': %w", err)
}
m := respMsg.Map()
if len(m) == 0 {
return nil, ErrAfcStatNotExist
}
info = &AfcFileInfo{
source: m,
name: path.Base(filename),
ifmt: m["st_ifmt"],
}
if info.creationTime, err = strconv.ParseUint(m["st_birthtime"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'Stat': %w", err)
}
if info.blocks, err = strconv.ParseUint(m["st_blocks"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'Stat': %w", err)
}
if info.modTime, err = strconv.ParseUint(m["st_mtime"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'Stat': %w", err)
}
if info.nlink, err = strconv.ParseUint(m["st_nlink"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'Stat': %w", err)
}
if info.size, err = strconv.ParseUint(m["st_size"], 10, 64); err != nil {
return nil, fmt.Errorf("afc 'Stat': %w", err)
}
return
}
func (c *afc) Open(filename string, mode AfcFileMode) (file *AfcFile, err error) {
buf := new(bytes.Buffer)
if err = binary.Write(buf, binary.LittleEndian, uint64(mode)); err != nil {
return nil, fmt.Errorf("afc send 'Open': %w", err)
}
buf.Write(toCString(filename))
if err = c.client.Send(libimobiledevice.AfcOperationFileOpen, buf.Bytes(), nil); err != nil {
return nil, fmt.Errorf("afc send 'Open': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return nil, fmt.Errorf("afc receive 'Open': %w", err)
}
if err = respMsg.Err(); err != nil {
return nil, fmt.Errorf("afc 'Open': %w", err)
}
if respMsg.Operation != libimobiledevice.AfcOperationFileOpenResult {
return nil, fmt.Errorf("afc operation mistake 'Open': '%d'", respMsg.Operation)
}
file = &AfcFile{
client: c.client,
fd: respMsg.Uint64(),
}
return
}
func (c *afc) Remove(filePath string) (err error) {
if err = c.client.Send(libimobiledevice.AfcOperationRemovePath, toCString(filePath), nil); err != nil {
return fmt.Errorf("afc send 'Remove': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'Remove': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'Remove': %w", err)
}
return
}
func (c *afc) Rename(oldPath string, newPath string) (err error) {
if err = c.client.Send(libimobiledevice.AfcOperationRenamePath, toCString(oldPath, newPath), nil); err != nil {
return fmt.Errorf("afc send 'Rename': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'Rename': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'Rename': %w", err)
}
return
}
func (c *afc) Mkdir(path string) (err error) {
if err = c.client.Send(libimobiledevice.AfcOperationMakeDir, toCString(path), nil); err != nil {
return fmt.Errorf("afc send 'Mkdir': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'Mkdir': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'Mkdir': %w", err)
}
return
}
func (c *afc) Link(oldName string, newName string, linkType AfcLinkType) (err error) {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, uint64(linkType))
buf.Write(toCString(oldName, newName))
if err = c.client.Send(libimobiledevice.AfcOperationMakeLink, buf.Bytes(), nil); err != nil {
return fmt.Errorf("afc send 'Link': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'Link': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'Link': %w", err)
}
return
}
func (c *afc) Truncate(filePath string, size int64) (err error) {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, uint64(size))
buf.Write(toCString(filePath))
if err = c.client.Send(libimobiledevice.AfcOperationTruncateFile, buf.Bytes(), nil); err != nil {
return fmt.Errorf("afc send 'Truncate': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'Truncate': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'Truncate': %w", err)
}
return
}
func (c *afc) SetFileModTime(filePath string, modTime time.Time) (err error) {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, uint64(modTime.Unix()))
buf.Write(toCString(filePath))
if err = c.client.Send(libimobiledevice.AfcOperationSetFileModTime, buf.Bytes(), nil); err != nil {
return fmt.Errorf("afc send 'SetFileModTime': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'SetFileModTime': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'SetFileModTime': %w", err)
}
return
}
func (c *afc) Hash(filePath string) ([]byte, error) {
var err error
if err = c.client.Send(libimobiledevice.AfcOperationGetFileHash, toCString(filePath), nil); err != nil {
return nil, fmt.Errorf("afc send 'Hash': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return nil, fmt.Errorf("afc receive 'Hash': %w", err)
}
if err = respMsg.Err(); err != nil {
return nil, fmt.Errorf("afc 'Hash': %w", err)
}
return respMsg.Payload, nil
}
func (c *afc) HashWithRange(filePath string, start, end uint64) ([]byte, error) {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, start)
_ = binary.Write(buf, binary.LittleEndian, end)
buf.Write(toCString(filePath))
var err error
if err = c.client.Send(libimobiledevice.AfcOperationGetFileHashRange, buf.Bytes(), nil); err != nil {
return nil, fmt.Errorf("afc send 'HashWithRange': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return nil, fmt.Errorf("afc receive 'HashWithRange': %w", err)
}
if err = respMsg.Err(); err != nil {
return nil, fmt.Errorf("afc 'HashWithRange': %w", err)
}
return respMsg.Payload, nil
}
// RemoveAll since iOS6+
func (c *afc) RemoveAll(path string) (err error) {
if err = c.client.Send(libimobiledevice.AfcOperationRemovePathAndContents, toCString(path), nil); err != nil {
return fmt.Errorf("afc send 'RemoveAll': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = c.client.Receive(); err != nil {
return fmt.Errorf("afc receive 'RemoveAll': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc 'RemoveAll': %w", err)
}
return
}
func (c *afc) WriteFile(filename string, data []byte, perm AfcFileMode) (err error) {
var file *AfcFile
if file, err = c.Open(filename, perm); err != nil {
return err
}
defer func() {
err = file.Close()
}()
if _, err = file.Write(data); err != nil {
return err
}
return
}
func toCString(s ...string) []byte {
buf := new(bytes.Buffer)
for _, v := range s {
buf.WriteString(v)
buf.WriteByte(0)
}
return buf.Bytes()
}
type AfcDiskInfo struct {
Model string
TotalBytes uint64
FreeBytes uint64
BlockSize uint64
}
type AfcFileInfo struct {
name string
creationTime uint64
blocks uint64
ifmt string
modTime uint64
nlink uint64
size uint64
source map[string]string
}
func (f *AfcFileInfo) Name() string {
return f.name
}
func (f *AfcFileInfo) Size() int64 {
return int64(f.size)
}
// func (f *AfcFileInfo) Mode() os.FileMode {
// return os.ModeType
// }
func (f *AfcFileInfo) ModTime() time.Time {
return time.Unix(0, int64(f.modTime))
}
func (f *AfcFileInfo) IsDir() bool {
return f.ifmt == "S_IFDIR"
}
// func (f *AfcFileInfo) Sys() interface{} {
// return f.source
// }
func (f *AfcFileInfo) CreationTime() time.Time {
return time.Unix(0, int64(f.creationTime))
}
// func (f *AfcFileInfo) Blocks() uint64 {
// return f.blocks
// }
// func (f *AfcFileInfo) Format() string {
// return f.ifmt
// }
// func (f *AfcFileInfo) Link() uint64 {
// return f.nlink
// }
// func (f *AfcFileInfo) PhysicalSize(info *AfcDiskInfo) int64 {
// return int64(f.blocks * (info.BlockSize / 8))
// }
type AfcFileMode uint32
const (
AfcFileModeRdOnly AfcFileMode = 0x00000001
AfcFileModeRw AfcFileMode = 0x00000002
AfcFileModeWrOnly AfcFileMode = 0x00000003
AfcFileModeWr AfcFileMode = 0x00000004
AfcFileModeAppend AfcFileMode = 0x00000005
AfcFileModeRdAppend AfcFileMode = 0x00000006
)
type AfcLockType int
const (
AfcLockTypeSharedLock AfcLockType = 1 | 4
AfcLockTypeExclusiveLock AfcLockType = 2 | 4
AfcLockTypeUnlock AfcLockType = 8 | 4
)
type AfcFile struct {
client *libimobiledevice.AfcClient
fd uint64
reader *bytes.Reader
}
func (f *AfcFile) op(o ...uint64) []byte {
buf := new(bytes.Buffer)
_ = binary.Write(buf, binary.LittleEndian, f.fd)
if len(o) == 0 {
return buf.Bytes()
}
for _, v := range o {
_ = binary.Write(buf, binary.LittleEndian, v)
}
return buf.Bytes()
}
func (f *AfcFile) Lock(lockType AfcLockType) (err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileRefLock, f.op(uint64(lockType)), nil); err != nil {
return fmt.Errorf("afc file send 'Lock': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = f.client.Receive(); err != nil {
return fmt.Errorf("afc file receive 'Lock': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc file 'Lock': %w", err)
}
return
}
func (f *AfcFile) Unlock() (err error) {
return f.Lock(AfcLockTypeUnlock)
}
func (f *AfcFile) Read(b []byte) (n int, err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileRead, f.op(uint64(len(b))), nil); err != nil {
return -1, fmt.Errorf("afc file send 'Read': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = f.client.Receive(); err != nil {
return -1, fmt.Errorf("afc file receive 'Read': %w", err)
}
if err = respMsg.Err(); err != nil {
return -1, fmt.Errorf("afc file 'Read': %w", err)
}
if respMsg.Payload == nil {
return 0, io.EOF
}
if f.reader == nil {
f.reader = bytes.NewReader(respMsg.Payload)
} else {
f.reader.Reset(respMsg.Payload)
}
return f.reader.Read(b)
}
func (f *AfcFile) Write(b []byte) (n int, err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileWrite, f.op(), b); err != nil {
return -1, fmt.Errorf("afc file send 'Write': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = f.client.Receive(); err != nil {
return -1, fmt.Errorf("afc file receive 'Write': %w", err)
}
if err = respMsg.Err(); err != nil {
return -1, fmt.Errorf("afc file 'Write': %w", err)
}
n = len(b)
return
}
func (f *AfcFile) Tell() (n uint64, err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileTell, f.op(), nil); err != nil {
return 0, fmt.Errorf("afc file 'Tell': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = f.client.Receive(); err != nil {
return 0, fmt.Errorf("afc file receive 'Tell': %w", err)
}
if err = respMsg.Err(); err != nil {
return 0, fmt.Errorf("afc file 'Tell': %w", err)
}
if respMsg.Operation != libimobiledevice.AfcOperationFileTellResult {
return 0, fmt.Errorf("afc operation mistake 'Tell': '%d'", respMsg.Operation)
}
n = respMsg.Uint64()
return
}
func (f *AfcFile) Seek(offset int64, whence int) (ret int64, err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileSeek, f.op(uint64(whence), uint64(offset)), nil); err != nil {
return -1, fmt.Errorf("afc file 'Seek': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = f.client.Receive(); err != nil {
return -1, fmt.Errorf("afc file receive 'Seek': %w", err)
}
if err = respMsg.Err(); err != nil {
return -1, fmt.Errorf("afc file 'Seek': %w", err)
}
var tell uint64
if tell, err = f.Tell(); err != nil {
return -1, err
}
ret = int64(tell)
return
}
func (f *AfcFile) Truncate(size int64) (err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileSetSize, f.op(uint64(size)), nil); err != nil {
return fmt.Errorf("afc file 'Truncate': %w", err)
}
var respMsg *libimobiledevice.AfcMessage
if respMsg, err = f.client.Receive(); err != nil {
return fmt.Errorf("afc file receive 'Truncate': %w", err)
}
if err = respMsg.Err(); err != nil {
return fmt.Errorf("afc file 'Truncate': %w", err)
}
return
}
func (f *AfcFile) Close() (err error) {
if err = f.client.Send(libimobiledevice.AfcOperationFileClose, f.op(), nil); err != nil {
return fmt.Errorf("afc file 'Close': %w", err)
}
if _, err = f.client.Receive(); err != nil {
return fmt.Errorf("afc file receive 'Close': %w", err)
}
return
}
type AfcLinkType int
const (
AfcLinkTypeHardLink AfcLinkType = 1
AfcLinkTypeSymLink AfcLinkType = 2
)