diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/btrfs/internal/uuid.go | 26 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/csums.go | 7 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/scandevices.go | 15 |
3 files changed, 35 insertions, 13 deletions
diff --git a/lib/btrfs/internal/uuid.go b/lib/btrfs/internal/uuid.go index 3749b1a..96807b6 100644 --- a/lib/btrfs/internal/uuid.go +++ b/lib/btrfs/internal/uuid.go @@ -5,6 +5,7 @@ package internal import ( + "encoding" "encoding/hex" "fmt" "strings" @@ -14,6 +15,13 @@ import ( type UUID [16]byte +var ( + _ fmt.Stringer = UUID{} + _ fmt.Formatter = UUID{} + _ encoding.TextMarshaler = UUID{} + _ encoding.TextUnmarshaler = (*UUID)(nil) +) + func (uuid UUID) String() string { str := hex.EncodeToString(uuid[:]) return strings.Join([]string{ @@ -25,6 +33,20 @@ func (uuid UUID) String() string { }, "-") } +func (uuid UUID) MarshalText() ([]byte, error) { + return []byte(uuid.String()), nil +} + +func (uuid *UUID) UnmarshalText(text []byte) error { + var err error + *uuid, err = ParseUUID(string(text)) + return err +} + +func (uuid UUID) Format(f fmt.State, verb rune) { + fmtutil.FormatByteArrayStringer(uuid, uuid[:], f, verb) +} + func (a UUID) Cmp(b UUID) int { for i := range a { if d := int(a[i]) - int(b[i]); d != 0 { @@ -34,10 +56,6 @@ func (a UUID) Cmp(b UUID) int { return 0 } -func (uuid UUID) Format(f fmt.State, verb rune) { - fmtutil.FormatByteArrayStringer(uuid, uuid[:], f, verb) -} - func ParseUUID(str string) (UUID, error) { var ret UUID j := 0 diff --git a/lib/btrfsprogs/btrfsinspect/csums.go b/lib/btrfsprogs/btrfsinspect/csums.go index 6335cb9..e690d24 100644 --- a/lib/btrfsprogs/btrfsinspect/csums.go +++ b/lib/btrfsprogs/btrfsinspect/csums.go @@ -103,12 +103,9 @@ type SumRun[Addr btrfsvol.IntAddr[Addr]] struct { // How big a ShortSum is in this Run. ChecksumSize int // Base address where this run starts. - Addr Addr + Addr Addr `json:",omitempty"` // All of the ShortSums in this run, concatenated together. - // - // This is a 'string' rather than a 'ShortSum' to make it hard - // to accidentally use it as a single sum. - Sums string + Sums ShortSum } func (run SumRun[Addr]) NumSums() int { diff --git a/lib/btrfsprogs/btrfsinspect/scandevices.go b/lib/btrfsprogs/btrfsinspect/scandevices.go index b0ec0ff..f4da3dc 100644 --- a/lib/btrfsprogs/btrfsinspect/scandevices.go +++ b/lib/btrfsprogs/btrfsinspect/scandevices.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "strings" "sync" "github.com/datawire/dlib/dgroup" @@ -97,7 +98,8 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfs.Superblock) alg := sb.ChecksumType csumSize := alg.Size() numSums := int(devSize / btrfsitem.CSumBlockSize) - sums := make([]byte, numSums*csumSize) + var sums strings.Builder + sums.Grow(numSums * csumSize) lastProgress := -1 progress := func(pos btrfsvol.PhysicalAddr) { @@ -126,9 +128,9 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfs.Superblock) if err != nil { return result, err } - copy(sums[i*csumSize:], sum[:csumSize]) + sums.Write(sum[:csumSize]) - checkForNode := pos >= minNextNode + checkForNode := pos >= minNextNode && pos+btrfsvol.PhysicalAddr(sb.NodeSize) <= devSize if checkForNode { for _, sbAddr := range btrfs.SuperblockAddrs { if sbAddr <= pos && pos < sbAddr+sbSize { @@ -142,7 +144,7 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfs.Superblock) nodeRef, err := btrfs.ReadNode[btrfsvol.PhysicalAddr](dev, sb, pos, nil) if err != nil { if !errors.Is(err, btrfs.ErrNotANode) { - dlog.Infof(ctx, "... dev[%q] error: %v", dev.Name(), err) + dlog.Errorf(ctx, "... dev[%q] error: %v", dev.Name(), err) } } else { result.FoundNodes[nodeRef.Data.Head.Addr] = append(result.FoundNodes[nodeRef.Data.Head.Addr], nodeRef.Addr) @@ -209,5 +211,10 @@ func ScanOneDevice(ctx context.Context, dev *btrfs.Device, sb btrfs.Superblock) } progress(devSize) + result.Checksums = SumRun[btrfsvol.PhysicalAddr]{ + ChecksumSize: csumSize, + Sums: ShortSum(sums.String()), + } + return result, nil } |