Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: move function copyFile into utils.go #424

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 0 additions & 46 deletions cmd/bbolt/surgery_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"

Expand Down Expand Up @@ -64,22 +62,6 @@ func (cmd *surgeryCommand) parsePathsAndCopyFile(fs *flag.FlagSet) error {
return errors.New("output file required")
}

// Ensure source file exists.
_, err := os.Stat(cmd.srcPath)
if os.IsNotExist(err) {
return ErrFileNotFound
} else if err != nil {
return err
}

// Ensure output file not exist.
_, err = os.Stat(cmd.dstPath)
if err == nil {
return fmt.Errorf("output file %q already exists", cmd.dstPath)
} else if !os.IsNotExist(err) {
return err
}

// Copy database from SrcPath to DstPath
if err := copyFile(cmd.srcPath, cmd.dstPath); err != nil {
return fmt.Errorf("failed to copy file: %w", err)
Expand All @@ -88,34 +70,6 @@ func (cmd *surgeryCommand) parsePathsAndCopyFile(fs *flag.FlagSet) error {
return nil
}

func copyFile(srcPath, dstPath string) error {
srcDB, err := os.Open(srcPath)
if err != nil {
return fmt.Errorf("failed to open source file %q: %w", srcPath, err)
}
defer srcDB.Close()
dstDB, err := os.Create(dstPath)
if err != nil {
return fmt.Errorf("failed to create output file %q: %w", dstPath, err)
}
defer dstDB.Close()
written, err := io.Copy(dstDB, srcDB)
if err != nil {
return fmt.Errorf("failed to copy database file from %q to %q: %w", srcPath, dstPath, err)
}

srcFi, err := srcDB.Stat()
if err != nil {
return fmt.Errorf("failed to get source file info %q: %w", srcPath, err)
}
initialSize := srcFi.Size()
if initialSize != written {
return fmt.Errorf("the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)", dstPath, written, srcPath, initialSize)
}

return nil
}

// Usage returns the help message.
func (cmd *surgeryCommand) Usage() string {
return strings.TrimLeft(`
Expand Down
51 changes: 51 additions & 0 deletions cmd/bbolt/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package main

import (
"fmt"
"io"
"os"
)

func copyFile(srcPath, dstPath string) error {
// Ensure source file exists.
_, err := os.Stat(srcPath)
if os.IsNotExist(err) {
return ErrFileNotFound
} else if err != nil {
return err
}

// Ensure output file not exist.
_, err = os.Stat(dstPath)
if err == nil {
return fmt.Errorf("output file %q already exists", dstPath)
} else if !os.IsNotExist(err) {
return err
}

srcDB, err := os.Open(srcPath)
if err != nil {
return fmt.Errorf("failed to open source file %q: %w", srcPath, err)
}
defer srcDB.Close()
dstDB, err := os.Create(dstPath)
if err != nil {
return fmt.Errorf("failed to create output file %q: %w", dstPath, err)
}
defer dstDB.Close()
written, err := io.Copy(dstDB, srcDB)
if err != nil {
return fmt.Errorf("failed to copy database file from %q to %q: %w", srcPath, dstPath, err)
}

srcFi, err := srcDB.Stat()
if err != nil {
return fmt.Errorf("failed to get source file info %q: %w", srcPath, err)
}
initialSize := srcFi.Size()
if initialSize != written {
return fmt.Errorf("the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)", dstPath, written, srcPath, initialSize)
}

return nil
}