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"
12 #include "nsWindowsHelpers.h"
16 // Scoped type used by HandleToFilename
17 struct ScopedMappedViewTraits
24 static void release(void* aPtr
)
26 UnmapViewOfFile(aPtr
);
29 typedef mozilla::Scoped
<ScopedMappedViewTraits
> ScopedMappedView
;
31 } // anonymous namespace
36 HandleToFilename(HANDLE aHandle
, const LARGE_INTEGER
& aOffset
,
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
,
47 ScopedMappedView
view(MapViewOfFile(fileMapping
, FILE_MAP_READ
,
48 aOffset
.HighPart
, aOffset
.LowPart
, 1));
52 nsAutoString mappedFilename
;
54 SetLastError(ERROR_SUCCESS
);
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
);
64 mappedFilename
.Truncate(len
);
65 return NtPathToDosPath(mappedFilename
, aFilename
);
68 } // namespace mozilla