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) \
63static BOOL drive_file_fix_path(WCHAR* path,
size_t length)
65 if ((length == 0) || (length > UINT32_MAX))
70 for (
size_t i = 0; i < length; i++)
78 if ((length == 3) && (path[1] == L
':') && (path[2] == L
'/'))
83 if ((length == 1) && (path[0] == L
'/'))
88 if ((length > 0) && (path[length - 1] == L
'/'))
89 path[length - 1] = L
'\0';
95static BOOL contains_dotdot(
const WCHAR* path,
size_t base_length,
size_t path_length)
97 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
98 const WCHAR* dotdot = InitializeConstWCharFromUtf8(
"..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
99 const WCHAR* tst = path;
106 tst = _wcsstr(tst, dotdot);
111 if ((base_length == 0) || (*(tst - 1) == L
'/') || (*(tst - 1) == L
'\\'))
113 if (tst + 2 < path + path_length)
115 if ((tst[2] ==
'/') || (tst[2] ==
'\\') || (tst[2] ==
'\0'))
127WINPR_ATTR_MALLOC(free, 1)
128static WCHAR* drive_file_combine_fullpath(const WCHAR* base_path, const WCHAR* path,
129 size_t PathWCharLength)
132 WCHAR* fullpath =
nullptr;
134 if (!base_path || (!path && (PathWCharLength > 0)))
138 size_t base_path_length = _wcsnlen(base_path, MAX_PATH);
139 if (base_path_length < 1)
142 const size_t length = base_path_length + PathWCharLength + 2;
143 fullpath = (WCHAR*)calloc(length,
sizeof(WCHAR));
148 CopyMemory(fullpath, base_path, base_path_length *
sizeof(WCHAR));
150 const WCHAR last = base_path[base_path_length - 1];
151 const WCHAR sepu = PathGetSeparatorW(PATH_STYLE_UNIX);
152 const WCHAR sepw = PathGetSeparatorW(PATH_STYLE_WINDOWS);
153 if ((last != sepu) && (last != sepw))
155 if (path && (PathWCharLength > 0) && (path[0] != sepu) && (path[0] != sepw))
156 fullpath[base_path_length++] = sepu;
160 CopyMemory(&fullpath[base_path_length], path, PathWCharLength *
sizeof(WCHAR));
162 if (!drive_file_fix_path(fullpath, length))
166 if (contains_dotdot(&fullpath[base_path_length], base_path_length, PathWCharLength))
168 char* abuffer = ConvertWCharToUtf8Alloc(&fullpath[base_path_length],
nullptr);
169 WLog_WARN(TAG,
"[rdpdr] received invalid file path '%s' from server, aborting!",
187static BOOL drive_file_set_fullpath(
DRIVE_FILE* file,
const WCHAR* fullpath)
189 if (!file || !fullpath)
192 const size_t len = _wcslen(fullpath);
193 free(file->fullpath);
194 file->fullpath =
nullptr;
199 file->fullpath = wcsndup(fullpath, len);
203 const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE),
'\0' };
204 WCHAR* filename = winpr_wcsnrchr(file->fullpath, len, *sep);
205 if (filename && _wcsncmp(filename, sep, ARRAYSIZE(sep)) == 0)
214 UINT CreateDisposition = 0;
215 DWORD dwAttr = GetFileAttributesW(file->fullpath);
217 if (dwAttr != INVALID_FILE_ATTRIBUTES)
220 file->is_dir = (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0;
224 if (file->CreateDisposition == FILE_CREATE)
226 SetLastError(ERROR_ALREADY_EXISTS);
230 if (file->CreateOptions & FILE_NON_DIRECTORY_FILE)
232 SetLastError(ERROR_ACCESS_DENIED);
240 if (file->CreateOptions & FILE_DIRECTORY_FILE)
242 SetLastError(ERROR_DIRECTORY);
249 file->is_dir = ((file->CreateOptions & FILE_DIRECTORY_FILE) != 0);
254 if ((file->CreateDisposition == FILE_OPEN_IF) ||
255 (file->CreateDisposition == FILE_CREATE))
257 if (CreateDirectoryW(file->fullpath,
nullptr) != 0)
263 SetLastError(ERROR_FILE_NOT_FOUND);
268 if (file->file_handle == INVALID_HANDLE_VALUE)
270 switch (file->CreateDisposition)
274 CreateDisposition = CREATE_ALWAYS;
279 CreateDisposition = OPEN_EXISTING;
284 CreateDisposition = CREATE_NEW;
289 CreateDisposition = OPEN_ALWAYS;
294 CreateDisposition = TRUNCATE_EXISTING;
297 case FILE_OVERWRITE_IF:
299 CreateDisposition = CREATE_ALWAYS;
307 file->SharedAccess = 0;
309 file->file_handle = CreateFileW(file->fullpath, file->DesiredAccess, file->SharedAccess,
310 nullptr, CreateDisposition, file->FileAttributes,
nullptr);
314 if (file->file_handle == INVALID_HANDLE_VALUE)
317 DWORD errorMessageID = GetLastError();
319 if (errorMessageID != 0)
321 LPSTR messageBuffer =
nullptr;
323 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
324 FORMAT_MESSAGE_IGNORE_INSERTS,
325 nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
326 (LPSTR)&messageBuffer, 0,
nullptr);
327 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
328 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
329 WLog_ERR(TAG,
"Error in drive_file_init: %s %s", messageBuffer, fullpath);
331 LocalFree(messageBuffer);
333 SetLastError(errorMessageID);
338 return file->file_handle != INVALID_HANDLE_VALUE;
341DRIVE_FILE* drive_file_new(
const WCHAR* base_path,
const WCHAR* path, UINT32 PathWCharLength,
342 UINT32
id, UINT32 DesiredAccess, UINT32 CreateDisposition,
343 UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess)
345 if (!base_path || (!path && (PathWCharLength > 0)))
352 WLog_ERR(TAG,
"calloc failed!");
356 file->file_handle = INVALID_HANDLE_VALUE;
357 file->find_handle = INVALID_HANDLE_VALUE;
359 file->basepath = base_path;
360 file->FileAttributes = FileAttributes;
361 file->DesiredAccess = DesiredAccess;
362 file->CreateDisposition = CreateDisposition;
363 file->CreateOptions = CreateOptions;
364 file->SharedAccess = SharedAccess;
366 WCHAR* p = drive_file_combine_fullpath(base_path, path, PathWCharLength);
367 (void)drive_file_set_fullpath(file, p);
370 if (!drive_file_init(file))
372 DWORD lastError = GetLastError();
373 drive_file_free(file);
374 SetLastError(lastError);
388 if (file->file_handle != INVALID_HANDLE_VALUE)
390 (void)CloseHandle(file->file_handle);
391 file->file_handle = INVALID_HANDLE_VALUE;
394 if (file->find_handle != INVALID_HANDLE_VALUE)
396 FindClose(file->find_handle);
397 file->find_handle = INVALID_HANDLE_VALUE;
400 if (file->CreateOptions & FILE_DELETE_ON_CLOSE)
401 file->delete_pending = TRUE;
403 if (file->delete_pending)
407 if (!winpr_RemoveDirectory_RecursiveW(file->fullpath))
410 else if (!DeleteFileW(file->fullpath))
416 DEBUG_WSTR(
"Free %s", file->fullpath);
417 free(file->fullpath);
422BOOL drive_file_seek(
DRIVE_FILE* file, UINT64 Offset)
429 if (Offset > INT64_MAX)
432 loffset.QuadPart = (LONGLONG)Offset;
433 return SetFilePointerEx(file->file_handle, loffset,
nullptr, FILE_BEGIN);
440 if (!file || !s || !Length)
442 SetLastError(ERROR_INVALID_PARAMETER);
446 UINT32 size = *Length;
449 if (!drive_file_seek(file, Offset))
452 DEBUG_WSTR(
"Read file %s", file->fullpath);
455 const DWORD sizeLow = GetFileSize(file, &sizeHigh);
456 const UINT64 size64 = 1ull * sizeLow + ((1ull * sizeHigh) << 32);
459 SetLastError(ERROR_INVALID_PARAMETER);
463 const UINT64 remain = size64 - Offset;
465 size = WINPR_ASSERTING_INT_CAST(UINT32, remain);
467 if (!Stream_EnsureRemainingCapacity(s, size))
469 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
473 void* buffer = Stream_Pointer(s);
474 if (!ReadFile(file->file_handle, buffer, size, &read,
nullptr))
481BOOL drive_file_write(
DRIVE_FILE* file,
const BYTE* buffer, UINT32 Length)
485 if (!file || !buffer)
488 DEBUG_WSTR(
"Write file %s", file->fullpath);
492 if (!WriteFile(file->file_handle, buffer, Length, &written,
nullptr))
503static BOOL drive_file_query_from_handle_information(
const DRIVE_FILE* file,
505 UINT32 FsInformationClass,
wStream* output)
507 switch (FsInformationClass)
509 case FileBasicInformation:
512 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
515 Stream_Write_UINT32(output, 36);
516 Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime);
517 Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime);
518 Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime);
519 Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime);
520 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
521 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
522 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
523 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
524 Stream_Write_UINT32(output, info->dwFileAttributes);
528 case FileStandardInformation:
531 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
534 Stream_Write_UINT32(output, 22);
535 Stream_Write_UINT32(output, info->nFileSizeLow);
536 Stream_Write_UINT32(output, info->nFileSizeHigh);
537 Stream_Write_UINT32(output, info->nFileSizeLow);
538 Stream_Write_UINT32(output, info->nFileSizeHigh);
539 Stream_Write_UINT32(output, info->nNumberOfLinks);
540 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
541 Stream_Write_UINT8(output, (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
546 case FileAttributeTagInformation:
549 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
552 Stream_Write_UINT32(output, 8);
553 Stream_Write_UINT32(output, info->dwFileAttributes);
554 Stream_Write_UINT32(output, 0);
559 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
560 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
568static BOOL drive_file_query_from_attributes(
const DRIVE_FILE* file,
570 UINT32 FsInformationClass,
wStream* output)
572 switch (FsInformationClass)
574 case FileBasicInformation:
577 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
580 Stream_Write_UINT32(output, 36);
581 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime);
582 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime);
583 Stream_Write_UINT32(output,
584 attrib->ftLastAccessTime.dwLowDateTime);
585 Stream_Write_UINT32(output,
586 attrib->ftLastAccessTime.dwHighDateTime);
587 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
588 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
589 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
590 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
591 Stream_Write_UINT32(output, attrib->dwFileAttributes);
595 case FileStandardInformation:
598 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
601 Stream_Write_UINT32(output, 22);
602 Stream_Write_UINT32(output, attrib->nFileSizeLow);
603 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
604 Stream_Write_UINT32(output, attrib->nFileSizeLow);
605 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
606 Stream_Write_UINT32(output, 0);
607 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
608 Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
613 case FileAttributeTagInformation:
616 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
619 Stream_Write_UINT32(output, 8);
620 Stream_Write_UINT32(output, attrib->dwFileAttributes);
621 Stream_Write_UINT32(output, 0);
626 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
627 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
634BOOL drive_file_query_information(
DRIVE_FILE* file, UINT32 FsInformationClass,
wStream* output)
639 if (!file || !output)
642 if ((file->file_handle != INVALID_HANDLE_VALUE) &&
643 GetFileInformationByHandle(file->file_handle, &fileInformation))
644 return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
649 HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
650 FILE_ATTRIBUTE_NORMAL,
nullptr);
651 if (hFile != INVALID_HANDLE_VALUE)
653 status = GetFileInformationByHandle(hFile, &fileInformation);
654 (void)CloseHandle(hFile);
658 if (!drive_file_query_from_handle_information(file, &fileInformation,
659 FsInformationClass, output))
670 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
673 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
679 Stream_Write_UINT32(output, 0);
684static BOOL drive_file_set_basic_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
688 const uint32_t expect = 36;
689 if (Length != expect)
691 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
696 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
697 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
698 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
699 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
700 const uint32_t FileAttributes = Stream_Get_UINT32(input);
702 if (!PathFileExistsW(file->fullpath))
705 if (file->file_handle == INVALID_HANDLE_VALUE)
707 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
708 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath) - 1);
710 WLog_ERR(TAG,
"Unable to set file time %s (%" PRIu32
")", fullpath, GetLastError());
714 FILETIME ftCreationTime = WINPR_C_ARRAY_INIT;
715 FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT;
716 FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT;
717 FILETIME* pftCreationTime =
nullptr;
718 FILETIME* pftLastAccessTime =
nullptr;
719 FILETIME* pftLastWriteTime =
nullptr;
720 if (liCreationTime.QuadPart != 0)
722 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
723 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
724 pftCreationTime = &ftCreationTime;
727 if (liLastAccessTime.QuadPart != 0)
729 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
730 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
731 pftLastAccessTime = &ftLastAccessTime;
734 if (liLastWriteTime.QuadPart != 0)
736 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
737 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
738 pftLastWriteTime = &ftLastWriteTime;
741 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
743 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
744 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
745 pftLastWriteTime = &ftLastWriteTime;
748 DEBUG_WSTR(
"SetFileTime %s", file->fullpath);
750 if (!SetFileAttributesW(file->fullpath, FileAttributes))
752 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
753 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
754 WLog_ERR(TAG,
"Unable to set file attributes for %s", fullpath);
758 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
760 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
761 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
762 WLog_ERR(TAG,
"Unable to set file time for %s", fullpath);
769static BOOL drive_file_set_alloc_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
772 const uint32_t expect = 8;
773 if (Length != expect)
775 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
780 const int64_t size = Stream_Get_INT64(input);
782 if (file->file_handle == INVALID_HANDLE_VALUE)
784 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
785 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
786 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
793 if (!SetFilePointerEx(file->file_handle, liSize,
nullptr, FILE_BEGIN))
795 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
796 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
797 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
802 DEBUG_WSTR(
"Truncate %s", file->fullpath);
804 if (SetEndOfFile(file->file_handle) == 0)
806 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
807 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
808 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
817static BOOL drive_file_set_disposition_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
820 uint8_t delete_pending = 0;
823 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
825 SetLastError(ERROR_DIR_NOT_EMPTY);
831 const uint32_t expect = 1;
832 if (Length != expect)
833 WLog_DBG(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
835 delete_pending = Stream_Get_UINT8(input);
842 DEBUG_WSTR(
"SetDeletePending %s", file->fullpath);
843 const uint32_t attr = GetFileAttributesW(file->fullpath);
845 if (attr & FILE_ATTRIBUTE_READONLY)
847 SetLastError(ERROR_ACCESS_DENIED);
852 file->delete_pending = delete_pending;
857static BOOL drive_file_set_rename_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
861 const uint32_t expect = 6;
864 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected at least %" PRIu32, Length, expect);
869 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
870 Stream_Seek_UINT8(input);
871 const uint64_t FileNameLength = Stream_Get_UINT32(input);
873 if (Length != expect + FileNameLength)
875 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu64, Length,
876 expect + FileNameLength);
880 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
881 FileNameLength /
sizeof(WCHAR));
888 if (file->file_handle != INVALID_HANDLE_VALUE)
890 (void)CloseHandle(file->file_handle);
891 file->file_handle = INVALID_HANDLE_VALUE;
895 DEBUG_WSTR(
"MoveFileExW %s", file->fullpath);
897 if (MoveFileExW(file->fullpath, fullpath,
898 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
900 const BOOL rc = drive_file_set_fullpath(file, fullpath);
912 drive_file_init(file);
917BOOL drive_file_set_information(
DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
923 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
926 switch (FsInformationClass)
928 case FileBasicInformation:
929 return drive_file_set_basic_information(file, Length, input);
931 case FileEndOfFileInformation:
933 case FileAllocationInformation:
934 return drive_file_set_alloc_information(file, Length, input);
936 case FileDispositionInformation:
937 return drive_file_set_disposition_information(file, Length, input);
939 case FileRenameInformation:
940 return drive_file_set_rename_information(file, Length, input);
943 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
944 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
952static BOOL drive_file_query_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
955 WINPR_ASSERT(output);
958 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
961 if (length > UINT32_MAX - 64)
964 Stream_Write_UINT32(output, (UINT32)(64 + length));
965 Stream_Write_UINT32(output, 0);
966 Stream_Write_UINT32(output, 0);
967 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
968 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
969 Stream_Write_UINT32(output,
970 file->find_data.ftLastAccessTime.dwLowDateTime);
971 Stream_Write_UINT32(output,
972 file->find_data.ftLastAccessTime.dwHighDateTime);
973 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
974 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
975 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
976 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
977 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
978 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
979 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
980 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
981 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
982 Stream_Write_UINT32(output, (UINT32)length);
983 Stream_Write(output, file->find_data.cFileName, length);
988static BOOL drive_file_query_full_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
991 WINPR_ASSERT(output);
993 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
996 if (length > UINT32_MAX - 68)
999 Stream_Write_UINT32(output, (UINT32)(68 + length));
1000 Stream_Write_UINT32(output, 0);
1001 Stream_Write_UINT32(output, 0);
1002 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
1003 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
1004 Stream_Write_UINT32(output,
1005 file->find_data.ftLastAccessTime.dwLowDateTime);
1006 Stream_Write_UINT32(output,
1007 file->find_data.ftLastAccessTime.dwHighDateTime);
1008 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
1009 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
1010 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
1011 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
1012 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
1013 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
1014 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
1015 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
1016 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
1017 Stream_Write_UINT32(output, (UINT32)length);
1018 Stream_Write_UINT32(output, 0);
1019 Stream_Write(output, file->find_data.cFileName, length);
1024static BOOL drive_file_query_both_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1027 WINPR_ASSERT(output);
1029 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
1032 if (length > UINT32_MAX - 93)
1035 Stream_Write_UINT32(output, (UINT32)(93 + length));
1036 Stream_Write_UINT32(output, 0);
1037 Stream_Write_UINT32(output, 0);
1038 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
1039 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
1040 Stream_Write_UINT32(output,
1041 file->find_data.ftLastAccessTime.dwLowDateTime);
1042 Stream_Write_UINT32(output,
1043 file->find_data.ftLastAccessTime.dwHighDateTime);
1044 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
1045 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
1046 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
1047 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
1048 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
1049 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
1050 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
1051 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
1052 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
1053 Stream_Write_UINT32(output, (UINT32)length);
1054 Stream_Write_UINT32(output, 0);
1055 Stream_Write_UINT8(output, 0);
1057 Stream_Zero(output, 24);
1058 Stream_Write(output, file->find_data.cFileName, length);
1063static BOOL drive_file_query_names_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1066 WINPR_ASSERT(output);
1068 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1071 if (length > UINT32_MAX - 12)
1074 Stream_Write_UINT32(output, (UINT32)(12 + length));
1075 Stream_Write_UINT32(output, 0);
1076 Stream_Write_UINT32(output, 0);
1077 Stream_Write_UINT32(output, (UINT32)length);
1078 Stream_Write(output, file->find_data.cFileName, length);
1082BOOL drive_file_query_directory(
DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1083 const WCHAR* path, UINT32 PathWCharLength,
wStream* output)
1087 WCHAR* ent_path =
nullptr;
1089 if (!file || !path || !output)
1092 if (InitialQuery != 0)
1095 if (file->find_handle != INVALID_HANDLE_VALUE)
1096 FindClose(file->find_handle);
1098 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1100 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1103 if (file->find_handle == INVALID_HANDLE_VALUE)
1106 else if (!FindNextFileW(file->find_handle, &file->find_data))
1109 length = _wcslen(file->find_data.cFileName) *
sizeof(WCHAR);
1111 switch (FsInformationClass)
1113 case FileDirectoryInformation:
1114 rc = drive_file_query_dir_info(file, output, length);
1117 case FileFullDirectoryInformation:
1118 rc = drive_file_query_full_dir_info(file, output, length);
1121 case FileBothDirectoryInformation:
1122 rc = drive_file_query_both_dir_info(file, output, length);
1125 case FileNamesInformation:
1126 rc = drive_file_query_names_info(file, output, length);
1130 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
1131 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1139 Stream_Write_UINT32(output, 0);
1140 Stream_Write_UINT8(output, 0);