From 8bd5e18fc931b39dd892f9435d7970e311c7d3e1 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Fri, 28 Apr 2023 16:41:06 +0200 Subject: [PATCH 1/3] Ignore folder when installing packages --- arduino/resources/install.go | 3 + arduino/resources/install_test.go | 75 ++++++++++++++++++ ...latform_with_multiple_root_folders.tar.bz2 | Bin 0 -> 148 bytes .../platform_without_root_folder.tar.bz2 | Bin 0 -> 177 bytes ...tform_with_root_and__MACOSX_folder.tar.bz2 | Bin 0 -> 157 bytes 5 files changed, 78 insertions(+) create mode 100644 arduino/resources/install_test.go create mode 100644 arduino/resources/testdata/invalid/platform_with_multiple_root_folders.tar.bz2 create mode 100644 arduino/resources/testdata/invalid/platform_without_root_folder.tar.bz2 create mode 100644 arduino/resources/testdata/valid/platform_with_root_and__MACOSX_folder.tar.bz2 diff --git a/arduino/resources/install.go b/arduino/resources/install.go index 188a8d2f1f5..0cbd55a8e42 100644 --- a/arduino/resources/install.go +++ b/arduino/resources/install.go @@ -120,6 +120,9 @@ func findPackageRoot(parent *paths.Path) (*paths.Path, error) { if err != nil { return nil, fmt.Errorf(tr("reading package root dir: %s", err)) } + + files.FilterOutPrefix("__MACOSX") + var root *paths.Path for _, file := range files { if !file.IsDir() { diff --git a/arduino/resources/install_test.go b/arduino/resources/install_test.go new file mode 100644 index 00000000000..ed3956e7a20 --- /dev/null +++ b/arduino/resources/install_test.go @@ -0,0 +1,75 @@ +package resources + +import ( + "os" + "path" + "path/filepath" + "testing" + + "github.com/arduino/go-paths-helper" + + "github.com/stretchr/testify/require" +) + +func TestInstallPlatform(t *testing.T) { + t.Run("ignore __MACOSX folder", func(t *testing.T) { + testFileName := "platform_with_root_and__MACOSX_folder.tar.bz2" + testFilePath := filepath.Join("testdata/valid", testFileName) + + downloadDir, tempPath, destDir := paths.New(t.TempDir()), paths.New(t.TempDir()), paths.New(t.TempDir()) + + // copy testfile in the download dir + origin, err := os.ReadFile(testFilePath) + require.NoError(t, err) + require.NoError(t, os.WriteFile(path.Join(downloadDir.String(), testFileName), origin, 0644)) + + r := &DownloadResource{ + ArchiveFileName: testFileName, + Checksum: "SHA-256:600ad56b6260352e0b2cee786f60749e778e179252a0594ba542f0bd1f8adee5", + Size: 157, + } + + require.NoError(t, r.Install(downloadDir, tempPath, destDir)) + }) + + tests := []struct { + testName string + downloadResource *DownloadResource + error string + }{ + { + testName: "multiple root folders not allowed", + downloadResource: &DownloadResource{ + ArchiveFileName: "platform_with_multiple_root_folders.tar.bz2", + Checksum: "SHA-256:8b3fc6253c5ac2f3ba684eba0d62bb8a4ee93469fa822f81e2cd7d1b959c4044", + Size: 148, + }, + error: "no unique root dir in archive", + }, + { + testName: "root folder not present", + downloadResource: &DownloadResource{ + ArchiveFileName: "platform_without_root_folder.tar.bz2", + Checksum: "SHA-256:bc00db9784e20f50d7a5fceccb6bd95ebff4a3e847aac88365b95a6851a24963", + Size: 177, + }, + error: "files in archive must be placed in a subdirectory", + }, + } + for _, test := range tests { + t.Run(test.testName, func(t *testing.T) { + downloadDir, tempPath, destDir := paths.New(t.TempDir()), paths.New(t.TempDir()), paths.New(t.TempDir()) + testFileName := test.downloadResource.ArchiveFileName + testFilePath := filepath.Join("testdata/invalid", testFileName) + + // copy testfile in the download dir + origin, err := os.ReadFile(testFilePath) + require.NoError(t, err) + require.NoError(t, os.WriteFile(path.Join(downloadDir.String(), testFileName), origin, 0644)) + + err = test.downloadResource.Install(downloadDir, tempPath, destDir) + require.Error(t, err) + require.Contains(t, err.Error(), test.error) + }) + } +} diff --git a/arduino/resources/testdata/invalid/platform_with_multiple_root_folders.tar.bz2 b/arduino/resources/testdata/invalid/platform_with_multiple_root_folders.tar.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..fea2f2021a5c90b69904f52beba8a596e221822d GIT binary patch literal 148 zcmV;F0Biq3T4*^jL0KkKSp#3YDF6VLdxF4#03bjD|9}_(V@KWqKmY^?AOMtvr1XhB zL6F%9^q!+ZB{dBOfB*wbN0p5bKk44;GurBt@Pk~cQHcDFO2Zeb9X8FFRy z<|*L0#4*PmC{V)#x;DXiO^LAII;%TD7HxYLYC{o-XeUbpOre9n2QO8uAoD-GAyKjv f#Yv)6B0{-LD2Pdg83%#{UyHdSoG3^#USDDWIzK}k literal 0 HcmV?d00001 diff --git a/arduino/resources/testdata/valid/platform_with_root_and__MACOSX_folder.tar.bz2 b/arduino/resources/testdata/valid/platform_with_root_and__MACOSX_folder.tar.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..285042c62fd7f93a6e85a42b9483f9fefad4a579 GIT binary patch literal 157 zcmV;O0Al|_T4*^jL0KkKS>{bQ_y7Qwe}ce(03bjD|9~h0n84!(-T*)V1PCAik_kj8 z@)HCyH1!5(^+2jZo~D`_13{1gV9L^;;t6CzA#SM^i?b-Hog_gtlX*?w-ZsL*F~Y=9 zl1Aw(M9N;hJ_;t+Rei@Ka)PX)IDAl%=fw1frJNbzGr6h@O+!O!GzU?KF$OIW1Ngg= LDZ+$@GHJiS=L10! literal 0 HcmV?d00001 From 20ab0914f7bcae2a1580ba120e3b064261d9c6f2 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Fri, 28 Apr 2023 16:42:26 +0200 Subject: [PATCH 2/3] Propose a small refactoring of the func --- arduino/resources/install.go | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/arduino/resources/install.go b/arduino/resources/install.go index 0cbd55a8e42..6b7c09bb9e1 100644 --- a/arduino/resources/install.go +++ b/arduino/resources/install.go @@ -121,21 +121,15 @@ func findPackageRoot(parent *paths.Path) (*paths.Path, error) { return nil, fmt.Errorf(tr("reading package root dir: %s", err)) } + files.FilterDirs() files.FilterOutPrefix("__MACOSX") - var root *paths.Path - for _, file := range files { - if !file.IsDir() { - continue - } - if root == nil { - root = file - } else { - return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'", root, file)) - } - } - if root == nil { + if len(files) == 0 { return nil, fmt.Errorf(tr("files in archive must be placed in a subdirectory")) } - return root, nil + if len(files) > 1 { + return nil, fmt.Errorf(tr("no unique root dir in archive, found '%[1]s' and '%[2]s'", files[0], files[1])) + } + + return files[0], nil } From 9e4e5e01864f38809bd90d192a167d6c1a1198d8 Mon Sep 17 00:00:00 2001 From: Alessio Perugini Date: Tue, 2 May 2023 12:14:29 +0200 Subject: [PATCH 3/3] Add missing license header --- arduino/resources/install_test.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/arduino/resources/install_test.go b/arduino/resources/install_test.go index ed3956e7a20..ce17bcbd293 100644 --- a/arduino/resources/install_test.go +++ b/arduino/resources/install_test.go @@ -1,3 +1,18 @@ +// This file is part of arduino-cli. +// +// Copyright 2023 ARDUINO SA (http://www.arduino.cc/) +// +// This software is released under the GNU General Public License version 3, +// which covers the main part of arduino-cli. +// The terms of this license can be found at: +// https://www.gnu.org/licenses/gpl-3.0.en.html +// +// You can be released from the requirements of the above licenses by purchasing +// a commercial license. Buying such a license is mandatory if you want to +// modify or otherwise use the software for commercial activities involving the +// Arduino software without disclosing the source code of your own applications. +// To purchase a commercial license, send an email to license@arduino.cc. + package resources import (