CopyRegularFile copies a regular file
(clone bool, src, dst string, makeDir bool)
| 516 | |
| 517 | // CopyRegularFile copies a regular file |
| 518 | func CopyRegularFile(clone bool, src, dst string, makeDir bool) error { |
| 519 | log.Debugf("CopyRegularFile(%v,%v,%v,%v)", clone, src, dst, makeDir) |
| 520 | //'clone' should be true only for the dst files that need to clone the dir properties from src |
| 521 | s, err := os.Open(src) |
| 522 | if err != nil { |
| 523 | return err |
| 524 | } |
| 525 | defer s.Close() |
| 526 | |
| 527 | srcFileInfo, err := s.Stat() |
| 528 | if err != nil { |
| 529 | return err |
| 530 | } |
| 531 | |
| 532 | if !srcFileInfo.Mode().IsRegular() { |
| 533 | return ErrSrcNotRegularFile |
| 534 | } |
| 535 | |
| 536 | if makeDir { |
| 537 | dstDirPath := FileDir(dst) |
| 538 | if _, err := os.Stat(dstDirPath); err != nil { |
| 539 | if os.IsNotExist(err) { |
| 540 | srcDirPath := FileDir(src) |
| 541 | |
| 542 | if clone { |
| 543 | cloneDirPath(srcDirPath, dstDirPath) |
| 544 | } else { |
| 545 | var dirMode os.FileMode = 0777 |
| 546 | err = os.MkdirAll(dstDirPath, dirMode) |
| 547 | if err != nil { |
| 548 | return err |
| 549 | } |
| 550 | |
| 551 | //try copying the timestamps too (even without cloning) |
| 552 | srcDirInfo, err := os.Stat(srcDirPath) |
| 553 | if err == nil { |
| 554 | if sysStat, ok := srcDirInfo.Sys().(*syscall.Stat_t); ok { |
| 555 | ssi := SysStatInfo(sysStat) |
| 556 | if ssi.Ok { |
| 557 | if err := UpdateFileTimes(dstDirPath, ssi.Atime, ssi.Mtime); err != nil { |
| 558 | log.Warnf("CopyRegularFile() - UpdateFileTimes(%v) error - %v", dstDirPath, err) |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | } else { |
| 563 | log.Warnf("CopyRegularFile() - os.Stat(%v) error - %v", srcDirPath, err) |
| 564 | } |
| 565 | } |
| 566 | } else { |
| 567 | return err |
| 568 | } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | d, err := os.Create(dst) |
| 573 | if err != nil { |
| 574 | return err |
| 575 | } |
no test coverage detected