aboutsummaryrefslogtreecommitdiff
path: root/object/store/loose/read_bytes.go
blob: 0b6da81b2d8087860a97cdc8cbb433f7ca990e30 (about) (plain) (blame)
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 loose

import (
	objectid "codeberg.org/lindenii/furgit/object/id"
	objecttype "codeberg.org/lindenii/furgit/object/type"
)

// readBytesParsed reads, inflates, and parses a loose object in one pass.
// It returns the full raw payload and its parsed type and content.
func (store *Store) readBytesParsed(id objectid.ObjectID) ([]byte, objecttype.Type, []byte, error) {
	file, err := store.openObject(id)
	if err != nil {
		return nil, objecttype.TypeInvalid, nil, err
	}

	defer func() { _ = file.Close() }()

	raw, err := decodeAll(file)
	if err != nil {
		return nil, objecttype.TypeInvalid, nil, err
	}

	ty, content, err := parseRaw(raw)
	if err != nil {
		return nil, objecttype.TypeInvalid, nil, err
	}

	return raw, ty, content, nil
}

// ReadBytesFull reads a full serialized object as "type size\0content".
func (store *Store) ReadBytesFull(id objectid.ObjectID) ([]byte, error) {
	raw, _, _, err := store.readBytesParsed(id)
	if err != nil {
		return nil, err
	}

	return raw, nil
}

// ReadBytesContent reads an object's type and content bytes.
func (store *Store) ReadBytesContent(id objectid.ObjectID) (objecttype.Type, []byte, error) {
	_, ty, content, err := store.readBytesParsed(id)
	if err != nil {
		return objecttype.TypeInvalid, nil, err
	}

	return ty, content, nil
}