From e4a7aa0742f5070299d37e8421c99d67f0af3f90 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 25 Mar 2026 14:34:50 +0000 Subject: *: object/store -> object/storer --- object/storer/loose/read_bytes.go | 49 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 object/storer/loose/read_bytes.go (limited to 'object/storer/loose/read_bytes.go') diff --git a/object/storer/loose/read_bytes.go b/object/storer/loose/read_bytes.go new file mode 100644 index 00000000..0b6da81b --- /dev/null +++ b/object/storer/loose/read_bytes.go @@ -0,0 +1,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 +} -- cgit v1.3.1-10-gc9f91