// 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 osimport (_// for go:linkname)// random number source provided by runtime.// 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.////go:linkname runtime_rand runtime.randfunc runtime_rand() uint64func nextRandom() string {returnitoa.Uitoa(uint(uint32(runtime_rand())))}// CreateTemp creates a new temporary file in the directory dir,// opens the file for reading and writing, and returns the resulting 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 "*".// The file is created with mode 0o600 (before umask).// If dir is the empty string, CreateTemp uses the default directory for temporary files, as returned by [TempDir].// Multiple programs or goroutines calling CreateTemp simultaneously will not choose the same file.// The caller can use the file's Name method to find the pathname of the file.// It is the caller's responsibility to remove the file when it is no longer needed.func (, string) (*File, error) {if == "" { = TempDir() } , , := prefixAndSuffix()if != nil {returnnil, &PathError{Op: "createtemp", Path: , Err: } } = joinPath(, ) := 0for { := + nextRandom() + , := OpenFile(, O_RDWR|O_CREATE|O_EXCL, 0600)ifIsExist() {if ++; < 10000 {continue }returnnil, &PathError{Op: "createtemp", Path: + "*" + , Err: ErrExist} }return , }}var errPatternHasSeparator = errors.New("pattern contains path separator")// prefixAndSuffix splits pattern by the last wildcard "*", if applicable,// returning prefix as the part before "*" and suffix as the part after "*".func prefixAndSuffix( string) (, string, error) {for := 0; < len(); ++ {ifIsPathSeparator([]) {return"", "", errPatternHasSeparator } }if := bytealg.LastIndexByteString(, '*'); != -1 { , = [:], [+1:] } else { = }return , , nil}// MkdirTemp creates a new temporary directory in the directory dir// and returns the pathname of the new directory.// The new directory's name is generated by adding a random string to the end of pattern.// If pattern includes a "*", the random string replaces the last "*" instead.// The directory is created with mode 0o700 (before umask).// If dir is the empty string, MkdirTemp uses the default directory for temporary files, as returned by TempDir.// Multiple programs or goroutines calling MkdirTemp simultaneously will not choose the same directory.// It is the caller's responsibility to remove the directory when it is no longer needed.func (, string) (string, error) {if == "" { = TempDir() } , , := prefixAndSuffix()if != nil {return"", &PathError{Op: "mkdirtemp", Path: , Err: } } = joinPath(, ) := 0for { := + nextRandom() + := Mkdir(, 0700)if == nil {return , nil }ifIsExist() {if ++; < 10000 {continue }return"", &PathError{Op: "mkdirtemp", Path: + string(PathSeparator) + + "*" + , Err: ErrExist} }ifIsNotExist() {if , := Stat(); IsNotExist() {return"", } }return"", }}func joinPath(, string) string {iflen() > 0 && IsPathSeparator([len()-1]) {return + }return + string(PathSeparator) + }
The pages are generated with Goldsv0.7.3-preview. (GOOS=linux 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 @zigo_101 (reachable from the left QR code) to get the latest news of Golds.