@@ -404,10 +404,17 @@ func downloadBazelIfNecessary(version string, bazeliskHome string, bazelForkOrUR
404
404
}
405
405
406
406
func atomicWriteFile (path string , contents []byte , perm os.FileMode ) error {
407
- if err := os .MkdirAll (filepath .Dir (path ), 0755 ); err != nil {
407
+ parent := filepath .Dir (path )
408
+ if err := os .MkdirAll (parent , 0755 ); err != nil {
408
409
return fmt .Errorf ("failed to MkdirAll parent of %s: %w" , path , err )
409
410
}
410
- tmpPath := path + ".tmp"
411
+ tmpFile , err := os .CreateTemp (parent , filepath .Base (path )+ ".tmp" )
412
+ if err != nil {
413
+ return fmt .Errorf ("failed to create temporary file in %s: %w" , parent , err )
414
+ }
415
+ tmpFile .Close ()
416
+ defer os .Remove (tmpFile .Name ())
417
+ tmpPath := tmpFile .Name ()
411
418
if err := os .WriteFile (tmpPath , contents , perm ); err != nil {
412
419
return fmt .Errorf ("failed to write file %s: %w" , tmpPath , err )
413
420
}
@@ -458,12 +465,20 @@ func downloadBazelToCAS(version string, bazeliskHome string, repos *Repositories
458
465
f .Close ()
459
466
actualSha256 := strings .ToLower (fmt .Sprintf ("%x" , h .Sum (nil )))
460
467
461
- pathToBazelInCAS := filepath .Join (casDir , actualSha256 , "bin" , "bazel" + platforms .DetermineExecutableFilenameSuffix ())
462
- if err := os .MkdirAll (filepath .Dir (pathToBazelInCAS ), 0755 ); err != nil {
468
+ bazelInCASBasename := "bazel" + platforms .DetermineExecutableFilenameSuffix ()
469
+ pathToBazelInCAS := filepath .Join (casDir , actualSha256 , "bin" , bazelInCASBasename )
470
+ dirForBazelInCAS := filepath .Dir (pathToBazelInCAS )
471
+ if err := os .MkdirAll (dirForBazelInCAS , 0755 ); err != nil {
463
472
return "" , "" , fmt .Errorf ("failed to MkdirAll parent of %s: %w" , pathToBazelInCAS , err )
464
473
}
465
474
466
- tmpPathInCorrectDirectory := pathToBazelInCAS + ".tmp"
475
+ tmpPathFile , err := os .CreateTemp (dirForBazelInCAS , bazelInCASBasename + ".tmp" )
476
+ if err != nil {
477
+ return "" , "" , fmt .Errorf ("failed to create temporary file in %s: %w" , dirForBazelInCAS , err )
478
+ }
479
+ tmpPathFile .Close ()
480
+ defer os .Remove (tmpPathFile .Name ())
481
+ tmpPathInCorrectDirectory := tmpPathFile .Name ()
467
482
if err := os .Rename (tmpDestPath , tmpPathInCorrectDirectory ); err != nil {
468
483
return "" , "" , fmt .Errorf ("failed to move %s to %s: %w" , tmpDestPath , tmpPathInCorrectDirectory , err )
469
484
}
0 commit comments