aboutsummaryrefslogtreecommitdiff
path: root/fsck.go
blob: 3dbc1ef8ef4e6b25efed9aa5617a8ca1d8afc4a1 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"code.laria.me/petrific/backup"
	"code.laria.me/petrific/objects"
	"flag"
	"os"
)

func Fsck(env *Env, args []string) int {
	flags := flag.NewFlagSet(os.Args[0]+" fsck", flag.ContinueOnError)
	full := flags.Bool("full", false, "also check blobs")

	flags.Usage = subcmdUsage("fsck", "[flags] [object-id]", flags)
	errout := subcmdErrout(env.Log, "fsck")

	if err := flags.Parse(args); err != nil {
		errout(err)
		return 2
	}

	var fsck_id *objects.ObjectId = nil

	if len(flags.Args()) > 0 {
		id, err := objects.ParseObjectId(flags.Args()[0])
		if err != nil {
			env.Log.Error().Printf("Could not parse object id: %s", err)
			return 1
		}

		fsck_id = &id
	}

	env.Log.Debug().Printf("id: %v", fsck_id)

	problems := make(chan backup.FsckProblem)
	var err error
	go func() {
		err = backup.Fsck(env.Store, fsck_id, *full, problems, env.Log)
		close(problems)
	}()

	problems_found := false
	for p := range problems {
		env.Log.Warn().Print(p)
		problems_found = true
	}

	if problems_found {
		env.Log.Error().Print("Problems found. See warnings in the log")
	}

	if err != nil {
		env.Log.Error().Print(err)
	}

	if err != nil || problems_found {
		return 1
	}
	return 0
}