MCPcopy
hub / github.com/slimtoolkit/slim / CopyRegularFile

Function CopyRegularFile

pkg/util/fsutil/fsutil.go:518–637  ·  view source on GitHub ↗

CopyRegularFile copies a regular file

(clone bool, src, dst string, makeDir bool)

Source from the content-addressed store, hash-verified

516
517// CopyRegularFile copies a regular file
518func 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 }

Callers 10

saveArtifactsMethod · 0.92
fixPy3CacheFileFunction · 0.92
rbEnsureGemFilesFunction · 0.92
nodeEnsurePackageFilesFunction · 0.92
CopyMetaArtifactsFunction · 0.92
CopyFileFunction · 0.85
CopyAndObfuscateFileFunction · 0.85
copyFileObjectHandlerFunction · 0.85
PrepareImageStateDirsFunction · 0.85

Calls 8

FileDirFunction · 0.85
cloneDirPathFunction · 0.85
UpdateFileTimesFunction · 0.85
ModeMethod · 0.80
SysMethod · 0.80
SizeMethod · 0.80
SysStatInfoFunction · 0.70
CloseMethod · 0.65

Tested by

no test coverage detected