-
Notifications
You must be signed in to change notification settings - Fork 649
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mustafa Elbehery <melbeher@redhat.com>
- Loading branch information
Showing
5 changed files
with
116 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
"go.etcd.io/bbolt/internal/guts_cli" | ||
) | ||
|
||
func newCheckCobraCommand() *cobra.Command { | ||
checkCmd := &cobra.Command{ | ||
Use: "check", | ||
Short: "verifies integrity of bbolt database", | ||
Long: strings.TrimLeft(` | ||
usage: bolt check PATH | ||
Check opens a database at PATH and runs an exhaustive check to verify that | ||
all pages are accessible or are marked as freed. It also verifies that no | ||
pages are double referenced. | ||
Verification errors will stream out as they are found and the process will | ||
return after all pages have been checked. | ||
`, "\n"), | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) == 0 || args[0] == "" { | ||
return ErrPathRequired | ||
} | ||
if len(args) > 1 { | ||
return ErrTooManyArgs | ||
} | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return checkFunc(cmd, args[0]) | ||
}, | ||
} | ||
|
||
return checkCmd | ||
} | ||
|
||
func checkFunc(cmd *cobra.Command, dbPath string) error { | ||
if _, err := checkSourceDBPath(dbPath); err != nil { | ||
return err | ||
} | ||
|
||
// Open database. | ||
db, err := bolt.Open(dbPath, 0600, &bolt.Options{ | ||
ReadOnly: true, | ||
PreLoadFreelist: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
defer db.Close() | ||
|
||
// Perform consistency check. | ||
return db.View(func(tx *bolt.Tx) error { | ||
var count int | ||
for err := range tx.Check(bolt.WithKVStringer(CmdKvStringer())) { | ||
fmt.Fprintln(cmd.OutOrStdout(), err) | ||
count++ | ||
} | ||
|
||
// Print summary of errors. | ||
if count > 0 { | ||
fmt.Fprintf(cmd.OutOrStdout(), "%d errors found\n", count) | ||
return guts_cli.ErrCorrupt | ||
} | ||
|
||
// Notify user that database is valid. | ||
fmt.Fprintln(cmd.OutOrStdout(), "OK") | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package main_test | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
main "go.etcd.io/bbolt/cmd/bbolt" | ||
"go.etcd.io/bbolt/internal/btesting" | ||
) | ||
|
||
func TestCheckCommand_Run(t *testing.T) { | ||
db := btesting.MustCreateDB(t) | ||
db.Close() | ||
defer requireDBNoChange(t, dbData(t, db.Path()), db.Path()) | ||
|
||
rootCmd := main.NewRootCommand() | ||
// capture output for assertion | ||
outputBuf := bytes.NewBufferString("") | ||
rootCmd.SetOut(outputBuf) | ||
|
||
rootCmd.SetArgs([]string{ | ||
"check", db.Path(), | ||
}) | ||
err := rootCmd.Execute() | ||
require.NoError(t, err) | ||
|
||
output, err := io.ReadAll(outputBuf) | ||
require.NoError(t, err) | ||
if string(output) != "OK\n" { | ||
t.Fatalf("unexpected stdout:\n\n%s", string(output)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters