|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package stack |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "unsafe" |
| 12 | + |
| 13 | + "golang.org/x/sys/windows" |
| 14 | +) |
| 15 | + |
| 16 | +func readStateFile(path string) ([]byte, error) { |
| 17 | + name, err := windowsFilePath(path) |
| 18 | + if err != nil { |
| 19 | + return nil, err |
| 20 | + } |
| 21 | + // Let publication replace the name while readers finish with the old file. |
| 22 | + handle, err := windows.CreateFile(&name[0], windows.GENERIC_READ, |
| 23 | + windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 24 | + nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0) |
| 25 | + if err != nil { |
| 26 | + return nil, &os.PathError{Op: "open", Path: path, Err: err} |
| 27 | + } |
| 28 | + f := os.NewFile(uintptr(handle), path) |
| 29 | + data, err := io.ReadAll(f) |
| 30 | + return data, errors.Join(err, f.Close()) |
| 31 | +} |
| 32 | + |
| 33 | +func publishFile(temp, path string, replace bool) error { |
| 34 | + from, err := windowsFilePath(temp) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + to, err := windowsFilePath(path) |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + // Never remove the destination first, or allow a cross-volume copy/delete. |
| 43 | + if replace { |
| 44 | + err = replaceFileWindows(from, to) |
| 45 | + } else { |
| 46 | + err = windows.MoveFileEx(&from[0], &to[0], windows.MOVEFILE_WRITE_THROUGH) |
| 47 | + } |
| 48 | + if err != nil { |
| 49 | + return &os.LinkError{Op: "publish", Old: temp, New: path, Err: err} |
| 50 | + } |
| 51 | + return nil |
| 52 | +} |
| 53 | + |
| 54 | +func replaceFileWindows(from, to []uint16) error { |
| 55 | + handle, err := windows.CreateFile(&from[0], windows.DELETE|windows.GENERIC_WRITE, |
| 56 | + windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 57 | + nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL|windows.FILE_FLAG_WRITE_THROUGH, 0) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + // FILE_RENAME_INFO has pointer-sized padding and a trailing UTF-16 name. |
| 63 | + type renameInfo struct { |
| 64 | + Flags uint32 |
| 65 | + RootDirectory windows.Handle |
| 66 | + FileNameLength uint32 |
| 67 | + FileName [1]uint16 |
| 68 | + } |
| 69 | + buffer := make([]byte, int(unsafe.Sizeof(renameInfo{}))+len(to)*2) |
| 70 | + info := (*renameInfo)(unsafe.Pointer(&buffer[0])) |
| 71 | + info.Flags = windows.FILE_RENAME_REPLACE_IF_EXISTS | windows.FILE_RENAME_POSIX_SEMANTICS |
| 72 | + info.FileNameLength = uint32(len(to)-1) * 2 |
| 73 | + copy(unsafe.Slice(&info.FileName[0], len(to)), to) |
| 74 | + |
| 75 | + err = windows.SetFileInformationByHandle(handle, windows.FileRenameInfoEx, &buffer[0], uint32(len(buffer))) |
| 76 | + if err == nil { |
| 77 | + return errors.Join(windows.FlushFileBuffers(handle), windows.CloseHandle(handle)) |
| 78 | + } |
| 79 | + if closeErr := windows.CloseHandle(handle); closeErr != nil { |
| 80 | + return errors.Join(err, closeErr) |
| 81 | + } |
| 82 | + |
| 83 | + // Older Windows versions/filesystems may lack POSIX rename. Only fall back |
| 84 | + // for unsupported operations; legacy rename still reports open-reader errors. |
| 85 | + if errors.Is(err, windows.ERROR_NOT_SUPPORTED) || errors.Is(err, windows.ERROR_INVALID_PARAMETER) || |
| 86 | + errors.Is(err, windows.ERROR_INVALID_FUNCTION) { |
| 87 | + return windows.MoveFileEx(&from[0], &to[0], windows.MOVEFILE_REPLACE_EXISTING|windows.MOVEFILE_WRITE_THROUGH) |
| 88 | + } |
| 89 | + return err |
| 90 | +} |
| 91 | + |
| 92 | +func windowsFilePath(path string) ([]uint16, error) { |
| 93 | + path, err := filepath.Abs(path) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + switch { |
| 98 | + case strings.HasPrefix(path, `\\?\`), strings.HasPrefix(path, `\\.\`): |
| 99 | + case strings.HasPrefix(path, `\\`): |
| 100 | + path = `\\?\UNC\` + path[2:] |
| 101 | + default: |
| 102 | + path = `\\?\` + path |
| 103 | + } |
| 104 | + return windows.UTF16FromString(path) |
| 105 | +} |
| 106 | + |
| 107 | +func syncDirectory(string) error { |
| 108 | + // Windows has no directory fsync; publication flushes the file or uses WRITE_THROUGH. |
| 109 | + return nil |
| 110 | +} |
0 commit comments