2019-02-21 23:37:15 +00:00
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/cache"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2019-02-21 23:37:15 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
|
|
|
"github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
|
2021-07-09 07:48:03 +00:00
|
|
|
"github.com/moby/buildkit/util/bklog"
|
2019-02-21 23:37:15 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-02-27 22:40:45 +00:00
|
|
|
func NewRefManager(cm cache.Manager) *RefManager {
|
|
|
|
return &RefManager{cm: cm}
|
|
|
|
}
|
|
|
|
|
2019-02-21 23:37:15 +00:00
|
|
|
type RefManager struct {
|
|
|
|
cm cache.Manager
|
|
|
|
}
|
|
|
|
|
2021-07-01 23:21:47 +00:00
|
|
|
func (rm *RefManager) Prepare(ctx context.Context, ref fileoptypes.Ref, readonly bool, g session.Group) (_ fileoptypes.Mount, rerr error) {
|
2019-02-21 23:37:15 +00:00
|
|
|
ir, ok := ref.(cache.ImmutableRef)
|
2019-02-27 22:40:45 +00:00
|
|
|
if !ok && ref != nil {
|
2019-02-21 23:37:15 +00:00
|
|
|
return nil, errors.Errorf("invalid ref type: %T", ref)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ir != nil && readonly {
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := ir.Mount(ctx, readonly, g)
|
2019-02-21 23:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-29 01:16:44 +00:00
|
|
|
return &Mount{m: m, readonly: readonly}, nil
|
2019-02-21 23:37:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
mr, err := rm.cm.New(ctx, ir, g, cache.WithDescription("fileop target"), cache.CachePolicyRetain)
|
2019-02-21 23:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-01 23:21:47 +00:00
|
|
|
defer func() {
|
|
|
|
if rerr != nil {
|
2021-07-09 00:09:35 +00:00
|
|
|
if err := mr.SetCachePolicyDefault(); err != nil {
|
2021-07-09 07:48:03 +00:00
|
|
|
bklog.G(ctx).Errorf("failed to reset FileOp mutable ref cachepolicy: %v", err)
|
2021-07-01 23:21:47 +00:00
|
|
|
}
|
|
|
|
mr.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}()
|
2020-10-27 06:13:39 +00:00
|
|
|
m, err := mr.Mount(ctx, readonly, g)
|
2019-02-21 23:37:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-29 01:16:44 +00:00
|
|
|
return &Mount{m: m, mr: mr, readonly: readonly}, nil
|
2019-02-21 23:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rm *RefManager) Commit(ctx context.Context, mount fileoptypes.Mount) (fileoptypes.Ref, error) {
|
|
|
|
m, ok := mount.(*Mount)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("invalid mount type %T", mount)
|
|
|
|
}
|
|
|
|
if m.mr == nil {
|
|
|
|
return nil, errors.Errorf("invalid mount without active ref for commit")
|
|
|
|
}
|
2019-08-15 22:42:03 +00:00
|
|
|
defer func() {
|
|
|
|
m.mr = nil
|
|
|
|
}()
|
2019-02-21 23:37:15 +00:00
|
|
|
return m.mr.Commit(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Mount struct {
|
2020-10-29 01:16:44 +00:00
|
|
|
m snapshot.Mountable
|
|
|
|
mr cache.MutableRef
|
|
|
|
readonly bool
|
2019-02-21 23:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Mount) Release(ctx context.Context) error {
|
|
|
|
if m.mr != nil {
|
|
|
|
return m.mr.Release(ctx)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (m *Mount) IsFileOpMount() {}
|
2020-10-29 01:16:44 +00:00
|
|
|
|
|
|
|
func (m *Mount) Readonly() bool {
|
|
|
|
return m.readonly
|
|
|
|
}
|