@@ -279,7 +279,7 @@ func (u *CoreUpdater) clean(updateDir string) {
279279// Existing files are overwritten
280280// All files are created inside outDir, subdirectories are not created
281281// Return the output file name
282- func (u * CoreUpdater ) gzFileUnpack (gzfile , outDir string ) (string , error ) {
282+ func (u * CoreUpdater ) gzFileUnpack (gzfile , outDir string ) (outputName string , err error ) {
283283 f , err := os .Open (gzfile )
284284 if err != nil {
285285 return "" , fmt .Errorf ("os.Open(): %w" , err )
@@ -311,14 +311,10 @@ func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) {
311311 originalName = strings .TrimSuffix (originalName , ".gz" )
312312 }
313313
314- outputName : = filepath .Join (outDir , originalName )
314+ outputName = filepath .Join (outDir , originalName )
315315
316316 // Create the output file
317- wc , err := os .OpenFile (
318- outputName ,
319- os .O_WRONLY | os .O_CREATE | os .O_TRUNC ,
320- 0o755 ,
321- )
317+ wc , err := os .OpenFile (outputName , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0o755 )
322318 if err != nil {
323319 return "" , fmt .Errorf ("os.OpenFile(%s): %w" , outputName , err )
324320 }
@@ -343,7 +339,7 @@ func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) {
343339// Existing files are overwritten
344340// All files are created inside 'outDir', subdirectories are not created
345341// Return the output file name
346- func (u * CoreUpdater ) zipFileUnpack (zipfile , outDir string ) (string , error ) {
342+ func (u * CoreUpdater ) zipFileUnpack (zipfile , outDir string ) (outputName string , err error ) {
347343 zrc , err := zip .OpenReader (zipfile )
348344 if err != nil {
349345 return "" , fmt .Errorf ("zip.OpenReader(): %w" , err )
@@ -375,7 +371,7 @@ func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) {
375371 }()
376372 fi := zf .FileInfo ()
377373 name := fi .Name ()
378- outputName : = filepath .Join (outDir , name )
374+ outputName = filepath .Join (outDir , name )
379375
380376 if fi .IsDir () {
381377 return "" , fmt .Errorf ("the target file is a directory" )
@@ -402,14 +398,38 @@ func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) {
402398}
403399
404400// Copy file on disk
405- func (u * CoreUpdater ) copyFile (src , dst string ) error {
406- d , e := os .ReadFile (src )
407- if e != nil {
408- return e
401+ func (u * CoreUpdater ) copyFile (src , dst string ) ( err error ) {
402+ rc , err := os .Open (src )
403+ if err != nil {
404+ return fmt . Errorf ( "os.Open(%s): %w" , src , err )
409405 }
410- e = os .WriteFile (dst , d , 0o644 )
411- if e != nil {
412- return e
406+
407+ defer func () {
408+ closeErr := rc .Close ()
409+ if closeErr != nil && err == nil {
410+ err = closeErr
411+ }
412+ }()
413+
414+ // Create the output file
415+ // If the file does not exist, creates it with permissions perm (before umask);
416+ // otherwise truncates it before writing, without changing permissions.
417+ wc , err := os .OpenFile (dst , os .O_WRONLY | os .O_CREATE | os .O_TRUNC , 0o644 )
418+ if err != nil {
419+ return fmt .Errorf ("os.OpenFile(%s): %w" , dst , err )
413420 }
421+
422+ defer func () {
423+ closeErr := wc .Close ()
424+ if closeErr != nil && err == nil {
425+ err = closeErr
426+ }
427+ }()
428+
429+ _ , err = io .Copy (wc , rc )
430+ if err != nil {
431+ return fmt .Errorf ("io.Copy(): %w" , err )
432+ }
433+
414434 return nil
415435}
0 commit comments