Skip to content

Commit

Permalink
Add temporary file while copy
Browse files Browse the repository at this point in the history
  • Loading branch information
rpanchyk committed Apr 18, 2024
1 parent ffe3cd0 commit 6449d7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/spf13/cobra"
)

var verboseFlag bool

var deleteFlag bool
var (
verboseFlag bool
deleteFlag bool
)

var rootCmd = &cobra.Command{
Use: "fsync [flags] SRC DEST",
Expand All @@ -29,7 +30,7 @@ Attention! Use this tool on your own risk! Author is not responsible of synced f

if verboseFlag {
fmt.Println("verbose:", verboseFlag)
// fmt.Println("delete:", deleteFlag)
fmt.Println("delete:", deleteFlag)
fmt.Println("Non-flag arguments:", args)
fmt.Println()
}
Expand All @@ -47,7 +48,6 @@ Attention! Use this tool on your own risk! Author is not responsible of synced f
os.Exit(1)
}
fmt.Println("Sync finished")
os.Exit(0)
},
}

Expand Down
24 changes: 15 additions & 9 deletions internal/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type Syncer struct {
absoluteDestinationPath string
}

const TEMP_FILE_EXT = ".fsync-tmp"

func (s *Syncer) Sync() error {
src, dst, err := s.normalizePaths(s.Source, s.Destination)
if err != nil {
Expand Down Expand Up @@ -77,7 +79,7 @@ func (s *Syncer) absolutePath(path string) (string, error) {
func (s *Syncer) copy(src, dst string) error {
if _, err := os.Stat(dst); os.IsNotExist(err) {
if err = os.MkdirAll(dst, os.ModeDir); err != nil {
return fmt.Errorf("cannot create destination folder: %s", dst)
return fmt.Errorf("cannot create destination folder %s error: %s", dst, err.Error())
} else {
if s.VerboseFlag {
fmt.Println("Created destination folder:", dst)
Expand All @@ -92,15 +94,15 @@ func (s *Syncer) copy(src, dst string) error {
if srcInfo.IsDir() {
srcDirEntries, err := os.ReadDir(src)
if err != nil {
return fmt.Errorf("cannot get entries of source folder: %s", src)
return fmt.Errorf("cannot get entries of source folder %s error: %s", src, err.Error())
}

srcEntries := make(map[string]struct{})

for _, dirEntry := range srcDirEntries {
entryInfo, err := dirEntry.Info()
if err != nil {
return fmt.Errorf("cannot get entry info: %s", dirEntry)
return fmt.Errorf("cannot get entry info %s error: %s", dirEntry, err.Error())
}

srcPath := filepath.Join(src, entryInfo.Name())
Expand All @@ -124,20 +126,20 @@ func (s *Syncer) copy(src, dst string) error {
if s.DeleteFlag {
dstDirEntries, err := os.ReadDir(dst)
if err != nil {
return fmt.Errorf("cannot get entries of destination folder: %s", dst)
return fmt.Errorf("cannot get entries of destination folder %s error: %s", dst, err.Error())
}

for _, dirEntry := range dstDirEntries {
entryInfo, err := dirEntry.Info()
if err != nil {
return fmt.Errorf("cannot get entry info: %s", dirEntry)
return fmt.Errorf("cannot get entry info %s error: %s", dirEntry, err.Error())
}

dstPath := filepath.Join(dst, entryInfo.Name())
relativePath := s.relativePath(s.absoluteDestinationPath, dstPath)
if _, ok := srcEntries[relativePath]; !ok {
if err = s.removeExtraneous(dstPath, dirEntry.IsDir()); err != nil {
return fmt.Errorf("cannot remove extraneous: %s error: %s", dstPath, err.Error())
return fmt.Errorf("cannot remove extraneous %s error: %s", dstPath, err.Error())
}
if s.VerboseFlag {
fmt.Println("Removed extraneous:", dstPath)
Expand All @@ -146,10 +148,14 @@ func (s *Syncer) copy(src, dst string) error {
}
}
} else {
dstPath := filepath.Join(dst, filepath.Base(src))
nBytes, err := s.copyFile(src, dstPath)
origDstPath := filepath.Join(dst, filepath.Base(src))
tempDstPath := origDstPath + TEMP_FILE_EXT
nBytes, err := s.copyFile(src, tempDstPath)
if err != nil {
return fmt.Errorf("cannot copy file %s to %s", src, dstPath)
return fmt.Errorf("cannot copy file %s to %s error: %s", src, tempDstPath, err.Error())
}
if err = os.Rename(tempDstPath, origDstPath); err != nil {
return fmt.Errorf("cannot rename file %s to %s error: %s", tempDstPath, origDstPath, err.Error())
}
if s.VerboseFlag {
fmt.Println("Copied file", s.relativePath(s.absoluteSourcePath, src), "-->", nBytes, "bytes")
Expand Down

0 comments on commit 6449d7f

Please sign in to comment.