From 0a6abbd5e7773271dc4f7c9bde1b5434cc98fa00 Mon Sep 17 00:00:00 2001 From: German Lashevich Date: Sun, 24 Dec 2023 20:18:34 +0100 Subject: [PATCH] fix: check overlapping paths separately for directories and contents Signed-off-by: German Lashevich --- pkg/vendir/config/config.go | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/pkg/vendir/config/config.go b/pkg/vendir/config/config.go index a591d338..ea84c1e7 100644 --- a/pkg/vendir/config/config.go +++ b/pkg/vendir/config/config.go @@ -9,9 +9,10 @@ import ( "reflect" "strings" - "carvel.dev/vendir/pkg/vendir/version" semver "github.com/hashicorp/go-version" "sigs.k8s.io/yaml" + + "carvel.dev/vendir/pkg/vendir/version" ) const ( @@ -259,20 +260,34 @@ func (c Config) Lock(lockConfig LockConfig) error { } func (c Config) checkOverlappingPaths() error { + checkPaths := func(paths []string) error { + for i, path := range paths { + for i2, path2 := range paths { + if i != i2 && strings.HasPrefix(path2+string(filepath.Separator), path+string(filepath.Separator)) { + return fmt.Errorf("Expected to not manage overlapping paths: '%s' and '%s'", path2, path) + } + } + } + return nil + } + paths := []string{} + for _, dir := range c.Directories { + paths = append(paths, dir.Path) + } + + if err := checkPaths(paths); err != nil { + return err + } for _, dir := range c.Directories { - for _, con := range dir.Contents { - paths = append(paths, filepath.Join(dir.Path, con.Path)) + paths = []string{} + for _, cont := range dir.Contents { + paths = append(paths, filepath.Join(dir.Path, cont.Path)) } - } - for i, path := range paths { - for i2, path2 := range paths { - if i != i2 && strings.HasPrefix(path2+string(filepath.Separator), path+string(filepath.Separator)) { - return fmt.Errorf("Expected to not "+ - "manage overlapping paths: '%s' and '%s'", path2, path) - } + if err := checkPaths(paths); err != nil { + return err } }