Make snapshotting faster by using filepath.SkipDir. (#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.
This commit is contained in:
parent
f99e5f5c6d
commit
0c294138b8
|
|
@ -143,6 +143,14 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
|
||||||
// Save the fs state in a map to iterate over later.
|
// Save the fs state in a map to iterate over later.
|
||||||
memFs := map[string]os.FileInfo{}
|
memFs := map[string]os.FileInfo{}
|
||||||
filepath.Walk(s.directory, func(path string, info os.FileInfo, err error) error {
|
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
|
memFs[path] = info
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
@ -174,7 +182,6 @@ func (s *Snapshotter) TakeSnapshotFS() (string, error) {
|
||||||
logrus.Debugf("Not adding %s to layer, as it's whitelisted", path)
|
logrus.Debugf("Not adding %s to layer, as it's whitelisted", path)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(path)
|
maybeAdd, err := s.l.MaybeAdd(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -270,6 +270,15 @@ func extractFile(dest string, hdr *tar.Header, tr io.Reader) error {
|
||||||
return nil
|
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) {
|
func CheckWhitelist(path string) (bool, error) {
|
||||||
abs, err := filepath.Abs(path)
|
abs, err := filepath.Abs(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue