Skip to content

Commit

Permalink
Combine hash calculating into backup step
Browse files Browse the repository at this point in the history
  • Loading branch information
lflare committed Jan 8, 2022
1 parent b1001cb commit bdb05f6
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,17 @@ func Rewrite(path string, info os.FileInfo, err error) error {
}
defer backupFile.Close()

// Copy to backup file
// Copy to backup file while calculating hash
log.Infof("Backing up file '%s'", path)
bar := progressbar.DefaultBytes(info.Size(), "backing up")
_, err = io.Copy(io.MultiWriter(backupFile, bar), file)
if err != nil {
oldHash := sha256.New()
if _, err = io.Copy(io.MultiWriter(backupFile, bar, oldHash), file); err != nil {
return err
}
bar.Finish()
backupFile.Sync()
log.Infof("Backed up file '%s'", path)

// Calculate original hash
oldHash := sha256.New()
if _, err := io.Copy(oldHash, file); err != nil {
return err
}

// Prepare buffer
buf := make([]byte, 2)

Expand Down

0 comments on commit bdb05f6

Please sign in to comment.