Implement fix, add comments so it's more apparent why we're doing special logic
This commit is contained in:
parent
7ecfb057ff
commit
6e8c6a15ad
@ -115,7 +115,10 @@ func (d *DownloadClient) Get() (string, error) {
|
|||||||
// Files when we don't copy the file are special cased.
|
// Files when we don't copy the file are special cased.
|
||||||
var f *os.File
|
var f *os.File
|
||||||
var finalPath string
|
var finalPath string
|
||||||
|
sourcePath := ""
|
||||||
if url.Scheme == "file" && !d.config.CopyFile {
|
if url.Scheme == "file" && !d.config.CopyFile {
|
||||||
|
// This is a special case where we use a source file that already exists
|
||||||
|
// locally and we don't make a copy. Normally we would copy or download.
|
||||||
finalPath = url.Path
|
finalPath = url.Path
|
||||||
log.Printf("Using local file: %s", finalPath)
|
log.Printf("Using local file: %s", finalPath)
|
||||||
|
|
||||||
@ -123,6 +126,8 @@ func (d *DownloadClient) Get() (string, error) {
|
|||||||
if runtime.GOOS == "windows" && len(finalPath) > 0 && finalPath[0] == '/' {
|
if runtime.GOOS == "windows" && len(finalPath) > 0 && finalPath[0] == '/' {
|
||||||
finalPath = finalPath[1:len(finalPath)]
|
finalPath = finalPath[1:len(finalPath)]
|
||||||
}
|
}
|
||||||
|
// Keep track of the source so we can make sure not to delete this later
|
||||||
|
sourcePath = finalPath
|
||||||
} else {
|
} else {
|
||||||
finalPath = d.config.TargetPath
|
finalPath = d.config.TargetPath
|
||||||
|
|
||||||
@ -150,8 +155,10 @@ func (d *DownloadClient) Get() (string, error) {
|
|||||||
var verify bool
|
var verify bool
|
||||||
verify, err = d.VerifyChecksum(finalPath)
|
verify, err = d.VerifyChecksum(finalPath)
|
||||||
if err == nil && !verify {
|
if err == nil && !verify {
|
||||||
// Delete the file
|
// Only delete the file if we made a copy or downloaded it
|
||||||
os.Remove(finalPath)
|
if sourcePath != finalPath {
|
||||||
|
os.Remove(finalPath)
|
||||||
|
}
|
||||||
|
|
||||||
err = fmt.Errorf(
|
err = fmt.Errorf(
|
||||||
"checksums didn't match expected: %s",
|
"checksums didn't match expected: %s",
|
||||||
|
@ -340,6 +340,10 @@ func TestHashForType(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestDownloadFileUrl tests a special case where we use a local file for
|
||||||
|
// iso_url. In this case we can still verify the checksum but we should not
|
||||||
|
// delete the file if the checksum fails. Instead we'll just error and let the
|
||||||
|
// user fix the checksum.
|
||||||
func TestDownloadFileUrl(t *testing.T) {
|
func TestDownloadFileUrl(t *testing.T) {
|
||||||
cwd, err := os.Getwd()
|
cwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -361,14 +365,10 @@ func TestDownloadFileUrl(t *testing.T) {
|
|||||||
|
|
||||||
client := NewDownloadClient(config)
|
client := NewDownloadClient(config)
|
||||||
|
|
||||||
filename, err := client.Get()
|
// Verify that we fail to match the checksum
|
||||||
defer os.Remove(config.TargetPath)
|
_, err = client.Get()
|
||||||
if err != nil {
|
if err.Error() != "checksums didn't match expected: 6e6f7065" {
|
||||||
t.Fatalf("Failed to download test file")
|
t.Fatalf("Unexpected failure; expected checksum not to match")
|
||||||
}
|
|
||||||
|
|
||||||
if sourcePath != filename {
|
|
||||||
t.Errorf("Filename doesn't match; expected %s got %s", sourcePath, filename)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = os.Stat(sourcePath); err != nil {
|
if _, err = os.Stat(sourcePath); err != nil {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user