From 5d2729a4e15dca6e22187cd5de272a2a2d191131 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 10 Mar 2023 19:31:50 -0700 Subject: cmd/btrfs-rec: Move --node-list to be a global flag --- cmd/btrfs-rec/inspect_lstrees.go | 12 +++------ cmd/btrfs-rec/inspect_rebuildtrees.go | 24 +++--------------- cmd/btrfs-rec/main.go | 48 +++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 43 deletions(-) (limited to 'cmd/btrfs-rec') diff --git a/cmd/btrfs-rec/inspect_lstrees.go b/cmd/btrfs-rec/inspect_lstrees.go index cb7bef8..decb75c 100644 --- a/cmd/btrfs-rec/inspect_lstrees.go +++ b/cmd/btrfs-rec/inspect_lstrees.go @@ -25,15 +25,14 @@ import ( ) func init() { - var nodeListFilename string - cmd := &cobra.Command{ + inspectors.AddCommand(&cobra.Command{ Use: "ls-trees", Short: "A brief view what types of items are in each tree", Long: "" + "If no --node-list is given, then a slow sector-by-sector scan " + "will be used to find all lost+found nodes.", Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - RunE: runWithReadableFSAndNodeList(&nodeListFilename, func(fs btrfs.ReadableFS, nodeList []btrfsvol.LogicalAddr, cmd *cobra.Command, _ []string) error { + RunE: runWithReadableFSAndNodeList(func(fs btrfs.ReadableFS, nodeList []btrfsvol.LogicalAddr, cmd *cobra.Command, _ []string) error { ctx := cmd.Context() var treeErrCnt int @@ -113,10 +112,5 @@ func init() { return nil }), - } - cmd.Flags().StringVar(&nodeListFilename, "node-list", "", - "Output of 'btrfs-recs inspect [rebuild-mappings] list-nodes' to use for a lost+found tree") - noError(cmd.MarkFlagFilename("node-list")) - - inspectors.AddCommand(cmd) + }) } diff --git a/cmd/btrfs-rec/inspect_rebuildtrees.go b/cmd/btrfs-rec/inspect_rebuildtrees.go index 676533a..1e808a0 100644 --- a/cmd/btrfs-rec/inspect_rebuildtrees.go +++ b/cmd/btrfs-rec/inspect_rebuildtrees.go @@ -17,13 +17,11 @@ import ( "git.lukeshu.com/btrfs-progs-ng/cmd/btrfs-rec/inspect/rebuildtrees" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" - "git.lukeshu.com/btrfs-progs-ng/lib/btrfsutil" "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) func init() { - var nodeListFilename string - cmd := &cobra.Command{ + inspectors.AddCommand(&cobra.Command{ Use: "rebuild-trees", Long: "" + "Rebuild broken btrees based on missing items that are implied " + @@ -34,20 +32,9 @@ func init() { "If no --node-list is given, then a slow sector-by-sector scan " + "will be used to find all nodes.", Args: cliutil.WrapPositionalArgs(cobra.NoArgs), - RunE: runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + RunE: runWithRawFSAndNodeList(func(fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr, cmd *cobra.Command, args []string) error { ctx := cmd.Context() - var nodeList []btrfsvol.LogicalAddr - var err error - if nodeListFilename != "" { - nodeList, err = readJSONFile[[]btrfsvol.LogicalAddr](ctx, nodeListFilename) - } else { - nodeList, err = btrfsutil.ListNodes(ctx, fs) - } - if err != nil { - return err - } - rebuilder, err := rebuildtrees.NewRebuilder(ctx, fs, nodeList) if err != nil { return err @@ -78,10 +65,5 @@ func init() { return rebuildErr }), - } - cmd.Flags().StringVar(&nodeListFilename, "node-list", "", - "Output of 'btrfs-recs inspect [rebuild-mappings] list-nodes' to use for the node list") - noError(cmd.MarkFlagFilename("node-list")) - - inspectors.AddCommand(cmd) + }) } diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index 8d3193a..da3aced 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -50,6 +50,7 @@ var globalFlags struct { pvs []string mappings string + nodeList string rebuild bool stopProfiling profile.StopFunc @@ -96,6 +97,10 @@ func main() { "load chunk/dev-extent/blockgroup data from external JSON file `mappings.json`") noError(argparser.MarkPersistentFlagFilename("mappings")) + argparser.PersistentFlags().StringVar(&globalFlags.nodeList, "node-list", "", + "load node list (output of 'btrfs-recs inspect [rebuild-mappings] list-nodes') from external JSON file `nodes.json`") + noError(argparser.MarkPersistentFlagFilename("node-list")) + argparser.PersistentFlags().BoolVar(&globalFlags.rebuild, "rebuild", false, "attempt to rebuild broken btrees when reading") @@ -183,34 +188,51 @@ func runWithRawFS(runE func(*btrfs.FS, *cobra.Command, []string) error) func(*co }) } -func runWithReadableFS(runE func(btrfs.ReadableFS, *cobra.Command, []string) error) func(*cobra.Command, []string) error { +func runWithRawFSAndNodeList(runE func(*btrfs.FS, []btrfsvol.LogicalAddr, *cobra.Command, []string) error) func(*cobra.Command, []string) error { return runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { - var rfs btrfs.ReadableFS = fs - if globalFlags.rebuild { - rfs = btrfsutil.NewOldRebuiltForrest(fs) - } - return runE(rfs, cmd, args) - }) -} + ctx := cmd.Context() -func runWithReadableFSAndNodeList(nodeListFilename *string, runE func(btrfs.ReadableFS, []btrfsvol.LogicalAddr, *cobra.Command, []string) error) func(*cobra.Command, []string) error { - return runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { var nodeList []btrfsvol.LogicalAddr var err error - if *nodeListFilename != "" { - nodeList, err = readJSONFile[[]btrfsvol.LogicalAddr](cmd.Context(), *nodeListFilename) + if globalFlags.nodeList != "" { + nodeList, err = readJSONFile[[]btrfsvol.LogicalAddr](ctx, globalFlags.nodeList) } else { - nodeList, err = btrfsutil.ListNodes(cmd.Context(), fs) + nodeList, err = btrfsutil.ListNodes(ctx, fs) } if err != nil { return err } + return runE(fs, nodeList, cmd, args) + }) +} + +func _runWithReadableFS(wantNodeList bool, runE func(btrfs.ReadableFS, []btrfsvol.LogicalAddr, *cobra.Command, []string) error) func(*cobra.Command, []string) error { + inner := func(fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr, cmd *cobra.Command, args []string) error { var rfs btrfs.ReadableFS = fs if globalFlags.rebuild { rfs = btrfsutil.NewOldRebuiltForrest(fs) } return runE(rfs, nodeList, cmd, args) + } + + return func(cmd *cobra.Command, args []string) error { + if wantNodeList { + return runWithRawFSAndNodeList(inner)(cmd, args) + } + return runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { + return inner(fs, nil, cmd, args) + })(cmd, args) + } +} + +func runWithReadableFSAndNodeList(runE func(btrfs.ReadableFS, []btrfsvol.LogicalAddr, *cobra.Command, []string) error) func(*cobra.Command, []string) error { + return _runWithReadableFS(true, runE) +} + +func runWithReadableFS(runE func(btrfs.ReadableFS, *cobra.Command, []string) error) func(*cobra.Command, []string) error { + return _runWithReadableFS(false, func(fs btrfs.ReadableFS, _ []btrfsvol.LogicalAddr, cmd *cobra.Command, args []string) error { + return runE(fs, cmd, args) }) } -- cgit v1.2.3-2-g168b