From bfa0a3f5f18b752a6ebd3d5b37411c6871f7bb17 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Wed, 25 Mar 2026 14:30:31 +0000 Subject: *: objectstore -> object/store --- object/store/loose/paths.go | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 object/store/loose/paths.go (limited to 'object/store/loose/paths.go') diff --git a/object/store/loose/paths.go b/object/store/loose/paths.go new file mode 100644 index 00000000..e03f5c28 --- /dev/null +++ b/object/store/loose/paths.go @@ -0,0 +1,43 @@ +package loose + +import ( + "errors" + "fmt" + "io/fs" + "os" + "path/filepath" + + objectid "codeberg.org/lindenii/furgit/object/id" + "codeberg.org/lindenii/furgit/object/store" +) + +// objectPath returns the loose object path for id relative to the objects root. +func (store *Store) objectPath(id objectid.ObjectID) (string, error) { + if id.Algorithm() != store.algo { + return "", fmt.Errorf("objectstore/loose: object id algorithm mismatch: got %s want %s", id.Algorithm(), store.algo) + } + + hex := id.String() + + return filepath.Join(hex[:2], hex[2:]), nil +} + +// openObject opens the loose object file for id. +// Missing files cause objectstore.ErrObjectNotFound. +func (store *Store) openObject(id objectid.ObjectID) (*os.File, error) { + relPath, err := store.objectPath(id) + if err != nil { + return nil, err + } + + file, err := store.root.Open(relPath) + if err != nil { + if errors.Is(err, fs.ErrNotExist) { + return nil, objectstore.ErrObjectNotFound + } + + return nil, err + } + + return file, nil +} -- cgit v1.3.1-10-gc9f91