2014-01-31 19:14:03 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_FileUtilsWin_h
|
|
|
|
#define mozilla_FileUtilsWin_h
|
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
#include "mozilla/Scoped.h"
|
|
|
|
#include "nsStringGlue.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
NtPathToDosPath(const nsAString& aNtPath, nsAString& aDosPath)
|
|
|
|
{
|
|
|
|
aDosPath.Truncate();
|
|
|
|
if (aNtPath.IsEmpty()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_NAMED_LITERAL_STRING(symLinkPrefix, "\\??\\");
|
|
|
|
uint32_t ntPathLen = aNtPath.Length();
|
|
|
|
uint32_t symLinkPrefixLen = symLinkPrefix.Length();
|
|
|
|
if (ntPathLen >= 6 && aNtPath.CharAt(5) == L':' &&
|
|
|
|
ntPathLen >= symLinkPrefixLen &&
|
|
|
|
Substring(aNtPath, 0, symLinkPrefixLen).Equals(symLinkPrefix)) {
|
|
|
|
// Symbolic link for DOS device. Just strip off the prefix.
|
|
|
|
aDosPath = aNtPath;
|
|
|
|
aDosPath.Cut(0, 4);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
nsAutoString logicalDrives;
|
|
|
|
DWORD len = 0;
|
2014-05-14 13:15:46 -07:00
|
|
|
while (true) {
|
2014-08-25 12:17:24 -07:00
|
|
|
len = GetLogicalDriveStringsW(
|
|
|
|
len, reinterpret_cast<wchar_t*>(logicalDrives.BeginWriting()));
|
2014-01-31 19:14:03 -08:00
|
|
|
if (!len) {
|
|
|
|
return false;
|
|
|
|
} else if (len > logicalDrives.Length()) {
|
|
|
|
logicalDrives.SetLength(len);
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const char16_t* cur = logicalDrives.BeginReading();
|
|
|
|
const char16_t* end = logicalDrives.EndReading();
|
|
|
|
nsString targetPath;
|
|
|
|
targetPath.SetLength(MAX_PATH);
|
|
|
|
wchar_t driveTemplate[] = L" :";
|
|
|
|
do {
|
|
|
|
// Unfortunately QueryDosDevice doesn't support the idiom for querying the
|
|
|
|
// output buffer size, so it may require retries.
|
|
|
|
driveTemplate[0] = *cur;
|
|
|
|
DWORD targetPathLen = 0;
|
|
|
|
SetLastError(ERROR_SUCCESS);
|
|
|
|
while (true) {
|
2014-05-14 13:15:46 -07:00
|
|
|
targetPathLen = QueryDosDeviceW(driveTemplate,
|
|
|
|
reinterpret_cast<wchar_t*>(targetPath.BeginWriting()),
|
2014-01-31 19:14:03 -08:00
|
|
|
targetPath.Length());
|
|
|
|
if (targetPathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
targetPath.SetLength(targetPath.Length() * 2);
|
|
|
|
}
|
|
|
|
if (targetPathLen) {
|
|
|
|
// Need to use wcslen here because targetPath contains embedded NULL chars
|
|
|
|
size_t firstTargetPathLen = wcslen(targetPath.get());
|
|
|
|
const char16_t* pathComponent = aNtPath.BeginReading() +
|
|
|
|
firstTargetPathLen;
|
2014-02-13 02:25:26 -08:00
|
|
|
bool found = _wcsnicmp(char16ptr_t(aNtPath.BeginReading()), targetPath.get(),
|
2014-01-31 19:14:03 -08:00
|
|
|
firstTargetPathLen) == 0 &&
|
|
|
|
*pathComponent == L'\\';
|
|
|
|
if (found) {
|
|
|
|
aDosPath = driveTemplate;
|
|
|
|
aDosPath += pathComponent;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Advance to the next NUL character in logicalDrives
|
|
|
|
while (*cur++);
|
|
|
|
} while (cur != end);
|
2014-05-12 12:32:28 -07:00
|
|
|
// Try to handle UNC paths. NB: This must happen after we've checked drive
|
|
|
|
// mappings in case a UNC path is mapped to a drive!
|
|
|
|
NS_NAMED_LITERAL_STRING(uncPrefix, "\\\\");
|
2014-01-31 19:14:03 -08:00
|
|
|
NS_NAMED_LITERAL_STRING(deviceMupPrefix, "\\Device\\Mup\\");
|
2014-05-12 12:32:28 -07:00
|
|
|
if (StringBeginsWith(aNtPath, deviceMupPrefix)) {
|
|
|
|
aDosPath = uncPrefix;
|
|
|
|
aDosPath += Substring(aNtPath, deviceMupPrefix.Length());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
NS_NAMED_LITERAL_STRING(deviceLanmanRedirectorPrefix,
|
|
|
|
"\\Device\\LanmanRedirector\\");
|
|
|
|
if (StringBeginsWith(aNtPath, deviceLanmanRedirectorPrefix)) {
|
|
|
|
aDosPath = uncPrefix;
|
|
|
|
aDosPath += Substring(aNtPath, deviceLanmanRedirectorPrefix.Length());
|
|
|
|
return true;
|
2014-01-31 19:14:03 -08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
|
|
|
|
nsAString& aFilename);
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_FileUtilsWin_h
|