summaryrefslogtreecommitdiffstats
path: root/base/file_util_win.cc
diff options
context:
space:
mode:
authorskerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-17 22:44:31 +0000
committerskerner@chromium.org <skerner@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-01-17 22:44:31 +0000
commit71b7da7dbd4ed7fa4e28e23a796bd203601613aa (patch)
treed365103619c0a86e56d05eb74c2a121fb4424645 /base/file_util_win.cc
parent3a2630d61cbff2fffff994b9f9bcdb7f9fcc1893 (diff)
downloadchromium_src-71b7da7dbd4ed7fa4e28e23a796bd203601613aa.zip
chromium_src-71b7da7dbd4ed7fa4e28e23a796bd203601613aa.tar.gz
chromium_src-71b7da7dbd4ed7fa4e28e23a796bd203601613aa.tar.bz2
Match whole path components in DevicePathToDriveLetterPath(). Add tests.
+rvargas, who understands the code +brettw, who is in OWNERS BUG=109577 TEST=FileUtilTest.DevicePathToDriveLetter Review URL: http://codereview.chromium.org/9167004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@117986 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/file_util_win.cc')
-rw-r--r--base/file_util_win.cc88
1 files changed, 45 insertions, 43 deletions
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index 31d2f05..d1337a2 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
@@ -35,48 +35,6 @@ namespace {
const DWORD kFileShareAll =
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
-// Helper for NormalizeFilePath(), defined below.
-bool DevicePathToDriveLetterPath(const FilePath& device_path,
- FilePath* drive_letter_path) {
- base::ThreadRestrictions::AssertIOAllowed();
-
- // Get the mapping of drive letters to device paths.
- const int kDriveMappingSize = 1024;
- wchar_t drive_mapping[kDriveMappingSize] = {'\0'};
- if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) {
- DLOG(ERROR) << "Failed to get drive mapping.";
- return false;
- }
-
- // The drive mapping is a sequence of null terminated strings.
- // The last string is empty.
- wchar_t* drive_map_ptr = drive_mapping;
- wchar_t device_name[MAX_PATH];
- wchar_t drive[] = L" :";
-
- // For each string in the drive mapping, get the junction that links
- // to it. If that junction is a prefix of |device_path|, then we
- // know that |drive| is the real path prefix.
- while (*drive_map_ptr) {
- drive[0] = drive_map_ptr[0]; // Copy the drive letter.
-
- if (QueryDosDevice(drive, device_name, MAX_PATH) &&
- StartsWith(device_path.value(), device_name, true)) {
- *drive_letter_path = FilePath(drive +
- device_path.value().substr(wcslen(device_name)));
- return true;
- }
- // Move to the next drive letter string, which starts one
- // increment after the '\0' that terminates the current string.
- while (*drive_map_ptr++);
- }
-
- // No drive matched. The path does not start with a device junction
- // that is mounted as a drive letter. This means there is no drive
- // letter path to the volume that holds |device_path|, so fail.
- return false;
-}
-
} // namespace
bool AbsolutePath(FilePath* path) {
@@ -1047,6 +1005,50 @@ bool NormalizeFilePath(const FilePath& path, FilePath* real_path) {
return DevicePathToDriveLetterPath(mapped_file, real_path);
}
+bool DevicePathToDriveLetterPath(const FilePath& nt_device_path,
+ FilePath* out_drive_letter_path) {
+ base::ThreadRestrictions::AssertIOAllowed();
+
+ // Get the mapping of drive letters to device paths.
+ const int kDriveMappingSize = 1024;
+ wchar_t drive_mapping[kDriveMappingSize] = {'\0'};
+ if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) {
+ DLOG(ERROR) << "Failed to get drive mapping.";
+ return false;
+ }
+
+ // The drive mapping is a sequence of null terminated strings.
+ // The last string is empty.
+ wchar_t* drive_map_ptr = drive_mapping;
+ wchar_t device_path_as_string[MAX_PATH];
+ wchar_t drive[] = L" :";
+
+ // For each string in the drive mapping, get the junction that links
+ // to it. If that junction is a prefix of |device_path|, then we
+ // know that |drive| is the real path prefix.
+ while (*drive_map_ptr) {
+ drive[0] = drive_map_ptr[0]; // Copy the drive letter.
+
+ if (QueryDosDevice(drive, device_path_as_string, MAX_PATH)) {
+ FilePath device_path(device_path_as_string);
+ if (device_path == nt_device_path ||
+ device_path.IsParent(nt_device_path)) {
+ *out_drive_letter_path = FilePath(drive +
+ nt_device_path.value().substr(wcslen(device_path_as_string)));
+ return true;
+ }
+ }
+ // Move to the next drive letter string, which starts one
+ // increment after the '\0' that terminates the current string.
+ while (*drive_map_ptr++);
+ }
+
+ // No drive matched. The path does not start with a device junction
+ // that is mounted as a drive letter. This means there is no drive
+ // letter path to the volume that holds |device_path|, so fail.
+ return false;
+}
+
bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) {
base::ThreadRestrictions::AssertIOAllowed();
// In Vista, GetFinalPathNameByHandle() would give us the real path