summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-08-23 22:20:35 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-08-23 22:20:35 -0600
commit04c042756a3bf3114a3e0a7773d0943528c4d1ab (patch)
treea6376075b6730e6b11d3757f29143065e8fa7a0e /cmd
parentbdbf9c6fa89a0e76b48cc695baeb93652513f34d (diff)
Factor rebuildmappings.MapLogicalSums out of inspect_dbgsums
Diffstat (limited to 'cmd')
-rw-r--r--cmd/btrfs-rec/inspect_dbgsums.go73
1 files changed, 3 insertions, 70 deletions
diff --git a/cmd/btrfs-rec/inspect_dbgsums.go b/cmd/btrfs-rec/inspect_dbgsums.go
index b46a305..b5abf8f 100644
--- a/cmd/btrfs-rec/inspect_dbgsums.go
+++ b/cmd/btrfs-rec/inspect_dbgsums.go
@@ -8,7 +8,6 @@ import (
"bufio"
"fmt"
"os"
- "strings"
"git.lukeshu.com/go/lowmemjson"
"github.com/datawire/dlib/dlog"
@@ -16,9 +15,7 @@ import (
"github.com/spf13/cobra"
"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/maps"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsinspect/rebuildmappings"
)
func init() {
@@ -42,74 +39,10 @@ func init() {
}
dlog.Infof(ctx, "... done reading %q", args[0])
- dlog.Info(ctx, "Mapping the logical address space...")
- type record struct {
- Gen btrfs.Generation
- Sum btrfssum.ShortSum
- }
- addrspace := make(map[btrfsvol.LogicalAddr]record)
- var sumSize int
- for _, devResults := range scanResults {
- sumSize = devResults.Checksums.ChecksumSize
- for _, sumItem := range devResults.FoundExtentCSums {
- _ = sumItem.Sums.Walk(ctx, func(pos btrfsvol.LogicalAddr, sum btrfssum.ShortSum) error {
- new := record{
- Gen: sumItem.Generation,
- Sum: sum,
- }
- if old, ok := addrspace[pos]; ok {
- switch {
- case old.Gen > new.Gen:
- // do nothing
- case old.Gen < new.Gen:
- addrspace[pos] = new
- case old.Gen == new.Gen:
- if old != new {
- dlog.Errorf(ctx, "mismatch of laddr=%v sum: %v != %v", pos, old, new)
- }
- }
- } else {
- addrspace[pos] = new
- }
- return nil
- })
- }
- }
- if len(addrspace) == 0 {
+ flattened := rebuildmappings.MapLogicalSums(ctx, scanResults)
+ if len(flattened.Runs) == 0 {
return fmt.Errorf("no checksums were found in the scan")
}
- dlog.Info(ctx, "... done mapping")
-
- dlog.Info(ctx, "Flattening the map ...")
- var flattened btrfssum.SumRunWithGaps[btrfsvol.LogicalAddr]
- var curAddr btrfsvol.LogicalAddr
- var curSums strings.Builder
- for _, laddr := range maps.SortedKeys(addrspace) {
- if laddr != curAddr+(btrfsvol.LogicalAddr(curSums.Len()/sumSize)*btrfssum.BlockSize) {
- if curSums.Len() > 0 {
- flattened.Runs = append(flattened.Runs, btrfssum.SumRun[btrfsvol.LogicalAddr]{
- ChecksumSize: sumSize,
- Addr: curAddr,
- Sums: btrfssum.ShortSum(curSums.String()),
- })
- }
- curAddr = laddr
- curSums.Reset()
- }
- curSums.WriteString(string(addrspace[laddr].Sum))
- }
- if curSums.Len() > 0 {
- flattened.Runs = append(flattened.Runs, btrfssum.SumRun[btrfsvol.LogicalAddr]{
- ChecksumSize: sumSize,
- Addr: curAddr,
- Sums: btrfssum.ShortSum(curSums.String()),
- })
- }
- flattened.Addr = flattened.Runs[0].Addr
- last := flattened.Runs[len(flattened.Runs)-1]
- end := last.Addr.Add(last.Size())
- flattened.Size = end.Sub(flattened.Addr)
- dlog.Info(ctx, "... done flattening")
dlog.Info(ctx, "Writing addrspace sums to stdout...")
buffer := bufio.NewWriter(os.Stdout)