From bc6be5fe93f02a11077df6816c83edb363522934 Mon Sep 17 00:00:00 2001 From: Yasmin Valim Date: Tue, 16 Apr 2024 15:36:29 -0300 Subject: [PATCH] fix: improve performance --- config/v3_5_experimental/types/config.go | 54 ++++++++++-------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/config/v3_5_experimental/types/config.go b/config/v3_5_experimental/types/config.go index 352f73bda8..262e278370 100644 --- a/config/v3_5_experimental/types/config.go +++ b/config/v3_5_experimental/types/config.go @@ -35,6 +35,8 @@ var ( } ) +var paths = map[string]struct{}{} + func (cfg Config) Validate(c path.ContextPath) (r report.Report) { systemdPath := "/etc/systemd/system/" unitPaths := map[string]struct{}{} @@ -76,43 +78,21 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report { Path string Field string } - paths := map[string]struct{}{} r := report.Report{} for i, f := range cfg.Storage.Files { - if _, exists := paths[f.Path]; exists { - r.AddOnError(c.Append("storage", "files", i, "path"), errors.ErrPathConflictsParentDir) //TODO: should add different error? - return r - } - paths[f.Path] = struct{}{} - entries = append(entries, struct { - Path string - Field string - }{Path: f.Path, Field: "files"}) + r = handlePathConflict(f.Path, "files", i, c, r, errors.ErrPathConflictsParentDir) + addPathAndEntry(f.Path, "files", &entries) } for i, d := range cfg.Storage.Directories { - if _, exists := paths[d.Path]; exists { - r.AddOnError(c.Append("storage", "directories", i, "path"), errors.ErrPathConflictsParentDir) //TODO: should add different error? - return r - } - paths[d.Path] = struct{}{} - entries = append(entries, struct { - Path string - Field string - }{Path: d.Path, Field: "directories"}) + r = handlePathConflict(d.Path, "directories", i, c, r, errors.ErrPathConflictsParentDir) + addPathAndEntry(d.Path, "directories", &entries) } for i, l := range cfg.Storage.Links { - if _, exists := paths[l.Path]; exists { - r.AddOnError(c.Append("storage", "links", i, "path"), errors.ErrPathConflictsParentDir) //TODO: error to already exist path - return r - } - paths[l.Path] = struct{}{} - entries = append(entries, struct { - Path string - Field string - }{Path: l.Path, Field: "links"}) + r = handlePathConflict(l.Path, "links", i, c, r, errors.ErrPathConflictsParentDir) + addPathAndEntry(l.Path, "links", &entries) } sort.Slice(entries, func(i, j int) bool { @@ -122,7 +102,7 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report { for i, entry := range entries { if i > 0 && isWithin(entry.Path, entries[i-1].Path) { if entries[i-1].Field != "directories" { - r.AddOnError(c.Append("storage", entry.Field, i, "path"), errors.ErrPathConflictsParentDir) //TODO: conflict parent directories error + r.AddOnError(c.Append("storage", entry.Field, i, "path"), errors.ErrPathConflictsParentDir) return r } } @@ -131,7 +111,20 @@ func (cfg Config) validateParents(c path.ContextPath) report.Report { return r } -// check the depth +func handlePathConflict(path, fieldName string, index int, c path.ContextPath, r report.Report, err error) report.Report { + if _, exists := paths[path]; exists { + r.AddOnError(c.Append("storage", fieldName, index, "path"), err) + } + return r +} + +func addPathAndEntry(path, fieldName string, entries *[]struct{ Path, Field string }) { + *entries = append(*entries, struct { + Path string + Field string + }{Path: path, Field: fieldName}) +} + func depth(path string) uint { var count uint for p := filepath.Clean(path); p != "/" && p != "."; count++ { @@ -140,7 +133,6 @@ func depth(path string) uint { return count } -// isWithin checks if newPath is within prevPath. func isWithin(newPath, prevPath string) bool { return strings.HasPrefix(newPath, prevPath) && newPath != prevPath }