summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-08-23 20:28:30 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-08-23 20:29:25 -0600
commit9c46c8d8b447ff5e0a5ee688b15ef23034e6ce30 (patch)
treedd9be19efb43ea7fd245b46177dad7c7669bf658 /cmd
parentaaa85c40b2f8c5c4d107aaf3dee47901342fac78 (diff)
Work on the new rebuildmappings
rebuildmappings.go didn't actually change very much, but was reworked to be multi-device, so the diff looks like it totally changed.
Diffstat (limited to 'cmd')
-rw-r--r--cmd/btrfs-rec/inspect_dbgsums.go18
-rw-r--r--cmd/btrfs-rec/inspect_rebuildmappings.go62
-rw-r--r--cmd/btrfs-rec/inspect_scandevices.go48
3 files changed, 57 insertions, 71 deletions
diff --git a/cmd/btrfs-rec/inspect_dbgsums.go b/cmd/btrfs-rec/inspect_dbgsums.go
index 8e230f4..b46a305 100644
--- a/cmd/btrfs-rec/inspect_dbgsums.go
+++ b/cmd/btrfs-rec/inspect_dbgsums.go
@@ -18,7 +18,6 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
"git.lukeshu.com/btrfs-progs-ng/lib/maps"
)
@@ -37,19 +36,10 @@ func init() {
ctx := cmd.Context()
dlog.Infof(ctx, "Reading %q...", args[0])
- scanResults, err := func() (btrfsinspect.ScanDevicesResult, error) {
- fh, err := os.Open(args[0])
- if err != nil {
- return nil, err
- }
- var scanResults btrfsinspect.ScanDevicesResult
- buf := bufio.NewReader(fh)
- if err := lowmemjson.DecodeThenEOF(buf, &scanResults); err != nil {
- return nil, err
- }
- _ = fh.Close()
- return scanResults, nil
- }()
+ scanResults, err := readScanResults(args[0])
+ if err != nil {
+ return err
+ }
dlog.Infof(ctx, "... done reading %q", args[0])
dlog.Info(ctx, "Mapping the logical address space...")
diff --git a/cmd/btrfs-rec/inspect_rebuildmappings.go b/cmd/btrfs-rec/inspect_rebuildmappings.go
index 5b252d5..ce90139 100644
--- a/cmd/btrfs-rec/inspect_rebuildmappings.go
+++ b/cmd/btrfs-rec/inspect_rebuildmappings.go
@@ -5,19 +5,17 @@
package main
import (
- "encoding/json"
- "fmt"
+ "bufio"
"io"
"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/btrfs/btrfsvol"
- "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect"
- "git.lukeshu.com/btrfs-progs-ng/lib/maps"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildmappings"
)
func init() {
@@ -38,25 +36,15 @@ func init() {
RunE: func(fs *btrfs.FS, cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
- scanResultsBytes, err := os.ReadFile(args[0])
+ dlog.Infof(ctx, "Reading %q...", args[0])
+ scanResults, err := readScanResults(args[0])
if err != nil {
return err
}
- var scanResults map[btrfsvol.DeviceID]btrfsinspect.ScanOneDeviceResult
- if err := json.Unmarshal(scanResultsBytes, &scanResults); err != nil {
- return err
- }
+ dlog.Infof(ctx, "... done reading %q", args[0])
- devices := fs.LV.PhysicalVolumes()
- for _, devID := range maps.SortedKeys(scanResults) {
- dev, ok := devices[devID]
- if !ok {
- return fmt.Errorf("device ID %v mentioned in %q is not part of the filesystem",
- devID, args[0])
- }
- dlog.Infof(ctx, "Rebuilding mappings from results on device %v...",
- dev.Name())
- scanResults[devID].AddToLV(ctx, fs, dev)
+ if err := rebuildmappings.RebuildMappings(ctx, fs, scanResults); err != nil {
+ return err
}
dlog.Infof(ctx, "Writing reconstructed mappings to stdout...")
@@ -69,26 +57,18 @@ func init() {
})
}
-func writeMappingsJSON(w io.Writer, fs *btrfs.FS) error {
- mappings := fs.LV.Mappings()
- if _, err := io.WriteString(w, "[\n"); err != nil {
- return err
- }
- for i, mapping := range mappings {
- suffix := ","
- if i == len(mappings)-1 {
- suffix = ""
+func writeMappingsJSON(w io.Writer, fs *btrfs.FS) (err error) {
+ buffer := bufio.NewWriter(w)
+ defer func() {
+ if _err := buffer.Flush(); err == nil && _err != nil {
+ err = _err
}
- bs, err := json.Marshal(mapping)
- if err != nil {
- return err
- }
- if _, err := fmt.Printf(" %s%s\n", bs, suffix); err != nil {
- return err
- }
- }
- if _, err := io.WriteString(w, "]\n"); err != nil {
- return err
- }
- return nil
+ }()
+ return lowmemjson.Encode(&lowmemjson.ReEncoder{
+ Out: buffer,
+
+ Indent: "\t",
+ ForceTrailingNewlines: true,
+ CompactIfUnder: 120,
+ }, fs.LV.Mappings())
}
diff --git a/cmd/btrfs-rec/inspect_scandevices.go b/cmd/btrfs-rec/inspect_scandevices.go
index a7a7fb2..37bafe4 100644
--- a/cmd/btrfs-rec/inspect_scandevices.go
+++ b/cmd/btrfs-rec/inspect_scandevices.go
@@ -6,6 +6,7 @@ package main
import (
"bufio"
+ "io"
"os"
"git.lukeshu.com/go/lowmemjson"
@@ -24,11 +25,6 @@ func init() {
Args: cliutil.WrapPositionalArgs(cobra.NoArgs),
},
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()
results, err := btrfsinspect.ScanDevices(ctx, fs)
@@ -37,17 +33,37 @@ func init() {
}
dlog.Info(ctx, "Writing scan results to stdout...")
- buffer := bufio.NewWriter(os.Stdout)
- defer func() {
- maybeSetErr(buffer.Flush())
- }()
- return lowmemjson.Encode(&lowmemjson.ReEncoder{
- Out: buffer,
-
- Indent: "\t",
- ForceTrailingNewlines: true,
- CompactIfUnder: 16,
- }, results)
+ return writeScanResults(os.Stdout, results)
},
})
}
+
+func writeScanResults(w io.Writer, results btrfsinspect.ScanDevicesResult) (err error) {
+ buffer := bufio.NewWriter(w)
+ defer func() {
+ if _err := buffer.Flush(); err == nil && _err != nil {
+ err = _err
+ }
+ }()
+ return lowmemjson.Encode(&lowmemjson.ReEncoder{
+ Out: buffer,
+
+ Indent: "\t",
+ ForceTrailingNewlines: true,
+ CompactIfUnder: 16,
+ }, results)
+}
+
+func readScanResults(filename string) (btrfsinspect.ScanDevicesResult, error) {
+ fh, err := os.Open(filename)
+ if err != nil {
+ return nil, err
+ }
+ var scanResults btrfsinspect.ScanDevicesResult
+ buf := bufio.NewReader(fh)
+ if err := lowmemjson.DecodeThenEOF(buf, &scanResults); err != nil {
+ return nil, err
+ }
+ _ = fh.Close()
+ return scanResults, nil
+}