From be1379b04842046a3ef2ae0231bb53db7f411ed3 Mon Sep 17 00:00:00 2001 From: Alexander Sharov Date: Sun, 28 Apr 2024 17:23:50 +0300 Subject: [PATCH 1/3] feat: skip non-binary files extraction Signed-off-by: Alexander Sharov --- .gitignore | 7 +++++- Makefile | 7 ++++-- pkg/check/binary/check.go | 48 +++++++++++++++++++++++++++++++++++++++ pkg/zip/zip.go | 22 ++++++++++++++++++ 4 files changed, 81 insertions(+), 3 deletions(-) create mode 100644 pkg/check/binary/check.go diff --git a/.gitignore b/.gitignore index 43c233ec..fb7d19d9 100644 --- a/.gitignore +++ b/.gitignore @@ -24,4 +24,9 @@ bin/terraform-* # Idea .idea -.DS_Store \ No newline at end of file +.DS_Store + +tenv +terraform +terragrunt +tofu \ No newline at end of file diff --git a/Makefile b/Makefile index 627f41ff..0158aca4 100644 --- a/Makefile +++ b/Makefile @@ -21,17 +21,20 @@ help: ## Display this help. fmt: ## Run go fmt against code. go fmt ./... +get: ## Install dependencies. + go get ./... + vet: ## Run go vet against code. go vet ./... ##@ Build -build: fmt vet ## Build service binary. +build: get fmt ## Build service binary. go build -o tenv ./cmd/tenv go build -o tofu ./cmd/tofu go build -o terraform ./cmd/terraform go build -o terragrunt ./cmd/terragrunt -run: vet ## Run service from your laptop. +run: ## Run service from your laptop. go run ./main.go ##@ Test diff --git a/pkg/check/binary/check.go b/pkg/check/binary/check.go new file mode 100644 index 00000000..a8409eb8 --- /dev/null +++ b/pkg/check/binary/check.go @@ -0,0 +1,48 @@ +/* + * + * 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(filePath string) (bool, error) { + file, err := os.Open(filePath) + if err != nil { + return false, err + } + defer file.Close() + + buf := make([]byte, 512) + _, err = file.Read(buf) + if err != nil { + return false, err + } + + for _, b := range buf { + if b == 0 { + return true, nil + } + if b < 7 || (b > 14 && b < 32) { + return true, nil + } + } + + return false, nil +} diff --git a/pkg/zip/zip.go b/pkg/zip/zip.go index 6bbd7052..40b75c72 100644 --- a/pkg/zip/zip.go +++ b/pkg/zip/zip.go @@ -26,6 +26,8 @@ import ( "os" "path/filepath" "strings" + + bincheck "github.com/tofuutils/tenv/pkg/check/binary" ) // ensure the directory exists with a MkdirAll call. @@ -45,6 +47,26 @@ func UnzipToDir(dataZip []byte, dirPath string) error { if err = copyZipFileToDir(file, dirPath); 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 From 26606e8c4a83b61ccb1d81ce9853de8d3e7fda8b Mon Sep 17 00:00:00 2001 From: Alexander Sharov Date: Sun, 28 Apr 2024 17:54:03 +0300 Subject: [PATCH 2/3] feat: add unit tests Signed-off-by: Alexander Sharov --- pkg/check/binary/check.go | 25 +++++++++------ pkg/check/binary/check_test.go | 49 +++++++++++++++++++++++++++++ pkg/check/binary/testdata/test.bin | Bin 0 -> 4 bytes pkg/check/binary/testdata/test.txt | 1 + 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 pkg/check/binary/check_test.go create mode 100644 pkg/check/binary/testdata/test.bin create mode 100644 pkg/check/binary/testdata/test.txt diff --git a/pkg/check/binary/check.go b/pkg/check/binary/check.go index a8409eb8..1bbf478f 100644 --- a/pkg/check/binary/check.go +++ b/pkg/check/binary/check.go @@ -22,27 +22,32 @@ import ( "os" ) -func Check(filePath string) (bool, error) { - file, err := os.Open(filePath) +func Check(filename string) (bool, error) { + file, err := os.Open(filename) if err != nil { return false, err } defer file.Close() - buf := make([]byte, 512) - _, err = file.Read(buf) + const maxBytes = 8000 // Read up to 8000 bytes for analysis + bytes := make([]byte, maxBytes) + n, err := file.Read(bytes) if err != nil { return false, err } - for _, b := range buf { - if b == 0 { - return true, nil - } - if b < 7 || (b > 14 && b < 32) { - return true, nil + // Check for the presence of a null byte within the read bytes + for i := 0; i < n; 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 n >= 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/check_test.go b/pkg/check/binary/check_test.go new file mode 100644 index 00000000..78be7b4f --- /dev/null +++ b/pkg/check/binary/check_test.go @@ -0,0 +1,49 @@ +/* + * + * 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_test + +import ( + "testing" + + bincheck "github.com/tofuutils/tenv/pkg/check/binary" +) + +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") + } +} diff --git a/pkg/check/binary/testdata/test.bin b/pkg/check/binary/testdata/test.bin new file mode 100644 index 0000000000000000000000000000000000000000..7a42ff114c2efb839de997255d60948997a6db36 GIT binary patch literal 4 LcmeZZ@MHi00k!~= literal 0 HcmV?d00001 diff --git a/pkg/check/binary/testdata/test.txt b/pkg/check/binary/testdata/test.txt new file mode 100644 index 00000000..2c1f75cf --- /dev/null +++ b/pkg/check/binary/testdata/test.txt @@ -0,0 +1 @@ +This is text file for testing From 0d7a0823028725711b197fc4a9cf0bb6d6a6c664 Mon Sep 17 00:00:00 2001 From: Alexander Sharov Date: Sun, 28 Apr 2024 18:00:30 +0300 Subject: [PATCH 3/3] fix: linter errors Signed-off-by: Alexander Sharov --- pkg/check/binary/check.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/check/binary/check.go b/pkg/check/binary/check.go index 1bbf478f..a5acf3ec 100644 --- a/pkg/check/binary/check.go +++ b/pkg/check/binary/check.go @@ -31,20 +31,20 @@ func Check(filename string) (bool, error) { const maxBytes = 8000 // Read up to 8000 bytes for analysis bytes := make([]byte, maxBytes) - n, err := file.Read(bytes) + 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 < n; i++ { + 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 n >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF { + if numberOfBytes >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF { return false, nil // UTF-8 encoded text file }