Retro68/gcc/libgo/go/io/ioutil/tempfile.go

115 lines
2.9 KiB
Go
Raw Normal View History

// Copyright 2010 The Go Authors. All rights reserved.
2012-03-27 23:13:14 +00:00
// 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"
2019-06-02 15:48:37 +00:00
"strings"
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()))
}
2019-06-02 15:48:37 +00:00
func nextRandom() 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:]
}
2019-06-02 15:48:37 +00:00
// 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 "*".
2012-03-27 23:13:14 +00:00
// 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
2014-09-21 17:33:12 +00:00
// to remove the file when no longer needed.
2019-06-02 15:48:37 +00:00
func TempFile(dir, pattern string) (f *os.File, err error) {
2012-03-27 23:13:14 +00:00
if dir == "" {
dir = os.TempDir()
}
2019-06-02 15:48:37 +00:00
var prefix, suffix string
if pos := strings.LastIndex(pattern, "*"); pos != -1 {
prefix, suffix = pattern[:pos], pattern[pos+1:]
} else {
prefix = pattern
}
2012-03-27 23:13:14 +00:00
nconflict := 0
for i := 0; i < 10000; i++ {
2019-06-02 15:48:37 +00:00
name := filepath.Join(dir, prefix+nextRandom()+suffix)
2012-03-27 23:13:14 +00:00
f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
2017-04-10 11:32:00 +00:00
randmu.Lock()
2012-03-27 23:13:14 +00:00
rand = reseed()
2017-04-10 11:32:00 +00:00
randmu.Unlock()
2012-03-27 23:13:14 +00:00
}
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
2012-03-27 23:13:14 +00:00
// 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
2012-03-27 23:13:14 +00:00
// 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++ {
2019-06-02 15:48:37 +00:00
try := filepath.Join(dir, prefix+nextRandom())
2012-03-27 23:13:14 +00:00
err = os.Mkdir(try, 0700)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
2017-04-10 11:32:00 +00:00
randmu.Lock()
2012-03-27 23:13:14 +00:00
rand = reseed()
2017-04-10 11:32:00 +00:00
randmu.Unlock()
2012-03-27 23:13:14 +00:00
}
continue
}
if os.IsNotExist(err) {
if _, err := os.Stat(dir); os.IsNotExist(err) {
return "", err
}
}
2012-03-27 23:13:14 +00:00
if err == nil {
name = try
}
break
}
return
}