diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-02 16:42:13 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2022-07-02 17:04:11 -0600 |
commit | cd46b6b734d833a9e2d83ba92e8f7348e63afe7e (patch) | |
tree | 929fc86215381fe43927894d86869e0c17d808c8 /pkg/btrfs/btrfsitem/item_extentcsum.go | |
parent | 9ddcd2c3ed2f85247161c5ffa653f33e6a01a9cd (diff) |
implement EXTENT_CSUM
Diffstat (limited to 'pkg/btrfs/btrfsitem/item_extentcsum.go')
-rw-r--r-- | pkg/btrfs/btrfsitem/item_extentcsum.go | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/pkg/btrfs/btrfsitem/item_extentcsum.go b/pkg/btrfs/btrfsitem/item_extentcsum.go new file mode 100644 index 0000000..27c7c9b --- /dev/null +++ b/pkg/btrfs/btrfsitem/item_extentcsum.go @@ -0,0 +1,39 @@ +package btrfsitem + +import ( + "fmt" + + "lukeshu.com/btrfs-tools/pkg/btrfs/btrfssum" +) + +// 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 +} + +func (o *ExtentCSum) UnmarshalBinary(dat []byte) (int, error) { + if o.ChecksumSize == 0 { + return 0, fmt.Errorf("btrfs.ExtentCSum.UnmarshalBinary: .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 +} + +func (o ExtentCSum) MarshalBinary() ([]byte, error) { + if o.ChecksumSize == 0 { + return nil, fmt.Errorf("btrfs.ExtentCSum.MarshalBinary: .ChecksumSize must be set") + } + var dat []byte + for _, csum := range o.Sums { + dat = append(dat, csum[:o.ChecksumSize]...) + } + return dat, nil +} |