|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/cashapp/hermit/ui" |
| 6 | + "github.com/cashapp/hermit/util" |
| 7 | + "github.com/pkg/errors" |
| 8 | + "net/http" |
| 9 | + "net/url" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | +) |
| 14 | + |
| 15 | +type packageSource interface { |
| 16 | + OpenLocal(cache *Cache, checksum string) (*os.File, error) |
| 17 | + Download(b *ui.Task, cache *Cache, checksum string) (path string, etag string, err error) |
| 18 | + ETag(b *ui.Task, cache *Cache) (etag string, err error) |
| 19 | +} |
| 20 | + |
| 21 | +func getSource(uri string) (packageSource, error) { |
| 22 | + if strings.HasSuffix(uri, ".git") { |
| 23 | + return &gitSource{URL: uri}, nil |
| 24 | + } |
| 25 | + |
| 26 | + u, err := url.Parse(uri) |
| 27 | + if err != nil { |
| 28 | + return nil, errors.WithStack(err) |
| 29 | + } |
| 30 | + |
| 31 | + switch u.Scheme { |
| 32 | + case "", "file": |
| 33 | + return &fileSource{Path: u.Path}, nil |
| 34 | + |
| 35 | + case "http", "https": |
| 36 | + return &httpSource{uri}, errors.WithStack(err) |
| 37 | + default: |
| 38 | + return nil, errors.Errorf("unsupported URI %s", uri) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +type fileSource struct { |
| 43 | + Path string |
| 44 | +} |
| 45 | + |
| 46 | +func (s *fileSource) OpenLocal(_ *Cache, _ string) (*os.File, error) { |
| 47 | + f, err := os.Open(s.Path) |
| 48 | + return f, errors.WithStack(err) |
| 49 | +} |
| 50 | + |
| 51 | +func (s *fileSource) Download(_ *ui.Task, _ *Cache, _ string) (path string, etag string, err error) { |
| 52 | + // TODO: Checksum it again? |
| 53 | + // Local file, just open it. |
| 54 | + return s.Path, "", nil |
| 55 | +} |
| 56 | + |
| 57 | +func (s *fileSource) ETag(_ *ui.Task, _ *Cache) (etag string, err error) { |
| 58 | + return "", nil |
| 59 | +} |
| 60 | + |
| 61 | +type httpSource struct { |
| 62 | + URL string |
| 63 | +} |
| 64 | + |
| 65 | +func (s *httpSource) OpenLocal(c *Cache, checksum string) (*os.File, error) { |
| 66 | + f, err := os.Open(c.Path(checksum, s.URL)) |
| 67 | + return f, errors.WithStack(err) |
| 68 | +} |
| 69 | + |
| 70 | +func (s *httpSource) Download(b *ui.Task, cache *Cache, checksum string) (path string, etag string, err error) { |
| 71 | + cachePath := cache.Path(checksum, s.URL) |
| 72 | + return cache.downloadHTTP(b, checksum, s.URL, cachePath) |
| 73 | +} |
| 74 | + |
| 75 | +func (s *httpSource) ETag(_ *ui.Task, c *Cache) (etag string, err error) { |
| 76 | + uri := s.URL |
| 77 | + req, err := http.NewRequestWithContext(context.Background(), http.MethodHead, uri, nil) |
| 78 | + if err != nil { |
| 79 | + return "", errors.Wrap(err, uri) |
| 80 | + } |
| 81 | + resp, err := c.fastFailHTTPClient.Do(req) |
| 82 | + if err != nil { |
| 83 | + return "", errors.Wrap(err, uri) |
| 84 | + } |
| 85 | + defer resp.Body.Close() |
| 86 | + // Normal HTTP error, log and try the next mirror. |
| 87 | + if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
| 88 | + return "", errors.Errorf("%s failed: %d", uri, resp.StatusCode) |
| 89 | + } |
| 90 | + result := resp.Header.Get("ETag") |
| 91 | + return result, nil |
| 92 | +} |
| 93 | + |
| 94 | +type gitSource struct { |
| 95 | + URL string |
| 96 | +} |
| 97 | + |
| 98 | +func (s *gitSource) OpenLocal(c *Cache, checksum string) (*os.File, error) { |
| 99 | + f, err := os.Open(c.Path(checksum, s.URL)) |
| 100 | + return f, errors.WithStack(err) |
| 101 | +} |
| 102 | + |
| 103 | +func (s *gitSource) Download(b *ui.Task, cache *Cache, checksum string) (string, string, error) { |
| 104 | + base := BasePath(checksum, s.URL) |
| 105 | + err := util.RunInDir(b, cache.root, "git", "clone", "--depth=1", s.URL, base) |
| 106 | + if err != nil { |
| 107 | + return "", "", errors.Wrap(err, s.URL) |
| 108 | + } |
| 109 | + etag, err := s.ETag(b, cache) |
| 110 | + if err != nil { |
| 111 | + return "", "", errors.Wrap(err, s.URL) |
| 112 | + } |
| 113 | + |
| 114 | + return filepath.Join(cache.root, base), etag, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (s *gitSource) ETag(b *ui.Task, c *Cache) (etag string, err error) { |
| 118 | + bts, err := util.Capture(b, "git", "ls-remote", s.URL, "HEAD") |
| 119 | + if err != nil { |
| 120 | + return "", errors.Wrap(err, s.URL) |
| 121 | + } |
| 122 | + str := string(bts) |
| 123 | + parts := strings.Split(str, "\t") |
| 124 | + if len(parts) != 2 { |
| 125 | + return "", errors.Errorf("invalid HEAD: %s", str) |
| 126 | + } |
| 127 | + |
| 128 | + return parts[0], nil |
| 129 | +} |
0 commit comments