2012-03-27 23:13:14 +00:00
|
|
|
// Copyright 2010 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package ioutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
2014-09-21 17:33:12 +00:00
|
|
|
"sync"
|
2012-03-27 23:13:14 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-09-21 17:33:12 +00:00
|
|
|
// Random number state.
|
2012-03-27 23:13:14 +00:00
|
|
|
// 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
|
2014-09-21 17:33:12 +00:00
|
|
|
var randmu sync.Mutex
|
2012-03-27 23:13:14 +00:00
|
|
|
|
|
|
|
func reseed() uint32 {
|
|
|
|
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
|
|
|
|
}
|
|
|
|
|
|
|
|
func nextSuffix() string {
|
2014-09-21 17:33:12 +00:00
|
|
|
randmu.Lock()
|
2012-03-27 23:13:14 +00:00
|
|
|
r := rand
|
|
|
|
if r == 0 {
|
|
|
|
r = reseed()
|
|
|
|
}
|
|
|
|
r = r*1664525 + 1013904223 // constants from Numerical Recipes
|
|
|
|
rand = r
|
2014-09-21 17:33:12 +00:00
|
|
|
randmu.Unlock()
|
2012-03-27 23:13:14 +00:00
|
|
|
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 *os.File.
|
|
|
|
// 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()
|
2014-09-21 17:33:12 +00:00
|
|
|
// to find the pathname of the file. It is the caller's responsibility
|
|
|
|
// to remove the file when no longer needed.
|
2012-03-27 23:13:14 +00:00
|
|
|
func TempFile(dir, prefix string) (f *os.File, err error) {
|
|
|
|
if dir == "" {
|
|
|
|
dir = os.TempDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
nconflict := 0
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
name := filepath.Join(dir, prefix+nextSuffix())
|
|
|
|
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
|
if os.IsExist(err) {
|
|
|
|
if nconflict++; nconflict > 10 {
|
|
|
|
rand = reseed()
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// TempDir creates a new temporary directory in the directory dir
|
|
|
|
// with a name beginning with prefix and returns the path of the
|
|
|
|
// new directory. If dir is the empty string, TempDir uses the
|
|
|
|
// default directory for temporary files (see os.TempDir).
|
|
|
|
// Multiple programs calling TempDir simultaneously
|
|
|
|
// will not choose the same directory. It is the caller's responsibility
|
|
|
|
// to remove the directory when no longer needed.
|
|
|
|
func TempDir(dir, prefix string) (name string, err error) {
|
|
|
|
if dir == "" {
|
|
|
|
dir = os.TempDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
nconflict := 0
|
|
|
|
for i := 0; i < 10000; i++ {
|
|
|
|
try := filepath.Join(dir, prefix+nextSuffix())
|
|
|
|
err = os.Mkdir(try, 0700)
|
|
|
|
if os.IsExist(err) {
|
|
|
|
if nconflict++; nconflict > 10 {
|
|
|
|
rand = reseed()
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
name = try
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|