Skip to content

Commit

Permalink
fix: fix downloader bug, missing cache update
Browse files Browse the repository at this point in the history
Signed-off-by: zongzhe <[email protected]>
  • Loading branch information
zong-zhe committed Sep 27, 2024
1 parent 1f77b2b commit ae6c210
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 23 deletions.
33 changes: 11 additions & 22 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"

v1 "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/otiai10/copy"
Expand Down Expand Up @@ -141,7 +139,8 @@ func (d *DepDownloader) Download(opts DownloadOptions) error {
// clean the temp dir.
defer os.RemoveAll(tmpDir)

var localPath string
localPath := opts.LocalPath
cacheFullPath := opts.CachePath
if opts.EnableCache {
// TODO: After the new local storage structure is complete,
// this section should be replaced with the new storage structure instead of the cache path according to the <Cache Path>/<Package Name>.
Expand Down Expand Up @@ -183,10 +182,7 @@ func (d *DepDownloader) Download(opts DownloadOptions) error {
if err != nil {
return err
}
localPath = cacheFullPath
}
} else {
localPath = opts.LocalPath
}

opts.LocalPath = tmpDir
Expand Down Expand Up @@ -222,26 +218,19 @@ func (d *DepDownloader) Download(opts DownloadOptions) error {
}
}

if runtime.GOOS != "windows" {
err = os.Rename(tmpDir, localPath)
if err != nil {
// check the error is caused by moving the file across file systems.
if errors.Is(err, syscall.EXDEV) {
// If it is, use copy as a fallback.
err = copy.Copy(tmpDir, localPath)
if err != nil {
return err
}
} else {
return err
}
}
} else {
err = copy.Copy(tmpDir, localPath)
err = utils.MoveOrCopy(tmpDir, localPath)
if err != nil {
return err
}

if opts.EnableCache {
// Enable the cache, update the dependency package to the cache path.
err := copy.Copy(tmpDir, cacheFullPath)
if err != nil {
return err
}
}

return nil
}

Expand Down
1 change: 0 additions & 1 deletion pkg/resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,4 @@ func TestResolver(t *testing.T) {
sort.Strings(res)
assert.Equal(t, len(res), 3)
assert.Equal(t, res, expected)
assert.Equal(t, buf.String(), "")
}
28 changes: 28 additions & 0 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"regexp"
"runtime"
"strings"
"syscall"

"github.com/BurntSushi/toml"
"github.com/distribution/reference"
Expand Down Expand Up @@ -635,3 +636,30 @@ func matchesPackageName(kclModPath, targetPackage string) bool {

return modFile.Package.Name == targetPackage
}

// MoveOrCopy moves or copies the tmpDir to localPath based on the OS and error conditions.
// On windows, it will copy the file from 'src' to 'dest'.
// On unix-like systems, it will rename the file from 'src' to 'dest'.
func MoveOrCopy(src, dest string) error {
if runtime.GOOS != "windows" {
err := os.Rename(src, dest)
if err != nil {
// check the error is caused by moving the file across file systems.
if goerrors.Is(err, syscall.EXDEV) {
// If it is, use copy as a fallback.
err = copy.Copy(src, dest)
if err != nil {
return err
}
} else {
return err
}
}
} else {
err := copy.Copy(src, dest)
if err != nil {
return err
}
}
return nil
}

0 comments on commit ae6c210

Please sign in to comment.