From 691230ef74baca742120f4e68ce28352c4a5919e Mon Sep 17 00:00:00 2001 From: Sherman Beus Date: Tue, 20 Feb 2024 11:26:38 -0800 Subject: [PATCH] Fixes issues around using "/" or "C:\" as root outgoing directory --- http/server.go | 3 ++- http/server_test.go | 4 ++-- store/local.go | 8 +++++++- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/http/server.go b/http/server.go index f3cacd4..bde6e6d 100644 --- a/http/server.go +++ b/http/server.go @@ -404,7 +404,8 @@ func (s *Server) routeCheckMapping(w http.ResponseWriter, r *http.Request) { Logger: log.Get(), } path := reqBody.ExamplePath - if strings.HasPrefix(path, reqBody.RootPath) { + root := strings.TrimSuffix(strings.TrimSuffix(reqBody.RootPath, "/"), `\`) + if strings.HasPrefix(path, root) { path = path[len(reqBody.RootPath)+1:] } mappedPath := pathMapper.Translate(path) diff --git a/http/server_test.go b/http/server_test.go index be37227..d091a9a 100644 --- a/http/server_test.go +++ b/http/server_test.go @@ -166,10 +166,10 @@ func TestMisc(t *testing.T) { payload := map[string]any{ "path": `C:\Data\2024\133.dat`, "source": source, - "root": `C:\Data`, + "root": `C:\`, "mapping": []any{ map[string]string{ - "from": `^(?P\d{4})\\(?P\d+)\.dat$`, + "from": `(?P\d{4})\\(?P\d+)\.dat$`, "to": `{{.__source}}.{{parseDayOfYear .year .day | formatDate "Ymd"}}.000000.dat`, }, }, diff --git a/store/local.go b/store/local.go index 0f49132..1f69e6b 100644 --- a/store/local.go +++ b/store/local.go @@ -149,7 +149,9 @@ func (dir *Local) getRelPath(path string) string { } func (dir *Local) shouldIgnore(relPath string, isDir bool) bool { - if !dir.IncludeHidden && strings.HasPrefix(filepath.Base(relPath), ".") { + if !dir.IncludeHidden && + relPath != "" && + strings.HasPrefix(filepath.Base(relPath), ".") { return true } var pattern *regexp.Regexp @@ -181,6 +183,10 @@ func (dir *Local) handleNode(path string, info os.FileInfo, err error) error { } relPath := dir.getRelPath(path) + if relPath == "" { + // Root directory + return nil + } if info.IsDir() { if dir.shouldIgnore(relPath, true) { dir.debug("Ignored Local Directory:", path)