restic/cmd/khepri/cmd_fsck.go

124 lines
2.2 KiB
Go
Raw Normal View History

2014-08-04 21:25:58 +00:00
package main
2014-11-30 21:35:42 +00:00
import (
"errors"
"fmt"
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
"github.com/fd0/khepri"
"github.com/fd0/khepri/backend"
)
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
func init() {
commands["fsck"] = commandFsck
}
func fsckFile(ch *khepri.ContentHandler, IDs []backend.ID) error {
for _, id := range IDs {
debug("checking data blob %v\n", id)
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
// load content
_, err := ch.Load(backend.Data, id)
if err != nil {
return err
}
}
return nil
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
func fsckTree(ch *khepri.ContentHandler, id backend.ID) error {
debug("checking tree %v\n", id)
tree, err := khepri.LoadTree(ch, id)
if err != nil {
return err
}
for i, node := range tree {
if node.Name == "" {
return fmt.Errorf("node %v of tree %v has no name", i, id)
}
if node.Type == "" {
return fmt.Errorf("node %q of tree %v has no type", node.Name, id)
}
switch node.Type {
case "file":
if node.Content == nil {
return fmt.Errorf("file node %q of tree %v has no content", node.Name, id)
}
err := fsckFile(ch, node.Content)
if err != nil {
return err
}
case "dir":
if node.Subtree == nil {
return fmt.Errorf("dir node %q of tree %v has no subtree", node.Name, id)
}
err := fsckTree(ch, node.Subtree)
if err != nil {
return err
}
}
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
return nil
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
func fsck_snapshot(be backend.Server, key *khepri.Key, id backend.ID) error {
debug("checking snapshot %v\n", id)
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
ch, err := khepri.NewContentHandler(be, key)
if err != nil {
return err
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
sn, err := ch.LoadSnapshot(id)
if err != nil {
return err
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
if sn.Content == nil {
return fmt.Errorf("snapshot %v has no content", sn.ID)
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
if sn.Map == nil {
return fmt.Errorf("snapshot %v has no map", sn.ID)
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
return fsckTree(ch, sn.Content)
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
func commandFsck(be backend.Server, key *khepri.Key, args []string) error {
if len(args) == 0 {
return errors.New("usage: fsck [all|snapshot-id]")
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
if len(args) == 1 && args[0] != "all" {
snapshotID, err := backend.FindSnapshot(be, args[0])
if err != nil {
return fmt.Errorf("invalid id %q: %v", args[1], err)
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
return fsck_snapshot(be, key, snapshotID)
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
list, err := be.List(backend.Snapshot)
if err != nil {
return err
}
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
for _, snapshotID := range list {
err := fsck_snapshot(be, key, snapshotID)
2014-08-04 21:25:58 +00:00
2014-11-30 21:35:42 +00:00
if err != nil {
return err
}
}
2014-08-04 21:25:58 +00:00
return nil
}