-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.go
66 lines (52 loc) · 1.32 KB
/
io.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
59
60
61
62
63
64
65
66
package filecache
import (
"context"
"fmt"
"io"
"os"
)
const filesMode os.FileMode = 0644
func create(key string, path string) (*os.File, error) {
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, filesMode)
if err != nil {
return nil, fmt.Errorf("failed to create file for cache key %s: %w", key, err)
}
return f, nil
}
type readerFunc func(p []byte) (n int, err error)
func (rf readerFunc) Read(p []byte) (n int, err error) { return rf(p) }
// copyWithCtx is a wrapper for the io.copyWithCtx function with context handling.
func copyWithCtx(ctx context.Context, dst io.Writer, src io.Reader) (readN int64, err error) {
if err := ctx.Err(); err != nil {
return 0, err
}
return io.Copy(dst, readerFunc(func(p []byte) (int, error) {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
return src.Read(p)
}
}))
}
// readAll is an alias for an io.ReadAll, but with context cancel respect.
func readAll(ctx context.Context, r io.Reader) ([]byte, error) {
b := make([]byte, 0, 512)
for {
if err := ctx.Err(); err != nil {
return nil, err
}
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
n, err := r.Read(b[len(b):cap(b)])
b = b[:len(b)+n]
if err != nil {
if err == io.EOF {
err = nil
}
return b, err
}
}
}