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 = _wcsdup(fullpath);
203 const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE),
'\0' };
204 WCHAR* filename = _wcsrchr(file->fullpath, *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);
436BOOL drive_file_read(
DRIVE_FILE* file, BYTE* buffer, UINT32* Length)
440 if (!file || !buffer || !Length)
443 DEBUG_WSTR(
"Read file %s", file->fullpath);
445 if (ReadFile(file->file_handle, buffer, *Length, &read,
nullptr))
454BOOL drive_file_write(
DRIVE_FILE* file,
const BYTE* buffer, UINT32 Length)
458 if (!file || !buffer)
461 DEBUG_WSTR(
"Write file %s", file->fullpath);
465 if (!WriteFile(file->file_handle, buffer, Length, &written,
nullptr))
476static BOOL drive_file_query_from_handle_information(
const DRIVE_FILE* file,
478 UINT32 FsInformationClass,
wStream* output)
480 switch (FsInformationClass)
482 case FileBasicInformation:
485 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
488 Stream_Write_UINT32(output, 36);
489 Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime);
490 Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime);
491 Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime);
492 Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime);
493 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
494 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
495 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
496 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
497 Stream_Write_UINT32(output, info->dwFileAttributes);
501 case FileStandardInformation:
504 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
507 Stream_Write_UINT32(output, 22);
508 Stream_Write_UINT32(output, info->nFileSizeLow);
509 Stream_Write_UINT32(output, info->nFileSizeHigh);
510 Stream_Write_UINT32(output, info->nFileSizeLow);
511 Stream_Write_UINT32(output, info->nFileSizeHigh);
512 Stream_Write_UINT32(output, info->nNumberOfLinks);
513 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
514 Stream_Write_UINT8(output, (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
519 case FileAttributeTagInformation:
522 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
525 Stream_Write_UINT32(output, 8);
526 Stream_Write_UINT32(output, info->dwFileAttributes);
527 Stream_Write_UINT32(output, 0);
532 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
533 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
541static BOOL drive_file_query_from_attributes(
const DRIVE_FILE* file,
543 UINT32 FsInformationClass,
wStream* output)
545 switch (FsInformationClass)
547 case FileBasicInformation:
550 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
553 Stream_Write_UINT32(output, 36);
554 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime);
555 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime);
556 Stream_Write_UINT32(output,
557 attrib->ftLastAccessTime.dwLowDateTime);
558 Stream_Write_UINT32(output,
559 attrib->ftLastAccessTime.dwHighDateTime);
560 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
561 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
562 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
563 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
564 Stream_Write_UINT32(output, attrib->dwFileAttributes);
568 case FileStandardInformation:
571 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
574 Stream_Write_UINT32(output, 22);
575 Stream_Write_UINT32(output, attrib->nFileSizeLow);
576 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
577 Stream_Write_UINT32(output, attrib->nFileSizeLow);
578 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
579 Stream_Write_UINT32(output, 0);
580 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
581 Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
586 case FileAttributeTagInformation:
589 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
592 Stream_Write_UINT32(output, 8);
593 Stream_Write_UINT32(output, attrib->dwFileAttributes);
594 Stream_Write_UINT32(output, 0);
599 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
600 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
607BOOL drive_file_query_information(
DRIVE_FILE* file, UINT32 FsInformationClass,
wStream* output)
612 if (!file || !output)
615 if ((file->file_handle != INVALID_HANDLE_VALUE) &&
616 GetFileInformationByHandle(file->file_handle, &fileInformation))
617 return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
622 HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING,
623 FILE_ATTRIBUTE_NORMAL,
nullptr);
624 if (hFile != INVALID_HANDLE_VALUE)
626 status = GetFileInformationByHandle(hFile, &fileInformation);
627 (void)CloseHandle(hFile);
631 if (!drive_file_query_from_handle_information(file, &fileInformation,
632 FsInformationClass, output))
643 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
646 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
652 Stream_Write_UINT32(output, 0);
657static BOOL drive_file_set_basic_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
661 const uint32_t expect = 36;
662 if (Length != expect)
664 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
669 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
670 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
671 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
672 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
673 const uint32_t FileAttributes = Stream_Get_UINT32(input);
675 if (!PathFileExistsW(file->fullpath))
678 if (file->file_handle == INVALID_HANDLE_VALUE)
680 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
681 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath) - 1);
683 WLog_ERR(TAG,
"Unable to set file time %s (%" PRIu32
")", fullpath, GetLastError());
687 FILETIME ftCreationTime = WINPR_C_ARRAY_INIT;
688 FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT;
689 FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT;
690 FILETIME* pftCreationTime =
nullptr;
691 FILETIME* pftLastAccessTime =
nullptr;
692 FILETIME* pftLastWriteTime =
nullptr;
693 if (liCreationTime.QuadPart != 0)
695 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
696 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
697 pftCreationTime = &ftCreationTime;
700 if (liLastAccessTime.QuadPart != 0)
702 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
703 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
704 pftLastAccessTime = &ftLastAccessTime;
707 if (liLastWriteTime.QuadPart != 0)
709 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
710 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
711 pftLastWriteTime = &ftLastWriteTime;
714 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
716 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
717 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
718 pftLastWriteTime = &ftLastWriteTime;
721 DEBUG_WSTR(
"SetFileTime %s", file->fullpath);
723 if (!SetFileAttributesW(file->fullpath, FileAttributes))
725 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
726 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
727 WLog_ERR(TAG,
"Unable to set file attributes for %s", fullpath);
731 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
733 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
734 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
735 WLog_ERR(TAG,
"Unable to set file time for %s", fullpath);
742static BOOL drive_file_set_alloc_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
745 const uint32_t expect = 8;
746 if (Length != expect)
748 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
753 const int64_t size = Stream_Get_INT64(input);
755 if (file->file_handle == INVALID_HANDLE_VALUE)
757 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
758 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
759 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
766 if (!SetFilePointerEx(file->file_handle, liSize,
nullptr, FILE_BEGIN))
768 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
769 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
770 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
775 DEBUG_WSTR(
"Truncate %s", file->fullpath);
777 if (SetEndOfFile(file->file_handle) == 0)
779 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
780 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
781 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRIu32
")", fullpath, size,
790static BOOL drive_file_set_disposition_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
793 uint8_t delete_pending = 0;
796 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
798 SetLastError(ERROR_DIR_NOT_EMPTY);
804 const uint32_t expect = 1;
805 if (Length != expect)
806 WLog_DBG(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
808 delete_pending = Stream_Get_UINT8(input);
815 DEBUG_WSTR(
"SetDeletePending %s", file->fullpath);
816 const uint32_t attr = GetFileAttributesW(file->fullpath);
818 if (attr & FILE_ATTRIBUTE_READONLY)
820 SetLastError(ERROR_ACCESS_DENIED);
825 file->delete_pending = delete_pending;
830static BOOL drive_file_set_rename_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
834 const uint32_t expect = 6;
837 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected at least %" PRIu32, Length, expect);
842 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
843 Stream_Seek_UINT8(input);
844 const uint64_t FileNameLength = Stream_Get_UINT32(input);
846 if (Length != expect + FileNameLength)
848 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu64, Length,
849 expect + FileNameLength);
853 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
854 FileNameLength /
sizeof(WCHAR));
861 if (file->file_handle != INVALID_HANDLE_VALUE)
863 (void)CloseHandle(file->file_handle);
864 file->file_handle = INVALID_HANDLE_VALUE;
868 DEBUG_WSTR(
"MoveFileExW %s", file->fullpath);
870 if (MoveFileExW(file->fullpath, fullpath,
871 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
873 const BOOL rc = drive_file_set_fullpath(file, fullpath);
885 drive_file_init(file);
890BOOL drive_file_set_information(
DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
896 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
899 switch (FsInformationClass)
901 case FileBasicInformation:
902 return drive_file_set_basic_information(file, Length, input);
904 case FileEndOfFileInformation:
906 case FileAllocationInformation:
907 return drive_file_set_alloc_information(file, Length, input);
909 case FileDispositionInformation:
910 return drive_file_set_disposition_information(file, Length, input);
912 case FileRenameInformation:
913 return drive_file_set_rename_information(file, Length, input);
916 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
917 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
925static BOOL drive_file_query_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
928 WINPR_ASSERT(output);
931 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
934 if (length > UINT32_MAX - 64)
937 Stream_Write_UINT32(output, (UINT32)(64 + length));
938 Stream_Write_UINT32(output, 0);
939 Stream_Write_UINT32(output, 0);
940 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
941 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
942 Stream_Write_UINT32(output,
943 file->find_data.ftLastAccessTime.dwLowDateTime);
944 Stream_Write_UINT32(output,
945 file->find_data.ftLastAccessTime.dwHighDateTime);
946 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
947 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
948 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
949 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
950 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
951 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
952 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
953 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
954 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
955 Stream_Write_UINT32(output, (UINT32)length);
956 Stream_Write(output, file->find_data.cFileName, length);
961static BOOL drive_file_query_full_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
964 WINPR_ASSERT(output);
966 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
969 if (length > UINT32_MAX - 68)
972 Stream_Write_UINT32(output, (UINT32)(68 + length));
973 Stream_Write_UINT32(output, 0);
974 Stream_Write_UINT32(output, 0);
975 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
976 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
977 Stream_Write_UINT32(output,
978 file->find_data.ftLastAccessTime.dwLowDateTime);
979 Stream_Write_UINT32(output,
980 file->find_data.ftLastAccessTime.dwHighDateTime);
981 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
982 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
983 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
984 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
985 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
986 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
987 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
988 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
989 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
990 Stream_Write_UINT32(output, (UINT32)length);
991 Stream_Write_UINT32(output, 0);
992 Stream_Write(output, file->find_data.cFileName, length);
997static BOOL drive_file_query_both_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1000 WINPR_ASSERT(output);
1002 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
1005 if (length > UINT32_MAX - 93)
1008 Stream_Write_UINT32(output, (UINT32)(93 + length));
1009 Stream_Write_UINT32(output, 0);
1010 Stream_Write_UINT32(output, 0);
1011 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
1012 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
1013 Stream_Write_UINT32(output,
1014 file->find_data.ftLastAccessTime.dwLowDateTime);
1015 Stream_Write_UINT32(output,
1016 file->find_data.ftLastAccessTime.dwHighDateTime);
1017 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
1018 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
1019 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
1020 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime);
1021 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
1022 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
1023 Stream_Write_UINT32(output, file->find_data.nFileSizeLow);
1024 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
1025 Stream_Write_UINT32(output, file->find_data.dwFileAttributes);
1026 Stream_Write_UINT32(output, (UINT32)length);
1027 Stream_Write_UINT32(output, 0);
1028 Stream_Write_UINT8(output, 0);
1030 Stream_Zero(output, 24);
1031 Stream_Write(output, file->find_data.cFileName, length);
1036static BOOL drive_file_query_names_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1039 WINPR_ASSERT(output);
1041 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1044 if (length > UINT32_MAX - 12)
1047 Stream_Write_UINT32(output, (UINT32)(12 + length));
1048 Stream_Write_UINT32(output, 0);
1049 Stream_Write_UINT32(output, 0);
1050 Stream_Write_UINT32(output, (UINT32)length);
1051 Stream_Write(output, file->find_data.cFileName, length);
1055BOOL drive_file_query_directory(
DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1056 const WCHAR* path, UINT32 PathWCharLength,
wStream* output)
1060 WCHAR* ent_path =
nullptr;
1062 if (!file || !path || !output)
1065 if (InitialQuery != 0)
1068 if (file->find_handle != INVALID_HANDLE_VALUE)
1069 FindClose(file->find_handle);
1071 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1073 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1076 if (file->find_handle == INVALID_HANDLE_VALUE)
1079 else if (!FindNextFileW(file->find_handle, &file->find_data))
1082 length = _wcslen(file->find_data.cFileName) *
sizeof(WCHAR);
1084 switch (FsInformationClass)
1086 case FileDirectoryInformation:
1087 rc = drive_file_query_dir_info(file, output, length);
1090 case FileFullDirectoryInformation:
1091 rc = drive_file_query_full_dir_info(file, output, length);
1094 case FileBothDirectoryInformation:
1095 rc = drive_file_query_both_dir_info(file, output, length);
1098 case FileNamesInformation:
1099 rc = drive_file_query_names_info(file, output, length);
1103 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
1104 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1112 Stream_Write_UINT32(output, 0);
1113 Stream_Write_UINT8(output, 0);