|
@@ -6,8 +6,11 @@ import (
|
|
|
"os"
|
|
|
"path/filepath"
|
|
|
"regexp"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
+ "sync"
|
|
|
"syscall"
|
|
|
+ "time"
|
|
|
"unsafe"
|
|
|
|
|
|
winio "github.com/Microsoft/go-winio"
|
|
@@ -234,3 +237,55 @@ func syscallOpenSequential(path string, mode int, _ uint32) (fd syscall.Handle,
|
|
|
h, e := syscall.CreateFile(pathp, access, sharemode, sa, createmode, fileFlagSequentialScan, 0)
|
|
|
return h, e
|
|
|
}
|
|
|
+
|
|
|
+// Helpers for TempFileSequential
|
|
|
+var rand uint32
|
|
|
+var randmu sync.Mutex
|
|
|
+
|
|
|
+func reseed() uint32 {
|
|
|
+ return uint32(time.Now().UnixNano() + int64(os.Getpid()))
|
|
|
+}
|
|
|
+func nextSuffix() string {
|
|
|
+ randmu.Lock()
|
|
|
+ r := rand
|
|
|
+ if r == 0 {
|
|
|
+ r = reseed()
|
|
|
+ }
|
|
|
+ r = r*1664525 + 1013904223 // constants from Numerical Recipes
|
|
|
+ rand = r
|
|
|
+ randmu.Unlock()
|
|
|
+ return strconv.Itoa(int(1e9 + r%1e9))[1:]
|
|
|
+}
|
|
|
+
|
|
|
+// TempFileSequential is a copy of ioutil.TempFile, modified to use sequential
|
|
|
+// file access. Below is the original comment from golang:
|
|
|
+// 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()
|
|
|
+// to find the pathname of the file. It is the caller's responsibility
|
|
|
+// to remove the file when no longer needed.
|
|
|
+func TempFileSequential(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 = OpenFileSequential(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
|
|
+ if os.IsExist(err) {
|
|
|
+ if nconflict++; nconflict > 10 {
|
|
|
+ randmu.Lock()
|
|
|
+ rand = reseed()
|
|
|
+ randmu.Unlock()
|
|
|
+ }
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ break
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|