aboutsummaryrefslogtreecommitdiff
path: root/format/packfile/ingest/fill.go
diff options
context:
space:
mode:
Diffstat (limited to 'format/packfile/ingest/fill.go')
-rw-r--r--format/packfile/ingest/fill.go44
1 files changed, 44 insertions, 0 deletions
diff --git a/format/packfile/ingest/fill.go b/format/packfile/ingest/fill.go
new file mode 100644
index 00000000..eca4e4d6
--- /dev/null
+++ b/format/packfile/ingest/fill.go
@@ -0,0 +1,44 @@
+package ingest
+
+import (
+ "errors"
+ "fmt"
+ "io"
+)
+
+// fill ensures at least min unread bytes are available in receiver's buffer.
+func (scanner *streamScanner) fill(minLen int) error {
+ if minLen <= 0 {
+ return nil
+ }
+
+ if minLen > len(scanner.buf) {
+ return fmt.Errorf("packfile/ingest: fill(%d) exceeds scanner buffer", minLen)
+ }
+
+ for scanner.n-scanner.off < minLen {
+ err := scanner.flushConsumedPrefix()
+ if err != nil {
+ return err
+ }
+
+ readN, err := scanner.src.Read(scanner.buf[scanner.n:])
+ if readN > 0 {
+ scanner.n += readN
+ }
+
+ if err != nil {
+ if errors.Is(err, io.EOF) && scanner.n-scanner.off >= minLen {
+ return nil
+ }
+
+ return err
+ }
+
+ if readN == 0 {
+ return io.ErrNoProgress
+ }
+ }
+
+ return nil
+}