Repository navigation
Expand file tree
/
Copy pathfetch.go
More file actions
288 lines (233 loc) · 7.54 KB
/
Copy pathfetch.go
File metadata and controls
288 lines (233 loc) · 7.54 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Package fetch downloads one zip archive from the first of several hosts to
// respond, verifies its SHA-256 before extraction, and lands the extracted
// tree in the destination directory with one atomic rename.
//
// The package knows nothing about what the archive holds: URL templates,
// platform names and version pins belong to the caller.
package fetch
import (
"context"
"encoding/hex"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gofrs/flock"
)
// Logger receives one line per event: the host chosen, download progress,
// verification and extraction. A *log.Logger satisfies it.
type Logger interface {
Println(v ...any)
}
// HashMismatchError reports an archive whose SHA-256 differs from the one
// given with WithSHA256. Nothing was extracted from it.
type HashMismatchError struct {
URL string
Expected string
Actual string
}
func (e *HashMismatchError) Error() string {
return fmt.Sprintf("sha256 mismatch for %s: expected %s, got %s", e.URL, e.Expected, e.Actual)
}
// sha256Len is the length of a SHA-256 digest in bytes.
const sha256Len = 32
// lockRetry is how often a fetcher waiting for another's lock checks again.
const lockRetry = 50 * time.Millisecond
type quiet struct{}
func (quiet) Println(...any) {}
// fetcher is what the options build: how to reach the hosts, what to verify
// against, how to shape the tree and where to report.
type fetcher struct {
digest string
strip bool
client *http.Client
logger Logger
}
// Option configures Zip.
type Option func(*fetcher)
// WithSHA256 sets the expected hex digest of the archive. The download is
// hashed as it streams and compared before the archive is opened; a mismatch
// fails with a *HashMismatchError. An empty digest means no verification,
// which the logger reports.
func WithSHA256(hexDigest string) Option {
return func(f *fetcher) { f.digest = hexDigest }
}
// WithStripFirstDir expects the archive to hold exactly one top-level
// directory and makes that directory's content the destination, the way
// browser archives nest one folder.
func WithStripFirstDir() Option {
return func(f *fetcher) { f.strip = true }
}
// WithClient sets the HTTP client. The default is http.DefaultClient.
func WithClient(client *http.Client) Option {
return func(f *fetcher) {
if client != nil {
f.client = client
}
}
}
// WithLogger sets where event lines go. The default discards them.
func WithLogger(logger Logger) Option {
return func(f *fetcher) {
if logger != nil {
f.logger = logger
}
}
}
// Zip fetches the archive served at any of urls and extracts it into dir.
//
// Every URL is probed concurrently and the first host to answer with the
// start of the archive is downloaded from; the others are fallbacks, in the
// order they answered, for a download that fails or does not match the
// expected digest. Extraction goes into a temporary sibling of dir that is
// renamed into place at the end, so dir either does not exist or is
// complete. A file lock next to dir serializes concurrent fetchers of the
// same directory; when dir already exists, Zip returns without a request.
func Zip(ctx context.Context, dir string, urls []string, opts ...Option) error {
f := fetcher{client: http.DefaultClient, logger: quiet{}}
for _, opt := range opts {
opt(&f)
}
if len(urls) == 0 {
return errors.New("fetch: no URLs to fetch from")
}
digest, err := normalizeDigest(f.digest)
if err != nil {
return err
}
f.digest = digest
// Absolute, so the lock file and the work directory are siblings of dir
// whatever the working directory is.
dir, err = filepath.Abs(dir)
if err != nil {
return fmt.Errorf("fetch: %w", err)
}
err = os.MkdirAll(filepath.Dir(dir), 0o755)
if err != nil {
return fmt.Errorf("fetch: %w", err)
}
done, err := present(dir)
if done || err != nil {
return err
}
// The lock file sits next to dir rather than inside it, since dir is
// renamed into place as a whole. It is never removed: deleting it would
// race a fetcher that has just opened it to wait.
lock := flock.New(dir + ".lock")
_, err = lock.TryLockContext(ctx, lockRetry)
if err != nil {
return fmt.Errorf("fetch: waiting for the lock on %s: %w", dir, err)
}
defer func() { _ = lock.Unlock() }()
// Whoever held the lock before may have landed dir meanwhile.
done, err = present(dir)
if done || err != nil {
if done {
f.logger.Println("fetch: found", dir, "in place")
}
return err
}
return f.fetch(ctx, dir, urls)
}
// fetch does the work of Zip once the lock is held and dir is known to be
// absent.
func (f *fetcher) fetch(ctx context.Context, dir string, urls []string) error {
work, err := os.MkdirTemp(filepath.Dir(dir), "."+filepath.Base(dir)+".fetch-")
if err != nil {
return fmt.Errorf("fetch: %w", err)
}
defer func() { _ = os.RemoveAll(work) }()
// Probes outlive the winner's download only as far as the fallbacks need
// them; cancelling on return stops the stragglers.
probeCtx, cancel := context.WithCancel(ctx)
defer cancel()
var errs []error
for r := range probe(probeCtx, f.client, urls) {
if r.err != nil {
f.logger.Println("fetch: no answer from", r.err)
errs = append(errs, r.err)
continue
}
err := f.fetchFrom(ctx, r.url, work, dir)
if err == nil {
return nil
}
f.logger.Println("fetch: giving up on", err)
errs = append(errs, err)
}
// Every URL reported once, so errs holds at least one error here.
err = errors.Join(errs...)
if ctx.Err() != nil {
return fmt.Errorf("fetch: %w: %w", ctx.Err(), err)
}
return fmt.Errorf("fetch: no URL yielded the archive: %w", err)
}
// fetchFrom downloads url into the work directory, verifies it, extracts it
// and renames the extracted tree to dir.
func (f *fetcher) fetchFrom(ctx context.Context, url, work, dir string) error {
f.logger.Println("fetch: downloading", url)
archive := filepath.Join(work, "archive.zip")
actual, err := f.download(ctx, url, archive)
if err != nil {
return err
}
switch {
case f.digest == "":
f.logger.Println("fetch: no expected SHA-256, extracting unverified")
case actual != f.digest:
return &HashMismatchError{URL: url, Expected: f.digest, Actual: actual}
default:
f.logger.Println("fetch: verified sha256", actual)
}
// A fresh tree per attempt; a failed one goes with the work directory.
tree, err := os.MkdirTemp(work, "tree-")
if err != nil {
return err
}
tops, err := unzip(ctx, archive, tree)
if err != nil {
return fmt.Errorf("%s: %w", url, err)
}
if f.strip {
tree, err = stripped(tree, tops)
if err != nil {
return fmt.Errorf("%s: %w", url, err)
}
}
err = os.Rename(tree, dir)
if err != nil {
return err
}
f.logger.Println("fetch: extracted to", dir)
return nil
}
// normalizeDigest lower-cases an expected digest and rejects one that is not
// a SHA-256 in hex, before any request is made. Empty stays empty.
func normalizeDigest(hexDigest string) (string, error) {
if hexDigest == "" {
return "", nil
}
digest := strings.ToLower(strings.TrimSpace(hexDigest))
if b, err := hex.DecodeString(digest); err != nil || len(b) != sha256Len {
return "", fmt.Errorf("fetch: expected SHA-256 %q is not %d hex digits", hexDigest, 2*sha256Len)
}
return digest, nil
}
// present reports whether dir already exists; a non-directory in its place is
// an error.
func present(dir string) (bool, error) {
info, err := os.Stat(dir)
if errors.Is(err, os.ErrNotExist) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("fetch: %w", err)
}
if !info.IsDir() {
return false, fmt.Errorf("fetch: %s exists and is not a directory", dir)
}
return true, nil
}