2019-02-08 01:30:57 +00:00
|
|
|
package ops
|
|
|
|
|
|
|
|
import (
|
2019-02-27 22:40:45 +00:00
|
|
|
"bytes"
|
2019-02-08 01:30:57 +00:00
|
|
|
"context"
|
2019-02-27 22:40:45 +00:00
|
|
|
"encoding/json"
|
2019-02-08 01:30:57 +00:00
|
|
|
"fmt"
|
2019-02-27 22:40:45 +00:00
|
|
|
"path"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
2019-02-08 01:30:57 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-02-27 22:40:45 +00:00
|
|
|
"github.com/moby/buildkit/cache"
|
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2020-06-30 01:06:02 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2019-02-27 22:40:45 +00:00
|
|
|
"github.com/moby/buildkit/solver"
|
|
|
|
"github.com/moby/buildkit/solver/llbsolver"
|
2020-10-28 21:07:44 +00:00
|
|
|
"github.com/moby/buildkit/solver/llbsolver/errdefs"
|
2019-02-27 22:40:45 +00:00
|
|
|
"github.com/moby/buildkit/solver/llbsolver/file"
|
2019-02-08 01:30:57 +00:00
|
|
|
"github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
|
|
|
|
"github.com/moby/buildkit/solver/pb"
|
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2019-02-27 22:40:45 +00:00
|
|
|
"github.com/moby/buildkit/worker"
|
|
|
|
digest "github.com/opencontainers/go-digest"
|
2019-02-08 01:30:57 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
2019-03-03 00:45:45 +00:00
|
|
|
const fileCacheType = "buildkit.file.v0"
|
2019-02-27 22:40:45 +00:00
|
|
|
|
|
|
|
type fileOp struct {
|
|
|
|
op *pb.FileOp
|
|
|
|
md *metadata.Store
|
|
|
|
w worker.Worker
|
|
|
|
solver *FileOpSolver
|
|
|
|
numInputs int
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileOp(v solver.Vertex, op *pb.Op_File, cm cache.Manager, md *metadata.Store, w worker.Worker) (solver.Op, error) {
|
2019-06-15 00:26:27 +00:00
|
|
|
if err := llbsolver.ValidateOp(&pb.Op{Op: op}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-02-27 22:40:45 +00:00
|
|
|
return &fileOp{
|
|
|
|
op: op.File,
|
|
|
|
md: md,
|
|
|
|
numInputs: len(v.Inputs()),
|
|
|
|
w: w,
|
2020-10-28 21:07:44 +00:00
|
|
|
solver: NewFileOpSolver(w, &file.Backend{}, file.NewRefManager(cm)),
|
2019-02-27 22:40:45 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (f *fileOp) CacheMap(ctx context.Context, g session.Group, index int) (*solver.CacheMap, bool, error) {
|
2019-02-27 22:40:45 +00:00
|
|
|
selectors := map[int]map[llbsolver.Selector]struct{}{}
|
2019-03-08 02:40:44 +00:00
|
|
|
invalidSelectors := map[int]struct{}{}
|
2019-02-27 22:40:45 +00:00
|
|
|
|
2019-03-03 00:45:45 +00:00
|
|
|
actions := make([][]byte, 0, len(f.op.Actions))
|
2019-02-27 22:40:45 +00:00
|
|
|
|
2019-03-08 02:40:44 +00:00
|
|
|
markInvalid := func(idx pb.InputIndex) {
|
|
|
|
if idx != -1 {
|
|
|
|
invalidSelectors[int(idx)] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 05:59:25 +00:00
|
|
|
indexes := make([][]int, 0, len(f.op.Actions))
|
|
|
|
|
2019-02-27 22:40:45 +00:00
|
|
|
for _, action := range f.op.Actions {
|
|
|
|
var dt []byte
|
|
|
|
var err error
|
|
|
|
switch a := action.Action.(type) {
|
|
|
|
case *pb.FileAction_Mkdir:
|
|
|
|
p := *a.Mkdir
|
2019-03-08 02:40:44 +00:00
|
|
|
markInvalid(action.Input)
|
2019-03-15 23:29:14 +00:00
|
|
|
processOwner(p.Owner, selectors)
|
2019-02-27 22:40:45 +00:00
|
|
|
dt, err = json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
case *pb.FileAction_Mkfile:
|
|
|
|
p := *a.Mkfile
|
2019-03-08 02:40:44 +00:00
|
|
|
markInvalid(action.Input)
|
2019-03-15 23:29:14 +00:00
|
|
|
processOwner(p.Owner, selectors)
|
2019-02-27 22:40:45 +00:00
|
|
|
dt, err = json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
case *pb.FileAction_Rm:
|
|
|
|
p := *a.Rm
|
2019-03-08 02:40:44 +00:00
|
|
|
markInvalid(action.Input)
|
2019-02-27 22:40:45 +00:00
|
|
|
dt, err = json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
case *pb.FileAction_Copy:
|
|
|
|
p := *a.Copy
|
2019-03-08 02:40:44 +00:00
|
|
|
markInvalid(action.Input)
|
2019-03-15 23:29:14 +00:00
|
|
|
processOwner(p.Owner, selectors)
|
2019-02-27 22:40:45 +00:00
|
|
|
if action.SecondaryInput != -1 && int(action.SecondaryInput) < f.numInputs {
|
2019-03-03 00:45:45 +00:00
|
|
|
addSelector(selectors, int(action.SecondaryInput), p.Src, p.AllowWildcard, p.FollowSymlink)
|
2019-03-09 00:15:42 +00:00
|
|
|
p.Src = path.Base(p.Src)
|
2019-02-27 22:40:45 +00:00
|
|
|
}
|
|
|
|
dt, err = json.Marshal(p)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-03 00:45:45 +00:00
|
|
|
actions = append(actions, dt)
|
2021-02-04 05:59:25 +00:00
|
|
|
indexes = append(indexes, []int{int(action.Input), int(action.SecondaryInput), int(action.Output)})
|
|
|
|
}
|
|
|
|
|
|
|
|
if isDefaultIndexes(indexes) {
|
|
|
|
indexes = nil
|
2019-03-03 00:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dt, err := json.Marshal(struct {
|
|
|
|
Type string
|
|
|
|
Actions [][]byte
|
2021-02-04 05:59:25 +00:00
|
|
|
Indexes [][]int `json:"indexes,omitempty"`
|
2019-03-03 00:45:45 +00:00
|
|
|
}{
|
|
|
|
Type: fileCacheType,
|
|
|
|
Actions: actions,
|
2021-02-04 05:59:25 +00:00
|
|
|
Indexes: indexes,
|
2019-03-03 00:45:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
2019-02-27 22:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cm := &solver.CacheMap{
|
2019-03-03 00:45:45 +00:00
|
|
|
Digest: digest.FromBytes(dt),
|
2019-02-27 22:40:45 +00:00
|
|
|
Deps: make([]struct {
|
|
|
|
Selector digest.Digest
|
|
|
|
ComputeDigestFunc solver.ResultBasedCacheFunc
|
2020-10-31 03:55:33 +00:00
|
|
|
PreprocessFunc solver.PreprocessFunc
|
2019-02-27 22:40:45 +00:00
|
|
|
}, f.numInputs),
|
|
|
|
}
|
|
|
|
|
|
|
|
for idx, m := range selectors {
|
2019-03-08 02:40:44 +00:00
|
|
|
if _, ok := invalidSelectors[idx]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-27 22:40:45 +00:00
|
|
|
dgsts := make([][]byte, 0, len(m))
|
|
|
|
for k := range m {
|
|
|
|
dgsts = append(dgsts, []byte(k.Path))
|
|
|
|
}
|
|
|
|
sort.Slice(dgsts, func(i, j int) bool {
|
|
|
|
return bytes.Compare(dgsts[i], dgsts[j]) > 0
|
|
|
|
})
|
|
|
|
cm.Deps[idx].Selector = digest.FromBytes(bytes.Join(dgsts, []byte{0}))
|
|
|
|
|
|
|
|
cm.Deps[idx].ComputeDigestFunc = llbsolver.NewContentHashFunc(dedupeSelectors(m))
|
|
|
|
}
|
2020-10-31 03:55:33 +00:00
|
|
|
for idx := range cm.Deps {
|
|
|
|
cm.Deps[idx].PreprocessFunc = llbsolver.UnlazyResultFunc
|
|
|
|
}
|
2019-02-27 22:40:45 +00:00
|
|
|
|
|
|
|
return cm, true, nil
|
|
|
|
}
|
|
|
|
|
2020-06-30 01:06:02 +00:00
|
|
|
func (f *fileOp) Exec(ctx context.Context, g session.Group, inputs []solver.Result) ([]solver.Result, error) {
|
2019-02-27 22:40:45 +00:00
|
|
|
inpRefs := make([]fileoptypes.Ref, 0, len(inputs))
|
2019-03-03 00:45:45 +00:00
|
|
|
for _, inp := range inputs {
|
2019-02-27 22:40:45 +00:00
|
|
|
workerRef, ok := inp.Sys().(*worker.WorkerRef)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid reference for exec %T", inp.Sys())
|
|
|
|
}
|
|
|
|
inpRefs = append(inpRefs, workerRef.ImmutableRef)
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
outs, err := f.solver.Solve(ctx, inpRefs, f.op.Actions, g)
|
2019-02-27 22:40:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outResults := make([]solver.Result, 0, len(outs))
|
|
|
|
for _, out := range outs {
|
|
|
|
outResults = append(outResults, worker.NewWorkerRefResult(out.(cache.ImmutableRef), f.w))
|
|
|
|
}
|
|
|
|
|
|
|
|
return outResults, nil
|
|
|
|
}
|
|
|
|
|
2019-03-03 00:45:45 +00:00
|
|
|
func addSelector(m map[int]map[llbsolver.Selector]struct{}, idx int, sel string, wildcard, followLinks bool) {
|
2019-02-27 22:40:45 +00:00
|
|
|
mm, ok := m[idx]
|
|
|
|
if !ok {
|
|
|
|
mm = map[llbsolver.Selector]struct{}{}
|
|
|
|
m[idx] = mm
|
|
|
|
}
|
2019-03-03 00:45:45 +00:00
|
|
|
s := llbsolver.Selector{Path: sel}
|
|
|
|
|
2019-02-27 22:40:45 +00:00
|
|
|
if wildcard && containsWildcards(sel) {
|
2019-03-03 00:45:45 +00:00
|
|
|
s.Wildcard = true
|
|
|
|
}
|
|
|
|
if followLinks {
|
|
|
|
s.FollowLinks = true
|
2019-02-27 22:40:45 +00:00
|
|
|
}
|
2019-03-03 00:45:45 +00:00
|
|
|
mm[s] = struct{}{}
|
2019-02-27 22:40:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func containsWildcards(name string) bool {
|
|
|
|
isWindows := runtime.GOOS == "windows"
|
|
|
|
for i := 0; i < len(name); i++ {
|
|
|
|
ch := name[i]
|
|
|
|
if ch == '\\' && !isWindows {
|
|
|
|
i++
|
|
|
|
} else if ch == '*' || ch == '?' || ch == '[' {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func dedupeSelectors(m map[llbsolver.Selector]struct{}) []llbsolver.Selector {
|
|
|
|
paths := make([]string, 0, len(m))
|
2019-03-03 00:45:45 +00:00
|
|
|
pathsFollow := make([]string, 0, len(m))
|
2019-02-27 22:40:45 +00:00
|
|
|
for sel := range m {
|
|
|
|
if !sel.Wildcard {
|
2019-03-03 00:45:45 +00:00
|
|
|
if sel.FollowLinks {
|
|
|
|
pathsFollow = append(pathsFollow, sel.Path)
|
|
|
|
} else {
|
|
|
|
paths = append(paths, sel.Path)
|
|
|
|
}
|
2019-02-27 22:40:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
paths = dedupePaths(paths)
|
2019-03-03 00:45:45 +00:00
|
|
|
pathsFollow = dedupePaths(pathsFollow)
|
2019-02-27 22:40:45 +00:00
|
|
|
selectors := make([]llbsolver.Selector, 0, len(m))
|
|
|
|
|
|
|
|
for _, p := range paths {
|
|
|
|
selectors = append(selectors, llbsolver.Selector{Path: p})
|
|
|
|
}
|
2019-03-03 00:45:45 +00:00
|
|
|
for _, p := range pathsFollow {
|
|
|
|
selectors = append(selectors, llbsolver.Selector{Path: p, FollowLinks: true})
|
|
|
|
}
|
2019-02-27 22:40:45 +00:00
|
|
|
|
|
|
|
for sel := range m {
|
|
|
|
if sel.Wildcard {
|
|
|
|
selectors = append(selectors, sel)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(selectors, func(i, j int) bool {
|
|
|
|
return selectors[i].Path < selectors[j].Path
|
|
|
|
})
|
|
|
|
|
|
|
|
return selectors
|
|
|
|
}
|
|
|
|
|
2019-03-15 23:29:14 +00:00
|
|
|
func processOwner(chopt *pb.ChownOpt, selectors map[int]map[llbsolver.Selector]struct{}) error {
|
|
|
|
if chopt == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if chopt.User != nil {
|
|
|
|
if u, ok := chopt.User.User.(*pb.UserOpt_ByName); ok {
|
|
|
|
if u.ByName.Input < 0 {
|
|
|
|
return errors.Errorf("invalid user index %d", u.ByName.Input)
|
|
|
|
}
|
|
|
|
addSelector(selectors, int(u.ByName.Input), "/etc/passwd", false, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if chopt.Group != nil {
|
|
|
|
if u, ok := chopt.Group.User.(*pb.UserOpt_ByName); ok {
|
|
|
|
if u.ByName.Input < 0 {
|
|
|
|
return errors.Errorf("invalid user index %d", u.ByName.Input)
|
|
|
|
}
|
|
|
|
addSelector(selectors, int(u.ByName.Input), "/etc/group", false, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-28 21:07:44 +00:00
|
|
|
func NewFileOpSolver(w worker.Worker, b fileoptypes.Backend, r fileoptypes.RefManager) *FileOpSolver {
|
2019-02-08 01:30:57 +00:00
|
|
|
return &FileOpSolver{
|
2020-10-28 21:07:44 +00:00
|
|
|
w: w,
|
2019-02-08 01:30:57 +00:00
|
|
|
b: b,
|
|
|
|
r: r,
|
|
|
|
outs: map[int]int{},
|
|
|
|
ins: map[int]input{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type FileOpSolver struct {
|
2020-10-28 21:07:44 +00:00
|
|
|
w worker.Worker
|
2019-02-08 01:30:57 +00:00
|
|
|
b fileoptypes.Backend
|
|
|
|
r fileoptypes.RefManager
|
|
|
|
|
|
|
|
mu sync.Mutex
|
|
|
|
outs map[int]int
|
|
|
|
ins map[int]input
|
|
|
|
g flightcontrol.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
type input struct {
|
|
|
|
requiresCommit bool
|
|
|
|
mount fileoptypes.Mount
|
|
|
|
ref fileoptypes.Ref
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (s *FileOpSolver) Solve(ctx context.Context, inputs []fileoptypes.Ref, actions []*pb.FileAction, g session.Group) ([]fileoptypes.Ref, error) {
|
2019-02-08 01:30:57 +00:00
|
|
|
for i, a := range actions {
|
|
|
|
if int(a.Input) < -1 || int(a.Input) >= len(inputs)+len(actions) {
|
2019-02-27 22:40:45 +00:00
|
|
|
return nil, errors.Errorf("invalid input index %d, %d provided", a.Input, len(inputs)+len(actions))
|
2019-02-08 01:30:57 +00:00
|
|
|
}
|
|
|
|
if int(a.SecondaryInput) < -1 || int(a.SecondaryInput) >= len(inputs)+len(actions) {
|
|
|
|
return nil, errors.Errorf("invalid secondary input index %d, %d provided", a.Input, len(inputs))
|
|
|
|
}
|
|
|
|
|
|
|
|
inp, ok := s.ins[int(a.Input)]
|
|
|
|
if ok {
|
|
|
|
inp.requiresCommit = true
|
|
|
|
}
|
|
|
|
s.ins[int(a.Input)] = inp
|
|
|
|
|
|
|
|
inp, ok = s.ins[int(a.SecondaryInput)]
|
|
|
|
if ok {
|
|
|
|
inp.requiresCommit = true
|
|
|
|
}
|
|
|
|
s.ins[int(a.SecondaryInput)] = inp
|
|
|
|
|
|
|
|
if a.Output != -1 {
|
|
|
|
if _, ok := s.outs[int(a.Output)]; ok {
|
|
|
|
return nil, errors.Errorf("duplicate output %d", a.Output)
|
|
|
|
}
|
|
|
|
idx := len(inputs) + i
|
|
|
|
s.outs[int(a.Output)] = idx
|
|
|
|
s.ins[idx] = input{requiresCommit: true}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(s.outs) == 0 {
|
|
|
|
return nil, errors.Errorf("no outputs specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < len(s.outs); i++ {
|
|
|
|
if _, ok := s.outs[i]; !ok {
|
|
|
|
return nil, errors.Errorf("missing output index %d", i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-20 02:41:45 +00:00
|
|
|
defer func() {
|
|
|
|
for _, in := range s.ins {
|
|
|
|
if in.ref == nil && in.mount != nil {
|
|
|
|
in.mount.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-02-08 01:30:57 +00:00
|
|
|
outs := make([]fileoptypes.Ref, len(s.outs))
|
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
for i, idx := range s.outs {
|
|
|
|
func(i, idx int) {
|
|
|
|
eg.Go(func() error {
|
|
|
|
if err := s.validate(idx, inputs, actions, nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
inp, err := s.getInput(ctx, idx, inputs, actions, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
2020-10-29 01:16:44 +00:00
|
|
|
return errdefs.WithFileActionError(err, idx-len(inputs))
|
2019-02-08 01:30:57 +00:00
|
|
|
}
|
|
|
|
outs[i] = inp.ref
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}(i, idx)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
2019-02-20 02:41:45 +00:00
|
|
|
for _, r := range outs {
|
|
|
|
if r != nil {
|
|
|
|
r.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}
|
2019-02-08 01:30:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return outs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileOpSolver) validate(idx int, inputs []fileoptypes.Ref, actions []*pb.FileAction, loaded []int) error {
|
|
|
|
for _, check := range loaded {
|
|
|
|
if idx == check {
|
|
|
|
return errors.Errorf("loop from index %d", idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if idx < len(inputs) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
loaded = append(loaded, idx)
|
|
|
|
action := actions[idx-len(inputs)]
|
|
|
|
for _, inp := range []int{int(action.Input), int(action.SecondaryInput)} {
|
|
|
|
if err := s.validate(inp, inputs, actions, loaded); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (s *FileOpSolver) getInput(ctx context.Context, idx int, inputs []fileoptypes.Ref, actions []*pb.FileAction, g session.Group) (input, error) {
|
2019-03-09 00:15:42 +00:00
|
|
|
inp, err := s.g.Do(ctx, fmt.Sprintf("inp-%d", idx), func(ctx context.Context) (_ interface{}, err error) {
|
2019-02-08 01:30:57 +00:00
|
|
|
s.mu.Lock()
|
|
|
|
inp := s.ins[idx]
|
|
|
|
s.mu.Unlock()
|
|
|
|
if inp.mount != nil || inp.ref != nil {
|
|
|
|
return inp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx < len(inputs) {
|
|
|
|
inp.ref = inputs[idx]
|
|
|
|
s.mu.Lock()
|
|
|
|
s.ins[idx] = inp
|
|
|
|
s.mu.Unlock()
|
|
|
|
return inp, nil
|
|
|
|
}
|
|
|
|
|
2019-03-09 00:15:42 +00:00
|
|
|
var inpMount, inpMountSecondary fileoptypes.Mount
|
2019-03-15 23:29:14 +00:00
|
|
|
var toRelease []fileoptypes.Mount
|
2020-10-29 01:16:44 +00:00
|
|
|
action := actions[idx-len(inputs)]
|
|
|
|
|
2019-03-15 23:29:14 +00:00
|
|
|
defer func() {
|
2020-10-28 21:07:44 +00:00
|
|
|
if err != nil && inpMount != nil {
|
2020-10-29 01:16:44 +00:00
|
|
|
inputRes := make([]solver.Result, len(inputs))
|
|
|
|
for i, input := range inputs {
|
|
|
|
inputRes[i] = worker.NewWorkerRefResult(input.(cache.ImmutableRef), s.w)
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
2020-10-29 01:16:44 +00:00
|
|
|
|
|
|
|
outputRes := make([]solver.Result, len(actions))
|
|
|
|
|
|
|
|
// Commit the mutable for the primary input of the failed action.
|
|
|
|
if !inpMount.Readonly() {
|
|
|
|
ref, cerr := s.r.Commit(ctx, inpMount)
|
|
|
|
if cerr == nil {
|
|
|
|
outputRes[idx-len(inputs)] = worker.NewWorkerRefResult(ref.(cache.ImmutableRef), s.w)
|
|
|
|
}
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
2020-10-29 01:16:44 +00:00
|
|
|
|
|
|
|
// If the action has a secondary input, commit it and set the ref on
|
|
|
|
// the output results.
|
|
|
|
if inpMountSecondary != nil && !inpMountSecondary.Readonly() {
|
|
|
|
ref2, cerr := s.r.Commit(ctx, inpMountSecondary)
|
|
|
|
if cerr == nil {
|
|
|
|
outputRes[int(action.SecondaryInput)-len(inputs)] = worker.NewWorkerRefResult(ref2.(cache.ImmutableRef), s.w)
|
|
|
|
}
|
2020-10-28 21:07:44 +00:00
|
|
|
}
|
2020-10-29 01:16:44 +00:00
|
|
|
|
2020-10-28 21:07:44 +00:00
|
|
|
err = errdefs.WithExecError(err, inputRes, outputRes)
|
2019-03-09 00:15:42 +00:00
|
|
|
}
|
2020-10-29 01:16:44 +00:00
|
|
|
for _, m := range toRelease {
|
|
|
|
m.Release(context.TODO())
|
|
|
|
}
|
2019-03-15 23:29:14 +00:00
|
|
|
}()
|
|
|
|
|
2019-02-08 01:30:57 +00:00
|
|
|
loadInput := func(ctx context.Context) func() error {
|
|
|
|
return func() error {
|
2020-10-27 06:13:39 +00:00
|
|
|
inp, err := s.getInput(ctx, int(action.Input), inputs, actions, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if inp.ref != nil {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := s.r.Prepare(ctx, inp.ref, false, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
inpMount = m
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
inpMount = inp.mount
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadSecondaryInput := func(ctx context.Context) func() error {
|
|
|
|
return func() error {
|
2020-10-27 06:13:39 +00:00
|
|
|
inp, err := s.getInput(ctx, int(action.SecondaryInput), inputs, actions, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if inp.ref != nil {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := s.r.Prepare(ctx, inp.ref, true, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
inpMountSecondary = m
|
2019-03-15 23:29:14 +00:00
|
|
|
toRelease = append(toRelease, m)
|
2019-02-08 01:30:57 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
inpMountSecondary = inp.mount
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 23:29:14 +00:00
|
|
|
loadUser := func(ctx context.Context, uopt *pb.UserOpt) (fileoptypes.Mount, error) {
|
|
|
|
if uopt == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
switch u := uopt.User.(type) {
|
|
|
|
case *pb.UserOpt_ByName:
|
|
|
|
var m fileoptypes.Mount
|
|
|
|
if u.ByName.Input < 0 {
|
|
|
|
return nil, errors.Errorf("invalid user index: %d", u.ByName.Input)
|
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
inp, err := s.getInput(ctx, int(u.ByName.Input), inputs, actions, g)
|
2019-03-15 23:29:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if inp.ref != nil {
|
2020-10-27 06:13:39 +00:00
|
|
|
mm, err := s.r.Prepare(ctx, inp.ref, true, g)
|
2019-03-15 23:29:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
toRelease = append(toRelease, mm)
|
|
|
|
m = mm
|
|
|
|
} else {
|
|
|
|
m = inp.mount
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
default:
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadOwner := func(ctx context.Context, chopt *pb.ChownOpt) (fileoptypes.Mount, fileoptypes.Mount, error) {
|
|
|
|
if chopt == nil {
|
|
|
|
return nil, nil, nil
|
|
|
|
}
|
|
|
|
um, err := loadUser(ctx, chopt.User)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
gm, err := loadUser(ctx, chopt.Group)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return um, gm, nil
|
|
|
|
}
|
|
|
|
|
2019-02-08 01:30:57 +00:00
|
|
|
if action.Input != -1 && action.SecondaryInput != -1 {
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
eg.Go(loadInput(ctx))
|
|
|
|
eg.Go(loadSecondaryInput(ctx))
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if action.Input != -1 {
|
|
|
|
if err := loadInput(ctx)(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if action.SecondaryInput != -1 {
|
|
|
|
if err := loadSecondaryInput(ctx)(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if inpMount == nil {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := s.r.Prepare(ctx, nil, false, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inpMount = m
|
|
|
|
}
|
|
|
|
|
|
|
|
switch a := action.Action.(type) {
|
|
|
|
case *pb.FileAction_Mkdir:
|
2019-03-15 23:29:14 +00:00
|
|
|
user, group, err := loadOwner(ctx, a.Mkdir.Owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.b.Mkdir(ctx, inpMount, user, group, *a.Mkdir); err != nil {
|
2019-02-08 01:30:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *pb.FileAction_Mkfile:
|
2019-03-15 23:29:14 +00:00
|
|
|
user, group, err := loadOwner(ctx, a.Mkfile.Owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.b.Mkfile(ctx, inpMount, user, group, *a.Mkfile); err != nil {
|
2019-02-08 01:30:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *pb.FileAction_Rm:
|
|
|
|
if err := s.b.Rm(ctx, inpMount, *a.Rm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case *pb.FileAction_Copy:
|
|
|
|
if inpMountSecondary == nil {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := s.r.Prepare(ctx, nil, true, g)
|
2019-02-08 01:30:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inpMountSecondary = m
|
|
|
|
}
|
2019-03-15 23:29:14 +00:00
|
|
|
user, group, err := loadOwner(ctx, a.Copy.Owner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := s.b.Copy(ctx, inpMountSecondary, inpMount, user, group, *a.Copy); err != nil {
|
2019-02-08 01:30:57 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("invalid action type %T", action.Action)
|
|
|
|
}
|
|
|
|
|
|
|
|
if inp.requiresCommit {
|
|
|
|
ref, err := s.r.Commit(ctx, inpMount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
inp.ref = ref
|
|
|
|
} else {
|
|
|
|
inp.mount = inpMount
|
|
|
|
}
|
|
|
|
s.mu.Lock()
|
|
|
|
s.ins[idx] = inp
|
|
|
|
s.mu.Unlock()
|
|
|
|
return inp, nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return input{}, err
|
|
|
|
}
|
|
|
|
return inp.(input), err
|
|
|
|
}
|
2021-02-04 05:59:25 +00:00
|
|
|
|
|
|
|
func isDefaultIndexes(idxs [][]int) bool {
|
|
|
|
// Older version of checksum did not contain indexes for actions resulting in possibility for a wrong cache match.
|
|
|
|
// We detect the most common pattern for indexes and maintain old checksum for that case to minimize cache misses on upgrade.
|
|
|
|
// If a future change causes braking changes in instruction cache consider removing this exception.
|
|
|
|
if len(idxs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, idx := range idxs {
|
|
|
|
if len(idx) != 3 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// input for first action is first input
|
|
|
|
if i == 0 && idx[0] != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// input for other actions is previous action
|
|
|
|
if i != 0 && idx[0] != len(idxs)+(i-1) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// secondary input is second input or -1
|
|
|
|
if idx[1] != -1 && idx[1] != 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// last action creates output
|
|
|
|
if i == len(idxs)-1 && idx[2] != 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
// other actions do not create an output
|
|
|
|
if i != len(idxs)-1 && idx[2] != -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|