-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from JaSei/temp_file_pattern
tempfile pattern test
- Loading branch information
Showing
6 changed files
with
102 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,9 @@ go: | |
- 1.7 | ||
- 1.8 | ||
- 1.9 | ||
- "1.10" | ||
- 1.11 | ||
- 1.12 | ||
|
||
install: | ||
- make setup | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.3.0 | ||
0.3.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package pathutil | ||
|
||
// copy of go1.11 version of ioutil.TempFile | ||
// https://golang.org/src/io/ioutil/tempfile.go?s=1419:1477#L40 | ||
// because eerlier versions don't support pattern | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Random number state. | ||
// We generate random temporary file names so that there's a good | ||
// chance the file doesn't exist yet - keeps the number of tries in | ||
// TempFile to a minimum. | ||
var rand uint32 | ||
var randmu sync.Mutex | ||
|
||
func reseed() uint32 { | ||
return uint32(time.Now().UnixNano() + int64(os.Getpid())) | ||
} | ||
|
||
func nextRandom() string { | ||
randmu.Lock() | ||
r := rand | ||
if r == 0 { | ||
r = reseed() | ||
} | ||
r = r*1664525 + 1013904223 // constants from Numerical Recipes | ||
rand = r | ||
randmu.Unlock() | ||
return strconv.Itoa(int(1e9 + r%1e9))[1:] | ||
} | ||
|
||
func tempFile(dir, pattern string) (f *os.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+nextRandom()+suffix) | ||
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) | ||
if os.IsExist(err) { | ||
if nconflict++; nconflict > 10 { | ||
randmu.Lock() | ||
rand = reseed() | ||
randmu.Unlock() | ||
} | ||
continue | ||
} | ||
break | ||
} | ||
return | ||
} |