From 51c71f2c8a332de67a2a465f50da73d6fa5cd9f9 Mon Sep 17 00:00:00 2001 From: Umberto Baldi Date: Mon, 5 Aug 2024 14:45:04 +0200 Subject: [PATCH] implement properly rename function to fix extraction issue with esptool --- v2/pkgs/tools.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/v2/pkgs/tools.go b/v2/pkgs/tools.go index b0daaaae..2a752410 100644 --- a/v2/pkgs/tools.go +++ b/v2/pkgs/tools.go @@ -269,15 +269,20 @@ func (t *Tools) Remove(ctx context.Context, payload *tools.ToolPayload) (*tools. return &tools.Operation{Status: "ok"}, nil } +// rename function is used to rename the path of the extracted files func rename(base string) extract.Renamer { + // "Rename" the given path adding the "base" and removing the root folder in "path" (if present). return func(path string) string { parts := strings.Split(filepath.ToSlash(path), "/") - newPath := strings.Join(parts[1:], "/") - if newPath == "" { - newPath = filepath.Join(newPath, path) + if len(parts) <= 1 { + // The path does not contain a root folder. This might happen for tool packages (zip files) + // that have an invalid structure. Do not try to remove the root folder in these cases. + return filepath.Join(base, path) + } else { + // Removes the first part of the path (the root folder). + path = strings.Join(parts[1:], "/") + return filepath.Join(base, path) } - path = filepath.Join(base, newPath) - return path } }