From 843e6bcbf15ea7e67575081c0ce6c1e5be3dca44 Mon Sep 17 00:00:00 2001 From: Denis Vaumoron Date: Sat, 22 Jun 2024 23:03:21 +0200 Subject: [PATCH] add a filter to pkg/zip to extract only necessary binary (#182) Signed-off-by: Denis Vaumoron --- pkg/check/binary/check.go | 51 ------------------ pkg/check/binary/testdata/test.bin | Bin 4 -> 0 bytes pkg/check/binary/testdata/test.txt | 1 - .../pathfilter.go} | 36 ++++--------- pkg/zip/zip.go | 32 +++-------- .../retriever/terraform/terraformretriever.go | 4 +- .../retriever/tofu/tofuretriever.go | 4 +- 7 files changed, 23 insertions(+), 105 deletions(-) delete mode 100644 pkg/check/binary/check.go delete mode 100644 pkg/check/binary/testdata/test.bin delete mode 100644 pkg/check/binary/testdata/test.txt rename pkg/{check/binary/check_test.go => pathfilter/pathfilter.go} (50%) diff --git a/pkg/check/binary/check.go b/pkg/check/binary/check.go deleted file mode 100644 index a13e858a..00000000 --- a/pkg/check/binary/check.go +++ /dev/null @@ -1,51 +0,0 @@ -/* - * - * Copyright 2024 tofuutils authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package bincheck - -import "os" - -func Check(filename string) (bool, error) { - file, err := os.Open(filename) - if err != nil { - return false, err - } - defer file.Close() - - const maxBytes = 8000 // Read up to 8000 bytes for analysis - bytes := make([]byte, maxBytes) - numberOfBytes, err := file.Read(bytes) - if err != nil { - return false, err - } - - // Check for the presence of a null byte within the read bytes - for i := 0; i < numberOfBytes; i++ { - if bytes[i] == 0 { - return true, nil // Null byte found, file is binary - } - } - - // If no null byte found, check for a UTF-8 encoding signature (BOM) - if numberOfBytes >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF { - return false, nil // UTF-8 encoded text file - } - - // If no null byte or UTF-8 BOM found, assume it's a text file - return false, nil -} diff --git a/pkg/check/binary/testdata/test.bin b/pkg/check/binary/testdata/test.bin deleted file mode 100644 index 7a42ff114c2efb839de997255d60948997a6db36..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4 LcmeZZ@MHi00k!~= diff --git a/pkg/check/binary/testdata/test.txt b/pkg/check/binary/testdata/test.txt deleted file mode 100644 index 2c1f75cf..00000000 --- a/pkg/check/binary/testdata/test.txt +++ /dev/null @@ -1 +0,0 @@ -This is text file for testing diff --git a/pkg/check/binary/check_test.go b/pkg/pathfilter/pathfilter.go similarity index 50% rename from pkg/check/binary/check_test.go rename to pkg/pathfilter/pathfilter.go index 303254b1..af334d01 100644 --- a/pkg/check/binary/check_test.go +++ b/pkg/pathfilter/pathfilter.go @@ -16,34 +16,18 @@ * */ -package bincheck_test +package pathfilter -import ( - "testing" +import "strings" - bincheck "github.com/tofuutils/tenv/v2/pkg/check/binary" -) +// Handle '/' and '\' separated path. +func NameEqual(targetName string) func(string) bool { + return func(path string) bool { + separatorIndex := strings.LastIndexByte(path, '/') + if separatorIndex == -1 { + separatorIndex = strings.LastIndexByte(path, '\\') + } -func TestTextFileCheck(t *testing.T) { - t.Parallel() - - isBinary, err := bincheck.Check("testdata/test.txt") - if err != nil { - t.Fatalf("Error checking non-binary file: %v", err) - } - if isBinary { - t.Errorf("Expected non-binary file, got binary") - } -} - -func TestBinaryFileCheck(t *testing.T) { - t.Parallel() - - isBinary, err := bincheck.Check("testdata/test.bin") - if err != nil { - t.Fatalf("Error checking non-binary file: %v", err) - } - if !isBinary { - t.Errorf("Expected binary file, got non-binary") + return path[separatorIndex+1:] == targetName } } diff --git a/pkg/zip/zip.go b/pkg/zip/zip.go index 33179853..109beb6a 100644 --- a/pkg/zip/zip.go +++ b/pkg/zip/zip.go @@ -26,12 +26,10 @@ import ( "os" "path/filepath" "strings" - - bincheck "github.com/tofuutils/tenv/v2/pkg/check/binary" ) // ensure the directory exists with a MkdirAll call. -func UnzipToDir(dataZip []byte, dirPath string) error { +func UnzipToDir(dataZip []byte, dirPath string, filter func(string) bool) error { err := os.MkdirAll(dirPath, 0o755) if err != nil { return err @@ -44,36 +42,16 @@ func UnzipToDir(dataZip []byte, dirPath string) error { } for _, file := range zipReader.File { - if err = copyZipFileToDir(file, dirPath); err != nil { + if err = copyZipFileToDir(file, dirPath, filter); err != nil { return err } - - files, err := os.ReadDir(dirPath) - if err != nil { - return err - } - - for _, file := range files { - filePath := filepath.Join(dirPath, file.Name()) - - isBinary, err := bincheck.Check(filePath) - if err != nil { - return err - } - - if !isBinary { - if err = os.Remove(filePath); err != nil { - return err - } - } - } } return nil } // a separate function allows deferred Close to execute earlier. -func copyZipFileToDir(zipFile *zip.File, dirPath string) error { +func copyZipFileToDir(zipFile *zip.File, dirPath string, filter func(string) bool) error { destPath, err := sanitizeArchivePath(dirPath, zipFile.Name) if err != nil { return err @@ -95,6 +73,10 @@ func copyZipFileToDir(zipFile *zip.File, dirPath string) error { return err } + if !filter(destPath) { + return nil + } + return os.WriteFile(destPath, data, zipFile.Mode()) } diff --git a/versionmanager/retriever/terraform/terraformretriever.go b/versionmanager/retriever/terraform/terraformretriever.go index 37dd289b..086af164 100644 --- a/versionmanager/retriever/terraform/terraformretriever.go +++ b/versionmanager/retriever/terraform/terraformretriever.go @@ -34,6 +34,8 @@ import ( pgpcheck "github.com/tofuutils/tenv/v2/pkg/check/pgp" sha256check "github.com/tofuutils/tenv/v2/pkg/check/sha256" "github.com/tofuutils/tenv/v2/pkg/download" + "github.com/tofuutils/tenv/v2/pkg/pathfilter" + "github.com/tofuutils/tenv/v2/pkg/winbin" "github.com/tofuutils/tenv/v2/pkg/zip" htmlretriever "github.com/tofuutils/tenv/v2/versionmanager/retriever/html" ) @@ -130,7 +132,7 @@ func (r TerraformRetriever) InstallRelease(version string, targetPath string) er return err } - return zip.UnzipToDir(data, targetPath) + return zip.UnzipToDir(data, targetPath, pathfilter.NameEqual(winbin.GetBinaryName(config.TerraformName))) } func (r TerraformRetriever) ListReleases() ([]string, error) { diff --git a/versionmanager/retriever/tofu/tofuretriever.go b/versionmanager/retriever/tofu/tofuretriever.go index a5d1ef12..cf8b5ebc 100644 --- a/versionmanager/retriever/tofu/tofuretriever.go +++ b/versionmanager/retriever/tofu/tofuretriever.go @@ -34,6 +34,8 @@ import ( sha256check "github.com/tofuutils/tenv/v2/pkg/check/sha256" "github.com/tofuutils/tenv/v2/pkg/download" "github.com/tofuutils/tenv/v2/pkg/github" + "github.com/tofuutils/tenv/v2/pkg/pathfilter" + "github.com/tofuutils/tenv/v2/pkg/winbin" "github.com/tofuutils/tenv/v2/pkg/zip" htmlretriever "github.com/tofuutils/tenv/v2/versionmanager/retriever/html" ) @@ -114,7 +116,7 @@ func (r TofuRetriever) InstallRelease(versionStr string, targetPath string) erro return err } - return zip.UnzipToDir(data, targetPath) + return zip.UnzipToDir(data, targetPath, pathfilter.NameEqual(winbin.GetBinaryName(config.TofuName))) } func (r TofuRetriever) ListReleases() ([]string, error) {