aboutsummaryrefslogtreecommitdiff
path: root/cmd/bolt/check.go
blob: 1436aba9149ab27f9c9b03226cd19a987c85de45 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package main

import (
	"os"

	"github.com/boltdb/bolt"
)

// Check performs a consistency check on the database and prints any errors found.
func Check(path string) {
	if _, err := os.Stat(path); os.IsNotExist(err) {
		fatal(err)
		return
	}

	db, err := bolt.Open(path, 0600)
	if err != nil {
		fatal(err)
		return
	}
	defer db.Close()

	// Perform consistency check.
	err = db.View(func(tx *bolt.Tx) error {
		return tx.Check()
	})

	// Print out any errors that occur.
	if err != nil {
		if errors, ok := err.(bolt.ErrorList); ok {
			for _, err := range errors {
				println(err)
			}
		}
		fatalln(err)
		return
	}
	println("OK")
}