summaryrefslogtreecommitdiff
path: root/pkg/btrfs/btrfsitem/item_uuid.go
blob: d66e10207f60aeaf2f04b2f8c41c5f70636b79d4 (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
package btrfsitem

import (
	"lukeshu.com/btrfs-tools/pkg/binstruct"
	"lukeshu.com/btrfs-tools/pkg/btrfs/internal"
)

// The Key for this item is a UUID, and the item is a list of
// subvolume IDs (ObjectIDs) that that UUID maps to.
type UUIDMap []internal.ObjID // UUID_SUBVOL=251 UUID_RECEIVED_SUBVOL=252

func (o *UUIDMap) UnmarshalBinary(dat []byte) (int, error) {
	*o = nil
	var n int
	for len(dat) > n {
		var subvolID internal.ObjID
		_n, err := binstruct.Unmarshal(dat[n:], &subvolID)
		n += _n
		if err != nil {
			return n, err
		}
		*o = append(*o, subvolID)
	}
	return n, nil
}

func (o UUIDMap) MarshalBinary() ([]byte, error) {
	var ret []byte
	for _, subvolID := range o {
		bs, err := binstruct.Marshal(subvolID)
		ret = append(ret, bs...)
		if err != nil {
			return ret, err
		}
	}
	return ret, nil
}