| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "unsafe" |
| 7 | |
| 8 | "golang.org/x/sys/windows" |
| 9 | ) |
| 10 | |
| 11 | func openWorkspaceTallyFile(root *os.Root, path string) (*os.File, error) { |
| 12 | parent, err := root.Open(".") |
| 13 | if err != nil { |
| 14 | return nil, err |
| 15 | } |
| 16 | defer parent.Close() |
| 17 | name, err := windows.NewNTUnicodeString(filepath.Clean(path)) |
| 18 | if err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | // Resolve relative to the pinned workspace handle and reject reparse points |
| 22 | // anywhere in the path, including directory junctions. |
| 23 | attributes := windows.OBJECT_ATTRIBUTES{ |
| 24 | RootDirectory: windows.Handle(parent.Fd()), ObjectName: name, |
| 25 | Attributes: windows.OBJ_CASE_INSENSITIVE | windows.OBJ_DONT_REPARSE, |
| 26 | } |
| 27 | attributes.Length = uint32(unsafe.Sizeof(attributes)) |
| 28 | var handle windows.Handle |
| 29 | var status windows.IO_STATUS_BLOCK |
| 30 | err = windows.NtCreateFile(&handle, windows.FILE_GENERIC_READ, &attributes, &status, nil, 0, |
| 31 | windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, windows.FILE_OPEN, |
| 32 | windows.FILE_NON_DIRECTORY_FILE|windows.FILE_SYNCHRONOUS_IO_NONALERT, 0, 0) |
| 33 | if err != nil { |
| 34 | return nil, err |
| 35 | } |
| 36 | return os.NewFile(uintptr(handle), path), nil |
| 37 | } |
| 38 |