@@ -17,7 +17,6 @@ package commands
1717
1818import (
1919 "context"
20- "errors"
2120 "fmt"
2221 "io/ioutil"
2322 "net/url"
@@ -37,6 +36,7 @@ import (
3736 "github.com/arduino/arduino-cli/configuration"
3837 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
3938 paths "github.com/arduino/go-paths-helper"
39+ "github.com/pkg/errors"
4040 "github.com/sirupsen/logrus"
4141 "go.bug.st/downloader/v2"
4242)
@@ -179,14 +179,40 @@ func UpdateLibrariesIndex(ctx context.Context, req *rpc.UpdateLibrariesIndexRequ
179179 if err != nil {
180180 return err
181181 }
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" )
183189 if err != nil {
184190 return err
185191 }
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" )
189214 }
215+
190216 if _ , err := Rescan (req .GetInstance ().GetId ()); err != nil {
191217 return fmt .Errorf ("rescanning filesystem: %s" , err )
192218 }
0 commit comments