@@ -19,10 +19,9 @@ import (
19
19
"context"
20
20
"errors"
21
21
"fmt"
22
- "io/ioutil"
23
22
"net/url"
24
23
"os"
25
- "path "
24
+ "strings "
26
25
27
26
"github.com/arduino/arduino-cli/arduino"
28
27
"github.com/arduino/arduino-cli/arduino/cores"
@@ -32,7 +31,6 @@ import (
32
31
"github.com/arduino/arduino-cli/arduino/libraries/librariesindex"
33
32
"github.com/arduino/arduino-cli/arduino/libraries/librariesmanager"
34
33
"github.com/arduino/arduino-cli/arduino/resources"
35
- "github.com/arduino/arduino-cli/arduino/security"
36
34
sk "github.com/arduino/arduino-cli/arduino/sketch"
37
35
"github.com/arduino/arduino-cli/arduino/utils"
38
36
"github.com/arduino/arduino-cli/cli/globals"
@@ -41,7 +39,6 @@ import (
41
39
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
42
40
paths "github.com/arduino/go-paths-helper"
43
41
"github.com/sirupsen/logrus"
44
- "go.bug.st/downloader/v2"
45
42
"google.golang.org/grpc/codes"
46
43
"google.golang.org/grpc/status"
47
44
)
@@ -381,41 +378,12 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
381
378
}
382
379
defer tmp .RemoveAll ()
383
380
384
- // Download gzipped library_index
385
- tmpIndexGz := tmp .Join ("library_index.json.gz" )
386
- tmpIndexURL := librariesmanager .LibraryIndexGZURL .String ()
387
- if err := resources .DownloadFile (tmpIndexGz , tmpIndexURL , tr ("Updating index: library_index.json.gz" ), downloadCB .FromRPC (), nil , downloader .NoResume ); err != nil {
388
- return & arduino.FailedDownloadError {Message : tr ("Error downloading library_index.json.gz" ), Cause : err }
381
+ indexResource := resources.IndexResource {
382
+ URL : librariesmanager .LibraryIndexGZURL ,
383
+ SignatureURL : librariesmanager .LibraryIndexSignature ,
389
384
}
390
-
391
- // Download signature
392
- tmpSignature := tmp .Join ("library_index.json.sig" )
393
- tmpSignatureURL := librariesmanager .LibraryIndexSignature .String ()
394
- if err := resources .DownloadFile (tmpSignature , tmpSignatureURL , tr ("Updating index: library_index.json.sig" ), downloadCB .FromRPC (), nil , downloader .NoResume ); err != nil {
395
- return & arduino.FailedDownloadError {Message : tr ("Error downloading library_index.json.sig" ), Cause : err }
396
- }
397
-
398
- // Extract the real library_index
399
- tmpIndex := tmp .Join ("library_index.json" )
400
- if err := paths .GUnzip (tmpIndexGz , tmpIndex ); err != nil {
401
- return & arduino.PermissionDeniedError {Message : tr ("Error extracting library_index.json.gz" ), Cause : err }
402
- }
403
-
404
- // Check signature
405
- if ok , _ , err := security .VerifyArduinoDetachedSignature (tmpIndex , tmpSignature ); err != nil {
406
- return & arduino.PermissionDeniedError {Message : tr ("Error verifying signature" ), Cause : err }
407
- } else if ! ok {
408
- return & arduino.SignatureVerificationFailedError {File : "library_index.json" }
409
- }
410
-
411
- // Copy extracted library_index and signature to final destination
412
- lm .IndexFile .Remove ()
413
- lm .IndexFileSignature .Remove ()
414
- if err := tmpIndex .CopyTo (lm .IndexFile ); err != nil {
415
- return & arduino.PermissionDeniedError {Message : tr ("Error writing library_index.json" ), Cause : err }
416
- }
417
- if err := tmpSignature .CopyTo (lm .IndexFileSignature ); err != nil {
418
- return & arduino.PermissionDeniedError {Message : tr ("Error writing library_index.json.sig" ), Cause : err }
385
+ if err := indexResource .Download (lm .IndexFile .Parent (), downloadCB .FromRPC ()); err != nil {
386
+ return err
419
387
}
420
388
421
389
return nil
@@ -458,67 +426,15 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do
458
426
continue
459
427
}
460
428
461
- var tmp * paths.Path
462
- if tmpFile , err := ioutil .TempFile ("" , "" ); err != nil {
463
- return nil , & arduino.TempFileCreationFailedError {Cause : err }
464
- } else if err := tmpFile .Close (); err != nil {
465
- return nil , & arduino.TempFileCreationFailedError {Cause : err }
466
- } else {
467
- tmp = paths .New (tmpFile .Name ())
468
- }
469
- defer tmp .Remove ()
470
-
471
- coreIndexPath := indexpath .Join (path .Base (URL .Path ))
472
- if err := resources .DownloadFile (tmp , URL .String (), tr ("Updating index: %s" , coreIndexPath .Base ()), downloadCB .FromRPC (), nil , downloader .NoResume ); err != nil {
473
- return nil , & arduino.FailedDownloadError {Message : tr ("Error downloading index '%s'" , URL ), Cause : err }
474
- }
475
-
476
- // Check for signature
477
- var tmpSig * paths.Path
478
- var coreIndexSigPath * paths.Path
479
- if URL .Hostname () == "downloads.arduino.cc" {
480
- URLSig , err := url .Parse (URL .String ())
481
- if err != nil {
482
- return nil , & arduino.InvalidURLError {Cause : err }
483
- }
484
- URLSig .Path += ".sig"
485
-
486
- if t , err := ioutil .TempFile ("" , "" ); err != nil {
487
- return nil , & arduino.TempFileCreationFailedError {Cause : err }
488
- } else if err := t .Close (); err != nil {
489
- return nil , & arduino.TempFileCreationFailedError {Cause : err }
490
- } else {
491
- tmpSig = paths .New (t .Name ())
492
- }
493
- defer tmpSig .Remove ()
494
-
495
- coreIndexSigPath = indexpath .Join (path .Base (URLSig .Path ))
496
- if err := resources .DownloadFile (tmpSig , URLSig .String (), tr ("Updating index: %s" , coreIndexSigPath .Base ()), downloadCB .FromRPC (), nil , downloader .NoResume ); err != nil {
497
- return nil , & arduino.FailedDownloadError {Message : tr ("Error downloading index signature '%s'" , URLSig ), Cause : err }
498
- }
499
-
500
- if valid , _ , err := security .VerifyArduinoDetachedSignature (tmp , tmpSig ); err != nil {
501
- return nil , & arduino.PermissionDeniedError {Message : tr ("Error verifying signature" ), Cause : err }
502
- } else if ! valid {
503
- return nil , & arduino.SignatureVerificationFailedError {File : URL .String ()}
504
- }
429
+ indexResource := resources.IndexResource {
430
+ URL : URL ,
505
431
}
506
-
507
- if _ , err := packageindex .LoadIndex (tmp ); err != nil {
508
- return nil , & arduino.InvalidArgumentError {Message : tr ("Invalid package index in %s" , URL ), Cause : err }
509
- }
510
-
511
- if err := indexpath .MkdirAll (); err != nil {
512
- return nil , & arduino.PermissionDeniedError {Message : tr ("Can't create data directory %s" , indexpath ), Cause : err }
432
+ if strings .HasSuffix (URL .Host , "arduino.cc" ) {
433
+ indexResource .SignatureURL , _ = url .Parse (u ) // should not fail because we already parsed it
434
+ indexResource .SignatureURL .Path += ".sig"
513
435
}
514
-
515
- if err := tmp .CopyTo (coreIndexPath ); err != nil {
516
- return nil , & arduino.PermissionDeniedError {Message : tr ("Error saving downloaded index %s" , URL ), Cause : err }
517
- }
518
- if tmpSig != nil {
519
- if err := tmpSig .CopyTo (coreIndexSigPath ); err != nil {
520
- return nil , & arduino.PermissionDeniedError {Message : tr ("Error saving downloaded index signature" ), Cause : err }
521
- }
436
+ if err := indexResource .Download (indexpath , downloadCB .FromRPC ()); err != nil {
437
+ return nil , err
522
438
}
523
439
}
524
440
0 commit comments