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

package main

import (
	"os"

	"git.lukeshu.com/go/lowmemjson"
	"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/btrfsutil"
)

func init() {
	inspectors.AddCommand(&cobra.Command{
		Use:   "list-nodes",
		Short: "Scan the filesystem for btree nodes",
		Long: "" +
			"This scans the filesystem sector-by-sector looking for nodes.  " +
			"If you are needing to rebuild the chunk/dev-extent/blockgroup " +
			"trees with `btrfs-rec inspect rebuild-mappings` anyway, you may " +
			"want to instead use `btrfs-rec inspect rebuild-mappings list-nodes` " +
			"to take advantage of the sector-by-sector scan that's already " +
			"performed by `btrfs-rec inspect rebuild-mappings scan`.",
		Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)),
		RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error {
			ctx := cmd.Context()

			nodeList, err := btrfsutil.ListNodes(ctx, fs)
			if err != nil {
				return err
			}

			dlog.Infof(ctx, "Writing nodes to stdout...")
			if err := writeJSONFile(os.Stdout, nodeList, lowmemjson.ReEncoderConfig{
				Indent:                "\t",
				ForceTrailingNewlines: true,
			}); err != nil {
				return err
			}
			dlog.Info(ctx, "... done writing")

			return nil
		}),
	})
}