Skip to content

Commit

Permalink
Keep downloaded pack #276 (#277)
Browse files Browse the repository at this point in the history
[cpackget] cpackcpackget deletes pack file content when installing from
$CMSIS_PACK_ROOT/.Download/ #276
  • Loading branch information
edriouk authored Apr 4, 2024
1 parent 09b0b17 commit 2a4e9e1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 14 deletions.
10 changes: 10 additions & 0 deletions cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ func (p *PackType) purge() error {
// - If "CMSIS_PACK_ROOT/.Web/p.Vendor.p.Name.pdsc" does not exist then
// - Save an unversioned copy of the pdsc file in "CMSIS_PACK_ROOT/.Local/"
func (p *PackType) install(installation *PacksInstallationType, checkEula bool) error {

// normalize pack path
p.path = filepath.FromSlash(p.path)
p.path = filepath.Clean(p.path)

log.Debugf("Installing \"%s\"", p.path)

var err error
Expand All @@ -258,6 +263,11 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)

packHomeDir := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersionNoMeta())
packBackupPath := filepath.Join(Installation.DownloadDir, p.PackFileName())
packBackupPath = filepath.FromSlash(packBackupPath)
packBackupPath = filepath.Clean(packBackupPath)
if utils.SameFile(packBackupPath, p.path) {
p.isDownloaded = true
}

if len(p.Pdsc.License) > 0 {
if checkEula {
Expand Down
22 changes: 22 additions & 0 deletions cmd/installer/root_pack_add_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ func TestAddPack(t *testing.T) {
checkPackIsInstalled(t, packInfoToType(packToReinstall))
})

t.Run("test installing downloaded pack", func(t *testing.T) {
localTestingDir := "test-add-downloaded-pack"
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))
installer.UnlockPackRoot()
defer removePackRoot(localTestingDir)

packPath := packToReinstall
addPack(t, packPath, ConfigType{})
removePack(t, packPath, true, true, false)
packPath = filepath.Join(installer.Installation.DownloadDir, packToReinstallFileName)
err := installer.AddPack(packPath, !CheckEula, !ExtractEula, !ForceReinstall, !NoRequirements, Timeout)
assert.Nil(err)

// ensure downloaded pack remains valid
err = installer.AddPack(packPath, !CheckEula, !ExtractEula, ForceReinstall, !NoRequirements, Timeout)
assert.Nil(err)

packToReinstall, err := utils.ExtractPackInfo(packPath)
assert.Nil(err)
checkPackIsInstalled(t, packInfoToType(packToReinstall))
})

t.Run("test force-reinstalling an installed pack using encoded progress", func(t *testing.T) {
localTestingDir := "test-add-pack-force-reinstall-already-installed-using-encoded-progress"
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))
Expand Down
3 changes: 2 additions & 1 deletion cmd/installer/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,8 @@ var (

malformedPackNames = []string{"pAck-WiTH-HiFenS", "[$pecialC#aracter£]", "Spaced Pack Name", " "}
packThatDoesNotExist = "ThisPack.DoesNotExist.0.0.1.pack"
packToReinstall = filepath.Join(testDir, "TheVendor.PackToReinstall.1.2.3.pack")
packToReinstallFileName = "TheVendor.PackToReinstall.1.2.3.pack"
packToReinstall = filepath.Join(testDir, packToReinstallFileName)
packWithCorruptZip = filepath.Join(testDir, "FakeZip.PackName.1.2.3.pack")
packWithMalformedURL = "http://:malformed-url*/TheVendor.PackName.1.2.3.pack"
packWithoutPdscFileInside = filepath.Join(testDir, "PackWithout.PdscFileInside.1.2.3.pack")
Expand Down
23 changes: 19 additions & 4 deletions cmd/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,27 @@ func EnsureDir(dirName string) error {
return nil
}

func SameFile(source, destination string) bool {
if source == destination {
return true
}
srcInfo, err := os.Stat(source)
if err != nil {
return false
}
dstInfo, err := os.Stat(destination)
if err != nil {
return false
}
return os.SameFile(srcInfo, dstInfo)
}

// CopyFile copies the contents of source into a new file in destination
func CopyFile(source, destination string) error {
log.Debugf("Copying file from \"%s\" to \"%s\"", source, destination)

if source == destination {
return errs.ErrCopyingEqualPaths
if SameFile(source, destination) {
return nil
}

sourceFile, err := os.Open(source)
Expand All @@ -274,8 +289,8 @@ func CopyFile(source, destination string) error {
func MoveFile(source, destination string) error {
log.Debugf("Moving file from \"%s\" to \"%s\"", source, destination)

if source == destination {
return errs.ErrCopyingEqualPaths
if SameFile(source, destination) {
return nil
}

UnsetReadOnly(source)
Expand Down
10 changes: 4 additions & 6 deletions cmd/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,10 @@ func TestEnsureDir(t *testing.T) {
func TestCopyFile(t *testing.T) {
assert := assert.New(t)

t.Run("test fail if copying to same file", func(t *testing.T) {
t.Run("test succeeds if copying to same file", func(t *testing.T) {
fileName := "dummy-file"
err := utils.CopyFile(fileName, fileName)
assert.NotNil(err)
errs.Is(err, errs.ErrCopyingEqualPaths)
assert.Nil(err)
})

t.Run("test failed to open source", func(t *testing.T) {
Expand Down Expand Up @@ -240,11 +239,10 @@ func TestCopyFile(t *testing.T) {
func TestMoveFile(t *testing.T) {
assert := assert.New(t)

t.Run("test fail if moving to same file", func(t *testing.T) {
t.Run("test succeeds if moving to same file", func(t *testing.T) {
fileName := "dummy-file"
err := utils.MoveFile(fileName, fileName)
assert.NotNil(err)
errs.Is(err, errs.ErrMovingEqualPaths)
assert.Nil(err)
})

t.Run("test fail moving files", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.9.0
golang.org/x/mod v0.16.0
golang.org/x/net v0.22.0
golang.org/x/net v0.23.0
golang.org/x/sync v0.6.0
golang.org/x/term v0.18.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down

0 comments on commit 2a4e9e1

Please sign in to comment.