Skip to content

Commit be976aa

Browse files
bgn42soumeh01
andauthored
try to synchronize index.pidx with psdc when adding (#548)
## Fixes try to correct different version numbers between index.pidx and corresponding PDSC when adding ## Checklist <!-- Put an `x` in the boxes. All tasks must be completed and boxes checked before merging. --> - [x] 🤖 This change is covered by unit tests (if applicable). - [x] 🤹 Manual testing has been performed (if necessary). - [x] 🛡️ Security impacts have been considered (if relevant). - [x] 📖 Documentation updates are complete (if required). - [x] 🧠 Third-party dependencies and TPIP updated (if required). --------- Co-authored-by: Sourabh Mehta <[email protected]>
1 parent 6d40503 commit be976aa

File tree

20 files changed

+428
-202
lines changed

20 files changed

+428
-202
lines changed

cmd/commands/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Add a pack using the following "<pack>" specification or using packs provided by
100100
installer.UnlockPackRoot()
101101
for _, packPath := range args {
102102
var err error
103-
if filepath.Ext(packPath) == ".pdsc" {
103+
if filepath.Ext(packPath) == installer.PdscExtension {
104104
err = installer.AddPdsc(packPath)
105105
} else {
106106
err = installer.AddPack(packPath, !addCmdFlags.skipEula, addCmdFlags.extractEula, addCmdFlags.forceReinstall, addCmdFlags.noRequirements, false, viper.GetInt("timeout"))

cmd/commands/rm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ please use "--purge".`,
6060
installer.UnlockPackRoot()
6161
for _, packPath := range args {
6262
var err error
63-
if filepath.Ext(packPath) == ".pdsc" {
63+
if filepath.Ext(packPath) == installer.PdscExtension {
6464
err = installer.RemovePdsc(packPath)
6565
if err == errs.ErrPdscEntryNotFound {
6666
err = errs.ErrPackNotInstalled

cmd/commands/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func configureInstaller(cmd *cobra.Command, args []string) error {
7979
// Exclude index updating commands to not double update
8080
if cmd.Name() != "init" && cmd.Name() != "index" && cmd.Name() != "update-index" && cmd.Name() != "list" {
8181
installer.UnlockPackRoot()
82-
err = installer.UpdatePublicIndex(installer.DefaultPublicIndex, true, true, false, false, 0, 0)
82+
err = installer.UpdatePublicIndex(installer.ActualPublicIndex, true, true, false, false, 0, 0)
8383
if err != nil {
8484
return err
8585
}

cmd/cryptography/checksum.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
errs "github.com/open-cmsis-pack/cpackget/cmd/errors"
12+
"github.com/open-cmsis-pack/cpackget/cmd/installer"
1213
"github.com/open-cmsis-pack/cpackget/cmd/utils"
1314
log "github.com/sirupsen/logrus"
1415
)
@@ -51,7 +52,7 @@ func GenerateChecksum(sourcePack, destinationDir, hashFunction string) error {
5152
return errs.ErrHashNotSupported
5253
}
5354
if !utils.FileExists(sourcePack) {
54-
log.Errorf("\"%s\" does not exist", sourcePack)
55+
log.Errorf("%q does not exist", sourcePack)
5556
return errs.ErrFileNotFound
5657
}
5758

@@ -63,11 +64,11 @@ func GenerateChecksum(sourcePack, destinationDir, hashFunction string) error {
6364
if !utils.DirExists(destinationDir) {
6465
return errs.ErrDirectoryNotFound
6566
}
66-
base = filepath.Clean(destinationDir) + string(filepath.Separator) + strings.TrimSuffix(string(filepath.Base(sourcePack)), ".pack")
67+
base = filepath.Clean(destinationDir) + string(filepath.Separator) + strings.TrimSuffix(string(filepath.Base(sourcePack)), installer.PackExtension)
6768
}
6869
checksumFilename := base + "." + strings.ReplaceAll(hashFunction, "-", "") + ".checksum"
6970
if utils.FileExists(checksumFilename) {
70-
log.Errorf("\"%s\" already exists, choose a diferent path", checksumFilename)
71+
log.Errorf("%q already exists, choose a different path", checksumFilename)
7172
return errs.ErrPathAlreadyExists
7273
}
7374

@@ -86,7 +87,7 @@ func GenerateChecksum(sourcePack, destinationDir, hashFunction string) error {
8687
// according to a provided .checksum file.
8788
func VerifyChecksum(packPath, checksumPath string) error {
8889
if !utils.FileExists(packPath) {
89-
log.Errorf("\"%s\" does not exist", packPath)
90+
log.Errorf("%q does not exist", packPath)
9091
return errs.ErrFileNotFound
9192
}
9293

@@ -95,15 +96,15 @@ func VerifyChecksum(packPath, checksumPath string) error {
9596
// exist .checksums with different algos in the same dir
9697
if checksumPath == "" {
9798
for _, hash := range Hashes {
98-
checksumPath = strings.ReplaceAll(packPath, ".pack", "."+hash+".checksum")
99+
checksumPath = strings.ReplaceAll(packPath, installer.PackExtension, "."+hash+".checksum")
99100
if utils.FileExists(checksumPath) {
100101
break
101102
}
102103
}
103104
}
104105

105106
if !utils.FileExists(checksumPath) {
106-
log.Errorf("\"%s\" does not exist", checksumPath)
107+
log.Errorf("%q does not exist", checksumPath)
107108
return errs.ErrFileNotFound
108109
}
109110
hashFunction := filepath.Ext(strings.Split(checksumPath, ".checksum")[0])[1:]
@@ -137,7 +138,7 @@ func VerifyChecksum(packPath, checksumPath string) error {
137138

138139
if digests[targetFile] != targetDigest {
139140
if digests[targetFile] == "" {
140-
log.Errorf("\"%s\" does not exist in the provided pack but is listed in the checksum file", targetFile)
141+
log.Errorf("%q does not exist in the provided pack but is listed in the checksum file", targetFile)
141142
return errs.ErrIntegrityCheckFailed
142143
}
143144
log.Debugf("%s != %s", digests[targetFile], targetDigest)

cmd/cryptography/signature.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -324,19 +324,19 @@ func embedPack(packFilename, version string, z *zip.ReadCloser, rawCert, signedH
324324
// specific creation functions.
325325
func SignPack(packPath, certPath, keyPath, outputDir, version string, certOnly, skipCertValidation, skipInfo bool) error {
326326
if !utils.FileExists(packPath) {
327-
log.Errorf("\"%s\" does not exist", packPath)
327+
log.Errorf("%q does not exist", packPath)
328328
return errs.ErrFileNotFound
329329
}
330330
// Flag validation is already performed in the command package,
331331
// so we can assume they make sense
332332
if keyPath != "" && !utils.FileExists(keyPath) {
333-
log.Errorf("\"%s\" does not exist", keyPath)
333+
log.Errorf("%q does not exist", keyPath)
334334
return errs.ErrFileNotFound
335335
}
336336
pgp := false
337337
if certPath != "" {
338338
if !utils.FileExists(certPath) {
339-
log.Errorf("\"%s\" does not exist", certPath)
339+
log.Errorf("%q does not exist", certPath)
340340
return errs.ErrFileNotFound
341341
}
342342
} else {
@@ -362,7 +362,7 @@ func SignPack(packPath, certPath, keyPath, outputDir, version string, certOnly,
362362

363363
zip, err := zip.OpenReader(packPath)
364364
if err != nil {
365-
log.Errorf("Can't decompress \"%s\": %s", packPath, err)
365+
log.Errorf("Can't decompress %q: %s", packPath, err)
366366
return errs.ErrFailedDecompressingFile
367367
}
368368
switch validateSignatureScheme(zip, version, true) {
@@ -514,16 +514,16 @@ func verifyPackPGPSignature(zip *zip.ReadCloser, keyPath, b64Signature string) e
514514
// specific validation functions.
515515
func VerifyPackSignature(packPath, pubPath, version string, export, skipCertValidation, skipInfo bool) error {
516516
if !utils.FileExists(packPath) {
517-
log.Errorf("\"%s\" does not exist", packPath)
517+
log.Errorf("%q does not exist", packPath)
518518
return errs.ErrFileNotFound
519519
}
520520
if pubPath != "" && !utils.FileExists(pubPath) {
521-
log.Errorf("\"%s\" does not exist", packPath)
521+
log.Errorf("%q does not exist", packPath)
522522
return errs.ErrFileNotFound
523523
}
524524
zip, err := zip.OpenReader(packPath)
525525
if err != nil {
526-
log.Errorf("Can't decompress \"%s\": %s", packPath, err)
526+
log.Errorf("Can't decompress %q: %s", packPath, err)
527527
return errs.ErrFailedDecompressingFile
528528
}
529529

cmd/cryptography/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func getDigestList(sourcePack, hashFunction string) (map[string]string, error) {
100100

101101
zipReader, err := zip.OpenReader(sourcePack)
102102
if err != nil {
103-
log.Errorf("can't decompress \"%s\": %s", sourcePack, err)
103+
log.Errorf("can't decompress %q: %s", sourcePack, err)
104104
return nil, errs.ErrFailedDecompressingFile
105105
}
106106

cmd/installer/pack.go

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func preparePack(packPath string, toBeRemoved, forceLatest, noLocal, nometa bool
155155

156156
if pack.isPackID && nometa {
157157
if meta, found := utils.SemverHasMeta(pack.Version); found {
158-
return pack, fmt.Errorf("%w: \"%s\". Expected vendor.pack.version", errs.ErrBadPackVersion, meta)
158+
return pack, fmt.Errorf("%w: %q. Expected vendor.pack.version", errs.ErrBadPackVersion, meta)
159159
}
160160
}
161161

@@ -188,20 +188,20 @@ func preparePack(packPath string, toBeRemoved, forceLatest, noLocal, nometa bool
188188
// Returns:
189189
// - error: an error object if the file does not exist or if there is an issue during download.
190190
func (p *PackType) fetch(timeout int) error {
191-
log.Debugf("Fetching pack file \"%s\" (or just making sure it exists locally)", p.path)
191+
log.Debugf("Fetching pack file %q (or just making sure it exists locally)", p.path)
192192
var err error
193193
if strings.HasPrefix(p.path, "http") {
194-
p.path, err = utils.DownloadFile(p.path, false, timeout)
194+
p.path, err = utils.DownloadFile(p.path, true, false, timeout)
195195
if err == errs.ErrTerminatedByUser {
196-
log.Infof("Aborting pack download. Removing \"%s\"", p.path)
196+
log.Infof("Aborting pack download. Removing %q", p.path)
197197
}
198198

199199
p.isDownloaded = true
200200
return err
201201
}
202202

203203
if !utils.FileExists(p.path) {
204-
log.Errorf("File \"%s\" doesn't exist", p.path)
204+
log.Errorf("File %q doesn't exist", p.path)
205205
return errs.ErrFileNotFound
206206
}
207207

@@ -212,10 +212,10 @@ func (p *PackType) fetch(timeout int) error {
212212
// to be installed.
213213
func (p *PackType) validate() error {
214214
log.Debug("Validating pack")
215-
pdscFileName := p.PdscFileName()
215+
myPdscFileName := p.PdscFileName()
216216
for _, file := range p.zipReader.File {
217-
if filepath.Base(file.Name) == pdscFileName {
218-
217+
ext := strings.ToLower(filepath.Ext(file.Name))
218+
if ext == PdscExtension {
219219
// Check if pack was compressed in a subfolder
220220
subfoldersCount := strings.Count(file.Name, "/") + strings.Count(file.Name, "\\")
221221
if subfoldersCount > 1 {
@@ -226,10 +226,12 @@ func (p *PackType) validate() error {
226226

227227
// Ensure the file path does not contain ".."
228228
if strings.Contains(file.Name, "..") {
229-
log.Errorf("File \"%s\" invalid file path", file.Name)
229+
log.Errorf("File %q invalid file path", file.Name)
230230
return errs.ErrInvalidFilePath
231231
}
232232

233+
myPdscFileName = p.PackID() + filepath.Ext(file.Name)
234+
233235
// Read pack's pdsc
234236
tmpPdscFileName := filepath.Join(os.TempDir(), utils.RandStringBytes(10))
235237
defer os.RemoveAll(tmpPdscFileName)
@@ -247,20 +249,31 @@ func (p *PackType) validate() error {
247249
version := p.GetVersion()
248250
latestVersion := p.Pdsc.LatestVersion()
249251

250-
log.Debugf("Making sure %s is the latest release in %s", p.targetVersion, pdscFileName)
252+
log.Debugf("Making sure %s is the latest release in %s", version, myPdscFileName)
251253

252254
if utils.SemverCompare(version, latestVersion) != 0 {
253255
releaseTag := p.Pdsc.FindReleaseTagByVersion(version)
254256
if releaseTag == nil {
255-
log.Errorf("The pack's pdsc (%s) has no release tag matching version \"%s\"", pdscFileName, version)
257+
log.Errorf("The pack's pdsc (%s) has no release tag matching version %q", myPdscFileName, version)
256258
return errs.ErrPackVersionNotFoundInPdsc
257259
}
258260

259-
log.Errorf("The latest release (%s) in pack's pdsc (%s) does not match pack version \"%s\"", latestVersion, pdscFileName, version)
261+
log.Errorf("The latest release (%s) in pack's pdsc (%s) does not match pack version %q", latestVersion, myPdscFileName, version)
260262
return errs.ErrPackVersionNotLatestReleasePdsc
261263
}
262264

263265
p.Pdsc.FileName = file.Name
266+
267+
pdscFileName := p.PdscFileName() // destination file name in .Download directory
268+
pdscFilePath := filepath.Join(tmpPdscFileName, file.Name) // #nosec
269+
newPdscFileName := p.PdscFileNameWithVersion()
270+
271+
if !p.IsPublic {
272+
_ = utils.CopyFile(pdscFilePath, filepath.Join(Installation.LocalDir, pdscFileName))
273+
}
274+
275+
_ = utils.CopyFile(pdscFilePath, filepath.Join(Installation.DownloadDir, newPdscFileName))
276+
264277
return nil
265278
} else {
266279
if strings.Contains(file.Name, "..") {
@@ -269,7 +282,7 @@ func (p *PackType) validate() error {
269282
}
270283
}
271284

272-
log.Errorf("\"%s\" not found in \"%s\"", pdscFileName, p.path)
285+
log.Errorf("%q not found in %q", myPdscFileName, p.path)
273286
return errs.ErrPdscFileNotFound
274287
}
275288

@@ -329,12 +342,12 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)
329342
p.path = filepath.FromSlash(p.path)
330343
p.path = filepath.Clean(p.path)
331344

332-
log.Debugf("Installing \"%s\"", p.path)
345+
log.Debugf("Installing %q", p.path)
333346

334347
var err error
335348
p.zipReader, err = zip.OpenReader(p.path)
336349
if err != nil {
337-
log.Errorf("Can't decompress \"%s\": %s", p.path, err)
350+
log.Errorf("Can't decompress %q: %s", p.path, err)
338351
return errs.ErrFailedDecompressingFile
339352
}
340353

@@ -379,12 +392,12 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)
379392
// Inflate all files
380393
err = utils.EnsureDir(packHomeDir)
381394
if err != nil {
382-
log.Errorf("Can't access pack directory \"%s\": %s", packHomeDir, err)
395+
log.Errorf("Can't access pack directory %q: %s", packHomeDir, err)
383396
return err
384397
}
385398

386399
if log.IsLevelEnabled(log.DebugLevel) {
387-
log.Debugf("Extracting files from \"%s\" to \"%s\"", p.path, packHomeDir)
400+
log.Debugf("Extracting files from %q to %q", p.path, packHomeDir)
388401
} else {
389402
log.Infof("Extracting files to %s...", packHomeDir)
390403
}
@@ -411,7 +424,7 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)
411424
defer p.zipReader.Close()
412425

413426
if err == errs.ErrTerminatedByUser {
414-
log.Infof("Aborting pack extraction. Removing \"%s\"", packHomeDir)
427+
log.Infof("Aborting pack extraction. Removing %q", packHomeDir)
415428
if newErr := p.uninstall(installation); newErr != nil {
416429
log.Error(err)
417430
}
@@ -423,16 +436,6 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)
423436
// Close zip file so Windows can't complain if we rename it
424437
p.zipReader.Close()
425438

426-
pdscFileName := p.PdscFileName()
427-
pdscFilePath := filepath.Join(packHomeDir, pdscFileName)
428-
newPdscFileName := p.PdscFileNameWithVersion()
429-
430-
if !p.IsPublic {
431-
_ = utils.CopyFile(pdscFilePath, filepath.Join(Installation.LocalDir, pdscFileName))
432-
}
433-
434-
_ = utils.CopyFile(pdscFilePath, filepath.Join(Installation.DownloadDir, newPdscFileName))
435-
436439
if !p.isDownloaded {
437440
return utils.CopyFile(p.path, packBackupPath)
438441
}
@@ -552,7 +555,7 @@ func (p *PackType) extractEula(packPath string) error {
552555
eulaFileName := packPath + "." + filepath.Base(p.Pdsc.License)
553556

554557
if utils.GetEncodedProgress() {
555-
log.Infof("[L:F\"%s\"]", eulaFileName)
558+
log.Infof("[L:F%q]", eulaFileName)
556559
} else {
557560
log.Infof("Extracting embedded license to %v", eulaFileName)
558561
}
@@ -562,7 +565,7 @@ func (p *PackType) extractEula(packPath string) error {
562565
os.Remove(eulaFileName)
563566
}
564567
if utils.FileExists(eulaFileName) {
565-
log.Errorf("Cannot remove previous copy of license file: \"%s\"", eulaFileName)
568+
log.Errorf("Cannot remove previous copy of license file: %q", eulaFileName)
566569
return errs.ErrFailedCreatingFile
567570
}
568571

@@ -572,7 +575,7 @@ func (p *PackType) extractEula(packPath string) error {
572575
// resolveVersionModifier takes into account eventual versionModifiers (@, @^, @~ and @>=) to determine
573576
// which version of a pack should be targeted for installation
574577
func (p *PackType) resolveVersionModifier(pdscXML *xml.PdscXML) {
575-
log.Debugf("Resolving version modifier for \"%s\" using PDSC \"%s\"", p.path, pdscXML.FileName)
578+
log.Debugf("Resolving version modifier for %q using PDSC %q", p.path, pdscXML.FileName)
576579

577580
if p.versionModifier == utils.ExactVersion {
578581
p.targetVersion = p.Version
@@ -728,17 +731,17 @@ func (p *PackType) PackIDWithVersion() string {
728731

729732
// PackFileName returns a string with how the pack file name would be: Vendor.PackName.x.y.z.pack
730733
func (p *PackType) PackFileName() string {
731-
return p.PackIDWithVersion() + ".pack"
734+
return p.PackIDWithVersion() + PackExtension
732735
}
733736

734737
// PdscFileName returns a string with how the pack's pdsc file name would be: Vendor.PackName.pdsc
735738
func (p *PackType) PdscFileName() string {
736-
return p.PackID() + ".pdsc"
739+
return p.PackID() + PdscExtension
737740
}
738741

739742
// PdscFileNameWithVersion returns a string with how the pack's pdsc file name would be: Vendor.PackName.x.y.z.pdsc
740743
func (p *PackType) PdscFileNameWithVersion() string {
741-
return p.PackIDWithVersion() + ".pdsc"
744+
return p.PackIDWithVersion() + PdscExtension
742745
}
743746

744747
// GetVersion makes sure to get the latest version for the pack

0 commit comments

Comments
 (0)