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)))
132 const size_t base_path_length = _wcsnlen(base_path, MAX_PATH);
133 const size_t length = base_path_length + PathWCharLength + 1;
134 fullpath = (WCHAR*)calloc(length,
sizeof(WCHAR));
139 CopyMemory(fullpath, base_path, base_path_length *
sizeof(WCHAR));
141 CopyMemory(&fullpath[base_path_length], path, PathWCharLength *
sizeof(WCHAR));
143 if (!drive_file_fix_path(fullpath, length))
147 if (contains_dotdot(&fullpath[base_path_length], base_path_length, PathWCharLength))
149 char abuffer[MAX_PATH] = { 0 };
150 (void)ConvertWCharToUtf8(&fullpath[base_path_length], abuffer, ARRAYSIZE(abuffer));
152 WLog_WARN(TAG,
"[rdpdr] received invalid file path '%s' from server, aborting!",
153 &abuffer[base_path_length]);
167static BOOL drive_file_set_fullpath(
DRIVE_FILE* file,
const WCHAR* fullpath)
169 if (!file || !fullpath)
172 const size_t len = _wcslen(fullpath);
173 free(file->fullpath);
174 file->fullpath = NULL;
179 file->fullpath = _wcsdup(fullpath);
183 const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE),
'\0' };
184 WCHAR* filename = _wcsrchr(file->fullpath, *sep);
185 if (filename && _wcsncmp(filename, sep, ARRAYSIZE(sep)) == 0)
193 UINT CreateDisposition = 0;
194 DWORD dwAttr = GetFileAttributesW(file->fullpath);
196 if (dwAttr != INVALID_FILE_ATTRIBUTES)
199 file->is_dir = (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0;
203 if (file->CreateDisposition == FILE_CREATE)
205 SetLastError(ERROR_ALREADY_EXISTS);
209 if (file->CreateOptions & FILE_NON_DIRECTORY_FILE)
211 SetLastError(ERROR_ACCESS_DENIED);
219 if (file->CreateOptions & FILE_DIRECTORY_FILE)
221 SetLastError(ERROR_DIRECTORY);
228 file->is_dir = ((file->CreateOptions & FILE_DIRECTORY_FILE) ? TRUE : FALSE);
233 if ((file->CreateDisposition == FILE_OPEN_IF) ||
234 (file->CreateDisposition == FILE_CREATE))
236 if (CreateDirectoryW(file->fullpath, NULL) != 0)
242 SetLastError(ERROR_FILE_NOT_FOUND);
247 if (file->file_handle == INVALID_HANDLE_VALUE)
249 switch (file->CreateDisposition)
253 CreateDisposition = CREATE_ALWAYS;
258 CreateDisposition = OPEN_EXISTING;
263 CreateDisposition = CREATE_NEW;
268 CreateDisposition = OPEN_ALWAYS;
273 CreateDisposition = TRUNCATE_EXISTING;
276 case FILE_OVERWRITE_IF:
278 CreateDisposition = CREATE_ALWAYS;
286 file->SharedAccess = 0;
288 file->file_handle = CreateFileW(file->fullpath, file->DesiredAccess, file->SharedAccess,
289 NULL, CreateDisposition, file->FileAttributes, NULL);
293 if (file->file_handle == INVALID_HANDLE_VALUE)
296 DWORD errorMessageID = GetLastError();
298 if (errorMessageID != 0)
300 LPSTR messageBuffer = NULL;
302 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
303 FORMAT_MESSAGE_IGNORE_INSERTS,
304 NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
305 (LPSTR)&messageBuffer, 0, NULL);
306 char fullpath[MAX_PATH] = { 0 };
307 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
308 WLog_ERR(TAG,
"Error in drive_file_init: %s %s", messageBuffer, fullpath);
310 LocalFree(messageBuffer);
312 SetLastError(errorMessageID);
317 return file->file_handle != INVALID_HANDLE_VALUE;
320DRIVE_FILE* drive_file_new(
const WCHAR* base_path,
const WCHAR* path, UINT32 PathWCharLength,
321 UINT32
id, UINT32 DesiredAccess, UINT32 CreateDisposition,
322 UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess)
324 if (!base_path || (!path && (PathWCharLength > 0)))
331 WLog_ERR(TAG,
"calloc failed!");
335 file->file_handle = INVALID_HANDLE_VALUE;
336 file->find_handle = INVALID_HANDLE_VALUE;
338 file->basepath = base_path;
339 file->FileAttributes = FileAttributes;
340 file->DesiredAccess = DesiredAccess;
341 file->CreateDisposition = CreateDisposition;
342 file->CreateOptions = CreateOptions;
343 file->SharedAccess = SharedAccess;
345 WCHAR* p = drive_file_combine_fullpath(base_path, path, PathWCharLength);
346 (void)drive_file_set_fullpath(file, p);
349 if (!drive_file_init(file))
351 DWORD lastError = GetLastError();
352 drive_file_free(file);
353 SetLastError(lastError);
367 if (file->file_handle != INVALID_HANDLE_VALUE)
369 (void)CloseHandle(file->file_handle);
370 file->file_handle = INVALID_HANDLE_VALUE;
373 if (file->find_handle != INVALID_HANDLE_VALUE)
375 FindClose(file->find_handle);
376 file->find_handle = INVALID_HANDLE_VALUE;
379 if (file->CreateOptions & FILE_DELETE_ON_CLOSE)
380 file->delete_pending = TRUE;
382 if (file->delete_pending)
386 if (!winpr_RemoveDirectory_RecursiveW(file->fullpath))
389 else if (!DeleteFileW(file->fullpath))
395 DEBUG_WSTR(
"Free %s", file->fullpath);
396 free(file->fullpath);
401BOOL drive_file_seek(
DRIVE_FILE* file, UINT64 Offset)
408 if (Offset > INT64_MAX)
411 loffset.QuadPart = (LONGLONG)Offset;
412 return SetFilePointerEx(file->file_handle, loffset, NULL, FILE_BEGIN);
415BOOL drive_file_read(
DRIVE_FILE* file, BYTE* buffer, UINT32* Length)
419 if (!file || !buffer || !Length)
422 DEBUG_WSTR(
"Read file %s", file->fullpath);
424 if (ReadFile(file->file_handle, buffer, *Length, &read, NULL))
433BOOL drive_file_write(
DRIVE_FILE* file,
const BYTE* buffer, UINT32 Length)
437 if (!file || !buffer)
440 DEBUG_WSTR(
"Write file %s", file->fullpath);
444 if (!WriteFile(file->file_handle, buffer, Length, &written, NULL))
454static BOOL drive_file_query_from_handle_information(
const DRIVE_FILE* file,
456 UINT32 FsInformationClass,
wStream* output)
458 switch (FsInformationClass)
460 case FileBasicInformation:
463 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
466 Stream_Write_UINT32(output, 36);
467 Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime);
468 Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime);
469 Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime);
470 Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime);
471 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
472 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
473 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);
474 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);
475 Stream_Write_UINT32(output, info->dwFileAttributes);
479 case FileStandardInformation:
482 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
485 Stream_Write_UINT32(output, 22);
486 Stream_Write_UINT32(output, info->nFileSizeLow);
487 Stream_Write_UINT32(output, info->nFileSizeHigh);
488 Stream_Write_UINT32(output, info->nFileSizeLow);
489 Stream_Write_UINT32(output, info->nFileSizeHigh);
490 Stream_Write_UINT32(output, info->nNumberOfLinks);
491 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
492 Stream_Write_UINT8(output, info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
498 case FileAttributeTagInformation:
501 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
504 Stream_Write_UINT32(output, 8);
505 Stream_Write_UINT32(output, info->dwFileAttributes);
506 Stream_Write_UINT32(output, 0);
511 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
512 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
519static BOOL drive_file_query_from_attributes(
const DRIVE_FILE* file,
521 UINT32 FsInformationClass,
wStream* output)
523 switch (FsInformationClass)
525 case FileBasicInformation:
528 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
531 Stream_Write_UINT32(output, 36);
532 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime);
533 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime);
534 Stream_Write_UINT32(output,
535 attrib->ftLastAccessTime.dwLowDateTime);
536 Stream_Write_UINT32(output,
537 attrib->ftLastAccessTime.dwHighDateTime);
538 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
539 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
540 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);
541 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime);
542 Stream_Write_UINT32(output, attrib->dwFileAttributes);
546 case FileStandardInformation:
549 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
552 Stream_Write_UINT32(output, 22);
553 Stream_Write_UINT32(output, attrib->nFileSizeLow);
554 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
555 Stream_Write_UINT32(output, attrib->nFileSizeLow);
556 Stream_Write_UINT32(output, attrib->nFileSizeHigh);
557 Stream_Write_UINT32(output, 0);
558 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0);
559 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, NULL, OPEN_EXISTING,
602 FILE_ATTRIBUTE_NORMAL, NULL);
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))
621 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
624 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
629 Stream_Write_UINT32(output, 0);
633static BOOL drive_file_set_basic_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
637 const uint32_t expect = 36;
638 if (Length != expect)
640 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
645 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
646 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
647 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
648 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
649 const uint32_t FileAttributes = Stream_Get_UINT32(input);
651 if (!PathFileExistsW(file->fullpath))
654 if (file->file_handle == INVALID_HANDLE_VALUE)
656 char fullpath[MAX_PATH] = { 0 };
657 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath) - 1);
659 WLog_ERR(TAG,
"Unable to set file time %s (%" PRId32
")", fullpath, GetLastError());
669 if (liCreationTime.QuadPart != 0)
671 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
672 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
673 pftCreationTime = &ftCreationTime;
676 if (liLastAccessTime.QuadPart != 0)
678 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
679 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
680 pftLastAccessTime = &ftLastAccessTime;
683 if (liLastWriteTime.QuadPart != 0)
685 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
686 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
687 pftLastWriteTime = &ftLastWriteTime;
690 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
692 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
693 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
694 pftLastWriteTime = &ftLastWriteTime;
697 DEBUG_WSTR(
"SetFileTime %s", file->fullpath);
699 if (!SetFileAttributesW(file->fullpath, FileAttributes))
701 char fullpath[MAX_PATH] = { 0 };
702 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
703 WLog_ERR(TAG,
"Unable to set file attributes for %s", fullpath);
707 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
709 char fullpath[MAX_PATH] = { 0 };
710 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
711 WLog_ERR(TAG,
"Unable to set file time for %s", fullpath);
717static BOOL drive_file_set_alloc_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
720 const uint32_t expect = 8;
721 if (Length != expect)
723 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
728 const int64_t size = Stream_Get_INT64(input);
730 if (file->file_handle == INVALID_HANDLE_VALUE)
732 char fullpath[MAX_PATH] = { 0 };
733 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
734 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRId32
")", fullpath, size,
741 if (!SetFilePointerEx(file->file_handle, liSize, NULL, FILE_BEGIN))
743 char fullpath[MAX_PATH] = { 0 };
744 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
745 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRId32
")", fullpath, size,
750 DEBUG_WSTR(
"Truncate %s", file->fullpath);
752 if (SetEndOfFile(file->file_handle) == 0)
754 char fullpath[MAX_PATH] = { 0 };
755 (void)ConvertWCharToUtf8(file->fullpath, fullpath,
sizeof(fullpath));
756 WLog_ERR(TAG,
"Unable to truncate %s to %" PRId64
" (%" PRId32
")", fullpath, size,
764static BOOL drive_file_set_disposition_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
767 uint8_t delete_pending = 0;
770 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
772 SetLastError(ERROR_DIR_NOT_EMPTY);
778 const uint32_t expect = 1;
779 if (Length != expect)
780 WLog_DBG(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length, expect);
782 delete_pending = Stream_Get_UINT8(input);
789 DEBUG_WSTR(
"SetDeletePending %s", file->fullpath);
790 const uint32_t attr = GetFileAttributesW(file->fullpath);
792 if (attr & FILE_ATTRIBUTE_READONLY)
794 SetLastError(ERROR_ACCESS_DENIED);
799 file->delete_pending = delete_pending;
803static BOOL drive_file_set_rename_information(
DRIVE_FILE* file, UINT32 Length,
wStream* input)
807 const uint32_t expect = 6;
810 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected at least %" PRIu32, Length, expect);
815 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
816 Stream_Seek_UINT8(input);
817 const uint32_t FileNameLength = Stream_Get_UINT32(input);
819 if (Length != expect + FileNameLength)
821 WLog_WARN(TAG,
"Unexpected Length=%" PRIu32
", expected %" PRIu32, Length,
822 expect + FileNameLength);
826 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
827 FileNameLength /
sizeof(WCHAR));
834 if (file->file_handle != INVALID_HANDLE_VALUE)
836 (void)CloseHandle(file->file_handle);
837 file->file_handle = INVALID_HANDLE_VALUE;
841 DEBUG_WSTR(
"MoveFileExW %s", file->fullpath);
843 if (MoveFileExW(file->fullpath, fullpath,
844 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
846 const BOOL rc = drive_file_set_fullpath(file, fullpath);
858 drive_file_init(file);
863BOOL drive_file_set_information(
DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
869 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
872 switch (FsInformationClass)
874 case FileBasicInformation:
875 return drive_file_set_basic_information(file, Length, input);
877 case FileEndOfFileInformation:
879 case FileAllocationInformation:
880 return drive_file_set_alloc_information(file, Length, input);
882 case FileDispositionInformation:
883 return drive_file_set_disposition_information(file, Length, input);
885 case FileRenameInformation:
886 return drive_file_set_rename_information(file, Length, input);
889 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
890 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
897static BOOL drive_file_query_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
900 WINPR_ASSERT(output);
903 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
906 if (length > UINT32_MAX - 64)
909 Stream_Write_UINT32(output, (UINT32)(64 + length));
910 Stream_Write_UINT32(output, 0);
911 Stream_Write_UINT32(output, 0);
912 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
913 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
914 Stream_Write_UINT32(output,
915 file->find_data.ftLastAccessTime.dwLowDateTime);
916 Stream_Write_UINT32(output,
917 file->find_data.ftLastAccessTime.dwHighDateTime);
918 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
919 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.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.nFileSizeLow);
923 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
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.dwFileAttributes);
927 Stream_Write_UINT32(output, (UINT32)length);
928 Stream_Write(output, file->find_data.cFileName, length);
932static BOOL drive_file_query_full_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
935 WINPR_ASSERT(output);
937 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
940 if (length > UINT32_MAX - 68)
943 Stream_Write_UINT32(output, (UINT32)(68 + length));
944 Stream_Write_UINT32(output, 0);
945 Stream_Write_UINT32(output, 0);
946 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
947 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
948 Stream_Write_UINT32(output,
949 file->find_data.ftLastAccessTime.dwLowDateTime);
950 Stream_Write_UINT32(output,
951 file->find_data.ftLastAccessTime.dwHighDateTime);
952 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
953 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.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.nFileSizeLow);
957 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
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.dwFileAttributes);
961 Stream_Write_UINT32(output, (UINT32)length);
962 Stream_Write_UINT32(output, 0);
963 Stream_Write(output, file->find_data.cFileName, length);
967static BOOL drive_file_query_both_dir_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
970 WINPR_ASSERT(output);
972 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
975 if (length > UINT32_MAX - 93)
978 Stream_Write_UINT32(output, (UINT32)(93 + length));
979 Stream_Write_UINT32(output, 0);
980 Stream_Write_UINT32(output, 0);
981 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime);
982 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime);
983 Stream_Write_UINT32(output,
984 file->find_data.ftLastAccessTime.dwLowDateTime);
985 Stream_Write_UINT32(output,
986 file->find_data.ftLastAccessTime.dwHighDateTime);
987 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);
988 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.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.nFileSizeLow);
992 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);
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.dwFileAttributes);
996 Stream_Write_UINT32(output, (UINT32)length);
997 Stream_Write_UINT32(output, 0);
998 Stream_Write_UINT8(output, 0);
1000 Stream_Zero(output, 24);
1001 Stream_Write(output, file->find_data.cFileName, length);
1005static BOOL drive_file_query_names_info(
DRIVE_FILE* file,
wStream* output,
size_t length)
1008 WINPR_ASSERT(output);
1010 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1013 if (length > UINT32_MAX - 12)
1016 Stream_Write_UINT32(output, (UINT32)(12 + length));
1017 Stream_Write_UINT32(output, 0);
1018 Stream_Write_UINT32(output, 0);
1019 Stream_Write_UINT32(output, (UINT32)length);
1020 Stream_Write(output, file->find_data.cFileName, length);
1024BOOL drive_file_query_directory(
DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1025 const WCHAR* path, UINT32 PathWCharLength,
wStream* output)
1029 WCHAR* ent_path = NULL;
1031 if (!file || !path || !output)
1034 if (InitialQuery != 0)
1037 if (file->find_handle != INVALID_HANDLE_VALUE)
1038 FindClose(file->find_handle);
1040 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1042 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1045 if (file->find_handle == INVALID_HANDLE_VALUE)
1048 else if (!FindNextFileW(file->find_handle, &file->find_data))
1051 length = _wcslen(file->find_data.cFileName) * 2;
1053 switch (FsInformationClass)
1055 case FileDirectoryInformation:
1056 rc = drive_file_query_dir_info(file, output, length);
1059 case FileFullDirectoryInformation:
1060 rc = drive_file_query_full_dir_info(file, output, length);
1063 case FileBothDirectoryInformation:
1064 rc = drive_file_query_both_dir_info(file, output, length);
1067 case FileNamesInformation:
1068 rc = drive_file_query_names_info(file, output, length);
1072 WLog_WARN(TAG,
"Unhandled FSInformationClass %s [0x%08" PRIx32
"]",
1073 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1081 Stream_Write_UINT32(output, 0);
1082 Stream_Write_UINT8(output, 0);