Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --parents flag for ADD/COPY instructions #3000

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ run:
build-tags:
- dfrunsecurity
- dfaddgit
- dfparents

linters:
enable:
Expand Down
2 changes: 2 additions & 0 deletions client/llb/fileop.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ type CopyInfo struct {
AllowEmptyWildcard bool
ChownOpt *ChownOpt
CreatedTime *time.Time
Parents bool
}

func (mi *CopyInfo) SetCopyOption(mi2 *CopyInfo) {
Expand Down Expand Up @@ -473,6 +474,7 @@ func (a *fileActionCopy) toProtoAction(ctx context.Context, parent string, base
AttemptUnpackDockerCompatibility: a.info.AttemptUnpack,
CreateDestPath: a.info.CreateDestPath,
Timestamp: marshalTime(a.info.CreatedTime),
Parents: a.info.Parents,
}
if a.info.Mode != nil {
c.Mode = int32(*a.info.Mode)
Expand Down
7 changes: 7 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
chmod: c.Chmod,
link: c.Link,
keepGitDir: c.KeepGitDir,
parents: c.Parents,
location: c.Location(),
opt: opt,
})
Expand Down Expand Up @@ -699,6 +700,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error {
chown: c.Chown,
chmod: c.Chmod,
link: c.Link,
parents: c.Parents,
location: c.Location(),
opt: opt,
})
Expand Down Expand Up @@ -1066,6 +1068,9 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
a = a.Copy(st, f, dest, opts...)
}
} else {
if cfg.parents && !parentsEnabled {
return errors.New("flag --parents for ADD/COPY instructions requires the labs channel")
}
opts := append([]llb.CopyOption{&llb.CopyInfo{
Mode: mode,
FollowSymlinks: true,
Expand All @@ -1074,6 +1079,7 @@ func dispatchCopy(d *dispatchState, cfg copyConfig) error {
CreateDestPath: true,
AllowWildcard: true,
AllowEmptyWildcard: true,
Parents: cfg.parents,
}}, copyOpt...)

if a == nil {
Expand Down Expand Up @@ -1159,6 +1165,7 @@ type copyConfig struct {
chmod string
link bool
keepGitDir bool
parents bool
location []parser.Range
opt dispatchOpt
}
Expand Down
6 changes: 6 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert_noparents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build !dfparents
// +build !dfparents

package dockerfile2llb

const parentsEnabled = false
6 changes: 6 additions & 0 deletions frontend/dockerfile/dockerfile2llb/convert_parents.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
//go:build dfparents
// +build dfparents

package dockerfile2llb

const parentsEnabled = true
10 changes: 6 additions & 4 deletions frontend/dockerfile/instructions/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ type AddCommand struct {
Chmod string
Link bool
KeepGitDir bool // whether to keep .git dir, only meaningful for git sources
Parents bool // parents preserves directory structure
}

// Expand variables
Expand All @@ -248,10 +249,11 @@ func (c *AddCommand) Expand(expander SingleWordExpander) error {
type CopyCommand struct {
withNameAndCode
SourcesAndDest
From string
Chown string
Chmod string
Link bool
From string
Chown string
Chmod string
Link bool
Parents bool // parents preserves directory structure
}

// Expand variables
Expand Down
4 changes: 4 additions & 0 deletions frontend/dockerfile/instructions/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ func parseAdd(req parseRequest) (*AddCommand, error) {
flChmod := req.flags.AddString("chmod", "")
flLink := req.flags.AddBool("link", false)
flKeepGitDir := req.flags.AddBool("keep-git-dir", false)
flParents := req.flags.AddBool("parents", false)
if err := req.flags.Parse(); err != nil {
return nil, err
}
Expand All @@ -298,6 +299,7 @@ func parseAdd(req parseRequest) (*AddCommand, error) {
Chmod: flChmod.Value,
Link: flLink.Value == "true",
KeepGitDir: flKeepGitDir.Value == "true",
Parents: flParents.Value == "true",
}, nil
}

Expand All @@ -309,6 +311,7 @@ func parseCopy(req parseRequest) (*CopyCommand, error) {
flFrom := req.flags.AddString("from", "")
flChmod := req.flags.AddString("chmod", "")
flLink := req.flags.AddBool("link", false)
flParents := req.flags.AddBool("parents", false)
if err := req.flags.Parse(); err != nil {
return nil, err
}
Expand All @@ -325,6 +328,7 @@ func parseCopy(req parseRequest) (*CopyCommand, error) {
Chown: flChown.Value,
Chmod: flChmod.Value,
Link: flLink.Value == "true",
Parents: flParents.Value == "true",
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/dockerfile/release/labs/tags
Original file line number Diff line number Diff line change
@@ -1 +1 @@
dfrunsecurity dfaddgit
dfrunsecurity dfaddgit dfparents
41 changes: 30 additions & 11 deletions solver/llbsolver/file/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,15 +205,31 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
copy.WithXAttrErrorHandler(xattrErrorHandler),
}

if !action.AllowWildcard {
copy_one := func(s string) error {
d := destPath
if action.Parents {
d = filepath.Join("/", d, s)
}
if action.AttemptUnpackDockerCompatibility {
if ok, err := unpack(ctx, src, srcPath, dest, destPath, ch, timestampToTime(action.Timestamp)); err != nil {
if ok, err := unpack(ctx, src, s, dest, d, ch, timestampToTime(action.Timestamp)); err != nil {
return err
} else if ok {
return nil
}
}
return copy.Copy(ctx, src, srcPath, dest, destPath, opt...)
return copy.Copy(ctx, src, s, dest, d, opt...)
}

if !action.AllowWildcard {
// if action.AttemptUnpackDockerCompatibility {
// if ok, err := unpack(ctx, src, srcPath, dest, destPath, ch, timestampToTime(action.Timestamp)); err != nil {
// return err
// } else if ok {
// return nil
// }
// }
// return copy.Copy(ctx, src, srcPath, dest, destPath, opt...)
return copy_one(src)
}

m, err := copy.ResolveWildcards(src, srcPath, action.FollowSymlink)
Expand All @@ -229,14 +245,17 @@ func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *
}

for _, s := range m {
if action.AttemptUnpackDockerCompatibility {
if ok, err := unpack(ctx, src, s, dest, destPath, ch, timestampToTime(action.Timestamp)); err != nil {
return err
} else if ok {
continue
}
}
if err := copy.Copy(ctx, src, s, dest, destPath, opt...); err != nil {
// if action.AttemptUnpackDockerCompatibility {
// if ok, err := unpack(ctx, src, s, dest, destPath, ch, timestampToTime(action.Timestamp)); err != nil {
// return err
// } else if ok {
// continue
// }
// }
// if err := copy.Copy(ctx, src, s, dest, destPath, opt...); err != nil {
// return err
// }
if err := copy_one(s); err != nil {
return err
}
}
Expand Down
Loading