File: uuid_tree.go

package info (click to toggle)
golang-github-dennwc-btrfs 0.0~git20240418.0167142-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 312 kB
  • sloc: makefile: 4; sh: 4
file content (49 lines) | stat: -rw-r--r-- 1,273 bytes parent folder | download | duplicates (2)
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
package btrfs

import (
	"encoding/binary"
	"fmt"
	"os"
)

func lookupUUIDSubvolItem(f *os.File, uuid UUID) (objectID, error) {
	return uuidTreeLookupAny(f, uuid, uuidKeySubvol)
}

func lookupUUIDReceivedSubvolItem(f *os.File, uuid UUID) (objectID, error) {
	return uuidTreeLookupAny(f, uuid, uuidKeyReceivedSubvol)
}

func (id UUID) toKey() (objID objectID, off uint64) {
	objID = objectID(binary.LittleEndian.Uint64(id[:8]))
	off = binary.LittleEndian.Uint64(id[8:16])
	return
}

// uuidTreeLookupAny searches uuid tree for a given uuid in specified field.
// It returns ErrNotFound if object was not found.
func uuidTreeLookupAny(f *os.File, uuid UUID, typ treeKeyType) (objectID, error) {
	objId, off := uuid.toKey()
	args := btrfs_ioctl_search_key{
		tree_id:      uuidTreeObjectid,
		min_objectid: objId,
		max_objectid: objId,
		min_type:     typ,
		max_type:     typ,
		min_offset:   off,
		max_offset:   off,
		max_transid:  maxUint64,
		nr_items:     1,
	}
	res, err := treeSearchRaw(f, args)
	if err != nil {
		return 0, err
	} else if len(res) < 1 {
		return 0, ErrNotFound
	}
	out := res[0]
	if len(out.Data) != 8 {
		return 0, fmt.Errorf("btrfs: uuid item with illegal size %d", len(out.Data))
	}
	return objectID(binary.LittleEndian.Uint64(out.Data)), nil
}