@@ -279,7 +279,7 @@ func (u *CoreUpdater) clean(updateDir string) {
279
279
// Existing files are overwritten
280
280
// All files are created inside outDir, subdirectories are not created
281
281
// 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 ) {
283
283
f , err := os .Open (gzfile )
284
284
if err != nil {
285
285
return "" , fmt .Errorf ("os.Open(): %w" , err )
@@ -311,14 +311,10 @@ func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) {
311
311
originalName = strings .TrimSuffix (originalName , ".gz" )
312
312
}
313
313
314
- outputName : = filepath .Join (outDir , originalName )
314
+ outputName = filepath .Join (outDir , originalName )
315
315
316
316
// 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 )
322
318
if err != nil {
323
319
return "" , fmt .Errorf ("os.OpenFile(%s): %w" , outputName , err )
324
320
}
@@ -343,7 +339,7 @@ func (u *CoreUpdater) gzFileUnpack(gzfile, outDir string) (string, error) {
343
339
// Existing files are overwritten
344
340
// All files are created inside 'outDir', subdirectories are not created
345
341
// 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 ) {
347
343
zrc , err := zip .OpenReader (zipfile )
348
344
if err != nil {
349
345
return "" , fmt .Errorf ("zip.OpenReader(): %w" , err )
@@ -375,7 +371,7 @@ func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) {
375
371
}()
376
372
fi := zf .FileInfo ()
377
373
name := fi .Name ()
378
- outputName : = filepath .Join (outDir , name )
374
+ outputName = filepath .Join (outDir , name )
379
375
380
376
if fi .IsDir () {
381
377
return "" , fmt .Errorf ("the target file is a directory" )
@@ -402,14 +398,38 @@ func (u *CoreUpdater) zipFileUnpack(zipfile, outDir string) (string, error) {
402
398
}
403
399
404
400
// 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 )
409
405
}
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 )
413
420
}
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
+
414
434
return nil
415
435
}
0 commit comments