| 1 | //go:build windows |
| 2 | |
| 3 | package fileops |
| 4 | |
| 5 | import ( |
| 6 | "os" |
| 7 | |
| 8 | "golang.org/x/sys/windows" |
| 9 | ) |
| 10 | |
| 11 | // OpenReplaceableRead keeps the source readable while another process |
| 12 | // atomically replaces its pathname. os.Open uses a share mode that can reject |
| 13 | // replacement on Windows, which would make source-fencing races fail before |
| 14 | // the pager can observe the changed identity. |
| 15 | func OpenReplaceableRead(path string) (*os.File, error) { |
| 16 | name, err := windows.UTF16PtrFromString(path) |
| 17 | if err != nil { |
| 18 | return nil, err |
| 19 | } |
| 20 | handle, err := windows.CreateFile( |
| 21 | name, |
| 22 | windows.GENERIC_READ, |
| 23 | windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 24 | nil, |
| 25 | windows.OPEN_EXISTING, |
| 26 | windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_OPEN_REPARSE_POINT|windows.FILE_FLAG_BACKUP_SEMANTICS, |
| 27 | 0, |
| 28 | ) |
| 29 | if err != nil { |
| 30 | return nil, err |
| 31 | } |
| 32 | file := os.NewFile(uintptr(handle), path) |
| 33 | if file == nil { |
| 34 | _ = windows.CloseHandle(handle) |
| 35 | return nil, os.ErrInvalid |
| 36 | } |
| 37 | return file, nil |
| 38 | } |
| 39 |