/src/FreeRDP/channels/drive/client/drive_file.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * File System Virtual Channel |
4 | | * |
5 | | * Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * Copyright 2010-2011 Vic Lee |
7 | | * Copyright 2012 Gerald Richter |
8 | | * Copyright 2015 Thincast Technologies GmbH |
9 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
10 | | * Copyright 2016 Inuvika Inc. |
11 | | * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com> |
12 | | * Copyright 2017 Armin Novak <armin.novak@thincast.com> |
13 | | * Copyright 2017 Thincast Technologies GmbH |
14 | | * |
15 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
16 | | * you may not use this file except in compliance with the License. |
17 | | * You may obtain a copy of the License at |
18 | | * |
19 | | * http://www.apache.org/licenses/LICENSE-2.0 |
20 | | * |
21 | | * Unless required by applicable law or agreed to in writing, software |
22 | | * distributed under the License is distributed on an "AS IS" BASIS, |
23 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
24 | | * See the License for the specific language governing permissions and |
25 | | * limitations under the License. |
26 | | */ |
27 | | |
28 | | #include <freerdp/config.h> |
29 | | |
30 | | #include <errno.h> |
31 | | #include <stdio.h> |
32 | | #include <stdlib.h> |
33 | | #include <string.h> |
34 | | #include <time.h> |
35 | | |
36 | | #include <winpr/wtypes.h> |
37 | | #include <winpr/crt.h> |
38 | | #include <winpr/string.h> |
39 | | #include <winpr/path.h> |
40 | | #include <winpr/file.h> |
41 | | #include <winpr/stream.h> |
42 | | |
43 | | #include <freerdp/channels/rdpdr.h> |
44 | | |
45 | | #include "drive_file.h" |
46 | | |
47 | | #ifdef WITH_DEBUG_RDPDR |
48 | | #define DEBUG_WSTR(msg, wstr) \ |
49 | | do \ |
50 | | { \ |
51 | | char lpstr[1024] = WINPR_C_ARRAY_INIT; \ |
52 | | (void)ConvertWCharToUtf8(wstr, lpstr, ARRAYSIZE(lpstr)); \ |
53 | | WLog_DBG(TAG, msg, lpstr); \ |
54 | | } while (0) |
55 | | #else |
56 | | #define DEBUG_WSTR(msg, wstr) \ |
57 | 0 | do \ |
58 | 0 | { \ |
59 | 0 | } while (0) |
60 | | #endif |
61 | | |
62 | | static BOOL drive_file_fix_path(WCHAR* path, size_t length) |
63 | 0 | { |
64 | 0 | if ((length == 0) || (length > UINT32_MAX)) |
65 | 0 | return FALSE; |
66 | | |
67 | 0 | WINPR_ASSERT(path); |
68 | |
|
69 | 0 | for (size_t i = 0; i < length; i++) |
70 | 0 | { |
71 | 0 | if (path[i] == L'\\') |
72 | 0 | path[i] = L'/'; |
73 | 0 | } |
74 | |
|
75 | | #ifdef WIN32 |
76 | | |
77 | | if ((length == 3) && (path[1] == L':') && (path[2] == L'/')) |
78 | | return FALSE; |
79 | | |
80 | | #else |
81 | |
|
82 | 0 | if ((length == 1) && (path[0] == L'/')) |
83 | 0 | return FALSE; |
84 | | |
85 | 0 | #endif |
86 | | |
87 | 0 | if ((length > 0) && (path[length - 1] == L'/')) |
88 | 0 | path[length - 1] = L'\0'; |
89 | |
|
90 | 0 | return TRUE; |
91 | 0 | } |
92 | | |
93 | | static BOOL contains_dotdot(const WCHAR* path, size_t base_length, size_t path_length) |
94 | 0 | { |
95 | 0 | WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT; |
96 | 0 | const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer)); |
97 | 0 | const WCHAR* tst = path; |
98 | |
|
99 | 0 | if (path_length < 2) |
100 | 0 | return FALSE; |
101 | | |
102 | 0 | do |
103 | 0 | { |
104 | 0 | tst = _wcsstr(tst, dotdot); |
105 | 0 | if (!tst) |
106 | 0 | return FALSE; |
107 | | |
108 | | /* Filter .. sequences in file or directory names */ |
109 | 0 | if ((base_length == 0) || (*(tst - 1) == L'/') || (*(tst - 1) == L'\\')) |
110 | 0 | { |
111 | 0 | if (tst + 2 < path + path_length) |
112 | 0 | { |
113 | 0 | if ((tst[2] == '/') || (tst[2] == '\\') || (tst[2] == '\0')) |
114 | 0 | return TRUE; |
115 | 0 | } |
116 | 0 | else |
117 | 0 | return TRUE; |
118 | 0 | } |
119 | 0 | tst += 2; |
120 | 0 | } while (TRUE); |
121 | | |
122 | 0 | return FALSE; |
123 | 0 | } |
124 | | |
125 | | static WCHAR* drive_file_combine_fullpath(const WCHAR* base_path, const WCHAR* path, |
126 | | size_t PathWCharLength) |
127 | 0 | { |
128 | 0 | BOOL ok = FALSE; |
129 | 0 | WCHAR* fullpath = nullptr; |
130 | |
|
131 | 0 | if (!base_path || (!path && (PathWCharLength > 0))) |
132 | 0 | goto fail; |
133 | | |
134 | 0 | { |
135 | 0 | size_t base_path_length = _wcsnlen(base_path, MAX_PATH); |
136 | 0 | if (base_path_length < 1) |
137 | 0 | goto fail; |
138 | | |
139 | 0 | const size_t length = base_path_length + PathWCharLength + 2; |
140 | 0 | fullpath = (WCHAR*)calloc(length, sizeof(WCHAR)); |
141 | |
|
142 | 0 | if (!fullpath) |
143 | 0 | goto fail; |
144 | | |
145 | 0 | CopyMemory(fullpath, base_path, base_path_length * sizeof(WCHAR)); |
146 | |
|
147 | 0 | const WCHAR last = base_path[base_path_length - 1]; |
148 | 0 | const WCHAR sepu = PathGetSeparatorW(PATH_STYLE_UNIX); |
149 | 0 | const WCHAR sepw = PathGetSeparatorW(PATH_STYLE_WINDOWS); |
150 | 0 | if ((last != sepu) && (last != sepw)) |
151 | 0 | { |
152 | 0 | if (path && (PathWCharLength > 0) && (path[0] != sepu) && (path[0] != sepw)) |
153 | 0 | fullpath[base_path_length++] = sepu; |
154 | 0 | } |
155 | |
|
156 | 0 | if (path) |
157 | 0 | CopyMemory(&fullpath[base_path_length], path, PathWCharLength * sizeof(WCHAR)); |
158 | |
|
159 | 0 | if (!drive_file_fix_path(fullpath, length)) |
160 | 0 | goto fail; |
161 | | |
162 | | /* Ensure the path does not contain sequences like '..' */ |
163 | 0 | if (contains_dotdot(&fullpath[base_path_length], base_path_length, PathWCharLength)) |
164 | 0 | { |
165 | 0 | char* abuffer = ConvertWCharToUtf8Alloc(&fullpath[base_path_length], nullptr); |
166 | 0 | WLog_WARN(TAG, "[rdpdr] received invalid file path '%s' from server, aborting!", |
167 | 0 | abuffer); |
168 | 0 | free(abuffer); |
169 | 0 | goto fail; |
170 | 0 | } |
171 | 0 | } |
172 | | |
173 | 0 | ok = TRUE; |
174 | 0 | fail: |
175 | 0 | if (!ok) |
176 | 0 | { |
177 | 0 | free(fullpath); |
178 | 0 | fullpath = nullptr; |
179 | 0 | } |
180 | 0 | return fullpath; |
181 | 0 | } |
182 | | |
183 | | static BOOL drive_file_set_fullpath(DRIVE_FILE* file, const WCHAR* fullpath) |
184 | 0 | { |
185 | 0 | if (!file || !fullpath) |
186 | 0 | return FALSE; |
187 | | |
188 | 0 | const size_t len = _wcslen(fullpath); |
189 | 0 | free(file->fullpath); |
190 | 0 | file->fullpath = nullptr; |
191 | |
|
192 | 0 | if (len == 0) |
193 | 0 | return TRUE; |
194 | | |
195 | 0 | file->fullpath = _wcsdup(fullpath); |
196 | 0 | if (!file->fullpath) |
197 | 0 | return FALSE; |
198 | | |
199 | 0 | const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE), '\0' }; |
200 | 0 | WCHAR* filename = _wcsrchr(file->fullpath, *sep); |
201 | 0 | if (filename && _wcsncmp(filename, sep, ARRAYSIZE(sep)) == 0) |
202 | 0 | *filename = '\0'; |
203 | |
|
204 | 0 | return TRUE; |
205 | 0 | } |
206 | | |
207 | | static BOOL drive_file_init(DRIVE_FILE* file) |
208 | 0 | { |
209 | 0 | UINT CreateDisposition = 0; |
210 | 0 | DWORD dwAttr = GetFileAttributesW(file->fullpath); |
211 | |
|
212 | 0 | if (dwAttr != INVALID_FILE_ATTRIBUTES) |
213 | 0 | { |
214 | | /* The file exists */ |
215 | 0 | file->is_dir = (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0; |
216 | |
|
217 | 0 | if (file->is_dir) |
218 | 0 | { |
219 | 0 | if (file->CreateDisposition == FILE_CREATE) |
220 | 0 | { |
221 | 0 | SetLastError(ERROR_ALREADY_EXISTS); |
222 | 0 | return FALSE; |
223 | 0 | } |
224 | | |
225 | 0 | if (file->CreateOptions & FILE_NON_DIRECTORY_FILE) |
226 | 0 | { |
227 | 0 | SetLastError(ERROR_ACCESS_DENIED); |
228 | 0 | return FALSE; |
229 | 0 | } |
230 | | |
231 | 0 | return TRUE; |
232 | 0 | } |
233 | 0 | else |
234 | 0 | { |
235 | 0 | if (file->CreateOptions & FILE_DIRECTORY_FILE) |
236 | 0 | { |
237 | 0 | SetLastError(ERROR_DIRECTORY); |
238 | 0 | return FALSE; |
239 | 0 | } |
240 | 0 | } |
241 | 0 | } |
242 | 0 | else |
243 | 0 | { |
244 | 0 | file->is_dir = ((file->CreateOptions & FILE_DIRECTORY_FILE) != 0); |
245 | |
|
246 | 0 | if (file->is_dir) |
247 | 0 | { |
248 | | /* Should only create the directory if the disposition allows for it */ |
249 | 0 | if ((file->CreateDisposition == FILE_OPEN_IF) || |
250 | 0 | (file->CreateDisposition == FILE_CREATE)) |
251 | 0 | { |
252 | 0 | if (CreateDirectoryW(file->fullpath, nullptr) != 0) |
253 | 0 | { |
254 | 0 | return TRUE; |
255 | 0 | } |
256 | 0 | } |
257 | | |
258 | 0 | SetLastError(ERROR_FILE_NOT_FOUND); |
259 | 0 | return FALSE; |
260 | 0 | } |
261 | 0 | } |
262 | | |
263 | 0 | if (file->file_handle == INVALID_HANDLE_VALUE) |
264 | 0 | { |
265 | 0 | switch (file->CreateDisposition) |
266 | 0 | { |
267 | 0 | case FILE_SUPERSEDE: /* If the file already exists, replace it with the given file. If |
268 | | it does not, create the given file. */ |
269 | 0 | CreateDisposition = CREATE_ALWAYS; |
270 | 0 | break; |
271 | | |
272 | 0 | case FILE_OPEN: /* If the file already exists, open it instead of creating a new file. |
273 | | If it does not, fail the request and do not create a new file. */ |
274 | 0 | CreateDisposition = OPEN_EXISTING; |
275 | 0 | break; |
276 | | |
277 | 0 | case FILE_CREATE: /* If the file already exists, fail the request and do not create or |
278 | | open the given file. If it does not, create the given file. */ |
279 | 0 | CreateDisposition = CREATE_NEW; |
280 | 0 | break; |
281 | | |
282 | 0 | case FILE_OPEN_IF: /* If the file already exists, open it. If it does not, create the |
283 | | given file. */ |
284 | 0 | CreateDisposition = OPEN_ALWAYS; |
285 | 0 | break; |
286 | | |
287 | 0 | case FILE_OVERWRITE: /* If the file already exists, open it and overwrite it. If it does |
288 | | not, fail the request. */ |
289 | 0 | CreateDisposition = TRUNCATE_EXISTING; |
290 | 0 | break; |
291 | | |
292 | 0 | case FILE_OVERWRITE_IF: /* If the file already exists, open it and overwrite it. If it |
293 | | does not, create the given file. */ |
294 | 0 | CreateDisposition = CREATE_ALWAYS; |
295 | 0 | break; |
296 | | |
297 | 0 | default: |
298 | 0 | break; |
299 | 0 | } |
300 | | |
301 | 0 | #ifndef WIN32 |
302 | 0 | file->SharedAccess = 0; |
303 | 0 | #endif |
304 | 0 | file->file_handle = CreateFileW(file->fullpath, file->DesiredAccess, file->SharedAccess, |
305 | 0 | nullptr, CreateDisposition, file->FileAttributes, nullptr); |
306 | 0 | } |
307 | | |
308 | | #ifdef WIN32 |
309 | | if (file->file_handle == INVALID_HANDLE_VALUE) |
310 | | { |
311 | | /* Get the error message, if any. */ |
312 | | DWORD errorMessageID = GetLastError(); |
313 | | |
314 | | if (errorMessageID != 0) |
315 | | { |
316 | | LPSTR messageBuffer = nullptr; |
317 | | size_t size = |
318 | | FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
319 | | FORMAT_MESSAGE_IGNORE_INSERTS, |
320 | | nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
321 | | (LPSTR)&messageBuffer, 0, nullptr); |
322 | | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
323 | | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath)); |
324 | | WLog_ERR(TAG, "Error in drive_file_init: %s %s", messageBuffer, fullpath); |
325 | | /* Free the buffer. */ |
326 | | LocalFree(messageBuffer); |
327 | | /* restore original error code */ |
328 | | SetLastError(errorMessageID); |
329 | | } |
330 | | } |
331 | | #endif |
332 | | |
333 | 0 | return file->file_handle != INVALID_HANDLE_VALUE; |
334 | 0 | } |
335 | | |
336 | | DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathWCharLength, |
337 | | UINT32 id, UINT32 DesiredAccess, UINT32 CreateDisposition, |
338 | | UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess) |
339 | 0 | { |
340 | 0 | if (!base_path || (!path && (PathWCharLength > 0))) |
341 | 0 | return nullptr; |
342 | | |
343 | 0 | DRIVE_FILE* file = (DRIVE_FILE*)calloc(1, sizeof(DRIVE_FILE)); |
344 | |
|
345 | 0 | if (!file) |
346 | 0 | { |
347 | 0 | WLog_ERR(TAG, "calloc failed!"); |
348 | 0 | return nullptr; |
349 | 0 | } |
350 | | |
351 | 0 | file->file_handle = INVALID_HANDLE_VALUE; |
352 | 0 | file->find_handle = INVALID_HANDLE_VALUE; |
353 | 0 | file->id = id; |
354 | 0 | file->basepath = base_path; |
355 | 0 | file->FileAttributes = FileAttributes; |
356 | 0 | file->DesiredAccess = DesiredAccess; |
357 | 0 | file->CreateDisposition = CreateDisposition; |
358 | 0 | file->CreateOptions = CreateOptions; |
359 | 0 | file->SharedAccess = SharedAccess; |
360 | |
|
361 | 0 | WCHAR* p = drive_file_combine_fullpath(base_path, path, PathWCharLength); |
362 | 0 | (void)drive_file_set_fullpath(file, p); |
363 | 0 | free(p); |
364 | |
|
365 | 0 | if (!drive_file_init(file)) |
366 | 0 | { |
367 | 0 | DWORD lastError = GetLastError(); |
368 | 0 | drive_file_free(file); |
369 | 0 | SetLastError(lastError); |
370 | 0 | return nullptr; |
371 | 0 | } |
372 | | |
373 | 0 | return file; |
374 | 0 | } |
375 | | |
376 | | BOOL drive_file_free(DRIVE_FILE* file) |
377 | 0 | { |
378 | 0 | BOOL rc = FALSE; |
379 | |
|
380 | 0 | if (!file) |
381 | 0 | return FALSE; |
382 | | |
383 | 0 | if (file->file_handle != INVALID_HANDLE_VALUE) |
384 | 0 | { |
385 | 0 | (void)CloseHandle(file->file_handle); |
386 | 0 | file->file_handle = INVALID_HANDLE_VALUE; |
387 | 0 | } |
388 | |
|
389 | 0 | if (file->find_handle != INVALID_HANDLE_VALUE) |
390 | 0 | { |
391 | 0 | FindClose(file->find_handle); |
392 | 0 | file->find_handle = INVALID_HANDLE_VALUE; |
393 | 0 | } |
394 | |
|
395 | 0 | if (file->CreateOptions & FILE_DELETE_ON_CLOSE) |
396 | 0 | file->delete_pending = TRUE; |
397 | |
|
398 | 0 | if (file->delete_pending) |
399 | 0 | { |
400 | 0 | if (file->is_dir) |
401 | 0 | { |
402 | 0 | if (!winpr_RemoveDirectory_RecursiveW(file->fullpath)) |
403 | 0 | goto fail; |
404 | 0 | } |
405 | 0 | else if (!DeleteFileW(file->fullpath)) |
406 | 0 | goto fail; |
407 | 0 | } |
408 | | |
409 | 0 | rc = TRUE; |
410 | 0 | fail: |
411 | 0 | DEBUG_WSTR("Free %s", file->fullpath); |
412 | 0 | free(file->fullpath); |
413 | 0 | free(file); |
414 | 0 | return rc; |
415 | 0 | } |
416 | | |
417 | | BOOL drive_file_seek(DRIVE_FILE* file, UINT64 Offset) |
418 | 0 | { |
419 | 0 | LARGE_INTEGER loffset = WINPR_C_ARRAY_INIT; |
420 | |
|
421 | 0 | if (!file) |
422 | 0 | return FALSE; |
423 | | |
424 | 0 | if (Offset > INT64_MAX) |
425 | 0 | return FALSE; |
426 | | |
427 | 0 | loffset.QuadPart = (LONGLONG)Offset; |
428 | 0 | return SetFilePointerEx(file->file_handle, loffset, nullptr, FILE_BEGIN); |
429 | 0 | } |
430 | | |
431 | | BOOL drive_file_read(DRIVE_FILE* file, BYTE* buffer, UINT32* Length) |
432 | 0 | { |
433 | 0 | DWORD read = 0; |
434 | |
|
435 | 0 | if (!file || !buffer || !Length) |
436 | 0 | return FALSE; |
437 | | |
438 | 0 | DEBUG_WSTR("Read file %s", file->fullpath); |
439 | |
|
440 | 0 | if (ReadFile(file->file_handle, buffer, *Length, &read, nullptr)) |
441 | 0 | { |
442 | 0 | *Length = read; |
443 | 0 | return TRUE; |
444 | 0 | } |
445 | | |
446 | 0 | return FALSE; |
447 | 0 | } |
448 | | |
449 | | BOOL drive_file_write(DRIVE_FILE* file, const BYTE* buffer, UINT32 Length) |
450 | 0 | { |
451 | 0 | DWORD written = 0; |
452 | |
|
453 | 0 | if (!file || !buffer) |
454 | 0 | return FALSE; |
455 | | |
456 | 0 | DEBUG_WSTR("Write file %s", file->fullpath); |
457 | |
|
458 | 0 | while (Length > 0) |
459 | 0 | { |
460 | 0 | if (!WriteFile(file->file_handle, buffer, Length, &written, nullptr)) |
461 | 0 | return FALSE; |
462 | | |
463 | 0 | Length -= written; |
464 | 0 | buffer += written; |
465 | 0 | } |
466 | | |
467 | 0 | return TRUE; |
468 | 0 | } |
469 | | |
470 | | static BOOL drive_file_query_from_handle_information(const DRIVE_FILE* file, |
471 | | const BY_HANDLE_FILE_INFORMATION* info, |
472 | | UINT32 FsInformationClass, wStream* output) |
473 | 0 | { |
474 | 0 | switch (FsInformationClass) |
475 | 0 | { |
476 | 0 | case FileBasicInformation: |
477 | | |
478 | | /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */ |
479 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 36)) |
480 | 0 | return FALSE; |
481 | | |
482 | 0 | Stream_Write_UINT32(output, 36); /* Length */ |
483 | 0 | Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime); /* CreationTime */ |
484 | 0 | Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime); /* CreationTime */ |
485 | 0 | Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */ |
486 | 0 | Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */ |
487 | 0 | Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */ |
488 | 0 | Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */ |
489 | 0 | Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime); /* ChangeTime */ |
490 | 0 | Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime); /* ChangeTime */ |
491 | 0 | Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */ |
492 | | /* Reserved(4), MUST NOT be added! */ |
493 | 0 | break; |
494 | | |
495 | 0 | case FileStandardInformation: |
496 | | |
497 | | /* http://msdn.microsoft.com/en-us/library/cc232088.aspx */ |
498 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 22)) |
499 | 0 | return FALSE; |
500 | | |
501 | 0 | Stream_Write_UINT32(output, 22); /* Length */ |
502 | 0 | Stream_Write_UINT32(output, info->nFileSizeLow); /* AllocationSize */ |
503 | 0 | Stream_Write_UINT32(output, info->nFileSizeHigh); /* AllocationSize */ |
504 | 0 | Stream_Write_UINT32(output, info->nFileSizeLow); /* EndOfFile */ |
505 | 0 | Stream_Write_UINT32(output, info->nFileSizeHigh); /* EndOfFile */ |
506 | 0 | Stream_Write_UINT32(output, info->nNumberOfLinks); /* NumberOfLinks */ |
507 | 0 | Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */ |
508 | 0 | Stream_Write_UINT8(output, (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != |
509 | 0 | 0); /* Directory */ |
510 | | /* Reserved(2), MUST NOT be added! */ |
511 | 0 | break; |
512 | | |
513 | 0 | case FileAttributeTagInformation: |
514 | | |
515 | | /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */ |
516 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 8)) |
517 | 0 | return FALSE; |
518 | | |
519 | 0 | Stream_Write_UINT32(output, 8); /* Length */ |
520 | 0 | Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */ |
521 | 0 | Stream_Write_UINT32(output, 0); /* ReparseTag */ |
522 | 0 | break; |
523 | | |
524 | 0 | default: |
525 | | /* Unhandled FsInformationClass */ |
526 | 0 | WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]", |
527 | 0 | FSInformationClass2Tag(FsInformationClass), FsInformationClass); |
528 | 0 | return FALSE; |
529 | 0 | } |
530 | | |
531 | 0 | return TRUE; |
532 | 0 | } |
533 | | |
534 | | static BOOL drive_file_query_from_attributes(const DRIVE_FILE* file, |
535 | | const WIN32_FILE_ATTRIBUTE_DATA* attrib, |
536 | | UINT32 FsInformationClass, wStream* output) |
537 | 0 | { |
538 | 0 | switch (FsInformationClass) |
539 | 0 | { |
540 | 0 | case FileBasicInformation: |
541 | | |
542 | | /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */ |
543 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 36)) |
544 | 0 | return FALSE; |
545 | | |
546 | 0 | Stream_Write_UINT32(output, 36); /* Length */ |
547 | 0 | Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime); /* CreationTime */ |
548 | 0 | Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime); /* CreationTime */ |
549 | 0 | Stream_Write_UINT32(output, |
550 | 0 | attrib->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */ |
551 | 0 | Stream_Write_UINT32(output, |
552 | 0 | attrib->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */ |
553 | 0 | Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */ |
554 | 0 | Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */ |
555 | 0 | Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* ChangeTime */ |
556 | 0 | Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* ChangeTime */ |
557 | 0 | Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */ |
558 | | /* Reserved(4), MUST NOT be added! */ |
559 | 0 | break; |
560 | | |
561 | 0 | case FileStandardInformation: |
562 | | |
563 | | /* http://msdn.microsoft.com/en-us/library/cc232088.aspx */ |
564 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 22)) |
565 | 0 | return FALSE; |
566 | | |
567 | 0 | Stream_Write_UINT32(output, 22); /* Length */ |
568 | 0 | Stream_Write_UINT32(output, attrib->nFileSizeLow); /* AllocationSize */ |
569 | 0 | Stream_Write_UINT32(output, attrib->nFileSizeHigh); /* AllocationSize */ |
570 | 0 | Stream_Write_UINT32(output, attrib->nFileSizeLow); /* EndOfFile */ |
571 | 0 | Stream_Write_UINT32(output, attrib->nFileSizeHigh); /* EndOfFile */ |
572 | 0 | Stream_Write_UINT32(output, 0); /* NumberOfLinks */ |
573 | 0 | Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */ |
574 | 0 | Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != |
575 | 0 | 0); /* Directory */ |
576 | | /* Reserved(2), MUST NOT be added! */ |
577 | 0 | break; |
578 | | |
579 | 0 | case FileAttributeTagInformation: |
580 | | |
581 | | /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */ |
582 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 8)) |
583 | 0 | return FALSE; |
584 | | |
585 | 0 | Stream_Write_UINT32(output, 8); /* Length */ |
586 | 0 | Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */ |
587 | 0 | Stream_Write_UINT32(output, 0); /* ReparseTag */ |
588 | 0 | break; |
589 | | |
590 | 0 | default: |
591 | | /* Unhandled FsInformationClass */ |
592 | 0 | WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]", |
593 | 0 | FSInformationClass2Tag(FsInformationClass), FsInformationClass); |
594 | 0 | return FALSE; |
595 | 0 | } |
596 | | |
597 | 0 | return TRUE; |
598 | 0 | } |
599 | | |
600 | | BOOL drive_file_query_information(DRIVE_FILE* file, UINT32 FsInformationClass, wStream* output) |
601 | 0 | { |
602 | 0 | BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT; |
603 | 0 | BOOL status = 0; |
604 | |
|
605 | 0 | if (!file || !output) |
606 | 0 | return FALSE; |
607 | | |
608 | 0 | if ((file->file_handle != INVALID_HANDLE_VALUE) && |
609 | 0 | GetFileInformationByHandle(file->file_handle, &fileInformation)) |
610 | 0 | return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass, |
611 | 0 | output); |
612 | | |
613 | 0 | if (!file->is_dir) |
614 | 0 | { |
615 | 0 | HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, |
616 | 0 | FILE_ATTRIBUTE_NORMAL, nullptr); |
617 | 0 | if (hFile != INVALID_HANDLE_VALUE) |
618 | 0 | { |
619 | 0 | status = GetFileInformationByHandle(hFile, &fileInformation); |
620 | 0 | (void)CloseHandle(hFile); |
621 | 0 | if (!status) |
622 | 0 | goto out_fail; |
623 | | |
624 | 0 | if (!drive_file_query_from_handle_information(file, &fileInformation, |
625 | 0 | FsInformationClass, output)) |
626 | 0 | goto out_fail; |
627 | | |
628 | 0 | return TRUE; |
629 | 0 | } |
630 | 0 | } |
631 | | |
632 | | /* If we failed before (i.e. if information for a drive is queried) fall back to |
633 | | * GetFileAttributesExW */ |
634 | 0 | { |
635 | 0 | WIN32_FILE_ATTRIBUTE_DATA fileAttributes = WINPR_C_ARRAY_INIT; |
636 | 0 | if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes)) |
637 | 0 | goto out_fail; |
638 | | |
639 | 0 | if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output)) |
640 | 0 | goto out_fail; |
641 | 0 | } |
642 | | |
643 | 0 | return TRUE; |
644 | 0 | out_fail: |
645 | 0 | Stream_Write_UINT32(output, 0); /* Length */ |
646 | 0 | return FALSE; |
647 | 0 | } |
648 | | |
649 | | static BOOL drive_file_set_basic_information(DRIVE_FILE* file, UINT32 Length, wStream* input) |
650 | 0 | { |
651 | 0 | WINPR_ASSERT(file); |
652 | |
|
653 | 0 | const uint32_t expect = 36; |
654 | 0 | if (Length != expect) |
655 | 0 | { |
656 | 0 | WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect); |
657 | 0 | return FALSE; |
658 | 0 | } |
659 | | |
660 | | /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */ |
661 | 0 | const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) }; |
662 | 0 | const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) }; |
663 | 0 | const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) }; |
664 | 0 | const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) }; |
665 | 0 | const uint32_t FileAttributes = Stream_Get_UINT32(input); |
666 | |
|
667 | 0 | if (!PathFileExistsW(file->fullpath)) |
668 | 0 | return FALSE; |
669 | | |
670 | 0 | if (file->file_handle == INVALID_HANDLE_VALUE) |
671 | 0 | { |
672 | 0 | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
673 | 0 | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath) - 1); |
674 | |
|
675 | 0 | WLog_ERR(TAG, "Unable to set file time %s (%" PRIu32 ")", fullpath, GetLastError()); |
676 | 0 | return FALSE; |
677 | 0 | } |
678 | | |
679 | 0 | FILETIME ftCreationTime = WINPR_C_ARRAY_INIT; |
680 | 0 | FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT; |
681 | 0 | FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT; |
682 | 0 | FILETIME* pftCreationTime = nullptr; |
683 | 0 | FILETIME* pftLastAccessTime = nullptr; |
684 | 0 | FILETIME* pftLastWriteTime = nullptr; |
685 | 0 | if (liCreationTime.QuadPart != 0) |
686 | 0 | { |
687 | 0 | ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart; |
688 | 0 | ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart; |
689 | 0 | pftCreationTime = &ftCreationTime; |
690 | 0 | } |
691 | |
|
692 | 0 | if (liLastAccessTime.QuadPart != 0) |
693 | 0 | { |
694 | 0 | ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart; |
695 | 0 | ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart; |
696 | 0 | pftLastAccessTime = &ftLastAccessTime; |
697 | 0 | } |
698 | |
|
699 | 0 | if (liLastWriteTime.QuadPart != 0) |
700 | 0 | { |
701 | 0 | ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart; |
702 | 0 | ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart; |
703 | 0 | pftLastWriteTime = &ftLastWriteTime; |
704 | 0 | } |
705 | |
|
706 | 0 | if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart) |
707 | 0 | { |
708 | 0 | ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart; |
709 | 0 | ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart; |
710 | 0 | pftLastWriteTime = &ftLastWriteTime; |
711 | 0 | } |
712 | |
|
713 | 0 | DEBUG_WSTR("SetFileTime %s", file->fullpath); |
714 | |
|
715 | 0 | if (!SetFileAttributesW(file->fullpath, FileAttributes)) |
716 | 0 | { |
717 | 0 | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
718 | 0 | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath)); |
719 | 0 | WLog_ERR(TAG, "Unable to set file attributes for %s", fullpath); |
720 | 0 | return FALSE; |
721 | 0 | } |
722 | | |
723 | 0 | if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime)) |
724 | 0 | { |
725 | 0 | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
726 | 0 | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath)); |
727 | 0 | WLog_ERR(TAG, "Unable to set file time for %s", fullpath); |
728 | 0 | return FALSE; |
729 | 0 | } |
730 | 0 | return TRUE; |
731 | 0 | } |
732 | | |
733 | | static BOOL drive_file_set_alloc_information(DRIVE_FILE* file, UINT32 Length, wStream* input) |
734 | 0 | { |
735 | 0 | WINPR_ASSERT(file); |
736 | 0 | const uint32_t expect = 8; |
737 | 0 | if (Length != expect) |
738 | 0 | { |
739 | 0 | WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect); |
740 | 0 | return FALSE; |
741 | 0 | } |
742 | | |
743 | | /* http://msdn.microsoft.com/en-us/library/cc232076.aspx */ |
744 | 0 | const int64_t size = Stream_Get_INT64(input); |
745 | |
|
746 | 0 | if (file->file_handle == INVALID_HANDLE_VALUE) |
747 | 0 | { |
748 | 0 | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
749 | 0 | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath)); |
750 | 0 | WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size, |
751 | 0 | GetLastError()); |
752 | 0 | return FALSE; |
753 | 0 | } |
754 | | |
755 | 0 | LARGE_INTEGER liSize = { .QuadPart = size }; |
756 | |
|
757 | 0 | if (!SetFilePointerEx(file->file_handle, liSize, nullptr, FILE_BEGIN)) |
758 | 0 | { |
759 | 0 | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
760 | 0 | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath)); |
761 | 0 | WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size, |
762 | 0 | GetLastError()); |
763 | 0 | return FALSE; |
764 | 0 | } |
765 | | |
766 | 0 | DEBUG_WSTR("Truncate %s", file->fullpath); |
767 | |
|
768 | 0 | if (SetEndOfFile(file->file_handle) == 0) |
769 | 0 | { |
770 | 0 | char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT; |
771 | 0 | (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath)); |
772 | 0 | WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size, |
773 | 0 | GetLastError()); |
774 | 0 | return FALSE; |
775 | 0 | } |
776 | | |
777 | 0 | return TRUE; |
778 | 0 | } |
779 | | |
780 | | static BOOL drive_file_set_disposition_information(DRIVE_FILE* file, UINT32 Length, wStream* input) |
781 | 0 | { |
782 | 0 | WINPR_ASSERT(file); |
783 | 0 | uint8_t delete_pending = 0; |
784 | | /* http://msdn.microsoft.com/en-us/library/cc232098.aspx */ |
785 | | /* http://msdn.microsoft.com/en-us/library/cc241371.aspx */ |
786 | 0 | if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath)) |
787 | 0 | { |
788 | 0 | SetLastError(ERROR_DIR_NOT_EMPTY); |
789 | 0 | return FALSE; |
790 | 0 | } |
791 | | |
792 | 0 | if (Length) |
793 | 0 | { |
794 | 0 | const uint32_t expect = 1; |
795 | 0 | if (Length != expect) |
796 | 0 | WLog_DBG(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect); |
797 | |
|
798 | 0 | delete_pending = Stream_Get_UINT8(input); |
799 | 0 | } |
800 | 0 | else |
801 | 0 | delete_pending = 1; |
802 | |
|
803 | 0 | if (delete_pending) |
804 | 0 | { |
805 | 0 | DEBUG_WSTR("SetDeletePending %s", file->fullpath); |
806 | 0 | const uint32_t attr = GetFileAttributesW(file->fullpath); |
807 | |
|
808 | 0 | if (attr & FILE_ATTRIBUTE_READONLY) |
809 | 0 | { |
810 | 0 | SetLastError(ERROR_ACCESS_DENIED); |
811 | 0 | return FALSE; |
812 | 0 | } |
813 | 0 | } |
814 | | |
815 | 0 | file->delete_pending = delete_pending; |
816 | 0 | return TRUE; |
817 | 0 | } |
818 | | |
819 | | static BOOL drive_file_set_rename_information(DRIVE_FILE* file, UINT32 Length, wStream* input) |
820 | 0 | { |
821 | 0 | WINPR_ASSERT(file); |
822 | |
|
823 | 0 | const uint32_t expect = 6; |
824 | 0 | if (Length < expect) |
825 | 0 | { |
826 | 0 | WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected at least %" PRIu32, Length, expect); |
827 | 0 | return FALSE; |
828 | 0 | } |
829 | | |
830 | | /* http://msdn.microsoft.com/en-us/library/cc232085.aspx */ |
831 | 0 | const uint8_t ReplaceIfExists = Stream_Get_UINT8(input); |
832 | 0 | Stream_Seek_UINT8(input); /* RootDirectory */ |
833 | 0 | const uint64_t FileNameLength = Stream_Get_UINT32(input); |
834 | |
|
835 | 0 | if (Length != expect + FileNameLength) |
836 | 0 | { |
837 | 0 | WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu64, Length, |
838 | 0 | expect + FileNameLength); |
839 | 0 | return FALSE; |
840 | 0 | } |
841 | | |
842 | 0 | WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input), |
843 | 0 | FileNameLength / sizeof(WCHAR)); |
844 | |
|
845 | 0 | if (!fullpath) |
846 | 0 | return FALSE; |
847 | | |
848 | | #ifdef _WIN32 |
849 | | |
850 | | if (file->file_handle != INVALID_HANDLE_VALUE) |
851 | | { |
852 | | (void)CloseHandle(file->file_handle); |
853 | | file->file_handle = INVALID_HANDLE_VALUE; |
854 | | } |
855 | | |
856 | | #endif |
857 | 0 | DEBUG_WSTR("MoveFileExW %s", file->fullpath); |
858 | |
|
859 | 0 | if (MoveFileExW(file->fullpath, fullpath, |
860 | 0 | MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0))) |
861 | 0 | { |
862 | 0 | const BOOL rc = drive_file_set_fullpath(file, fullpath); |
863 | 0 | free(fullpath); |
864 | 0 | if (!rc) |
865 | 0 | return FALSE; |
866 | 0 | } |
867 | 0 | else |
868 | 0 | { |
869 | 0 | free(fullpath); |
870 | 0 | return FALSE; |
871 | 0 | } |
872 | | |
873 | | #ifdef _WIN32 |
874 | | drive_file_init(file); |
875 | | #endif |
876 | 0 | return TRUE; |
877 | 0 | } |
878 | | |
879 | | BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length, |
880 | | wStream* input) |
881 | 0 | { |
882 | 0 | if (!file || !input) |
883 | 0 | return FALSE; |
884 | | |
885 | 0 | if (!Stream_CheckAndLogRequiredLength(TAG, input, Length)) |
886 | 0 | return FALSE; |
887 | | |
888 | 0 | switch (FsInformationClass) |
889 | 0 | { |
890 | 0 | case FileBasicInformation: |
891 | 0 | return drive_file_set_basic_information(file, Length, input); |
892 | | |
893 | 0 | case FileEndOfFileInformation: |
894 | | /* http://msdn.microsoft.com/en-us/library/cc232067.aspx */ |
895 | 0 | case FileAllocationInformation: |
896 | 0 | return drive_file_set_alloc_information(file, Length, input); |
897 | | |
898 | 0 | case FileDispositionInformation: |
899 | 0 | return drive_file_set_disposition_information(file, Length, input); |
900 | | |
901 | 0 | case FileRenameInformation: |
902 | 0 | return drive_file_set_rename_information(file, Length, input); |
903 | | |
904 | 0 | default: |
905 | 0 | WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]", |
906 | 0 | FSInformationClass2Tag(FsInformationClass), FsInformationClass); |
907 | 0 | return FALSE; |
908 | 0 | } |
909 | | |
910 | 0 | return TRUE; |
911 | 0 | } |
912 | | |
913 | | static BOOL drive_file_query_dir_info(DRIVE_FILE* file, wStream* output, size_t length) |
914 | 0 | { |
915 | 0 | WINPR_ASSERT(file); |
916 | 0 | WINPR_ASSERT(output); |
917 | | |
918 | | /* http://msdn.microsoft.com/en-us/library/cc232097.aspx */ |
919 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length)) |
920 | 0 | return FALSE; |
921 | | |
922 | 0 | if (length > UINT32_MAX - 64) |
923 | 0 | return FALSE; |
924 | | |
925 | 0 | Stream_Write_UINT32(output, (UINT32)(64 + length)); /* Length */ |
926 | 0 | Stream_Write_UINT32(output, 0); /* NextEntryOffset */ |
927 | 0 | Stream_Write_UINT32(output, 0); /* FileIndex */ |
928 | 0 | Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */ |
929 | 0 | Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */ |
930 | 0 | Stream_Write_UINT32(output, |
931 | 0 | file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */ |
932 | 0 | Stream_Write_UINT32(output, |
933 | 0 | file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */ |
934 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */ |
935 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */ |
936 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */ |
937 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */ |
938 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */ |
939 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */ |
940 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */ |
941 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */ |
942 | 0 | Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */ |
943 | 0 | Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */ |
944 | 0 | Stream_Write(output, file->find_data.cFileName, length); |
945 | 0 | return TRUE; |
946 | 0 | } |
947 | | |
948 | | static BOOL drive_file_query_full_dir_info(DRIVE_FILE* file, wStream* output, size_t length) |
949 | 0 | { |
950 | 0 | WINPR_ASSERT(file); |
951 | 0 | WINPR_ASSERT(output); |
952 | | /* http://msdn.microsoft.com/en-us/library/cc232068.aspx */ |
953 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length)) |
954 | 0 | return FALSE; |
955 | | |
956 | 0 | if (length > UINT32_MAX - 68) |
957 | 0 | return FALSE; |
958 | | |
959 | 0 | Stream_Write_UINT32(output, (UINT32)(68 + length)); /* Length */ |
960 | 0 | Stream_Write_UINT32(output, 0); /* NextEntryOffset */ |
961 | 0 | Stream_Write_UINT32(output, 0); /* FileIndex */ |
962 | 0 | Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */ |
963 | 0 | Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */ |
964 | 0 | Stream_Write_UINT32(output, |
965 | 0 | file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */ |
966 | 0 | Stream_Write_UINT32(output, |
967 | 0 | file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */ |
968 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */ |
969 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */ |
970 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */ |
971 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */ |
972 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */ |
973 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */ |
974 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */ |
975 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */ |
976 | 0 | Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */ |
977 | 0 | Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */ |
978 | 0 | Stream_Write_UINT32(output, 0); /* EaSize */ |
979 | 0 | Stream_Write(output, file->find_data.cFileName, length); |
980 | 0 | return TRUE; |
981 | 0 | } |
982 | | |
983 | | static BOOL drive_file_query_both_dir_info(DRIVE_FILE* file, wStream* output, size_t length) |
984 | 0 | { |
985 | 0 | WINPR_ASSERT(file); |
986 | 0 | WINPR_ASSERT(output); |
987 | | /* http://msdn.microsoft.com/en-us/library/cc232095.aspx */ |
988 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length)) |
989 | 0 | return FALSE; |
990 | | |
991 | 0 | if (length > UINT32_MAX - 93) |
992 | 0 | return FALSE; |
993 | | |
994 | 0 | Stream_Write_UINT32(output, (UINT32)(93 + length)); /* Length */ |
995 | 0 | Stream_Write_UINT32(output, 0); /* NextEntryOffset */ |
996 | 0 | Stream_Write_UINT32(output, 0); /* FileIndex */ |
997 | 0 | Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */ |
998 | 0 | Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */ |
999 | 0 | Stream_Write_UINT32(output, |
1000 | 0 | file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */ |
1001 | 0 | Stream_Write_UINT32(output, |
1002 | 0 | file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */ |
1003 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */ |
1004 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */ |
1005 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */ |
1006 | 0 | Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */ |
1007 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */ |
1008 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */ |
1009 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */ |
1010 | 0 | Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */ |
1011 | 0 | Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */ |
1012 | 0 | Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */ |
1013 | 0 | Stream_Write_UINT32(output, 0); /* EaSize */ |
1014 | 0 | Stream_Write_UINT8(output, 0); /* ShortNameLength */ |
1015 | | /* Reserved(1), MUST NOT be added! */ |
1016 | 0 | Stream_Zero(output, 24); /* ShortName */ |
1017 | 0 | Stream_Write(output, file->find_data.cFileName, length); |
1018 | 0 | return TRUE; |
1019 | 0 | } |
1020 | | |
1021 | | static BOOL drive_file_query_names_info(DRIVE_FILE* file, wStream* output, size_t length) |
1022 | 0 | { |
1023 | 0 | WINPR_ASSERT(file); |
1024 | 0 | WINPR_ASSERT(output); |
1025 | | /* http://msdn.microsoft.com/en-us/library/cc232077.aspx */ |
1026 | 0 | if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length)) |
1027 | 0 | return FALSE; |
1028 | | |
1029 | 0 | if (length > UINT32_MAX - 12) |
1030 | 0 | return FALSE; |
1031 | | |
1032 | 0 | Stream_Write_UINT32(output, (UINT32)(12 + length)); /* Length */ |
1033 | 0 | Stream_Write_UINT32(output, 0); /* NextEntryOffset */ |
1034 | 0 | Stream_Write_UINT32(output, 0); /* FileIndex */ |
1035 | 0 | Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */ |
1036 | 0 | Stream_Write(output, file->find_data.cFileName, length); |
1037 | 0 | return TRUE; |
1038 | 0 | } |
1039 | | |
1040 | | BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery, |
1041 | | const WCHAR* path, UINT32 PathWCharLength, wStream* output) |
1042 | 0 | { |
1043 | 0 | BOOL rc = FALSE; |
1044 | 0 | size_t length = 0; |
1045 | 0 | WCHAR* ent_path = nullptr; |
1046 | |
|
1047 | 0 | if (!file || !path || !output) |
1048 | 0 | return FALSE; |
1049 | | |
1050 | 0 | if (InitialQuery != 0) |
1051 | 0 | { |
1052 | | /* release search handle */ |
1053 | 0 | if (file->find_handle != INVALID_HANDLE_VALUE) |
1054 | 0 | FindClose(file->find_handle); |
1055 | |
|
1056 | 0 | ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength); |
1057 | | /* open new search handle and retrieve the first entry */ |
1058 | 0 | file->find_handle = FindFirstFileW(ent_path, &file->find_data); |
1059 | 0 | free(ent_path); |
1060 | |
|
1061 | 0 | if (file->find_handle == INVALID_HANDLE_VALUE) |
1062 | 0 | goto out_fail; |
1063 | 0 | } |
1064 | 0 | else if (!FindNextFileW(file->find_handle, &file->find_data)) |
1065 | 0 | goto out_fail; |
1066 | | |
1067 | 0 | length = _wcslen(file->find_data.cFileName) * sizeof(WCHAR); |
1068 | |
|
1069 | 0 | switch (FsInformationClass) |
1070 | 0 | { |
1071 | 0 | case FileDirectoryInformation: |
1072 | 0 | rc = drive_file_query_dir_info(file, output, length); |
1073 | 0 | break; |
1074 | | |
1075 | 0 | case FileFullDirectoryInformation: |
1076 | 0 | rc = drive_file_query_full_dir_info(file, output, length); |
1077 | 0 | break; |
1078 | | |
1079 | 0 | case FileBothDirectoryInformation: |
1080 | 0 | rc = drive_file_query_both_dir_info(file, output, length); |
1081 | 0 | break; |
1082 | | |
1083 | 0 | case FileNamesInformation: |
1084 | 0 | rc = drive_file_query_names_info(file, output, length); |
1085 | 0 | break; |
1086 | | |
1087 | 0 | default: |
1088 | 0 | WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]", |
1089 | 0 | FSInformationClass2Tag(FsInformationClass), FsInformationClass); |
1090 | | /* Unhandled FsInformationClass */ |
1091 | 0 | goto out_fail; |
1092 | 0 | } |
1093 | | |
1094 | 0 | out_fail: |
1095 | 0 | if (!rc) |
1096 | 0 | { |
1097 | 0 | Stream_Write_UINT32(output, 0); /* Length */ |
1098 | 0 | Stream_Write_UINT8(output, 0); /* Padding */ |
1099 | 0 | } |
1100 | 0 | return rc; |
1101 | 0 | } |