ca68d1d188160c6f5690ab2eeb44409fdf6e6f97
[reactos.git] / reactos / lib / rtl / path.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/rtl/path.c
5 * PURPOSE: Path and current directory functions
6 * PROGRAMMERS: Wine team
7 * Thomas Weidenmueller
8 * Gunnar Dalsnes
9 * Alex Ionescu (alex.ionescu@reactos.org)
10 * Pierre Schweitzer (pierre@reactos.org)
11 */
12
13 /* INCLUDES *****************************************************************/
14
15 #include <rtl.h>
16
17 #define NDEBUG
18 #include <debug.h>
19
20 /* DEFINITONS and MACROS ******************************************************/
21
22 #define MAX_PFX_SIZE 16
23
24 #define IS_PATH_SEPARATOR(x) (((x)==L'\\')||((x)==L'/'))
25
26 #define RTL_CURDIR_IS_REMOVABLE 0x1
27 #define RTL_CURDIR_DROP_OLD_HANDLE 0x2
28 #define RTL_CURDIR_ALL_FLAGS (RTL_CURDIR_DROP_OLD_HANDLE | RTL_CURDIR_IS_REMOVABLE) // 0x3
29 C_ASSERT(RTL_CURDIR_ALL_FLAGS == OBJ_HANDLE_TAGBITS);
30
31
32 /* GLOBALS ********************************************************************/
33
34 static const WCHAR DeviceRootW[] = L"\\\\.\\";
35 const UNICODE_STRING DeviceRootString = RTL_CONSTANT_STRING(L"\\\\.\\");
36
37 const UNICODE_STRING RtlpDosDevicesUncPrefix = RTL_CONSTANT_STRING(L"\\??\\UNC\\");
38 const UNICODE_STRING RtlpWin32NtRootSlash = RTL_CONSTANT_STRING(L"\\\\?\\");
39 const UNICODE_STRING RtlpDosSlashCONDevice = RTL_CONSTANT_STRING(L"\\\\.\\CON");
40 const UNICODE_STRING RtlpDosDevicesPrefix = RTL_CONSTANT_STRING(L"\\??\\");
41
42 const UNICODE_STRING RtlpDosLPTDevice = RTL_CONSTANT_STRING(L"LPT");
43 const UNICODE_STRING RtlpDosCOMDevice = RTL_CONSTANT_STRING(L"COM");
44 const UNICODE_STRING RtlpDosPRNDevice = RTL_CONSTANT_STRING(L"PRN");
45 const UNICODE_STRING RtlpDosAUXDevice = RTL_CONSTANT_STRING(L"AUX");
46 const UNICODE_STRING RtlpDosCONDevice = RTL_CONSTANT_STRING(L"CON");
47 const UNICODE_STRING RtlpDosNULDevice = RTL_CONSTANT_STRING(L"NUL");
48
49 PRTLP_CURDIR_REF RtlpCurDirRef;
50
51 /* PRIVATE FUNCTIONS **********************************************************/
52
53 RTL_PATH_TYPE
54 NTAPI
55 RtlDetermineDosPathNameType_Ustr(IN PCUNICODE_STRING PathString)
56 {
57 PWCHAR Path;
58 ULONG Chars;
59
60 Path = PathString->Buffer;
61 Chars = PathString->Length / sizeof(WCHAR);
62
63 /* Return if there are no characters */
64 if (!Chars) return RtlPathTypeRelative;
65
66 /*
67 * The algorithm is similar to RtlDetermineDosPathNameType_U but here we
68 * actually check for the path length before touching the characters
69 */
70 if (IS_PATH_SEPARATOR(Path[0]))
71 {
72 if ((Chars < 2) || !(IS_PATH_SEPARATOR(Path[1]))) return RtlPathTypeRooted; /* \x */
73 if ((Chars < 3) || ((Path[2] != L'.') && (Path[2] != L'?'))) return RtlPathTypeUncAbsolute;/* \\x */
74 if ((Chars >= 4) && (IS_PATH_SEPARATOR(Path[3]))) return RtlPathTypeLocalDevice; /* \\.\x or \\?\x */
75 if (Chars != 3) return RtlPathTypeUncAbsolute; /* \\.x or \\?x */
76 return RtlPathTypeRootLocalDevice; /* \\. or \\? */
77 }
78 else
79 {
80 if ((Chars < 2) || (Path[1] != L':')) return RtlPathTypeRelative; /* x */
81 if ((Chars < 3) || !(IS_PATH_SEPARATOR(Path[2]))) return RtlPathTypeDriveRelative; /* x: */
82 return RtlPathTypeDriveAbsolute; /* x:\ */
83 }
84 }
85
86 ULONG
87 NTAPI
88 RtlIsDosDeviceName_Ustr(IN PCUNICODE_STRING PathString)
89 {
90 UNICODE_STRING PathCopy;
91 PWCHAR Start, End;
92 USHORT PathChars, ColonCount = 0;
93 USHORT ReturnOffset = 0, ReturnLength, OriginalLength;
94 WCHAR c;
95
96 /* Validate the input */
97 if (!PathString) return 0;
98
99 /* Check what type of path this is */
100 switch (RtlDetermineDosPathNameType_Ustr(PathString))
101 {
102 /* Fail for UNC or unknown paths */
103 case RtlPathTypeUnknown:
104 case RtlPathTypeUncAbsolute:
105 return 0;
106
107 /* Make special check for the CON device */
108 case RtlPathTypeLocalDevice:
109 if (RtlEqualUnicodeString(PathString, &RtlpDosSlashCONDevice, TRUE))
110 {
111 /* This should return 0x80006 */
112 return MAKELONG(RtlpDosCONDevice.Length, DeviceRootString.Length);
113 }
114 return 0;
115
116 default:
117 break;
118 }
119
120 /* Make a copy of the string */
121 PathCopy = *PathString;
122 OriginalLength = PathString->Length;
123
124 /* Return if there's no characters */
125 PathChars = PathCopy.Length / sizeof(WCHAR);
126 if (!PathChars) return 0;
127
128 /* Check for drive path and truncate */
129 if (PathCopy.Buffer[PathChars - 1] == L':')
130 {
131 /* Fixup the lengths */
132 PathCopy.Length -= sizeof(WCHAR);
133 if (!--PathChars) return 0;
134
135 /* Remember this for later */
136 ColonCount = 1;
137 }
138
139 /* Check for extension or space, and truncate */
140 do
141 {
142 /* Stop if we hit something else than a space or period */
143 c = PathCopy.Buffer[PathChars - 1];
144 if ((c != L'.') && (c != L' ')) break;
145
146 /* Fixup the lengths */
147 PathCopy.Length -= sizeof(WCHAR);
148
149 /* Remember this for later */
150 ColonCount++;
151 } while (--PathChars);
152
153 /* Anything still left? */
154 if (PathChars)
155 {
156 /* Loop from the end */
157 for (End = &PathCopy.Buffer[PathChars - 1];
158 End >= PathCopy.Buffer;
159 --End)
160 {
161 /* Check if the character is a path or drive separator */
162 c = *End;
163 if (IS_PATH_SEPARATOR(c) || ((c == L':') && (End == PathCopy.Buffer + 1)))
164 {
165 /* Get the next lower case character */
166 End++;
167 c = RtlDowncaseUnicodeChar(*End);
168
169 /* Check if it's a DOS device (LPT, COM, PRN, AUX, or NUL) */
170 if ((End < &PathCopy.Buffer[OriginalLength / sizeof(WCHAR)]) &&
171 ((c == L'l') || (c == L'c') || (c == L'p') || (c == L'a') || (c == L'n')))
172 {
173 /* Calculate the offset */
174 ReturnOffset = (USHORT)((PCHAR)End - (PCHAR)PathCopy.Buffer);
175
176 /* Build the final string */
177 PathCopy.Length = OriginalLength - ReturnOffset - (ColonCount * sizeof(WCHAR));
178 PathCopy.Buffer = End;
179
180 /* Save new amount of chars in the path */
181 PathChars = PathCopy.Length / sizeof(WCHAR);
182
183 break;
184 }
185 else
186 {
187 return 0;
188 }
189 }
190 }
191
192 /* Get the next lower case character and check if it's a DOS device */
193 c = RtlDowncaseUnicodeChar(*PathCopy.Buffer);
194 if ((c != L'l') && (c != L'c') && (c != L'p') && (c != L'a') && (c != L'n'))
195 {
196 /* Not LPT, COM, PRN, AUX, or NUL */
197 return 0;
198 }
199 }
200
201 /* Now skip past any extra extension or drive letter characters */
202 Start = PathCopy.Buffer;
203 End = &Start[PathChars];
204 while (Start < End)
205 {
206 c = *Start;
207 if ((c == L'.') || (c == L':')) break;
208 Start++;
209 }
210
211 /* And then go backwards to get rid of spaces */
212 while ((Start > PathCopy.Buffer) && (Start[-1] == L' ')) --Start;
213
214 /* Finally see how many characters are left, and that's our size */
215 PathChars = (USHORT)(Start - PathCopy.Buffer);
216 PathCopy.Length = PathChars * sizeof(WCHAR);
217
218 /* Check if this is a COM or LPT port, which has a digit after it */
219 if ((PathChars == 4) &&
220 (iswdigit(PathCopy.Buffer[3]) && (PathCopy.Buffer[3] != L'0')))
221 {
222 /* Don't compare the number part, just check for LPT or COM */
223 PathCopy.Length -= sizeof(WCHAR);
224 if ((RtlEqualUnicodeString(&PathCopy, &RtlpDosLPTDevice, TRUE)) ||
225 (RtlEqualUnicodeString(&PathCopy, &RtlpDosCOMDevice, TRUE)))
226 {
227 /* Found it */
228 ReturnLength = sizeof(L"COM1") - sizeof(WCHAR);
229 return MAKELONG(ReturnLength, ReturnOffset);
230 }
231 }
232 else if ((PathChars == 3) &&
233 ((RtlEqualUnicodeString(&PathCopy, &RtlpDosPRNDevice, TRUE)) ||
234 (RtlEqualUnicodeString(&PathCopy, &RtlpDosAUXDevice, TRUE)) ||
235 (RtlEqualUnicodeString(&PathCopy, &RtlpDosNULDevice, TRUE)) ||
236 (RtlEqualUnicodeString(&PathCopy, &RtlpDosCONDevice, TRUE))))
237 {
238 /* Otherwise this was something like AUX, NUL, PRN, or CON */
239 ReturnLength = sizeof(L"AUX") - sizeof(WCHAR);
240 return MAKELONG(ReturnLength, ReturnOffset);
241 }
242
243 /* Otherwise, this is not a valid DOS device */
244 return 0;
245 }
246
247 NTSTATUS
248 NTAPI
249 RtlpCheckDeviceName(IN PUNICODE_STRING FileName,
250 IN ULONG Length,
251 OUT PBOOLEAN NameInvalid)
252 {
253 PWCHAR Buffer;
254 NTSTATUS Status;
255
256 /* Allocate a large enough buffer */
257 Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, FileName->Length);
258 if (Buffer)
259 {
260 /* Assume failure */
261 *NameInvalid = TRUE;
262
263 /* Copy the filename */
264 RtlCopyMemory(Buffer, FileName->Buffer, FileName->Length);
265
266 /* And add a dot at the end */
267 Buffer[Length / sizeof(WCHAR)] = L'.';
268 Buffer[(Length / sizeof(WCHAR)) + 1] = UNICODE_NULL;
269
270 /* Check if the file exists or not */
271 *NameInvalid = RtlDoesFileExists_U(Buffer) ? FALSE: TRUE;
272
273 /* Get rid of the buffer now */
274 Status = RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
275 }
276 else
277 {
278 /* Assume the name is ok, but fail the call */
279 *NameInvalid = FALSE;
280 Status = STATUS_NO_MEMORY;
281 }
282
283 /* Return the status */
284 return Status;
285 }
286
287
288
289 /******************************************************************
290 * RtlpCollapsePath (from WINE)
291 *
292 * Helper for RtlGetFullPathName_U.
293 * 1) Convert slashes into backslashes
294 * 2) Get rid of duplicate backslashes
295 * 3) Get rid of . and .. components in the path.
296 */
297 static VOID
298 RtlpCollapsePath(PWSTR path, ULONG mark, BOOLEAN SkipTrailingPathSeparators)
299 {
300 PWSTR p, next;
301
302 /* convert every / into a \ */
303 for (p = path; *p; p++)
304 {
305 if (*p == L'/') *p = L'\\';
306 }
307
308 /* collapse duplicate backslashes */
309 next = path + max( 1, mark );
310 for (p = next; *p; p++)
311 {
312 if (*p != L'\\' || next[-1] != L'\\') *next++ = *p;
313 }
314 *next = UNICODE_NULL;
315
316 p = path + mark;
317 while (*p)
318 {
319 if (*p == L'.')
320 {
321 switch(p[1])
322 {
323 case UNICODE_NULL: /* final . */
324 if (p > path + mark) p--;
325 *p = UNICODE_NULL;
326 continue;
327 case L'\\': /* .\ component */
328 next = p + 2;
329 RtlMoveMemory( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
330 continue;
331 case L'.':
332 if (p[2] == L'\\') /* ..\ component */
333 {
334 next = p + 3;
335 if (p > path + mark)
336 {
337 p--;
338 while (p > path + mark && p[-1] != L'\\') p--;
339 }
340 RtlMoveMemory( p, next, (wcslen(next) + 1) * sizeof(WCHAR) );
341 continue;
342 }
343 else if (!p[2]) /* final .. */
344 {
345 if (p > path + mark)
346 {
347 p--;
348 while (p > path + mark && p[-1] != L'\\') p--;
349 if (p > path + mark) p--;
350 }
351 *p = UNICODE_NULL;
352 continue;
353 }
354 break;
355 }
356 }
357 /* skip to the next component */
358 while (*p && *p != L'\\') p++;
359 if (*p == L'\\')
360 {
361 /* remove last dot in previous dir name */
362 if (p > path + mark && p[-1] == L'.')
363 RtlMoveMemory( p-1, p, (wcslen(p) + 1) * sizeof(WCHAR) );
364 else
365 p++;
366 }
367 }
368
369 /* Remove trailing backslashes if needed (after the UNC part if it exists) */
370 if (SkipTrailingPathSeparators)
371 {
372 while (p > path + mark && IS_PATH_SEPARATOR(p[-1])) *p-- = UNICODE_NULL;
373 }
374
375 /* Remove trailing spaces and dots (for all the path) */
376 while (p > path && (p[-1] == L' ' || p[-1] == L'.')) *p-- = UNICODE_NULL;
377
378 /* Null-terminate the string */
379 *p = UNICODE_NULL;
380 }
381
382 /******************************************************************
383 * RtlpSkipUNCPrefix (from WINE)
384 *
385 * Helper for RtlGetFullPathName_U
386 * Skip the \\share\dir part of a file name and return the new position
387 * (which can point on the last backslash of "dir\")
388 */
389 static SIZE_T
390 RtlpSkipUNCPrefix(PCWSTR FileNameBuffer)
391 {
392 PCWSTR UncPath = FileNameBuffer + 2;
393 DPRINT("RtlpSkipUNCPrefix(%S)\n", FileNameBuffer);
394
395 while (*UncPath && !IS_PATH_SEPARATOR(*UncPath)) UncPath++; /* share name */
396 while (IS_PATH_SEPARATOR(*UncPath)) UncPath++;
397 while (*UncPath && !IS_PATH_SEPARATOR(*UncPath)) UncPath++; /* dir name */
398 /* while (IS_PATH_SEPARATOR(*UncPath)) UncPath++; */
399
400 return (UncPath - FileNameBuffer);
401 }
402
403 NTSTATUS
404 NTAPI
405 RtlpApplyLengthFunction(IN ULONG Flags,
406 IN ULONG Type,
407 IN PVOID UnicodeStringOrUnicodeStringBuffer,
408 IN PVOID LengthFunction)
409 {
410 UNIMPLEMENTED;
411 return STATUS_NOT_IMPLEMENTED;
412 }
413
414 NTSTATUS
415 NTAPI
416 RtlGetLengthWithoutLastFullDosOrNtPathElement(IN ULONG Flags,
417 IN PWCHAR Path,
418 OUT PULONG LengthOut)
419 {
420 UNIMPLEMENTED;
421 return STATUS_NOT_IMPLEMENTED;
422 }
423
424 NTSTATUS
425 NTAPI
426 RtlComputePrivatizedDllName_U(IN PUNICODE_STRING DllName,
427 IN PUNICODE_STRING a2,
428 IN PUNICODE_STRING a3)
429 {
430 UNIMPLEMENTED;
431 return STATUS_NOT_IMPLEMENTED;
432 }
433
434 ULONG
435 NTAPI
436 RtlGetFullPathName_Ustr(
437 _In_ PUNICODE_STRING FileName,
438 _In_ ULONG Size,
439 _Out_z_bytecap_(Size) PWSTR Buffer,
440 _Out_opt_ PCWSTR *ShortName,
441 _Out_opt_ PBOOLEAN InvalidName,
442 _Out_ RTL_PATH_TYPE *PathType)
443 {
444 NTSTATUS Status;
445 PWCHAR FileNameBuffer;
446 ULONG FileNameLength, FileNameChars, DosLength, DosLengthOffset, FullLength;
447 BOOLEAN SkipTrailingPathSeparators;
448 WCHAR c;
449
450
451 ULONG reqsize = 0;
452 PCWSTR ptr;
453
454 PCUNICODE_STRING CurDirName;
455 UNICODE_STRING EnvVarName, EnvVarValue;
456 WCHAR EnvVarNameBuffer[4];
457
458 ULONG PrefixCut = 0; // Where the path really starts (after the skipped prefix)
459 PWCHAR Prefix = NULL; // pointer to the string to be inserted as the new path prefix
460 ULONG PrefixLength = 0;
461 PWCHAR Source;
462 ULONG SourceLength;
463
464
465 /* For now, assume the name is valid */
466 DPRINT("Filename: %wZ\n", FileName);
467 DPRINT("Size and buffer: %lx %p\n", Size, Buffer);
468 if (InvalidName) *InvalidName = FALSE;
469
470 /* Handle initial path type and failure case */
471 *PathType = RtlPathTypeUnknown;
472 if ((FileName->Length == 0) || (FileName->Buffer[0] == UNICODE_NULL)) return 0;
473
474 /* Break filename into component parts */
475 FileNameBuffer = FileName->Buffer;
476 FileNameLength = FileName->Length;
477 FileNameChars = FileNameLength / sizeof(WCHAR);
478
479 /* Kill trailing spaces */
480 c = FileNameBuffer[FileNameChars - 1];
481 while ((FileNameLength != 0) && (c == L' '))
482 {
483 /* Keep going, ignoring the spaces */
484 FileNameLength -= sizeof(WCHAR);
485 if (FileNameLength != 0) c = FileNameBuffer[FileNameLength / sizeof(WCHAR) - 1];
486 }
487
488 /* Check if anything is left */
489 if (FileNameLength == 0) return 0;
490
491 /*
492 * Check whether we'll need to skip trailing path separators in the
493 * computed full path name. If the original file name already contained
494 * trailing separators, then we keep them in the full path name. On the
495 * other hand, if the original name didn't contain any trailing separators
496 * then we'll skip it in the full path name.
497 */
498 SkipTrailingPathSeparators = !IS_PATH_SEPARATOR(FileNameBuffer[FileNameChars - 1]);
499
500 /* Check if this is a DOS name */
501 DosLength = RtlIsDosDeviceName_Ustr(FileName);
502 DPRINT("DOS length for filename: %lx %wZ\n", DosLength, FileName);
503 if (DosLength != 0)
504 {
505 /* Zero out the short name */
506 if (ShortName) *ShortName = NULL;
507
508 /* See comment for RtlIsDosDeviceName_Ustr if this is confusing... */
509 DosLengthOffset = HIWORD(DosLength);
510 DosLength = LOWORD(DosLength);
511
512 /* Do we have a DOS length, and does the caller want validity? */
513 if (InvalidName && (DosLengthOffset != 0))
514 {
515 /* Do the check */
516 Status = RtlpCheckDeviceName(FileName, DosLengthOffset, InvalidName);
517
518 /* If the check failed, or the name is invalid, fail here */
519 if (!NT_SUCCESS(Status)) return 0;
520 if (*InvalidName) return 0;
521 }
522
523 /* Add the size of the device root and check if it fits in the size */
524 FullLength = DosLength + DeviceRootString.Length;
525 if (FullLength < Size)
526 {
527 /* Add the device string */
528 RtlMoveMemory(Buffer, DeviceRootString.Buffer, DeviceRootString.Length);
529
530 /* Now add the DOS device name */
531 RtlMoveMemory((PCHAR)Buffer + DeviceRootString.Length,
532 (PCHAR)FileNameBuffer + DosLengthOffset,
533 DosLength);
534
535 /* Null terminate */
536 *(PWCHAR)((ULONG_PTR)Buffer + FullLength) = UNICODE_NULL;
537 return FullLength;
538 }
539
540 /* Otherwise, there's no space, so return the buffer size needed */
541 if ((FullLength + sizeof(UNICODE_NULL)) > UNICODE_STRING_MAX_BYTES) return 0;
542 return FullLength + sizeof(UNICODE_NULL);
543 }
544
545 /* Zero out the destination buffer. FileName must be different from Buffer */
546 RtlZeroMemory(Buffer, Size);
547
548 /* Get the path type */
549 *PathType = RtlDetermineDosPathNameType_U(FileNameBuffer);
550
551
552
553 /**********************************************
554 ** CODE REWRITTEN IS HAPPENING THERE **
555 **********************************************/
556 Source = FileNameBuffer;
557 SourceLength = FileNameLength;
558 EnvVarValue.Buffer = NULL;
559
560 /* Lock the PEB to get the current directory */
561 RtlAcquirePebLock();
562 CurDirName = &NtCurrentPeb()->ProcessParameters->CurrentDirectory.DosPath;
563
564 switch (*PathType)
565 {
566 case RtlPathTypeUncAbsolute: /* \\foo */
567 PrefixCut = RtlpSkipUNCPrefix(FileNameBuffer);
568 break;
569
570 case RtlPathTypeLocalDevice: /* \\.\foo */
571 PrefixCut = 4;
572 break;
573
574 case RtlPathTypeDriveAbsolute: /* c:\foo */
575 ASSERT(FileNameBuffer[1] == L':');
576 ASSERT(IS_PATH_SEPARATOR(FileNameBuffer[2]));
577
578 Prefix = FileNameBuffer;
579 PrefixLength = 3 * sizeof(WCHAR);
580 Source += 3;
581 SourceLength -= 3 * sizeof(WCHAR);
582
583 PrefixCut = 3;
584 break;
585
586 case RtlPathTypeDriveRelative: /* c:foo */
587 Source += 2;
588 SourceLength -= 2 * sizeof(WCHAR);
589 if (RtlUpcaseUnicodeChar(FileNameBuffer[0]) != RtlUpcaseUnicodeChar(CurDirName->Buffer[0]) ||
590 CurDirName->Buffer[1] != L':')
591 {
592 EnvVarNameBuffer[0] = L'=';
593 EnvVarNameBuffer[1] = FileNameBuffer[0];
594 EnvVarNameBuffer[2] = L':';
595 EnvVarNameBuffer[3] = UNICODE_NULL;
596
597 EnvVarName.Length = 3 * sizeof(WCHAR);
598 EnvVarName.MaximumLength = EnvVarName.Length + sizeof(WCHAR);
599 EnvVarName.Buffer = EnvVarNameBuffer;
600
601 // FIXME: Is it possible to use the user-given buffer ?
602 // RtlInitEmptyUnicodeString(&EnvVarValue, NULL, Size);
603 EnvVarValue.Length = 0;
604 EnvVarValue.MaximumLength = (USHORT)Size;
605 EnvVarValue.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
606 if (EnvVarValue.Buffer == NULL)
607 {
608 Prefix = NULL;
609 PrefixLength = 0;
610 goto Quit;
611 }
612
613 Status = RtlQueryEnvironmentVariable_U(NULL, &EnvVarName, &EnvVarValue);
614 switch (Status)
615 {
616 case STATUS_SUCCESS:
617 /*
618 * (From Wine)
619 * FIXME: Win2k seems to check that the environment
620 * variable actually points to an existing directory.
621 * If not, root of the drive is used (this seems also
622 * to be the only spot in RtlGetFullPathName that the
623 * existence of a part of a path is checked).
624 */
625 EnvVarValue.Buffer[EnvVarValue.Length / sizeof(WCHAR)] = L'\\';
626 Prefix = EnvVarValue.Buffer;
627 PrefixLength = EnvVarValue.Length + sizeof(WCHAR); /* Append trailing '\\' */
628 break;
629
630 case STATUS_BUFFER_TOO_SMALL:
631 reqsize = EnvVarValue.Length + SourceLength + sizeof(UNICODE_NULL);
632 goto Quit;
633
634 default:
635 DPRINT1("RtlQueryEnvironmentVariable_U returned 0x%08lx\n", Status);
636
637 EnvVarNameBuffer[0] = FileNameBuffer[0];
638 EnvVarNameBuffer[1] = L':';
639 EnvVarNameBuffer[2] = L'\\';
640 EnvVarNameBuffer[3] = UNICODE_NULL;
641 Prefix = EnvVarNameBuffer;
642 PrefixLength = 3 * sizeof(WCHAR);
643
644 RtlFreeHeap(RtlGetProcessHeap(), 0, EnvVarValue.Buffer);
645 EnvVarValue.Buffer = NULL;
646 break;
647 }
648 PrefixCut = 3;
649 break;
650 }
651 /* Fall through */
652 DPRINT("RtlPathTypeDriveRelative - Using fall-through to RtlPathTypeRelative\n");
653
654 case RtlPathTypeRelative: /* foo */
655 Prefix = CurDirName->Buffer;
656 PrefixLength = CurDirName->Length;
657 if (CurDirName->Buffer[1] != L':')
658 {
659 PrefixCut = RtlpSkipUNCPrefix(CurDirName->Buffer);
660 }
661 else
662 {
663 PrefixCut = 3;
664 }
665 break;
666
667 case RtlPathTypeRooted: /* \xxx */
668 if (CurDirName->Buffer[1] == L':')
669 {
670 // The path starts with "C:\"
671 ASSERT(CurDirName->Buffer[1] == L':');
672 ASSERT(IS_PATH_SEPARATOR(CurDirName->Buffer[2]));
673
674 Prefix = CurDirName->Buffer;
675 PrefixLength = 3 * sizeof(WCHAR); // Skip "C:\"
676
677 PrefixCut = 3; // Source index location incremented of + 3
678 }
679 else
680 {
681 PrefixCut = RtlpSkipUNCPrefix(CurDirName->Buffer);
682 PrefixLength = PrefixCut * sizeof(WCHAR);
683 Prefix = CurDirName->Buffer;
684 }
685 break;
686
687 case RtlPathTypeRootLocalDevice: /* \\. */
688 Prefix = DeviceRootString.Buffer;
689 PrefixLength = DeviceRootString.Length;
690 Source += 3;
691 SourceLength -= 3 * sizeof(WCHAR);
692
693 PrefixCut = 4;
694 break;
695
696 case RtlPathTypeUnknown:
697 goto Quit;
698 }
699
700 /* Do we have enough space for storing the full path? */
701 reqsize = PrefixLength;
702 if (reqsize + SourceLength + sizeof(WCHAR) > Size)
703 {
704 /* Not enough space, return needed size (including terminating '\0') */
705 reqsize += SourceLength + sizeof(WCHAR);
706 goto Quit;
707 }
708
709 /*
710 * Build the full path
711 */
712 // if (ShortName) DPRINT1("buffer(1) = %S\n", Buffer);
713 /* Copy the path's prefix */
714 if (PrefixLength) RtlMoveMemory(Buffer, Prefix, PrefixLength);
715 // if (ShortName) DPRINT1("buffer(2) = %S\n", Buffer);
716 /* Copy the remaining part of the path */
717 RtlMoveMemory(Buffer + PrefixLength / sizeof(WCHAR), Source, SourceLength + sizeof(WCHAR));
718 // if (ShortName) DPRINT1("buffer(3) = %S\n", Buffer);
719
720 /* Some cleanup */
721 Prefix = NULL;
722 if (EnvVarValue.Buffer)
723 {
724 RtlFreeHeap(RtlGetProcessHeap(), 0, EnvVarValue.Buffer);
725 EnvVarValue.Buffer = NULL;
726 }
727
728 /*
729 * Finally, put the path in canonical form,
730 * i.e. simplify redundant . and .., etc...
731 */
732 // if (*PathType == RtlPathTypeUncAbsolute) DPRINT1("RtlpCollapsePath('%S', %lu)\n", Buffer, PrefixCut);
733 RtlpCollapsePath(Buffer, PrefixCut, SkipTrailingPathSeparators);
734 // if (ShortName) DPRINT1("buffer(4) = %S\n", Buffer);
735
736 /* Get the length of the full path name, without its terminating null character */
737 reqsize = wcslen(Buffer) * sizeof(WCHAR);
738
739 /* Find the file part, which is present after the last path separator */
740 if (ShortName)
741 {
742 ptr = wcsrchr(Buffer, L'\\');
743 if (ptr) ++ptr; // Skip it
744
745 /*
746 * For UNC paths, the file part is after the \\share\dir part of the path.
747 */
748 PrefixCut = (*PathType == RtlPathTypeUncAbsolute ? PrefixCut : 3);
749
750 if (ptr && *ptr && (ptr >= Buffer + PrefixCut))
751 {
752 *ShortName = ptr;
753 }
754 else
755 {
756 /* Zero-out the short name */
757 *ShortName = NULL;
758 }
759 }
760
761 Quit:
762 /* Release PEB lock */
763 RtlReleasePebLock();
764 return (ULONG)reqsize;
765 }
766
767 NTSTATUS
768 NTAPI
769 RtlpWin32NTNameToNtPathName_U(IN PUNICODE_STRING DosPath,
770 OUT PUNICODE_STRING NtPath,
771 OUT PCWSTR *PartName,
772 OUT PRTL_RELATIVE_NAME_U RelativeName)
773 {
774 ULONG DosLength;
775 PWSTR NewBuffer, p;
776
777 /* Validate the input */
778 if (!DosPath) return STATUS_OBJECT_NAME_INVALID;
779
780 /* Validate the DOS length */
781 DosLength = DosPath->Length;
782 if (DosLength >= UNICODE_STRING_MAX_BYTES) return STATUS_NAME_TOO_LONG;
783
784 /* Make space for the new path */
785 NewBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
786 0,
787 DosLength + sizeof(UNICODE_NULL));
788 if (!NewBuffer) return STATUS_NO_MEMORY;
789
790 /* Copy the prefix, and then the rest of the DOS path, and NULL-terminate */
791 RtlCopyMemory(NewBuffer, RtlpDosDevicesPrefix.Buffer, RtlpDosDevicesPrefix.Length);
792 RtlCopyMemory((PCHAR)NewBuffer + RtlpDosDevicesPrefix.Length,
793 DosPath->Buffer + RtlpDosDevicesPrefix.Length / sizeof(WCHAR),
794 DosPath->Length - RtlpDosDevicesPrefix.Length);
795 NewBuffer[DosLength / sizeof(WCHAR)] = UNICODE_NULL;
796
797 /* Did the caller send a relative name? */
798 if (RelativeName)
799 {
800 /* Zero initialize it */
801 RtlInitEmptyUnicodeString(&RelativeName->RelativeName, NULL, 0);
802 RelativeName->ContainingDirectory = NULL;
803 RelativeName->CurDirRef = 0;
804 }
805
806 /* Did the caller request a partial name? */
807 if (PartName)
808 {
809 /* Loop from the back until we find a path separator */
810 p = &NewBuffer[DosLength / sizeof(WCHAR)];
811 while (--p > NewBuffer)
812 {
813 /* We found a path separator, move past it */
814 if (*p == OBJ_NAME_PATH_SEPARATOR)
815 {
816 ++p;
817 break;
818 }
819 }
820
821 /* Check whether a separator was found and if something remains */
822 if ((p > NewBuffer) && *p)
823 {
824 /* What follows the path separator is the partial name */
825 *PartName = p;
826 }
827 else
828 {
829 /* The path ends with a path separator, no partial name */
830 *PartName = NULL;
831 }
832 }
833
834 /* Build the final NT path string */
835 NtPath->Buffer = NewBuffer;
836 NtPath->Length = (USHORT)DosLength;
837 NtPath->MaximumLength = (USHORT)DosLength + sizeof(UNICODE_NULL);
838 return STATUS_SUCCESS;
839 }
840
841 NTSTATUS
842 NTAPI
843 RtlpDosPathNameToRelativeNtPathName_Ustr(IN BOOLEAN HaveRelative,
844 IN PCUNICODE_STRING DosName,
845 OUT PUNICODE_STRING NtName,
846 OUT PCWSTR *PartName,
847 OUT PRTL_RELATIVE_NAME_U RelativeName)
848 {
849 WCHAR BigBuffer[MAX_PATH + 1];
850 PWCHAR PrefixBuffer, NewBuffer, Buffer;
851 ULONG MaxLength, PathLength, PrefixLength, PrefixCut, LengthChars, Length;
852 UNICODE_STRING CapturedDosName, PartNameString, FullPath;
853 BOOLEAN QuickPath;
854 RTL_PATH_TYPE InputPathType, BufferPathType;
855 NTSTATUS Status;
856 BOOLEAN NameInvalid;
857 PCURDIR CurrentDirectory;
858
859 /* Assume MAX_PATH for now */
860 DPRINT("Relative: %lx DosName: %wZ NtName: %p, PartName: %p, RelativeName: %p\n",
861 HaveRelative, DosName, NtName, PartName, RelativeName);
862 MaxLength = sizeof(BigBuffer);
863
864 /* Validate the input */
865 if (!DosName) return STATUS_OBJECT_NAME_INVALID;
866
867 /* Capture input string */
868 CapturedDosName = *DosName;
869
870 /* Check for the presence or absence of the NT prefix "\\?\" form */
871 // if (!RtlPrefixUnicodeString(&RtlpWin32NtRootSlash, &CapturedDosName, FALSE))
872 if ((CapturedDosName.Length <= RtlpWin32NtRootSlash.Length) ||
873 (CapturedDosName.Buffer[0] != RtlpWin32NtRootSlash.Buffer[0]) ||
874 (CapturedDosName.Buffer[1] != RtlpWin32NtRootSlash.Buffer[1]) ||
875 (CapturedDosName.Buffer[2] != RtlpWin32NtRootSlash.Buffer[2]) ||
876 (CapturedDosName.Buffer[3] != RtlpWin32NtRootSlash.Buffer[3]))
877 {
878 /* NT prefix not present */
879
880 /* Quick path won't be used */
881 QuickPath = FALSE;
882
883 /* Use the static buffer */
884 Buffer = BigBuffer;
885 MaxLength += RtlpDosDevicesUncPrefix.Length;
886
887 /* Allocate a buffer to hold the path */
888 NewBuffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, MaxLength);
889 DPRINT("MaxLength: %lx\n", MaxLength);
890 if (!NewBuffer) return STATUS_NO_MEMORY;
891 }
892 else
893 {
894 /* NT prefix present */
895
896 /* Use the optimized path after acquiring the lock */
897 QuickPath = TRUE;
898 NewBuffer = NULL;
899 }
900
901 /* Lock the PEB and check if the quick path can be used */
902 RtlAcquirePebLock();
903 if (QuickPath)
904 {
905 /* Some simple fixups will get us the correct path */
906 DPRINT("Quick path\n");
907 Status = RtlpWin32NTNameToNtPathName_U(&CapturedDosName,
908 NtName,
909 PartName,
910 RelativeName);
911
912 /* Release the lock, we're done here */
913 RtlReleasePebLock();
914 return Status;
915 }
916
917 /* Call the main function to get the full path name and length */
918 PathLength = RtlGetFullPathName_Ustr(&CapturedDosName,
919 MAX_PATH * sizeof(WCHAR),
920 Buffer,
921 PartName,
922 &NameInvalid,
923 &InputPathType);
924 if ((NameInvalid) || !(PathLength) || (PathLength > (MAX_PATH * sizeof(WCHAR))))
925 {
926 /* Invalid name, fail */
927 DPRINT("Invalid name: %lx Path Length: %lx\n", NameInvalid, PathLength);
928 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBuffer);
929 RtlReleasePebLock();
930 return STATUS_OBJECT_NAME_INVALID;
931 }
932
933 /* Start by assuming the path starts with \??\ (DOS Devices Path) */
934 PrefixLength = RtlpDosDevicesPrefix.Length;
935 PrefixBuffer = RtlpDosDevicesPrefix.Buffer;
936 PrefixCut = 0;
937
938 /* Check where it really is */
939 BufferPathType = RtlDetermineDosPathNameType_U(Buffer);
940 DPRINT("Buffer: %S Type: %lx\n", Buffer, BufferPathType);
941 switch (BufferPathType)
942 {
943 /* It's actually a UNC path in \??\UNC\ */
944 case RtlPathTypeUncAbsolute:
945 PrefixLength = RtlpDosDevicesUncPrefix.Length;
946 PrefixBuffer = RtlpDosDevicesUncPrefix.Buffer;
947 PrefixCut = 2;
948 break;
949
950 case RtlPathTypeLocalDevice:
951 /* We made a good guess, go with it but skip the \??\ */
952 PrefixCut = 4;
953 break;
954
955 case RtlPathTypeDriveAbsolute:
956 case RtlPathTypeDriveRelative:
957 case RtlPathTypeRooted:
958 case RtlPathTypeRelative:
959 /* Our guess was good, roll with it */
960 break;
961
962 /* Nothing else is expected */
963 default:
964 ASSERT(FALSE);
965 }
966
967 /* Now copy the prefix and the buffer */
968 RtlCopyMemory(NewBuffer, PrefixBuffer, PrefixLength);
969 RtlCopyMemory((PCHAR)NewBuffer + PrefixLength,
970 Buffer + PrefixCut,
971 PathLength - (PrefixCut * sizeof(WCHAR)));
972
973 /* Compute the length */
974 Length = PathLength + PrefixLength - PrefixCut * sizeof(WCHAR);
975 LengthChars = Length / sizeof(WCHAR);
976
977 /* Setup the actual NT path string and terminate it */
978 NtName->Buffer = NewBuffer;
979 NtName->Length = (USHORT)Length;
980 NtName->MaximumLength = (USHORT)MaxLength;
981 NewBuffer[LengthChars] = UNICODE_NULL;
982 DPRINT("New buffer: %S\n", NewBuffer);
983 DPRINT("NT Name: %wZ\n", NtName);
984
985 /* Check if a partial name was requested */
986 if ((PartName) && (*PartName))
987 {
988 /* Convert to Unicode */
989 Status = RtlInitUnicodeStringEx(&PartNameString, *PartName);
990 if (NT_SUCCESS(Status))
991 {
992 /* Set the partial name */
993 *PartName = &NewBuffer[LengthChars - (PartNameString.Length / sizeof(WCHAR))];
994 }
995 else
996 {
997 /* Fail */
998 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBuffer);
999 RtlReleasePebLock();
1000 return Status;
1001 }
1002 }
1003
1004 /* Check if a relative name was asked for */
1005 if (RelativeName)
1006 {
1007 /* Setup the structure */
1008 RtlInitEmptyUnicodeString(&RelativeName->RelativeName, NULL, 0);
1009 RelativeName->ContainingDirectory = NULL;
1010 RelativeName->CurDirRef = NULL;
1011
1012 /* Check if the input path itself was relative */
1013 if (InputPathType == RtlPathTypeRelative)
1014 {
1015 /* Get current directory */
1016 CurrentDirectory = &(NtCurrentPeb()->ProcessParameters->CurrentDirectory);
1017 if (CurrentDirectory->Handle)
1018 {
1019 Status = RtlInitUnicodeStringEx(&FullPath, Buffer);
1020 if (!NT_SUCCESS(Status))
1021 {
1022 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBuffer);
1023 RtlReleasePebLock();
1024 return Status;
1025 }
1026
1027 /* If current directory is bigger than full path, there's no way */
1028 if (CurrentDirectory->DosPath.Length > FullPath.Length)
1029 {
1030 RtlReleasePebLock();
1031 return Status;
1032 }
1033
1034 /* File is in current directory */
1035 if (RtlEqualUnicodeString(&FullPath, &CurrentDirectory->DosPath, TRUE))
1036 {
1037 /* Make relative name string */
1038 RelativeName->RelativeName.Buffer = (PWSTR)((ULONG_PTR)NewBuffer + PrefixLength + FullPath.Length - PrefixCut * sizeof(WCHAR));
1039 RelativeName->RelativeName.Length = (USHORT)(PathLength - FullPath.Length);
1040 /* If relative name starts with \, skip it */
1041 if (RelativeName->RelativeName.Buffer[0] == OBJ_NAME_PATH_SEPARATOR)
1042 {
1043 RelativeName->RelativeName.Buffer++;
1044 RelativeName->RelativeName.Length -= sizeof(WCHAR);
1045 }
1046 RelativeName->RelativeName.MaximumLength = RelativeName->RelativeName.Length;
1047 DPRINT("RelativeName: %wZ\n", &(RelativeName->RelativeName));
1048
1049 if (!HaveRelative)
1050 {
1051 RelativeName->ContainingDirectory = CurrentDirectory->Handle;
1052 return Status;
1053 }
1054
1055 /* Give back current directory data & reference counter */
1056 RelativeName->CurDirRef = RtlpCurDirRef;
1057 if (RelativeName->CurDirRef)
1058 {
1059 InterlockedIncrement(&RtlpCurDirRef->RefCount);
1060 }
1061
1062 RelativeName->ContainingDirectory = CurrentDirectory->Handle;
1063 }
1064 }
1065 }
1066 }
1067
1068 /* Done */
1069 RtlReleasePebLock();
1070 return STATUS_SUCCESS;
1071 }
1072
1073 NTSTATUS
1074 NTAPI
1075 RtlpDosPathNameToRelativeNtPathName_U(IN BOOLEAN HaveRelative,
1076 IN PCWSTR DosName,
1077 OUT PUNICODE_STRING NtName,
1078 OUT PCWSTR *PartName,
1079 OUT PRTL_RELATIVE_NAME_U RelativeName)
1080 {
1081 NTSTATUS Status;
1082 UNICODE_STRING NameString;
1083
1084 /* Create the unicode name */
1085 Status = RtlInitUnicodeStringEx(&NameString, DosName);
1086 if (NT_SUCCESS(Status))
1087 {
1088 /* Call the unicode function */
1089 Status = RtlpDosPathNameToRelativeNtPathName_Ustr(HaveRelative,
1090 &NameString,
1091 NtName,
1092 PartName,
1093 RelativeName);
1094 }
1095
1096 /* Return status */
1097 return Status;
1098 }
1099
1100 BOOLEAN
1101 NTAPI
1102 RtlDosPathNameToRelativeNtPathName_Ustr(IN PCUNICODE_STRING DosName,
1103 OUT PUNICODE_STRING NtName,
1104 OUT PCWSTR *PartName,
1105 OUT PRTL_RELATIVE_NAME_U RelativeName)
1106 {
1107 /* Call the internal function */
1108 ASSERT(RelativeName);
1109 return NT_SUCCESS(RtlpDosPathNameToRelativeNtPathName_Ustr(TRUE,
1110 DosName,
1111 NtName,
1112 PartName,
1113 RelativeName));
1114 }
1115
1116 BOOLEAN
1117 NTAPI
1118 RtlDoesFileExists_UstrEx(IN PCUNICODE_STRING FileName,
1119 IN BOOLEAN SucceedIfBusy)
1120 {
1121 BOOLEAN Result;
1122 RTL_RELATIVE_NAME_U RelativeName;
1123 UNICODE_STRING NtPathName;
1124 PVOID Buffer;
1125 OBJECT_ATTRIBUTES ObjectAttributes;
1126 NTSTATUS Status;
1127 FILE_BASIC_INFORMATION BasicInformation;
1128
1129 /* Get the NT Path */
1130 Result = RtlDosPathNameToRelativeNtPathName_Ustr(FileName,
1131 &NtPathName,
1132 NULL,
1133 &RelativeName);
1134 if (!Result) return FALSE;
1135
1136 /* Save the buffer */
1137 Buffer = NtPathName.Buffer;
1138
1139 /* Check if we have a relative name */
1140 if (RelativeName.RelativeName.Length)
1141 {
1142 /* Use it */
1143 NtPathName = RelativeName.RelativeName;
1144 }
1145 else
1146 {
1147 /* Otherwise ignore it */
1148 RelativeName.ContainingDirectory = NULL;
1149 }
1150
1151 /* Initialize the object attributes */
1152 InitializeObjectAttributes(&ObjectAttributes,
1153 &NtPathName,
1154 OBJ_CASE_INSENSITIVE,
1155 RelativeName.ContainingDirectory,
1156 NULL);
1157
1158 /* Query the attributes and free the buffer now */
1159 Status = ZwQueryAttributesFile(&ObjectAttributes, &BasicInformation);
1160 RtlReleaseRelativeName(&RelativeName);
1161 RtlFreeHeap(RtlGetProcessHeap(), 0, Buffer);
1162
1163 /* Check if we failed */
1164 if (!NT_SUCCESS(Status))
1165 {
1166 /* Check if we failed because the file is in use */
1167 if ((Status == STATUS_SHARING_VIOLATION) ||
1168 (Status == STATUS_ACCESS_DENIED))
1169 {
1170 /* Check if the caller wants this to be considered OK */
1171 Result = SucceedIfBusy ? TRUE : FALSE;
1172 }
1173 else
1174 {
1175 /* A failure because the file didn't exist */
1176 Result = FALSE;
1177 }
1178 }
1179 else
1180 {
1181 /* The file exists */
1182 Result = TRUE;
1183 }
1184
1185 /* Return the result */
1186 return Result;
1187 }
1188
1189 BOOLEAN
1190 NTAPI
1191 RtlDoesFileExists_UStr(IN PUNICODE_STRING FileName)
1192 {
1193 /* Call the updated API */
1194 return RtlDoesFileExists_UstrEx(FileName, TRUE);
1195 }
1196
1197 BOOLEAN
1198 NTAPI
1199 RtlDoesFileExists_UEx(IN PCWSTR FileName,
1200 IN BOOLEAN SucceedIfBusy)
1201 {
1202 UNICODE_STRING NameString;
1203
1204 /* Create the unicode name*/
1205 if (NT_SUCCESS(RtlInitUnicodeStringEx(&NameString, FileName)))
1206 {
1207 /* Call the unicode function */
1208 return RtlDoesFileExists_UstrEx(&NameString, SucceedIfBusy);
1209 }
1210
1211 /* Fail */
1212 return FALSE;
1213 }
1214
1215 /* PUBLIC FUNCTIONS ***********************************************************/
1216
1217 /*
1218 * @implemented
1219 */
1220 VOID
1221 NTAPI
1222 RtlReleaseRelativeName(IN PRTL_RELATIVE_NAME_U RelativeName)
1223 {
1224 /* Check if a directory reference was grabbed */
1225 if (RelativeName->CurDirRef)
1226 {
1227 /* Decrease reference count */
1228 if (!InterlockedDecrement(&RelativeName->CurDirRef->RefCount))
1229 {
1230 /* If no one uses it any longer, close handle & free */
1231 NtClose(RelativeName->CurDirRef->Handle);
1232 RtlFreeHeap(RtlGetProcessHeap(), 0, RelativeName->CurDirRef);
1233 }
1234 RelativeName->CurDirRef = NULL;
1235 }
1236 }
1237
1238 /*
1239 * @implemented
1240 */
1241 ULONG
1242 NTAPI
1243 RtlGetLongestNtPathLength(VOID)
1244 {
1245 /*
1246 * The longest NT path is a DOS path that actually sits on a UNC path (ie:
1247 * a mapped network drive), which is accessed through the DOS Global?? path.
1248 * This is, and has always been equal to, 269 characters, except in Wine
1249 * which claims this is 277. Go figure.
1250 */
1251 return MAX_PATH + RtlpDosDevicesUncPrefix.Length / sizeof(WCHAR) + sizeof(ANSI_NULL);
1252 }
1253
1254 /*
1255 * @implemented
1256 */
1257 RTL_PATH_TYPE
1258 NTAPI
1259 RtlDetermineDosPathNameType_U(IN PCWSTR Path)
1260 {
1261 DPRINT("RtlDetermineDosPathNameType_U %S\n", Path);
1262
1263 /* Unlike the newer RtlDetermineDosPathNameType_U we assume 4 characters */
1264 if (IS_PATH_SEPARATOR(Path[0]))
1265 {
1266 if (!IS_PATH_SEPARATOR(Path[1])) return RtlPathTypeRooted; /* \x */
1267 if ((Path[2] != L'.') && (Path[2] != L'?')) return RtlPathTypeUncAbsolute;/* \\x */
1268 if (IS_PATH_SEPARATOR(Path[3])) return RtlPathTypeLocalDevice; /* \\.\x or \\?\x */
1269 if (Path[3]) return RtlPathTypeUncAbsolute; /* \\.x or \\?x */
1270 return RtlPathTypeRootLocalDevice; /* \\. or \\? */
1271 }
1272 else
1273 {
1274 if (!(Path[0]) || (Path[1] != L':')) return RtlPathTypeRelative; /* x */
1275 if (IS_PATH_SEPARATOR(Path[2])) return RtlPathTypeDriveAbsolute; /* x:\ */
1276 return RtlPathTypeDriveRelative; /* x: */
1277 }
1278 }
1279
1280 /*
1281 * @implemented
1282 */
1283 ULONG
1284 NTAPI
1285 RtlIsDosDeviceName_U(IN PCWSTR Path)
1286 {
1287 UNICODE_STRING PathString;
1288 NTSTATUS Status;
1289
1290 /* Build the string */
1291 Status = RtlInitUnicodeStringEx(&PathString, Path);
1292 if (!NT_SUCCESS(Status)) return 0;
1293
1294 /*
1295 * Returns 0 if name is not valid DOS device name, or DWORD with
1296 * offset in bytes to DOS device name from beginning of buffer in high word
1297 * and size in bytes of DOS device name in low word
1298 */
1299 return RtlIsDosDeviceName_Ustr(&PathString);
1300 }
1301
1302 /*
1303 * @implemented
1304 */
1305 ULONG
1306 NTAPI
1307 RtlGetCurrentDirectory_U(
1308 _In_ ULONG MaximumLength,
1309 _Out_bytecap_(MaximumLength) PWSTR Buffer)
1310 {
1311 ULONG Length, Bytes;
1312 PCURDIR CurDir;
1313 PWSTR CurDirName;
1314 DPRINT("RtlGetCurrentDirectory %lu %p\n", MaximumLength, Buffer);
1315
1316 /* Lock the PEB to get the current directory */
1317 RtlAcquirePebLock();
1318 CurDir = &NtCurrentPeb()->ProcessParameters->CurrentDirectory;
1319
1320 /* Get the buffer and character length */
1321 CurDirName = CurDir->DosPath.Buffer;
1322 Length = CurDir->DosPath.Length / sizeof(WCHAR);
1323 ASSERT((CurDirName != NULL) && (Length > 0));
1324
1325 /*
1326 * DosPath.Buffer should always have a trailing slash. There is an assert
1327 * below which checks for this.
1328 *
1329 * This function either returns x:\ for a root (keeping the original buffer)
1330 * or it returns x:\path\foo for a directory (replacing the trailing slash
1331 * with a NULL.
1332 */
1333 Bytes = Length * sizeof(WCHAR);
1334 if ((Length <= 1) || (CurDirName[Length - 2] == L':'))
1335 {
1336 /* Check if caller does not have enough space */
1337 if (MaximumLength <= Bytes)
1338 {
1339 /* Call has no space for it, fail, add the trailing slash */
1340 RtlReleasePebLock();
1341 return Bytes + sizeof(OBJ_NAME_PATH_SEPARATOR);
1342 }
1343 }
1344 else
1345 {
1346 /* Check if caller does not have enough space */
1347 if (MaximumLength < Bytes)
1348 {
1349 /* Call has no space for it, fail */
1350 RtlReleasePebLock();
1351 return Bytes;
1352 }
1353 }
1354
1355 /* Copy the buffer since we seem to have space */
1356 RtlCopyMemory(Buffer, CurDirName, Bytes);
1357
1358 /* The buffer should end with a path separator */
1359 ASSERT(Buffer[Length - 1] == OBJ_NAME_PATH_SEPARATOR);
1360
1361 /* Again check for our two cases (drive root vs path) */
1362 if ((Length <= 1) || (Buffer[Length - 2] != L':'))
1363 {
1364 /* Replace the trailing slash with a null */
1365 Buffer[Length - 1] = UNICODE_NULL;
1366 --Length;
1367 }
1368 else
1369 {
1370 /* Append the null char since there's no trailing slash */
1371 Buffer[Length] = UNICODE_NULL;
1372 }
1373
1374 /* Release PEB lock */
1375 RtlReleasePebLock();
1376 DPRINT("CurrentDirectory %S\n", Buffer);
1377 return Length * sizeof(WCHAR);
1378 }
1379
1380 /*
1381 * @implemented
1382 */
1383 NTSTATUS
1384 NTAPI
1385 RtlSetCurrentDirectory_U(IN PUNICODE_STRING Path)
1386 {
1387 PCURDIR CurDir;
1388 NTSTATUS Status;
1389 RTL_PATH_TYPE PathType;
1390 IO_STATUS_BLOCK IoStatusBlock;
1391 UNICODE_STRING FullPath, NtName;
1392 PRTLP_CURDIR_REF OldCurDir = NULL;
1393 OBJECT_ATTRIBUTES ObjectAttributes;
1394 FILE_FS_DEVICE_INFORMATION FileFsDeviceInfo;
1395 ULONG SavedLength, CharLength, FullPathLength;
1396 HANDLE OldHandle = NULL, CurDirHandle = NULL, OldCurDirHandle = NULL;
1397
1398 DPRINT("RtlSetCurrentDirectory_U %wZ\n", Path);
1399
1400 /* Initialize for failure case */
1401 RtlInitEmptyUnicodeString(&NtName, NULL, 0);
1402
1403 /* Can't set current directory on DOS device */
1404 if (RtlIsDosDeviceName_Ustr(Path))
1405 {
1406 return STATUS_NOT_A_DIRECTORY;
1407 }
1408
1409 /* Get current directory */
1410 RtlAcquirePebLock();
1411 CurDir = &NtCurrentPeb()->ProcessParameters->CurrentDirectory;
1412
1413 /* Check if we have to drop current handle */
1414 if (((ULONG_PTR)(CurDir->Handle) & RTL_CURDIR_ALL_FLAGS) == RTL_CURDIR_DROP_OLD_HANDLE)
1415 {
1416 OldHandle = CurDir->Handle;
1417 CurDir->Handle = NULL;
1418 }
1419
1420 /* Allocate a buffer for full path (using max possible length */
1421 FullPath.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, CurDir->DosPath.MaximumLength);
1422 if (!FullPath.Buffer)
1423 {
1424 Status = STATUS_NO_MEMORY;
1425 goto Leave;
1426 }
1427
1428 /* Init string */
1429 FullPath.Length = 0;
1430 FullPath.MaximumLength = CurDir->DosPath.MaximumLength;
1431
1432 /* Get new directory full path */
1433 FullPathLength = RtlGetFullPathName_Ustr(Path, FullPath.MaximumLength, FullPath.Buffer, NULL, NULL, &PathType);
1434 if (!FullPathLength)
1435 {
1436 Status = STATUS_OBJECT_NAME_INVALID;
1437 goto Leave;
1438 }
1439
1440 SavedLength = FullPath.MaximumLength;
1441 CharLength = FullPathLength / sizeof(WCHAR);
1442
1443 if (FullPathLength > FullPath.MaximumLength)
1444 {
1445 Status = STATUS_NAME_TOO_LONG;
1446 goto Leave;
1447 }
1448
1449 /* Translate it to NT name */
1450 if (!RtlDosPathNameToNtPathName_U(FullPath.Buffer, &NtName, NULL, NULL))
1451 {
1452 Status = STATUS_OBJECT_NAME_INVALID;
1453 goto Leave;
1454 }
1455
1456 InitializeObjectAttributes(&ObjectAttributes, &NtName,
1457 OBJ_CASE_INSENSITIVE | OBJ_INHERIT,
1458 NULL, NULL);
1459
1460 /* If previous current directory was removable, then check it for dropping */
1461 if (((ULONG_PTR)(CurDir->Handle) & RTL_CURDIR_ALL_FLAGS) == RTL_CURDIR_ALL_FLAGS)
1462 {
1463 /* Get back normal handle */
1464 CurDirHandle = (HANDLE)((ULONG_PTR)(CurDir->Handle) & ~RTL_CURDIR_ALL_FLAGS);
1465 CurDir->Handle = NULL;
1466
1467 /* Get device information */
1468 Status = NtQueryVolumeInformationFile(CurDirHandle,
1469 &IoStatusBlock,
1470 &FileFsDeviceInfo,
1471 sizeof(FileFsDeviceInfo),
1472 FileFsDeviceInformation);
1473 /* Retry without taking care of removable device */
1474 if (!NT_SUCCESS(Status))
1475 {
1476 Status = RtlSetCurrentDirectory_U(Path);
1477 goto Leave;
1478 }
1479 }
1480 else
1481 {
1482 /* Open directory */
1483 Status = NtOpenFile(&CurDirHandle,
1484 SYNCHRONIZE | FILE_TRAVERSE,
1485 &ObjectAttributes,
1486 &IoStatusBlock,
1487 FILE_SHARE_READ | FILE_SHARE_WRITE,
1488 FILE_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
1489 if (!NT_SUCCESS(Status)) goto Leave;
1490
1491 /* Get device information */
1492 Status = NtQueryVolumeInformationFile(CurDirHandle,
1493 &IoStatusBlock,
1494 &FileFsDeviceInfo,
1495 sizeof(FileFsDeviceInfo),
1496 FileFsDeviceInformation);
1497 if (!NT_SUCCESS(Status)) goto Leave;
1498 }
1499
1500 /* If device is removable, mark handle */
1501 if (FileFsDeviceInfo.Characteristics & FILE_REMOVABLE_MEDIA)
1502 {
1503 CurDirHandle = (HANDLE)((ULONG_PTR)CurDirHandle | RTL_CURDIR_IS_REMOVABLE);
1504 }
1505
1506 FullPath.Length = (USHORT)FullPathLength;
1507
1508 /* If full path isn't \ terminated, do it */
1509 if (FullPath.Buffer[CharLength - 1] != OBJ_NAME_PATH_SEPARATOR)
1510 {
1511 if ((CharLength + 1) * sizeof(WCHAR) > SavedLength)
1512 {
1513 Status = STATUS_NAME_TOO_LONG;
1514 goto Leave;
1515 }
1516
1517 FullPath.Buffer[CharLength] = OBJ_NAME_PATH_SEPARATOR;
1518 FullPath.Buffer[CharLength + 1] = UNICODE_NULL;
1519 FullPath.Length += sizeof(WCHAR);
1520 }
1521
1522 /* If we have previous current directory with only us as reference, save it */
1523 if (RtlpCurDirRef != NULL && RtlpCurDirRef->RefCount == 1)
1524 {
1525 OldCurDirHandle = RtlpCurDirRef->Handle;
1526 }
1527 else
1528 {
1529 /* Allocate new current directory struct saving previous one */
1530 OldCurDir = RtlpCurDirRef;
1531 RtlpCurDirRef = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(RTLP_CURDIR_REF));
1532 if (!RtlpCurDirRef)
1533 {
1534 RtlpCurDirRef = OldCurDir;
1535 OldCurDir = NULL;
1536 Status = STATUS_NO_MEMORY;
1537 goto Leave;
1538 }
1539
1540 /* Set reference to 1 (us) */
1541 RtlpCurDirRef->RefCount = 1;
1542 }
1543
1544 /* Save new data */
1545 CurDir->Handle = CurDirHandle;
1546 RtlpCurDirRef->Handle = CurDirHandle;
1547 CurDirHandle = NULL;
1548
1549 /* Copy full path */
1550 RtlCopyMemory(CurDir->DosPath.Buffer, FullPath.Buffer, FullPath.Length + sizeof(WCHAR));
1551 CurDir->DosPath.Length = FullPath.Length;
1552
1553 Status = STATUS_SUCCESS;
1554
1555 Leave:
1556 RtlReleasePebLock();
1557
1558 if (FullPath.Buffer)
1559 {
1560 RtlFreeHeap(RtlGetProcessHeap(), 0, FullPath.Buffer);
1561 }
1562
1563 if (NtName.Buffer)
1564 {
1565 RtlFreeHeap(RtlGetProcessHeap(), 0, NtName.Buffer);
1566 }
1567
1568 if (CurDirHandle) NtClose(CurDirHandle);
1569
1570 if (OldHandle) NtClose(OldHandle);
1571
1572 if (OldCurDirHandle) NtClose(OldCurDirHandle);
1573
1574 if (OldCurDir && InterlockedDecrement(&OldCurDir->RefCount) == 0)
1575 {
1576 NtClose(OldCurDir->Handle);
1577 RtlFreeHeap(RtlGetProcessHeap(), 0, OldCurDir);
1578 }
1579
1580 return Status;
1581 }
1582
1583
1584 /******************************************************************
1585 * RtlGetFullPathName_U (NTDLL.@)
1586 *
1587 * Returns the number of bytes written to buffer (not including the
1588 * terminating NULL) if the function succeeds, or the required number of bytes
1589 * (including the terminating NULL) if the buffer is too small.
1590 *
1591 * file_part will point to the filename part inside buffer (except if we use
1592 * DOS device name, in which case file_in_buf is NULL)
1593 *
1594 * @implemented
1595 */
1596
1597 /*
1598 * @implemented
1599 */
1600 ULONG
1601 NTAPI
1602 RtlGetFullPathName_U(
1603 _In_ PCWSTR FileName,
1604 _In_ ULONG Size,
1605 _Out_z_bytecap_(Size) PWSTR Buffer,
1606 _Out_opt_ PWSTR *ShortName)
1607 {
1608 NTSTATUS Status;
1609 UNICODE_STRING FileNameString;
1610 RTL_PATH_TYPE PathType;
1611
1612 /* Build the string */
1613 Status = RtlInitUnicodeStringEx(&FileNameString, FileName);
1614 if (!NT_SUCCESS(Status)) return 0;
1615
1616 /* Call the extended function */
1617 return RtlGetFullPathName_Ustr(&FileNameString,
1618 Size,
1619 Buffer,
1620 (PCWSTR*)ShortName,
1621 NULL,
1622 &PathType);
1623 }
1624
1625 /*
1626 * @implemented
1627 */
1628 BOOLEAN
1629 NTAPI
1630 RtlDosPathNameToNtPathName_U(IN PCWSTR DosName,
1631 OUT PUNICODE_STRING NtName,
1632 OUT PCWSTR *PartName,
1633 OUT PRTL_RELATIVE_NAME_U RelativeName)
1634 {
1635 /* Call the internal function */
1636 return NT_SUCCESS(RtlpDosPathNameToRelativeNtPathName_U(FALSE,
1637 DosName,
1638 NtName,
1639 PartName,
1640 RelativeName));
1641 }
1642
1643 /*
1644 * @implemented
1645 */
1646 NTSTATUS
1647 NTAPI
1648 RtlDosPathNameToNtPathName_U_WithStatus(IN PCWSTR DosName,
1649 OUT PUNICODE_STRING NtName,
1650 OUT PCWSTR *PartName,
1651 OUT PRTL_RELATIVE_NAME_U RelativeName)
1652 {
1653 /* Call the internal function */
1654 return RtlpDosPathNameToRelativeNtPathName_U(FALSE,
1655 DosName,
1656 NtName,
1657 PartName,
1658 RelativeName);
1659 }
1660
1661 /*
1662 * @implemented
1663 */
1664 BOOLEAN
1665 NTAPI
1666 RtlDosPathNameToRelativeNtPathName_U(IN PCWSTR DosName,
1667 OUT PUNICODE_STRING NtName,
1668 OUT PCWSTR *PartName,
1669 OUT PRTL_RELATIVE_NAME_U RelativeName)
1670 {
1671 /* Call the internal function */
1672 ASSERT(RelativeName);
1673 return NT_SUCCESS(RtlpDosPathNameToRelativeNtPathName_U(TRUE,
1674 DosName,
1675 NtName,
1676 PartName,
1677 RelativeName));
1678 }
1679
1680 /*
1681 * @implemented
1682 */
1683 NTSTATUS
1684 NTAPI
1685 RtlDosPathNameToRelativeNtPathName_U_WithStatus(IN PCWSTR DosName,
1686 OUT PUNICODE_STRING NtName,
1687 OUT PCWSTR *PartName,
1688 OUT PRTL_RELATIVE_NAME_U RelativeName)
1689 {
1690 /* Call the internal function */
1691 ASSERT(RelativeName);
1692 return RtlpDosPathNameToRelativeNtPathName_U(TRUE,
1693 DosName,
1694 NtName,
1695 PartName,
1696 RelativeName);
1697 }
1698
1699 /*
1700 * @unimplemented
1701 */
1702 NTSTATUS NTAPI
1703 RtlNtPathNameToDosPathName(ULONG Unknown1, ULONG Unknown2, ULONG Unknown3, ULONG Unknown4)
1704 {
1705 DPRINT1("RtlNtPathNameToDosPathName: stub\n");
1706 return STATUS_NOT_IMPLEMENTED;
1707 }
1708
1709 /*
1710 * @implemented
1711 */
1712 ULONG
1713 NTAPI
1714 RtlDosSearchPath_U(IN PCWSTR Path,
1715 IN PCWSTR FileName,
1716 IN PCWSTR Extension,
1717 IN ULONG Size,
1718 IN PWSTR Buffer,
1719 OUT PWSTR *PartName)
1720 {
1721 NTSTATUS Status;
1722 ULONG ExtensionLength, Length, FileNameLength, PathLength;
1723 UNICODE_STRING TempString;
1724 PWCHAR NewBuffer, BufferStart;
1725 PCWSTR p;
1726
1727 /* Check if this is an absolute path */
1728 if (RtlDetermineDosPathNameType_U(FileName) != RtlPathTypeRelative)
1729 {
1730 /* Check if the file exists */
1731 if (RtlDoesFileExists_UEx(FileName, TRUE))
1732 {
1733 /* Get the full name, which does the DOS lookup */
1734 return RtlGetFullPathName_U(FileName, Size, Buffer, PartName);
1735 }
1736
1737 /* Doesn't exist, so fail */
1738 return 0;
1739 }
1740
1741 /* Scan the filename */
1742 p = FileName;
1743 while (*p)
1744 {
1745 /* Looking for an extension */
1746 if (*p == L'.')
1747 {
1748 /* No extension string needed -- it's part of the filename */
1749 Extension = NULL;
1750 break;
1751 }
1752
1753 /* Next character */
1754 p++;
1755 }
1756
1757 /* Do we have an extension? */
1758 if (!Extension)
1759 {
1760 /* Nope, don't worry about one */
1761 ExtensionLength = 0;
1762 }
1763 else
1764 {
1765 /* Build a temporary string to get the extension length */
1766 Status = RtlInitUnicodeStringEx(&TempString, Extension);
1767 if (!NT_SUCCESS(Status)) return 0;
1768 ExtensionLength = TempString.Length;
1769 }
1770
1771 /* Build a temporary string to get the path length */
1772 Status = RtlInitUnicodeStringEx(&TempString, Path);
1773 if (!NT_SUCCESS(Status)) return 0;
1774 PathLength = TempString.Length;
1775
1776 /* Build a temporary string to get the filename length */
1777 Status = RtlInitUnicodeStringEx(&TempString, FileName);
1778 if (!NT_SUCCESS(Status)) return 0;
1779 FileNameLength = TempString.Length;
1780
1781 /* Allocate the buffer for the new string name */
1782 NewBuffer = RtlAllocateHeap(RtlGetProcessHeap(),
1783 0,
1784 FileNameLength +
1785 ExtensionLength +
1786 PathLength +
1787 3 * sizeof(WCHAR));
1788 if (!NewBuffer)
1789 {
1790 /* Fail the call */
1791 DbgPrint("%s: Failing due to out of memory (RtlAllocateHeap failure)\n",
1792 __FUNCTION__);
1793 return 0;
1794 }
1795
1796 /* Final loop to build the path */
1797 while (TRUE)
1798 {
1799 /* Check if we have a valid character */
1800 BufferStart = NewBuffer;
1801 if (*Path)
1802 {
1803 /* Loop as long as there's no semicolon */
1804 while (*Path != L';')
1805 {
1806 /* Copy the next character */
1807 *BufferStart++ = *Path++;
1808 if (!*Path) break;
1809 }
1810
1811 /* We found a semi-colon, to stop path processing on this loop */
1812 if (*Path == L';') ++Path;
1813 }
1814
1815 /* Add a terminating slash if needed */
1816 if ((BufferStart != NewBuffer) && (BufferStart[-1] != OBJ_NAME_PATH_SEPARATOR))
1817 {
1818 *BufferStart++ = OBJ_NAME_PATH_SEPARATOR;
1819 }
1820
1821 /* Bail out if we reached the end */
1822 if (!*Path) Path = NULL;
1823
1824 /* Copy the file name and check if an extension is needed */
1825 RtlCopyMemory(BufferStart, FileName, FileNameLength);
1826 if (ExtensionLength)
1827 {
1828 /* Copy the extension too */
1829 RtlCopyMemory((PCHAR)BufferStart + FileNameLength,
1830 Extension,
1831 ExtensionLength + sizeof(WCHAR));
1832 }
1833 else
1834 {
1835 /* Just NULL-terminate */
1836 *(PWCHAR)((PCHAR)BufferStart + FileNameLength) = UNICODE_NULL;
1837 }
1838
1839 /* Now, does this file exist? */
1840 if (RtlDoesFileExists_UEx(NewBuffer, FALSE))
1841 {
1842 /* Call the full-path API to get the length */
1843 Length = RtlGetFullPathName_U(NewBuffer, Size, Buffer, PartName);
1844 break;
1845 }
1846
1847 /* If we got here, path doesn't exist, so fail the call */
1848 Length = 0;
1849 if (!Path) break;
1850 }
1851
1852 /* Free the allocation and return the length */
1853 RtlFreeHeap(RtlGetProcessHeap(), 0, NewBuffer);
1854 return Length;
1855 }
1856
1857 /*
1858 * @implemented
1859 */
1860 NTSTATUS
1861 NTAPI
1862 RtlGetFullPathName_UstrEx(IN PUNICODE_STRING FileName,
1863 IN PUNICODE_STRING StaticString,
1864 IN PUNICODE_STRING DynamicString,
1865 IN PUNICODE_STRING *StringUsed,
1866 IN PSIZE_T FilePartSize,
1867 OUT PBOOLEAN NameInvalid,
1868 OUT RTL_PATH_TYPE* PathType,
1869 OUT PSIZE_T LengthNeeded)
1870 {
1871 NTSTATUS Status;
1872 PWCHAR StaticBuffer;
1873 PCWCH ShortName;
1874 ULONG Length;
1875 USHORT StaticLength;
1876 UNICODE_STRING TempDynamicString;
1877
1878 /* Initialize all our locals */
1879 ShortName = NULL;
1880 StaticBuffer = NULL;
1881 TempDynamicString.Buffer = NULL;
1882
1883 /* Initialize the input parameters */
1884 if (StringUsed) *StringUsed = NULL;
1885 if (LengthNeeded) *LengthNeeded = 0;
1886 if (FilePartSize) *FilePartSize = 0;
1887
1888 /* Check for invalid parameters */
1889 if ((DynamicString) && !(StringUsed) && (StaticString))
1890 {
1891 return STATUS_INVALID_PARAMETER;
1892 }
1893
1894 /* Check if we did not get an input string */
1895 if (!StaticString)
1896 {
1897 /* Allocate one */
1898 StaticLength = MAX_PATH * sizeof(WCHAR);
1899 StaticBuffer = RtlpAllocateStringMemory(MAX_PATH * sizeof(WCHAR), TAG_USTR);
1900 if (!StaticBuffer) return STATUS_NO_MEMORY;
1901 }
1902 else
1903 {
1904 /* Use the one we received */
1905 StaticBuffer = StaticString->Buffer;
1906 StaticLength = StaticString->MaximumLength;
1907 }
1908
1909 /* Call the lower-level function */
1910 Length = RtlGetFullPathName_Ustr(FileName,
1911 StaticLength,
1912 StaticBuffer,
1913 &ShortName,
1914 NameInvalid,
1915 PathType);
1916 DPRINT("Length: %u StaticBuffer: %S\n", Length, StaticBuffer);
1917 if (!Length)
1918 {
1919 /* Fail if it failed */
1920 DbgPrint("%s(%d) - RtlGetFullPathName_Ustr() returned 0\n",
1921 __FUNCTION__,
1922 __LINE__);
1923 Status = STATUS_OBJECT_NAME_INVALID;
1924 goto Quickie;
1925 }
1926
1927 /* Check if it fits inside our static string */
1928 if ((StaticString) && (Length < StaticLength))
1929 {
1930 /* Set the final length */
1931 StaticString->Length = (USHORT)Length;
1932
1933 /* Set the file part size */
1934 if (FilePartSize) *FilePartSize = ShortName ? (ShortName - StaticString->Buffer) : 0;
1935
1936 /* Return the static string if requested */
1937 if (StringUsed) *StringUsed = StaticString;
1938
1939 /* We are done with success */
1940 Status = STATUS_SUCCESS;
1941 goto Quickie;
1942 }
1943
1944 /* Did we not have an input dynamic string ?*/
1945 if (!DynamicString)
1946 {
1947 /* Return the length we need */
1948 if (LengthNeeded) *LengthNeeded = Length;
1949
1950 /* And fail such that the caller can try again */
1951 Status = STATUS_BUFFER_TOO_SMALL;
1952 goto Quickie;
1953 }
1954
1955 /* Check if it fits in our static buffer */
1956 if ((StaticBuffer) && (Length < StaticLength))
1957 {
1958 /* NULL-terminate it */
1959 StaticBuffer[Length / sizeof(WCHAR)] = UNICODE_NULL;
1960
1961 /* Set the settings for the dynamic string the caller sent */
1962 DynamicString->MaximumLength = StaticLength;
1963 DynamicString->Length = (USHORT)Length;
1964 DynamicString->Buffer = StaticBuffer;
1965
1966 /* Set the part size */
1967 if (FilePartSize) *FilePartSize = ShortName ? (ShortName - StaticBuffer) : 0;
1968
1969 /* Return the dynamic string if requested */
1970 if (StringUsed) *StringUsed = DynamicString;
1971
1972 /* Do not free the static buffer on exit, and return success */
1973 StaticBuffer = NULL;
1974 Status = STATUS_SUCCESS;
1975 goto Quickie;
1976 }
1977
1978 /* Now try again under the PEB lock */
1979 RtlAcquirePebLock();
1980 Length = RtlGetFullPathName_Ustr(FileName,
1981 StaticLength,
1982 StaticBuffer,
1983 &ShortName,
1984 NameInvalid,
1985 PathType);
1986 if (!Length)
1987 {
1988 /* It failed */
1989 DbgPrint("%s line %d: RtlGetFullPathName_Ustr() returned 0\n",
1990 __FUNCTION__, __LINE__);
1991 Status = STATUS_OBJECT_NAME_INVALID;
1992 goto Release;
1993 }
1994
1995 /* Check if it fits inside our static string now */
1996 if ((StaticString) && (Length < StaticLength))
1997 {
1998 /* Set the final length */
1999 StaticString->Length = (USHORT)Length;
2000
2001 /* Set the file part size */
2002 if (FilePartSize) *FilePartSize = ShortName ? (ShortName - StaticString->Buffer) : 0;
2003
2004 /* Return the static string if requested */
2005 if (StringUsed) *StringUsed = StaticString;
2006
2007 /* We are done with success */
2008 Status = STATUS_SUCCESS;
2009 goto Release;
2010 }
2011
2012 /* Check if the path won't even fit in a real string */
2013 if ((Length + sizeof(WCHAR)) > UNICODE_STRING_MAX_BYTES)
2014 {
2015 /* Name is way too long, fail */
2016 Status = STATUS_NAME_TOO_LONG;
2017 goto Release;
2018 }
2019
2020 /* Allocate the string to hold the path name now */
2021 TempDynamicString.Buffer = RtlpAllocateStringMemory(Length + sizeof(WCHAR),
2022 TAG_USTR);
2023 if (!TempDynamicString.Buffer)
2024 {
2025 /* Out of memory, fail */
2026 Status = STATUS_NO_MEMORY;
2027 goto Release;
2028 }
2029
2030 /* Add space for a NULL terminator, and now check the full path */
2031 TempDynamicString.MaximumLength = (USHORT)Length + sizeof(UNICODE_NULL);
2032 Length = RtlGetFullPathName_Ustr(FileName,
2033 Length,
2034 TempDynamicString.Buffer,
2035 &ShortName,
2036 NameInvalid,
2037 PathType);
2038 if (!Length)
2039 {
2040 /* Some path error, so fail out */
2041 DbgPrint("%s line %d: RtlGetFullPathName_Ustr() returned 0\n",
2042 __FUNCTION__, __LINE__);
2043 Status = STATUS_OBJECT_NAME_INVALID;
2044 goto Release;
2045 }
2046
2047 /* It should fit in the string we just allocated */
2048 ASSERT(Length < (TempDynamicString.MaximumLength - sizeof(WCHAR)));
2049 if (Length > TempDynamicString.MaximumLength)
2050 {
2051 /* This is really weird and would mean some kind of race */
2052 Status = STATUS_INTERNAL_ERROR;
2053 goto Release;
2054 }
2055
2056 /* Return the file part size */
2057 if (FilePartSize) *FilePartSize = ShortName ? (ShortName - TempDynamicString.Buffer) : 0;
2058
2059 /* Terminate the whole string now */
2060 TempDynamicString.Buffer[Length / sizeof(WCHAR)] = UNICODE_NULL;
2061
2062 /* Finalize the string and return it to the user */
2063 DynamicString->Buffer = TempDynamicString.Buffer;
2064 DynamicString->Length = (USHORT)Length;
2065 DynamicString->MaximumLength = TempDynamicString.MaximumLength;
2066 if (StringUsed) *StringUsed = DynamicString;
2067
2068 /* Return success and make sure we don't free the buffer on exit */
2069 TempDynamicString.Buffer = NULL;
2070 Status = STATUS_SUCCESS;
2071
2072 Release:
2073 /* Release the PEB lock */
2074 RtlReleasePebLock();
2075
2076 Quickie:
2077 /* Free any buffers we should be freeing */
2078 DPRINT("Status: %lx %S %S\n", Status, StaticBuffer, TempDynamicString.Buffer);
2079 if ((StaticString) && (StaticBuffer) && (StaticBuffer != StaticString->Buffer))
2080 {
2081 RtlpFreeMemory(StaticBuffer, TAG_USTR);
2082 }
2083 if (TempDynamicString.Buffer)
2084 {
2085 RtlpFreeMemory(TempDynamicString.Buffer, TAG_USTR);
2086 }
2087
2088 /* Print out any unusual errors */
2089 if ((NT_ERROR(Status)) &&
2090 (Status != STATUS_NO_SUCH_FILE) && (Status != STATUS_BUFFER_TOO_SMALL))
2091 {
2092 DbgPrint("RTL: %s - failing on filename %wZ with status %08lx\n",
2093 __FUNCTION__, FileName, Status);
2094 }
2095
2096 /* Return, we're all done */
2097 return Status;
2098 }
2099
2100 /*
2101 * @implemented
2102 */
2103 NTSTATUS
2104 NTAPI
2105 RtlDosSearchPath_Ustr(IN ULONG Flags,
2106 IN PUNICODE_STRING PathString,
2107 IN PUNICODE_STRING FileNameString,
2108 IN PUNICODE_STRING ExtensionString,
2109 IN PUNICODE_STRING CallerBuffer,
2110 IN OUT PUNICODE_STRING DynamicString OPTIONAL,
2111 OUT PUNICODE_STRING* FullNameOut OPTIONAL,
2112 OUT PSIZE_T FilePartSize OPTIONAL,
2113 OUT PSIZE_T LengthNeeded OPTIONAL)
2114 {
2115 WCHAR StaticCandidateBuffer[MAX_PATH];
2116 UNICODE_STRING StaticCandidateString;
2117 NTSTATUS Status;
2118 RTL_PATH_TYPE PathType;
2119 PWCHAR p, End, CandidateEnd, SegmentEnd;
2120 SIZE_T SegmentSize, ByteCount, PathSize, MaxPathSize = 0;
2121 USHORT NamePlusExtLength, WorstCaseLength, ExtensionLength = 0;
2122 PUNICODE_STRING FullIsolatedPath;
2123 DPRINT("DOS Path Search: %lx %wZ %wZ %wZ %wZ %wZ\n",
2124 Flags, PathString, FileNameString, ExtensionString, CallerBuffer, DynamicString);
2125
2126 /* Initialize the input string */
2127 RtlInitEmptyUnicodeString(&StaticCandidateString,
2128 StaticCandidateBuffer,
2129 sizeof(StaticCandidateBuffer));
2130
2131 /* Initialize optional arguments */
2132 if (FullNameOut ) *FullNameOut = NULL;
2133 if (FilePartSize) *FilePartSize = 0;
2134 if (LengthNeeded) *LengthNeeded = 0;
2135 if (DynamicString)
2136 {
2137 DynamicString->Length = DynamicString->MaximumLength = 0;
2138 DynamicString->Buffer = NULL;
2139 }
2140
2141 /* Check for invalid parameters */
2142 if ((Flags & ~7) ||
2143 !(PathString) ||
2144 !(FileNameString) ||
2145 ((CallerBuffer) && (DynamicString) && !(FullNameOut)))
2146 {
2147 /* Fail */
2148 DbgPrint("%s: Invalid parameters passed\n", __FUNCTION__);
2149 Status = STATUS_INVALID_PARAMETER;
2150 goto Quickie;
2151 }
2152
2153 /* First check what kind of path this is */
2154 PathType = RtlDetermineDosPathNameType_Ustr(FileNameString);
2155
2156 /* Check if the caller wants to prevent relative .\ and ..\ paths */
2157 if ((Flags & 2) &&
2158 (PathType == RtlPathTypeRelative) &&
2159 (FileNameString->Length >= (2 * sizeof(WCHAR))) &&
2160 (FileNameString->Buffer[0] == L'.') &&
2161 ((IS_PATH_SEPARATOR(FileNameString->Buffer[1])) ||
2162 ((FileNameString->Buffer[1] == L'.') &&
2163 ((FileNameString->Length >= (3 * sizeof(WCHAR))) &&
2164 (IS_PATH_SEPARATOR(FileNameString->Buffer[2]))))))
2165 {
2166 /* Yes, and this path is like that, so make it seem unknown */
2167 PathType = RtlPathTypeUnknown;
2168 }
2169
2170 /* Now check relative vs non-relative paths */
2171 if (PathType == RtlPathTypeRelative)
2172 {
2173 /* Does the caller want SxS? */
2174 if (Flags & 1)
2175 {
2176 /* Apply the SxS magic */
2177 FullIsolatedPath = NULL;
2178 Status = RtlDosApplyFileIsolationRedirection_Ustr(TRUE,
2179 FileNameString,
2180 ExtensionString,
2181 CallerBuffer,
2182 DynamicString,
2183 &FullIsolatedPath,
2184 NULL,
2185 FilePartSize,
2186 LengthNeeded);
2187 if (NT_SUCCESS(Status))
2188 {
2189 /* We found the SxS path, return it */
2190 if (FullNameOut) *FullNameOut = FullIsolatedPath;
2191 goto Quickie;
2192 }
2193 else if (Status != STATUS_SXS_KEY_NOT_FOUND)
2194 {
2195 /* Critical SxS error, fail */
2196 DbgPrint("%s: Failing because call to "
2197 "RtlDosApplyIsolationRedirection_Ustr(%wZ) failed with "
2198 "status 0x%08lx\n",
2199 __FUNCTION__,
2200 FileNameString,
2201 Status);
2202 goto Quickie;
2203 }
2204 }
2205
2206 /* No SxS key found, or not requested, check if there's an extension */
2207 if (ExtensionString)
2208 {
2209 /* Save the extension length, and check if there's a file name */
2210 ExtensionLength = ExtensionString->Length;
2211 if (FileNameString->Length)
2212 {
2213 /* Start parsing the file name */
2214 End = &FileNameString->Buffer[FileNameString->Length / sizeof(WCHAR)];
2215 while (End > FileNameString->Buffer)
2216 {
2217 /* If we find a path separator, there's no extension */
2218 if (IS_PATH_SEPARATOR(*--End)) break;
2219
2220 /* Otherwise, did we find an extension dot? */
2221 if (*End == L'.')
2222 {
2223 /* Ignore what the caller sent it, use the filename's */
2224 ExtensionString = NULL;
2225 ExtensionLength = 0;
2226 break;
2227 }
2228 }
2229 }
2230 }
2231
2232 /* Check if we got a path */
2233 if (PathString->Length)
2234 {
2235 /* Start parsing the path name, looking for path separators */
2236 End = &PathString->Buffer[PathString->Length / sizeof(WCHAR)];
2237 p = End;
2238 while ((p > PathString->Buffer) && (*--p == L';'))
2239 {
2240 /* This is the size of the path -- handle a trailing slash */
2241 PathSize = End - p - 1;
2242 if ((PathSize) && !(IS_PATH_SEPARATOR(*(End - 1)))) PathSize++;
2243
2244 /* Check if we found a bigger path than before */
2245 if (PathSize > MaxPathSize) MaxPathSize = PathSize;
2246
2247 /* Keep going with the path after this path separator */
2248 End = p;
2249 }
2250
2251 /* This is the trailing path, run the same code as above */
2252 PathSize = End - p;
2253 if ((PathSize) && !(IS_PATH_SEPARATOR(*(End - 1)))) PathSize++;
2254 if (PathSize > MaxPathSize) MaxPathSize = PathSize;
2255
2256 /* Finally, convert the largest path size into WCHAR */
2257 MaxPathSize *= sizeof(WCHAR);
2258 }
2259
2260 /* Use the extension, the file name, and the largest path as the size */
2261 WorstCaseLength = ExtensionLength +
2262 FileNameString->Length +
2263 (USHORT)MaxPathSize +
2264 sizeof(UNICODE_NULL);
2265 if (WorstCaseLength > UNICODE_STRING_MAX_BYTES)
2266 {
2267 /* It has to fit in a registry string, if not, fail here */
2268 DbgPrint("%s returning STATUS_NAME_TOO_LONG because the computed "
2269 "worst case file name length is %Iu bytes\n",
2270 __FUNCTION__,
2271 WorstCaseLength);
2272 Status = STATUS_NAME_TOO_LONG;
2273 goto Quickie;
2274 }
2275
2276 /* Scan the path now, to see if we can find the file */
2277 p = PathString->Buffer;
2278 End = &p[PathString->Length / sizeof(WCHAR)];
2279 while (p < End)
2280 {
2281 /* Find out where this path ends */
2282 for (SegmentEnd = p;
2283 ((SegmentEnd != End) && (*SegmentEnd != L';'));
2284 SegmentEnd++);
2285
2286 /* Compute the size of this path */
2287 ByteCount = SegmentSize = (SegmentEnd - p) * sizeof(WCHAR);
2288
2289 /* Handle trailing slash if there isn't one */
2290 if ((SegmentSize) && !(IS_PATH_SEPARATOR(*(SegmentEnd - 1))))
2291 {
2292 /* Add space for one */
2293 SegmentSize += sizeof(OBJ_NAME_PATH_SEPARATOR);
2294 }
2295
2296 /* Now check if our initial static buffer is too small */
2297 if (StaticCandidateString.MaximumLength <
2298 (SegmentSize + ExtensionLength + FileNameString->Length))
2299 {
2300 /* At this point we should've been using our static buffer */
2301 ASSERT(StaticCandidateString.Buffer == StaticCandidateBuffer);
2302 if (StaticCandidateString.Buffer != StaticCandidateBuffer)
2303 {
2304 /* Something is really messed up if this was the dynamic string */
2305 DbgPrint("%s: internal error #1; "
2306 "CandidateString.Buffer = %p; "
2307 "StaticCandidateBuffer = %p\n",
2308 __FUNCTION__,
2309 StaticCandidateString.Buffer,
2310 StaticCandidateBuffer);
2311 Status = STATUS_INTERNAL_ERROR;
2312 goto Quickie;
2313 }
2314
2315 /* We checked before that the maximum possible size shoudl fit! */
2316 ASSERT((SegmentSize + FileNameString->Length + ExtensionLength) <
2317 UNICODE_STRING_MAX_BYTES);
2318 if ((SegmentSize + ExtensionLength + FileNameString->Length) >
2319 (UNICODE_STRING_MAX_BYTES - sizeof(WCHAR)))
2320 {
2321 /* For some reason it's not fitting anymore. Something messed up */
2322 DbgPrint("%s: internal error #2; SegmentSize = %u, "
2323 "FileName->Length = %u, DefaultExtensionLength = %u\n",
2324 __FUNCTION__,
2325 SegmentSize,
2326 FileNameString->Length,
2327 ExtensionLength);
2328 Status = STATUS_INTERNAL_ERROR;
2329 goto Quickie;
2330 }
2331
2332 /* Now allocate the dynamic string */
2333 StaticCandidateString.MaximumLength = FileNameString->Length +
2334 WorstCaseLength;
2335 StaticCandidateString.Buffer = RtlpAllocateStringMemory(WorstCaseLength,
2336 TAG_USTR);
2337 if (!StaticCandidateString.Buffer)
2338 {
2339 /* Out of memory, fail */
2340 DbgPrint("%s: Unable to allocate %u byte buffer for path candidate\n",
2341 __FUNCTION__,
2342 StaticCandidateString.MaximumLength);
2343 Status = STATUS_NO_MEMORY;
2344 goto Quickie;
2345 }
2346 }
2347
2348 /* Copy the path in the string */
2349 RtlCopyMemory(StaticCandidateString.Buffer, p, ByteCount);
2350
2351 /* Get to the end of the string, and add the trailing slash if missing */
2352 CandidateEnd = &StaticCandidateString.Buffer[ByteCount / sizeof(WCHAR)];
2353 if ((SegmentSize) && (SegmentSize != ByteCount))
2354 {
2355 *CandidateEnd++ = OBJ_NAME_PATH_SEPARATOR;
2356 }
2357
2358 /* Copy the filename now */
2359 RtlCopyMemory(CandidateEnd,
2360 FileNameString->Buffer,
2361 FileNameString->Length);
2362 CandidateEnd += (FileNameString->Length / sizeof(WCHAR));
2363
2364 /* Check if there was an extension */
2365 if (ExtensionString)
2366 {
2367 /* Copy the extension too */
2368 RtlCopyMemory(CandidateEnd,
2369 ExtensionString->Buffer,
2370 ExtensionString->Length);
2371 CandidateEnd += (ExtensionString->Length / sizeof(WCHAR));
2372 }
2373
2374 /* We are done, terminate it */
2375 *CandidateEnd = UNICODE_NULL;
2376
2377 /* Now set the final length of the string so it becomes valid */
2378 StaticCandidateString.Length = (USHORT)(CandidateEnd -
2379 StaticCandidateString.Buffer) *
2380 sizeof(WCHAR);
2381
2382 /* Check if this file exists */
2383 DPRINT("BUFFER: %S\n", StaticCandidateString.Buffer);
2384 if (RtlDoesFileExists_UEx(StaticCandidateString.Buffer, FALSE))
2385 {
2386 /* Awesome, it does, now get the full path */
2387 Status = RtlGetFullPathName_UstrEx(&StaticCandidateString,
2388 CallerBuffer,
2389 DynamicString,
2390 (PUNICODE_STRING*)FullNameOut,
2391 FilePartSize,
2392 NULL,
2393 &PathType,
2394 LengthNeeded);
2395 if (!(NT_SUCCESS(Status)) &&
2396 ((Status != STATUS_NO_SUCH_FILE) &&
2397 (Status != STATUS_BUFFER_TOO_SMALL)))
2398 {
2399 DbgPrint("%s: Failing because we thought we found %wZ on "
2400 "the search path, but RtlGetfullPathNameUStrEx() "
2401 "returned %08lx\n",
2402 __FUNCTION__,
2403 &StaticCandidateString,
2404 Status);
2405 }
2406 DPRINT("Status: %lx BUFFER: %S\n", Status, CallerBuffer->Buffer);
2407 goto Quickie;
2408 }
2409 else
2410 {
2411 /* Otherwise, move to the next path */
2412 if (SegmentEnd != End)
2413 {
2414 /* Handle the case of the path separator trailing */
2415 p = SegmentEnd + 1;
2416 }
2417 else
2418 {
2419 p = SegmentEnd;
2420 }
2421 }
2422 }
2423
2424 /* Loop finished and we didn't break out -- fail */
2425 Status = STATUS_NO_SUCH_FILE;
2426 }
2427 else
2428 {
2429 /* We have a full path, so check if it does exist */
2430 DPRINT("%wZ\n", FileNameString);
2431 if (!RtlDoesFileExists_UstrEx(FileNameString, TRUE))
2432 {
2433 /* It doesn't exist, did we have an extension? */
2434 if (!(ExtensionString) || !(ExtensionString->Length))
2435 {
2436 /* No extension, so just fail */
2437 Status = STATUS_NO_SUCH_FILE;
2438 goto Quickie;
2439 }
2440
2441 /* There was an extension, check if the filename already had one */
2442 if (!(Flags & 4) && (FileNameString->Length))
2443 {
2444 /* Parse the filename */
2445 p = FileNameString->Buffer;
2446 End = &p[FileNameString->Length / sizeof(WCHAR)];
2447 while (End > p)
2448 {
2449 /* If there's a path separator, there's no extension */
2450 if (IS_PATH_SEPARATOR(*--End)) break;
2451
2452 /* Othwerwise, did we find an extension dot? */
2453 if (*End == L'.')
2454 {
2455 /* File already had an extension, so fail */
2456 Status = STATUS_NO_SUCH_FILE;
2457 goto Quickie;
2458 }
2459 }
2460 }
2461
2462 /* So there is an extension, we'll try again by adding it */
2463 NamePlusExtLength = FileNameString->Length +
2464 ExtensionString->Length +
2465 sizeof(UNICODE_NULL);
2466 if (NamePlusExtLength > UNICODE_STRING_MAX_BYTES)
2467 {
2468 /* It won't fit in any kind of valid string, so fail */
2469 DbgPrint("%s: Failing because filename plus extension (%Iu bytes) is too big\n",
2470 __FUNCTION__,
2471 NamePlusExtLength);
2472 Status = STATUS_NAME_TOO_LONG;
2473 goto Quickie;
2474 }
2475
2476 /* Fill it fit in our temporary string? */
2477 if (NamePlusExtLength > StaticCandidateString.MaximumLength)
2478 {
2479 /* It won't fit anymore, allocate a dynamic string for it */
2480 StaticCandidateString.MaximumLength = NamePlusExtLength;
2481 StaticCandidateString.Buffer = RtlpAllocateStringMemory(NamePlusExtLength,
2482 TAG_USTR);
2483 if (!StaticCandidateString.Buffer)
2484 {
2485 /* Ran out of memory, so fail */
2486 DbgPrint("%s: Failing because allocating the dynamic filename buffer failed\n",
2487 __FUNCTION__);
2488 Status = STATUS_NO_MEMORY;
2489 goto Quickie;
2490 }
2491 }
2492
2493 /* Copy the filename */
2494 RtlCopyUnicodeString(&StaticCandidateString, FileNameString);
2495
2496 /* Copy the extension */
2497 RtlAppendUnicodeStringToString(&StaticCandidateString,
2498 ExtensionString);
2499
2500 DPRINT("SB: %wZ\n", &StaticCandidateString);
2501
2502 /* And check if this file now exists */
2503 if (!RtlDoesFileExists_UstrEx(&StaticCandidateString, TRUE))
2504 {
2505 /* Still no joy, fail out */
2506 Status = STATUS_NO_SUCH_FILE;
2507 goto Quickie;
2508 }
2509
2510 /* File was found, get the final full path */
2511 Status = RtlGetFullPathName_UstrEx(&StaticCandidateString,
2512 CallerBuffer,
2513 DynamicString,
2514 (PUNICODE_STRING*)FullNameOut,
2515 FilePartSize,
2516 NULL,
2517 &PathType,
2518 LengthNeeded);
2519 if (!(NT_SUCCESS(Status)) && (Status != STATUS_NO_SUCH_FILE))
2520 {
2521 DbgPrint("%s: Failing on \"%wZ\" because RtlGetfullPathNameUStrEx() "
2522 "failed with status %08lx\n",
2523 __FUNCTION__,
2524 &StaticCandidateString,
2525 Status);
2526 }
2527 DPRINT("Status: %lx BUFFER: %S\n", Status, CallerBuffer->Buffer);
2528 }
2529 else
2530 {
2531 /* File was found on the first try, get the final full path */
2532 Status = RtlGetFullPathName_UstrEx(FileNameString,
2533 CallerBuffer,
2534 DynamicString,
2535 (PUNICODE_STRING*)FullNameOut,
2536 FilePartSize,
2537 NULL,
2538 &PathType,
2539 LengthNeeded);
2540 if (!(NT_SUCCESS(Status)) &&
2541 ((Status != STATUS_NO_SUCH_FILE) &&
2542 (Status != STATUS_BUFFER_TOO_SMALL)))
2543 {
2544 DbgPrint("%s: Failing because RtlGetfullPathNameUStrEx() on %wZ "
2545 "failed with status %08lx\n",
2546 __FUNCTION__,
2547 FileNameString,
2548 Status);
2549 }
2550 DPRINT("Status: %lx BUFFER: %S\n", Status, CallerBuffer->Buffer);
2551 }
2552 }
2553
2554 Quickie:
2555 /* Anything that was not an error, turn into STATUS_SUCCESS */
2556 if (NT_SUCCESS(Status)) Status = STATUS_SUCCESS;
2557
2558 /* Check if we had a dynamic string */
2559 if ((StaticCandidateString.Buffer) &&
2560 (StaticCandidateString.Buffer != StaticCandidateBuffer))
2561 {
2562 /* Free it */
2563 RtlFreeUnicodeString(&StaticCandidateString);
2564 }
2565
2566 /* Return the status */
2567 return Status;
2568 }
2569
2570 /*
2571 * @implemented
2572 */
2573 BOOLEAN
2574 NTAPI
2575 RtlDoesFileExists_U(IN PCWSTR FileName)
2576 {
2577 /* Call the new function */
2578 return RtlDoesFileExists_UEx(FileName, TRUE);
2579 }
2580
2581 /* EOF */