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: #include "FileUtilsWin.h" michael@0: michael@0: #include michael@0: #include michael@0: michael@0: #include "nsWindowsHelpers.h" michael@0: michael@0: namespace { michael@0: michael@0: // Scoped type used by HandleToFilename michael@0: struct ScopedMappedViewTraits michael@0: { michael@0: typedef void* type; michael@0: static void* empty() { return nullptr; } michael@0: static void release(void* ptr) { UnmapViewOfFile(ptr); } michael@0: }; michael@0: typedef mozilla::Scoped ScopedMappedView; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: namespace mozilla { michael@0: michael@0: bool michael@0: HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset, michael@0: nsAString& aFilename) michael@0: { michael@0: aFilename.Truncate(); michael@0: // This implementation is nice because it uses fully documented APIs that michael@0: // are available on all Windows versions that we support. michael@0: nsAutoHandle fileMapping(CreateFileMapping(aHandle, nullptr, PAGE_READONLY, michael@0: 0, 1, nullptr)); michael@0: if (!fileMapping) { michael@0: return false; michael@0: } michael@0: ScopedMappedView view(MapViewOfFile(fileMapping, FILE_MAP_READ, michael@0: aOffset.HighPart, aOffset.LowPart, 1)); michael@0: if (!view) { michael@0: return false; michael@0: } michael@0: nsAutoString mappedFilename; michael@0: DWORD len = 0; michael@0: SetLastError(ERROR_SUCCESS); michael@0: do { michael@0: mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH); michael@0: len = GetMappedFileNameW(GetCurrentProcess(), view, michael@0: wwc(mappedFilename.BeginWriting()), michael@0: mappedFilename.Length()); michael@0: } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER); michael@0: if (!len) { michael@0: return false; michael@0: } michael@0: mappedFilename.Truncate(len); michael@0: return NtPathToDosPath(mappedFilename, aFilename); michael@0: } michael@0: michael@0: } // namespace mozilla michael@0: