Fix issue with file caching + prevent wrong cache hit

Docker-DCO-1.0-Signed-off-by: Guillaume J. Charmes <guillaume.charmes@docker.com> (github: creack)
This commit is contained in:
Guillaume J. Charmes 2014-01-07 16:53:55 -08:00
parent ef7e000a13
commit f3103e5c91
No known key found for this signature in database
GPG Key ID: B33E4642CB6E3FF3
2 changed files with 26 additions and 8 deletions

View File

@ -407,18 +407,20 @@ func (b *buildFile) CmdAdd(args string) error {
hash string hash string
sums = b.context.GetSums() sums = b.context.GetSums()
) )
// Has tarsum strips the '.' and './', we put it back for comparaison.
for file, sum := range sums {
if len(file) == 0 || file[0] != '.' && file[0] != '/' {
delete(sums, file)
sums["./"+file] = sum
}
}
if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil { if fi, err := os.Stat(path.Join(b.contextPath, origPath)); err != nil {
return err return err
} else if fi.IsDir() { } else if fi.IsDir() {
var subfiles []string var subfiles []string
for file, sum := range sums { for file, sum := range sums {
// Has tarsum stips the '.' and './', we put it back for comparaison.
if len(file) == 0 {
file = "./"
}
if file[0] != '.' && file[0] != '/' {
file = "./" + file
}
if strings.HasPrefix(file, origPath) { if strings.HasPrefix(file, origPath) {
subfiles = append(subfiles, sum) subfiles = append(subfiles, sum)
} }
@ -435,7 +437,8 @@ func (b *buildFile) CmdAdd(args string) error {
if err != nil { if err != nil {
return err return err
} }
if hit { // If we do not have a hash, never use the cache
if hit && hash != "" {
return nil return nil
} }
} }

View File

@ -532,6 +532,21 @@ func TestBuildADDLocalFileWithCache(t *testing.T) {
if id5 == id6 { if id5 == id6 {
t.Fatal("The cache should have been invalided but hasn't.") t.Fatal("The cache should have been invalided but hasn't.")
} }
template.dockerfile += `
add bar /src2/bar2
add /bar /src2/bar3
run ls /src2/bar2 /src2/bar3
`
id7 := checkCacheBehaviorFromEngime(t, template, true, eng)
if id6 == id7 {
t.Fatal("The cache should have been invalided but hasn't.")
}
template.files[1][1] = "hello5"
id8 := checkCacheBehaviorFromEngime(t, template, true, eng)
if id7 == id8 {
t.Fatal("The cache should have been invalided but hasn't.")
}
} }
func TestBuildADDLocalFileWithoutCache(t *testing.T) { func TestBuildADDLocalFileWithoutCache(t *testing.T) {