1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_FileUtilsWin_h
8 #define mozilla_FileUtilsWin_h
12 #include "mozilla/Scoped.h"
13 #include "nsStringGlue.h"
18 NtPathToDosPath(const nsAString
& aNtPath
, nsAString
& aDosPath
)
21 if (aNtPath
.IsEmpty()) {
24 NS_NAMED_LITERAL_STRING(symLinkPrefix
, "\\??\\");
25 uint32_t ntPathLen
= aNtPath
.Length();
26 uint32_t symLinkPrefixLen
= symLinkPrefix
.Length();
27 if (ntPathLen
>= 6 && aNtPath
.CharAt(5) == L
':' &&
28 ntPathLen
>= symLinkPrefixLen
&&
29 Substring(aNtPath
, 0, symLinkPrefixLen
).Equals(symLinkPrefix
)) {
30 // Symbolic link for DOS device. Just strip off the prefix.
35 nsAutoString logicalDrives
;
38 len
= GetLogicalDriveStringsW(
39 len
, reinterpret_cast<wchar_t*>(logicalDrives
.BeginWriting()));
42 } else if (len
> logicalDrives
.Length()) {
43 logicalDrives
.SetLength(len
);
48 const char16_t
* cur
= logicalDrives
.BeginReading();
49 const char16_t
* end
= logicalDrives
.EndReading();
51 targetPath
.SetLength(MAX_PATH
);
52 wchar_t driveTemplate
[] = L
" :";
54 // Unfortunately QueryDosDevice doesn't support the idiom for querying the
55 // output buffer size, so it may require retries.
56 driveTemplate
[0] = *cur
;
57 DWORD targetPathLen
= 0;
58 SetLastError(ERROR_SUCCESS
);
60 targetPathLen
= QueryDosDeviceW(driveTemplate
,
61 reinterpret_cast<wchar_t*>(targetPath
.BeginWriting()),
63 if (targetPathLen
|| GetLastError() != ERROR_INSUFFICIENT_BUFFER
) {
66 targetPath
.SetLength(targetPath
.Length() * 2);
69 // Need to use wcslen here because targetPath contains embedded NULL chars
70 size_t firstTargetPathLen
= wcslen(targetPath
.get());
71 const char16_t
* pathComponent
= aNtPath
.BeginReading() +
73 bool found
= _wcsnicmp(char16ptr_t(aNtPath
.BeginReading()), targetPath
.get(),
74 firstTargetPathLen
) == 0 &&
75 *pathComponent
== L
'\\';
77 aDosPath
= driveTemplate
;
78 aDosPath
+= pathComponent
;
82 // Advance to the next NUL character in logicalDrives
85 // Try to handle UNC paths. NB: This must happen after we've checked drive
86 // mappings in case a UNC path is mapped to a drive!
87 NS_NAMED_LITERAL_STRING(uncPrefix
, "\\\\");
88 NS_NAMED_LITERAL_STRING(deviceMupPrefix
, "\\Device\\Mup\\");
89 if (StringBeginsWith(aNtPath
, deviceMupPrefix
)) {
91 aDosPath
+= Substring(aNtPath
, deviceMupPrefix
.Length());
94 NS_NAMED_LITERAL_STRING(deviceLanmanRedirectorPrefix
,
95 "\\Device\\LanmanRedirector\\");
96 if (StringBeginsWith(aNtPath
, deviceLanmanRedirectorPrefix
)) {
98 aDosPath
+= Substring(aNtPath
, deviceLanmanRedirectorPrefix
.Length());
105 HandleToFilename(HANDLE aHandle
, const LARGE_INTEGER
& aOffset
,
106 nsAString
& aFilename
);
108 } // namespace mozilla
110 #endif // mozilla_FileUtilsWin_h