|
11 | 11 | #include <assert.h> |
12 | 12 | #include <ntstatus.h> |
13 | 13 | #include <bcrypt.h> |
| 14 | +#include <winternl.h> |
| 15 | +#include <ntdef.h> |
| 16 | + |
| 17 | +typedef ULONG (__stdcall *pNtCreateFile)( |
| 18 | + PHANDLE FileHandle, |
| 19 | + ULONG DesiredAccess, |
| 20 | + PVOID ObjectAttributes, |
| 21 | + PVOID IoStatusBlock, |
| 22 | + PLARGE_INTEGER AllocationSize, |
| 23 | + ULONG FileAttributes, |
| 24 | + ULONG ShareAccess, |
| 25 | + ULONG CreateDisposition, |
| 26 | + ULONG CreateOptions, |
| 27 | + PVOID EaBuffer, |
| 28 | + ULONG EaLength |
| 29 | + ); |
14 | 30 |
|
15 | 31 | #include <caml/mlvalues.h> |
16 | 32 | #include <caml/memory.h> |
17 | 33 | #include <caml/alloc.h> |
18 | 34 | #include <caml/unixsupport.h> |
19 | 35 | #include <caml/bigarray.h> |
| 36 | +#include <caml/osdeps.h> |
20 | 37 |
|
21 | 38 | #ifdef ARCH_SIXTYFOUR |
22 | 39 | #define Int63_val(v) Long_val(v) |
@@ -64,19 +81,170 @@ CAMLprim value caml_eio_windows_pwritev(value v_fd, value v_bufs, value v_offset |
64 | 81 | uerror("pwritev is not supported on windows yet", Nothing); |
65 | 82 | } |
66 | 83 |
|
67 | | -CAMLprim value caml_eio_windows_openat(value v_dirfd, value v_pathname, value v_flags, value v_mode) |
68 | | -{ |
69 | | - uerror("openat is not supported on windows yet", Nothing); |
| 84 | +// File-system operations |
| 85 | + |
| 86 | +// No follow |
| 87 | +void no_follow(HANDLE h) { |
| 88 | + BY_HANDLE_FILE_INFORMATION b; |
| 89 | + |
| 90 | + if (!GetFileInformationByHandle(h, &b)) { |
| 91 | + caml_win32_maperr(GetLastError()); |
| 92 | + uerror("nofollow", Nothing); |
| 93 | + } |
| 94 | + |
| 95 | + if (b.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { |
| 96 | + CloseHandle(h); |
| 97 | + caml_unix_error(ELOOP, "nofollow", Nothing); |
| 98 | + } |
70 | 99 | } |
71 | 100 |
|
72 | | -CAMLprim value caml_eio_windows_mkdirat(value v_fd, value v_path, value v_perm) |
| 101 | +// We recreate an openat like function using NtCreateFile |
| 102 | +CAMLprim value caml_eio_windows_openat(value v_dirfd, value v_nofollow, value v_pathname, value v_desired_access, value v_create_disposition, value v_create_options) |
73 | 103 | { |
74 | | - uerror("mkdirat is not supported on windows yet", Nothing); |
| 104 | + CAMLparam2(v_dirfd, v_pathname); |
| 105 | + HANDLE h, dir; |
| 106 | + OBJECT_ATTRIBUTES obj_attr; |
| 107 | + IO_STATUS_BLOCK io_status; |
| 108 | + wchar_t *pathname; |
| 109 | + UNICODE_STRING relative; |
| 110 | + NTSTATUS r; |
| 111 | + |
| 112 | + // Not sure what the overhead of this is, but it allows us to have low-level control |
| 113 | + // over file creation. In particular, we can specify the HANDLE to the parent directory |
| 114 | + // of a relative path a la openat. |
| 115 | + pNtCreateFile NtCreatefile = (pNtCreateFile)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtCreateFile"); |
| 116 | + caml_unix_check_path(v_pathname, "openat"); |
| 117 | + pathname = caml_stat_strdup_to_utf16(String_val(v_pathname)); |
| 118 | + RtlInitUnicodeString(&relative, pathname); |
| 119 | + |
| 120 | + // If NULL the filepath has to be absolute |
| 121 | + if (Is_some(v_dirfd)) { |
| 122 | + dir = Handle_val(Field(v_dirfd, 0)); |
| 123 | + } else { |
| 124 | + dir = NULL; |
| 125 | + } |
| 126 | + |
| 127 | + // Initialise object attributes, passing in the root directory FD |
| 128 | + InitializeObjectAttributes( |
| 129 | + &obj_attr, |
| 130 | + &relative, |
| 131 | + OBJ_CASE_INSENSITIVE, // TODO: Double-check what flags need to be passed at this point. |
| 132 | + dir, |
| 133 | + NULL |
| 134 | + ); |
| 135 | + |
| 136 | + // Create the file |
| 137 | + r = NtCreatefile( |
| 138 | + &h, |
| 139 | + Int_val(v_desired_access) | FILE_READ_ATTRIBUTES, |
| 140 | + &obj_attr, |
| 141 | + &io_status, |
| 142 | + 0, // Allocation size |
| 143 | + FILE_ATTRIBUTE_NORMAL, // TODO: Could check flags to see if we can do READONLY here a la OCaml |
| 144 | + (FILE_SHARE_READ | FILE_SHARE_WRITE), |
| 145 | + Int_val(v_create_disposition), |
| 146 | + ( |
| 147 | + FILE_SYNCHRONOUS_IO_NONALERT |
| 148 | + | FILE_OPEN_FOR_BACKUP_INTENT |
| 149 | + | (Bool_val(v_nofollow) ? FILE_FLAG_OPEN_REPARSE_POINT : Int_val(v_create_options))), |
| 150 | + NULL, // Extended attribute buffer |
| 151 | + 0 // Extended attribute buffer length |
| 152 | + ); |
| 153 | + |
| 154 | + // Free the allocated pathname |
| 155 | + caml_stat_free(pathname); |
| 156 | + |
| 157 | + if (h == INVALID_HANDLE_VALUE) { |
| 158 | + caml_win32_maperr(RtlNtStatusToDosError(r)); |
| 159 | + uerror("openat handle", v_pathname); |
| 160 | + } |
| 161 | + |
| 162 | + if (!NT_SUCCESS(r)) { |
| 163 | + caml_win32_maperr(RtlNtStatusToDosError(r)); |
| 164 | + uerror("openat", Nothing); |
| 165 | + } |
| 166 | + |
| 167 | + // No follow check -- Windows doesn't actually have that ability |
| 168 | + // so we have to do it after the fact. This will raise if a symbolic |
| 169 | + // link is encountered and will close the handle. |
| 170 | + if (Bool_val(v_nofollow)) { |
| 171 | + no_follow(h); |
| 172 | + } |
| 173 | + |
| 174 | + CAMLreturn(caml_win32_alloc_handle(h)); |
| 175 | +} |
| 176 | + |
| 177 | +value caml_eio_windows_openat_bytes(value* values, int argc) { |
| 178 | + return caml_eio_windows_openat(values[0], values[1], values[2], values[3], values[4], values[5]); |
75 | 179 | } |
76 | 180 |
|
77 | | -CAMLprim value caml_eio_windows_unlinkat(value v_fd, value v_path, value v_dir) |
| 181 | +CAMLprim value caml_eio_windows_unlinkat(value v_dirfd, value v_pathname, value v_dir) |
78 | 182 | { |
79 | | - uerror("unlinkat is not supported on windows yet", Nothing); |
| 183 | + CAMLparam2(v_dirfd, v_pathname); |
| 184 | + HANDLE h, dir; |
| 185 | + OBJECT_ATTRIBUTES obj_attr; |
| 186 | + IO_STATUS_BLOCK io_status; |
| 187 | + wchar_t *pathname; |
| 188 | + UNICODE_STRING relative; |
| 189 | + NTSTATUS r; |
| 190 | + |
| 191 | + // Not sure what the overhead of this is, but it allows us to have low-level control |
| 192 | + // over file creation. In particular, we can specify the HANDLE to the parent directory |
| 193 | + // of a relative path a la openat. |
| 194 | + pNtCreateFile NtCreatefile = (pNtCreateFile)GetProcAddress(GetModuleHandle("ntdll.dll"), "NtCreateFile"); |
| 195 | + caml_unix_check_path(v_pathname, "openat"); |
| 196 | + pathname = caml_stat_strdup_to_utf16(String_val(v_pathname)); |
| 197 | + RtlInitUnicodeString(&relative, pathname); |
| 198 | + |
| 199 | + // If NULL the filepath has to be absolute |
| 200 | + if (Is_some(v_dirfd)) { |
| 201 | + dir = Handle_val(Field(v_dirfd, 0)); |
| 202 | + } else { |
| 203 | + dir = NULL; |
| 204 | + } |
| 205 | + |
| 206 | + // Initialise object attributes, passing in the root directory FD |
| 207 | + InitializeObjectAttributes( |
| 208 | + &obj_attr, |
| 209 | + &relative, |
| 210 | + OBJ_CASE_INSENSITIVE, // TODO: Double-check what flags need to be passed at this point. |
| 211 | + dir, |
| 212 | + NULL |
| 213 | + ); |
| 214 | + |
| 215 | + // Create the file |
| 216 | + r = NtCreatefile( |
| 217 | + &h, |
| 218 | + (SYNCHRONIZE | DELETE), |
| 219 | + &obj_attr, |
| 220 | + &io_status, |
| 221 | + 0, // Allocation size |
| 222 | + FILE_ATTRIBUTE_NORMAL, // TODO: Could check flags to see if we can do READONLY here a la OCaml |
| 223 | + (FILE_SHARE_DELETE), |
| 224 | + FILE_OPEN, |
| 225 | + ((Bool_val(v_dir) ? FILE_DIRECTORY_FILE : FILE_NON_DIRECTORY_FILE) | FILE_SYNCHRONOUS_IO_NONALERT | FILE_DELETE_ON_CLOSE), |
| 226 | + NULL, // Extended attribute buffer |
| 227 | + 0 // Extended attribute buffer length |
| 228 | + ); |
| 229 | + |
| 230 | + // Free the allocated pathname |
| 231 | + caml_stat_free(pathname); |
| 232 | + |
| 233 | + if (h == INVALID_HANDLE_VALUE) { |
| 234 | + caml_win32_maperr(RtlNtStatusToDosError(r)); |
| 235 | + uerror("openat", v_pathname); |
| 236 | + } |
| 237 | + |
| 238 | + if (!NT_SUCCESS(r)) { |
| 239 | + caml_win32_maperr(RtlNtStatusToDosError(r)); |
| 240 | + uerror("openat", Nothing); |
| 241 | + } |
| 242 | + |
| 243 | + // Now close the file to delete it |
| 244 | + BOOL closed; |
| 245 | + closed = CloseHandle(h); |
| 246 | + |
| 247 | + CAMLreturn(Val_unit); |
80 | 248 | } |
81 | 249 |
|
82 | 250 | CAMLprim value caml_eio_windows_renameat(value v_old_fd, value v_old_path, value v_new_fd, value v_new_path) |
|
0 commit comments