OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <propvarutil.h> | 8 #include <propvarutil.h> |
9 #include <psapi.h> | 9 #include <psapi.h> |
10 #include <shellapi.h> | 10 #include <shellapi.h> |
11 #include <shlobj.h> | 11 #include <shlobj.h> |
12 #include <time.h> | 12 #include <time.h> |
13 | 13 |
14 #include <limits> | 14 #include <limits> |
15 #include <string> | 15 #include <string> |
16 | 16 |
17 #include "base/file_path.h" | 17 #include "base/file_path.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/string_number_conversions.h" | 20 #include "base/string_number_conversions.h" |
21 #include "base/string_util.h" | 21 #include "base/string_util.h" |
22 #include "base/threading/thread_restrictions.h" | 22 #include "base/threading/thread_restrictions.h" |
23 #include "base/time.h" | 23 #include "base/time.h" |
24 #include "base/utf_string_conversions.h" | 24 #include "base/utf_string_conversions.h" |
25 #include "base/win/pe_image.h" | 25 #include "base/win/pe_image.h" |
26 #include "base/win/scoped_comptr.h" | 26 #include "base/win/scoped_comptr.h" |
27 #include "base/win/scoped_handle.h" | 27 #include "base/win/scoped_handle.h" |
28 #include "base/win/win_api_interface.h" | |
28 #include "base/win/win_util.h" | 29 #include "base/win/win_util.h" |
29 #include "base/win/windows_version.h" | 30 #include "base/win/windows_version.h" |
30 | 31 |
31 namespace file_util { | 32 namespace file_util { |
32 | 33 |
33 namespace { | 34 namespace { |
34 | 35 |
35 const DWORD kFileShareAll = | 36 const DWORD kFileShareAll = |
36 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 37 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
37 | 38 |
39 } // namespace | |
40 | |
38 // Helper for NormalizeFilePath(), defined below. | 41 // Helper for NormalizeFilePath(), defined below. |
39 bool DevicePathToDriveLetterPath(const FilePath& device_path, | 42 bool DevicePathToDriveLetterPath(base::win::WinApiInterface& win_api, |
rvargas (doing something else)
2012/01/11 22:26:26
I don't think that adding an argument with fn poin
Sam Kerner (Chrome)
2012/01/11 23:26:27
If I tested the caller by adding a parameter, user
rvargas (doing something else)
2012/01/11 23:53:33
And that's one of the arguments against testing pa
| |
40 FilePath* drive_letter_path) { | 43 const FilePath& full_nt_device_path, |
44 FilePath* out_drive_letter_path) { | |
41 base::ThreadRestrictions::AssertIOAllowed(); | 45 base::ThreadRestrictions::AssertIOAllowed(); |
42 | 46 |
43 // Get the mapping of drive letters to device paths. | 47 // Get the mapping of drive letters to device paths. |
44 const int kDriveMappingSize = 1024; | 48 const int kDriveMappingSize = 1024; |
45 wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; | 49 wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; |
46 if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { | 50 if (!win_api.GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { |
47 DLOG(ERROR) << "Failed to get drive mapping."; | 51 DLOG(ERROR) << "Failed to get drive mapping."; |
48 return false; | 52 return false; |
49 } | 53 } |
50 | 54 |
51 // The drive mapping is a sequence of null terminated strings. | 55 // The drive mapping is a sequence of null terminated strings. |
52 // The last string is empty. | 56 // The last string is empty. |
53 wchar_t* drive_map_ptr = drive_mapping; | 57 wchar_t* drive_map_ptr = drive_mapping; |
54 wchar_t device_name[MAX_PATH]; | 58 wchar_t device_path_as_string[MAX_PATH]; |
55 wchar_t drive[] = L" :"; | 59 wchar_t drive[] = L" :"; |
56 | 60 |
57 // For each string in the drive mapping, get the junction that links | 61 // For each string in the drive mapping, get the junction that links |
58 // to it. If that junction is a prefix of |device_path|, then we | 62 // to it. If that junction is a prefix of |device_path|, then we |
59 // know that |drive| is the real path prefix. | 63 // know that |drive| is the real path prefix. |
60 while (*drive_map_ptr) { | 64 while (*drive_map_ptr) { |
61 drive[0] = drive_map_ptr[0]; // Copy the drive letter. | 65 drive[0] = drive_map_ptr[0]; // Copy the drive letter. |
62 | 66 |
63 if (QueryDosDevice(drive, device_name, MAX_PATH) && | 67 if (win_api.QueryDosDevice(drive, device_path_as_string, MAX_PATH)) { |
64 StartsWith(device_path.value(), device_name, true)) { | 68 FilePath device_path_to_drive(device_path_as_string); |
rvargas (doing something else)
2012/01/11 22:26:26
This name is confusing. How about just device_path
| |
65 *drive_letter_path = FilePath(drive + | 69 if (device_path_to_drive == full_nt_device_path || |
66 device_path.value().substr(wcslen(device_name))); | 70 device_path_to_drive.IsParent(full_nt_device_path)) { |
67 return true; | 71 *out_drive_letter_path = FilePath(drive + |
72 full_nt_device_path.value().substr(wcslen(device_path_as_string))); | |
73 return true; | |
74 } | |
68 } | 75 } |
69 // Move to the next drive letter string, which starts one | 76 // Move to the next drive letter string, which starts one |
70 // increment after the '\0' that terminates the current string. | 77 // increment after the '\0' that terminates the current string. |
71 while (*drive_map_ptr++); | 78 while (*drive_map_ptr++); |
72 } | 79 } |
73 | 80 |
74 // No drive matched. The path does not start with a device junction | 81 // No drive matched. The path does not start with a device junction |
75 // that is mounted as a drive letter. This means there is no drive | 82 // that is mounted as a drive letter. This means there is no drive |
76 // letter path to the volume that holds |device_path|, so fail. | 83 // letter path to the volume that holds |device_path|, so fail. |
77 return false; | 84 return false; |
78 } | 85 } |
79 | 86 |
80 } // namespace | |
81 | |
82 bool AbsolutePath(FilePath* path) { | 87 bool AbsolutePath(FilePath* path) { |
83 base::ThreadRestrictions::AssertIOAllowed(); | 88 base::ThreadRestrictions::AssertIOAllowed(); |
84 wchar_t file_path_buf[MAX_PATH]; | 89 wchar_t file_path_buf[MAX_PATH]; |
85 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) | 90 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) |
86 return false; | 91 return false; |
87 *path = FilePath(file_path_buf); | 92 *path = FilePath(file_path_buf); |
88 return true; | 93 return true; |
89 } | 94 } |
90 | 95 |
91 int CountFilesCreatedAfter(const FilePath& path, | 96 int CountFilesCreatedAfter(const FilePath& path, |
(...skipping 945 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1037 | 1042 |
1038 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { | 1043 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { |
1039 base::ThreadRestrictions::AssertIOAllowed(); | 1044 base::ThreadRestrictions::AssertIOAllowed(); |
1040 FilePath mapped_file; | 1045 FilePath mapped_file; |
1041 if (!NormalizeToNativeFilePath(path, &mapped_file)) | 1046 if (!NormalizeToNativeFilePath(path, &mapped_file)) |
1042 return false; | 1047 return false; |
1043 // NormalizeToNativeFilePath() will return a path that starts with | 1048 // NormalizeToNativeFilePath() will return a path that starts with |
1044 // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath() | 1049 // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath() |
1045 // will find a drive letter which maps to the path's device, so | 1050 // will find a drive letter which maps to the path's device, so |
1046 // that we return a path starting with a drive letter. | 1051 // that we return a path starting with a drive letter. |
1047 return DevicePathToDriveLetterPath(mapped_file, real_path); | 1052 |
1053 return DevicePathToDriveLetterPath( | |
1054 *base::win::WinApiInterface::GetInstance(), mapped_file, real_path); | |
1048 } | 1055 } |
1049 | 1056 |
1050 bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) { | 1057 bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) { |
1051 base::ThreadRestrictions::AssertIOAllowed(); | 1058 base::ThreadRestrictions::AssertIOAllowed(); |
1052 // In Vista, GetFinalPathNameByHandle() would give us the real path | 1059 // In Vista, GetFinalPathNameByHandle() would give us the real path |
1053 // from a file handle. If we ever deprecate XP, consider changing the | 1060 // from a file handle. If we ever deprecate XP, consider changing the |
1054 // code below to a call to GetFinalPathNameByHandle(). The method this | 1061 // code below to a call to GetFinalPathNameByHandle(). The method this |
1055 // function uses is explained in the following msdn article: | 1062 // function uses is explained in the following msdn article: |
1056 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx | 1063 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx |
1057 base::win::ScopedHandle file_handle( | 1064 base::win::ScopedHandle file_handle( |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1162 uint8 unused = *(touch + offset); | 1169 uint8 unused = *(touch + offset); |
1163 offset += step_size; | 1170 offset += step_size; |
1164 } | 1171 } |
1165 FreeLibrary(dll_module); | 1172 FreeLibrary(dll_module); |
1166 } | 1173 } |
1167 | 1174 |
1168 return true; | 1175 return true; |
1169 } | 1176 } |
1170 | 1177 |
1171 } // namespace file_util | 1178 } // namespace file_util |
OLD | NEW |