restic/cmd/khepri/cmd_fsck.go

85 lines
1.5 KiB
Go
Raw Normal View History

2014-08-04 21:25:58 +00:00
package main
import (
"encoding/json"
"io/ioutil"
2014-08-04 21:25:58 +00:00
"log"
"github.com/fd0/khepri"
)
func fsck_tree(repo *khepri.Repository, id khepri.ID) (bool, error) {
log.Printf(" checking dir %s", id)
rd, err := repo.Get(khepri.TYPE_BLOB, id)
if err != nil {
return false, err
}
buf, err := ioutil.ReadAll(rd)
2014-08-04 21:25:58 +00:00
tree := &khepri.Tree{}
err = json.Unmarshal(buf, tree)
2014-08-04 21:25:58 +00:00
if err != nil {
return false, err
}
if !id.Equal(khepri.IDFromData(buf)) {
2014-08-04 21:25:58 +00:00
return false, nil
}
return true, nil
}
func fsck_snapshot(repo *khepri.Repository, id khepri.ID) (bool, error) {
log.Printf("checking snapshot %s", id)
sn, err := khepri.LoadSnapshot(repo, id)
if err != nil {
return false, err
}
2014-08-11 20:47:24 +00:00
return fsck_tree(repo, sn.Content)
2014-08-04 21:25:58 +00:00
}
func commandFsck(repo *khepri.Repository, args []string) error {
var snapshots khepri.IDs
var err error
if len(args) != 0 {
snapshots = make(khepri.IDs, 0, len(args))
for _, arg := range args {
id, err := khepri.ParseID(arg)
if err != nil {
log.Fatal(err)
}
snapshots = append(snapshots, id)
}
} else {
snapshots, err = repo.List(khepri.TYPE_REF)
if err != nil {
log.Fatalf("error reading list of snapshot IDs: %v", err)
}
}
log.Printf("checking %d snapshots", len(snapshots))
for _, id := range snapshots {
ok, err := fsck_snapshot(repo, id)
if err != nil {
log.Printf("error checking snapshot %s: %v", id, err)
continue
}
if !ok {
log.Printf("snapshot %s failed", id)
}
}
return nil
}