summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem/item_extentcsum.go
blob: a963c16be5de2c291fb9ea4eefe80b7c94c3ee5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (C) 2022-2023  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfsitem

import (
	"fmt"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
)

// An ExtentCSum checksums regions of the logical address space.
//
// Key:
//
//	key.objectid = BTRFS_EXTENT_CSUM_OBJECTID
//	key.offset   = laddr of checksummed region
type ExtentCSum struct { // trivial EXTENT_CSUM=128
	// Checksum of each sector starting at key.offset
	btrfssum.SumRun[btrfsvol.LogicalAddr]
}

func (o *ExtentCSum) UnmarshalBinary(dat []byte) (int, error) {
	if o.ChecksumSize == 0 {
		return 0, fmt.Errorf(".ChecksumSize must be set")
	}
	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")
	}
	return []byte(o.Sums), nil
}