summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/spf13/afero/ioutil.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/spf13/afero/ioutil.go')
-rw-r--r--vendor/github.com/spf13/afero/ioutil.go32
1 files changed, 21 insertions, 11 deletions
diff --git a/vendor/github.com/spf13/afero/ioutil.go b/vendor/github.com/spf13/afero/ioutil.go
index 5c3a3d8f..a403133e 100644
--- a/vendor/github.com/spf13/afero/ioutil.go
+++ b/vendor/github.com/spf13/afero/ioutil.go
@@ -22,6 +22,7 @@ import (
"path/filepath"
"sort"
"strconv"
+ "strings"
"sync"
"time"
)
@@ -147,7 +148,7 @@ func reseed() uint32 {
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 @@ func nextSuffix() string {
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 @@ func TempDir(fs Fs, dir, prefix string) (name string, err error) {
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 {