Skip to content

Commit

Permalink
Make snapshotting faster by using filepath.SkipDir. (GoogleContainerT…
Browse files Browse the repository at this point in the history
…ools#451)

filepath.Walk has a special error you can return from your walkFn
indicating it should skip directories. This change makes use of that
to skip whitelisted directories.
  • Loading branch information
dlorenc authored Nov 14, 2018
1 parent f99e5f5 commit 0c29413
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
9 changes: 8 additions & 1 deletion pkg/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
// Save the fs state in a map to iterate over later.
memFs := map[string]os.FileInfo{}
filepath.Walk(s.directory, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if util.IsInWhitelist(path) {
logrus.Infof("Skipping paths under %s, as it is a whitelisted directory", path)
return filepath.SkipDir
}

memFs[path] = info
return nil
})
Expand Down Expand Up @@ -174,7 +182,6 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
logrus.Debugf("Not adding %s to layer, as it's whitelisted", path)
continue
}

// Only add to the tar if we add it to the layeredmap.
maybeAdd, err := s.l.MaybeAdd(path)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions pkg/util/fs_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error {
return nil
}

func IsInWhitelist(path string) bool {
for _, wl := range whitelist {
if !wl.PrefixMatchOnly && path == wl.Path {
return true
}
}
return false
}

func CheckWhitelist(path string) (bool, error) {
abs, err := filepath.Abs(path)
if err != nil {
Expand Down

0 comments on commit 0c29413

Please sign in to comment.