Skip to content
This repository was archived by the owner on Jul 30, 2021. It is now read-only.

Commit d173849

Browse files
committed
pkg/bootkube: Fix time-of-check-to-time-of-use in copyFile
If not overwriting, use the `O_EXCL` flag when opening the destination file to ensure we create it.
1 parent 40a7f46 commit d173849

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

pkg/bootkube/bootstrap.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package bootkube
22

33
import (
4-
"fmt"
5-
"io/ioutil"
4+
"io"
65
"os"
76
"path/filepath"
87
"strings"
@@ -62,20 +61,23 @@ func (b *bootstrapControlPlane) Teardown() error {
6261
// copyFile copies a single file from src to dst. Returns an error if overwrite is true and dst
6362
// exists, or if any I/O error occurs during copying.
6463
func copyFile(src, dst string, overwrite bool) error {
64+
flags := os.O_CREATE|os.O_WRONLY
6565
if !overwrite {
66-
fi, err := os.Stat(dst)
67-
if fi != nil {
68-
return fmt.Errorf("file already exists: %v", dst)
69-
}
70-
if !os.IsNotExist(err) {
71-
return err
72-
}
66+
flags |= os.O_EXCL
67+
}
68+
69+
dstfile, err := os.OpenFile(dst, flags, os.FileMode(0600))
70+
if err != nil {
71+
return err
7372
}
74-
data, err := ioutil.ReadFile(src)
73+
74+
srcfile, err := os.Open(src)
7575
if err != nil {
7676
return err
7777
}
78-
return ioutil.WriteFile(dst, data, os.FileMode(0600))
78+
79+
_, err = io.Copy(dstfile, srcfile)
80+
return err
7981
}
8082

8183
// copyDirectory copies srcDir to dstDir recursively. It returns the paths of files (not

0 commit comments

Comments
 (0)