mirror of https://github.com/etcd-io/bbolt.git
move copyFile into internal/common/util.go
Signed-off-by: Benjamin Wang <wachao@vmware.com>pull/450/head
parent
e6563eef17
commit
1776766466
|
@ -57,7 +57,7 @@ func newSurgeryClearPageElementsCommand() *cobra.Command {
|
|||
func surgeryClearPageElementFunc(cmd *cobra.Command, args []string) error {
|
||||
srcDBPath := args[0]
|
||||
|
||||
if err := copyFile(srcDBPath, surgeryTargetDBFilePath); err != nil {
|
||||
if err := common.CopyFile(srcDBPath, surgeryTargetDBFilePath); err != nil {
|
||||
return fmt.Errorf("[clear-page-element] copy file failed: %w", err)
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,7 @@ func newSurgeryFreelistAbandonCommand() *cobra.Command {
|
|||
func surgeryFreelistAbandonFunc(cmd *cobra.Command, args []string) error {
|
||||
srcDBPath := args[0]
|
||||
|
||||
if err := copyFile(srcDBPath, surgeryTargetDBFilePath); err != nil {
|
||||
if err := common.CopyFile(srcDBPath, surgeryTargetDBFilePath); err != nil {
|
||||
return fmt.Errorf("[abandon-freelist] copy file failed: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -64,7 +64,7 @@ func (cmd *surgeryCommand) parsePathsAndCopyFile(fs *flag.FlagSet) error {
|
|||
}
|
||||
|
||||
// Copy database from SrcPath to DstPath
|
||||
if err := copyFile(cmd.srcPath, cmd.dstPath); err != nil {
|
||||
if err := common.CopyFile(cmd.srcPath, cmd.dstPath); err != nil {
|
||||
return fmt.Errorf("failed to copy file: %w", err)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
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
|
||||
}
|
|
@ -2,6 +2,8 @@ package common
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
|
@ -23,3 +25,47 @@ func LoadPage(buf []byte) *Page {
|
|||
func LoadPageMeta(buf []byte) *Meta {
|
||||
return (*Meta)(unsafe.Pointer(&buf[PageHeaderSize]))
|
||||
}
|
||||
|
||||
func CopyFile(srcPath, dstPath string) error {
|
||||
// Ensure source file exists.
|
||||
_, err := os.Stat(srcPath)
|
||||
if os.IsNotExist(err) {
|
||||
return fmt.Errorf("source file %q not found", srcPath)
|
||||
} 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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue