aboutsummaryrefslogtreecommitdiff
path: root/objectstore/packed/entry_inflate.go
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 05:35:12 +0800
committerGravatar Runxi Yu2026-02-21 11:15:18 +0800
commitae879b8cf5a87199802a33d6b15c76afafa8002b (patch)
treea93e9486a9610b78823e157c68b75e0724366217 /objectstore/packed/entry_inflate.go
parentcache/lru: Add basic LRU (diff)
signatureNo signature
objectstore/packed: Add initial pack reading support
Diffstat (limited to 'objectstore/packed/entry_inflate.go')
-rw-r--r--objectstore/packed/entry_inflate.go41
1 files changed, 41 insertions, 0 deletions
diff --git a/objectstore/packed/entry_inflate.go b/objectstore/packed/entry_inflate.go
new file mode 100644
index 00000000..5f1c476e
--- /dev/null
+++ b/objectstore/packed/entry_inflate.go
@@ -0,0 +1,41 @@
+package packed
+
+import (
+ "bytes"
+ "compress/zlib"
+ "fmt"
+ "io"
+)
+
+// zlibReaderAt opens a zlib reader starting at data offset within pack.
+func zlibReaderAt(pack *packFile, offset int) (io.ReadCloser, error) {
+ if offset < 0 || offset > len(pack.data) {
+ return nil, fmt.Errorf("objectstore/packed: pack %q zlib offset out of bounds", pack.name)
+ }
+ return zlib.NewReader(bytes.NewReader(pack.data[offset:]))
+}
+
+// inflateAt inflates one entry payload from data offset.
+//
+// When expectedSize is non-negative, the inflated length must match.
+func inflateAt(pack *packFile, offset int, expectedSize int64) ([]byte, error) {
+ reader, err := zlibReaderAt(pack, offset)
+ if err != nil {
+ return nil, err
+ }
+ defer func() { _ = reader.Close() }()
+
+ body, err := io.ReadAll(reader)
+ if err != nil {
+ return nil, err
+ }
+ if expectedSize >= 0 && int64(len(body)) != expectedSize {
+ return nil, fmt.Errorf(
+ "objectstore/packed: pack %q inflated size mismatch: got %d want %d",
+ pack.name,
+ len(body),
+ expectedSize,
+ )
+ }
+ return body, nil
+}