From cac614a4b7ea684b5b4183dc47f1fb50c869fde9 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Sat, 10 Sep 2022 22:51:51 -0600 Subject: [PATCH] Correct handling of exclude patterns Signed-off-by: Jonathon Anderson --- internal/pkg/container/build.go | 13 +++---------- internal/pkg/util/util.go | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/internal/pkg/container/build.go b/internal/pkg/container/build.go index 147334ce..cd3e45d9 100644 --- a/internal/pkg/container/build.go +++ b/internal/pkg/container/build.go @@ -2,7 +2,6 @@ package container import ( "path" - "strings" "github.com/pkg/errors" @@ -27,20 +26,14 @@ func Build(name string, buildForce bool) error { } } - excludes_file := path.Join(rootfsPath, "./etc/warewulf/excludes") ignore := []string{} - + excludes_file := path.Join(rootfsPath, "./etc/warewulf/excludes") if util.IsFile(excludes_file) { - ignore, err := util.ReadFile(excludes_file) + var err error + ignore, err = util.ReadFile(excludes_file) if err != nil { return errors.Wrapf(err, "Failed creating directory: %s", imagePath) } - - for i, pattern := range ignore { - if ( strings.HasPrefix(pattern, "/") ) { - ignore[i] = pattern[1:] - } - } } err := util.BuildFsImage( diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index 05ebfe0d..17b95a85 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -281,8 +281,9 @@ func FindFilterFiles( for i, pattern := range(ignore) { + ignore[i] = strings.TrimLeft(pattern, "/") if strings.HasPrefix(pattern, "./") { - ignore[i] = pattern[2:] + ignore[i] = strings.TrimPrefix(pattern, "./") } wwlog.Debug("Ignore pattern (%d): %s", i, ignore[i]) } @@ -309,6 +310,7 @@ func FindFilterFiles( // recursivly include from the matched directory num_init := len(ofiles) + ignoreDirs := []string{} err = filepath.Walk(ifile, func(location string, info os.FileInfo, err error) error { var file string @@ -331,6 +333,12 @@ func FindFilterFiles( return nil } + for _, ignoreDir := range(ignoreDirs) { + if strings.HasPrefix(location, ignoreDir) { + wwlog.Debug("Ignored (dir): %s", location) + return nil + } + } for i, pattern := range(ignore) { m, err := filepath.Match(pattern, location) if err != nil { @@ -339,6 +347,9 @@ func FindFilterFiles( if m { wwlog.Debug("Ignored (%d): %s", i, file) + if info.IsDir() { + ignoreDirs = append(ignoreDirs, file) + } return nil } } @@ -354,7 +365,7 @@ func FindFilterFiles( if err != nil { return ofiles, err } - }else{ + } else { wwlog.Debug("Included: %s", ifile) ofiles = append(ofiles, ifile) }