xpcom/io/FileUtilsWin.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/io/FileUtilsWin.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "FileUtilsWin.h"
    1.11 +
    1.12 +#include <windows.h>
    1.13 +#include <psapi.h>
    1.14 +
    1.15 +#include "nsWindowsHelpers.h"
    1.16 +
    1.17 +namespace {
    1.18 +
    1.19 +// Scoped type used by HandleToFilename
    1.20 +struct ScopedMappedViewTraits
    1.21 +{
    1.22 +  typedef void* type;
    1.23 +  static void* empty() { return nullptr; }
    1.24 +  static void release(void* ptr) { UnmapViewOfFile(ptr); }
    1.25 +};
    1.26 +typedef mozilla::Scoped<ScopedMappedViewTraits> ScopedMappedView;
    1.27 +
    1.28 +} // anonymous namespace
    1.29 +
    1.30 +namespace mozilla {
    1.31 +
    1.32 +bool
    1.33 +HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
    1.34 +                 nsAString& aFilename)
    1.35 +{
    1.36 +  aFilename.Truncate();
    1.37 +  // This implementation is nice because it uses fully documented APIs that
    1.38 +  // are available on all Windows versions that we support.
    1.39 +  nsAutoHandle fileMapping(CreateFileMapping(aHandle, nullptr, PAGE_READONLY,
    1.40 +                                             0, 1, nullptr));
    1.41 +  if (!fileMapping) {
    1.42 +    return false;
    1.43 +  }
    1.44 +  ScopedMappedView view(MapViewOfFile(fileMapping, FILE_MAP_READ,
    1.45 +                                      aOffset.HighPart, aOffset.LowPart, 1));
    1.46 +  if (!view) {
    1.47 +    return false;
    1.48 +  }
    1.49 +  nsAutoString mappedFilename;
    1.50 +  DWORD len = 0;
    1.51 +  SetLastError(ERROR_SUCCESS);
    1.52 +  do {
    1.53 +    mappedFilename.SetLength(mappedFilename.Length() + MAX_PATH);
    1.54 +    len = GetMappedFileNameW(GetCurrentProcess(), view,
    1.55 +                             wwc(mappedFilename.BeginWriting()),
    1.56 +                             mappedFilename.Length());
    1.57 +  } while (!len && GetLastError() == ERROR_INSUFFICIENT_BUFFER);
    1.58 +  if (!len) {
    1.59 +    return false;
    1.60 +  }
    1.61 +  mappedFilename.Truncate(len);
    1.62 +  return NtPathToDosPath(mappedFilename, aFilename);
    1.63 +}
    1.64 +
    1.65 +} // namespace mozilla
    1.66 +

mercurial