summaryrefslogtreecommitdiff
path: root/lib/btrfs
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-08-18 02:37:23 -0600
committerLuke Shumaker <lukeshu@lukeshu.com>2022-08-18 04:17:35 -0600
commit96051aae4e41df836f956926ed57310f8dad4a5f (patch)
treec9023e5c882602c9b86082d5ab2f3d4f0027dbf1 /lib/btrfs
parentff4435105d35aff994492517a292e60628bce5bf (diff)
Have ExtentCSum use SumRun
Diffstat (limited to 'lib/btrfs')
-rw-r--r--lib/btrfs/btrfsitem/item_extentcsum.go53
-rw-r--r--lib/btrfs/btrfsitem/items.go2
2 files changed, 7 insertions, 48 deletions
diff --git a/lib/btrfs/btrfsitem/item_extentcsum.go b/lib/btrfs/btrfsitem/item_extentcsum.go
index 18ee572..bcfe334 100644
--- a/lib/btrfs/btrfsitem/item_extentcsum.go
+++ b/lib/btrfs/btrfsitem/item_extentcsum.go
@@ -5,73 +5,30 @@
package btrfsitem
import (
- "encoding/hex"
"fmt"
- "io"
-
- "git.lukeshu.com/go/lowmemjson"
"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
+ "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
)
// key.objectid = BTRFS_EXTENT_CSUM_OBJECTID
// key.offset = laddr of checksummed region
type ExtentCSum struct { // EXTENT_CSUM=128
- ChecksumSize int
// Checksum of each sector starting at key.offset
- Sums []btrfssum.CSum
+ btrfssum.SumRun[btrfsvol.LogicalAddr]
}
func (o *ExtentCSum) UnmarshalBinary(dat []byte) (int, error) {
if o.ChecksumSize == 0 {
return 0, fmt.Errorf(".ChecksumSize must be set")
}
- for len(dat) >= o.ChecksumSize {
- var csum btrfssum.CSum
- copy(csum[:], dat[:o.ChecksumSize])
- dat = dat[o.ChecksumSize:]
- o.Sums = append(o.Sums, csum)
- }
- return len(o.Sums) * o.ChecksumSize, nil
+ o.Sums = btrfssum.ShortSum(dat)
+ return len(dat), nil
}
func (o ExtentCSum) MarshalBinary() ([]byte, error) {
if o.ChecksumSize == 0 {
return nil, fmt.Errorf(".ChecksumSize must be set")
}
- var dat []byte
- for _, csum := range o.Sums {
- dat = append(dat, csum[:o.ChecksumSize]...)
- }
- return dat, nil
-}
-
-var (
- _ lowmemjson.Encodable = ExtentCSum{}
-)
-
-func (o ExtentCSum) EncodeJSON(w io.Writer) error {
- if _, err := fmt.Fprintf(w, `{"ChecksumSize":%d,"Sums":[`, o.ChecksumSize); err != nil {
- return err
- }
- for i, sum := range o.Sums {
- if i > 0 {
- if _, err := w.Write([]byte(",")); err != nil {
- return err
- }
- }
- if _, err := w.Write([]byte(`"`)); err != nil {
- return err
- }
- if _, err := hex.NewEncoder(w).Write(sum[:o.ChecksumSize]); err != nil {
- return err
- }
- if _, err := w.Write([]byte(`"`)); err != nil {
- return err
- }
- }
- if _, err := w.Write([]byte(`]}`)); err != nil {
- return err
- }
- return nil
+ return []byte(o.Sums), nil
}
diff --git a/lib/btrfs/btrfsitem/items.go b/lib/btrfs/btrfsitem/items.go
index 769a0a8..35d8b4f 100644
--- a/lib/btrfs/btrfsitem/items.go
+++ b/lib/btrfs/btrfsitem/items.go
@@ -10,6 +10,7 @@ import (
"git.lukeshu.com/btrfs-progs-ng/lib/binstruct"
"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/btrfs/internal"
)
@@ -61,6 +62,7 @@ func UnmarshalItem(key internal.Key, csumType btrfssum.CSumType, dat []byte) Ite
retPtr := reflect.New(gotyp)
if csums, ok := retPtr.Interface().(*ExtentCSum); ok {
csums.ChecksumSize = csumType.Size()
+ csums.Addr = btrfsvol.LogicalAddr(key.Offset)
}
n, err := binstruct.Unmarshal(dat, retPtr.Interface())
if err != nil {