xpcom/io/FileUtilsWin.h

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:d993222c9ba3
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/. */
6
7 #ifndef mozilla_FileUtilsWin_h
8 #define mozilla_FileUtilsWin_h
9
10 #include <windows.h>
11
12 #include "mozilla/Scoped.h"
13 #include "nsStringGlue.h"
14
15 namespace mozilla {
16
17 inline bool
18 NtPathToDosPath(const nsAString& aNtPath, nsAString& aDosPath)
19 {
20 aDosPath.Truncate();
21 if (aNtPath.IsEmpty()) {
22 return true;
23 }
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.
31 aDosPath = aNtPath;
32 aDosPath.Cut(0, 4);
33 return true;
34 }
35 nsAutoString logicalDrives;
36 DWORD len = 0;
37 while(true) {
38 len = GetLogicalDriveStringsW(len, reinterpret_cast<wchar_t*>(logicalDrives.BeginWriting()));
39 if (!len) {
40 return false;
41 } else if (len > logicalDrives.Length()) {
42 logicalDrives.SetLength(len);
43 } else {
44 break;
45 }
46 }
47 const char16_t* cur = logicalDrives.BeginReading();
48 const char16_t* end = logicalDrives.EndReading();
49 nsString targetPath;
50 targetPath.SetLength(MAX_PATH);
51 wchar_t driveTemplate[] = L" :";
52 do {
53 // Unfortunately QueryDosDevice doesn't support the idiom for querying the
54 // output buffer size, so it may require retries.
55 driveTemplate[0] = *cur;
56 DWORD targetPathLen = 0;
57 SetLastError(ERROR_SUCCESS);
58 while (true) {
59 targetPathLen = QueryDosDeviceW(driveTemplate, reinterpret_cast<wchar_t*>(targetPath.BeginWriting()),
60 targetPath.Length());
61 if (targetPathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
62 break;
63 }
64 targetPath.SetLength(targetPath.Length() * 2);
65 }
66 if (targetPathLen) {
67 // Need to use wcslen here because targetPath contains embedded NULL chars
68 size_t firstTargetPathLen = wcslen(targetPath.get());
69 const char16_t* pathComponent = aNtPath.BeginReading() +
70 firstTargetPathLen;
71 bool found = _wcsnicmp(char16ptr_t(aNtPath.BeginReading()), targetPath.get(),
72 firstTargetPathLen) == 0 &&
73 *pathComponent == L'\\';
74 if (found) {
75 aDosPath = driveTemplate;
76 aDosPath += pathComponent;
77 return true;
78 }
79 }
80 // Advance to the next NUL character in logicalDrives
81 while (*cur++);
82 } while (cur != end);
83 // Code for handling UNC paths would go here, if eventually required.
84 #if defined(DEBUG)
85 NS_NAMED_LITERAL_STRING(deviceMupPrefix, "\\Device\\Mup\\");
86 uint32_t deviceMupPrefixLen = deviceMupPrefix.Length();
87 if (ntPathLen >= deviceMupPrefixLen &&
88 Substring(aNtPath, 0, deviceMupPrefixLen).Equals(deviceMupPrefix)) {
89 NS_WARNING("UNC paths not yet supported in NtPathToDosPath");
90 }
91 #endif // defined(DEBUG)
92 return false;
93 }
94
95 bool
96 HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
97 nsAString& aFilename);
98
99 } // namespace mozilla
100
101 #endif // mozilla_FileUtilsWin_h

mercurial