Skip to content

Commit

Permalink
Skip copy if file is already up to date
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanchyk committed Apr 18, 2024
1 parent df948ae commit b8a9a1d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions internal/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ func (s *Syncer) copy(src, dst string) error {
}
}

srcInfo, err := os.Stat(src)
srcFileInfo, err := os.Stat(src)
if err != nil {
return fmt.Errorf("cannot analyze source %s error: %s", src, err.Error())
}
if srcInfo.IsDir() {
if srcFileInfo.IsDir() {
srcDirEntries, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("cannot get entries of source folder %s error: %s", src, err.Error())
Expand All @@ -118,7 +118,7 @@ func (s *Syncer) copy(src, dst string) error {
}

if s.DeleteFlag {
relativePath := s.relativePath(s.absoluteDestinationPath, dstPath)
relativePath := s.relativePath(s.absoluteSourcePath, srcPath)
srcEntries[relativePath] = struct{}{}
}
}
Expand Down Expand Up @@ -149,6 +149,18 @@ func (s *Syncer) copy(src, dst string) error {
}
} else {
dstPath := filepath.Join(dst, filepath.Base(src))

if dstFileInfo, err := os.Stat(dstPath); err == nil && !dstFileInfo.IsDir() {
ok, err := s.ChecksumVerifier.Same(src, dstPath)
if err != nil {
return fmt.Errorf("cannot get checksum for file %s error: %s", dstPath, err.Error())
}
if ok {
fmt.Println("File", s.relativePath(s.absoluteDestinationPath, dstPath), "is up do date")
return nil
}
}

tempDstPath := dstPath + TEMP_FILE_EXT
nBytes, err := s.copyFile(src, tempDstPath)
if err != nil {
Expand All @@ -166,11 +178,11 @@ func (s *Syncer) copy(src, dst string) error {
}

func (s *Syncer) copyFile(src, dst string) (int64, error) {
sourceFileInfo, err := os.Stat(src)
srcFileInfo, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileInfo.Mode().IsRegular() {
if !srcFileInfo.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}

Expand Down

0 comments on commit b8a9a1d

Please sign in to comment.