|
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 #include "FileUtilsWin.h" |
|
8 |
|
9 #include <windows.h> |
|
10 #include <psapi.h> |
|
11 |
|
12 #include "nsWindowsHelpers.h" |
|
13 |
|
14 namespace { |
|
15 |
|
16 // Scoped type used by HandleToFilename |
|
17 struct ScopedMappedViewTraits |
|
18 { |
|
19 typedef void* type; |
|
20 static void* empty() { return nullptr; } |
|
21 static void release(void* ptr) { UnmapViewOfFile(ptr); } |
|
22 }; |
|
23 typedef mozilla::Scoped<ScopedMappedViewTraits> ScopedMappedView; |
|
24 |
|
25 } // anonymous namespace |
|
26 |
|
27 namespace mozilla { |
|
28 |
|
29 bool |
|
30 HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, |
|
31 nsAString& aFilename) |
|
32 { |
|
33 aFilename.Truncate(); |
|
34 // This implementation is nice because it uses fully documented APIs that |
|
35 // are available on all Windows versions that we support. |
|
36 nsAutoHandle fileMapping(CreateFileMapping(aHandle, nullptr, PAGE_READONLY, |
|
37 0, 1, nullptr)); |
|
38 if (!fileMapping) { |
|
39 return false; |
|
40 } |
|
41 ScopedMappedView view(MapViewOfFile(fileMapping, FILE_MAP_READ, |
|
42 aOffset.HighPart, aOffset.LowPart, 1)); |
|
43 if (!view) { |
|
44 return false; |
|
45 } |
|
46 nsAutoString mappedFilename; |
|
47 DWORD len = 0; |
|
48 SetLastError(ERROR_SUCCESS); |
|
49 do { |
|
50 mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH); |
|
51 len = GetMappedFileNameW(GetCurrentProcess(), view, |
|
52 wwc(mappedFilename.BeginWriting()), |
|
53 mappedFilename.Length()); |
|
54 } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER); |
|
55 if (!len) { |
|
56 return false; |
|
57 } |
|
58 mappedFilename.Truncate(len); |
|
59 return NtPathToDosPath(mappedFilename, aFilename); |
|
60 } |
|
61 |
|
62 } // namespace mozilla |
|
63 |