diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-07-23 00:18:47 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-07-23 00:46:29 -0600 |
commit | 6e104326f81ec59ece1817988af41b70e4f4cd15 (patch) | |
tree | 8d22d276e869e5b816e3113def16b3defcbd538c | |
parent | c65d6effc26c3d97a6193f65c5b7698c830d9ff0 (diff) |
rebuildmappings scan: Include the device size and superblock
-rw-r--r-- | cmd/btrfs-rec/inspect/rebuildmappings/scan.go | 13 | ||||
-rw-r--r-- | lib/jsonutil/binstruct.go | 48 |
2 files changed, 56 insertions, 5 deletions
diff --git a/cmd/btrfs-rec/inspect/rebuildmappings/scan.go b/cmd/btrfs-rec/inspect/rebuildmappings/scan.go index d96005e..9454ca0 100644 --- a/cmd/btrfs-rec/inspect/rebuildmappings/scan.go +++ b/cmd/btrfs-rec/inspect/rebuildmappings/scan.go @@ -19,6 +19,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/btrfsutil" "git.lukeshu.com/btrfs-progs-ng/lib/containers" + "git.lukeshu.com/btrfs-progs-ng/lib/jsonutil" "git.lukeshu.com/btrfs-progs-ng/lib/textui" ) @@ -27,6 +28,8 @@ import ( type ScanDevicesResult = map[btrfsvol.DeviceID]ScanOneDeviceResult type ScanOneDeviceResult struct { + Size btrfsvol.PhysicalAddr + Superblock jsonutil.Binary[btrfstree.Superblock] Checksums btrfssum.SumRun[btrfsvol.PhysicalAddr] FoundNodes map[btrfsvol.LogicalAddr][]btrfsvol.PhysicalAddr FoundChunks []FoundChunk @@ -72,7 +75,6 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device) (ScanOneDeviceResult, // scanner implementation ////////////////////////////////////////////////////// type deviceScanner struct { - alg btrfssum.CSumType sums strings.Builder result ScanOneDeviceResult } @@ -104,17 +106,18 @@ func (scanner *deviceScanner) ScanStats() scanStats { } } -func newDeviceScanner(_ context.Context, sb btrfstree.Superblock, _ btrfsvol.PhysicalAddr, numSectors int) btrfsutil.DeviceScanner[scanStats, ScanOneDeviceResult] { +func newDeviceScanner(_ context.Context, sb btrfstree.Superblock, numBytes btrfsvol.PhysicalAddr, numSectors int) btrfsutil.DeviceScanner[scanStats, ScanOneDeviceResult] { scanner := new(deviceScanner) - scanner.alg = sb.ChecksumType + scanner.result.Size = numBytes + scanner.result.Superblock.Val = sb scanner.result.FoundNodes = make(map[btrfsvol.LogicalAddr][]btrfsvol.PhysicalAddr) - scanner.result.Checksums.ChecksumSize = scanner.alg.Size() + scanner.result.Checksums.ChecksumSize = scanner.result.Superblock.Val.ChecksumType.Size() scanner.sums.Grow(scanner.result.Checksums.ChecksumSize * numSectors) return scanner } func (scanner *deviceScanner) ScanSector(_ context.Context, dev *btrfs.Device, paddr btrfsvol.PhysicalAddr) error { - sum, err := btrfs.ChecksumPhysical(dev, scanner.alg, paddr) + sum, err := btrfs.ChecksumPhysical(dev, scanner.result.Superblock.Val.ChecksumType, paddr) if err != nil { return err } diff --git a/lib/jsonutil/binstruct.go b/lib/jsonutil/binstruct.go new file mode 100644 index 0000000..7f4bd3f --- /dev/null +++ b/lib/jsonutil/binstruct.go @@ -0,0 +1,48 @@ +// Copyright (C) 2023 Luke Shumaker <lukeshu@lukeshu.com> +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package jsonutil + +import ( + "bytes" + "fmt" + "io" + + "git.lukeshu.com/go/lowmemjson" + + "git.lukeshu.com/btrfs-progs-ng/lib/binstruct" + "git.lukeshu.com/btrfs-progs-ng/lib/textui" +) + +type Binary[T any] struct { + Val T +} + +var ( + _ lowmemjson.Encodable = Binary[int]{} + _ lowmemjson.Decodable = (*Binary[int])(nil) +) + +func (o Binary[T]) EncodeJSON(w io.Writer) error { + bs, err := binstruct.Marshal(o.Val) + if err != nil { + return err + } + return EncodeSplitHexString(w, bs, textui.Tunable(80)) +} + +func (o *Binary[T]) DecodeJSON(r io.RuneScanner) error { + var buf bytes.Buffer + if err := DecodeSplitHexString(r, &buf); err != nil { + return err + } + n, err := binstruct.Unmarshal(buf.Bytes(), &o.Val) + if err != nil { + return err + } + if n < buf.Len() { + return fmt.Errorf("%d bytes of garbage after value", n-buf.Len()) + } + return nil +} |