aboutsummaryrefslogtreecommitdiff
path: root/objectstore/loose/paths.go
blob: 04730bd312f0424baf19acb987337ee43c6053b9 (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
package loose

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
	"path/filepath"

	"codeberg.org/lindenii/furgit/objectid"
	"codeberg.org/lindenii/furgit/objectstore"
)

// 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
}