Ask ioutil to write in a temp file

Similar to https://github.com/kubernetes/helm/issues/3253 we're using
Helm by directly importing the Go package.

A concurrency issue arises when multiple requests come for the same
package. Currently DownloadTo truncates contents of destination file and
rewrites it.

This patch changes the behaviour so instead of directly writing into the
destination it'll write the content in a temp file and renames it once
download is finished.

This change fixes sporadic issues in
5c5c6a1c21/main_test.go
pull/4106/head
Amir Saeid 7 years ago
parent b6660cd5c9
commit e6e6e4347d

@ -97,7 +97,9 @@ func (c *ChartDownloader) DownloadTo(ref, version, dest string) (string, *proven
name := filepath.Base(u.Path)
destfile := filepath.Join(dest, name)
if err := ioutil.WriteFile(destfile, data.Bytes(), 0644); err != nil {
tempdestfile := destfile + ".part"
if err := ioutil.WriteFile(tempdestfile, data.Bytes(), 0644); err != nil {
err = os.Rename(tempdestfile, destfile)
return destfile, nil, err
}

Loading…
Cancel
Save