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

Make sure COPY/ADD on dirs doesn't grab too many files #8186

Merged
merged 1 commit into from
Sep 23, 2014

Conversation

duglin
Copy link
Contributor

@duglin duglin commented Sep 23, 2014

While working on #6820 I noticed that the algorithm used to calculate the hash/cache had this line in builder/internals.go - around line 628:


if strings.HasPrefix(absFile, absOrigPath) {

which means that if you're asking for a dir called "foo" to be copied, it would also treat a sibling file named "foo_bar" to also be included in the hash calculation. To fix this I simply added a "/" to the end of the absOrigPath.

To verify this I'm also including a testcase to ensure that adding a new sibling file with a similar name as a the dir doesn't invalidate the cache from a previous build.

I also moved the path.Join() out of the for-loop since its a static value that doesn't change during the loop.

To be honest, I would love to revamp a lot of this code. I'm very uncomfortable with the idea that we calculate the list of things to copy twice, once for the hash calculation and once during the copy/tar process. This leaves it very open to the two processes being different and inconsistent. I would prefer to just calc the total list of files once and use that list during both times but I'll leave that for another PR. However, I wouldn't mind some feedback on this idea because perhaps I'm missing something.

Signed-off-by: Doug Davis dug@us.ibm.com

@@ -293,9 +293,10 @@ func calcCopyInfo(b *Builder, cmdName string, ci *copyInfo, allowRemote bool, al
return err
} else if fi.IsDir() {
var subfiles []string
absOrigPath := path.Join(b.contextPath, ci.origPath)
absOrigPath += "/"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't you check that there is no "/" already?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a check for that - thanks

@duglin duglin force-pushed the ErrInCopyCache branch 2 times, most recently from 2bc3e58 to 31c3beb Compare September 23, 2014 19:53
@erikh
Copy link
Contributor

erikh commented Sep 23, 2014

LGTM

dockerfile := `
FROM scratch
MAINTAINER dockerio
ADD dir /tmp/`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use the preferred COPY.
No need for MAINTAINER.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tiborvass ok, done!

Add check for / first - per LK4D4's comment.
Add a comment to explain why we're adding a /

Signed-off-by: Doug Davis <dug@us.ibm.com>
@duglin
Copy link
Contributor Author

duglin commented Sep 23, 2014

Despite the drone error, all of the tests do run for me

@jessfraz
Copy link
Contributor

LGTM

1 similar comment
@vieux
Copy link
Contributor

vieux commented Sep 23, 2014

LGTM

vieux added a commit that referenced this pull request Sep 23, 2014
Make sure COPY/ADD on dirs doesn't grab too many files
@vieux vieux merged commit 81a6432 into moby:master Sep 23, 2014
@duglin duglin deleted the ErrInCopyCache branch September 23, 2014 22:21
@thaJeztah thaJeztah added area/builder kind/bugfix PR's that fix bugs labels Dec 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/builder kind/bugfix PR's that fix bugs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants