diff options
Diffstat (limited to 'cmd/btrfs-rec')
-rw-r--r-- | cmd/btrfs-rec/inspect_dumpsums.go | 35 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_scandevices.go (renamed from cmd/btrfs-rec/inspect_scanfornodes.go) | 34 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect_scanforextents.go | 55 |
3 files changed, 17 insertions, 107 deletions
diff --git a/cmd/btrfs-rec/inspect_dumpsums.go b/cmd/btrfs-rec/inspect_dumpsums.go deleted file mode 100644 index 28ae7ef..0000000 --- a/cmd/btrfs-rec/inspect_dumpsums.go +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (C) 2022 Luke Shumaker <lukeshu@lukeshu.com> -// -// SPDX-License-Identifier: GPL-2.0-or-later - -package main - -import ( - "os" - - "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: "dump-sums", - Short: "Dump a buncha checksums as JSON", - Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { - ctx := cmd.Context() - sums, err := rebuildmappings.SumEverything(ctx, fs) - if err != nil { - return err - } - dlog.Info(ctx, "Writing sums as gob to stdout...") - return rebuildmappings.WriteAllSums(os.Stdout, sums) - }, - }) -} diff --git a/cmd/btrfs-rec/inspect_scanfornodes.go b/cmd/btrfs-rec/inspect_scandevices.go index 13f7760..f5caadb 100644 --- a/cmd/btrfs-rec/inspect_scanfornodes.go +++ b/cmd/btrfs-rec/inspect_scandevices.go @@ -5,11 +5,12 @@ package main import ( + "bufio" "context" - "encoding/json" "os" "sync" + "git.lukeshu.com/go/lowmemjson" "github.com/datawire/dlib/dgroup" "github.com/datawire/dlib/dlog" "github.com/datawire/ocibuild/pkg/cliutil" @@ -23,23 +24,15 @@ import ( func init() { inspectors = append(inspectors, subcommand{ Command: cobra.Command{ - Use: "scan-for-nodes", - Short: "Scan devices for (potentially lost) nodes", - Long: "" + - "The found information is printed as JSON on stdout, and can\n" + - "be read by `btrfs-rec inspect rebuild-mappings`.\n" + - "\n" + - "This information is mostly useful for rebuilding a broken\n" + - "chunk/dev-extent/blockgroup trees, but can also have some\n" + - "minimal utility in repairing other trees.\n" + - "\n" + - "This is very similar the initial scan done by\n" + - "`btrfs rescue chunk-recover`. Like `btrfs rescue chunk-recover`,\n" + - "this is likely probably slow because it reads the entirety of\n" + - "each device.", + Use: "scandevices", Args: cliutil.WrapPositionalArgs(cobra.NoArgs), }, - RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) error { + RunE: func(fs *btrfs.FS, cmd *cobra.Command, _ []string) (err error) { + maybeSetErr := func(_err error) { + if err == nil && _err != nil { + err = _err + } + } ctx := cmd.Context() var resultsMu sync.Mutex @@ -66,7 +59,14 @@ func init() { } dlog.Info(ctx, "Writing scan results to stdout...") - return json.NewEncoder(os.Stdout).Encode(results) + buffer := bufio.NewWriter(os.Stdout) + defer func() { + maybeSetErr(buffer.Flush()) + }() + return lowmemjson.Encode(&lowmemjson.ReEncoder{ + Out: buffer, + Indent: "\t", + }, results) }, }) } diff --git a/cmd/btrfs-rec/inspect_scanforextents.go b/cmd/btrfs-rec/inspect_scanforextents.go deleted file mode 100644 index 9b5ada4..0000000 --- a/cmd/btrfs-rec/inspect_scanforextents.go +++ /dev/null @@ -1,55 +0,0 @@ -// 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 - }, - }) -} |