-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
dockerfile: add --chmod support for COPY/ADD command #1492
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ import ( | |
"fmt" | ||
"math" | ||
"net/url" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"sort" | ||
|
@@ -493,7 +494,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error { | |
case *instructions.WorkdirCommand: | ||
err = dispatchWorkdir(d, c, true, &opt) | ||
case *instructions.AddCommand: | ||
err = dispatchCopy(d, c.SourcesAndDest, opt.buildContext, true, c, c.Chown, c.Location(), opt) | ||
err = dispatchCopy(d, c.SourcesAndDest, opt.buildContext, true, c, c.Chown, c.Chmod, c.Location(), opt) | ||
if err == nil { | ||
for _, src := range c.Sources() { | ||
if !strings.HasPrefix(src, "http://") && !strings.HasPrefix(src, "https://") { | ||
|
@@ -528,7 +529,7 @@ func dispatch(d *dispatchState, cmd command, opt dispatchOpt) error { | |
if len(cmd.sources) != 0 { | ||
l = cmd.sources[0].state | ||
} | ||
err = dispatchCopy(d, c.SourcesAndDest, l, false, c, c.Chown, c.Location(), opt) | ||
err = dispatchCopy(d, c.SourcesAndDest, l, false, c, c.Chown, c.Chmod, c.Location(), opt) | ||
if err == nil && len(cmd.sources) == 0 { | ||
for _, src := range c.Sources() { | ||
d.ctxPaths[path.Join("/", filepath.ToSlash(src))] = struct{}{} | ||
|
@@ -722,7 +723,7 @@ func dispatchWorkdir(d *dispatchState, c *instructions.WorkdirCommand, commit bo | |
return nil | ||
} | ||
|
||
func dispatchCopyFileOp(d *dispatchState, c instructions.SourcesAndDest, sourceState llb.State, isAddCommand bool, cmdToPrint fmt.Stringer, chown string, loc []parser.Range, opt dispatchOpt) error { | ||
func dispatchCopyFileOp(d *dispatchState, c instructions.SourcesAndDest, sourceState llb.State, isAddCommand bool, cmdToPrint fmt.Stringer, chown string, chmod string, loc []parser.Range, opt dispatchOpt) error { | ||
pp, err := pathRelativeToWorkingDir(d.state, c.Dest()) | ||
if err != nil { | ||
return err | ||
|
@@ -738,6 +739,15 @@ func dispatchCopyFileOp(d *dispatchState, c instructions.SourcesAndDest, sourceS | |
copyOpt = append(copyOpt, llb.WithUser(chown)) | ||
} | ||
|
||
var mode *os.FileMode | ||
if chmod != "" { | ||
p, err := strconv.ParseUint(chmod, 8, 32) | ||
if err == nil { | ||
perm := os.FileMode(p) | ||
mode = &perm | ||
} | ||
} | ||
|
||
commitMessage := bytes.NewBufferString("") | ||
if isAddCommand { | ||
commitMessage.WriteString("ADD") | ||
|
@@ -780,6 +790,7 @@ func dispatchCopyFileOp(d *dispatchState, c instructions.SourcesAndDest, sourceS | |
} | ||
} else { | ||
opts := append([]llb.CopyOption{&llb.CopyInfo{ | ||
Mode: mode, | ||
FollowSymlinks: true, | ||
CopyDirContentsOnly: true, | ||
AttemptUnpack: isAddCommand, | ||
|
@@ -820,9 +831,16 @@ func dispatchCopyFileOp(d *dispatchState, c instructions.SourcesAndDest, sourceS | |
return commitToHistory(&d.image, commitMessage.String(), true, &d.state) | ||
} | ||
|
||
func dispatchCopy(d *dispatchState, c instructions.SourcesAndDest, sourceState llb.State, isAddCommand bool, cmdToPrint fmt.Stringer, chown string, loc []parser.Range, opt dispatchOpt) error { | ||
func dispatchCopy(d *dispatchState, c instructions.SourcesAndDest, sourceState llb.State, isAddCommand bool, cmdToPrint fmt.Stringer, chown string, chmod string, loc []parser.Range, opt dispatchOpt) error { | ||
if useFileOp(opt.buildArgValues, opt.llbCaps) { | ||
return dispatchCopyFileOp(d, c, sourceState, isAddCommand, cmdToPrint, chown, loc, opt) | ||
return dispatchCopyFileOp(d, c, sourceState, isAddCommand, cmdToPrint, chown, chmod, loc, opt) | ||
} | ||
|
||
if chmod != "" { | ||
if opt.llbCaps != nil && opt.llbCaps.Supports(pb.CapFileBase) != nil { | ||
return errors.Wrap(opt.llbCaps.Supports(pb.CapFileBase), "chmod is not supported") | ||
} | ||
return errors.New("chmod is not supported") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @hinshun only on old buildkit instances that don't have fileop support. Otherwise, it is an early return on line 836. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see, so if BuildKit doesn't have fileop and chmod is specified, it should still return error... a bit confusing. My bad. |
||
} | ||
|
||
img := llb.Image(opt.copyImage, llb.MarkImageInternal, llb.Platform(opt.buildPlatforms[0]), WithInternalName("helper image for file operations")) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.