package runner
import (
"archive/tar"
"compress/gzip"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
)
// ArchiveCheckout fetches a check-in's tree as the forge's own archive
// (GET <repo>/archive/<checkin>, a gzip tarball) and unpacks it into a
// fresh directory. It needs nothing on the machine but this program:
// no zit, no clone, no history. A task that needs a real repository
// uses ZitCheckout instead.
type ArchiveCheckout struct {
URL string // the repository's URL on the forge
// User and Password, for a private repository: an account that may
// read it and that account's sync token.
User, Password string
Client *http.Client
}
// Checkout replaces dir with checkin's tree. The tree is unpacked
// beside dir and swapped in only once it is whole, so a failed fetch
// never leaves half of one check-in over the rest of another.
func (a ArchiveCheckout) Checkout(ctx context.Context, dir, checkin string) error {
req, err := http.NewRequestWithContext(ctx, "GET", strings.TrimSuffix(a.URL, "/")+"/archive/"+url.PathEscape(checkin), nil)
if err != nil {
return err
}
if a.User != "" {
req.SetBasicAuth(a.User, a.Password)
}
c := a.Client
if c == nil {
c = http.DefaultClient
}
resp, err := c.Do(req)
if err != nil {
return withoutURL(err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
msg, _ := io.ReadAll(io.LimitReader(resp.Body, 16<<10))
if c, ok := mergeConflictIn(resp.StatusCode, msg); ok {
return c
}
return &ArchiveRefused{Subject: checkin, Status: resp.StatusCode, Detail: refusalText(msg)}
}
tmp := dir + ".new"
os.RemoveAll(tmp)
if err := os.MkdirAll(tmp, 0o755); err != nil {
return err
}
if err := untar(resp.Body, tmp); err != nil {
os.RemoveAll(tmp)
return fmt.Errorf("archive of %s: %w", SubjectLabel(checkin), err)
}
if err := os.RemoveAll(dir); err != nil {
return err
}
return os.Rename(tmp, dir)
}
// ArchiveRefused is the forge answering a checkout's archive with a
// status other than 200. It carries the subject and the status and
// never the request, so no credential or address leaves with it.
type ArchiveRefused struct {
Subject string
Status int
Detail string // the forge's own words, when it sent plain ones
}
func (e *ArchiveRefused) Error() string {
out := fmt.Sprintf("the forge answered %d %s for the archive of %s", e.Status, http.StatusText(e.Status), SubjectLabel(e.Subject))
if e.Detail != "" {
out += ": " + e.Detail
}
switch e.Status {
case http.StatusUnauthorized, http.StatusForbidden, http.StatusNotFound:
out += "; on a private repository this is how the forge answers an account that may not read it, so check the runner's --user and its sync token"
}
return out
}
// refusalText is a refusal's own sentence: the error of a JSON answer or
// a short plain-text one, and nothing from a page of markup.
func refusalText(body []byte) string {
var j struct {
Error string `json:"error"`
}
text := strings.TrimSpace(string(body))
if json.Unmarshal(body, &j) == nil && j.Error != "" {
text = j.Error
}
if strings.ContainsAny(text, "<>") || strings.Contains(text, "\n") {
return ""
}
if len(text) > 200 {
text = text[:200]
}
return text
}
// withoutURL is err with the address a transport error carries cut
// down to its scheme and host: a log line names the forge, never a path
// or a query that could hold a credential.
func withoutURL(err error) error {
var ue *url.Error
if !errors.As(err, &ue) {
return err
}
where := "the forge"
if u, perr := url.Parse(ue.URL); perr == nil && u.Host != "" {
where = u.Scheme + "://" + u.Host
}
return fmt.Errorf("could not reach %s: %w", where, ue.Err)
}
// SubjectLabel names a checkout's subject for a log: a check-in by its
// first twelve characters, a pair by both of its tips.
func SubjectLabel(checkin string) string {
if rest, ok := strings.CutPrefix(checkin, "pair:"); ok {
if branch, target, ok := strings.Cut(rest, ":"); ok {
return fmt.Sprintf("the pair %.12s onto %.12s", branch, target)
}
}
return fmt.Sprintf("%.12s", checkin)
}
// MergeConflict is a premerge pair's verdict arriving as its checkout:
// the branch does not merge into the target, so there is no tree to
// test (RFC 0009 §premerge). It fails the task at once, never retried,
// because trying again asks the same two fixed trees the same question.
type MergeConflict struct {
Message string
Paths []string
}
func (c *MergeConflict) Error() string {
if len(c.Paths) == 0 {
return c.Message
}
return c.Message + "\n " + strings.Join(c.Paths, "\n ")
}
// mergeConflictIn reads the forge's refusal of a pair archive.
func mergeConflictIn(status int, body []byte) (*MergeConflict, bool) {
if status != http.StatusBadRequest {
return nil, false
}
var r struct {
Code string `json:"code"`
Error string `json:"error"`
Paths string `json:"paths"`
}
if json.Unmarshal(body, &r) != nil || r.Code != "merge_conflict" {
return nil, false
}
c := &MergeConflict{Message: r.Error}
for _, p := range strings.Split(r.Paths, "\n") {
if p = strings.TrimSpace(p); p != "" {
c.Paths = append(c.Paths, p)
}
}
return c, true
}
// IsPair reports a premerge subject, "pair:<branch tip>:<target tip>".
func IsPair(checkin string) bool { return strings.HasPrefix(checkin, "pair:") }
// untar unpacks a gzip tarball into root, dropping the archive's single
// top-level directory (the forge names it <repo>-<checkin>/). Every
// path must land inside root: an absolute name, a "..", or a link that
// points out of the tree is refused rather than skipped, because a
// tree with a file silently missing is a test result nobody can trust.
func untar(r io.Reader, root string) error {
gz, err := gzip.NewReader(r)
if err != nil {
return err
}
defer gz.Close()
tr := tar.NewReader(gz)
for {
h, err := tr.Next()
if errors.Is(err, io.EOF) {
return nil
}
if err != nil {
return err
}
name := strings.TrimPrefix(h.Name, "./")
_, rel, ok := strings.Cut(name, "/")
if !ok || rel == "" || strings.Trim(rel, "/") == "" {
continue // the top-level directory itself
}
rel = strings.TrimSuffix(rel, "/")
if !filepath.IsLocal(filepath.FromSlash(rel)) {
return fmt.Errorf("refusing %q: it names a path outside the tree", h.Name)
}
target := filepath.Join(root, filepath.FromSlash(rel))
switch h.Typeflag {
case tar.TypeDir:
if err := mkdirBelow(root, filepath.FromSlash(rel)); err != nil {
return err
}
case tar.TypeReg:
if err := mkdirBelow(root, filepath.Dir(filepath.FromSlash(rel))); err != nil {
return err
}
if fi, err := os.Lstat(target); err == nil && fi.Mode()&os.ModeSymlink != 0 {
return fmt.Errorf("refusing %q: a link is already there", h.Name)
}
mode := os.FileMode(0o644)
if h.FileInfo().Mode().Perm()&0o111 != 0 {
mode = 0o755
}
f, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
if err != nil {
return err
}
if _, err := io.Copy(f, tr); err != nil {
f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
case tar.TypeSymlink:
link := filepath.FromSlash(h.Linkname)
if filepath.IsAbs(link) || !filepath.IsLocal(filepath.Join(filepath.Dir(filepath.FromSlash(rel)), link)) {
return fmt.Errorf("refusing link %q -> %q: it points outside the tree", h.Name, h.Linkname)
}
if err := mkdirBelow(root, filepath.Dir(filepath.FromSlash(rel))); err != nil {
return err
}
// The target, followed through any link already on disk,
// must stay in the tree too.
if real, err := filepath.EvalSymlinks(filepath.Join(filepath.Dir(target), link)); err == nil {
if realRoot, err := filepath.EvalSymlinks(root); err == nil {
if r, err := filepath.Rel(realRoot, real); err != nil || !filepath.IsLocal(r) && r != "." {
return fmt.Errorf("refusing link %q -> %q: it resolves outside the tree", h.Name, h.Linkname)
}
}
}
if err := os.Symlink(link, target); err != nil {
return err
}
}
}
}
// mkdirBelow makes rel under root one component at a time, looking at
// what is on disk first: a component that is a link, or is not a
// directory, ends the extraction. An archive that made a link earlier
// cannot then carry a later entry through it out of the tree.
func mkdirBelow(root, rel string) error {
if rel == "." || rel == "" {
return nil
}
cur := root
for _, part := range strings.Split(rel, string(filepath.Separator)) {
cur = filepath.Join(cur, part)
fi, err := os.Lstat(cur)
switch {
case errors.Is(err, os.ErrNotExist):
if err := os.Mkdir(cur, 0o755); err != nil {
return err
}
case err != nil:
return err
case fi.Mode()&os.ModeSymlink != 0 || !fi.IsDir():
return fmt.Errorf("refusing to extract through %q: it is a link or not a directory", part)
}
}
return nil
}