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] = WINPR_C_ARRAY_INIT; \
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] = WINPR_C_ARRAY_INIT;
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 =
nullptr;
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] = WINPR_C_ARRAY_INIT;
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 =
nullptr;
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) != 0);
235 if ((file->CreateDisposition == FILE_OPEN_IF) ||
236 (file->CreateDisposition == FILE_CREATE))
238 if (CreateDirectoryW(file->fullpath,
nullptr) != 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 nullptr, CreateDisposition, file->FileAttributes,
nullptr);
295 if (file->file_handle == INVALID_HANDLE_VALUE)
298 DWORD errorMessageID = GetLastError();
300 if (errorMessageID != 0)
302 LPSTR messageBuffer =
nullptr;
304 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
305 FORMAT_MESSAGE_IGNORE_INSERTS,
306 nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
307 (LPSTR)&messageBuffer, 0,
nullptr);
308 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
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,
nullptr, 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,
nullptr))
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,
nullptr))
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) !=
499 case FileAttributeTagInformation:
502 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
505 Stream_Write_UINT32(output, 8);
506 Stream_Write_UINT32(output, info->dwFileAttributes);
507 Stream_Write_UINT32(output, 0);
512 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
513 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
520static BOOL drive_file_query_from_attributes(
const DRIVE_FILE* file,
522 UINT32 FsInformationClass,
wStream* output)
524 switch (FsInformationClass)
526 case FileBasicInformation:
529 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
532 Stream_Write_UINT32(output, 36);
533 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime);
534 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime);
535 Stream_Write_UINT32(output,
536 attrib->ftLastAccessTime.dwLowDateTime);
537 Stream_Write_UINT32(output,
538 attrib->ftLastAccessTime.dwHighDateTime);
539 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
540 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
541 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
542 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
543 Stream_Write_UINT32(output, attrib->dwFileAttributes);
547 case FileStandardInformation:
550 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
553 Stream_Write_UINT32(output, 22);
554 Stream_Write_UINT32(output, attrib->nFileSizeLow);
555 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
556 Stream_Write_UINT32(output, attrib->nFileSizeLow);
557 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
558 Stream_Write_UINT32(output, 0);
559 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
560 Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
565 case FileAttributeTagInformation:
568 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
571 Stream_Write_UINT32(output, 8);
572 Stream_Write_UINT32(output, attrib->dwFileAttributes);
573 Stream_Write_UINT32(output, 0);
578 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
579 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
586BOOL drive_file_query_information(
DRIVE_FILE* file, UINT32 FsInformationClass,
wStream* output)
591 if (!file || !output)
594 if ((file->file_handle != INVALID_HANDLE_VALUE) &&
595 GetFileInformationByHandle(file->file_handle, &fileInformation))
596 return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
601 HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
602 FILE_ATTRIBUTE_NORMAL,
nullptr);
603 if (hFile != INVALID_HANDLE_VALUE)
605 status = GetFileInformationByHandle(hFile, &fileInformation);
606 (void)CloseHandle(hFile);
610 if (!drive_file_query_from_handle_information(file, &fileInformation,
611 FsInformationClass, output))
622 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
625 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
631 Stream_Write_UINT32(output, 0);
635static BOOL drive_file_set_basic_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
639 const uint32_t expect = 36;
640 if (Length != expect)
642 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
647 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
648 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
649 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
650 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
651 const uint32_t FileAttributes = Stream_Get_UINT32(input);
653 if (!PathFileExistsW(file->fullpath))
656 if (file->file_handle == INVALID_HANDLE_VALUE)
658 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
659 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath) - 1);
661 WLog_ERR(TAG,
"Unable to set file time %s (%" PRIu32
")", fullpath, GetLastError());
665 FILETIME ftCreationTime = WINPR_C_ARRAY_INIT;
666 FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT;
667 FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT;
668 FILETIME* pftCreationTime =
nullptr;
669 FILETIME* pftLastAccessTime =
nullptr;
670 FILETIME* pftLastWriteTime =
nullptr;
671 if (liCreationTime.QuadPart != 0)
673 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
674 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
675 pftCreationTime = &ftCreationTime;
678 if (liLastAccessTime.QuadPart != 0)
680 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
681 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
682 pftLastAccessTime = &ftLastAccessTime;
685 if (liLastWriteTime.QuadPart != 0)
687 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
688 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
689 pftLastWriteTime = &ftLastWriteTime;
692 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
694 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
695 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
696 pftLastWriteTime = &ftLastWriteTime;
699 DEBUG_WSTR(
"SetFileTime %s", file->fullpath);
701 if (!SetFileAttributesW(file->fullpath, FileAttributes))
703 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
704 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
705 WLog_ERR(TAG,
"Unable to set file attributes for %s", fullpath);
709 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
711 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
712 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
713 WLog_ERR(TAG,
"Unable to set file time for %s", fullpath);
719static BOOL drive_file_set_alloc_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
722 const uint32_t expect = 8;
723 if (Length != expect)
725 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
730 const int64_t size = Stream_Get_INT64(input);
732 if (file->file_handle == INVALID_HANDLE_VALUE)
734 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
735 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
736 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
743 if (!SetFilePointerEx(file->file_handle, liSize,
nullptr, FILE_BEGIN))
745 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
746 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
747 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
752 DEBUG_WSTR(
"Truncate %s", file->fullpath);
754 if (SetEndOfFile(file->file_handle) == 0)
756 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
757 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
758 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
766static BOOL drive_file_set_disposition_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
769 uint8_t delete_pending = 0;
772 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
774 SetLastError(ERROR_DIR_NOT_EMPTY);
780 const uint32_t expect = 1;
781 if (Length != expect)
782 WLog_DBG(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
784 delete_pending = Stream_Get_UINT8(input);
791 DEBUG_WSTR(
"SetDeletePending %s", file->fullpath);
792 const uint32_t attr = GetFileAttributesW(file->fullpath);
794 if (attr & FILE_ATTRIBUTE_READONLY)
796 SetLastError(ERROR_ACCESS_DENIED);
801 file->delete_pending = delete_pending;
805static BOOL drive_file_set_rename_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
809 const uint32_t expect = 6;
812 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected at least %" PRIu32, Length, expect);
817 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
818 Stream_Seek_UINT8(input);
819 const uint32_t FileNameLength = Stream_Get_UINT32(input);
821 if (Length != expect + FileNameLength)
823 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length,
824 expect + FileNameLength);
828 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
829 FileNameLength /
sizeof(WCHAR));
836 if (file->file_handle != INVALID_HANDLE_VALUE)
838 (void)CloseHandle(file->file_handle);
839 file->file_handle = INVALID_HANDLE_VALUE;
843 DEBUG_WSTR(
"MoveFileExW %s", file->fullpath);
845 if (MoveFileExW(file->fullpath, fullpath,
846 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
848 const BOOL rc = drive_file_set_fullpath(file, fullpath);
860 drive_file_init(file);
865BOOL drive_file_set_information(
DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
871 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
874 switch (FsInformationClass)
876 case FileBasicInformation:
877 return drive_file_set_basic_information(file, Length, input);
879 case FileEndOfFileInformation:
881 case FileAllocationInformation:
882 return drive_file_set_alloc_information(file, Length, input);
884 case FileDispositionInformation:
885 return drive_file_set_disposition_information(file, Length, input);
887 case FileRenameInformation:
888 return drive_file_set_rename_information(file, Length, input);
891 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
892 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
899static BOOL drive_file_query_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
902 WINPR_ASSERT(output);
905 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
908 if (length > UINT32_MAX - 64)
911 Stream_Write_UINT32(output, (UINT32)(64 + length));
912 Stream_Write_UINT32(output, 0);
913 Stream_Write_UINT32(output, 0);
914 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
915 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
916 Stream_Write_UINT32(output,
917 file->find_data.ftLastAccessTime.dwLowDateTime);
918 Stream_Write_UINT32(output,
919 file->find_data.ftLastAccessTime.dwHighDateTime);
920 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
921 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.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.nFileSizeLow);
925 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
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.dwFileAttributes);
929 Stream_Write_UINT32(output, (UINT32)length);
930 Stream_Write(output, file->find_data.cFileName, length);
934static BOOL drive_file_query_full_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
937 WINPR_ASSERT(output);
939 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
942 if (length > UINT32_MAX - 68)
945 Stream_Write_UINT32(output, (UINT32)(68 + length));
946 Stream_Write_UINT32(output, 0);
947 Stream_Write_UINT32(output, 0);
948 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
949 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
950 Stream_Write_UINT32(output,
951 file->find_data.ftLastAccessTime.dwLowDateTime);
952 Stream_Write_UINT32(output,
953 file->find_data.ftLastAccessTime.dwHighDateTime);
954 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
955 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.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.nFileSizeLow);
959 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
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.dwFileAttributes);
963 Stream_Write_UINT32(output, (UINT32)length);
964 Stream_Write_UINT32(output, 0);
965 Stream_Write(output, file->find_data.cFileName, length);
969static BOOL drive_file_query_both_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
972 WINPR_ASSERT(output);
974 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
977 if (length > UINT32_MAX - 93)
980 Stream_Write_UINT32(output, (UINT32)(93 + length));
981 Stream_Write_UINT32(output, 0);
982 Stream_Write_UINT32(output, 0);
983 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
984 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
985 Stream_Write_UINT32(output,
986 file->find_data.ftLastAccessTime.dwLowDateTime);
987 Stream_Write_UINT32(output,
988 file->find_data.ftLastAccessTime.dwHighDateTime);
989 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
990 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.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.nFileSizeLow);
994 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
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.dwFileAttributes);
998 Stream_Write_UINT32(output, (UINT32)length);
999 Stream_Write_UINT32(output, 0);
1000 Stream_Write_UINT8(output, 0);
1002 Stream_Zero(output, 24);
1003 Stream_Write(output, file->find_data.cFileName, length);
1007static BOOL drive_file_query_names_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1010 WINPR_ASSERT(output);
1012 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1015 if (length > UINT32_MAX - 12)
1018 Stream_Write_UINT32(output, (UINT32)(12 + length));
1019 Stream_Write_UINT32(output, 0);
1020 Stream_Write_UINT32(output, 0);
1021 Stream_Write_UINT32(output, (UINT32)length);
1022 Stream_Write(output, file->find_data.cFileName, length);
1026BOOL drive_file_query_directory(
DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1027 const WCHAR* path, UINT32 PathWCharLength,
wStream* output)
1031 WCHAR* ent_path =
nullptr;
1033 if (!file || !path || !output)
1036 if (InitialQuery != 0)
1039 if (file->find_handle != INVALID_HANDLE_VALUE)
1040 FindClose(file->find_handle);
1042 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1044 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1047 if (file->find_handle == INVALID_HANDLE_VALUE)
1050 else if (!FindNextFileW(file->find_handle, &file->find_data))
1053 length = _wcslen(file->find_data.cFileName) *
sizeof(WCHAR);
1055 switch (FsInformationClass)
1057 case FileDirectoryInformation:
1058 rc = drive_file_query_dir_info(file, output, length);
1061 case FileFullDirectoryInformation:
1062 rc = drive_file_query_full_dir_info(file, output, length);
1065 case FileBothDirectoryInformation:
1066 rc = drive_file_query_both_dir_info(file, output, length);
1069 case FileNamesInformation:
1070 rc = drive_file_query_names_info(file, output, length);
1074 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
1075 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1083 Stream_Write_UINT32(output, 0);
1084 Stream_Write_UINT8(output, 0);