aboutsummaryrefslogtreecommitdiff
path: root/objectstore/loose/paths.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 03:46:50 +0800
committerGravatar Runxi Yu2026-02-21 03:46:50 +0800
commit27ee98e7a9a8693db59e755739e10c1c1ba852b4 (patch)
tree2b69d6912851d36b06c0a795391120d204605ffe /objectstore/loose/paths.go
parentobjectstore: ReadReaderContent should have an advisory declared length (diff)
signatureNo signature
objectstore/loose: Add loose backend
Diffstat (limited to 'objectstore/loose/paths.go')
-rw-r--r--objectstore/loose/paths.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/objectstore/loose/paths.go b/objectstore/loose/paths.go
new file mode 100644
index 00000000..ee761f39
--- /dev/null
+++ b/objectstore/loose/paths.go
@@ -0,0 +1,41 @@
+package loose
+
+import (
+ "errors"
+ "fmt"
+ "io/fs"
+ "os"
+ "path"
+
+ "codeberg.org/lindenii/furgit/objectid"
+ "codeberg.org/lindenii/furgit/objectstore"
+)
+
+// objectPath returns the loose object path for id.
+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()
+ if len(hex) != store.algo.HexLen() {
+ return "", fmt.Errorf("objectstore/loose: malformed object id %q", hex)
+ }
+ return path.Join("objects", 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
+}