@@ -176,7 +176,7 @@ func downloadFile(ctx context.Context, path string, url string, downloadProgress
176176 if nr > 0 {
177177 nw , ew := file .Write (buf [0 :nr ])
178178 if nw < nr {
179- return fmt .Errorf ("short write: %d < %d" , nw , nr )
179+ return fmt .Errorf ("short file write: %d < %d" , nw , nr )
180180 }
181181 written += int64 (nw )
182182 if ew != nil {
@@ -240,7 +240,7 @@ func verifyFile(path string, expectedHash string, verifyProgress *float32, scope
240240 if nr > 0 {
241241 nw , ew := hash .Write (buf [0 :nr ])
242242 if nw < nr {
243- return fmt .Errorf ("short write: %d < %d" , nw , nr )
243+ return fmt .Errorf ("short hash write: %d < %d" , nw , nr )
244244 }
245245 verified += int64 (nw )
246246 if ew != nil {
@@ -260,11 +260,16 @@ func verifyFile(path string, expectedHash string, verifyProgress *float32, scope
260260 }
261261 }
262262
263- hashSum := hash .Sum (nil )
264- scopedLogger .Info ().Str ("path" , path ).Str ("hash" , hex .EncodeToString (hashSum )).Msg ("SHA256 hash of" )
263+ // close the file so we can rename below
264+ if err := fileToHash .Close (); err != nil {
265+ return fmt .Errorf ("error closing file: %w" , err )
266+ }
267+
268+ hashSum := hex .EncodeToString (hash .Sum (nil ))
269+ scopedLogger .Info ().Str ("path" , path ).Str ("hash" , hashSum ).Msg ("SHA256 hash of" )
265270
266- if hex . EncodeToString ( hashSum ) != expectedHash {
267- return fmt .Errorf ("hash mismatch: %x != %s" , hashSum , expectedHash )
271+ if hashSum != expectedHash {
272+ return fmt .Errorf ("hash mismatch: %s != %s" , hashSum , expectedHash )
268273 }
269274
270275 if err := os .Rename (unverifiedPath , path ); err != nil {
@@ -313,7 +318,7 @@ func triggerOTAStateUpdate() {
313318func TryUpdate (ctx context.Context , deviceId string , includePreRelease bool ) error {
314319 scopedLogger := otaLogger .With ().
315320 Str ("deviceId" , deviceId ).
316- Str ("includePreRelease" , fmt . Sprintf ( "%v" , includePreRelease ) ).
321+ Bool ("includePreRelease" , includePreRelease ).
317322 Logger ()
318323
319324 scopedLogger .Info ().Msg ("Trying to update..." )
@@ -362,8 +367,9 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
362367 otaState .Error = fmt .Sprintf ("Error downloading app update: %v" , err )
363368 scopedLogger .Error ().Err (err ).Msg ("Error downloading app update" )
364369 triggerOTAStateUpdate ()
365- return err
370+ return fmt . Errorf ( "error downloading app update: %w" , err )
366371 }
372+
367373 downloadFinished := time .Now ()
368374 otaState .AppDownloadFinishedAt = & downloadFinished
369375 otaState .AppDownloadProgress = 1
@@ -379,17 +385,21 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
379385 otaState .Error = fmt .Sprintf ("Error verifying app update hash: %v" , err )
380386 scopedLogger .Error ().Err (err ).Msg ("Error verifying app update hash" )
381387 triggerOTAStateUpdate ()
382- return err
388+ return fmt . Errorf ( "error verifying app update: %w" , err )
383389 }
390+
384391 verifyFinished := time .Now ()
385392 otaState .AppVerifiedAt = & verifyFinished
386393 otaState .AppVerificationProgress = 1
394+ triggerOTAStateUpdate ()
395+
387396 otaState .AppUpdatedAt = & verifyFinished
388397 otaState .AppUpdateProgress = 1
389398 triggerOTAStateUpdate ()
390399
391400 scopedLogger .Info ().Msg ("App update downloaded" )
392401 rebootNeeded = true
402+ triggerOTAStateUpdate ()
393403 } else {
394404 scopedLogger .Info ().Msg ("App is up to date" )
395405 }
@@ -405,8 +415,9 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
405415 otaState .Error = fmt .Sprintf ("Error downloading system update: %v" , err )
406416 scopedLogger .Error ().Err (err ).Msg ("Error downloading system update" )
407417 triggerOTAStateUpdate ()
408- return err
418+ return fmt . Errorf ( "error downloading system update: %w" , err )
409419 }
420+
410421 downloadFinished := time .Now ()
411422 otaState .SystemDownloadFinishedAt = & downloadFinished
412423 otaState .SystemDownloadProgress = 1
@@ -422,8 +433,9 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
422433 otaState .Error = fmt .Sprintf ("Error verifying system update hash: %v" , err )
423434 scopedLogger .Error ().Err (err ).Msg ("Error verifying system update hash" )
424435 triggerOTAStateUpdate ()
425- return err
436+ return fmt . Errorf ( "error verifying system update: %w" , err )
426437 }
438+
427439 scopedLogger .Info ().Msg ("System update downloaded" )
428440 verifyFinished := time .Now ()
429441 otaState .SystemVerifiedAt = & verifyFinished
@@ -439,8 +451,10 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
439451 if err != nil {
440452 otaState .Error = fmt .Sprintf ("Error starting rk_ota command: %v" , err )
441453 scopedLogger .Error ().Err (err ).Msg ("Error starting rk_ota command" )
454+ triggerOTAStateUpdate ()
442455 return fmt .Errorf ("error starting rk_ota command: %w" , err )
443456 }
457+
444458 ctx , cancel := context .WithCancel (context .Background ())
445459 defer cancel ()
446460
@@ -475,13 +489,15 @@ func TryUpdate(ctx context.Context, deviceId string, includePreRelease bool) err
475489 Str ("output" , output ).
476490 Int ("exitCode" , cmd .ProcessState .ExitCode ()).
477491 Msg ("Error executing rk_ota command" )
492+ triggerOTAStateUpdate ()
478493 return fmt .Errorf ("error executing rk_ota command: %w\n Output: %s" , err , output )
479494 }
495+
480496 scopedLogger .Info ().Str ("output" , output ).Msg ("rk_ota success" )
481497 otaState .SystemUpdateProgress = 1
482498 otaState .SystemUpdatedAt = & verifyFinished
483- triggerOTAStateUpdate ()
484499 rebootNeeded = true
500+ triggerOTAStateUpdate ()
485501 } else {
486502 scopedLogger .Info ().Msg ("System is up to date" )
487503 }
0 commit comments