Coverage Report

Created: 2026-09-14 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/channels/drive/client/drive_file.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * File System Virtual Channel
4
 *
5
 * Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 * Copyright 2010-2011 Vic Lee
7
 * Copyright 2012 Gerald Richter
8
 * Copyright 2015 Thincast Technologies GmbH
9
 * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
10
 * Copyright 2016 Inuvika Inc.
11
 * Copyright 2016 David PHAM-VAN <d.phamvan@inuvika.com>
12
 * Copyright 2017 Armin Novak <armin.novak@thincast.com>
13
 * Copyright 2017 Thincast Technologies GmbH
14
 *
15
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18
 *
19
 *     http://www.apache.org/licenses/LICENSE-2.0
20
 *
21
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26
 */
27
28
#include <freerdp/config.h>
29
30
#include <errno.h>
31
#include <stdio.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <time.h>
35
36
#include <winpr/wtypes.h>
37
#include <winpr/crt.h>
38
#include <winpr/string.h>
39
#include <winpr/path.h>
40
#include <winpr/file.h>
41
#include <winpr/stream.h>
42
43
#include <freerdp/channels/rdpdr.h>
44
45
#include "drive_file.h"
46
47
#ifdef WITH_DEBUG_RDPDR
48
#define DEBUG_WSTR(msg, wstr)                                    \
49
  do                                                           \
50
  {                                                            \
51
    char lpstr[1024] = WINPR_C_ARRAY_INIT;                   \
52
    (void)ConvertWCharToUtf8(wstr, lpstr, ARRAYSIZE(lpstr)); \
53
    WLog_DBG(TAG, msg, lpstr);                               \
54
  } while (0)
55
#else
56
#define DEBUG_WSTR(msg, wstr) \
57
0
  do                        \
58
0
  {                         \
59
0
  } while (0)
60
#endif
61
62
WINPR_ATTR_NODISCARD
63
static BOOL drive_file_fix_path(WCHAR* path, size_t length)
64
0
{
65
0
  if ((length == 0) || (length > UINT32_MAX))
66
0
    return FALSE;
67
68
0
  WINPR_ASSERT(path);
69
70
0
  for (size_t i = 0; i < length; i++)
71
0
  {
72
0
    if (path[i] == L'\\')
73
0
      path[i] = L'/';
74
0
  }
75
76
#ifdef WIN32
77
78
  if ((length == 3) && (path[1] == L':') && (path[2] == L'/'))
79
    return FALSE;
80
81
#else
82
83
0
  if ((length == 1) && (path[0] == L'/'))
84
0
    return FALSE;
85
86
0
#endif
87
88
0
  if ((length > 0) && (path[length - 1] == L'/'))
89
0
    path[length - 1] = L'\0';
90
91
0
  return TRUE;
92
0
}
93
94
WINPR_ATTR_NODISCARD
95
static BOOL contains_dotdot(const WCHAR* path, size_t base_length, size_t path_length)
96
0
{
97
0
  WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
98
0
  const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
99
0
  const WCHAR* tst = path;
100
101
0
  if (path_length < 2)
102
0
    return FALSE;
103
104
0
  do
105
0
  {
106
0
    tst = _wcsstr(tst, dotdot);
107
0
    if (!tst)
108
0
      return FALSE;
109
110
    /* Filter .. sequences in file or directory names */
111
0
    if ((base_length == 0) || (*(tst - 1) == L'/') || (*(tst - 1) == L'\\'))
112
0
    {
113
0
      if (tst + 2 < path + path_length)
114
0
      {
115
0
        if ((tst[2] == '/') || (tst[2] == '\\') || (tst[2] == '\0'))
116
0
          return TRUE;
117
0
      }
118
0
      else
119
0
        return TRUE;
120
0
    }
121
0
    tst += 2;
122
0
  } while (TRUE);
123
124
0
  return FALSE;
125
0
}
126
127
WINPR_ATTR_MALLOC(free, 1)
128
static WCHAR* drive_file_combine_fullpath(const WCHAR* base_path, const WCHAR* path,
129
                                          size_t PathWCharLength)
130
0
{
131
0
  BOOL ok = FALSE;
132
0
  WCHAR* fullpath = nullptr;
133
134
0
  if (!base_path || (!path && (PathWCharLength > 0)))
135
0
    goto fail;
136
137
0
  {
138
0
    size_t base_path_length = _wcsnlen(base_path, MAX_PATH);
139
0
    if (base_path_length < 1)
140
0
      goto fail;
141
142
0
    const size_t length = base_path_length + PathWCharLength + 2;
143
0
    fullpath = (WCHAR*)calloc(length, sizeof(WCHAR));
144
145
0
    if (!fullpath)
146
0
      goto fail;
147
148
0
    CopyMemory(fullpath, base_path, base_path_length * sizeof(WCHAR));
149
150
0
    const WCHAR last = base_path[base_path_length - 1];
151
0
    const WCHAR sepu = PathGetSeparatorW(PATH_STYLE_UNIX);
152
0
    const WCHAR sepw = PathGetSeparatorW(PATH_STYLE_WINDOWS);
153
0
    if ((last != sepu) && (last != sepw))
154
0
    {
155
0
      if (path && (PathWCharLength > 0) && (path[0] != sepu) && (path[0] != sepw))
156
0
        fullpath[base_path_length++] = sepu;
157
0
    }
158
159
0
    if (path)
160
0
      CopyMemory(&fullpath[base_path_length], path, PathWCharLength * sizeof(WCHAR));
161
162
0
    if (!drive_file_fix_path(fullpath, length))
163
0
      goto fail;
164
165
    /* Ensure the path does not contain sequences like '..' */
166
0
    if (contains_dotdot(&fullpath[base_path_length], base_path_length, PathWCharLength))
167
0
    {
168
0
      char* abuffer = ConvertWCharToUtf8Alloc(&fullpath[base_path_length], nullptr);
169
0
      WLog_WARN(TAG, "[rdpdr] received invalid file path '%s' from server, aborting!",
170
0
                abuffer);
171
0
      free(abuffer);
172
0
      goto fail;
173
0
    }
174
0
  }
175
176
0
  ok = TRUE;
177
0
fail:
178
0
  if (!ok)
179
0
  {
180
0
    free(fullpath);
181
0
    fullpath = nullptr;
182
0
  }
183
0
  return fullpath;
184
0
}
185
186
WINPR_ATTR_NODISCARD
187
static BOOL drive_file_set_fullpath(DRIVE_FILE* file, const WCHAR* fullpath)
188
0
{
189
0
  if (!file || !fullpath)
190
0
    return FALSE;
191
192
0
  const size_t len = _wcslen(fullpath);
193
0
  free(file->fullpath);
194
0
  file->fullpath = nullptr;
195
196
0
  if (len == 0)
197
0
    return TRUE;
198
199
0
  file->fullpath = _wcsdup(fullpath);
200
0
  if (!file->fullpath)
201
0
    return FALSE;
202
203
0
  const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE), '\0' };
204
0
  WCHAR* filename = _wcsrchr(file->fullpath, *sep);
205
0
  if (filename && _wcsncmp(filename, sep, ARRAYSIZE(sep)) == 0)
206
0
    *filename = '\0';
207
208
0
  return TRUE;
209
0
}
210
211
WINPR_ATTR_NODISCARD
212
static BOOL drive_file_init(DRIVE_FILE* file)
213
0
{
214
0
  UINT CreateDisposition = 0;
215
0
  DWORD dwAttr = GetFileAttributesW(file->fullpath);
216
217
0
  if (dwAttr != INVALID_FILE_ATTRIBUTES)
218
0
  {
219
    /* The file exists */
220
0
    file->is_dir = (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0;
221
222
0
    if (file->is_dir)
223
0
    {
224
0
      if (file->CreateDisposition == FILE_CREATE)
225
0
      {
226
0
        SetLastError(ERROR_ALREADY_EXISTS);
227
0
        return FALSE;
228
0
      }
229
230
0
      if (file->CreateOptions & FILE_NON_DIRECTORY_FILE)
231
0
      {
232
0
        SetLastError(ERROR_ACCESS_DENIED);
233
0
        return FALSE;
234
0
      }
235
236
0
      return TRUE;
237
0
    }
238
0
    else
239
0
    {
240
0
      if (file->CreateOptions & FILE_DIRECTORY_FILE)
241
0
      {
242
0
        SetLastError(ERROR_DIRECTORY);
243
0
        return FALSE;
244
0
      }
245
0
    }
246
0
  }
247
0
  else
248
0
  {
249
0
    file->is_dir = ((file->CreateOptions & FILE_DIRECTORY_FILE) != 0);
250
251
0
    if (file->is_dir)
252
0
    {
253
      /* Should only create the directory if the disposition allows for it */
254
0
      if ((file->CreateDisposition == FILE_OPEN_IF) ||
255
0
          (file->CreateDisposition == FILE_CREATE))
256
0
      {
257
0
        if (CreateDirectoryW(file->fullpath, nullptr) != 0)
258
0
        {
259
0
          return TRUE;
260
0
        }
261
0
      }
262
263
0
      SetLastError(ERROR_FILE_NOT_FOUND);
264
0
      return FALSE;
265
0
    }
266
0
  }
267
268
0
  if (file->file_handle == INVALID_HANDLE_VALUE)
269
0
  {
270
0
    switch (file->CreateDisposition)
271
0
    {
272
0
      case FILE_SUPERSEDE: /* If the file already exists, replace it with the given file. If
273
                              it does not, create the given file. */
274
0
        CreateDisposition = CREATE_ALWAYS;
275
0
        break;
276
277
0
      case FILE_OPEN: /* If the file already exists, open it instead of creating a new file.
278
                         If it does not, fail the request and do not create a new file. */
279
0
        CreateDisposition = OPEN_EXISTING;
280
0
        break;
281
282
0
      case FILE_CREATE: /* If the file already exists, fail the request and do not create or
283
                           open the given file. If it does not, create the given file. */
284
0
        CreateDisposition = CREATE_NEW;
285
0
        break;
286
287
0
      case FILE_OPEN_IF: /* If the file already exists, open it. If it does not, create the
288
                            given file. */
289
0
        CreateDisposition = OPEN_ALWAYS;
290
0
        break;
291
292
0
      case FILE_OVERWRITE: /* If the file already exists, open it and overwrite it. If it does
293
                              not, fail the request. */
294
0
        CreateDisposition = TRUNCATE_EXISTING;
295
0
        break;
296
297
0
      case FILE_OVERWRITE_IF: /* If the file already exists, open it and overwrite it. If it
298
                                 does not, create the given file. */
299
0
        CreateDisposition = CREATE_ALWAYS;
300
0
        break;
301
302
0
      default:
303
0
        break;
304
0
    }
305
306
0
#ifndef WIN32
307
0
    file->SharedAccess = 0;
308
0
#endif
309
0
    file->file_handle = CreateFileW(file->fullpath, file->DesiredAccess, file->SharedAccess,
310
0
                                    nullptr, CreateDisposition, file->FileAttributes, nullptr);
311
0
  }
312
313
#ifdef WIN32
314
  if (file->file_handle == INVALID_HANDLE_VALUE)
315
  {
316
    /* Get the error message, if any. */
317
    DWORD errorMessageID = GetLastError();
318
319
    if (errorMessageID != 0)
320
    {
321
      LPSTR messageBuffer = nullptr;
322
      size_t size =
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);
330
      /* Free the buffer. */
331
      LocalFree(messageBuffer);
332
      /* restore original error code */
333
      SetLastError(errorMessageID);
334
    }
335
  }
336
#endif
337
338
0
  return file->file_handle != INVALID_HANDLE_VALUE;
339
0
}
340
341
DRIVE_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)
344
0
{
345
0
  if (!base_path || (!path && (PathWCharLength > 0)))
346
0
    return nullptr;
347
348
0
  DRIVE_FILE* file = (DRIVE_FILE*)calloc(1, sizeof(DRIVE_FILE));
349
350
0
  if (!file)
351
0
  {
352
0
    WLog_ERR(TAG, "calloc failed!");
353
0
    return nullptr;
354
0
  }
355
356
0
  file->file_handle = INVALID_HANDLE_VALUE;
357
0
  file->find_handle = INVALID_HANDLE_VALUE;
358
0
  file->id = id;
359
0
  file->basepath = base_path;
360
0
  file->FileAttributes = FileAttributes;
361
0
  file->DesiredAccess = DesiredAccess;
362
0
  file->CreateDisposition = CreateDisposition;
363
0
  file->CreateOptions = CreateOptions;
364
0
  file->SharedAccess = SharedAccess;
365
366
0
  WCHAR* p = drive_file_combine_fullpath(base_path, path, PathWCharLength);
367
0
  (void)drive_file_set_fullpath(file, p);
368
0
  free(p);
369
370
0
  if (!drive_file_init(file))
371
0
  {
372
0
    DWORD lastError = GetLastError();
373
0
    drive_file_free(file);
374
0
    SetLastError(lastError);
375
0
    return nullptr;
376
0
  }
377
378
0
  return file;
379
0
}
380
381
BOOL drive_file_free(DRIVE_FILE* file)
382
0
{
383
0
  BOOL rc = FALSE;
384
385
0
  if (!file)
386
0
    return FALSE;
387
388
0
  if (file->file_handle != INVALID_HANDLE_VALUE)
389
0
  {
390
0
    (void)CloseHandle(file->file_handle);
391
0
    file->file_handle = INVALID_HANDLE_VALUE;
392
0
  }
393
394
0
  if (file->find_handle != INVALID_HANDLE_VALUE)
395
0
  {
396
0
    FindClose(file->find_handle);
397
0
    file->find_handle = INVALID_HANDLE_VALUE;
398
0
  }
399
400
0
  if (file->CreateOptions & FILE_DELETE_ON_CLOSE)
401
0
    file->delete_pending = TRUE;
402
403
0
  if (file->delete_pending)
404
0
  {
405
0
    if (file->is_dir)
406
0
    {
407
0
      if (!winpr_RemoveDirectory_RecursiveW(file->fullpath))
408
0
        goto fail;
409
0
    }
410
0
    else if (!DeleteFileW(file->fullpath))
411
0
      goto fail;
412
0
  }
413
414
0
  rc = TRUE;
415
0
fail:
416
0
  DEBUG_WSTR("Free %s", file->fullpath);
417
0
  free(file->fullpath);
418
0
  free(file);
419
0
  return rc;
420
0
}
421
422
BOOL drive_file_seek(DRIVE_FILE* file, UINT64 Offset)
423
0
{
424
0
  LARGE_INTEGER loffset = WINPR_C_ARRAY_INIT;
425
426
0
  if (!file)
427
0
    return FALSE;
428
429
0
  if (Offset > INT64_MAX)
430
0
    return FALSE;
431
432
0
  loffset.QuadPart = (LONGLONG)Offset;
433
0
  return SetFilePointerEx(file->file_handle, loffset, nullptr, FILE_BEGIN);
434
0
}
435
436
BOOL drive_file_read(DRIVE_FILE* file, BYTE* buffer, UINT32* Length)
437
0
{
438
0
  DWORD read = 0;
439
440
0
  if (!file || !buffer || !Length)
441
0
    return FALSE;
442
443
0
  DEBUG_WSTR("Read file %s", file->fullpath);
444
445
0
  if (ReadFile(file->file_handle, buffer, *Length, &read, nullptr))
446
0
  {
447
0
    *Length = read;
448
0
    return TRUE;
449
0
  }
450
451
0
  return FALSE;
452
0
}
453
454
BOOL drive_file_write(DRIVE_FILE* file, const BYTE* buffer, UINT32 Length)
455
0
{
456
0
  DWORD written = 0;
457
458
0
  if (!file || !buffer)
459
0
    return FALSE;
460
461
0
  DEBUG_WSTR("Write file %s", file->fullpath);
462
463
0
  while (Length > 0)
464
0
  {
465
0
    if (!WriteFile(file->file_handle, buffer, Length, &written, nullptr))
466
0
      return FALSE;
467
468
0
    Length -= written;
469
0
    buffer += written;
470
0
  }
471
472
0
  return TRUE;
473
0
}
474
475
WINPR_ATTR_NODISCARD
476
static BOOL drive_file_query_from_handle_information(const DRIVE_FILE* file,
477
                                                     const BY_HANDLE_FILE_INFORMATION* info,
478
                                                     UINT32 FsInformationClass, wStream* output)
479
0
{
480
0
  switch (FsInformationClass)
481
0
  {
482
0
    case FileBasicInformation:
483
484
      /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
485
0
      if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
486
0
        return FALSE;
487
488
0
      Stream_Write_UINT32(output, 36);                                    /* Length */
489
0
      Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime);    /* CreationTime */
490
0
      Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime);   /* CreationTime */
491
0
      Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime);  /* LastAccessTime */
492
0
      Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
493
0
      Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);   /* LastWriteTime */
494
0
      Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);  /* LastWriteTime */
495
0
      Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime);   /* ChangeTime */
496
0
      Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime);  /* ChangeTime */
497
0
      Stream_Write_UINT32(output, info->dwFileAttributes);                /* FileAttributes */
498
      /* Reserved(4), MUST NOT be added! */
499
0
      break;
500
501
0
    case FileStandardInformation:
502
503
      /*  http://msdn.microsoft.com/en-us/library/cc232088.aspx */
504
0
      if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
505
0
        return FALSE;
506
507
0
      Stream_Write_UINT32(output, 22);                          /* Length */
508
0
      Stream_Write_UINT32(output, info->nFileSizeLow);          /* AllocationSize */
509
0
      Stream_Write_UINT32(output, info->nFileSizeHigh);         /* AllocationSize */
510
0
      Stream_Write_UINT32(output, info->nFileSizeLow);          /* EndOfFile */
511
0
      Stream_Write_UINT32(output, info->nFileSizeHigh);         /* EndOfFile */
512
0
      Stream_Write_UINT32(output, info->nNumberOfLinks);        /* NumberOfLinks */
513
0
      Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */
514
0
      Stream_Write_UINT8(output, (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
515
0
                                     0); /* Directory */
516
      /* Reserved(2), MUST NOT be added! */
517
0
      break;
518
519
0
    case FileAttributeTagInformation:
520
521
      /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */
522
0
      if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
523
0
        return FALSE;
524
525
0
      Stream_Write_UINT32(output, 8);                      /* Length */
526
0
      Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */
527
0
      Stream_Write_UINT32(output, 0);                      /* ReparseTag */
528
0
      break;
529
530
0
    default:
531
      /* Unhandled FsInformationClass */
532
0
      WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
533
0
                FSInformationClass2Tag(FsInformationClass), FsInformationClass);
534
0
      return FALSE;
535
0
  }
536
537
0
  return TRUE;
538
0
}
539
540
WINPR_ATTR_NODISCARD
541
static BOOL drive_file_query_from_attributes(const DRIVE_FILE* file,
542
                                             const WIN32_FILE_ATTRIBUTE_DATA* attrib,
543
                                             UINT32 FsInformationClass, wStream* output)
544
0
{
545
0
  switch (FsInformationClass)
546
0
  {
547
0
    case FileBasicInformation:
548
549
      /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
550
0
      if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
551
0
        return FALSE;
552
553
0
      Stream_Write_UINT32(output, 36);                                    /* Length */
554
0
      Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime);  /* CreationTime */
555
0
      Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime); /* CreationTime */
556
0
      Stream_Write_UINT32(output,
557
0
                          attrib->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
558
0
      Stream_Write_UINT32(output,
559
0
                          attrib->ftLastAccessTime.dwHighDateTime);       /* LastAccessTime */
560
0
      Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
561
0
      Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
562
0
      Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime);  /* ChangeTime */
563
0
      Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* ChangeTime */
564
0
      Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */
565
      /* Reserved(4), MUST NOT be added! */
566
0
      break;
567
568
0
    case FileStandardInformation:
569
570
      /*  http://msdn.microsoft.com/en-us/library/cc232088.aspx */
571
0
      if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
572
0
        return FALSE;
573
574
0
      Stream_Write_UINT32(output, 22);                          /* Length */
575
0
      Stream_Write_UINT32(output, attrib->nFileSizeLow);        /* AllocationSize */
576
0
      Stream_Write_UINT32(output, attrib->nFileSizeHigh);       /* AllocationSize */
577
0
      Stream_Write_UINT32(output, attrib->nFileSizeLow);        /* EndOfFile */
578
0
      Stream_Write_UINT32(output, attrib->nFileSizeHigh);       /* EndOfFile */
579
0
      Stream_Write_UINT32(output, 0);                           /* NumberOfLinks */
580
0
      Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */
581
0
      Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
582
0
                                     0); /* Directory */
583
      /* Reserved(2), MUST NOT be added! */
584
0
      break;
585
586
0
    case FileAttributeTagInformation:
587
588
      /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */
589
0
      if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
590
0
        return FALSE;
591
592
0
      Stream_Write_UINT32(output, 8);                        /* Length */
593
0
      Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */
594
0
      Stream_Write_UINT32(output, 0);                        /* ReparseTag */
595
0
      break;
596
597
0
    default:
598
      /* Unhandled FsInformationClass */
599
0
      WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
600
0
                FSInformationClass2Tag(FsInformationClass), FsInformationClass);
601
0
      return FALSE;
602
0
  }
603
604
0
  return TRUE;
605
0
}
606
607
BOOL drive_file_query_information(DRIVE_FILE* file, UINT32 FsInformationClass, wStream* output)
608
0
{
609
0
  BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT;
610
0
  BOOL status = 0;
611
612
0
  if (!file || !output)
613
0
    return FALSE;
614
615
0
  if ((file->file_handle != INVALID_HANDLE_VALUE) &&
616
0
      GetFileInformationByHandle(file->file_handle, &fileInformation))
617
0
    return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
618
0
                                                    output);
619
620
0
  if (!file->is_dir)
621
0
  {
622
0
    HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
623
0
                               FILE_ATTRIBUTE_NORMAL, nullptr);
624
0
    if (hFile != INVALID_HANDLE_VALUE)
625
0
    {
626
0
      status = GetFileInformationByHandle(hFile, &fileInformation);
627
0
      (void)CloseHandle(hFile);
628
0
      if (!status)
629
0
        goto out_fail;
630
631
0
      if (!drive_file_query_from_handle_information(file, &fileInformation,
632
0
                                                    FsInformationClass, output))
633
0
        goto out_fail;
634
635
0
      return TRUE;
636
0
    }
637
0
  }
638
639
  /* If we failed before (i.e. if information for a drive is queried) fall back to
640
   * GetFileAttributesExW */
641
0
  {
642
0
    WIN32_FILE_ATTRIBUTE_DATA fileAttributes = WINPR_C_ARRAY_INIT;
643
0
    if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
644
0
      goto out_fail;
645
646
0
    if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
647
0
      goto out_fail;
648
0
  }
649
650
0
  return TRUE;
651
0
out_fail:
652
0
  Stream_Write_UINT32(output, 0); /* Length */
653
0
  return FALSE;
654
0
}
655
656
WINPR_ATTR_NODISCARD
657
static BOOL drive_file_set_basic_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
658
0
{
659
0
  WINPR_ASSERT(file);
660
661
0
  const uint32_t expect = 36;
662
0
  if (Length != expect)
663
0
  {
664
0
    WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
665
0
    return FALSE;
666
0
  }
667
668
  /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
669
0
  const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
670
0
  const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
671
0
  const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
672
0
  const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
673
0
  const uint32_t FileAttributes = Stream_Get_UINT32(input);
674
675
0
  if (!PathFileExistsW(file->fullpath))
676
0
    return FALSE;
677
678
0
  if (file->file_handle == INVALID_HANDLE_VALUE)
679
0
  {
680
0
    char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
681
0
    (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath) - 1);
682
683
0
    WLog_ERR(TAG, "Unable to set file time %s (%" PRIu32 ")", fullpath, GetLastError());
684
0
    return FALSE;
685
0
  }
686
687
0
  FILETIME ftCreationTime = WINPR_C_ARRAY_INIT;
688
0
  FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT;
689
0
  FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT;
690
0
  FILETIME* pftCreationTime = nullptr;
691
0
  FILETIME* pftLastAccessTime = nullptr;
692
0
  FILETIME* pftLastWriteTime = nullptr;
693
0
  if (liCreationTime.QuadPart != 0)
694
0
  {
695
0
    ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
696
0
    ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
697
0
    pftCreationTime = &ftCreationTime;
698
0
  }
699
700
0
  if (liLastAccessTime.QuadPart != 0)
701
0
  {
702
0
    ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
703
0
    ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
704
0
    pftLastAccessTime = &ftLastAccessTime;
705
0
  }
706
707
0
  if (liLastWriteTime.QuadPart != 0)
708
0
  {
709
0
    ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
710
0
    ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
711
0
    pftLastWriteTime = &ftLastWriteTime;
712
0
  }
713
714
0
  if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
715
0
  {
716
0
    ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
717
0
    ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
718
0
    pftLastWriteTime = &ftLastWriteTime;
719
0
  }
720
721
0
  DEBUG_WSTR("SetFileTime %s", file->fullpath);
722
723
0
  if (!SetFileAttributesW(file->fullpath, FileAttributes))
724
0
  {
725
0
    char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
726
0
    (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
727
0
    WLog_ERR(TAG, "Unable to set file attributes for %s", fullpath);
728
0
    return FALSE;
729
0
  }
730
731
0
  if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
732
0
  {
733
0
    char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
734
0
    (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
735
0
    WLog_ERR(TAG, "Unable to set file time for %s", fullpath);
736
0
    return FALSE;
737
0
  }
738
0
  return TRUE;
739
0
}
740
741
WINPR_ATTR_NODISCARD
742
static BOOL drive_file_set_alloc_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
743
0
{
744
0
  WINPR_ASSERT(file);
745
0
  const uint32_t expect = 8;
746
0
  if (Length != expect)
747
0
  {
748
0
    WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
749
0
    return FALSE;
750
0
  }
751
752
  /* http://msdn.microsoft.com/en-us/library/cc232076.aspx */
753
0
  const int64_t size = Stream_Get_INT64(input);
754
755
0
  if (file->file_handle == INVALID_HANDLE_VALUE)
756
0
  {
757
0
    char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
758
0
    (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
759
0
    WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
760
0
             GetLastError());
761
0
    return FALSE;
762
0
  }
763
764
0
  LARGE_INTEGER liSize = { .QuadPart = size };
765
766
0
  if (!SetFilePointerEx(file->file_handle, liSize, nullptr, FILE_BEGIN))
767
0
  {
768
0
    char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
769
0
    (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
770
0
    WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
771
0
             GetLastError());
772
0
    return FALSE;
773
0
  }
774
775
0
  DEBUG_WSTR("Truncate %s", file->fullpath);
776
777
0
  if (SetEndOfFile(file->file_handle) == 0)
778
0
  {
779
0
    char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
780
0
    (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
781
0
    WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
782
0
             GetLastError());
783
0
    return FALSE;
784
0
  }
785
786
0
  return TRUE;
787
0
}
788
789
WINPR_ATTR_NODISCARD
790
static BOOL drive_file_set_disposition_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
791
0
{
792
0
  WINPR_ASSERT(file);
793
0
  uint8_t delete_pending = 0;
794
  /* http://msdn.microsoft.com/en-us/library/cc232098.aspx */
795
  /* http://msdn.microsoft.com/en-us/library/cc241371.aspx */
796
0
  if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
797
0
  {
798
0
    SetLastError(ERROR_DIR_NOT_EMPTY);
799
0
    return FALSE;
800
0
  }
801
802
0
  if (Length)
803
0
  {
804
0
    const uint32_t expect = 1;
805
0
    if (Length != expect)
806
0
      WLog_DBG(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
807
808
0
    delete_pending = Stream_Get_UINT8(input);
809
0
  }
810
0
  else
811
0
    delete_pending = 1;
812
813
0
  if (delete_pending)
814
0
  {
815
0
    DEBUG_WSTR("SetDeletePending %s", file->fullpath);
816
0
    const uint32_t attr = GetFileAttributesW(file->fullpath);
817
818
0
    if (attr & FILE_ATTRIBUTE_READONLY)
819
0
    {
820
0
      SetLastError(ERROR_ACCESS_DENIED);
821
0
      return FALSE;
822
0
    }
823
0
  }
824
825
0
  file->delete_pending = delete_pending;
826
0
  return TRUE;
827
0
}
828
829
WINPR_ATTR_NODISCARD
830
static BOOL drive_file_set_rename_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
831
0
{
832
0
  WINPR_ASSERT(file);
833
834
0
  const uint32_t expect = 6;
835
0
  if (Length < expect)
836
0
  {
837
0
    WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected at least %" PRIu32, Length, expect);
838
0
    return FALSE;
839
0
  }
840
841
  /* http://msdn.microsoft.com/en-us/library/cc232085.aspx */
842
0
  const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
843
0
  Stream_Seek_UINT8(input); /* RootDirectory */
844
0
  const uint64_t FileNameLength = Stream_Get_UINT32(input);
845
846
0
  if (Length != expect + FileNameLength)
847
0
  {
848
0
    WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu64, Length,
849
0
              expect + FileNameLength);
850
0
    return FALSE;
851
0
  }
852
853
0
  WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
854
0
                                                FileNameLength / sizeof(WCHAR));
855
856
0
  if (!fullpath)
857
0
    return FALSE;
858
859
#ifdef _WIN32
860
861
  if (file->file_handle != INVALID_HANDLE_VALUE)
862
  {
863
    (void)CloseHandle(file->file_handle);
864
    file->file_handle = INVALID_HANDLE_VALUE;
865
  }
866
867
#endif
868
0
  DEBUG_WSTR("MoveFileExW %s", file->fullpath);
869
870
0
  if (MoveFileExW(file->fullpath, fullpath,
871
0
                  MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
872
0
  {
873
0
    const BOOL rc = drive_file_set_fullpath(file, fullpath);
874
0
    free(fullpath);
875
0
    if (!rc)
876
0
      return FALSE;
877
0
  }
878
0
  else
879
0
  {
880
0
    free(fullpath);
881
0
    return FALSE;
882
0
  }
883
884
#ifdef _WIN32
885
  drive_file_init(file);
886
#endif
887
0
  return TRUE;
888
0
}
889
890
BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
891
                                wStream* input)
892
0
{
893
0
  if (!file || !input)
894
0
    return FALSE;
895
896
0
  if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
897
0
    return FALSE;
898
899
0
  switch (FsInformationClass)
900
0
  {
901
0
    case FileBasicInformation:
902
0
      return drive_file_set_basic_information(file, Length, input);
903
904
0
    case FileEndOfFileInformation:
905
    /* http://msdn.microsoft.com/en-us/library/cc232067.aspx */
906
0
    case FileAllocationInformation:
907
0
      return drive_file_set_alloc_information(file, Length, input);
908
909
0
    case FileDispositionInformation:
910
0
      return drive_file_set_disposition_information(file, Length, input);
911
912
0
    case FileRenameInformation:
913
0
      return drive_file_set_rename_information(file, Length, input);
914
915
0
    default:
916
0
      WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
917
0
                FSInformationClass2Tag(FsInformationClass), FsInformationClass);
918
0
      return FALSE;
919
0
  }
920
921
0
  return TRUE;
922
0
}
923
924
WINPR_ATTR_NODISCARD
925
static BOOL drive_file_query_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
926
0
{
927
0
  WINPR_ASSERT(file);
928
0
  WINPR_ASSERT(output);
929
930
  /* http://msdn.microsoft.com/en-us/library/cc232097.aspx */
931
0
  if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
932
0
    return FALSE;
933
934
0
  if (length > UINT32_MAX - 64)
935
0
    return FALSE;
936
937
0
  Stream_Write_UINT32(output, (UINT32)(64 + length));                        /* Length */
938
0
  Stream_Write_UINT32(output, 0);                                            /* NextEntryOffset */
939
0
  Stream_Write_UINT32(output, 0);                                            /* FileIndex */
940
0
  Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
941
0
  Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
942
0
  Stream_Write_UINT32(output,
943
0
                      file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
944
0
  Stream_Write_UINT32(output,
945
0
                      file->find_data.ftLastAccessTime.dwHighDateTime);       /* LastAccessTime */
946
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
947
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
948
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);  /* ChangeTime */
949
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
950
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeLow);                   /* EndOfFile */
951
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);                  /* EndOfFile */
952
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeLow);     /* AllocationSize */
953
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);    /* AllocationSize */
954
0
  Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
955
0
  Stream_Write_UINT32(output, (UINT32)length);                   /* FileNameLength */
956
0
  Stream_Write(output, file->find_data.cFileName, length);
957
0
  return TRUE;
958
0
}
959
960
WINPR_ATTR_NODISCARD
961
static BOOL drive_file_query_full_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
962
0
{
963
0
  WINPR_ASSERT(file);
964
0
  WINPR_ASSERT(output);
965
  /* http://msdn.microsoft.com/en-us/library/cc232068.aspx */
966
0
  if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
967
0
    return FALSE;
968
969
0
  if (length > UINT32_MAX - 68)
970
0
    return FALSE;
971
972
0
  Stream_Write_UINT32(output, (UINT32)(68 + length));                        /* Length */
973
0
  Stream_Write_UINT32(output, 0);                                            /* NextEntryOffset */
974
0
  Stream_Write_UINT32(output, 0);                                            /* FileIndex */
975
0
  Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
976
0
  Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
977
0
  Stream_Write_UINT32(output,
978
0
                      file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
979
0
  Stream_Write_UINT32(output,
980
0
                      file->find_data.ftLastAccessTime.dwHighDateTime);       /* LastAccessTime */
981
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
982
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
983
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);  /* ChangeTime */
984
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
985
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeLow);                   /* EndOfFile */
986
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);                  /* EndOfFile */
987
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeLow);     /* AllocationSize */
988
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);    /* AllocationSize */
989
0
  Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
990
0
  Stream_Write_UINT32(output, (UINT32)length);                   /* FileNameLength */
991
0
  Stream_Write_UINT32(output, 0);                                /* EaSize */
992
0
  Stream_Write(output, file->find_data.cFileName, length);
993
0
  return TRUE;
994
0
}
995
996
WINPR_ATTR_NODISCARD
997
static BOOL drive_file_query_both_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
998
0
{
999
0
  WINPR_ASSERT(file);
1000
0
  WINPR_ASSERT(output);
1001
  /* http://msdn.microsoft.com/en-us/library/cc232095.aspx */
1002
0
  if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
1003
0
    return FALSE;
1004
1005
0
  if (length > UINT32_MAX - 93)
1006
0
    return FALSE;
1007
1008
0
  Stream_Write_UINT32(output, (UINT32)(93 + length));                        /* Length */
1009
0
  Stream_Write_UINT32(output, 0);                                            /* NextEntryOffset */
1010
0
  Stream_Write_UINT32(output, 0);                                            /* FileIndex */
1011
0
  Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
1012
0
  Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
1013
0
  Stream_Write_UINT32(output,
1014
0
                      file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
1015
0
  Stream_Write_UINT32(output,
1016
0
                      file->find_data.ftLastAccessTime.dwHighDateTime);       /* LastAccessTime */
1017
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
1018
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
1019
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime);  /* ChangeTime */
1020
0
  Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
1021
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeLow);                   /* EndOfFile */
1022
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);                  /* EndOfFile */
1023
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeLow);     /* AllocationSize */
1024
0
  Stream_Write_UINT32(output, file->find_data.nFileSizeHigh);    /* AllocationSize */
1025
0
  Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
1026
0
  Stream_Write_UINT32(output, (UINT32)length);                   /* FileNameLength */
1027
0
  Stream_Write_UINT32(output, 0);                                /* EaSize */
1028
0
  Stream_Write_UINT8(output, 0);                                 /* ShortNameLength */
1029
  /* Reserved(1), MUST NOT be added! */
1030
0
  Stream_Zero(output, 24); /* ShortName */
1031
0
  Stream_Write(output, file->find_data.cFileName, length);
1032
0
  return TRUE;
1033
0
}
1034
1035
WINPR_ATTR_NODISCARD
1036
static BOOL drive_file_query_names_info(DRIVE_FILE* file, wStream* output, size_t length)
1037
0
{
1038
0
  WINPR_ASSERT(file);
1039
0
  WINPR_ASSERT(output);
1040
  /* http://msdn.microsoft.com/en-us/library/cc232077.aspx */
1041
0
  if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1042
0
    return FALSE;
1043
1044
0
  if (length > UINT32_MAX - 12)
1045
0
    return FALSE;
1046
1047
0
  Stream_Write_UINT32(output, (UINT32)(12 + length)); /* Length */
1048
0
  Stream_Write_UINT32(output, 0);                     /* NextEntryOffset */
1049
0
  Stream_Write_UINT32(output, 0);                     /* FileIndex */
1050
0
  Stream_Write_UINT32(output, (UINT32)length);        /* FileNameLength */
1051
0
  Stream_Write(output, file->find_data.cFileName, length);
1052
0
  return TRUE;
1053
0
}
1054
1055
BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1056
                                const WCHAR* path, UINT32 PathWCharLength, wStream* output)
1057
0
{
1058
0
  BOOL rc = FALSE;
1059
0
  size_t length = 0;
1060
0
  WCHAR* ent_path = nullptr;
1061
1062
0
  if (!file || !path || !output)
1063
0
    return FALSE;
1064
1065
0
  if (InitialQuery != 0)
1066
0
  {
1067
    /* release search handle */
1068
0
    if (file->find_handle != INVALID_HANDLE_VALUE)
1069
0
      FindClose(file->find_handle);
1070
1071
0
    ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1072
    /* open new search handle and retrieve the first entry */
1073
0
    file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1074
0
    free(ent_path);
1075
1076
0
    if (file->find_handle == INVALID_HANDLE_VALUE)
1077
0
      goto out_fail;
1078
0
  }
1079
0
  else if (!FindNextFileW(file->find_handle, &file->find_data))
1080
0
    goto out_fail;
1081
1082
0
  length = _wcslen(file->find_data.cFileName) * sizeof(WCHAR);
1083
1084
0
  switch (FsInformationClass)
1085
0
  {
1086
0
    case FileDirectoryInformation:
1087
0
      rc = drive_file_query_dir_info(file, output, length);
1088
0
      break;
1089
1090
0
    case FileFullDirectoryInformation:
1091
0
      rc = drive_file_query_full_dir_info(file, output, length);
1092
0
      break;
1093
1094
0
    case FileBothDirectoryInformation:
1095
0
      rc = drive_file_query_both_dir_info(file, output, length);
1096
0
      break;
1097
1098
0
    case FileNamesInformation:
1099
0
      rc = drive_file_query_names_info(file, output, length);
1100
0
      break;
1101
1102
0
    default:
1103
0
      WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
1104
0
                FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1105
      /* Unhandled FsInformationClass */
1106
0
      goto out_fail;
1107
0
  }
1108
1109
0
out_fail:
1110
0
  if (!rc)
1111
0
  {
1112
0
    Stream_Write_UINT32(output, 0); /* Length */
1113
0
    Stream_Write_UINT8(output, 0);  /* Padding */
1114
0
  }
1115
0
  return rc;
1116
0
}