vendor/github.com/spf13/afero/ioutil.go
changeset 256 6d9efbef00a9
parent 242 2a9ec03fe5a1
child 260 445e01aede7e
--- a/vendor/github.com/spf13/afero/ioutil.go	Mon Jun 07 20:58:18 2021 +0200
+++ b/vendor/github.com/spf13/afero/ioutil.go	Sun Jul 11 10:35:56 2021 +0200
@@ -22,6 +22,7 @@
 	"path/filepath"
 	"sort"
 	"strconv"
+	"strings"
 	"sync"
 	"time"
 )
@@ -147,7 +148,7 @@
 	return uint32(time.Now().UnixNano() + int64(os.Getpid()))
 }
 
-func nextSuffix() string {
+func nextRandom() string {
 	randmu.Lock()
 	r := rand
 	if r == 0 {
@@ -159,27 +160,36 @@
 	return strconv.Itoa(int(1e9 + r%1e9))[1:]
 }
 
-// TempFile creates a new temporary file in the directory dir
-// with a name beginning with prefix, opens the file for reading
-// and writing, and returns the resulting *File.
+// TempFile creates a new temporary file in the directory dir,
+// opens the file for reading and writing, and returns the resulting *os.File.
+// The filename is generated by taking pattern and adding a random
+// string to the end. If pattern includes a "*", the random string
+// replaces the last "*".
 // If dir is the empty string, TempFile uses the default directory
 // for temporary files (see os.TempDir).
 // Multiple programs calling TempFile simultaneously
-// will not choose the same file.  The caller can use f.Name()
-// to find the pathname of the file.  It is the caller's responsibility
+// will not choose the same file. The caller can use f.Name()
+// to find the pathname of the file. It is the caller's responsibility
 // to remove the file when no longer needed.
-func (a Afero) TempFile(dir, prefix string) (f File, err error) {
-	return TempFile(a.Fs, dir, prefix)
+func (a Afero) TempFile(dir, pattern string) (f File, err error) {
+	return TempFile(a.Fs, dir, pattern)
 }
 
-func TempFile(fs Fs, dir, prefix string) (f File, err error) {
+func TempFile(fs Fs, dir, pattern string) (f File, err error) {
 	if dir == "" {
 		dir = os.TempDir()
 	}
 
+	var prefix, suffix string
+	if pos := strings.LastIndex(pattern, "*"); pos != -1 {
+		prefix, suffix = pattern[:pos], pattern[pos+1:]
+	} else {
+		prefix = pattern
+	}
+
 	nconflict := 0
 	for i := 0; i < 10000; i++ {
-		name := filepath.Join(dir, prefix+nextSuffix())
+		name := filepath.Join(dir, prefix+nextRandom()+suffix)
 		f, err = fs.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
 		if os.IsExist(err) {
 			if nconflict++; nconflict > 10 {
@@ -211,7 +221,7 @@
 
 	nconflict := 0
 	for i := 0; i < 10000; i++ {
-		try := filepath.Join(dir, prefix+nextSuffix())
+		try := filepath.Join(dir, prefix+nextRandom())
 		err = fs.Mkdir(try, 0700)
 		if os.IsExist(err) {
 			if nconflict++; nconflict > 10 {