-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
702 lines (563 loc) · 19.9 KB
/
main.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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
package mtpx
import (
"errors"
"fmt"
"github.com/ganeshrvel/go-mtpfs/mtp"
"os"
"path/filepath"
"strings"
"time"
)
// todo: work on documentations
// todo: hotplug
// initialize the mtp device
// returns mtp device
func Initialize(init Init) (*mtp.Device, error) {
dev, err := mtp.SelectDeviceWithDebugging("", init.DebugMode)
if err != nil {
return nil, MtpDetectFailedError{error: err}
}
dev.MTPDebug = init.DebugMode
dev.DataDebug = init.DebugMode
dev.USBDebug = init.DebugMode
dev.Timeout = devTimeout
if err = dev.Configure(); err != nil {
return nil, ConfigureError{error: err}
}
return dev, nil
}
// Dispose - close the mtp device
func Dispose(dev *mtp.Device) {
dev.Close()
}
// FetchDeviceInfo - fetch device Info
func FetchDeviceInfo(dev *mtp.Device) (*mtp.DeviceInfo, error) {
info := mtp.DeviceInfo{}
err := dev.GetDeviceInfo(&info)
if err != nil {
return nil, DeviceInfoError{error: err}
}
return &info, nil
}
// FetchStorages - fetch storages
func FetchStorages(dev *mtp.Device) ([]StorageData, error) {
sids := mtp.Uint32Array{}
if err := dev.GetStorageIDs(&sids); err != nil {
return nil, StorageInfoError{error: err}
}
if len(sids.Values) < 1 {
return nil, NoStorageError{error: fmt.Errorf("no storage found")}
}
var result []StorageData
for _, sid := range sids.Values {
var info mtp.StorageInfo
if err := dev.GetStorageInfo(sid, &info); err != nil {
return nil, StorageInfoError{error: err}
}
result = append(result, StorageData{
Sid: sid,
Info: info,
})
}
return result, nil
}
// MakeDirectory - create a new directory recursively using [fullPath]
// The path will be created if it does not Exists
func MakeDirectory(dev *mtp.Device, storageId uint32, fullPath string) (objectId uint32, err error) {
_fullPath := fixSlash(fullPath)
if _fullPath == PathSep {
return ParentObjectId, nil
}
splittedFullPath := strings.Split(_fullPath, PathSep)
objectId = uint32(ParentObjectId)
const skipIndex = 1
for _, fName := range splittedFullPath[skipIndex:] {
// fetch the parent object and
fi, err := GetObjectFromParentIdAndFilename(dev, storageId, objectId, fName)
if err != nil {
switch err.(type) {
case FileNotFoundError:
// if object does not Exists then create a new directory
_newObjectId, err := handleMakeDirectory(dev, storageId, objectId, fName)
if err != nil {
return 0, err
}
objectId = _newObjectId
continue
default:
return 0, err
}
}
// if the object Exists but if it's a file then throw an error
if !fi.IsDir {
return 0, InvalidPathError{error: fmt.Errorf("invalid path: %s. The object is not a directory", fName)}
}
objectId = fi.ObjectId
}
return objectId, nil
}
// List the contents in a directory
// use [recursive] to fetch the whole nested tree
// Tip: use [objectId] whenever possible to avoid traversing down the whole file tree to process and find the [objectId]
// if [skipDisallowedFiles] is true then files matching the [disallowedFiles] list will be ignored
// if [skipHiddenFiles] is true then hidden files (unix style) will be ignored
// return:
// [objectId]: objectId of the file/diectory
// [totalFiles]: total number of files
// [totalDirectories]: total number of directories
func Walk(dev *mtp.Device, storageId uint32, fullPath string, recursive, skipDisallowedFiles,
skipHiddenFiles bool, cb WalkCb) (objectId uint32, totalFiles, totalDirectories int64, err error) {
// fetch the objectId from [objectId] and/or [fullPath] parameters
fi, err := GetObjectFromPath(dev, storageId, fullPath)
if err != nil {
return 0, totalFiles, totalDirectories, err
}
// if the object file name matches [disallowedFiles] list then return an error
if skipDisallowedFiles {
fName := (*fi).Name
if ok := isDisallowedFiles(fName); ok {
return 0, totalFiles, totalDirectories, InvalidPathError{error: fmt.Errorf("disallowed file %v", fName)}
}
}
// if the object is a file then return objectId
if !fi.IsDir {
err := cb(fi.ObjectId, fi, nil)
if err != nil {
return 0, totalFiles, totalDirectories, err
}
totalFiles += 1
return fi.ObjectId, 1, totalDirectories, nil
}
totalFiles, totalDirectories, err = proccessWalk(dev, storageId, FileProp{fi.ObjectId, fullPath}, recursive, skipDisallowedFiles, skipHiddenFiles, cb)
if err != nil {
return 0, totalFiles, totalDirectories, err
}
return fi.ObjectId, totalFiles, totalDirectories, nil
}
// check if a file Exists
// returns Exists: bool, isDir: bool, objectId: uint32
// Since the [parentPath] is unavailable here the [fullPath] property of the resulting object [FileInfo] may not be valid.
func FileExists(dev *mtp.Device, storageId uint32, fileProps []FileProp) (fc []FileExistsContainer, err error) {
for _, fileProp := range fileProps {
fi, err := GetObjectFromObjectIdOrPath(dev, storageId, fileProp)
c := FileExistsContainer{}
if err != nil {
switch v := err.(type) {
case InvalidPathError:
c.Exists = false
case FileObjectError:
switch v1 := v.error.(type) {
case mtp.RCError:
if v1 == 0x2009 {
c.Exists = false
}
}
default:
return []FileExistsContainer{}, nil
}
} else {
c.Exists = true
c.FileInfo = fi
}
fc = append(fc, c)
}
return fc, nil
}
// Delete a file/directory
// [objectId] and [fullPath] are optional parameters
// if [objectId] is not available then [fullPath] will be used to fetch the [objectId]
// dont leave both [objectId] and [fullPath] empty
// Tip: use [objectId] whenever possible to avoid traversing down the whole file tree to process and find the [objectId]
func DeleteFile(dev *mtp.Device, storageId uint32, fileProps []FileProp) error {
for _, fileProp := range fileProps {
fc, err := FileExists(dev, storageId, []FileProp{fileProp})
if err != nil {
return nil
}
if !fc[0].Exists {
return nil
}
if err := dev.DeleteObject(fc[0].FileInfo.ObjectId); err != nil {
return FileObjectError{error: err}
}
}
return nil
}
// Rename a file/directory
// [objectId] and [fullPath] are optional parameters
// if [objectId] is not available then [fullPath] will be used to fetch the [objectId]
// dont leave both [objectId] and [fullPath] empty
// Tip: use [objectId] whenever possible to avoid traversing down the whole file tree to process and find the [objectId]
// return
// [objectId]: objectId of the file/diectory
func RenameFile(dev *mtp.Device, storageId uint32, fileProp FileProp, newFileName string) (objectId uint32, err error) {
fc, err := FileExists(dev, storageId, []FileProp{fileProp})
if err != nil {
return 0, err
}
if !fc[0].Exists {
return 0, InvalidPathError{error: fmt.Errorf("file not found: %s", fileProp.FullPath)}
}
fi := fc[0].FileInfo
if err := dev.SetObjectPropValue(fi.ObjectId, mtp.OPC_ObjectFileName, &mtp.StringValue{Value: newFileName}); err != nil {
switch v := err.(type) {
case mtp.RCError:
if v == 0x2002 {
return fi.ObjectId, nil
}
}
return 0, FileObjectError{error: err}
}
return fi.ObjectId, nil
}
// Transfer files from the local disk to the device
// sources: can be the list of files/directories that are to be sent to the device
// destination: fullPath to the destination directory
// preprocessFiles: if enabled, will fetch the total file size and count of the source. Use this will caution as it may take a few seconds to minutes to procress the files.
// return:
// [destinationObjectId]: objectId of [destination] directory
// [bulkFilesSent]: total transferred files (directory count not included)
// [bulkSizeSent]: total size of the uploaded files
func UploadFiles(dev *mtp.Device, storageId uint32, sources []string, destination string, preprocessFiles bool, preprocessCb LocalPreprocessCb, progressCb ProgressCb) (destinationObjectId uint32, bulkFilesSent int64, bulkSizeSent int64, err error) {
_destination := fixSlash(destination)
pInfo := ProgressInfo{
FileInfo: &FileInfo{},
StartTime: time.Now(),
LatestSentTime: time.Now(),
Speed: 0,
TotalFiles: 0,
TotalDirectories: 0,
FilesSent: 0,
FilesSentProgress: 0,
ActiveFileSize: &TransferSizeInfo{},
BulkFileSize: &TransferSizeInfo{},
Status: InProgress,
}
// if [preprocessFiles] is true then fetch the total number of files from the file tree
// total number of files in the current upload session
// if [preprocessFiles] is false then [totalFiles] is 0
var totalFiles int64 = 0
// if [preprocessFiles] is true then fetch the total number of files from the file tree
// total number of directories in the current upload session
// if [preprocessFiles] is false then [totalDirectories] is 0
var totalDirectories int64 = 0
// if [preprocessFiles] is true then fetch the total number of files from the file tree
// // total size of all the files combined in the current upload session
// if [preprocessFiles] is false then [totalDirectories] is 0
var totalSize int64 = 0
// keep track of [bulkSizeSent]
bulkSizeSent = 0
if preprocessFiles {
_totalFiles, _totalDirectories, _totalSize, err := walkLocalFiles(sources, func(fi *os.FileInfo, fullPath string, err error) error {
if err != nil {
return err
}
if (*fi).IsDir() {
return nil
}
if err = preprocessCb(fi, fullPath, nil); err != nil {
return err
}
return nil
})
if err != nil {
return 0, bulkFilesSent, bulkSizeSent, err
}
totalFiles = _totalFiles
totalDirectories = _totalDirectories
totalSize = _totalSize
}
destParentId, err := MakeDirectory(dev, storageId, _destination)
if err != nil {
return 0, bulkFilesSent, bulkSizeSent, err
}
pInfo.TotalFiles = totalFiles
pInfo.TotalDirectories = totalDirectories
pInfo.BulkFileSize.Total = totalSize
for _, source := range sources {
_source := fixSlash(source)
sourceParentPath := filepath.Dir(_source)
destinationFilesDict := map[string]uint32{
_destination: destParentId,
}
// walk through the source
err = filepath.Walk(_source,
func(path string, fInfo os.FileInfo, err error) error {
if err != nil {
return err
}
name := fInfo.Name()
// don't follow symlinks
if isSymlinkLocal(fInfo) {
return nil
}
// filter out disallowed files
if isDisallowedFiles(name) {
return nil
}
sourceFilePath := fixSlash(path)
// map the local files path to the mtp files path
destinationParentPath, destinationFilePath := mapSourcePathToDestinationPath(
sourceFilePath, sourceParentPath, _destination,
)
size := fInfo.Size()
isDir := fInfo.IsDir()
// if the object is a directory then create a directory using [MakeDirectory] or [MakeDirectory]
if isDir {
// if the parent path Exists within the [destinationFilesDict] then fetch the [parentId] (value) and make the destination directory
if _, ok := destinationFilesDict[destinationParentPath]; ok {
objId, err := MakeDirectory(dev, storageId, destinationFilePath)
if err != nil {
return err
}
// append the current objectId to [destinationFilesDict]
destinationFilesDict[destinationFilePath] = objId
// if the parent path DOES NOT Exists within the [destinationFilesDict] create a new directory using costlier [MakeDirectory] method
// this is a fallback situation
} else {
objId, err := MakeDirectory(dev, storageId, _destination)
if err != nil {
return err
}
// append the current objectId to [destinationFilesDict]
destinationFilesDict[destinationFilePath] = objId
}
return nil
}
/// if the object is a file then create a file
var fileParentId uint32
_parentId, ok := destinationFilesDict[destinationParentPath]
if ok {
// if [destinationParentPath] Exists within [destinationFilesDict] then use the value of the [destinationFilesDict] item as [parentId]
fileParentId = _parentId
} else {
// if [destinationParentPath] DOES NOT Exists within [destinationFilesDict] then create the parent directory using [MakeDirectory] and use the resulting objId as [parentId]
objId, err := MakeDirectory(dev, storageId, destinationParentPath)
if err != nil {
return err
}
// append the current objectId to [destinationFilesDict]
destinationFilesDict[destinationFilePath] = objId
fileParentId = objId
}
// read the local file
fileBuf, err := os.Open(sourceFilePath)
if err != nil {
return InvalidPathError{error: err}
}
defer fileBuf.Close()
var compressedSize uint32
// assign compressedSize of the file
if size > 0xFFFFFFFF {
compressedSize = 0xFFFFFFFF
} else {
compressedSize = uint32(size)
}
fObj := mtp.ObjectInfo{
StorageID: storageId,
ObjectFormat: mtp.OFC_Undefined,
ParentObject: fileParentId,
Filename: name,
CompressedSize: compressedSize,
ModificationDate: fInfo.ModTime(),
}
// keep track of [bulkFilesSent]
bulkFilesSent += 1
pInfo.FileInfo = &FileInfo{
Info: &fObj,
Size: size,
IsDir: isDir,
ModTime: fObj.ModificationDate,
Name: fObj.Filename,
FullPath: destinationFilePath,
ParentPath: destinationParentPath,
Extension: extension(fObj.Filename, isDir),
ParentId: fObj.ParentObject,
}
pInfo.LatestSentTime = time.Now()
// create file
var prevSentSize int64 = 0
objId, err := handleMakeFile(
dev, storageId, &fObj, &fInfo, fileBuf,
true,
func(total, sent int64, objId uint32, err error) error {
if err != nil {
return err
}
pInfo.FileInfo.ObjectId = objId
pInfo.ActiveFileSize.Total = total
pInfo.ActiveFileSize.Sent = sent
pInfo.ActiveFileSize.Progress = Percent(float32(sent), float32(total))
chunkSize := sent - prevSentSize
bulkSizeSent += chunkSize
pInfo.BulkFileSize.Sent = bulkSizeSent
pInfo.BulkFileSize.Progress = Percent(float32(bulkSizeSent), float32(totalSize))
pInfo.Speed = transferRate(chunkSize, pInfo.LatestSentTime)
if err = progressCb(&pInfo, nil); err != nil {
return err
}
pInfo.LatestSentTime = time.Now()
prevSentSize = sent
return nil
},
)
if err != nil {
return err
}
pInfo.FilesSent = bulkFilesSent
pInfo.FilesSentProgress = Percent(float32(bulkFilesSent), float32(totalFiles))
pInfo.FileInfo.ObjectId = objId
// append the current objectId to [destinationFilesDict]
destinationFilesDict[destinationFilePath] = objId
return nil
},
)
if err != nil {
switch err.(type) {
case InvalidPathError:
return destParentId, bulkFilesSent, bulkSizeSent, err
case *os.PathError:
if errors.Is(err, os.ErrPermission) {
return destParentId, bulkFilesSent, bulkSizeSent, FilePermissionError{error: err}
}
if errors.Is(err, os.ErrNotExist) {
return destParentId, bulkFilesSent, bulkSizeSent, InvalidPathError{error: err}
}
return destParentId, bulkFilesSent, bulkSizeSent, LocalFileError{error: err}
default:
return destParentId, bulkFilesSent, bulkSizeSent,
FileTransferError{error: fmt.Errorf("an error occured while uploading files. %+v", err.Error())}
}
}
}
pInfo.Status = Completed
if err := progressCb(&pInfo, nil); err != nil {
return destParentId, bulkFilesSent, bulkSizeSent, err
}
return destParentId, bulkFilesSent, bulkSizeSent, nil
}
// Transfer files from the device to the local disk
// sources: can be the list of files/directories that are to be sent to the local disk
// destination: fullPath to the destination directory
// return:
// [totalFiles]: total transferred files (directory count not included)
// [totalSize]: total size of the uploaded files
func DownloadFiles(dev *mtp.Device, storageId uint32, sources []string, destination string,
preprocessFiles bool, preprocessCb MtpPreprocessCb, progressCb ProgressCb) (bulkFilesSent int64, bulkSizeSent int64, err error) {
_destination := fixSlash(destination)
pInfo := ProgressInfo{
FileInfo: &FileInfo{},
StartTime: time.Now(),
LatestSentTime: time.Now(),
Speed: 0,
TotalFiles: 0,
TotalDirectories: 0,
FilesSent: 0,
FilesSentProgress: 0,
ActiveFileSize: &TransferSizeInfo{},
BulkFileSize: &TransferSizeInfo{},
Status: InProgress,
}
// if [preprocessFiles] is true then fetch the total number of files from the file tree
// total number of files in the current download session
// if [preprocessFiles] is false then [totalFiles] is 0
var totalFiles int64 = 0
// if [preprocessFiles] is true then fetch the total number of files from the file tree
// total number of directories in the current download session
// if [preprocessFiles] is false then [totalDirectories] is 0
var totalDirectories int64 = 0
// if [preprocessFiles] is true then fetch the total number of files from the file tree
// // total size of all the files combined in the current download session
// if [preprocessFiles] is false then [totalDirectories] is 0
var totalSize int64 = 0
var cache = downloadFilesObjectCache{}
if preprocessFiles {
for _, source := range sources {
_source := fixSlash(source)
_, _totalFiles, _totalDirectories, err := Walk(dev, storageId, _source, true, true, false,
func(objectId uint32, fi *FileInfo, err error) error {
if err != nil {
return err
}
sourceParentPath := filepath.Dir(_source)
destinationFileParentPath, destinationFilePath := mapSourcePathToDestinationPath(
fi.FullPath, sourceParentPath, _destination,
)
cache[destinationFilePath] = downloadFilesObjectCacheContainer{
fileInfo: fi,
sourceParentPath: sourceParentPath,
destinationFileParentPath: destinationFileParentPath,
destinationFilePath: destinationFilePath,
}
if fi.IsDir {
return nil
}
// filter out disallowed files
if isDisallowedFiles(fi.Name) {
return nil
}
if err = preprocessCb(fi, nil); err != nil {
return err
}
totalSize += fi.Size
return nil
})
if err != nil {
return bulkFilesSent, bulkSizeSent, err
}
totalFiles += _totalFiles
totalDirectories += _totalDirectories
}
}
pInfo.TotalFiles = totalFiles
pInfo.TotalDirectories = totalDirectories
pInfo.BulkFileSize.Total = totalSize
dfProps := &processDownloadFilesProps{
bulkFilesSent: bulkFilesSent,
bulkSizeSent: bulkSizeSent,
totalFiles: totalFiles,
totalSize: totalSize,
}
if len(cache) > 0 {
for _, c := range cache {
dfProps.sourceParentPath = c.sourceParentPath
dfProps.destinationFileParentPath = c.destinationFileParentPath
dfProps.destinationFilePath = c.destinationFilePath
err := processDownloadFiles(dev, &pInfo, c.fileInfo, progressCb, dfProps)
if err != nil {
return processDownloadFilesError(dfProps, err)
}
}
} else {
for _, source := range sources {
_source := fixSlash(source)
_, err := GetObjectFromPath(dev, storageId, _source)
if err != nil {
return dfProps.bulkFilesSent, dfProps.bulkSizeSent, err
}
_, _, _, wErr := Walk(dev, storageId, _source, true, true, false,
func(objectId uint32, fi *FileInfo, err error) error {
if err != nil {
return err
}
sourceParentPath := filepath.Dir(_source)
destinationFileParentPath, destinationFilePath := mapSourcePathToDestinationPath(
fi.FullPath, sourceParentPath, _destination,
)
dfProps.sourceParentPath = sourceParentPath
dfProps.destinationFileParentPath = destinationFileParentPath
dfProps.destinationFilePath = destinationFilePath
return processDownloadFiles(dev, &pInfo, fi, progressCb, dfProps)
})
if wErr != nil {
return processDownloadFilesError(dfProps, wErr)
}
}
}
pInfo.Status = Completed
if err := progressCb(&pInfo, nil); err != nil {
return dfProps.bulkFilesSent, dfProps.bulkSizeSent, err
}
return dfProps.bulkFilesSent, dfProps.bulkSizeSent, nil
}
func main() {}