Skip to content

Commit

Permalink
[cpackget] cpackcpackget deletes pack file content when installing fr…
Browse files Browse the repository at this point in the history
…om $CMSIS_PACK_ROOT/.Download/ #276
  • Loading branch information
edriouk committed Apr 4, 2024
1 parent b75890c commit 5bc211a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/installer/pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ func (p *PackType) install(installation *PacksInstallationType, checkEula bool)
packBackupPath := filepath.Join(Installation.DownloadDir, p.PackFileName())
packBackupPath = filepath.FromSlash(packBackupPath)
packBackupPath = filepath.Clean(packBackupPath)
if packBackupPath == p.path {
if utils.SameFile(packBackupPath, p.path) {
p.isDownloaded = true
}

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

0 comments on commit 5bc211a

Please sign in to comment.