From 4264394fae41b08026a187b7da9e9787927863ab Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 10 Mar 2023 19:31:50 -0700 Subject: cmd/btrfs-rec: main.go: Don't let lines get too long --- cmd/btrfs-rec/main.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'cmd/btrfs-rec/main.go') diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index d221c3d..e6305da 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -86,10 +86,12 @@ func main() { globalFlags.logLevel.Level = dlog.LogLevelInfo argparser.PersistentFlags().Var(&globalFlags.logLevel, "verbosity", "set the verbosity") - argparser.PersistentFlags().StringArrayVar(&globalFlags.pvs, "pv", nil, "open the file `physical_volume` as part of the filesystem") + argparser.PersistentFlags().StringArrayVar(&globalFlags.pvs, "pv", nil, + "open the file `physical_volume` as part of the filesystem") noError(argparser.MarkPersistentFlagFilename("pv")) - argparser.PersistentFlags().StringVar(&globalFlags.mappings, "mappings", "", "load chunk/dev-extent/blockgroup data from external JSON file `mappings.json`") + argparser.PersistentFlags().StringVar(&globalFlags.mappings, "mappings", "", + "load chunk/dev-extent/blockgroup data from external JSON file `mappings.json`") noError(argparser.MarkPersistentFlagFilename("mappings")) globalFlags.stopProfiling = profile.AddProfileFlags(argparser.PersistentFlags(), "profile.") -- cgit v1.2.3-2-g168b From 6b8c24fb78f0face6532c1170ff0dca46dd56223 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 1 Apr 2023 21:10:35 -0600 Subject: cmd/btrfs-rec: Have RebuiltForrest be opt-in on the CLI --- cmd/btrfs-rec/main.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'cmd/btrfs-rec/main.go') diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index e6305da..49a9649 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -48,7 +48,9 @@ var ( var globalFlags struct { logLevel textui.LogLevelFlag pvs []string + mappings string + rebuild bool stopProfiling profile.StopFunc @@ -94,6 +96,9 @@ func main() { "load chunk/dev-extent/blockgroup data from external JSON file `mappings.json`") noError(argparser.MarkPersistentFlagFilename("mappings")) + argparser.PersistentFlags().BoolVar(&globalFlags.rebuild, "rebuild", false, + "attempt to rebuild broken btrees when reading") + globalFlags.stopProfiling = profile.AddProfileFlags(argparser.PersistentFlags(), "profile.") globalFlags.openFlag = os.O_RDONLY @@ -180,6 +185,10 @@ 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 { return runWithRawFS(func(fs *btrfs.FS, cmd *cobra.Command, args []string) error { - return runE(btrfsutil.NewOldRebuiltForrest(fs), cmd, args) + var rfs btrfs.ReadableFS = fs + if globalFlags.rebuild { + rfs = btrfsutil.NewOldRebuiltForrest(fs) + } + return runE(rfs, cmd, args) }) } -- cgit v1.2.3-2-g168b From bc94e6dbf6e9212820615a3d3f3e4542693351a6 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 10 Mar 2023 19:31:50 -0700 Subject: cmd/btrfs-rec: ls-trees: Use runWithReadableFS --- cmd/btrfs-rec/main.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'cmd/btrfs-rec/main.go') diff --git a/cmd/btrfs-rec/main.go b/cmd/btrfs-rec/main.go index 49a9649..8d3193a 100644 --- a/cmd/btrfs-rec/main.go +++ b/cmd/btrfs-rec/main.go @@ -192,3 +192,25 @@ func runWithReadableFS(runE func(btrfs.ReadableFS, *cobra.Command, []string) err return runE(rfs, cmd, args) }) } + +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) + } else { + nodeList, err = btrfsutil.ListNodes(cmd.Context(), fs) + } + if err != nil { + return err + } + + var rfs btrfs.ReadableFS = fs + if globalFlags.rebuild { + rfs = btrfsutil.NewOldRebuiltForrest(fs) + } + + return runE(rfs, nodeList, cmd, args) + }) +} -- cgit v1.2.3-2-g168b 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/main.go | 48 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 13 deletions(-) (limited to 'cmd/btrfs-rec/main.go') 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