-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
support container to container copy #10728
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 |
---|---|---|
|
@@ -80,11 +80,14 @@ func cp(cmd *cobra.Command, args []string) error { | |
return err | ||
} | ||
|
||
if len(sourceContainerStr) > 0 { | ||
if len(sourceContainerStr) > 0 && len(destContainerStr) > 0 { | ||
return copyContainerToContainer(sourceContainerStr, sourcePath, destContainerStr, destPath) | ||
} else if len(sourceContainerStr) > 0 { | ||
return copyFromContainer(sourceContainerStr, sourcePath, destPath) | ||
} | ||
|
||
return copyToContainer(destContainerStr, destPath, sourcePath) | ||
|
||
} | ||
|
||
// containerMustExist returns an error if the specified container does not | ||
|
@@ -113,6 +116,82 @@ func doCopy(funcA func() error, funcB func() error) error { | |
return errorhandling.JoinErrors(copyErrors) | ||
} | ||
|
||
func copyContainerToContainer(fromContainer string, sourcePath string, toContainer string, destPath string) error { | ||
if err := containerMustExist(fromContainer); err != nil { | ||
return err | ||
} | ||
|
||
if err := containerMustExist(toContainer); err != nil { | ||
return err | ||
} | ||
|
||
fromContainerInfo, err := registry.ContainerEngine().ContainerStat(registry.GetContext(), fromContainer, sourcePath) | ||
if err != nil { | ||
return errors.Wrapf(err, "%q could not be found on container %s", sourcePath, fromContainer) | ||
} | ||
|
||
var toContainerBaseName string | ||
_ = toContainerBaseName | ||
toContainerInfo, err := registry.ContainerEngine().ContainerStat(registry.GetContext(), toContainer, destPath) | ||
if err != nil { | ||
if strings.HasSuffix(destPath, "/") { | ||
return errors.Wrapf(err, "%q could not be found on container %s", destPath, toContainer) | ||
} | ||
if toContainerInfo != nil { | ||
toContainerBaseName = filepath.Base(toContainerInfo.LinkTarget) | ||
} else { | ||
toContainerBaseName = filepath.Base(destPath) | ||
} | ||
|
||
parentDir, err := containerParentDir(toContainer, destPath) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not determine parent dir of %q on container %s", destPath, toContainer) | ||
} | ||
toContainerInfo, err = registry.ContainerEngine().ContainerStat(registry.GetContext(), toContainer, parentDir) | ||
if err != nil { | ||
return errors.Wrapf(err, "%q could not be found on container %s", destPath, toContainer) | ||
} | ||
} else { | ||
toContainerBaseName = filepath.Base(toContainerInfo.LinkTarget) | ||
} | ||
|
||
if fromContainerInfo.IsDir && !toContainerInfo.IsDir { | ||
return errors.New("destination must be a directory when copying a directory") | ||
} | ||
|
||
fromContainerTarget, toContainerTarget := fromContainerInfo.LinkTarget, toContainerInfo.LinkTarget | ||
if !toContainerInfo.IsDir { | ||
toContainerTarget = filepath.Dir(destPath) | ||
} | ||
reader, writer := io.Pipe() | ||
|
||
fromContainerCopy := func() error { | ||
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. potentially could consolidate 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. there might be some changes to the functions, so depending on what the final outcome looks like i'll refactor the code |
||
defer writer.Close() | ||
copyFunc, err := registry.ContainerEngine().ContainerCopyToArchive(registry.GetContext(), fromContainer, fromContainerTarget, writer) | ||
if err != nil { | ||
return err | ||
} | ||
if err := copyFunc(); err != nil { | ||
return errors.Wrap(err, "error copying from container") | ||
} | ||
return nil | ||
} | ||
|
||
toContainerCopy := func() error { | ||
defer reader.Close() | ||
copyFunc, err := registry.ContainerEngine().ContainerCopyFromArchive(registry.GetContext(), toContainer, toContainerTarget, reader) | ||
if err != nil { | ||
return err | ||
} | ||
if err := copyFunc(); err != nil { | ||
return errors.Wrap(err, "error copying to container") | ||
} | ||
return nil | ||
} | ||
|
||
return doCopy(fromContainerCopy, toContainerCopy) | ||
} | ||
|
||
// copyFromContainer copies from the containerPath on the container to hostPath. | ||
func copyFromContainer(container string, containerPath string, hostPath string) error { | ||
if err := containerMustExist(container); err != nil { | ||
|
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.
can you change to
toContainerBaseName := ""
removes an unnecessary line.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.
Also, I am seeing you are getting an ineffectual assignment error here. Where are you using
toContainerBaseName
after setting it?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.
sure, will make the changes once I figure everything out
I am not completely sure tbh, I had to do _ = toContainerBaseName to get rid of that unused variable error
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.
ok @infiniteregrets you're getting that error because you set the base name and never actually use it. the
_ =
is just a clever way to trick golang. You should use that name somewhere in your copy code.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.
@cdoern I am using it?
This looks like a shadowing issue? Im not sure but I'll try to figure it. Thanks!