| 1 |
package sequential |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"path/filepath" |
| 6 |
"strconv" |
| 7 |
"sync" |
| 8 |
"time" |
| 9 |
"unsafe" |
| 10 |
|
| 11 |
"golang.org/x/sys/windows" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
func Create(name string) (*os.File, error) { |
| 22 |
return openFileSequential(name, windows.O_RDWR|windows.O_CREAT|windows.O_TRUNC) |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
func Open(name string) (*os.File, error) { |
| 33 |
return openFileSequential(name, windows.O_RDONLY) |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
func OpenFile(name string, flag int, _ os.FileMode) (*os.File, error) { |
| 44 |
return openFileSequential(name, flag) |
| 45 |
} |
| 46 |
|
| 47 |
func openFileSequential(name string, flag int) (file *os.File, err error) { |
| 48 |
if name == "" { |
| 49 |
return nil, &os.PathError{Op: "open", Path: name, Err: windows.ERROR_FILE_NOT_FOUND} |
| 50 |
} |
| 51 |
r, e := openSequential(name, flag|windows.O_CLOEXEC) |
| 52 |
if e != nil { |
| 53 |
return nil, &os.PathError{Op: "open", Path: name, Err: e} |
| 54 |
} |
| 55 |
return os.NewFile(uintptr(r), name), nil |
| 56 |
} |
| 57 |
|
| 58 |
func makeInheritSa() *windows.SecurityAttributes { |
| 59 |
var sa windows.SecurityAttributes |
| 60 |
sa.Length = uint32(unsafe.Sizeof(sa)) |
| 61 |
sa.InheritHandle = 1 |
| 62 |
return &sa |
| 63 |
} |
| 64 |
|
| 65 |
func openSequential(path string, mode int) (fd windows.Handle, err error) { |
| 66 |
if len(path) == 0 { |
| 67 |
return windows.InvalidHandle, windows.ERROR_FILE_NOT_FOUND |
| 68 |
} |
| 69 |
pathp, err := windows.UTF16PtrFromString(path) |
| 70 |
if err != nil { |
| 71 |
return windows.InvalidHandle, err |
| 72 |
} |
| 73 |
var access uint32 |
| 74 |
switch mode & (windows.O_RDONLY | windows.O_WRONLY | windows.O_RDWR) { |
| 75 |
case windows.O_RDONLY: |
| 76 |
access = windows.GENERIC_READ |
| 77 |
case windows.O_WRONLY: |
| 78 |
access = windows.GENERIC_WRITE |
| 79 |
case windows.O_RDWR: |
| 80 |
access = windows.GENERIC_READ | windows.GENERIC_WRITE |
| 81 |
} |
| 82 |
if mode&windows.O_CREAT != 0 { |
| 83 |
access |= windows.GENERIC_WRITE |
| 84 |
} |
| 85 |
if mode&windows.O_APPEND != 0 { |
| 86 |
access &^= windows.GENERIC_WRITE |
| 87 |
access |= windows.FILE_APPEND_DATA |
| 88 |
} |
| 89 |
sharemode := uint32(windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE) |
| 90 |
var sa *windows.SecurityAttributes |
| 91 |
if mode&windows.O_CLOEXEC == 0 { |
| 92 |
sa = makeInheritSa() |
| 93 |
} |
| 94 |
var createmode uint32 |
| 95 |
switch { |
| 96 |
case mode&(windows.O_CREAT|windows.O_EXCL) == (windows.O_CREAT | windows.O_EXCL): |
| 97 |
createmode = windows.CREATE_NEW |
| 98 |
case mode&(windows.O_CREAT|windows.O_TRUNC) == (windows.O_CREAT | windows.O_TRUNC): |
| 99 |
createmode = windows.CREATE_ALWAYS |
| 100 |
case mode&windows.O_CREAT == windows.O_CREAT: |
| 101 |
createmode = windows.OPEN_ALWAYS |
| 102 |
case mode&windows.O_TRUNC == windows.O_TRUNC: |
| 103 |
createmode = windows.TRUNCATE_EXISTING |
| 104 |
default: |
| 105 |
createmode = windows.OPEN_EXISTING |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
h, e := windows.CreateFile(pathp, access, sharemode, sa, createmode, windows.FILE_FLAG_SEQUENTIAL_SCAN, 0) |
| 110 |
return h, e |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
var ( |
| 115 |
rand uint32 |
| 116 |
randmu sync.Mutex |
| 117 |
) |
| 118 |
|
| 119 |
func reseed() uint32 { |
| 120 |
return uint32(time.Now().UnixNano() + int64(os.Getpid())) |
| 121 |
} |
| 122 |
|
| 123 |
func nextSuffix() string { |
| 124 |
randmu.Lock() |
| 125 |
r := rand |
| 126 |
if r == 0 { |
| 127 |
r = reseed() |
| 128 |
} |
| 129 |
r = r*1664525 + 1013904223 |
| 130 |
rand = r |
| 131 |
randmu.Unlock() |
| 132 |
return strconv.Itoa(int(1e9 + r%1e9))[1:] |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
func CreateTemp(dir, prefix string) (f *os.File, err error) { |
| 143 |
if dir == "" { |
| 144 |
dir = os.TempDir() |
| 145 |
} |
| 146 |
|
| 147 |
nconflict := 0 |
| 148 |
for i := 0; i < 10000; i++ { |
| 149 |
name := filepath.Join(dir, prefix+nextSuffix()) |
| 150 |
f, err = openFileSequential(name, windows.O_RDWR|windows.O_CREAT|windows.O_EXCL) |
| 151 |
if os.IsExist(err) { |
| 152 |
if nconflict++; nconflict > 10 { |
| 153 |
randmu.Lock() |
| 154 |
rand = reseed() |
| 155 |
randmu.Unlock() |
| 156 |
} |
| 157 |
continue |
| 158 |
} |
| 159 |
break |
| 160 |
} |
| 161 |
return |
| 162 |
} |