Skip to content

Commit 90510dc

Browse files
committed
Change CopyFile routine
1 parent 090f4a5 commit 90510dc

File tree

1 file changed

+24
-40
lines changed
  • src/arduino.cc/builder/builder_utils

1 file changed

+24
-40
lines changed

src/arduino.cc/builder/builder_utils/utils.go

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -414,63 +414,47 @@ func RemoveHyphenMDDFlagFromGCCCommandLine(buildProperties properties.Map) {
414414
buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS] = strings.Replace(buildProperties[constants.BUILD_PROPERTIES_COMPILER_CPP_FLAGS], "-MMD", "", -1)
415415
}
416416

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
451418
// by dst. The file will be created if it does not already exist. If the
452419
// 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) {
455423
in, err := os.Open(src)
456424
if err != nil {
457425
return
458426
}
459427
defer in.Close()
428+
460429
out, err := os.Create(dst)
461430
if err != nil {
462431
return
463432
}
464433
defer func() {
465-
cerr := out.Close()
466-
if err == nil {
467-
err = cerr
434+
if e := out.Close(); e != nil {
435+
err = e
468436
}
469437
}()
470-
if _, err = io.Copy(out, in); err != nil {
438+
439+
_, err = io.Copy(out, in)
440+
if err != nil {
471441
return
472442
}
443+
473444
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+
474458
return
475459
}
476460

0 commit comments

Comments
 (0)