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