Source File
tempfile.go
Belonging Package
io/ioutil
package ioutil
import (
)
var rand uint32
var randmu sync.Mutex
func () uint32 {
return uint32(time.Now().UnixNano() + int64(os.Getpid()))
}
func () string {
randmu.Lock()
:= rand
if == 0 {
= reseed()
}
= *1664525 + 1013904223 // constants from Numerical Recipes
rand =
randmu.Unlock()
return strconv.Itoa(int(1e9 + %1e9))[1:]
}
func (, string) ( *os.File, error) {
if == "" {
= os.TempDir()
}
, , := prefixAndSuffix()
if != nil {
return
}
:= 0
for := 0; < 10000; ++ {
:= filepath.Join(, +nextRandom()+)
, = os.OpenFile(, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if os.IsExist() {
if ++; > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
break
}
return
}
var errPatternHasSeparator = errors.New("pattern contains path separator")
func ( string) (, string, error) {
if strings.ContainsRune(, os.PathSeparator) {
= errPatternHasSeparator
return
}
if := strings.LastIndex(, "*"); != -1 {
, = [:], [+1:]
} else {
=
}
return
}
func (, string) ( string, error) {
if == "" {
= os.TempDir()
}
, , := prefixAndSuffix()
if != nil {
return
}
:= 0
for := 0; < 10000; ++ {
:= filepath.Join(, +nextRandom()+)
= os.Mkdir(, 0700)
if os.IsExist() {
if ++; > 10 {
randmu.Lock()
rand = reseed()
randmu.Unlock()
}
continue
}
if os.IsNotExist() {
if , := os.Stat(); os.IsNotExist() {
return "",
}
}
if == nil {
=
}
break
}
return
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |