From b0164c36b93c4c2ea60b57117c42ca1b7647bb22 Mon Sep 17 00:00:00 2001 From: Jonathon Anderson Date: Thu, 19 Dec 2024 23:34:50 -0700 Subject: [PATCH] Remove Chdir from util.FindFiles Signed-off-by: Jonathon Anderson --- internal/pkg/config/buildconfig.go.in | 14 +++++------ internal/pkg/config/dhcp.go | 6 +---- internal/pkg/config/mounts.go | 8 ++---- internal/pkg/config/nfs.go | 6 ++--- internal/pkg/config/util.go | 5 ++++ internal/pkg/util/util.go | 35 +++++++++++++-------------- internal/pkg/util/util_test.go | 35 +++++++++++++++++++++++++++ 7 files changed, 68 insertions(+), 41 deletions(-) create mode 100644 internal/pkg/config/util.go diff --git a/internal/pkg/config/buildconfig.go.in b/internal/pkg/config/buildconfig.go.in index 45dd5969..9b3a5859 100644 --- a/internal/pkg/config/buildconfig.go.in +++ b/internal/pkg/config/buildconfig.go.in @@ -2,8 +2,6 @@ package config import ( "path" - - "github.com/warewulf/warewulf/internal/pkg/util" ) var ConfigFile = "@SYSCONFDIR@/warewulf/warewulf.conf" @@ -36,7 +34,7 @@ type TFTPConf struct { } func (this TFTPConf) Enabled() bool { - return util.BoolP(this.EnabledP) + return BoolP(this.EnabledP) } // WarewulfConf adds additional Warewulf-specific configuration to @@ -52,23 +50,23 @@ type WarewulfConf struct { } func (this WarewulfConf) Secure() bool { - return util.BoolP(this.SecureP) + return BoolP(this.SecureP) } func (this WarewulfConf) AutobuildOverlays() bool { - return util.BoolP(this.AutobuildOverlaysP) + return BoolP(this.AutobuildOverlaysP) } func (this WarewulfConf) EnableHostOverlay() bool { - return util.BoolP(this.EnableHostOverlayP) + return BoolP(this.EnableHostOverlayP) } func (this WarewulfConf) Syslog() bool { - return util.BoolP(this.SyslogP) + return BoolP(this.SyslogP) } func (this WarewulfConf) GrubBoot() bool { - return util.BoolP(this.GrubBootP) + return BoolP(this.GrubBootP) } func (paths BuildConfig) NodesConf() string { diff --git a/internal/pkg/config/dhcp.go b/internal/pkg/config/dhcp.go index 56c70986..7149594d 100644 --- a/internal/pkg/config/dhcp.go +++ b/internal/pkg/config/dhcp.go @@ -1,9 +1,5 @@ package config -import ( - "github.com/warewulf/warewulf/internal/pkg/util" -) - // DHCPConf represents the configuration for the DHCP service that // Warewulf will configure. type DHCPConf struct { @@ -15,5 +11,5 @@ type DHCPConf struct { } func (this DHCPConf) Enabled() bool { - return util.BoolP(this.EnabledP) + return BoolP(this.EnabledP) } diff --git a/internal/pkg/config/mounts.go b/internal/pkg/config/mounts.go index f228a4e5..bc4cf920 100644 --- a/internal/pkg/config/mounts.go +++ b/internal/pkg/config/mounts.go @@ -1,9 +1,5 @@ package config -import ( - "github.com/warewulf/warewulf/internal/pkg/util" -) - // A MountEntry represents a bind mount that is applied to a container // during exec and shell. type MountEntry struct { @@ -15,9 +11,9 @@ type MountEntry struct { } func (this MountEntry) ReadOnly() bool { - return util.BoolP(this.ReadOnlyP) + return BoolP(this.ReadOnlyP) } func (this MountEntry) Copy() bool { - return util.BoolP(this.CopyP) + return BoolP(this.CopyP) } diff --git a/internal/pkg/config/nfs.go b/internal/pkg/config/nfs.go index ca6fc8b3..061eb199 100644 --- a/internal/pkg/config/nfs.go +++ b/internal/pkg/config/nfs.go @@ -2,8 +2,6 @@ package config import ( "github.com/creasty/defaults" - - "github.com/warewulf/warewulf/internal/pkg/util" ) // NFSConf represents the NFS configuration that will be used by @@ -16,7 +14,7 @@ type NFSConf struct { } func (this NFSConf) Enabled() bool { - return util.BoolP(this.EnabledP) + return BoolP(this.EnabledP) } // An NFSExportConf reprents a single NFS export / mount. @@ -28,7 +26,7 @@ type NFSExportConf struct { } func (this NFSExportConf) Mount() bool { - return util.BoolP(this.MountP) + return BoolP(this.MountP) } // Implements the Unmarshal interface for NFSConf to set default diff --git a/internal/pkg/config/util.go b/internal/pkg/config/util.go new file mode 100644 index 00000000..1198d46b --- /dev/null +++ b/internal/pkg/config/util.go @@ -0,0 +1,5 @@ +package config + +func BoolP(p *bool) bool { + return p != nil && *p +} diff --git a/internal/pkg/util/util.go b/internal/pkg/util/util.go index bf57fed4..a47037e3 100644 --- a/internal/pkg/util/util.go +++ b/internal/pkg/util/util.go @@ -18,10 +18,6 @@ import ( "github.com/warewulf/warewulf/internal/pkg/wwlog" ) -func BoolP(p *bool) bool { - return p != nil && *p -} - func FirstError(errs ...error) (err error) { for _, e := range errs { if err == nil { @@ -136,37 +132,40 @@ func ValidString(pattern string, expr string) bool { return false } -// ****************************************************************************** func FindFiles(path string) []string { var ret []string - wwlog.Debug("Changing directory to FindFiles path: %s", path) - err := os.Chdir(path) - if err != nil { - wwlog.Warn("Could not chdir() to: %s", path) - return ret - } + wwlog.Debug("Finding files in path: %s", path) - err = filepath.Walk(".", func(location string, info os.FileInfo, err error) error { + err := filepath.Walk(path, func(location string, info os.FileInfo, err error) error { if err != nil { + wwlog.Warn("Error walking path %s: %v", location, err) return err } - if location == "." { + // Get the relative path from the base directory + relPath, relErr := filepath.Rel(path, location) + if relErr != nil { + wwlog.Warn("Error computing relative path for %s: %v", location, relErr) + return relErr + } + + if relPath == "." { return nil } - if IsDir(location) { - wwlog.Debug("FindFiles() found directory: %s", location) - ret = append(ret, location+"/") + if info.IsDir() { + wwlog.Debug("FindFiles() found directory: %s", relPath) + ret = append(ret, relPath+"/") } else { - wwlog.Debug("FindFiles() found file: %s", location) - ret = append(ret, location) + wwlog.Debug("FindFiles() found file: %s", relPath) + ret = append(ret, relPath) } return nil }) if err != nil { + wwlog.Warn("Error during file walk: %v", err) return ret } diff --git a/internal/pkg/util/util_test.go b/internal/pkg/util/util_test.go index e5087212..a8a4f4c1 100644 --- a/internal/pkg/util/util_test.go +++ b/internal/pkg/util/util_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/warewulf/warewulf/internal/pkg/testenv" "github.com/warewulf/warewulf/internal/pkg/wwlog" ) @@ -19,6 +20,40 @@ func TryCreatePath(t *testing.T, elem ...string) { } } +func Test_FindFiles(t *testing.T) { + var tests = map[string]struct { + createFiles []string + findFiles []string + }{ + "no files": { + createFiles: []string{}, + findFiles: nil, + }, + "single file": { + createFiles: []string{"testfile"}, + findFiles: []string{"testfile"}, + }, + "nested file": { + createFiles: []string{"testdir/testfile"}, + findFiles: []string{"testdir/", "testdir/testfile"}, + }, + } + + for name, tt := range tests { + t.Run(name, func(t *testing.T) { + env := testenv.New(t) + defer env.RemoveAll(t) + env.MkdirAll(t, "/test") + for _, file_ := range tt.createFiles { + env.CreateFile(t, filepath.Join("/test", file_)) + } + + files := FindFiles(env.GetPath("/test")) + assert.Equal(t, tt.findFiles, files) + }) + } +} + func Test_FindFilterFiles(t *testing.T) { wwlog.SetLogLevel(wwlog.DEBUG) dir, err := os.MkdirTemp(os.TempDir(), "warewulf-test")