Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef mozilla_FileUtilsWin_h |
michael@0 | 8 | #define mozilla_FileUtilsWin_h |
michael@0 | 9 | |
michael@0 | 10 | #include <windows.h> |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/Scoped.h" |
michael@0 | 13 | #include "nsStringGlue.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | |
michael@0 | 17 | inline bool |
michael@0 | 18 | NtPathToDosPath(const nsAString& aNtPath, nsAString& aDosPath) |
michael@0 | 19 | { |
michael@0 | 20 | aDosPath.Truncate(); |
michael@0 | 21 | if (aNtPath.IsEmpty()) { |
michael@0 | 22 | return true; |
michael@0 | 23 | } |
michael@0 | 24 | NS_NAMED_LITERAL_STRING(symLinkPrefix, "\\??\\"); |
michael@0 | 25 | uint32_t ntPathLen = aNtPath.Length(); |
michael@0 | 26 | uint32_t symLinkPrefixLen = symLinkPrefix.Length(); |
michael@0 | 27 | if (ntPathLen >= 6 && aNtPath.CharAt(5) == L':' && |
michael@0 | 28 | ntPathLen >= symLinkPrefixLen && |
michael@0 | 29 | Substring(aNtPath, 0, symLinkPrefixLen).Equals(symLinkPrefix)) { |
michael@0 | 30 | // Symbolic link for DOS device. Just strip off the prefix. |
michael@0 | 31 | aDosPath = aNtPath; |
michael@0 | 32 | aDosPath.Cut(0, 4); |
michael@0 | 33 | return true; |
michael@0 | 34 | } |
michael@0 | 35 | nsAutoString logicalDrives; |
michael@0 | 36 | DWORD len = 0; |
michael@0 | 37 | while(true) { |
michael@0 | 38 | len = GetLogicalDriveStringsW(len, reinterpret_cast<wchar_t*>(logicalDrives.BeginWriting())); |
michael@0 | 39 | if (!len) { |
michael@0 | 40 | return false; |
michael@0 | 41 | } else if (len > logicalDrives.Length()) { |
michael@0 | 42 | logicalDrives.SetLength(len); |
michael@0 | 43 | } else { |
michael@0 | 44 | break; |
michael@0 | 45 | } |
michael@0 | 46 | } |
michael@0 | 47 | const char16_t* cur = logicalDrives.BeginReading(); |
michael@0 | 48 | const char16_t* end = logicalDrives.EndReading(); |
michael@0 | 49 | nsString targetPath; |
michael@0 | 50 | targetPath.SetLength(MAX_PATH); |
michael@0 | 51 | wchar_t driveTemplate[] = L" :"; |
michael@0 | 52 | do { |
michael@0 | 53 | // Unfortunately QueryDosDevice doesn't support the idiom for querying the |
michael@0 | 54 | // output buffer size, so it may require retries. |
michael@0 | 55 | driveTemplate[0] = *cur; |
michael@0 | 56 | DWORD targetPathLen = 0; |
michael@0 | 57 | SetLastError(ERROR_SUCCESS); |
michael@0 | 58 | while (true) { |
michael@0 | 59 | targetPathLen = QueryDosDeviceW(driveTemplate, reinterpret_cast<wchar_t*>(targetPath.BeginWriting()), |
michael@0 | 60 | targetPath.Length()); |
michael@0 | 61 | if (targetPathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) { |
michael@0 | 62 | break; |
michael@0 | 63 | } |
michael@0 | 64 | targetPath.SetLength(targetPath.Length() * 2); |
michael@0 | 65 | } |
michael@0 | 66 | if (targetPathLen) { |
michael@0 | 67 | // Need to use wcslen here because targetPath contains embedded NULL chars |
michael@0 | 68 | size_t firstTargetPathLen = wcslen(targetPath.get()); |
michael@0 | 69 | const char16_t* pathComponent = aNtPath.BeginReading() + |
michael@0 | 70 | firstTargetPathLen; |
michael@0 | 71 | bool found = _wcsnicmp(char16ptr_t(aNtPath.BeginReading()), targetPath.get(), |
michael@0 | 72 | firstTargetPathLen) == 0 && |
michael@0 | 73 | *pathComponent == L'\\'; |
michael@0 | 74 | if (found) { |
michael@0 | 75 | aDosPath = driveTemplate; |
michael@0 | 76 | aDosPath += pathComponent; |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | } |
michael@0 | 80 | // Advance to the next NUL character in logicalDrives |
michael@0 | 81 | while (*cur++); |
michael@0 | 82 | } while (cur != end); |
michael@0 | 83 | // Code for handling UNC paths would go here, if eventually required. |
michael@0 | 84 | #if defined(DEBUG) |
michael@0 | 85 | NS_NAMED_LITERAL_STRING(deviceMupPrefix, "\\Device\\Mup\\"); |
michael@0 | 86 | uint32_t deviceMupPrefixLen = deviceMupPrefix.Length(); |
michael@0 | 87 | if (ntPathLen >= deviceMupPrefixLen && |
michael@0 | 88 | Substring(aNtPath, 0, deviceMupPrefixLen).Equals(deviceMupPrefix)) { |
michael@0 | 89 | NS_WARNING("UNC paths not yet supported in NtPathToDosPath"); |
michael@0 | 90 | } |
michael@0 | 91 | #endif // defined(DEBUG) |
michael@0 | 92 | return false; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | bool |
michael@0 | 96 | HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, |
michael@0 | 97 | nsAString& aFilename); |
michael@0 | 98 | |
michael@0 | 99 | } // namespace mozilla |
michael@0 | 100 | |
michael@0 | 101 | #endif // mozilla_FileUtilsWin_h |