Skip to content

Commit 8c04c68

Browse files
committed
Use os.CreateTemp
1 parent c46dd3f commit 8c04c68

1 file changed

Lines changed: 2 additions & 6 deletions

File tree

internal/batches/repozip/fetcher.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"io"
7-
"math/rand"
87
"net/http"
98
"os"
109
"path"
@@ -337,10 +336,7 @@ func fetchRepositoryFile(ctx context.Context, client HTTPClient, repo RepoRevisi
337336
return false, fmt.Errorf("unable to fetch archive (HTTP %d from %s)", resp.StatusCode, req.URL.String())
338337
}
339338

340-
tmpFileDir := fmt.Sprintf("%s.%d.tmp", dest, rand.Int())
341-
// Ensure the file doesn't exist.
342-
_ = os.Remove(tmpFileDir)
343-
f, err := os.Create(tmpFileDir)
339+
f, err := os.CreateTemp(path.Dir(dest), fmt.Sprintf("%s-*.tmp", path.Base(dest)))
344340
if err != nil {
345341
return false, err
346342
}
@@ -352,7 +348,7 @@ func fetchRepositoryFile(ctx context.Context, client HTTPClient, repo RepoRevisi
352348

353349
// Atomically create the actual file, so that there are no artifacts left behind
354350
// when this process is aborted, network errors occur, or some witchcraft goes on.
355-
if err := os.Rename(tmpFileDir, dest); err != nil {
351+
if err := os.Rename(f.Name(), dest); err != nil {
356352
return false, errors.Wrap(err, "renaming temp file")
357353
}
358354

0 commit comments

Comments
 (0)