summaryrefslogtreecommitdiff
path: root/cmd/btrfs-rec/inspect_scanforextents.go
blob: 9b5ada41f0eadbb386b9647a0d58c5e15fbadc22 (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
// Copyright (C) 2022  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package main

import (
	"os"
	"runtime"

	"github.com/datawire/dlib/dlog"
	"github.com/datawire/ocibuild/pkg/cliutil"
	"github.com/spf13/cobra"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildmappings"
)

func init() {
	inspectors = append(inspectors, subcommand{
		Command: cobra.Command{
			Use:  "scan-for-extents NODESCAN.json DUMPSUMS.gob",
			Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(2)),
		},
		RunE: func(fs *btrfs.FS, cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			dlog.Infof(ctx, "Reading %q...", args[0])
			bgs, err := rebuildmappings.ReadNodeScanResults(fs, args[0])
			if err != nil {
				return err
			}
			runtime.GC()
			dlog.Infof(ctx, "... done reading %q", args[0])

			dlog.Infof(ctx, "Reading %q...", args[1])
			sums, err := rebuildmappings.ReadAllSums(args[1])
			if err != nil {
				return err
			}
			dlog.Infof(ctx, "... done reading %q", args[1])

			if err := rebuildmappings.ScanForExtents(ctx, fs, bgs, sums); err != nil {
				return err
			}

			dlog.Infof(ctx, "Writing reconstructed mappings to stdout...")
			if err := writeMappingsJSON(os.Stdout, fs); err != nil {
				return err
			}

			return nil
		},
	})
}