From 4985d0d060bb4bf061be54cebcf128337782972f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Fri, 19 Sep 2025 15:36:53 +0200 Subject: [PATCH 1/4] Add readFile to FileManager --- fileutil/fileutil.go | 5 +++++ fileutil/fileutil_test.go | 7 +++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go index 4b2bfa5..17446b3 100644 --- a/fileutil/fileutil.go +++ b/fileutil/fileutil.go @@ -18,6 +18,7 @@ type FileManager interface { Write(path string, value string, perm os.FileMode) error WriteBytes(path string, value []byte) error FileSizeInBytes(pth string) (int64, error) + ReadFile(name string) ([]byte, error) } type fileManager struct { @@ -103,3 +104,7 @@ func (fileManager) FileSizeInBytes(pth string) (int64, error) { return fileInf.Size(), nil } + +func (fileManager) ReadFile(name string) ([]byte, error) { + return os.ReadFile(name) +} diff --git a/fileutil/fileutil_test.go b/fileutil/fileutil_test.go index 41fdc25..b460198 100644 --- a/fileutil/fileutil_test.go +++ b/fileutil/fileutil_test.go @@ -1,7 +1,6 @@ package fileutil import ( - "os" "path/filepath" "testing" @@ -21,7 +20,7 @@ func TestWrite(t *testing.T) { tmpFilePath := filepath.Join(tmpDirPath, "WriteStringToFile-success.txt") require.NoError(t, manager.Write(tmpFilePath, content, 0600)) - fileContent, err := os.ReadFile(tmpFilePath) + fileContent, err := manager.ReadFile(tmpFilePath) require.NoError(t, err) require.Equal(t, content, string(fileContent)) } @@ -31,7 +30,7 @@ func TestWrite(t *testing.T) { tmpFilePath := filepath.Join(tmpDirPath, "dir-does-not-exist", "WriteStringToFile-success.txt") require.NoError(t, manager.Write(tmpFilePath, content, 0600)) - fileContent, err := os.ReadFile(tmpFilePath) + fileContent, err := manager.ReadFile(tmpFilePath) require.NoError(t, err) require.Equal(t, content, string(fileContent)) } @@ -41,7 +40,7 @@ func TestWrite(t *testing.T) { tmpFilePath := filepath.Join(tmpDirPath, "WriteBytesToFile-success.txt") require.NoError(t, manager.WriteBytes(tmpFilePath, []byte("test string"))) - fileContent, err := os.ReadFile(tmpFilePath) + fileContent, err := manager.ReadFile(tmpFilePath) require.NoError(t, err) require.Equal(t, "test string", string(fileContent)) } From 9ed84c702359762ea18f0ed8348f263f54b240d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Fri, 19 Sep 2025 15:51:34 +0200 Subject: [PATCH 2/4] Add EscapeGlobPath to PathModifier --- pathutil/pathutil.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go index f173693..f1a9548 100644 --- a/pathutil/pathutil.go +++ b/pathutil/pathutil.go @@ -98,6 +98,18 @@ func (p pathModifier) AbsPath(pth string) (string, error) { return filepath.Abs(os.ExpandEnv(pth)) } +// EscapeGlobPath escapes a partial path, determined at runtime, used as a parameter for filepath.Glob +func (pathModifier) EscapeGlobPath(path string) string { + var escaped string + for _, ch := range path { + if ch == '[' || ch == ']' || ch == '-' || ch == '*' || ch == '?' || ch == '\\' { + escaped += "\\" + } + escaped += string(ch) + } + return escaped +} + func (pathModifier) expandTilde(pth string) (string, error) { if pth == "" { return "", errors.New("No Path provided") From c75794045c420874f7cf6dca9ef18799079dc68d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Fri, 19 Sep 2025 16:14:45 +0200 Subject: [PATCH 3/4] Fix pathutil EscapeGlobPath --- fileutil/fileutil.go | 36 ++++++++++++++++++------------------ pathutil/pathutil.go | 1 + 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go index 17446b3..6d2258d 100644 --- a/fileutil/fileutil.go +++ b/fileutil/fileutil.go @@ -29,19 +29,6 @@ func NewFileManager() FileManager { return fileManager{} } -// ReadDirEntryNames reads the named directory using os.ReadDir and returns the dir entries' names. -func (fileManager) ReadDirEntryNames(path string) ([]string, error) { - entries, err := os.ReadDir(path) - if err != nil { - return nil, err - } - var names []string - for _, entry := range entries { - names = append(names, entry.Name()) - } - return names, nil -} - // Open ... func (fileManager) Open(path string) (*os.File, error) { return os.Open(path) @@ -61,6 +48,19 @@ func (fileManager) OpenReaderIfExists(path string) (io.Reader, error) { return file, nil } +// ReadDirEntryNames reads the named directory using os.ReadDir and returns the dir entries' names. +func (fileManager) ReadDirEntryNames(path string) ([]string, error) { + entries, err := os.ReadDir(path) + if err != nil { + return nil, err + } + var names []string + for _, entry := range entries { + names = append(names, entry.Name()) + } + return names, nil +} + // Remove ... func (fileManager) Remove(path string) error { return os.Remove(path) @@ -82,11 +82,6 @@ func (f fileManager) Write(path string, value string, mode os.FileMode) error { return os.Chmod(path, mode) } -func (fileManager) ensureSavePath(savePath string) error { - dirPath := filepath.Dir(savePath) - return os.MkdirAll(dirPath, 0700) -} - // WriteBytes ... func (f fileManager) WriteBytes(path string, value []byte) error { return os.WriteFile(path, value, 0600) @@ -108,3 +103,8 @@ func (fileManager) FileSizeInBytes(pth string) (int64, error) { func (fileManager) ReadFile(name string) ([]byte, error) { return os.ReadFile(name) } + +func (fileManager) ensureSavePath(savePath string) error { + dirPath := filepath.Dir(savePath) + return os.MkdirAll(dirPath, 0700) +} diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go index f1a9548..aff9bed 100644 --- a/pathutil/pathutil.go +++ b/pathutil/pathutil.go @@ -75,6 +75,7 @@ func (pathChecker) genericIsPathExists(pth string) (os.FileInfo, bool, error) { // PathModifier ... type PathModifier interface { AbsPath(pth string) (string, error) + EscapeGlobPath(path string) string } type pathModifier struct{} From c61947f4dc1ed240cc05b54fd7fa41a840449c24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krisztia=CC=81n=20Go=CC=88drei?= Date: Fri, 26 Sep 2025 16:31:15 +0200 Subject: [PATCH 4/4] Code cleanup --- fileutil/fileutil.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/fileutil/fileutil.go b/fileutil/fileutil.go index 6d2258d..5b7d287 100644 --- a/fileutil/fileutil.go +++ b/fileutil/fileutil.go @@ -12,13 +12,13 @@ import ( type FileManager interface { Open(path string) (*os.File, error) OpenReaderIfExists(path string) (io.Reader, error) + ReadFile(name string) ([]byte, error) ReadDirEntryNames(path string) ([]string, error) Remove(path string) error RemoveAll(path string) error Write(path string, value string, perm os.FileMode) error WriteBytes(path string, value []byte) error FileSizeInBytes(pth string) (int64, error) - ReadFile(name string) ([]byte, error) } type fileManager struct { @@ -48,6 +48,11 @@ func (fileManager) OpenReaderIfExists(path string) (io.Reader, error) { return file, nil } +// ReadFile ... +func (fileManager) ReadFile(name string) ([]byte, error) { + return os.ReadFile(name) +} + // ReadDirEntryNames reads the named directory using os.ReadDir and returns the dir entries' names. func (fileManager) ReadDirEntryNames(path string) ([]string, error) { entries, err := os.ReadDir(path) @@ -100,10 +105,6 @@ func (fileManager) FileSizeInBytes(pth string) (int64, error) { return fileInf.Size(), nil } -func (fileManager) ReadFile(name string) ([]byte, error) { - return os.ReadFile(name) -} - func (fileManager) ensureSavePath(savePath string) error { dirPath := filepath.Dir(savePath) return os.MkdirAll(dirPath, 0700)