summaryrefslogtreecommitdiff
path: root/lib/btrfs/btrfsitem/item_rootref.go
blob: 1ee0ee45a9e548b01a5cf2de265f01e179c1917d (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
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright (C) 2022  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package btrfsitem

import (
	"fmt"

	"git.lukeshu.com/btrfs-progs-ng/lib/binstruct"
	"git.lukeshu.com/btrfs-progs-ng/lib/binstruct/binutil"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/internal"
)

type RootRef struct { // ROOT_REF=156 ROOT_BACKREF=144
	DirID         internal.ObjID `bin:"off=0x00, siz=0x8"`
	Sequence      int64          `bin:"off=0x08, siz=0x8"`
	NameLen       uint16         `bin:"off=0x10, siz=0x2"` // [ignored-when-writing]
	binstruct.End `bin:"off=0x12"`
	Name          []byte `bin:"-"`
}

func (o *RootRef) UnmarshalBinary(dat []byte) (int, error) {
	if err := binutil.NeedNBytes(dat, 0x12); err != nil {
		return 0, err
	}
	n, err := binstruct.UnmarshalWithoutInterface(dat, o)
	if err != nil {
		return n, err
	}
	if o.NameLen > MaxNameLen {
		return 0, fmt.Errorf("maximum name len is %v, but .NameLen=%v",
			MaxNameLen, o.NameLen)
	}
	if err := binutil.NeedNBytes(dat, 0x12+int(o.NameLen)); err != nil {
		return 0, err
	}
	o.Name = dat[n : n+int(o.NameLen)]
	n += int(o.NameLen)
	return n, nil
}

func (o RootRef) MarshalBinary() ([]byte, error) {
	o.NameLen = uint16(len(o.Name))
	dat, err := binstruct.MarshalWithoutInterface(o)
	if err != nil {
		return dat, err
	}
	dat = append(dat, o.Name...)
	return dat, nil
}