Skip to content

Commit 3267136

Browse files
Merge pull request #4632 from vanhalenar/unsafe
USHIFT-5373: Replace GenerateUniqueTempPath by Go library functions
2 parents 407022d + 3ec36ca commit 3267136

File tree

4 files changed

+36
-25
lines changed

4 files changed

+36
-25
lines changed

pkg/admin/autorecovery/state.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ func (s *state) SaveToIntermediate() error {
3232
if err != nil {
3333
return err
3434
}
35-
path, err := util.GenerateUniqueTempPath(filepath.Join(s.storagePath, stateFilename))
35+
file, err := util.CreateTempFile(filepath.Join(s.storagePath, stateFilename))
3636
if err != nil {
3737
return err
3838
}
39+
defer file.Close()
40+
path := file.Name()
3941
s.intermediatePath = path
4042
klog.InfoS("Saving intermediate state", "state", contents, "path", path)
41-
return os.WriteFile(path, contents, 0600)
43+
_, err = file.Write(contents)
44+
45+
return err
4246
}
4347

4448
func (s *state) MoveToFinal() error {

pkg/admin/data/atomic_dir_copy.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ var (
1717
// data dir as a backup with Copy-On-Write feature.
1818
cpArgs = []string{
1919
"--verbose",
20-
"--recursive", // copy directories recursively
21-
"--preserve", // preserve mode, ownership, timestamps
22-
"--reflink=auto", // enable Copy-on-Write copy
20+
"--recursive", // copy directories recursively
21+
"--no-target-directory", // do not create subdirectory in the target dir
22+
"--preserve", // preserve mode, ownership, timestamps
23+
"--reflink=auto", // enable Copy-on-Write copy
2324
}
2425
)
2526

@@ -41,7 +42,7 @@ func (c *AtomicDirCopy) CopyToIntermediate() error {
4142
return nil
4243
}
4344
var err error
44-
c.intermediatePath, err = util.GenerateUniqueTempPath(c.Destination)
45+
c.intermediatePath, err = util.CreateTempDir(c.Destination)
4546
if err != nil {
4647
return err
4748
}

pkg/config/node.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ func (c *Config) createNodeNameFile(nodeName, filePath, dataDir string) error {
6161
if err := os.MkdirAll(dataDir, 0700); err != nil {
6262
return fmt.Errorf("failed to create data dir: %w", err)
6363
}
64-
tmpPath, err := util.GenerateUniqueTempPath(filePath)
64+
file, err := util.CreateTempFile(filePath)
6565
if err != nil {
6666
return fmt.Errorf("failed to generate temp path for %s: %w", filePath, err)
6767
}
68-
if err := os.WriteFile(tmpPath, []byte(nodeName), 0400); err != nil {
69-
return fmt.Errorf("failed to write nodename file %q: %v", filePath, err)
68+
defer file.Close()
69+
tmpPath := file.Name()
70+
if _, err := file.Write([]byte(nodeName)); err != nil {
71+
return fmt.Errorf("failed to write nodename file %q: %v", tmpPath, err)
7072
}
7173
if err := os.Rename(tmpPath, filePath); err != nil {
7274
if err := os.RemoveAll(tmpPath); err != nil {

pkg/util/util.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"errors"
66
"fmt"
77
"io/fs"
8-
"math/rand/v2"
98
"net/http"
109
"os"
1110
"path/filepath"
@@ -144,19 +143,24 @@ func (l LogFilePath) Remove() error {
144143
return err
145144
}
146145

147-
// GenerateUniqueTempPath returns a filepath from given path with extra suffix
148-
// which doesn't exist.
149-
func GenerateUniqueTempPath(path string) (string, error) {
150-
// 1000 tries
151-
for i := 0; i < 1000; i++ {
152-
//nolint:gosec
153-
rnd := rand.IntN(100000)
154-
newPath := fmt.Sprintf("%s.tmp.%d", path, rnd)
155-
if exists, err := PathExists(newPath); err != nil {
156-
return "", err
157-
} else if !exists {
158-
return newPath, nil
159-
}
160-
}
161-
return "", fmt.Errorf("attempted to generate unique temporary path (%q) for 1000 tries - giving up", path)
146+
// GetTempPathArgs returns the directory in which to create a temp file
147+
// along with the pattern for the temp file name.
148+
func getTempPathArgs(path string) (string, string) {
149+
dir, file := filepath.Split(path)
150+
pattern := file + ".tmp."
151+
return dir, pattern
152+
}
153+
154+
// CreateTempFile creates a temporary file from given path and returns
155+
// resulting file
156+
func CreateTempFile(path string) (*os.File, error) {
157+
dir, pattern := getTempPathArgs(path)
158+
return os.CreateTemp(dir, pattern)
159+
}
160+
161+
// CreateTempDir creates a temporary directory from given path and returns
162+
// the pathname of the new directory.
163+
func CreateTempDir(path string) (string, error) {
164+
dir, pattern := getTempPathArgs(path)
165+
return os.MkdirTemp(dir, pattern)
162166
}

0 commit comments

Comments
 (0)