michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_FileUtilsWin_h michael@0: #define mozilla_FileUtilsWin_h michael@0: michael@0: #include michael@0: michael@0: #include "mozilla/Scoped.h" michael@0: #include "nsStringGlue.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: inline bool michael@0: NtPathToDosPath(const nsAString& aNtPath, nsAString& aDosPath) michael@0: { michael@0: aDosPath.Truncate(); michael@0: if (aNtPath.IsEmpty()) { michael@0: return true; michael@0: } michael@0: NS_NAMED_LITERAL_STRING(symLinkPrefix, "\\??\\"); michael@0: uint32_t ntPathLen = aNtPath.Length(); michael@0: uint32_t symLinkPrefixLen = symLinkPrefix.Length(); michael@0: if (ntPathLen >= 6 && aNtPath.CharAt(5) == L':' && michael@0: ntPathLen >= symLinkPrefixLen && michael@0: Substring(aNtPath, 0, symLinkPrefixLen).Equals(symLinkPrefix)) { michael@0: // Symbolic link for DOS device. Just strip off the prefix. michael@0: aDosPath = aNtPath; michael@0: aDosPath.Cut(0, 4); michael@0: return true; michael@0: } michael@0: nsAutoString logicalDrives; michael@0: DWORD len = 0; michael@0: while(true) { michael@0: len = GetLogicalDriveStringsW(len, reinterpret_cast(logicalDrives.BeginWriting())); michael@0: if (!len) { michael@0: return false; michael@0: } else if (len > logicalDrives.Length()) { michael@0: logicalDrives.SetLength(len); michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: const char16_t* cur = logicalDrives.BeginReading(); michael@0: const char16_t* end = logicalDrives.EndReading(); michael@0: nsString targetPath; michael@0: targetPath.SetLength(MAX_PATH); michael@0: wchar_t driveTemplate[] = L" :"; michael@0: do { michael@0: // Unfortunately QueryDosDevice doesn't support the idiom for querying the michael@0: // output buffer size, so it may require retries. michael@0: driveTemplate[0] = *cur; michael@0: DWORD targetPathLen = 0; michael@0: SetLastError(ERROR_SUCCESS); michael@0: while (true) { michael@0: targetPathLen = QueryDosDeviceW(driveTemplate, reinterpret_cast(targetPath.BeginWriting()), michael@0: targetPath.Length()); michael@0: if (targetPathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { michael@0: break; michael@0: } michael@0: targetPath.SetLength(targetPath.Length() * 2); michael@0: } michael@0: if (targetPathLen) { michael@0: // Need to use wcslen here because targetPath contains embedded NULL chars michael@0: size_t firstTargetPathLen = wcslen(targetPath.get()); michael@0: const char16_t* pathComponent = aNtPath.BeginReading() + michael@0: firstTargetPathLen; michael@0: bool found = _wcsnicmp(char16ptr_t(aNtPath.BeginReading()), targetPath.get(), michael@0: firstTargetPathLen) == 0 && michael@0: *pathComponent == L'\\'; michael@0: if (found) { michael@0: aDosPath = driveTemplate; michael@0: aDosPath += pathComponent; michael@0: return true; michael@0: } michael@0: } michael@0: // Advance to the next NUL character in logicalDrives michael@0: while (*cur++); michael@0: } while (cur != end); michael@0: // Code for handling UNC paths would go here, if eventually required. michael@0: #if defined(DEBUG) michael@0: NS_NAMED_LITERAL_STRING(deviceMupPrefix, "\\Device\\Mup\\"); michael@0: uint32_t deviceMupPrefixLen = deviceMupPrefix.Length(); michael@0: if (ntPathLen >= deviceMupPrefixLen && michael@0: Substring(aNtPath, 0, deviceMupPrefixLen).Equals(deviceMupPrefix)) { michael@0: NS_WARNING("UNC paths not yet supported in NtPathToDosPath"); michael@0: } michael@0: #endif // defined(DEBUG) michael@0: return false; michael@0: } michael@0: michael@0: bool michael@0: HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, michael@0: nsAString& aFilename); michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif // mozilla_FileUtilsWin_h