Skip to content

Commit 2a4e9e1

Browse files
authored
Keep downloaded pack #276 (#277)
[cpackget] cpackcpackget deletes pack file content when installing from $CMSIS_PACK_ROOT/.Download/ #276
1 parent 09b0b17 commit 2a4e9e1

File tree

7 files changed

+60
-14
lines changed

7 files changed

+60
-14
lines changed

cmd/installer/pack.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ func (p *PackType) purge() error {
243243
// - If "CMSIS_PACK_ROOT/.Web/p.Vendor.p.Name.pdsc" does not exist then
244244
// - Save an unversioned copy of the pdsc file in "CMSIS_PACK_ROOT/.Local/"
245245
func (p *PackType) install(installation *PacksInstallationType, checkEula bool) error {
246+
247+
// normalize pack path
248+
p.path = filepath.FromSlash(p.path)
249+
p.path = filepath.Clean(p.path)
250+
246251
log.Debugf("Installing \"%s\"", p.path)
247252

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

259264
packHomeDir := filepath.Join(Installation.PackRoot, p.Vendor, p.Name, p.GetVersionNoMeta())
260265
packBackupPath := filepath.Join(Installation.DownloadDir, p.PackFileName())
266+
packBackupPath = filepath.FromSlash(packBackupPath)
267+
packBackupPath = filepath.Clean(packBackupPath)
268+
if utils.SameFile(packBackupPath, p.path) {
269+
p.isDownloaded = true
270+
}
261271

262272
if len(p.Pdsc.License) > 0 {
263273
if checkEula {

cmd/installer/root_pack_add_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,28 @@ func TestAddPack(t *testing.T) {
106106
checkPackIsInstalled(t, packInfoToType(packToReinstall))
107107
})
108108

109+
t.Run("test installing downloaded pack", func(t *testing.T) {
110+
localTestingDir := "test-add-downloaded-pack"
111+
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))
112+
installer.UnlockPackRoot()
113+
defer removePackRoot(localTestingDir)
114+
115+
packPath := packToReinstall
116+
addPack(t, packPath, ConfigType{})
117+
removePack(t, packPath, true, true, false)
118+
packPath = filepath.Join(installer.Installation.DownloadDir, packToReinstallFileName)
119+
err := installer.AddPack(packPath, !CheckEula, !ExtractEula, !ForceReinstall, !NoRequirements, Timeout)
120+
assert.Nil(err)
121+
122+
// ensure downloaded pack remains valid
123+
err = installer.AddPack(packPath, !CheckEula, !ExtractEula, ForceReinstall, !NoRequirements, Timeout)
124+
assert.Nil(err)
125+
126+
packToReinstall, err := utils.ExtractPackInfo(packPath)
127+
assert.Nil(err)
128+
checkPackIsInstalled(t, packInfoToType(packToReinstall))
129+
})
130+
109131
t.Run("test force-reinstalling an installed pack using encoded progress", func(t *testing.T) {
110132
localTestingDir := "test-add-pack-force-reinstall-already-installed-using-encoded-progress"
111133
assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))

cmd/installer/root_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ var (
233233

234234
malformedPackNames = []string{"pAck-WiTH-HiFenS", "[$pecialC#aracter£]", "Spaced Pack Name", " "}
235235
packThatDoesNotExist = "ThisPack.DoesNotExist.0.0.1.pack"
236-
packToReinstall = filepath.Join(testDir, "TheVendor.PackToReinstall.1.2.3.pack")
236+
packToReinstallFileName = "TheVendor.PackToReinstall.1.2.3.pack"
237+
packToReinstall = filepath.Join(testDir, packToReinstallFileName)
237238
packWithCorruptZip = filepath.Join(testDir, "FakeZip.PackName.1.2.3.pack")
238239
packWithMalformedURL = "http://:malformed-url*/TheVendor.PackName.1.2.3.pack"
239240
packWithoutPdscFileInside = filepath.Join(testDir, "PackWithout.PdscFileInside.1.2.3.pack")

cmd/utils/utils.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,12 +246,27 @@ func EnsureDir(dirName string) error {
246246
return nil
247247
}
248248

249+
func SameFile(source, destination string) bool {
250+
if source == destination {
251+
return true
252+
}
253+
srcInfo, err := os.Stat(source)
254+
if err != nil {
255+
return false
256+
}
257+
dstInfo, err := os.Stat(destination)
258+
if err != nil {
259+
return false
260+
}
261+
return os.SameFile(srcInfo, dstInfo)
262+
}
263+
249264
// CopyFile copies the contents of source into a new file in destination
250265
func CopyFile(source, destination string) error {
251266
log.Debugf("Copying file from \"%s\" to \"%s\"", source, destination)
252267

253-
if source == destination {
254-
return errs.ErrCopyingEqualPaths
268+
if SameFile(source, destination) {
269+
return nil
255270
}
256271

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

277-
if source == destination {
278-
return errs.ErrCopyingEqualPaths
292+
if SameFile(source, destination) {
293+
return nil
279294
}
280295

281296
UnsetReadOnly(source)

cmd/utils/utils_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,10 @@ func TestEnsureDir(t *testing.T) {
171171
func TestCopyFile(t *testing.T) {
172172
assert := assert.New(t)
173173

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

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

243-
t.Run("test fail if moving to same file", func(t *testing.T) {
242+
t.Run("test succeeds if moving to same file", func(t *testing.T) {
244243
fileName := "dummy-file"
245244
err := utils.MoveFile(fileName, fileName)
246-
assert.NotNil(err)
247-
errs.Is(err, errs.ErrMovingEqualPaths)
245+
assert.Nil(err)
248246
})
249247

250248
t.Run("test fail moving files", func(t *testing.T) {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ require (
1313
github.com/spf13/viper v1.18.2
1414
github.com/stretchr/testify v1.9.0
1515
golang.org/x/mod v0.16.0
16-
golang.org/x/net v0.22.0
16+
golang.org/x/net v0.23.0
1717
golang.org/x/sync v0.6.0
1818
golang.org/x/term v0.18.0
1919
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
116116
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
117117
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
118118
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
119-
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
120-
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
119+
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
120+
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
121121
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
122122
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
123123
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

0 commit comments

Comments
 (0)