sequential_windows.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package sequential
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strconv"
  6. "sync"
  7. "syscall"
  8. "time"
  9. "unsafe"
  10. "golang.org/x/sys/windows"
  11. )
  12. // Create creates the named file with mode 0666 (before umask), truncating
  13. // it if it already exists. If successful, methods on the returned
  14. // File can be used for I/O; the associated file descriptor has mode
  15. // O_RDWR.
  16. // If there is an error, it will be of type *PathError.
  17. func Create(name string) (*os.File, error) {
  18. return OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0)
  19. }
  20. // Open opens the named file for reading. If successful, methods on
  21. // the returned file can be used for reading; the associated file
  22. // descriptor has mode O_RDONLY.
  23. // If there is an error, it will be of type *PathError.
  24. func Open(name string) (*os.File, error) {
  25. return OpenFile(name, os.O_RDONLY, 0)
  26. }
  27. // OpenFile is the generalized open call; most users will use Open
  28. // or Create instead.
  29. // If there is an error, it will be of type *PathError.
  30. func OpenFile(name string, flag int, _ os.FileMode) (*os.File, error) {
  31. if name == "" {
  32. return nil, &os.PathError{Op: "open", Path: name, Err: syscall.ENOENT}
  33. }
  34. r, err := openFileSequential(name, flag, 0)
  35. if err == nil {
  36. return r, nil
  37. }
  38. return nil, &os.PathError{Op: "open", Path: name, Err: err}
  39. }
  40. func openFileSequential(name string, flag int, _ os.FileMode) (file *os.File, err error) {
  41. r, e := openSequential(name, flag|windows.O_CLOEXEC, 0)
  42. if e != nil {
  43. return nil, e
  44. }
  45. return os.NewFile(uintptr(r), name), nil
  46. }
  47. func makeInheritSa() *windows.SecurityAttributes {
  48. var sa windows.SecurityAttributes
  49. sa.Length = uint32(unsafe.Sizeof(sa))
  50. sa.InheritHandle = 1
  51. return &sa
  52. }
  53. func openSequential(path string, mode int, _ uint32) (fd windows.Handle, err error) {
  54. if len(path) == 0 {
  55. return windows.InvalidHandle, windows.ERROR_FILE_NOT_FOUND
  56. }
  57. pathp, err := windows.UTF16PtrFromString(path)
  58. if err != nil {
  59. return windows.InvalidHandle, err
  60. }
  61. var access uint32
  62. switch mode & (windows.O_RDONLY | windows.O_WRONLY | windows.O_RDWR) {
  63. case windows.O_RDONLY:
  64. access = windows.GENERIC_READ
  65. case windows.O_WRONLY:
  66. access = windows.GENERIC_WRITE
  67. case windows.O_RDWR:
  68. access = windows.GENERIC_READ | windows.GENERIC_WRITE
  69. }
  70. if mode&windows.O_CREAT != 0 {
  71. access |= windows.GENERIC_WRITE
  72. }
  73. if mode&windows.O_APPEND != 0 {
  74. access &^= windows.GENERIC_WRITE
  75. access |= windows.FILE_APPEND_DATA
  76. }
  77. sharemode := uint32(windows.FILE_SHARE_READ | windows.FILE_SHARE_WRITE)
  78. var sa *windows.SecurityAttributes
  79. if mode&windows.O_CLOEXEC == 0 {
  80. sa = makeInheritSa()
  81. }
  82. var createmode uint32
  83. switch {
  84. case mode&(windows.O_CREAT|windows.O_EXCL) == (windows.O_CREAT | windows.O_EXCL):
  85. createmode = windows.CREATE_NEW
  86. case mode&(windows.O_CREAT|windows.O_TRUNC) == (windows.O_CREAT | windows.O_TRUNC):
  87. createmode = windows.CREATE_ALWAYS
  88. case mode&windows.O_CREAT == windows.O_CREAT:
  89. createmode = windows.OPEN_ALWAYS
  90. case mode&windows.O_TRUNC == windows.O_TRUNC:
  91. createmode = windows.TRUNCATE_EXISTING
  92. default:
  93. createmode = windows.OPEN_EXISTING
  94. }
  95. // Use FILE_FLAG_SEQUENTIAL_SCAN rather than FILE_ATTRIBUTE_NORMAL as implemented in golang.
  96. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
  97. const fileFlagSequentialScan = 0x08000000 // FILE_FLAG_SEQUENTIAL_SCAN
  98. h, e := windows.CreateFile(pathp, access, sharemode, sa, createmode, fileFlagSequentialScan, 0)
  99. return h, e
  100. }
  101. // Helpers for CreateTemp
  102. var rand uint32
  103. var randmu sync.Mutex
  104. func reseed() uint32 {
  105. return uint32(time.Now().UnixNano() + int64(os.Getpid()))
  106. }
  107. func nextSuffix() string {
  108. randmu.Lock()
  109. r := rand
  110. if r == 0 {
  111. r = reseed()
  112. }
  113. r = r*1664525 + 1013904223 // constants from Numerical Recipes
  114. rand = r
  115. randmu.Unlock()
  116. return strconv.Itoa(int(1e9 + r%1e9))[1:]
  117. }
  118. // CreateTemp is a copy of os.CreateTemp, modified to use sequential
  119. // file access. Below is the original comment from golang:
  120. // TempFile creates a new temporary file in the directory dir
  121. // with a name beginning with prefix, opens the file for reading
  122. // and writing, and returns the resulting *os.File.
  123. // If dir is the empty string, TempFile uses the default directory
  124. // for temporary files (see os.TempDir).
  125. // Multiple programs calling TempFile simultaneously
  126. // will not choose the same file. The caller can use f.Name()
  127. // to find the pathname of the file. It is the caller's responsibility
  128. // to remove the file when no longer needed.
  129. func CreateTemp(dir, prefix string) (f *os.File, err error) {
  130. if dir == "" {
  131. dir = os.TempDir()
  132. }
  133. nconflict := 0
  134. for i := 0; i < 10000; i++ {
  135. name := filepath.Join(dir, prefix+nextSuffix())
  136. f, err = OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0o600)
  137. if os.IsExist(err) {
  138. if nconflict++; nconflict > 10 {
  139. randmu.Lock()
  140. rand = reseed()
  141. randmu.Unlock()
  142. }
  143. continue
  144. }
  145. break
  146. }
  147. return
  148. }