Only add whiteout files once (#270)
* Only add whiteout files once * Updated vars
This commit is contained in:
parent
8a2492d241
commit
71c83e369c
|
|
@ -1,3 +1,4 @@
|
||||||
FROM busybox@sha256:1bd6df27274fef1dd36eb529d0f4c8033f61c675d6b04213dd913f902f7cafb5
|
FROM busybox@sha256:1bd6df27274fef1dd36eb529d0f4c8033f61c675d6b04213dd913f902f7cafb5
|
||||||
ADD context/tars /tmp/tars
|
ADD context/tars /tmp/tars
|
||||||
RUN mv /tmp/tars /foo
|
RUN mv /tmp/tars /foo
|
||||||
|
RUN echo "hi"
|
||||||
|
|
|
||||||
|
|
@ -22,8 +22,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type LayeredMap struct {
|
type LayeredMap struct {
|
||||||
layers []map[string]string
|
layers []map[string]string
|
||||||
hasher func(string) (string, error)
|
whiteouts []map[string]string
|
||||||
|
hasher func(string) (string, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLayeredMap(h func(string) (string, error)) *LayeredMap {
|
func NewLayeredMap(h func(string) (string, error)) *LayeredMap {
|
||||||
|
|
@ -35,6 +36,7 @@ func NewLayeredMap(h func(string) (string, error)) *LayeredMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *LayeredMap) Snapshot() {
|
func (l *LayeredMap) Snapshot() {
|
||||||
|
l.whiteouts = append(l.whiteouts, map[string]string{})
|
||||||
l.layers = append(l.layers, map[string]string{})
|
l.layers = append(l.layers, map[string]string{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -62,6 +64,24 @@ func (l *LayeredMap) Get(s string) (string, bool) {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LayeredMap) GetWhiteout(s string) (string, bool) {
|
||||||
|
for i := len(l.whiteouts) - 1; i >= 0; i-- {
|
||||||
|
if v, ok := l.whiteouts[i][s]; ok {
|
||||||
|
return v, ok
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *LayeredMap) MaybeAddWhiteout(s string) (bool, error) {
|
||||||
|
whiteout, ok := l.GetWhiteout(s)
|
||||||
|
if ok && whiteout == s {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
l.whiteouts[len(l.whiteouts)-1][s] = s
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (l *LayeredMap) MaybeAdd(s string) (bool, error) {
|
func (l *LayeredMap) MaybeAdd(s string) (bool, error) {
|
||||||
oldV, ok := l.Get(s)
|
oldV, ok := l.Get(s)
|
||||||
newV, err := l.hasher(s)
|
newV, err := l.hasher(s)
|
||||||
|
|
|
||||||
|
|
@ -19,12 +19,13 @@ package snapshot
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
"bytes"
|
"bytes"
|
||||||
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/GoogleContainerTools/kaniko/pkg/util"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
|
// Snapshotter holds the root directory from which to take snapshots, and a list of snapshots taken
|
||||||
|
|
@ -103,11 +104,11 @@ func (s *Snapshotter) snapshotFiles(f io.Writer, files []string) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
// Only add to the tar if we add it to the layeredmap.
|
// Only add to the tar if we add it to the layeredmap.
|
||||||
maybeAdd, err := s.l.MaybeAdd(file)
|
addFile, err := s.l.MaybeAdd(file)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if maybeAdd {
|
if addFile {
|
||||||
filesAdded = true
|
filesAdded = true
|
||||||
if err := util.AddToTar(file, info, s.hardlinks, w); err != nil {
|
if err := util.AddToTar(file, info, s.hardlinks, w); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
|
@ -141,10 +142,16 @@ func (s *Snapshotter) snapShotFS(f io.Writer) (bool, error) {
|
||||||
// Only add the whiteout if the directory for the file still exists.
|
// Only add the whiteout if the directory for the file still exists.
|
||||||
dir := filepath.Dir(path)
|
dir := filepath.Dir(path)
|
||||||
if _, ok := memFs[dir]; ok {
|
if _, ok := memFs[dir]; ok {
|
||||||
logrus.Infof("Adding whiteout for %s", path)
|
addWhiteout, err := s.l.MaybeAddWhiteout(path)
|
||||||
filesAdded = true
|
if err != nil {
|
||||||
if err := util.Whiteout(path, w); err != nil {
|
return false, nil
|
||||||
return false, err
|
}
|
||||||
|
if addWhiteout {
|
||||||
|
logrus.Infof("Adding whiteout for %s", path)
|
||||||
|
filesAdded = true
|
||||||
|
if err := util.Whiteout(path, w); err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue