-
Notifications
You must be signed in to change notification settings - Fork 1
/
tempfifo_unix.go
56 lines (47 loc) · 1010 Bytes
/
tempfifo_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 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.
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris
package adapted
import (
"os"
"path/filepath"
"strconv"
"sync"
"time"
"golang.org/x/sys/unix"
)
var (
rand uint32
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:]
}
func TempFifo(prefix string) (name string, err error) {
dir := os.TempDir()
nconflict := 0
for i := 0; i < 10000; i++ {
name = filepath.Join(dir, prefix+nextSuffix())
err = unix.Mkfifo(name, 0o600)
if os.IsExist(err) {
if nconflict++; nconflict > 10 {
rand = reseed()
}
continue
}
break
}
return
}