xpcom/io/FileUtilsWin.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/xpcom/io/FileUtilsWin.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     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 +#ifndef mozilla_FileUtilsWin_h
    1.11 +#define mozilla_FileUtilsWin_h
    1.12 +
    1.13 +#include <windows.h>
    1.14 +
    1.15 +#include "mozilla/Scoped.h"
    1.16 +#include "nsStringGlue.h"
    1.17 +
    1.18 +namespace mozilla {
    1.19 +
    1.20 +inline bool
    1.21 +NtPathToDosPath(const nsAString& aNtPath, nsAString& aDosPath)
    1.22 +{
    1.23 +  aDosPath.Truncate();
    1.24 +  if (aNtPath.IsEmpty()) {
    1.25 +    return true;
    1.26 +  }
    1.27 +  NS_NAMED_LITERAL_STRING(symLinkPrefix, "\\??\\");
    1.28 +  uint32_t ntPathLen = aNtPath.Length();
    1.29 +  uint32_t symLinkPrefixLen = symLinkPrefix.Length();
    1.30 +  if (ntPathLen >= 6 && aNtPath.CharAt(5) == L':' &&
    1.31 +      ntPathLen >= symLinkPrefixLen &&
    1.32 +      Substring(aNtPath, 0, symLinkPrefixLen).Equals(symLinkPrefix)) {
    1.33 +    // Symbolic link for DOS device. Just strip off the prefix.
    1.34 +    aDosPath = aNtPath;
    1.35 +    aDosPath.Cut(0, 4);
    1.36 +    return true;
    1.37 +  }
    1.38 +  nsAutoString logicalDrives;
    1.39 +  DWORD len = 0;
    1.40 +  while(true) {
    1.41 +    len = GetLogicalDriveStringsW(len, reinterpret_cast<wchar_t*>(logicalDrives.BeginWriting()));
    1.42 +    if (!len) {
    1.43 +      return false;
    1.44 +    } else if (len > logicalDrives.Length()) {
    1.45 +      logicalDrives.SetLength(len);
    1.46 +    } else {
    1.47 +      break;
    1.48 +    }
    1.49 +  }
    1.50 +  const char16_t* cur = logicalDrives.BeginReading();
    1.51 +  const char16_t* end = logicalDrives.EndReading();
    1.52 +  nsString targetPath;
    1.53 +  targetPath.SetLength(MAX_PATH);
    1.54 +  wchar_t driveTemplate[] = L" :";
    1.55 +  do {
    1.56 +    // Unfortunately QueryDosDevice doesn't support the idiom for querying the
    1.57 +    // output buffer size, so it may require retries.
    1.58 +    driveTemplate[0] = *cur;
    1.59 +    DWORD targetPathLen = 0;
    1.60 +    SetLastError(ERROR_SUCCESS);
    1.61 +    while (true) {
    1.62 +      targetPathLen = QueryDosDeviceW(driveTemplate, reinterpret_cast<wchar_t*>(targetPath.BeginWriting()),
    1.63 +                                      targetPath.Length());
    1.64 +      if (targetPathLen || GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
    1.65 +        break;
    1.66 +      }
    1.67 +      targetPath.SetLength(targetPath.Length() * 2);
    1.68 +    }
    1.69 +    if (targetPathLen) {
    1.70 +      // Need to use wcslen here because targetPath contains embedded NULL chars
    1.71 +      size_t firstTargetPathLen = wcslen(targetPath.get());
    1.72 +      const char16_t* pathComponent = aNtPath.BeginReading() +
    1.73 +                                      firstTargetPathLen;
    1.74 +      bool found = _wcsnicmp(char16ptr_t(aNtPath.BeginReading()), targetPath.get(),
    1.75 +                             firstTargetPathLen) == 0 &&
    1.76 +                   *pathComponent == L'\\';
    1.77 +      if (found) {
    1.78 +        aDosPath = driveTemplate;
    1.79 +        aDosPath += pathComponent;
    1.80 +        return true;
    1.81 +      }
    1.82 +    }
    1.83 +    // Advance to the next NUL character in logicalDrives
    1.84 +    while (*cur++);
    1.85 +  } while (cur != end);
    1.86 +  // Code for handling UNC paths would go here, if eventually required.
    1.87 +#if defined(DEBUG)
    1.88 +  NS_NAMED_LITERAL_STRING(deviceMupPrefix, "\\Device\\Mup\\");
    1.89 +  uint32_t deviceMupPrefixLen = deviceMupPrefix.Length();
    1.90 +  if (ntPathLen >= deviceMupPrefixLen &&
    1.91 +      Substring(aNtPath, 0, deviceMupPrefixLen).Equals(deviceMupPrefix)) {
    1.92 +    NS_WARNING("UNC paths not yet supported in NtPathToDosPath");
    1.93 +  }
    1.94 +#endif // defined(DEBUG)
    1.95 +  return false;
    1.96 +}
    1.97 +
    1.98 +bool
    1.99 +HandleToFilename(HANDLE aHandle, const LARGE_INTEGER& aOffset,
   1.100 +                 nsAString& aFilename);
   1.101 +
   1.102 +} // namespace mozilla
   1.103 +
   1.104 +#endif // mozilla_FileUtilsWin_h

mercurial