@@ -414,63 +414,47 @@ func RemoveHyphenMDDFlagFromGCCCommandLine(buildProperties properties.Map) {
414
414
buildProperties [constants .BUILD_PROPERTIES_COMPILER_CPP_FLAGS ] = strings .Replace (buildProperties [constants .BUILD_PROPERTIES_COMPILER_CPP_FLAGS ], "-MMD" , "" , - 1 )
415
415
}
416
416
417
- // CopyFile copies a file from src to dst. If src and dst files exist, and are
418
- // the same, then return success. Otherise, attempt to create a hard link
419
- // between the two files. If that fail, copy the file contents from src to dst.
420
- func CopyFile (src , dst string ) (err error ) {
421
- sfi , err := os .Stat (src )
422
- if err != nil {
423
- return
424
- }
425
- if ! sfi .Mode ().IsRegular () {
426
- // cannot copy non-regular files (e.g., directories,
427
- // symlinks, devices, etc.)
428
- return fmt .Errorf ("CopyFile: non-regular source file %s (%q)" , sfi .Name (), sfi .Mode ().String ())
429
- }
430
- dfi , err := os .Stat (dst )
431
- if err != nil {
432
- if ! os .IsNotExist (err ) {
433
- return
434
- }
435
- } else {
436
- if ! (dfi .Mode ().IsRegular ()) {
437
- return fmt .Errorf ("CopyFile: non-regular destination file %s (%q)" , dfi .Name (), dfi .Mode ().String ())
438
- }
439
- if os .SameFile (sfi , dfi ) {
440
- return
441
- }
442
- }
443
- if err = os .Link (src , dst ); err == nil {
444
- return
445
- }
446
- err = copyFileContents (src , dst )
447
- return
448
- }
449
-
450
- // copyFileContents copies the contents of the file named src to the file named
417
+ // CopyFile copies the contents of the file named src to the file named
451
418
// by dst. The file will be created if it does not already exist. If the
452
419
// destination file exists, all it's contents will be replaced by the contents
453
- // of the source file.
454
- func copyFileContents (src , dst string ) (err error ) {
420
+ // of the source file. The file mode will be copied from the source and
421
+ // the copied data is synced/flushed to stable storage.
422
+ func CopyFile (src , dst string ) (err error ) {
455
423
in , err := os .Open (src )
456
424
if err != nil {
457
425
return
458
426
}
459
427
defer in .Close ()
428
+
460
429
out , err := os .Create (dst )
461
430
if err != nil {
462
431
return
463
432
}
464
433
defer func () {
465
- cerr := out .Close ()
466
- if err == nil {
467
- err = cerr
434
+ if e := out .Close (); e != nil {
435
+ err = e
468
436
}
469
437
}()
470
- if _ , err = io .Copy (out , in ); err != nil {
438
+
439
+ _ , err = io .Copy (out , in )
440
+ if err != nil {
471
441
return
472
442
}
443
+
473
444
err = out .Sync ()
445
+ if err != nil {
446
+ return
447
+ }
448
+
449
+ si , err := os .Stat (src )
450
+ if err != nil {
451
+ return
452
+ }
453
+ err = os .Chmod (dst , si .Mode ())
454
+ if err != nil {
455
+ return
456
+ }
457
+
474
458
return
475
459
}
476
460
0 commit comments