1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
16 // Random number state.
17 // We generate random temporary file names so that there's a good
18 // chance the file doesn't exist yet - keeps the number of tries in
19 // TempFile to a minimum.
23 func reseed() uint32 {
24 return uint32(time.Now().UnixNano() + int64(os.Getpid()))
27 func nextRandom() string {
33 r = r*1664525 + 1013904223 // constants from Numerical Recipes
36 return strconv.Itoa(int(1e9 + r%1e9))[1:]
39 // TempFile creates a new temporary file in the directory dir,
40 // opens the file for reading and writing, and returns the resulting *os.File.
41 // The filename is generated by taking pattern and adding a random
42 // string to the end. If pattern includes a "*", the random string
43 // replaces the last "*".
44 // If dir is the empty string, TempFile uses the default directory
45 // for temporary files (see os.TempDir).
46 // Multiple programs calling TempFile simultaneously
47 // will not choose the same file. The caller can use f.Name()
48 // to find the pathname of the file. It is the caller's responsibility
49 // to remove the file when no longer needed.
50 func TempFile(dir, pattern string) (f *os.File, err error) {
55 var prefix, suffix string
56 if pos := strings.LastIndex(pattern, "*"); pos != -1 {
57 prefix, suffix = pattern[:pos], pattern[pos+1:]
63 for i := 0; i < 10000; i++ {
64 name := filepath.Join(dir, prefix+nextRandom()+suffix)
65 f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
67 if nconflict++; nconflict > 10 {
79 // TempDir creates a new temporary directory in the directory dir
80 // with a name beginning with prefix and returns the path of the
81 // new directory. If dir is the empty string, TempDir uses the
82 // default directory for temporary files (see os.TempDir).
83 // Multiple programs calling TempDir simultaneously
84 // will not choose the same directory. It is the caller's responsibility
85 // to remove the directory when no longer needed.
86 func TempDir(dir, prefix string) (name string, err error) {
92 for i := 0; i < 10000; i++ {
93 try := filepath.Join(dir, prefix+nextRandom())
94 err = os.Mkdir(try, 0700)
96 if nconflict++; nconflict > 10 {
103 if os.IsNotExist(err) {
104 if _, err := os.Stat(dir); os.IsNotExist(err) {