-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob-reader.go
58 lines (46 loc) · 858 Bytes
/
blob-reader.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"io"
"net/http"
"os"
)
type blobReader struct {
s *blobState
f *os.File
pos uint64
}
func (s *blobState) Reader() (io.ReadCloser, error) {
path := blobPath(s.digest)
if s.fetching {
path += ".part"
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
return &blobReader{s: s, f: f, pos: 0}, nil
}
func (r *blobReader) Read(b []byte) (n int, err error) {
if r.pos == r.s.length {
return 0, io.EOF
}
r.s.c.L.Lock()
for r.pos >= r.s.fetchPos && r.s.code == 200 {
r.s.c.Wait()
}
r.s.c.L.Unlock()
if r.s.code != 200 {
return 0, fmt.Errorf("fetch failed: %s", http.StatusText(r.s.code))
}
n, err = r.f.Read(b)
r.pos += uint64(n)
if err == io.EOF && r.pos < r.s.length {
// there will be more
err = nil
}
return
}
func (r *blobReader) Close() error {
return r.f.Close()
}