aboutsummaryrefslogtreecommitdiff
path: root/objectstore
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 13:38:02 +0800
committerGravatar Runxi Yu2026-02-21 14:28:15 +0800
commit94482cb2c97aa215f83940643c5d4c0933727dcb (patch)
treebee22fa113542abd1b863ee251fdcf0f9bd409b5 /objectstore
parentdiff: Add package-level doc comment (diff)
signatureNo signature
*: Modernize and lint; add CI v0.1.17
Diffstat (limited to 'objectstore')
-rw-r--r--objectstore/loose/read_test.go7
-rw-r--r--objectstore/loose/write_test.go28
-rw-r--r--objectstore/loose/write_writer.go84
-rw-r--r--objectstore/objectstore.go2
-rw-r--r--objectstore/packed/delta_plan.go4
-rw-r--r--objectstore/packed/entry_parse.go10
-rw-r--r--objectstore/packed/idx_load.go8
-rw-r--r--objectstore/packed/idx_parse.go5
-rw-r--r--objectstore/packed/pack.go8
-rw-r--r--objectstore/packed/read_test.go9
10 files changed, 104 insertions, 61 deletions
diff --git a/objectstore/loose/read_test.go b/objectstore/loose/read_test.go
index d125629a..d8166c9e 100644
--- a/objectstore/loose/read_test.go
+++ b/objectstore/loose/read_test.go
@@ -14,7 +14,8 @@ import (
)
func TestLooseStoreReadAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
blobID := testRepo.HashObject(t, "blob", []byte("blob body\n"))
_, treeID, commitID := testRepo.MakeCommit(t, "subject\n\nbody")
@@ -93,7 +94,8 @@ func TestLooseStoreReadAgainstGit(t *testing.T) {
}
func TestLooseStoreErrors(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
store := openLooseStore(t, testRepo.Dir(), algo)
@@ -136,6 +138,7 @@ func TestLooseStoreErrors(t *testing.T) {
}
func TestLooseStoreNewValidation(t *testing.T) {
+ t.Parallel()
root, err := os.OpenRoot(t.TempDir())
if err != nil {
t.Fatalf("OpenRoot: %v", err)
diff --git a/objectstore/loose/write_test.go b/objectstore/loose/write_test.go
index b9a318d2..411868a6 100644
--- a/objectstore/loose/write_test.go
+++ b/objectstore/loose/write_test.go
@@ -12,7 +12,8 @@ import (
)
func TestLooseStoreWriteWriterContentAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
store := openLooseStore(t, testRepo.Dir(), algo)
@@ -68,7 +69,8 @@ func TestLooseStoreWriteWriterContentAgainstGit(t *testing.T) {
}
func TestLooseStoreWriteWriterFullAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
store := openLooseStore(t, testRepo.Dir(), algo)
@@ -108,11 +110,13 @@ func TestLooseStoreWriteWriterFullAgainstGit(t *testing.T) {
}
func TestLooseStoreWriterValidationErrors(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
- testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
- store := openLooseStore(t, testRepo.Dir(), algo)
-
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
t.Run("content overflow", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterContent(objecttype.TypeBlob, 1)
if err != nil {
t.Fatalf("WriteWriterContent: %v", err)
@@ -127,6 +131,10 @@ func TestLooseStoreWriterValidationErrors(t *testing.T) {
})
t.Run("content short", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterContent(objecttype.TypeBlob, 5)
if err != nil {
t.Fatalf("WriteWriterContent: %v", err)
@@ -143,6 +151,10 @@ func TestLooseStoreWriterValidationErrors(t *testing.T) {
})
t.Run("full malformed header", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterFull()
if err != nil {
t.Fatalf("WriteWriterFull: %v", err)
@@ -159,6 +171,10 @@ func TestLooseStoreWriterValidationErrors(t *testing.T) {
})
t.Run("full size mismatch", func(t *testing.T) {
+ t.Parallel()
+ testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: algo, Bare: true})
+ store := openLooseStore(t, testRepo.Dir(), algo)
+
writer, finalize, err := store.WriteWriterFull()
if err != nil {
t.Fatalf("WriteWriterFull: %v", err)
diff --git a/objectstore/loose/write_writer.go b/objectstore/loose/write_writer.go
index abbbae31..e8f03f19 100644
--- a/objectstore/loose/write_writer.go
+++ b/objectstore/loose/write_writer.go
@@ -137,48 +137,6 @@ func (writer *streamWriter) Write(src []byte) (int, error) {
return len(src), nil
}
-// acceptFull validates and accounts raw full-object input.
-func (writer *streamWriter) acceptFull(src []byte) error {
- if !writer.headerDone {
- if nul := bytes.IndexByte(src, 0); nul >= 0 {
- headerChunkLen := nul + 1
- writer.headerBuf = append(writer.headerBuf, src[:headerChunkLen]...)
- _, size, _, ok := objectheader.Parse(writer.headerBuf)
- if !ok {
- return errors.New("objectstore/loose: malformed object header")
- }
- writer.headerDone = true
- writer.expectedContentLeft = size
- return writer.acceptContent(int64(len(src) - headerChunkLen))
- }
-
- writer.headerBuf = append(writer.headerBuf, src...)
- return nil
- }
-
- return writer.acceptContent(int64(len(src)))
-}
-
-// acceptContent validates and accounts content byte counts.
-func (writer *streamWriter) acceptContent(n int64) error {
- if n > writer.expectedContentLeft {
- return errors.New("objectstore/loose: object content exceeds declared size")
- }
- writer.expectedContentLeft -= n
- return nil
-}
-
-// writeRawChunk forwards raw bytes to the hash and deflate pipeline.
-func (writer *streamWriter) writeRawChunk(src []byte) error {
- if _, err := writer.hash.Write(src); err != nil {
- return err
- }
- if _, err := writer.zw.Write(src); err != nil {
- return err
- }
- return nil
-}
-
// Close flushes and closes the underlying zlib stream and temp file.
// It is safe to call multiple times.
func (writer *streamWriter) Close() error {
@@ -263,6 +221,48 @@ func (writer *streamWriter) Finalize() (objectid.ObjectID, error) {
return id, nil
}
+// acceptFull validates and accounts raw full-object input.
+func (writer *streamWriter) acceptFull(src []byte) error {
+ if !writer.headerDone {
+ if nul := bytes.IndexByte(src, 0); nul >= 0 {
+ headerChunkLen := nul + 1
+ writer.headerBuf = append(writer.headerBuf, src[:headerChunkLen]...)
+ _, size, _, ok := objectheader.Parse(writer.headerBuf)
+ if !ok {
+ return errors.New("objectstore/loose: malformed object header")
+ }
+ writer.headerDone = true
+ writer.expectedContentLeft = size
+ return writer.acceptContent(int64(len(src) - headerChunkLen))
+ }
+
+ writer.headerBuf = append(writer.headerBuf, src...)
+ return nil
+ }
+
+ return writer.acceptContent(int64(len(src)))
+}
+
+// acceptContent validates and accounts content byte counts.
+func (writer *streamWriter) acceptContent(n int64) error {
+ if n > writer.expectedContentLeft {
+ return errors.New("objectstore/loose: object content exceeds declared size")
+ }
+ writer.expectedContentLeft -= n
+ return nil
+}
+
+// writeRawChunk forwards raw bytes to the hash and deflate pipeline.
+func (writer *streamWriter) writeRawChunk(src []byte) error {
+ if _, err := writer.hash.Write(src); err != nil {
+ return err
+ }
+ if _, err := writer.zw.Write(src); err != nil {
+ return err
+ }
+ return nil
+}
+
// createTempObjectFile creates a unique temporary object file within dir.
// The returned path is relative to the objects root.
func (store *Store) createTempObjectFile(dir string) (string, *os.File, error) {
diff --git a/objectstore/objectstore.go b/objectstore/objectstore.go
index def71c85..053013ed 100644
--- a/objectstore/objectstore.go
+++ b/objectstore/objectstore.go
@@ -10,7 +10,7 @@ import (
)
// ErrObjectNotFound indicates that an object does not exist in a backend.
-// TODO: This might need to be an interface or otherwise be able to encapsulate multiple concrete backends'
+// TODO: This might need to be an interface or otherwise be able to encapsulate multiple concrete backends'.
var ErrObjectNotFound = errors.New("objectstore: object not found")
// Store reads Git objects by object ID.
diff --git a/objectstore/packed/delta_plan.go b/objectstore/packed/delta_plan.go
index e55400aa..5a989c62 100644
--- a/objectstore/packed/delta_plan.go
+++ b/objectstore/packed/delta_plan.go
@@ -74,6 +74,10 @@ func (store *Store) deltaPlanFor(start location) (deltaPlan, error) {
packName: current.packName,
offset: meta.baseOfs,
}
+ case objecttype.TypeCommit, objecttype.TypeTree, objecttype.TypeBlob, objecttype.TypeTag:
+ return deltaPlan{}, fmt.Errorf("objectstore/packed: internal invariant violation for base type %d", meta.ty)
+ case objecttype.TypeInvalid, objecttype.TypeFuture:
+ return deltaPlan{}, fmt.Errorf("objectstore/packed: unsupported pack type %d", meta.ty)
default:
return deltaPlan{}, fmt.Errorf("objectstore/packed: unsupported pack type %d", meta.ty)
}
diff --git a/objectstore/packed/entry_parse.go b/objectstore/packed/entry_parse.go
index e3cbeac3..76fcb754 100644
--- a/objectstore/packed/entry_parse.go
+++ b/objectstore/packed/entry_parse.go
@@ -3,6 +3,7 @@ package packed
import (
"fmt"
+ "codeberg.org/lindenii/furgit/internal/intconv"
"codeberg.org/lindenii/furgit/objectid"
"codeberg.org/lindenii/furgit/objecttype"
)
@@ -28,7 +29,10 @@ func parseEntryMeta(pack *packFile, algo objectid.Algorithm, offset uint64) (ent
return zero, fmt.Errorf("objectstore/packed: pack %q offset %d out of bounds", pack.name, offset)
}
- pos := int(offset)
+ pos, err := intconv.Uint64ToInt(offset)
+ if err != nil {
+ return zero, fmt.Errorf("objectstore/packed: pack %q offset conversion: %w", pack.name, err)
+ }
first := pack.data[pos]
pos++
@@ -76,6 +80,8 @@ func parseEntryMeta(pack *packFile, algo objectid.Algorithm, offset uint64) (ent
return zero, fmt.Errorf("objectstore/packed: pack %q has invalid ofs-delta base", pack.name)
}
meta.baseOfs = offset - dist
+ case objecttype.TypeInvalid, objecttype.TypeFuture:
+ return zero, fmt.Errorf("objectstore/packed: pack %q has unsupported object type %d", pack.name, meta.ty)
default:
return zero, fmt.Errorf("objectstore/packed: pack %q has unsupported object type %d", pack.name, meta.ty)
}
@@ -111,6 +117,8 @@ func isBaseObjectType(ty objecttype.Type) bool {
switch ty {
case objecttype.TypeCommit, objecttype.TypeTree, objecttype.TypeBlob, objecttype.TypeTag:
return true
+ case objecttype.TypeInvalid, objecttype.TypeFuture, objecttype.TypeOfsDelta, objecttype.TypeRefDelta:
+ return false
default:
return false
}
diff --git a/objectstore/packed/idx_load.go b/objectstore/packed/idx_load.go
index 106701fd..293e005f 100644
--- a/objectstore/packed/idx_load.go
+++ b/objectstore/packed/idx_load.go
@@ -7,6 +7,7 @@ import (
"strings"
"syscall"
+ "codeberg.org/lindenii/furgit/internal/intconv"
"codeberg.org/lindenii/furgit/objectid"
)
@@ -106,7 +107,12 @@ func openIdxFile(root *os.Root, idxName, packName string, algo objectid.Algorith
_ = file.Close()
return nil, fmt.Errorf("objectstore/packed: idx %q has unsupported size", idxName)
}
- data, err := syscall.Mmap(int(file.Fd()), 0, int(size), syscall.PROT_READ, syscall.MAP_PRIVATE)
+ fd, err := intconv.UintptrToInt(file.Fd())
+ if err != nil {
+ _ = file.Close()
+ return nil, err
+ }
+ data, err := syscall.Mmap(fd, 0, int(size), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
_ = file.Close()
return nil, err
diff --git a/objectstore/packed/idx_parse.go b/objectstore/packed/idx_parse.go
index a6adc721..0af72594 100644
--- a/objectstore/packed/idx_parse.go
+++ b/objectstore/packed/idx_parse.go
@@ -62,10 +62,7 @@ func (index *idxFile) parse() error {
return fmt.Errorf("objectstore/packed: idx %q has malformed 64-bit offset table", index.idxName)
}
index.offset64Count = offset64Bytes / 8
- maxOffset64Count := index.numObjects - 1
- if maxOffset64Count < 0 {
- maxOffset64Count = 0
- }
+ maxOffset64Count := max(index.numObjects-1, 0)
if index.offset64Count > maxOffset64Count {
return fmt.Errorf("objectstore/packed: idx %q has oversized 64-bit offset table", index.idxName)
}
diff --git a/objectstore/packed/pack.go b/objectstore/packed/pack.go
index 46eca524..00950159 100644
--- a/objectstore/packed/pack.go
+++ b/objectstore/packed/pack.go
@@ -5,6 +5,8 @@ import (
"fmt"
"os"
"syscall"
+
+ "codeberg.org/lindenii/furgit/internal/intconv"
)
const packSignature = 0x5041434b
@@ -27,7 +29,11 @@ func openPackFile(name string, file *os.File, size int64) (*packFile, error) {
if size > int64(int(^uint(0)>>1)) {
return nil, fmt.Errorf("objectstore/packed: pack %q has unsupported size", name)
}
- data, err := syscall.Mmap(int(file.Fd()), 0, int(size), syscall.PROT_READ, syscall.MAP_PRIVATE)
+ fd, err := intconv.UintptrToInt(file.Fd())
+ if err != nil {
+ return nil, err
+ }
+ data, err := syscall.Mmap(fd, 0, int(size), syscall.PROT_READ, syscall.MAP_PRIVATE)
if err != nil {
return nil, err
}
diff --git a/objectstore/packed/read_test.go b/objectstore/packed/read_test.go
index 0eb78366..9a7f2e4a 100644
--- a/objectstore/packed/read_test.go
+++ b/objectstore/packed/read_test.go
@@ -14,12 +14,12 @@ import (
)
func TestPackedStoreReadAgainstGit(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo, ids := createPackedFixtureRepo(t, algo)
store := openPackedStore(t, testRepo.Dir(), algo)
for _, id := range ids {
- id := id
t.Run(id.String(), func(t *testing.T) {
wantType, wantBody, wantRaw := expectedRawObject(t, testRepo, id)
@@ -80,7 +80,8 @@ func TestPackedStoreReadAgainstGit(t *testing.T) {
}
func TestPackedStoreErrors(t *testing.T) {
- testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) {
+ t.Parallel()
+ testgit.ForEachAlgorithm(t, func(t *testing.T, algo objectid.Algorithm) { //nolint:thelper
testRepo, _ := createPackedFixtureRepo(t, algo)
store := openPackedStore(t, testRepo.Dir(), algo)
@@ -125,6 +126,7 @@ func TestPackedStoreErrors(t *testing.T) {
}
func TestPackedStoreNewValidation(t *testing.T) {
+ t.Parallel()
testRepo, _ := createPackedFixtureRepo(t, objectid.AlgorithmSHA1)
store := openPackedStore(t, testRepo.Dir(), objectid.AlgorithmSHA1)
if err := store.Close(); err != nil {
@@ -136,6 +138,7 @@ func TestPackedStoreNewValidation(t *testing.T) {
}
func TestPackedStoreInvalidAlgorithm(t *testing.T) {
+ t.Parallel()
testRepo := testgit.NewRepo(t, testgit.RepoOptions{ObjectFormat: objectid.AlgorithmSHA1, Bare: true})
root, err := os.OpenRoot(testRepo.Dir())
if err != nil {