| 1 | package reversesshfs |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | |
| 8 | "golang.org/x/sys/windows" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | func openCloudRoot(directory string) (rootedSys, error) { |
| 14 | dir := filepath.Clean(directory) |
| 15 | drive := filepath.VolumeName(dir) |
| 16 | if len(drive) != 2 || drive[1] != ':' { |
| 17 | return rootedSys{}, errDenied |
| 18 | } |
| 19 | base, err := openRootedSys(drive + `\`) |
| 20 | if err != nil { |
| 21 | return rootedSys{}, err |
| 22 | } |
| 23 | defer base.close() |
| 24 | h, err := ntCreate(base.rootHandle, strings.TrimPrefix(dir[len(drive):], `\`), fileDirectoryAccess, 0, windows.FILE_OPEN, windows.FILE_DIRECTORY_FILE, windows.OBJ_DONT_REPARSE) |
| 25 | if err != nil { |
| 26 | return rootedSys{}, err |
| 27 | } |
| 28 | name, err := normalizedName(h) |
| 29 | if err != nil { |
| 30 | windows.CloseHandle(h) |
| 31 | return rootedSys{}, errDenied |
| 32 | } |
| 33 | return rootedSys{localPath: dir, rootHandle: h, rootName: name}, nil |
| 34 | } |
| 35 | |
| 36 | func (h *rootedHandlers) cloudOpenRead(rel string) (*os.File, error) { |
| 37 | var fh windows.Handle |
| 38 | var err error |
| 39 | if rel == "." { |
| 40 | self := windows.CurrentProcess() |
| 41 | err = windows.DuplicateHandle(self, h.rootHandle, self, &fh, 0, false, windows.DUPLICATE_SAME_ACCESS) |
| 42 | } else { |
| 43 | fh, err = ntCreate(h.rootHandle, filepath.FromSlash(rel), windows.FILE_GENERIC_READ, 0, windows.FILE_OPEN, 0, windows.OBJ_DONT_REPARSE) |
| 44 | } |
| 45 | if err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | err = h.checkHandle(fh) |
| 49 | var fi windows.ByHandleFileInformation |
| 50 | if err == nil { |
| 51 | err = windows.GetFileInformationByHandle(fh, &fi) |
| 52 | } |
| 53 | if err == nil && (fi.FileAttributes&(windows.FILE_ATTRIBUTE_REPARSE_POINT|windows.FILE_ATTRIBUTE_DEVICE) != 0 || (fi.FileAttributes&windows.FILE_ATTRIBUTE_DIRECTORY == 0 && fi.NumberOfLinks != 1)) { |
| 54 | err = errDenied |
| 55 | } |
| 56 | if err != nil { |
| 57 | windows.CloseHandle(fh) |
| 58 | return nil, err |
| 59 | } |
| 60 | return os.NewFile(uintptr(fh), rel), nil |
| 61 | } |