28#include <freerdp/config.h>
36#include <winpr/wtypes.h>
38#include <winpr/string.h>
39#include <winpr/path.h>
40#include <winpr/file.h>
41#include <winpr/stream.h>
43#include <freerdp/channels/rdpdr.h>
45#include "drive_file.h"
47#ifdef WITH_DEBUG_RDPDR
48#define DEBUG_WSTR(msg, wstr) \
51 char lpstr[1024] = { 0 }; \
52 (void)ConvertWCharToUtf8(wstr, lpstr, ARRAYSIZE(lpstr)); \
53 WLog_DBG(TAG, msg, lpstr); \
56#define DEBUG_WSTR(msg, wstr) \
62static BOOL drive_file_fix_path(WCHAR* path,
size_t length)
64 if ((length == 0) || (length > UINT32_MAX))
69 for (
size_t i = 0; i < length; i++)
77 if ((length == 3) && (path[1] == L
':') && (path[2] == L
'/'))
82 if ((length == 1) && (path[0] == L
'/'))
87 if ((length > 0) && (path[length - 1] == L
'/'))
88 path[length - 1] = L
'\0';
93static BOOL contains_dotdot(
const WCHAR* path,
size_t base_length,
size_t path_length)
95 WCHAR dotdotbuffer[6] = { 0 };
96 const WCHAR* dotdot = InitializeConstWCharFromUtf8(
"..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
97 const WCHAR* tst = path;
104 tst = _wcsstr(tst, dotdot);
109 if ((base_length == 0) || (*(tst - 1) == L
'/') || (*(tst - 1) == L
'\\'))
111 if (tst + 2 < path + path_length)
113 if ((tst[2] ==
'/') || (tst[2] ==
'\\'))
123static WCHAR* drive_file_combine_fullpath(
const WCHAR* base_path,
const WCHAR* path,
124 size_t PathWCharLength)
127 WCHAR* fullpath = NULL;
129 if (!base_path || (!path && (PathWCharLength > 0)))
133 const size_t base_path_length = _wcsnlen(base_path, MAX_PATH);
134 const size_t length = base_path_length + PathWCharLength + 1;
135 fullpath = (WCHAR*)calloc(length,
sizeof(WCHAR));
140 CopyMemory(fullpath, base_path, base_path_length *
sizeof(WCHAR));
142 CopyMemory(&fullpath[base_path_length], path, PathWCharLength *
sizeof(WCHAR));
144 if (!drive_file_fix_path(fullpath, length))
148 if (contains_dotdot(&fullpath[base_path_length], base_path_length, PathWCharLength))
150 char abuffer[MAX_PATH] = { 0 };
151 (void)ConvertWCharToUtf8(&fullpath[base_path_length], abuffer, ARRAYSIZE(abuffer));
153 WLog_WARN(TAG,
"[rdpdr] received invalid file path '%s' from server, aborting!",
154 &abuffer[base_path_length]);
169static BOOL drive_file_set_fullpath(
DRIVE_FILE* file,
const WCHAR* fullpath)
171 if (!file || !fullpath)
174 const size_t len = _wcslen(fullpath);
175 free(file->fullpath);
176 file->fullpath = NULL;
181 file->fullpath = _wcsdup(fullpath);
185 const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE),
'\0' };
186 WCHAR* filename = _wcsrchr(file->fullpath, *sep);
187 if (filename && _wcsncmp(filename, sep, ARRAYSIZE(sep)) == 0)
195 UINT CreateDisposition = 0;
196 DWORD dwAttr = GetFileAttributesW(file->fullpath);
198 if (dwAttr != INVALID_FILE_ATTRIBUTES)
201 file->is_dir = (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0;
205 if (file->CreateDisposition == FILE_CREATE)
207 SetLastError(ERROR_ALREADY_EXISTS);
211 if (file->CreateOptions & FILE_NON_DIRECTORY_FILE)
213 SetLastError(ERROR_ACCESS_DENIED);
221 if (file->CreateOptions & FILE_DIRECTORY_FILE)
223 SetLastError(ERROR_DIRECTORY);
230 file->is_dir = ((file->CreateOptions & FILE_DIRECTORY_FILE) ? TRUE : FALSE);
235 if ((file->CreateDisposition == FILE_OPEN_IF) ||
236 (file->CreateDisposition == FILE_CREATE))
238 if (CreateDirectoryW(file->fullpath, NULL) != 0)
244 SetLastError(ERROR_FILE_NOT_FOUND);
249 if (file->file_handle == INVALID_HANDLE_VALUE)
251 switch (file->CreateDisposition)
255 CreateDisposition = CREATE_ALWAYS;
260 CreateDisposition = OPEN_EXISTING;
265 CreateDisposition = CREATE_NEW;
270 CreateDisposition = OPEN_ALWAYS;
275 CreateDisposition = TRUNCATE_EXISTING;
278 case FILE_OVERWRITE_IF:
280 CreateDisposition = CREATE_ALWAYS;
288 file->SharedAccess = 0;
290 file->file_handle = CreateFileW(file->fullpath, file->DesiredAccess, file->SharedAccess,
291 NULL, CreateDisposition, file->FileAttributes, NULL);
295 if (file->file_handle == INVALID_HANDLE_VALUE)
298 DWORD errorMessageID = GetLastError();
300 if (errorMessageID != 0)
302 LPSTR messageBuffer = NULL;
304 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
305 FORMAT_MESSAGE_IGNORE_INSERTS,
306 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
307 (LPSTR)&messageBuffer, 0, NULL);
308 char fullpath[MAX_PATH] = { 0 };
309 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
310 WLog_ERR(TAG,
"Error in drive_file_init: %s %s", messageBuffer, fullpath);
312 LocalFree(messageBuffer);
314 SetLastError(errorMessageID);
319 return file->file_handle != INVALID_HANDLE_VALUE;
322DRIVE_FILE* drive_file_new(
const WCHAR* base_path,
const WCHAR* path, UINT32 PathWCharLength,
323 UINT32
id, UINT32 DesiredAccess, UINT32 CreateDisposition,
324 UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess)
326 if (!base_path || (!path && (PathWCharLength > 0)))
333 WLog_ERR(TAG,
"calloc failed!");
337 file->file_handle = INVALID_HANDLE_VALUE;
338 file->find_handle = INVALID_HANDLE_VALUE;
340 file->basepath = base_path;
341 file->FileAttributes = FileAttributes;
342 file->DesiredAccess = DesiredAccess;
343 file->CreateDisposition = CreateDisposition;
344 file->CreateOptions = CreateOptions;
345 file->SharedAccess = SharedAccess;
347 WCHAR* p = drive_file_combine_fullpath(base_path, path, PathWCharLength);
348 (void)drive_file_set_fullpath(file, p);
351 if (!drive_file_init(file))
353 DWORD lastError = GetLastError();
354 drive_file_free(file);
355 SetLastError(lastError);
369 if (file->file_handle != INVALID_HANDLE_VALUE)
371 (void)CloseHandle(file->file_handle);
372 file->file_handle = INVALID_HANDLE_VALUE;
375 if (file->find_handle != INVALID_HANDLE_VALUE)
377 FindClose(file->find_handle);
378 file->find_handle = INVALID_HANDLE_VALUE;
381 if (file->CreateOptions & FILE_DELETE_ON_CLOSE)
382 file->delete_pending = TRUE;
384 if (file->delete_pending)
388 if (!winpr_RemoveDirectory_RecursiveW(file->fullpath))
391 else if (!DeleteFileW(file->fullpath))
397 DEBUG_WSTR(
"Free %s", file->fullpath);
398 free(file->fullpath);
403BOOL drive_file_seek(
DRIVE_FILE* file, UINT64 Offset)
410 if (Offset > INT64_MAX)
413 loffset.QuadPart = (LONGLONG)Offset;
414 return SetFilePointerEx(file->file_handle, loffset, NULL, FILE_BEGIN);
417BOOL drive_file_read(
DRIVE_FILE* file, BYTE* buffer, UINT32* Length)
421 if (!file || !buffer || !Length)
424 DEBUG_WSTR(
"Read file %s", file->fullpath);
426 if (ReadFile(file->file_handle, buffer, *Length, &read, NULL))
435BOOL drive_file_write(
DRIVE_FILE* file,
const BYTE* buffer, UINT32 Length)
439 if (!file || !buffer)
442 DEBUG_WSTR(
"Write file %s", file->fullpath);
446 if (!WriteFile(file->file_handle, buffer, Length, &written, NULL))
456static BOOL drive_file_query_from_handle_information(
const DRIVE_FILE* file,
458 UINT32 FsInformationClass,
wStream* output)
460 switch (FsInformationClass)
462 case FileBasicInformation:
465 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
468 Stream_Write_UINT32(output, 36);
469 Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime);
470 Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime);
471 Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime);
472 Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime);
473 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
474 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
475 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
476 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
477 Stream_Write_UINT32(output, info->dwFileAttributes);
481 case FileStandardInformation:
484 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
487 Stream_Write_UINT32(output, 22);
488 Stream_Write_UINT32(output, info->nFileSizeLow);
489 Stream_Write_UINT32(output, info->nFileSizeHigh);
490 Stream_Write_UINT32(output, info->nFileSizeLow);
491 Stream_Write_UINT32(output, info->nFileSizeHigh);
492 Stream_Write_UINT32(output, info->nNumberOfLinks);
493 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
494 Stream_Write_UINT8(output, info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
500 case FileAttributeTagInformation:
503 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
506 Stream_Write_UINT32(output, 8);
507 Stream_Write_UINT32(output, info->dwFileAttributes);
508 Stream_Write_UINT32(output, 0);
513 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
514 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
521static BOOL drive_file_query_from_attributes(
const DRIVE_FILE* file,
523 UINT32 FsInformationClass,
wStream* output)
525 switch (FsInformationClass)
527 case FileBasicInformation:
530 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
533 Stream_Write_UINT32(output, 36);
534 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime);
535 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime);
536 Stream_Write_UINT32(output,
537 attrib->ftLastAccessTime.dwLowDateTime);
538 Stream_Write_UINT32(output,
539 attrib->ftLastAccessTime.dwHighDateTime);
540 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
541 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
542 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
543 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
544 Stream_Write_UINT32(output, attrib->dwFileAttributes);
548 case FileStandardInformation:
551 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
554 Stream_Write_UINT32(output, 22);
555 Stream_Write_UINT32(output, attrib->nFileSizeLow);
556 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
557 Stream_Write_UINT32(output, attrib->nFileSizeLow);
558 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
559 Stream_Write_UINT32(output, 0);
560 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
561 Stream_Write_UINT8(output, attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
567 case FileAttributeTagInformation:
570 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
573 Stream_Write_UINT32(output, 8);
574 Stream_Write_UINT32(output, attrib->dwFileAttributes);
575 Stream_Write_UINT32(output, 0);
580 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
581 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
588BOOL drive_file_query_information(
DRIVE_FILE* file, UINT32 FsInformationClass,
wStream* output)
593 if (!file || !output)
596 if ((file->file_handle != INVALID_HANDLE_VALUE) &&
597 GetFileInformationByHandle(file->file_handle, &fileInformation))
598 return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
603 HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
604 FILE_ATTRIBUTE_NORMAL, NULL);
605 if (hFile != INVALID_HANDLE_VALUE)
607 status = GetFileInformationByHandle(hFile, &fileInformation);
608 (void)CloseHandle(hFile);
612 if (!drive_file_query_from_handle_information(file, &fileInformation,
613 FsInformationClass, output))
624 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
627 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
633 Stream_Write_UINT32(output, 0);
637static BOOL drive_file_set_basic_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
641 const uint32_t expect = 36;
642 if (Length != expect)
644 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
649 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
650 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
651 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
652 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
653 const uint32_t FileAttributes = Stream_Get_UINT32(input);
655 if (!PathFileExistsW(file->fullpath))
658 if (file->file_handle == INVALID_HANDLE_VALUE)
660 char fullpath[MAX_PATH] = { 0 };
661 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath) - 1);
663 WLog_ERR(TAG,
"Unable to set file time %s (%" PRId32
")", fullpath, GetLastError());
673 if (liCreationTime.QuadPart != 0)
675 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
676 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
677 pftCreationTime = &ftCreationTime;
680 if (liLastAccessTime.QuadPart != 0)
682 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
683 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
684 pftLastAccessTime = &ftLastAccessTime;
687 if (liLastWriteTime.QuadPart != 0)
689 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
690 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
691 pftLastWriteTime = &ftLastWriteTime;
694 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
696 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
697 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
698 pftLastWriteTime = &ftLastWriteTime;
701 DEBUG_WSTR(
"SetFileTime %s", file->fullpath);
703 if (!SetFileAttributesW(file->fullpath, FileAttributes))
705 char fullpath[MAX_PATH] = { 0 };
706 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
707 WLog_ERR(TAG,
"Unable to set file attributes for %s", fullpath);
711 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
713 char fullpath[MAX_PATH] = { 0 };
714 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
715 WLog_ERR(TAG,
"Unable to set file time for %s", fullpath);
721static BOOL drive_file_set_alloc_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
724 const uint32_t expect = 8;
725 if (Length != expect)
727 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
732 const int64_t size = Stream_Get_INT64(input);
734 if (file->file_handle == INVALID_HANDLE_VALUE)
736 char fullpath[MAX_PATH] = { 0 };
737 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
738 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRId32
")", fullpath, size,
745 if (!SetFilePointerEx(file->file_handle, liSize, NULL, FILE_BEGIN))
747 char fullpath[MAX_PATH] = { 0 };
748 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
749 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRId32
")", fullpath, size,
754 DEBUG_WSTR(
"Truncate %s", file->fullpath);
756 if (SetEndOfFile(file->file_handle) == 0)
758 char fullpath[MAX_PATH] = { 0 };
759 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
760 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRId32
")", fullpath, size,
768static BOOL drive_file_set_disposition_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
771 uint8_t delete_pending = 0;
774 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
776 SetLastError(ERROR_DIR_NOT_EMPTY);
782 const uint32_t expect = 1;
783 if (Length != expect)
784 WLog_DBG(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
786 delete_pending = Stream_Get_UINT8(input);
793 DEBUG_WSTR(
"SetDeletePending %s", file->fullpath);
794 const uint32_t attr = GetFileAttributesW(file->fullpath);
796 if (attr & FILE_ATTRIBUTE_READONLY)
798 SetLastError(ERROR_ACCESS_DENIED);
803 file->delete_pending = delete_pending;
807static BOOL drive_file_set_rename_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
811 const uint32_t expect = 6;
814 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected at least %" PRIu32, Length, expect);
819 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
820 Stream_Seek_UINT8(input);
821 const uint32_t FileNameLength = Stream_Get_UINT32(input);
823 if (Length != expect + FileNameLength)
825 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length,
826 expect + FileNameLength);
830 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
831 FileNameLength /
sizeof(WCHAR));
838 if (file->file_handle != INVALID_HANDLE_VALUE)
840 (void)CloseHandle(file->file_handle);
841 file->file_handle = INVALID_HANDLE_VALUE;
845 DEBUG_WSTR(
"MoveFileExW %s", file->fullpath);
847 if (MoveFileExW(file->fullpath, fullpath,
848 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
850 const BOOL rc = drive_file_set_fullpath(file, fullpath);
862 drive_file_init(file);
867BOOL drive_file_set_information(
DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
873 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
876 switch (FsInformationClass)
878 case FileBasicInformation:
879 return drive_file_set_basic_information(file, Length, input);
881 case FileEndOfFileInformation:
883 case FileAllocationInformation:
884 return drive_file_set_alloc_information(file, Length, input);
886 case FileDispositionInformation:
887 return drive_file_set_disposition_information(file, Length, input);
889 case FileRenameInformation:
890 return drive_file_set_rename_information(file, Length, input);
893 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
894 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
901static BOOL drive_file_query_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
904 WINPR_ASSERT(output);
907 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
910 if (length > UINT32_MAX - 64)
913 Stream_Write_UINT32(output, (UINT32)(64 + length));
914 Stream_Write_UINT32(output, 0);
915 Stream_Write_UINT32(output, 0);
916 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
917 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
918 Stream_Write_UINT32(output,
919 file->find_data.ftLastAccessTime.dwLowDateTime);
920 Stream_Write_UINT32(output,
921 file->find_data.ftLastAccessTime.dwHighDateTime);
922 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
923 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
924 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
925 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
926 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
927 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
928 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
929 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
930 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
931 Stream_Write_UINT32(output, (UINT32)length);
932 Stream_Write(output, file->find_data.cFileName, length);
936static BOOL drive_file_query_full_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
939 WINPR_ASSERT(output);
941 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
944 if (length > UINT32_MAX - 68)
947 Stream_Write_UINT32(output, (UINT32)(68 + length));
948 Stream_Write_UINT32(output, 0);
949 Stream_Write_UINT32(output, 0);
950 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
951 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
952 Stream_Write_UINT32(output,
953 file->find_data.ftLastAccessTime.dwLowDateTime);
954 Stream_Write_UINT32(output,
955 file->find_data.ftLastAccessTime.dwHighDateTime);
956 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
957 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
958 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
959 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
960 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
961 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
962 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
963 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
964 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
965 Stream_Write_UINT32(output, (UINT32)length);
966 Stream_Write_UINT32(output, 0);
967 Stream_Write(output, file->find_data.cFileName, length);
971static BOOL drive_file_query_both_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
974 WINPR_ASSERT(output);
976 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
979 if (length > UINT32_MAX - 93)
982 Stream_Write_UINT32(output, (UINT32)(93 + length));
983 Stream_Write_UINT32(output, 0);
984 Stream_Write_UINT32(output, 0);
985 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
986 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
987 Stream_Write_UINT32(output,
988 file->find_data.ftLastAccessTime.dwLowDateTime);
989 Stream_Write_UINT32(output,
990 file->find_data.ftLastAccessTime.dwHighDateTime);
991 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
992 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
993 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
994 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
995 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
996 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
997 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
998 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
999 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
1000 Stream_Write_UINT32(output, (UINT32)length);
1001 Stream_Write_UINT32(output, 0);
1002 Stream_Write_UINT8(output, 0);
1004 Stream_Zero(output, 24);
1005 Stream_Write(output, file->find_data.cFileName, length);
1009static BOOL drive_file_query_names_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1012 WINPR_ASSERT(output);
1014 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1017 if (length > UINT32_MAX - 12)
1020 Stream_Write_UINT32(output, (UINT32)(12 + length));
1021 Stream_Write_UINT32(output, 0);
1022 Stream_Write_UINT32(output, 0);
1023 Stream_Write_UINT32(output, (UINT32)length);
1024 Stream_Write(output, file->find_data.cFileName, length);
1028BOOL drive_file_query_directory(
DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1029 const WCHAR* path, UINT32 PathWCharLength,
wStream* output)
1033 WCHAR* ent_path = NULL;
1035 if (!file || !path || !output)
1038 if (InitialQuery != 0)
1041 if (file->find_handle != INVALID_HANDLE_VALUE)
1042 FindClose(file->find_handle);
1044 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1046 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1049 if (file->find_handle == INVALID_HANDLE_VALUE)
1052 else if (!FindNextFileW(file->find_handle, &file->find_data))
1055 length = _wcslen(file->find_data.cFileName) * 2;
1057 switch (FsInformationClass)
1059 case FileDirectoryInformation:
1060 rc = drive_file_query_dir_info(file, output, length);
1063 case FileFullDirectoryInformation:
1064 rc = drive_file_query_full_dir_info(file, output, length);
1067 case FileBothDirectoryInformation:
1068 rc = drive_file_query_both_dir_info(file, output, length);
1071 case FileNamesInformation:
1072 rc = drive_file_query_names_info(file, output, length);
1076 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
1077 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1085 Stream_Write_UINT32(output, 0);
1086 Stream_Write_UINT8(output, 0);