| 565 | } |
| 566 | |
| 567 | func copyFile(srcPath, dstPath string) error { |
| 568 | // Ensure source file exists. |
| 569 | _, err := os.Stat(srcPath) |
| 570 | if os.IsNotExist(err) { |
| 571 | return fmt.Errorf("source file %q not found", srcPath) |
| 572 | } else if err != nil { |
| 573 | return err |
| 574 | } |
| 575 | |
| 576 | // Ensure output file not exist. |
| 577 | _, err = os.Stat(dstPath) |
| 578 | if err == nil { |
| 579 | return fmt.Errorf("output file %q already exists", dstPath) |
| 580 | } else if !os.IsNotExist(err) { |
| 581 | return err |
| 582 | } |
| 583 | |
| 584 | srcDB, err := os.Open(srcPath) |
| 585 | if err != nil { |
| 586 | return fmt.Errorf("failed to open source file %q: %w", srcPath, err) |
| 587 | } |
| 588 | defer srcDB.Close() |
| 589 | dstDB, err := os.Create(dstPath) |
| 590 | if err != nil { |
| 591 | return fmt.Errorf("failed to create output file %q: %w", dstPath, err) |
| 592 | } |
| 593 | defer dstDB.Close() |
| 594 | written, err := io.Copy(dstDB, srcDB) |
| 595 | if err != nil { |
| 596 | return fmt.Errorf("failed to copy database file from %q to %q: %w", srcPath, dstPath, err) |
| 597 | } |
| 598 | |
| 599 | srcFi, err := srcDB.Stat() |
| 600 | if err != nil { |
| 601 | return fmt.Errorf("failed to get source file info %q: %w", srcPath, err) |
| 602 | } |
| 603 | initialSize := srcFi.Size() |
| 604 | if initialSize != written { |
| 605 | return fmt.Errorf("the byte copied (%q: %d) isn't equal to the initial db size (%q: %d)", dstPath, written, srcPath, initialSize) |
| 606 | } |
| 607 | |
| 608 | return nil |
| 609 | } |
| 610 | |
| 611 | func persistHistoryRecords(t *testing.T, rs historyRecords, path string) { |
| 612 | recordFilePath := filepath.Join(path, "history_records.json") |