@@ -56,13 +56,28 @@ func (pt *PassThru) Read(p []byte) (int, error) {
5656 return n , nil
5757}
5858
59- func DownloadImage (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (string , string , error ) {
59+ func DownloadAndExtract (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , error ) {
60+ tmpZip , version , err := DownloadImage (client , targetVersion , upgradeConfirmCb , forceYes )
61+ if err != nil {
62+ return nil , fmt .Errorf ("error downloading the image: %v" , err )
63+ }
64+
65+ err = ExtractImage (tmpZip , tmpZip .Parent ())
66+ if err != nil {
67+ return nil , fmt .Errorf ("error extracting the image: %v" , err )
68+ }
69+
70+ imagePath := tmpZip .Parent ().Join ("arduino-unoq-debian-image-" + version )
71+ return imagePath , nil
72+ }
73+
74+ func DownloadImage (client * Client , targetVersion string , upgradeConfirmCb DownloadConfirmCB , forceYes bool ) (* paths.Path , string , error ) {
6075 var err error
6176
6277 slog .Info ("Checking for Debian image releases" )
6378 manifest , err := client .GetInfoManifest ()
6479 if err != nil {
65- return "" , "" , err
80+ return nil , "" , err
6681 }
6782
6883 if targetVersion == "latest" {
@@ -72,11 +87,11 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
7287 if ! forceYes {
7388 res , err := upgradeConfirmCb (targetVersion )
7489 if err != nil {
75- return "" , "" , err
90+ return nil , "" , err
7691 }
7792 if ! res {
7893 slog .Info ("Download not confirmed by user, exiting" )
79- return "" , "" , nil
94+ return nil , "" , nil
8095 }
8196 }
8297
@@ -87,63 +102,60 @@ func DownloadImage(client *Client, targetVersion string, upgradeConfirmCb Downlo
87102 slog .Info ("Downloading Debian image" , "version" , manifest .Latest .Version )
88103 download , size , err = client .FetchZip (manifest .Latest .Url )
89104 if err != nil {
90- return "" , "" , fmt .Errorf ("could not fetch Debian image: %w" , err )
105+ return nil , "" , fmt .Errorf ("could not fetch Debian image: %w" , err )
91106 }
92107 } else {
93108 // TODO: check the json for the specific version and download it
94- return "" , "" , nil
109+ return nil , "" , nil
95110 }
96111 defer download .Close ()
97112
98113 // Download the zip
99114 temp , err := paths .MkTempDir ("" , "flasher-updater-" )
100115 if err != nil {
101- return "" , "" , fmt .Errorf ("could not create temporary download directory: %w" , err )
116+ return nil , "" , fmt .Errorf ("could not create temporary download directory: %w" , err )
102117 }
103- tmpZip := temp .Join ("update.tar.xz" )
104- defer func () {
105- if err := tmpZip .Remove (); err != nil {
106- slog .Warn ("Could not remove temp zip" , "zip" , tmpZip .String (), "error" , err )
107- }
108- }()
109118
119+ tmpZip := temp .Join ("update.tar.zst" )
110120 tmpZipFile , err := tmpZip .Create ()
111121 if err != nil {
112- return "" , "" , err
122+ return nil , "" , err
113123 }
114124 defer tmpZipFile .Close ()
115125
116126 // Download and keep track of the progress
117127 src := & PassThru {Reader : download , length : size , progressCB : func (f float64 ) { feedback .Printf ("Download progress: %.2f %%" , f ) }}
118128 md5 := md5 .New ()
119129 if _ , err := io .Copy (io .MultiWriter (md5 , tmpZipFile ), src ); err != nil {
120- return "" , "" , err
130+ return nil , "" , err
121131 }
122- tmpZipFile .Close ()
123132
124133 // Check the hash
125134 if md5Byte , err := hex .DecodeString (manifest .Latest .Md5sum ); err != nil {
126- return "" , "" , fmt .Errorf ("could not convert md5 from hex to bytes: %w" , err )
135+ return nil , "" , fmt .Errorf ("could not convert md5 from hex to bytes: %w" , err )
127136 } else if s := md5 .Sum (nil ); ! bytes .Equal (s , md5Byte ) {
128- return "" , "" , fmt .Errorf ("bad hash: %x (expected %x)" , s , md5Byte )
137+ return nil , "" , fmt .Errorf ("bad hash: %x (expected %x)" , s , md5Byte )
129138 }
130139
140+ slog .Info ("Download of Debian image completed" , "path" , temp )
141+
142+ return tmpZip , targetVersion , nil
143+ }
144+
145+ func ExtractImage (archive , temp * paths.Path ) error {
131146 // Unzip the Debian image
132147 slog .Info ("Unzipping Debian image" , "tmpDir" , temp )
133- tmpZipFile , err = tmpZip .Open ()
148+ tmpZipFile , err := archive .Open ()
134149 if err != nil {
135- return "" , "" , fmt .Errorf ("could not open archive for unzip : %w" , err )
150+ return fmt .Errorf ("could not open archive: %w" , err )
136151 }
137152 defer tmpZipFile .Close ()
138153
139154 if err := extract .Archive (context .Background (), tmpZipFile , temp .String (), func (s string ) string {
140155 feedback .Print (s )
141156 return s
142157 }); err != nil {
143- return "" , "" , fmt .Errorf ("extracting archive: %w" , err )
158+ return fmt .Errorf ("could not extract archive: %w" , err )
144159 }
145-
146- slog .Info ("Download of Debian image completed" , "path" , temp )
147-
148- return temp .String (), targetVersion , nil
160+ return nil
149161}
0 commit comments