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

check file changed in loop #1302

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/snapshot/layered_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ import (
)

type LayeredMap struct {
layers []map[string]string
whiteouts []map[string]string
hasher func(string) (string, error)
layers []map[string]string
whiteouts []map[string]string
layerHashCache map[string]string
hasher func(string) (string, error)
// cacheHasher doesn't include mtime in it's hash so that filesystem cache keys are stable
cacheHasher func(string) (string, error)
}
Expand All @@ -43,6 +44,7 @@ func NewLayeredMap(h func(string) (string, error), c func(string) (string, error
cacheHasher: c,
}
l.layers = []map[string]string{}
l.layerHashCache = map[string]string{}
return &l
}

Expand Down Expand Up @@ -103,7 +105,14 @@ func (l *LayeredMap) MaybeAddWhiteout(s string) bool {
// Add will add the specified file s to the layered map.
func (l *LayeredMap) Add(s string) error {
// Use hash function and add to layers
newV, err := l.hasher(s)
newV, err := func(s string) (string, error) {
if v, ok := l.layerHashCache[s]; ok {
// clear it cache for next layer.
delete(l.layerHashCache, s)
return v, nil
}
return l.hasher(s)
}(s)
if err != nil {
return fmt.Errorf("error creating hash for %s: %v", s, err)
}
Expand All @@ -126,6 +135,7 @@ func (l *LayeredMap) CheckFileChange(s string) (bool, error) {
}
return false, err
}
l.layerHashCache[s] = newV
oldV, ok := l.Get(s)
if ok && newV == oldV {
return false, nil
Expand Down
53 changes: 19 additions & 34 deletions pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,61 +135,46 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {

timer := timing.Start("Walking filesystem")

foundPaths := make([]string, 0)
changedPaths := make([]string, 0)

// Get a list of all the files that existed before this layer
existingPaths := s.l.getFlattenedPathsForWhiteOut()

godirwalk.Walk(s.directory, &godirwalk.Options{
Callback: func(path string, ent *godirwalk.Dirent) error {
if util.IsInIgnoreList(path) {
if util.IsDestDir(path) {
logrus.Tracef("Skipping paths under %s, as it is a ignored directory", path)

return filepath.SkipDir
}

return nil
}

foundPaths = append(foundPaths, path)

if ok, err := s.l.CheckFileChange(path); err != nil {
return err
} else if ok {
changedPaths = append(changedPaths, path)
}
delete(existingPaths, path)
return nil
},
Unsorted: true,
},
)
timing.DefaultRun.Stop(timer)
timer = timing.Start("Resolving Paths")
// First handle whiteouts
// Get a list of all the files that existed before this layer
existingPaths := s.l.getFlattenedPathsForWhiteOut()

filesToAdd := []string{}
resolvedMemFs := make(map[string]bool)

for _, path := range foundPaths {
delete(existingPaths, path)
resolvedFiles, err := filesystem.ResolvePaths([]string{path}, s.ignorelist)
if err != nil {
return nil, nil, err
}
for _, path := range resolvedFiles {
// Continue if this path is already processed
if _, ok := resolvedMemFs[path]; ok {
continue
}
if util.CheckIgnoreList(path) {
logrus.Tracef("Not adding %s to layer, as it's whitelisted", path)
continue
}
// Only add changed files.
fileChanged, err := s.l.CheckFileChange(path)
if err != nil {
return nil, nil, fmt.Errorf("could not check if file has changed %s %s", path, err)
}
if fileChanged {
logrus.Tracef("Adding file %s to layer, because it was changed.", path)
filesToAdd = append(filesToAdd, path)
}
resolvedFiles, err := filesystem.ResolvePaths(changedPaths, s.ignorelist)
if err != nil {
return nil, nil, err
}
for _, path := range resolvedFiles {
if util.CheckIgnoreList(path) {
logrus.Tracef("Not adding %s to layer, as it's whitelisted", path)
continue
}
filesToAdd = append(filesToAdd, path)
}

// The paths left here are the ones that have been deleted in this layer.
Expand Down
2 changes: 2 additions & 0 deletions pkg/snapshot/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestSnapshotFSFileChange(t *testing.T) {
}
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(batPath) {
if path == "/" {
snapshotFiles["/"] = ""
continue
}
snapshotFiles[path+"/"] = ""
Expand Down Expand Up @@ -164,6 +165,7 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
}
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(batPathWithoutLeadingSlash) {
if path == "/" {
snapshotFiles["/"] = ""
continue
}
snapshotFiles[path+"/"] = ""
Expand Down