Skip to content

Commit

Permalink
copy file
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed Jun 17, 2024
1 parent 3153ec2 commit d3dfed9
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions cmd/util/cmd/find-trie-root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package find_trie_root
import (
"encoding/hex"
"fmt"
"io"
"math"
"os"

Expand Down Expand Up @@ -63,7 +64,7 @@ func run(*cobra.Command, []string) {
}

if flagExecutionStateDir == flagBackupDir {
log.Fatal().Msg("backup directory cannot be the same as the execution state directory")
log.Fatal().Msg("--backup-dir directory cannot be the same as the execution state directory")
}

segment, offset, err := searchRootHashInSegments(rootHash, flagExecutionStateDir, flagFrom, flagTo)
Expand All @@ -81,7 +82,7 @@ func run(*cobra.Command, []string) {
}

if len(flagBackupDir) == 0 {
log.Error().Msgf("backup directory is not provided")
log.Error().Msgf("--backup-dir directory is not provided")
return
}

Expand Down Expand Up @@ -341,10 +342,36 @@ func backupRollbackedWALsAndMoveLastSegmentFile(

log.Info().Msgf("moving segment file %s to %s", newSegmentFile, segmentToBeReplaced)

err = os.Rename(newSegmentFile, segmentToBeReplaced)
_, err = copyFile(newSegmentFile, segmentToBeReplaced)
if err != nil {
return fmt.Errorf("cannot move segment file %s to %s: %w", newSegmentFile, segmentToBeReplaced, err)
}

return nil
}

func copyFile(src, dst string) (int64, error) {
sourceFile, err := os.Open(src)
if err != nil {
return 0, err
}
defer sourceFile.Close()

destinationFile, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destinationFile.Close()

bytesCopied, err := io.Copy(destinationFile, sourceFile)
if err != nil {
return 0, err
}

err = destinationFile.Sync()
if err != nil {
return 0, err
}

return bytesCopied, nil
}

0 comments on commit d3dfed9

Please sign in to comment.