summaryrefslogtreecommitdiff
path: root/cmd/btrfs-fsck/main.go
blob: 7ab5a4486845972a06e76240bd402ca2a749d50d (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
package main

import (
	"fmt"
	"os"

	"lukeshu.com/btrfs-tools/pkg/btrfs"
)

func main() {
	if err := Main(os.Args[1]); err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}
}

func Main(imgfilename string) (err error) {
	maybeSetErr := func(_err error) {
		if _err != nil && err == nil {
			err = _err
		}
	}

	fh, err := os.OpenFile(imgfilename, os.O_RDWR, 0)
	if err != nil {
		return err
	}
	defer func() {
		maybeSetErr(fh.Close())
	}()
	fs := &btrfs.FS{
		Devices: []*btrfs.Device{
			{
				File: fh,
			},
		},
	}

	superblock, err := pass0(fs)
	if err != nil {
		return err
	}

	foundNodes, err := pass1(fs, superblock)
	if err != nil {
		return err
	}

	pass2(fs, foundNodes)

	return nil
}