Merge pull request #1302 from tejal29/check_file_changed_in_loop

check file changed in loop
This commit is contained in:
Tejal Desai 2020-06-06 16:45:19 -07:00 committed by GitHub
commit e73dea426d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 38 deletions

View File

@ -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)
}
@ -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
}
@ -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)
}
@ -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

View File

@ -135,22 +135,27 @@ 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,
@ -158,38 +163,18 @@ func (s *Snapshotter) scanFullFilesystem() ([]string, []string, error) {
)
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.

View File

@ -66,6 +66,7 @@ func TestSnapshotFSFileChange(t *testing.T) {
}
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(batPath) {
if path == "/" {
snapshotFiles["/"] = ""
continue
}
snapshotFiles[path+"/"] = ""
@ -164,6 +165,7 @@ func TestSnapshotFSChangePermissions(t *testing.T) {
}
for _, path := range util.ParentDirectoriesWithoutLeadingSlash(batPathWithoutLeadingSlash) {
if path == "/" {
snapshotFiles["/"] = ""
continue
}
snapshotFiles[path+"/"] = ""