diff options
-rw-r--r-- | cmd/btrfs-rec/inspect_dbgsums.go | 3 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | lib/btrfs/btrfssum/shortsum.go | 78 | ||||
-rwxr-xr-x | scripts/main.sh | 3 |
5 files changed, 85 insertions, 5 deletions
diff --git a/cmd/btrfs-rec/inspect_dbgsums.go b/cmd/btrfs-rec/inspect_dbgsums.go index e9fadce..8e230f4 100644 --- a/cmd/btrfs-rec/inspect_dbgsums.go +++ b/cmd/btrfs-rec/inspect_dbgsums.go @@ -43,7 +43,8 @@ func init() { return nil, err } var scanResults btrfsinspect.ScanDevicesResult - if err := lowmemjson.DecodeThenEOF(fh, &scanResults); err != nil { + buf := bufio.NewReader(fh) + if err := lowmemjson.DecodeThenEOF(buf, &scanResults); err != nil { return nil, err } _ = fh.Close() @@ -7,7 +7,7 @@ module git.lukeshu.com/btrfs-progs-ng go 1.19 require ( - git.lukeshu.com/go/lowmemjson v0.0.0-20220818070739-fb063e2955de + git.lukeshu.com/go/lowmemjson v0.0.0-20220822033959-325838f35ce9 github.com/datawire/dlib v1.3.0 github.com/datawire/ocibuild v0.0.3-0.20220423003204-fc6a4e9f90dc github.com/davecgh/go-spew v1.1.1 @@ -1,5 +1,5 @@ -git.lukeshu.com/go/lowmemjson v0.0.0-20220818070739-fb063e2955de h1:JfGT4m2u1ME94YQaxqXOSwEQswBxywen/mUqyu60YCw= -git.lukeshu.com/go/lowmemjson v0.0.0-20220818070739-fb063e2955de/go.mod h1:7StdaFpmZNKYJmQ67fGbzcIcnrGjmD54f/2WbeHLaBw= +git.lukeshu.com/go/lowmemjson v0.0.0-20220822033959-325838f35ce9 h1:YSrCKYaOFOtbBP2+rS6X0smKx07VvHTr9N7NgN+u05E= +git.lukeshu.com/go/lowmemjson v0.0.0-20220822033959-325838f35ce9/go.mod h1:7StdaFpmZNKYJmQ67fGbzcIcnrGjmD54f/2WbeHLaBw= github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/datawire/dlib v1.3.0 h1:KkmyXU1kwm3oPBk1ypR70YbcOlEXWzEbx5RE0iRXTGk= github.com/datawire/dlib v1.3.0/go.mod h1:NiGDmetmbkBvtznpWSx6C0vA0s0LK9aHna3LJDqjruk= diff --git a/lib/btrfs/btrfssum/shortsum.go b/lib/btrfs/btrfssum/shortsum.go index 90d2612..2d995b5 100644 --- a/lib/btrfs/btrfssum/shortsum.go +++ b/lib/btrfs/btrfssum/shortsum.go @@ -102,7 +102,7 @@ func (sum *ShortSum) DecodeJSON(r io.RuneScanner) error { type SumRun[Addr btrfsvol.IntAddr[Addr]] struct { // How big a ShortSum is in this Run. - ChecksumSize int + ChecksumSize int `json:",omitempty"` // Base address where this run starts. Addr Addr `json:",omitempty"` // All of the ShortSums in this run, concatenated together. @@ -157,6 +157,11 @@ type SumRunWithGaps[Addr btrfsvol.IntAddr[Addr]] struct { Runs []SumRun[Addr] } +var ( + _ lowmemjson.Encodable = SumRunWithGaps[btrfsvol.LogicalAddr]{} + _ lowmemjson.Decodable = (*SumRunWithGaps[btrfsvol.LogicalAddr])(nil) +) + func (sg SumRunWithGaps[Addr]) NumSums() int { return int(sg.Size / BlockSize) } @@ -192,3 +197,74 @@ func (sg SumRunWithGaps[Addr]) Get(sumIdx int64) (ShortSum, error) { addr := sg.Addr.Add(btrfsvol.AddrDelta(sumIdx) * BlockSize) return sg.SumForAddr(addr) } + +func (sg SumRunWithGaps[Addr]) EncodeJSON(w io.Writer) error { + if _, err := fmt.Fprintf(w, `{"Addr":%d,"Size":%d,"Runs":[`, sg.Addr, sg.Size); err != nil { + return err + } + cur := sg.Addr + for i, run := range sg.Runs { + if i > 0 { + if _, err := w.Write([]byte{','}); err != nil { + return err + } + } + switch { + case run.Addr < cur: + return fmt.Errorf("invalid %T: addr went backwards: %v < %v", sg, run.Addr, cur) + case run.Addr > cur: + if _, err := fmt.Fprintf(w, `{"Gap":%d},`, run.Addr.Sub(cur)); err != nil { + return err + } + fallthrough + default: + if err := lowmemjson.Encode(w, run); err != nil { + return err + } + cur = run.Addr.Add(run.Size()) + } + } + end := sg.Addr.Add(sg.Size) + switch { + case end < cur: + return fmt.Errorf("invalid %T: addr went backwards: %v < %v", sg, end, cur) + case end > cur: + if _, err := fmt.Fprintf(w, `,{"Gap":%d}`, end.Sub(cur)); err != nil { + return err + } + } + if _, err := w.Write([]byte("]}")); err != nil { + return err + } + return nil +} + +func (sg *SumRunWithGaps[Addr]) DecodeJSON(r io.RuneScanner) error { + *sg = SumRunWithGaps[Addr]{} + var name string + return lowmemjson.DecodeObject(r, + func(r io.RuneScanner) error { + return lowmemjson.Decode(r, &name) + }, + func(r io.RuneScanner) error { + switch name { + case "Addr": + return lowmemjson.Decode(r, &sg.Addr) + case "Size": + return lowmemjson.Decode(r, &sg.Size) + case "Runs": + return lowmemjson.DecodeArray(r, func(r io.RuneScanner) error { + var run SumRun[Addr] + if err := lowmemjson.Decode(r, &run); err != nil { + return err + } + if run.ChecksumSize > 0 { + sg.Runs = append(sg.Runs, run) + } + return nil + }) + default: + return fmt.Errorf("unknown key %q", name) + } + }) +} diff --git a/scripts/main.sh b/scripts/main.sh index f357754..a403100 100755 --- a/scripts/main.sh +++ b/scripts/main.sh @@ -20,6 +20,9 @@ mkdir -p "$b.gen" gen $b.gen/0.scandevices.json \ ./btrfs-rec --pv=$b.img \ inspect scandevices +gen $b.gen/1.dbgsums.json \ + ./btrfs-rec --pv=$b.img \ + inspect dbgsums $b.gen/0.scandevices.json # gen $b.gen/1.mappings.json \ # ./btrfs-rec --pv=$b.img \ # inspect rebuild-mappings $b.gen/0.scan-for-nodes.json |