Skip to content

Commit

Permalink
Fix races on temporary file copies (#616)
Browse files Browse the repository at this point in the history
* Fix races on temporary file copies

* Appease Windows

* Fix ordering
  • Loading branch information
sushain97 authored Oct 9, 2024
1 parent 572e6f7 commit 2f239be
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,17 @@ func downloadBazelIfNecessary(version string, bazeliskHome string, bazelForkOrUR
}

func atomicWriteFile(path string, contents []byte, perm os.FileMode) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
parent := filepath.Dir(path)
if err := os.MkdirAll(parent, 0755); err != nil {
return fmt.Errorf("failed to MkdirAll parent of %s: %w", path, err)
}
tmpPath := path + ".tmp"
tmpFile, err := os.CreateTemp(parent, filepath.Base(path)+".tmp")
if err != nil {
return fmt.Errorf("failed to create temporary file in %s: %w", parent, err)
}
tmpFile.Close()
defer os.Remove(tmpFile.Name())
tmpPath := tmpFile.Name()
if err := os.WriteFile(tmpPath, contents, perm); err != nil {
return fmt.Errorf("failed to write file %s: %w", tmpPath, err)
}
Expand Down Expand Up @@ -458,12 +465,20 @@ func downloadBazelToCAS(version string, bazeliskHome string, repos *Repositories
f.Close()
actualSha256 := strings.ToLower(fmt.Sprintf("%x", h.Sum(nil)))

pathToBazelInCAS := filepath.Join(casDir, actualSha256, "bin", "bazel"+platforms.DetermineExecutableFilenameSuffix())
if err := os.MkdirAll(filepath.Dir(pathToBazelInCAS), 0755); err != nil {
bazelInCASBasename := "bazel" + platforms.DetermineExecutableFilenameSuffix()
pathToBazelInCAS := filepath.Join(casDir, actualSha256, "bin", bazelInCASBasename)
dirForBazelInCAS := filepath.Dir(pathToBazelInCAS)
if err := os.MkdirAll(dirForBazelInCAS, 0755); err != nil {
return "", "", fmt.Errorf("failed to MkdirAll parent of %s: %w", pathToBazelInCAS, err)
}

tmpPathInCorrectDirectory := pathToBazelInCAS + ".tmp"
tmpPathFile, err := os.CreateTemp(dirForBazelInCAS, bazelInCASBasename+".tmp")
if err != nil {
return "", "", fmt.Errorf("failed to create temporary file in %s: %w", dirForBazelInCAS, err)
}
tmpPathFile.Close()
defer os.Remove(tmpPathFile.Name())
tmpPathInCorrectDirectory := tmpPathFile.Name()
if err := os.Rename(tmpDestPath, tmpPathInCorrectDirectory); err != nil {
return "", "", fmt.Errorf("failed to move %s to %s: %w", tmpDestPath, tmpPathInCorrectDirectory, err)
}
Expand Down

0 comments on commit 2f239be

Please sign in to comment.