Skip to content
Snippets Groups Projects
Commit c946c61e authored by Steve Azzopardi's avatar Steve Azzopardi
Browse files

Merge branch 'fix/windows-cache-extractor' into 'master'

Add fix for race condition in windows cache extraction

Closes #2574

See merge request gitlab-org/gitlab-runner!863
parents 1d141ad4 27119796
No related branches found
No related tags found
No related merge requests found
...@@ -35,16 +35,15 @@ func (c *CacheExtractorCommand) getClient() *CacheClient { ...@@ -35,16 +35,15 @@ func (c *CacheExtractorCommand) getClient() *CacheClient {
return c.client return c.client
} }
func checkIfUpToDate(path string, resp *http.Response) (bool, time.Time) {
fi, _ := os.Lstat(path)
date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified"))
return fi != nil && !date.After(fi.ModTime()), date
}
func (c *CacheExtractorCommand) download() (bool, error) { func (c *CacheExtractorCommand) download() (bool, error) {
os.MkdirAll(filepath.Dir(c.File), 0700) os.MkdirAll(filepath.Dir(c.File), 0700)
file, err := ioutil.TempFile(filepath.Dir(c.File), "cache")
if err != nil {
return false, err
}
defer file.Close()
defer os.Remove(file.Name())
resp, err := c.getClient().Get(c.URL) resp, err := c.getClient().Get(c.URL)
if err != nil { if err != nil {
return true, err return true, err
...@@ -59,13 +58,19 @@ func (c *CacheExtractorCommand) download() (bool, error) { ...@@ -59,13 +58,19 @@ func (c *CacheExtractorCommand) download() (bool, error) {
return retry, fmt.Errorf("Received: %s", resp.Status) return retry, fmt.Errorf("Received: %s", resp.Status)
} }
fi, _ := os.Lstat(c.File) upToDate, date := checkIfUpToDate(c.File, resp)
date, _ := time.Parse(http.TimeFormat, resp.Header.Get("Last-Modified")) if upToDate {
if fi != nil && !date.After(fi.ModTime()) {
logrus.Infoln(filepath.Base(c.File), "is up to date") logrus.Infoln(filepath.Base(c.File), "is up to date")
return false, nil return false, nil
} }
file, err := ioutil.TempFile(filepath.Dir(c.File), "cache")
if err != nil {
return false, err
}
defer os.Remove(file.Name())
defer file.Close()
logrus.Infoln("Downloading", filepath.Base(c.File), "from", url_helpers.CleanURL(c.URL)) logrus.Infoln("Downloading", filepath.Base(c.File), "from", url_helpers.CleanURL(c.URL))
_, err = io.Copy(file, resp.Body) _, err = io.Copy(file, resp.Body)
if err != nil { if err != nil {
...@@ -73,6 +78,11 @@ func (c *CacheExtractorCommand) download() (bool, error) { ...@@ -73,6 +78,11 @@ func (c *CacheExtractorCommand) download() (bool, error) {
} }
os.Chtimes(file.Name(), time.Now(), date) os.Chtimes(file.Name(), time.Now(), date)
err = file.Close()
if err != nil {
return false, err
}
err = os.Rename(file.Name(), c.File) err = os.Rename(file.Name(), c.File)
if err != nil { if err != nil {
return false, err return false, err
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment