@@ -17,7 +17,6 @@ package commands
17
17
18
18
import (
19
19
"context"
20
- "errors"
21
20
"fmt"
22
21
"io/ioutil"
23
22
"net/url"
@@ -37,6 +36,7 @@ import (
37
36
"github.com/arduino/arduino-cli/configuration"
38
37
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
39
38
paths "github.com/arduino/go-paths-helper"
39
+ "github.com/pkg/errors"
40
40
"github.com/sirupsen/logrus"
41
41
"go.bug.st/downloader/v2"
42
42
)
@@ -179,14 +179,40 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
179
179
if err != nil {
180
180
return err
181
181
}
182
- d , err := lm .UpdateIndex (config )
182
+
183
+ if err := lm .IndexFile .Parent ().MkdirAll (); err != nil {
184
+ return err
185
+ }
186
+
187
+ // Create a temp dir to stage all downloads
188
+ tmp , err := paths .MkTempDir ("" , "library_index_download" )
183
189
if err != nil {
184
190
return err
185
191
}
186
- Download (d , "Updating index: library_index.json" , downloadCB )
187
- if d .Error () != nil {
188
- return d .Error ()
192
+ defer tmp .RemoveAll ()
193
+
194
+ // Download gzipped library_index
195
+ tmpIndexGz := tmp .Join ("library_index.json.gz" )
196
+ if d , err := downloader .DownloadWithConfig (tmpIndexGz .String (), librariesmanager .LibraryIndexGZURL .String (), * config , downloader .NoResume ); err != nil {
197
+ return err
198
+ } else {
199
+ if err := Download (d , "Updating index: library_index.json.gz" , downloadCB ); err != nil {
200
+ return errors .Wrap (err , "downloading library_index.json.gz" )
201
+ }
202
+ }
203
+
204
+ // Extract the real library_index
205
+ tmpIndex := tmp .Join ("library_index.json" )
206
+ if err := paths .GUnzip (tmpIndexGz , tmpIndex ); err != nil {
207
+ return errors .Wrap (err , "unzipping library_index.json.gz" )
208
+ }
209
+
210
+ // Copy extracted library_index to final destination
211
+ lm .IndexFile .Remove ()
212
+ if err := tmpIndex .CopyTo (lm .IndexFile ); err != nil {
213
+ return errors .Wrap (err , "writing library_index.json" )
189
214
}
215
+
190
216
if _ , err := Rescan (req .GetInstance ().GetId ()); err != nil {
191
217
return fmt .Errorf ("rescanning filesystem: %s" , err )
192
218
}
0 commit comments