OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
18 #include "base/win/pe_image.h" | 18 #include "base/win/pe_image.h" |
19 #include "base/win/scoped_handle.h" | 19 #include "base/win/scoped_handle.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/thread_restrictions.h" | |
23 #include "base/time.h" | 22 #include "base/time.h" |
24 #include "base/utf_string_conversions.h" | 23 #include "base/utf_string_conversions.h" |
25 #include "base/win_util.h" | 24 #include "base/win_util.h" |
26 #include "base/win/scoped_comptr.h" | 25 #include "base/win/scoped_comptr.h" |
27 #include "base/win/windows_version.h" | 26 #include "base/win/windows_version.h" |
28 | 27 |
29 namespace file_util { | 28 namespace file_util { |
30 | 29 |
31 namespace { | 30 namespace { |
32 | 31 |
33 const DWORD kFileShareAll = | 32 const DWORD kFileShareAll = |
34 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; | 33 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE; |
35 | 34 |
36 // Helper for NormalizeFilePath(), defined below. | 35 // Helper for NormalizeFilePath(), defined below. |
37 bool DevicePathToDriveLetterPath(const FilePath& device_path, | 36 bool DevicePathToDriveLetterPath(const FilePath& device_path, |
38 FilePath* drive_letter_path) { | 37 FilePath* drive_letter_path) { |
39 base::ThreadRestrictions::AssertIOAllowed(); | |
40 | |
41 // Get the mapping of drive letters to device paths. | 38 // Get the mapping of drive letters to device paths. |
42 const int kDriveMappingSize = 1024; | 39 const int kDriveMappingSize = 1024; |
43 wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; | 40 wchar_t drive_mapping[kDriveMappingSize] = {'\0'}; |
44 if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { | 41 if (!::GetLogicalDriveStrings(kDriveMappingSize - 1, drive_mapping)) { |
45 LOG(ERROR) << "Failed to get drive mapping."; | 42 LOG(ERROR) << "Failed to get drive mapping."; |
46 return false; | 43 return false; |
47 } | 44 } |
48 | 45 |
49 // The drive mapping is a sequence of null terminated strings. | 46 // The drive mapping is a sequence of null terminated strings. |
50 // The last string is empty. | 47 // The last string is empty. |
(...skipping 20 matching lines...) Expand all Loading... |
71 | 68 |
72 // No drive matched. The path does not start with a device junction | 69 // No drive matched. The path does not start with a device junction |
73 // that is mounted as a drive letter. This means there is no drive | 70 // that is mounted as a drive letter. This means there is no drive |
74 // letter path to the volume that holds |device_path|, so fail. | 71 // letter path to the volume that holds |device_path|, so fail. |
75 return false; | 72 return false; |
76 } | 73 } |
77 | 74 |
78 } // namespace | 75 } // namespace |
79 | 76 |
80 std::wstring GetDirectoryFromPath(const std::wstring& path) { | 77 std::wstring GetDirectoryFromPath(const std::wstring& path) { |
81 base::ThreadRestrictions::AssertIOAllowed(); | |
82 wchar_t path_buffer[MAX_PATH]; | 78 wchar_t path_buffer[MAX_PATH]; |
83 wchar_t* file_ptr = NULL; | 79 wchar_t* file_ptr = NULL; |
84 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) | 80 if (GetFullPathName(path.c_str(), MAX_PATH, path_buffer, &file_ptr) == 0) |
85 return L""; | 81 return L""; |
86 | 82 |
87 std::wstring::size_type length = | 83 std::wstring::size_type length = |
88 file_ptr ? file_ptr - path_buffer : path.length(); | 84 file_ptr ? file_ptr - path_buffer : path.length(); |
89 std::wstring directory(path, 0, length); | 85 std::wstring directory(path, 0, length); |
90 return FilePath(directory).StripTrailingSeparators().value(); | 86 return FilePath(directory).StripTrailingSeparators().value(); |
91 } | 87 } |
92 | 88 |
93 bool AbsolutePath(FilePath* path) { | 89 bool AbsolutePath(FilePath* path) { |
94 base::ThreadRestrictions::AssertIOAllowed(); | |
95 wchar_t file_path_buf[MAX_PATH]; | 90 wchar_t file_path_buf[MAX_PATH]; |
96 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) | 91 if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) |
97 return false; | 92 return false; |
98 *path = FilePath(file_path_buf); | 93 *path = FilePath(file_path_buf); |
99 return true; | 94 return true; |
100 } | 95 } |
101 | 96 |
102 int CountFilesCreatedAfter(const FilePath& path, | 97 int CountFilesCreatedAfter(const FilePath& path, |
103 const base::Time& comparison_time) { | 98 const base::Time& comparison_time) { |
104 base::ThreadRestrictions::AssertIOAllowed(); | |
105 | |
106 int file_count = 0; | 99 int file_count = 0; |
107 FILETIME comparison_filetime(comparison_time.ToFileTime()); | 100 FILETIME comparison_filetime(comparison_time.ToFileTime()); |
108 | 101 |
109 WIN32_FIND_DATA find_file_data; | 102 WIN32_FIND_DATA find_file_data; |
110 // All files in given dir | 103 // All files in given dir |
111 std::wstring filename_spec = path.Append(L"*").value(); | 104 std::wstring filename_spec = path.Append(L"*").value(); |
112 HANDLE find_handle = FindFirstFile(filename_spec.c_str(), &find_file_data); | 105 HANDLE find_handle = FindFirstFile(filename_spec.c_str(), &find_file_data); |
113 if (find_handle != INVALID_HANDLE_VALUE) { | 106 if (find_handle != INVALID_HANDLE_VALUE) { |
114 do { | 107 do { |
115 // Don't count current or parent directories. | 108 // Don't count current or parent directories. |
116 if ((wcscmp(find_file_data.cFileName, L"..") == 0) || | 109 if ((wcscmp(find_file_data.cFileName, L"..") == 0) || |
117 (wcscmp(find_file_data.cFileName, L".") == 0)) | 110 (wcscmp(find_file_data.cFileName, L".") == 0)) |
118 continue; | 111 continue; |
119 | 112 |
120 long result = CompareFileTime(&find_file_data.ftCreationTime, | 113 long result = CompareFileTime(&find_file_data.ftCreationTime, |
121 &comparison_filetime); | 114 &comparison_filetime); |
122 // File was created after or on comparison time | 115 // File was created after or on comparison time |
123 if ((result == 1) || (result == 0)) | 116 if ((result == 1) || (result == 0)) |
124 ++file_count; | 117 ++file_count; |
125 } while (FindNextFile(find_handle, &find_file_data)); | 118 } while (FindNextFile(find_handle, &find_file_data)); |
126 FindClose(find_handle); | 119 FindClose(find_handle); |
127 } | 120 } |
128 | 121 |
129 return file_count; | 122 return file_count; |
130 } | 123 } |
131 | 124 |
132 bool Delete(const FilePath& path, bool recursive) { | 125 bool Delete(const FilePath& path, bool recursive) { |
133 base::ThreadRestrictions::AssertIOAllowed(); | |
134 | |
135 if (path.value().length() >= MAX_PATH) | 126 if (path.value().length() >= MAX_PATH) |
136 return false; | 127 return false; |
137 | 128 |
138 if (!recursive) { | 129 if (!recursive) { |
139 // If not recursing, then first check to see if |path| is a directory. | 130 // If not recursing, then first check to see if |path| is a directory. |
140 // If it is, then remove it with RemoveDirectory. | 131 // If it is, then remove it with RemoveDirectory. |
141 base::PlatformFileInfo file_info; | 132 base::PlatformFileInfo file_info; |
142 if (GetFileInfo(path, &file_info) && file_info.is_directory) | 133 if (GetFileInfo(path, &file_info) && file_info.is_directory) |
143 return RemoveDirectory(path.value().c_str()) != 0; | 134 return RemoveDirectory(path.value().c_str()) != 0; |
144 | 135 |
(...skipping 18 matching lines...) Expand all Loading... |
163 if (!recursive) | 154 if (!recursive) |
164 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 155 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
165 int err = SHFileOperation(&file_operation); | 156 int err = SHFileOperation(&file_operation); |
166 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting | 157 // Some versions of Windows return ERROR_FILE_NOT_FOUND (0x2) when deleting |
167 // an empty directory and some return 0x402 when they should be returning | 158 // an empty directory and some return 0x402 when they should be returning |
168 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. | 159 // ERROR_FILE_NOT_FOUND. MSDN says Vista and up won't return 0x402. |
169 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); | 160 return (err == 0 || err == ERROR_FILE_NOT_FOUND || err == 0x402); |
170 } | 161 } |
171 | 162 |
172 bool DeleteAfterReboot(const FilePath& path) { | 163 bool DeleteAfterReboot(const FilePath& path) { |
173 base::ThreadRestrictions::AssertIOAllowed(); | |
174 | |
175 if (path.value().length() >= MAX_PATH) | 164 if (path.value().length() >= MAX_PATH) |
176 return false; | 165 return false; |
177 | 166 |
178 return MoveFileEx(path.value().c_str(), NULL, | 167 return MoveFileEx(path.value().c_str(), NULL, |
179 MOVEFILE_DELAY_UNTIL_REBOOT | | 168 MOVEFILE_DELAY_UNTIL_REBOOT | |
180 MOVEFILE_REPLACE_EXISTING) != FALSE; | 169 MOVEFILE_REPLACE_EXISTING) != FALSE; |
181 } | 170 } |
182 | 171 |
183 bool Move(const FilePath& from_path, const FilePath& to_path) { | 172 bool Move(const FilePath& from_path, const FilePath& to_path) { |
184 base::ThreadRestrictions::AssertIOAllowed(); | |
185 | |
186 // NOTE: I suspect we could support longer paths, but that would involve | 173 // NOTE: I suspect we could support longer paths, but that would involve |
187 // analyzing all our usage of files. | 174 // analyzing all our usage of files. |
188 if (from_path.value().length() >= MAX_PATH || | 175 if (from_path.value().length() >= MAX_PATH || |
189 to_path.value().length() >= MAX_PATH) { | 176 to_path.value().length() >= MAX_PATH) { |
190 return false; | 177 return false; |
191 } | 178 } |
192 if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), | 179 if (MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), |
193 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) | 180 MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING) != 0) |
194 return true; | 181 return true; |
195 if (DirectoryExists(from_path)) { | 182 if (DirectoryExists(from_path)) { |
196 // MoveFileEx fails if moving directory across volumes. We will simulate | 183 // MoveFileEx fails if moving directory across volumes. We will simulate |
197 // the move by using Copy and Delete. Ideally we could check whether | 184 // the move by using Copy and Delete. Ideally we could check whether |
198 // from_path and to_path are indeed in different volumes. | 185 // from_path and to_path are indeed in different volumes. |
199 return CopyAndDeleteDirectory(from_path, to_path); | 186 return CopyAndDeleteDirectory(from_path, to_path); |
200 } | 187 } |
201 return false; | 188 return false; |
202 } | 189 } |
203 | 190 |
204 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { | 191 bool ReplaceFile(const FilePath& from_path, const FilePath& to_path) { |
205 base::ThreadRestrictions::AssertIOAllowed(); | |
206 | |
207 // Make sure that the target file exists. | 192 // Make sure that the target file exists. |
208 HANDLE target_file = ::CreateFile( | 193 HANDLE target_file = ::CreateFile( |
209 to_path.value().c_str(), | 194 to_path.value().c_str(), |
210 0, | 195 0, |
211 FILE_SHARE_READ | FILE_SHARE_WRITE, | 196 FILE_SHARE_READ | FILE_SHARE_WRITE, |
212 NULL, | 197 NULL, |
213 CREATE_NEW, | 198 CREATE_NEW, |
214 FILE_ATTRIBUTE_NORMAL, | 199 FILE_ATTRIBUTE_NORMAL, |
215 NULL); | 200 NULL); |
216 if (target_file != INVALID_HANDLE_VALUE) | 201 if (target_file != INVALID_HANDLE_VALUE) |
217 ::CloseHandle(target_file); | 202 ::CloseHandle(target_file); |
218 // When writing to a network share, we may not be able to change the ACLs. | 203 // When writing to a network share, we may not be able to change the ACLs. |
219 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). | 204 // Ignore ACL errors then (REPLACEFILE_IGNORE_MERGE_ERRORS). |
220 return ::ReplaceFile(to_path.value().c_str(), | 205 return ::ReplaceFile(to_path.value().c_str(), |
221 from_path.value().c_str(), NULL, | 206 from_path.value().c_str(), NULL, |
222 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; | 207 REPLACEFILE_IGNORE_MERGE_ERRORS, NULL, NULL) ? true : false; |
223 } | 208 } |
224 | 209 |
225 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { | 210 bool CopyFile(const FilePath& from_path, const FilePath& to_path) { |
226 base::ThreadRestrictions::AssertIOAllowed(); | |
227 | |
228 // NOTE: I suspect we could support longer paths, but that would involve | 211 // NOTE: I suspect we could support longer paths, but that would involve |
229 // analyzing all our usage of files. | 212 // analyzing all our usage of files. |
230 if (from_path.value().length() >= MAX_PATH || | 213 if (from_path.value().length() >= MAX_PATH || |
231 to_path.value().length() >= MAX_PATH) { | 214 to_path.value().length() >= MAX_PATH) { |
232 return false; | 215 return false; |
233 } | 216 } |
234 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), | 217 return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(), |
235 false) != 0); | 218 false) != 0); |
236 } | 219 } |
237 | 220 |
238 bool ShellCopy(const FilePath& from_path, const FilePath& to_path, | 221 bool ShellCopy(const FilePath& from_path, const FilePath& to_path, |
239 bool recursive) { | 222 bool recursive) { |
240 base::ThreadRestrictions::AssertIOAllowed(); | |
241 | |
242 // NOTE: I suspect we could support longer paths, but that would involve | 223 // NOTE: I suspect we could support longer paths, but that would involve |
243 // analyzing all our usage of files. | 224 // analyzing all our usage of files. |
244 if (from_path.value().length() >= MAX_PATH || | 225 if (from_path.value().length() >= MAX_PATH || |
245 to_path.value().length() >= MAX_PATH) { | 226 to_path.value().length() >= MAX_PATH) { |
246 return false; | 227 return false; |
247 } | 228 } |
248 | 229 |
249 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, | 230 // SHFILEOPSTRUCT wants the path to be terminated with two NULLs, |
250 // so we have to use wcscpy because wcscpy_s writes non-NULLs | 231 // so we have to use wcscpy because wcscpy_s writes non-NULLs |
251 // into the rest of the buffer. | 232 // into the rest of the buffer. |
(...skipping 11 matching lines...) Expand all Loading... |
263 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | | 244 file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | |
264 FOF_NOCONFIRMMKDIR; | 245 FOF_NOCONFIRMMKDIR; |
265 if (!recursive) | 246 if (!recursive) |
266 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; | 247 file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY; |
267 | 248 |
268 return (SHFileOperation(&file_operation) == 0); | 249 return (SHFileOperation(&file_operation) == 0); |
269 } | 250 } |
270 | 251 |
271 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, | 252 bool CopyDirectory(const FilePath& from_path, const FilePath& to_path, |
272 bool recursive) { | 253 bool recursive) { |
273 base::ThreadRestrictions::AssertIOAllowed(); | |
274 | |
275 if (recursive) | 254 if (recursive) |
276 return ShellCopy(from_path, to_path, true); | 255 return ShellCopy(from_path, to_path, true); |
277 | 256 |
278 // The following code assumes that from path is a directory. | 257 // The following code assumes that from path is a directory. |
279 DCHECK(DirectoryExists(from_path)); | 258 DCHECK(DirectoryExists(from_path)); |
280 | 259 |
281 // Instead of creating a new directory, we copy the old one to include the | 260 // Instead of creating a new directory, we copy the old one to include the |
282 // security information of the folder as part of the copy. | 261 // security information of the folder as part of the copy. |
283 if (!PathExists(to_path)) { | 262 if (!PathExists(to_path)) { |
284 // Except that Vista fails to do that, and instead do a recursive copy if | 263 // Except that Vista fails to do that, and instead do a recursive copy if |
285 // the target directory doesn't exist. | 264 // the target directory doesn't exist. |
286 if (base::win::GetVersion() >= base::win::VERSION_VISTA) | 265 if (base::win::GetVersion() >= base::win::VERSION_VISTA) |
287 CreateDirectory(to_path); | 266 CreateDirectory(to_path); |
288 else | 267 else |
289 ShellCopy(from_path, to_path, false); | 268 ShellCopy(from_path, to_path, false); |
290 } | 269 } |
291 | 270 |
292 FilePath directory = from_path.Append(L"*.*"); | 271 FilePath directory = from_path.Append(L"*.*"); |
293 return ShellCopy(directory, to_path, false); | 272 return ShellCopy(directory, to_path, false); |
294 } | 273 } |
295 | 274 |
296 bool CopyAndDeleteDirectory(const FilePath& from_path, | 275 bool CopyAndDeleteDirectory(const FilePath& from_path, |
297 const FilePath& to_path) { | 276 const FilePath& to_path) { |
298 base::ThreadRestrictions::AssertIOAllowed(); | |
299 if (CopyDirectory(from_path, to_path, true)) { | 277 if (CopyDirectory(from_path, to_path, true)) { |
300 if (Delete(from_path, true)) { | 278 if (Delete(from_path, true)) { |
301 return true; | 279 return true; |
302 } | 280 } |
303 // Like Move, this function is not transactional, so we just | 281 // Like Move, this function is not transactional, so we just |
304 // leave the copied bits behind if deleting from_path fails. | 282 // leave the copied bits behind if deleting from_path fails. |
305 // If to_path exists previously then we have already overwritten | 283 // If to_path exists previously then we have already overwritten |
306 // it by now, we don't get better off by deleting the new bits. | 284 // it by now, we don't get better off by deleting the new bits. |
307 } | 285 } |
308 return false; | 286 return false; |
309 } | 287 } |
310 | 288 |
311 | 289 |
312 bool PathExists(const FilePath& path) { | 290 bool PathExists(const FilePath& path) { |
313 base::ThreadRestrictions::AssertIOAllowed(); | |
314 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); | 291 return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES); |
315 } | 292 } |
316 | 293 |
317 bool PathIsWritable(const FilePath& path) { | 294 bool PathIsWritable(const FilePath& path) { |
318 base::ThreadRestrictions::AssertIOAllowed(); | |
319 HANDLE dir = | 295 HANDLE dir = |
320 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, | 296 CreateFile(path.value().c_str(), FILE_ADD_FILE, kFileShareAll, |
321 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); | 297 NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL); |
322 | 298 |
323 if (dir == INVALID_HANDLE_VALUE) | 299 if (dir == INVALID_HANDLE_VALUE) |
324 return false; | 300 return false; |
325 | 301 |
326 CloseHandle(dir); | 302 CloseHandle(dir); |
327 return true; | 303 return true; |
328 } | 304 } |
329 | 305 |
330 bool DirectoryExists(const FilePath& path) { | 306 bool DirectoryExists(const FilePath& path) { |
331 base::ThreadRestrictions::AssertIOAllowed(); | |
332 DWORD fileattr = GetFileAttributes(path.value().c_str()); | 307 DWORD fileattr = GetFileAttributes(path.value().c_str()); |
333 if (fileattr != INVALID_FILE_ATTRIBUTES) | 308 if (fileattr != INVALID_FILE_ATTRIBUTES) |
334 return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0; | 309 return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0; |
335 return false; | 310 return false; |
336 } | 311 } |
337 | 312 |
338 bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, | 313 bool GetFileCreationLocalTimeFromHandle(HANDLE file_handle, |
339 LPSYSTEMTIME creation_time) { | 314 LPSYSTEMTIME creation_time) { |
340 base::ThreadRestrictions::AssertIOAllowed(); | |
341 if (!file_handle) | 315 if (!file_handle) |
342 return false; | 316 return false; |
343 | 317 |
344 FILETIME utc_filetime; | 318 FILETIME utc_filetime; |
345 if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) | 319 if (!GetFileTime(file_handle, &utc_filetime, NULL, NULL)) |
346 return false; | 320 return false; |
347 | 321 |
348 FILETIME local_filetime; | 322 FILETIME local_filetime; |
349 if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) | 323 if (!FileTimeToLocalFileTime(&utc_filetime, &local_filetime)) |
350 return false; | 324 return false; |
351 | 325 |
352 return !!FileTimeToSystemTime(&local_filetime, creation_time); | 326 return !!FileTimeToSystemTime(&local_filetime, creation_time); |
353 } | 327 } |
354 | 328 |
355 bool GetFileCreationLocalTime(const std::wstring& filename, | 329 bool GetFileCreationLocalTime(const std::wstring& filename, |
356 LPSYSTEMTIME creation_time) { | 330 LPSYSTEMTIME creation_time) { |
357 base::ThreadRestrictions::AssertIOAllowed(); | |
358 base::win::ScopedHandle file_handle( | 331 base::win::ScopedHandle file_handle( |
359 CreateFile(filename.c_str(), GENERIC_READ, kFileShareAll, NULL, | 332 CreateFile(filename.c_str(), GENERIC_READ, kFileShareAll, NULL, |
360 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); | 333 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)); |
361 return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); | 334 return GetFileCreationLocalTimeFromHandle(file_handle.Get(), creation_time); |
362 } | 335 } |
363 | 336 |
364 bool ResolveShortcut(FilePath* path) { | 337 bool ResolveShortcut(FilePath* path) { |
365 base::ThreadRestrictions::AssertIOAllowed(); | |
366 | |
367 HRESULT result; | 338 HRESULT result; |
368 base::win::ScopedComPtr<IShellLink> i_shell_link; | 339 base::win::ScopedComPtr<IShellLink> i_shell_link; |
369 bool is_resolved = false; | 340 bool is_resolved = false; |
370 | 341 |
371 // Get pointer to the IShellLink interface | 342 // Get pointer to the IShellLink interface |
372 result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | 343 result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, |
373 CLSCTX_INPROC_SERVER); | 344 CLSCTX_INPROC_SERVER); |
374 if (SUCCEEDED(result)) { | 345 if (SUCCEEDED(result)) { |
375 base::win::ScopedComPtr<IPersistFile> persist; | 346 base::win::ScopedComPtr<IPersistFile> persist; |
376 // Query IShellLink for the IPersistFile interface | 347 // Query IShellLink for the IPersistFile interface |
(...skipping 15 matching lines...) Expand all Loading... |
392 } | 363 } |
393 } | 364 } |
394 | 365 |
395 return is_resolved; | 366 return is_resolved; |
396 } | 367 } |
397 | 368 |
398 bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination, | 369 bool CreateShortcutLink(const wchar_t *source, const wchar_t *destination, |
399 const wchar_t *working_dir, const wchar_t *arguments, | 370 const wchar_t *working_dir, const wchar_t *arguments, |
400 const wchar_t *description, const wchar_t *icon, | 371 const wchar_t *description, const wchar_t *icon, |
401 int icon_index, const wchar_t* app_id) { | 372 int icon_index, const wchar_t* app_id) { |
402 base::ThreadRestrictions::AssertIOAllowed(); | |
403 | |
404 // Length of arguments and description must be less than MAX_PATH. | 373 // Length of arguments and description must be less than MAX_PATH. |
405 DCHECK(lstrlen(arguments) < MAX_PATH); | 374 DCHECK(lstrlen(arguments) < MAX_PATH); |
406 DCHECK(lstrlen(description) < MAX_PATH); | 375 DCHECK(lstrlen(description) < MAX_PATH); |
407 | 376 |
408 base::win::ScopedComPtr<IShellLink> i_shell_link; | 377 base::win::ScopedComPtr<IShellLink> i_shell_link; |
409 base::win::ScopedComPtr<IPersistFile> i_persist_file; | 378 base::win::ScopedComPtr<IPersistFile> i_persist_file; |
410 | 379 |
411 // Get pointer to the IShellLink interface | 380 // Get pointer to the IShellLink interface |
412 HRESULT result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | 381 HRESULT result = i_shell_link.CreateInstance(CLSID_ShellLink, NULL, |
413 CLSCTX_INPROC_SERVER); | 382 CLSCTX_INPROC_SERVER); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 414 |
446 result = i_persist_file->Save(destination, TRUE); | 415 result = i_persist_file->Save(destination, TRUE); |
447 return SUCCEEDED(result); | 416 return SUCCEEDED(result); |
448 } | 417 } |
449 | 418 |
450 | 419 |
451 bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination, | 420 bool UpdateShortcutLink(const wchar_t *source, const wchar_t *destination, |
452 const wchar_t *working_dir, const wchar_t *arguments, | 421 const wchar_t *working_dir, const wchar_t *arguments, |
453 const wchar_t *description, const wchar_t *icon, | 422 const wchar_t *description, const wchar_t *icon, |
454 int icon_index, const wchar_t* app_id) { | 423 int icon_index, const wchar_t* app_id) { |
455 base::ThreadRestrictions::AssertIOAllowed(); | |
456 | |
457 // Length of arguments and description must be less than MAX_PATH. | 424 // Length of arguments and description must be less than MAX_PATH. |
458 DCHECK(lstrlen(arguments) < MAX_PATH); | 425 DCHECK(lstrlen(arguments) < MAX_PATH); |
459 DCHECK(lstrlen(description) < MAX_PATH); | 426 DCHECK(lstrlen(description) < MAX_PATH); |
460 | 427 |
461 // Get pointer to the IPersistFile interface and load existing link | 428 // Get pointer to the IPersistFile interface and load existing link |
462 base::win::ScopedComPtr<IShellLink> i_shell_link; | 429 base::win::ScopedComPtr<IShellLink> i_shell_link; |
463 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, | 430 if (FAILED(i_shell_link.CreateInstance(CLSID_ShellLink, NULL, |
464 CLSCTX_INPROC_SERVER))) | 431 CLSCTX_INPROC_SERVER))) |
465 return false; | 432 return false; |
466 | 433 |
(...skipping 26 matching lines...) Expand all Loading... |
493 | 460 |
494 if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) | 461 if (!win_util::SetAppIdForPropertyStore(property_store, app_id)) |
495 return false; | 462 return false; |
496 } | 463 } |
497 | 464 |
498 HRESULT result = i_persist_file->Save(destination, TRUE); | 465 HRESULT result = i_persist_file->Save(destination, TRUE); |
499 return SUCCEEDED(result); | 466 return SUCCEEDED(result); |
500 } | 467 } |
501 | 468 |
502 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { | 469 bool TaskbarPinShortcutLink(const wchar_t* shortcut) { |
503 base::ThreadRestrictions::AssertIOAllowed(); | |
504 | |
505 // "Pin to taskbar" is only supported after Win7. | 470 // "Pin to taskbar" is only supported after Win7. |
506 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 471 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
507 return false; | 472 return false; |
508 | 473 |
509 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, | 474 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarpin", shortcut, |
510 NULL, NULL, 0)); | 475 NULL, NULL, 0)); |
511 return result > 32; | 476 return result > 32; |
512 } | 477 } |
513 | 478 |
514 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { | 479 bool TaskbarUnpinShortcutLink(const wchar_t* shortcut) { |
515 base::ThreadRestrictions::AssertIOAllowed(); | |
516 | |
517 // "Unpin from taskbar" is only supported after Win7. | 480 // "Unpin from taskbar" is only supported after Win7. |
518 if (base::win::GetVersion() < base::win::VERSION_WIN7) | 481 if (base::win::GetVersion() < base::win::VERSION_WIN7) |
519 return false; | 482 return false; |
520 | 483 |
521 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", | 484 int result = reinterpret_cast<int>(ShellExecute(NULL, L"taskbarunpin", |
522 shortcut, NULL, NULL, 0)); | 485 shortcut, NULL, NULL, 0)); |
523 return result > 32; | 486 return result > 32; |
524 } | 487 } |
525 | 488 |
526 bool GetTempDir(FilePath* path) { | 489 bool GetTempDir(FilePath* path) { |
527 base::ThreadRestrictions::AssertIOAllowed(); | |
528 | |
529 wchar_t temp_path[MAX_PATH + 1]; | 490 wchar_t temp_path[MAX_PATH + 1]; |
530 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); | 491 DWORD path_len = ::GetTempPath(MAX_PATH, temp_path); |
531 if (path_len >= MAX_PATH || path_len <= 0) | 492 if (path_len >= MAX_PATH || path_len <= 0) |
532 return false; | 493 return false; |
533 // TODO(evanm): the old behavior of this function was to always strip the | 494 // TODO(evanm): the old behavior of this function was to always strip the |
534 // trailing slash. We duplicate this here, but it shouldn't be necessary | 495 // trailing slash. We duplicate this here, but it shouldn't be necessary |
535 // when everyone is using the appropriate FilePath APIs. | 496 // when everyone is using the appropriate FilePath APIs. |
536 *path = FilePath(temp_path).StripTrailingSeparators(); | 497 *path = FilePath(temp_path).StripTrailingSeparators(); |
537 return true; | 498 return true; |
538 } | 499 } |
539 | 500 |
540 bool GetShmemTempDir(FilePath* path) { | 501 bool GetShmemTempDir(FilePath* path) { |
541 return GetTempDir(path); | 502 return GetTempDir(path); |
542 } | 503 } |
543 | 504 |
544 bool CreateTemporaryFile(FilePath* path) { | 505 bool CreateTemporaryFile(FilePath* path) { |
545 base::ThreadRestrictions::AssertIOAllowed(); | |
546 | |
547 FilePath temp_file; | 506 FilePath temp_file; |
548 | 507 |
549 if (!GetTempDir(path)) | 508 if (!GetTempDir(path)) |
550 return false; | 509 return false; |
551 | 510 |
552 if (CreateTemporaryFileInDir(*path, &temp_file)) { | 511 if (CreateTemporaryFileInDir(*path, &temp_file)) { |
553 *path = temp_file; | 512 *path = temp_file; |
554 return true; | 513 return true; |
555 } | 514 } |
556 | 515 |
557 return false; | 516 return false; |
558 } | 517 } |
559 | 518 |
560 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { | 519 FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) { |
561 base::ThreadRestrictions::AssertIOAllowed(); | |
562 return CreateAndOpenTemporaryFile(path); | 520 return CreateAndOpenTemporaryFile(path); |
563 } | 521 } |
564 | 522 |
565 // On POSIX we have semantics to create and open a temporary file | 523 // On POSIX we have semantics to create and open a temporary file |
566 // atomically. | 524 // atomically. |
567 // TODO(jrg): is there equivalent call to use on Windows instead of | 525 // TODO(jrg): is there equivalent call to use on Windows instead of |
568 // going 2-step? | 526 // going 2-step? |
569 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { | 527 FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) { |
570 base::ThreadRestrictions::AssertIOAllowed(); | |
571 if (!CreateTemporaryFileInDir(dir, path)) { | 528 if (!CreateTemporaryFileInDir(dir, path)) { |
572 return NULL; | 529 return NULL; |
573 } | 530 } |
574 // Open file in binary mode, to avoid problems with fwrite. On Windows | 531 // Open file in binary mode, to avoid problems with fwrite. On Windows |
575 // it replaces \n's with \r\n's, which may surprise you. | 532 // it replaces \n's with \r\n's, which may surprise you. |
576 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx | 533 // Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx |
577 return OpenFile(*path, "wb+"); | 534 return OpenFile(*path, "wb+"); |
578 } | 535 } |
579 | 536 |
580 bool CreateTemporaryFileInDir(const FilePath& dir, | 537 bool CreateTemporaryFileInDir(const FilePath& dir, |
581 FilePath* temp_file) { | 538 FilePath* temp_file) { |
582 base::ThreadRestrictions::AssertIOAllowed(); | |
583 | |
584 wchar_t temp_name[MAX_PATH + 1]; | 539 wchar_t temp_name[MAX_PATH + 1]; |
585 | 540 |
586 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { | 541 if (!GetTempFileName(dir.value().c_str(), L"", 0, temp_name)) { |
587 PLOG(WARNING) << "Failed to get temporary file name in " << dir.value(); | 542 PLOG(WARNING) << "Failed to get temporary file name in " << dir.value(); |
588 return false; | 543 return false; |
589 } | 544 } |
590 | 545 |
591 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); | 546 DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH); |
592 if (path_len > MAX_PATH + 1 || path_len == 0) { | 547 if (path_len > MAX_PATH + 1 || path_len == 0) { |
593 PLOG(WARNING) << "Failed to get long path name for " << temp_name; | 548 PLOG(WARNING) << "Failed to get long path name for " << temp_name; |
594 return false; | 549 return false; |
595 } | 550 } |
596 | 551 |
597 std::wstring temp_file_str; | 552 std::wstring temp_file_str; |
598 temp_file_str.assign(temp_name, path_len); | 553 temp_file_str.assign(temp_name, path_len); |
599 *temp_file = FilePath(temp_file_str); | 554 *temp_file = FilePath(temp_file_str); |
600 return true; | 555 return true; |
601 } | 556 } |
602 | 557 |
603 bool CreateTemporaryDirInDir(const FilePath& base_dir, | 558 bool CreateTemporaryDirInDir(const FilePath& base_dir, |
604 const FilePath::StringType& prefix, | 559 const FilePath::StringType& prefix, |
605 FilePath* new_dir) { | 560 FilePath* new_dir) { |
606 base::ThreadRestrictions::AssertIOAllowed(); | |
607 | |
608 FilePath path_to_create; | 561 FilePath path_to_create; |
609 srand(static_cast<uint32>(time(NULL))); | 562 srand(static_cast<uint32>(time(NULL))); |
610 | 563 |
611 for (int count = 0; count < 50; ++count) { | 564 for (int count = 0; count < 50; ++count) { |
612 // Try create a new temporary directory with random generated name. If | 565 // Try create a new temporary directory with random generated name. If |
613 // the one exists, keep trying another path name until we reach some limit. | 566 // the one exists, keep trying another path name until we reach some limit. |
614 path_to_create = base_dir; | 567 path_to_create = base_dir; |
615 | 568 |
616 string16 new_dir_name; | 569 string16 new_dir_name; |
617 new_dir_name.assign(prefix); | 570 new_dir_name.assign(prefix); |
618 new_dir_name.append(base::IntToString16(rand() % kint16max)); | 571 new_dir_name.append(base::IntToString16(rand() % kint16max)); |
619 | 572 |
620 path_to_create = path_to_create.Append(new_dir_name); | 573 path_to_create = path_to_create.Append(new_dir_name); |
621 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { | 574 if (::CreateDirectory(path_to_create.value().c_str(), NULL)) { |
622 *new_dir = path_to_create; | 575 *new_dir = path_to_create; |
623 return true; | 576 return true; |
624 } | 577 } |
625 } | 578 } |
626 | 579 |
627 return false; | 580 return false; |
628 } | 581 } |
629 | 582 |
630 bool CreateNewTempDirectory(const FilePath::StringType& prefix, | 583 bool CreateNewTempDirectory(const FilePath::StringType& prefix, |
631 FilePath* new_temp_path) { | 584 FilePath* new_temp_path) { |
632 base::ThreadRestrictions::AssertIOAllowed(); | |
633 | |
634 FilePath system_temp_dir; | 585 FilePath system_temp_dir; |
635 if (!GetTempDir(&system_temp_dir)) | 586 if (!GetTempDir(&system_temp_dir)) |
636 return false; | 587 return false; |
637 | 588 |
638 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); | 589 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); |
639 } | 590 } |
640 | 591 |
641 bool CreateDirectory(const FilePath& full_path) { | 592 bool CreateDirectory(const FilePath& full_path) { |
642 base::ThreadRestrictions::AssertIOAllowed(); | |
643 | |
644 // If the path exists, we've succeeded if it's a directory, failed otherwise. | 593 // If the path exists, we've succeeded if it's a directory, failed otherwise. |
645 const wchar_t* full_path_str = full_path.value().c_str(); | 594 const wchar_t* full_path_str = full_path.value().c_str(); |
646 DWORD fileattr = ::GetFileAttributes(full_path_str); | 595 DWORD fileattr = ::GetFileAttributes(full_path_str); |
647 if (fileattr != INVALID_FILE_ATTRIBUTES) { | 596 if (fileattr != INVALID_FILE_ATTRIBUTES) { |
648 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { | 597 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { |
649 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " | 598 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " |
650 << "directory already exists."; | 599 << "directory already exists."; |
651 return true; | 600 return true; |
652 } | 601 } |
653 LOG(WARNING) << "CreateDirectory(" << full_path_str << "), " | 602 LOG(WARNING) << "CreateDirectory(" << full_path_str << "), " |
(...skipping 26 matching lines...) Expand all Loading... |
680 LOG(WARNING) << "Failed to create directory " << full_path_str | 629 LOG(WARNING) << "Failed to create directory " << full_path_str |
681 << ", last error is " << error_code << "."; | 630 << ", last error is " << error_code << "."; |
682 return false; | 631 return false; |
683 } | 632 } |
684 } else { | 633 } else { |
685 return true; | 634 return true; |
686 } | 635 } |
687 } | 636 } |
688 | 637 |
689 bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) { | 638 bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) { |
690 base::ThreadRestrictions::AssertIOAllowed(); | |
691 | |
692 WIN32_FILE_ATTRIBUTE_DATA attr; | 639 WIN32_FILE_ATTRIBUTE_DATA attr; |
693 if (!GetFileAttributesEx(file_path.value().c_str(), | 640 if (!GetFileAttributesEx(file_path.value().c_str(), |
694 GetFileExInfoStandard, &attr)) { | 641 GetFileExInfoStandard, &attr)) { |
695 return false; | 642 return false; |
696 } | 643 } |
697 | 644 |
698 ULARGE_INTEGER size; | 645 ULARGE_INTEGER size; |
699 size.HighPart = attr.nFileSizeHigh; | 646 size.HighPart = attr.nFileSizeHigh; |
700 size.LowPart = attr.nFileSizeLow; | 647 size.LowPart = attr.nFileSizeLow; |
701 results->size = size.QuadPart; | 648 results->size = size.QuadPart; |
702 | 649 |
703 results->is_directory = | 650 results->is_directory = |
704 (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 651 (attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
705 results->last_modified = base::Time::FromFileTime(attr.ftLastWriteTime); | 652 results->last_modified = base::Time::FromFileTime(attr.ftLastWriteTime); |
706 results->last_accessed = base::Time::FromFileTime(attr.ftLastAccessTime); | 653 results->last_accessed = base::Time::FromFileTime(attr.ftLastAccessTime); |
707 results->creation_time = base::Time::FromFileTime(attr.ftCreationTime); | 654 results->creation_time = base::Time::FromFileTime(attr.ftCreationTime); |
708 | 655 |
709 return true; | 656 return true; |
710 } | 657 } |
711 | 658 |
712 FILE* OpenFile(const FilePath& filename, const char* mode) { | 659 FILE* OpenFile(const FilePath& filename, const char* mode) { |
713 base::ThreadRestrictions::AssertIOAllowed(); | |
714 std::wstring w_mode = ASCIIToWide(std::string(mode)); | 660 std::wstring w_mode = ASCIIToWide(std::string(mode)); |
715 return _wfsopen(filename.value().c_str(), w_mode.c_str(), _SH_DENYNO); | 661 return _wfsopen(filename.value().c_str(), w_mode.c_str(), _SH_DENYNO); |
716 } | 662 } |
717 | 663 |
718 FILE* OpenFile(const std::string& filename, const char* mode) { | 664 FILE* OpenFile(const std::string& filename, const char* mode) { |
719 base::ThreadRestrictions::AssertIOAllowed(); | |
720 return _fsopen(filename.c_str(), mode, _SH_DENYNO); | 665 return _fsopen(filename.c_str(), mode, _SH_DENYNO); |
721 } | 666 } |
722 | 667 |
723 int ReadFile(const FilePath& filename, char* data, int size) { | 668 int ReadFile(const FilePath& filename, char* data, int size) { |
724 base::ThreadRestrictions::AssertIOAllowed(); | |
725 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 669 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
726 GENERIC_READ, | 670 GENERIC_READ, |
727 FILE_SHARE_READ | FILE_SHARE_WRITE, | 671 FILE_SHARE_READ | FILE_SHARE_WRITE, |
728 NULL, | 672 NULL, |
729 OPEN_EXISTING, | 673 OPEN_EXISTING, |
730 FILE_FLAG_SEQUENTIAL_SCAN, | 674 FILE_FLAG_SEQUENTIAL_SCAN, |
731 NULL)); | 675 NULL)); |
732 if (!file) | 676 if (!file) |
733 return -1; | 677 return -1; |
734 | 678 |
735 DWORD read; | 679 DWORD read; |
736 if (::ReadFile(file, data, size, &read, NULL) && | 680 if (::ReadFile(file, data, size, &read, NULL) && |
737 static_cast<int>(read) == size) | 681 static_cast<int>(read) == size) |
738 return read; | 682 return read; |
739 return -1; | 683 return -1; |
740 } | 684 } |
741 | 685 |
742 int WriteFile(const FilePath& filename, const char* data, int size) { | 686 int WriteFile(const FilePath& filename, const char* data, int size) { |
743 base::ThreadRestrictions::AssertIOAllowed(); | |
744 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), | 687 base::win::ScopedHandle file(CreateFile(filename.value().c_str(), |
745 GENERIC_WRITE, | 688 GENERIC_WRITE, |
746 0, | 689 0, |
747 NULL, | 690 NULL, |
748 CREATE_ALWAYS, | 691 CREATE_ALWAYS, |
749 0, | 692 0, |
750 NULL)); | 693 NULL)); |
751 if (!file) { | 694 if (!file) { |
752 LOG(WARNING) << "CreateFile failed for path " << filename.value() << | 695 LOG(WARNING) << "CreateFile failed for path " << filename.value() << |
753 " error code=" << GetLastError() << | 696 " error code=" << GetLastError() << |
(...skipping 14 matching lines...) Expand all Loading... |
768 } else { | 711 } else { |
769 // Didn't write all the bytes. | 712 // Didn't write all the bytes. |
770 LOG(WARNING) << "wrote" << written << " bytes to " << | 713 LOG(WARNING) << "wrote" << written << " bytes to " << |
771 filename.value() << " expected " << size; | 714 filename.value() << " expected " << size; |
772 } | 715 } |
773 return -1; | 716 return -1; |
774 } | 717 } |
775 | 718 |
776 bool RenameFileAndResetSecurityDescriptor(const FilePath& source_file_path, | 719 bool RenameFileAndResetSecurityDescriptor(const FilePath& source_file_path, |
777 const FilePath& target_file_path) { | 720 const FilePath& target_file_path) { |
778 base::ThreadRestrictions::AssertIOAllowed(); | |
779 | |
780 // The parameters to SHFileOperation must be terminated with 2 NULL chars. | 721 // The parameters to SHFileOperation must be terminated with 2 NULL chars. |
781 std::wstring source = source_file_path.value(); | 722 std::wstring source = source_file_path.value(); |
782 std::wstring target = target_file_path.value(); | 723 std::wstring target = target_file_path.value(); |
783 | 724 |
784 source.append(1, L'\0'); | 725 source.append(1, L'\0'); |
785 target.append(1, L'\0'); | 726 target.append(1, L'\0'); |
786 | 727 |
787 SHFILEOPSTRUCT move_info = {0}; | 728 SHFILEOPSTRUCT move_info = {0}; |
788 move_info.wFunc = FO_MOVE; | 729 move_info.wFunc = FO_MOVE; |
789 move_info.pFrom = source.c_str(); | 730 move_info.pFrom = source.c_str(); |
790 move_info.pTo = target.c_str(); | 731 move_info.pTo = target.c_str(); |
791 move_info.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | | 732 move_info.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | |
792 FOF_NOCONFIRMMKDIR | FOF_NOCOPYSECURITYATTRIBS; | 733 FOF_NOCONFIRMMKDIR | FOF_NOCOPYSECURITYATTRIBS; |
793 | 734 |
794 if (0 != SHFileOperation(&move_info)) | 735 if (0 != SHFileOperation(&move_info)) |
795 return false; | 736 return false; |
796 | 737 |
797 return true; | 738 return true; |
798 } | 739 } |
799 | 740 |
800 // Gets the current working directory for the process. | 741 // Gets the current working directory for the process. |
801 bool GetCurrentDirectory(FilePath* dir) { | 742 bool GetCurrentDirectory(FilePath* dir) { |
802 base::ThreadRestrictions::AssertIOAllowed(); | |
803 | |
804 wchar_t system_buffer[MAX_PATH]; | 743 wchar_t system_buffer[MAX_PATH]; |
805 system_buffer[0] = 0; | 744 system_buffer[0] = 0; |
806 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); | 745 DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer); |
807 if (len == 0 || len > MAX_PATH) | 746 if (len == 0 || len > MAX_PATH) |
808 return false; | 747 return false; |
809 // TODO(evanm): the old behavior of this function was to always strip the | 748 // TODO(evanm): the old behavior of this function was to always strip the |
810 // trailing slash. We duplicate this here, but it shouldn't be necessary | 749 // trailing slash. We duplicate this here, but it shouldn't be necessary |
811 // when everyone is using the appropriate FilePath APIs. | 750 // when everyone is using the appropriate FilePath APIs. |
812 std::wstring dir_str(system_buffer); | 751 std::wstring dir_str(system_buffer); |
813 *dir = FilePath(dir_str).StripTrailingSeparators(); | 752 *dir = FilePath(dir_str).StripTrailingSeparators(); |
814 return true; | 753 return true; |
815 } | 754 } |
816 | 755 |
817 // Sets the current working directory for the process. | 756 // Sets the current working directory for the process. |
818 bool SetCurrentDirectory(const FilePath& directory) { | 757 bool SetCurrentDirectory(const FilePath& directory) { |
819 base::ThreadRestrictions::AssertIOAllowed(); | |
820 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); | 758 BOOL ret = ::SetCurrentDirectory(directory.value().c_str()); |
821 return ret != 0; | 759 return ret != 0; |
822 } | 760 } |
823 | 761 |
824 /////////////////////////////////////////////// | 762 /////////////////////////////////////////////// |
825 // FileEnumerator | 763 // FileEnumerator |
826 | 764 |
827 FileEnumerator::FileEnumerator(const FilePath& root_path, | 765 FileEnumerator::FileEnumerator(const FilePath& root_path, |
828 bool recursive, | 766 bool recursive, |
829 FileEnumerator::FILE_TYPE file_type) | 767 FileEnumerator::FILE_TYPE file_type) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
867 bool FileEnumerator::IsDirectory(const FindInfo& info) { | 805 bool FileEnumerator::IsDirectory(const FindInfo& info) { |
868 return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; | 806 return (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; |
869 } | 807 } |
870 | 808 |
871 // static | 809 // static |
872 FilePath FileEnumerator::GetFilename(const FindInfo& find_info) { | 810 FilePath FileEnumerator::GetFilename(const FindInfo& find_info) { |
873 return FilePath(find_info.cFileName); | 811 return FilePath(find_info.cFileName); |
874 } | 812 } |
875 | 813 |
876 FilePath FileEnumerator::Next() { | 814 FilePath FileEnumerator::Next() { |
877 base::ThreadRestrictions::AssertIOAllowed(); | |
878 | |
879 while (has_find_data_ || !pending_paths_.empty()) { | 815 while (has_find_data_ || !pending_paths_.empty()) { |
880 if (!has_find_data_) { | 816 if (!has_find_data_) { |
881 // The last find FindFirstFile operation is done, prepare a new one. | 817 // The last find FindFirstFile operation is done, prepare a new one. |
882 root_path_ = pending_paths_.top(); | 818 root_path_ = pending_paths_.top(); |
883 pending_paths_.pop(); | 819 pending_paths_.pop(); |
884 | 820 |
885 // Start a new find operation. | 821 // Start a new find operation. |
886 FilePath src = root_path_; | 822 FilePath src = root_path_; |
887 | 823 |
888 if (pattern_.empty()) | 824 if (pattern_.empty()) |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
940 // MemoryMappedFile | 876 // MemoryMappedFile |
941 | 877 |
942 MemoryMappedFile::MemoryMappedFile() | 878 MemoryMappedFile::MemoryMappedFile() |
943 : file_(INVALID_HANDLE_VALUE), | 879 : file_(INVALID_HANDLE_VALUE), |
944 file_mapping_(INVALID_HANDLE_VALUE), | 880 file_mapping_(INVALID_HANDLE_VALUE), |
945 data_(NULL), | 881 data_(NULL), |
946 length_(INVALID_FILE_SIZE) { | 882 length_(INVALID_FILE_SIZE) { |
947 } | 883 } |
948 | 884 |
949 bool MemoryMappedFile::MapFileToMemoryInternal() { | 885 bool MemoryMappedFile::MapFileToMemoryInternal() { |
950 base::ThreadRestrictions::AssertIOAllowed(); | |
951 | |
952 if (file_ == INVALID_HANDLE_VALUE) | 886 if (file_ == INVALID_HANDLE_VALUE) |
953 return false; | 887 return false; |
954 | 888 |
955 length_ = ::GetFileSize(file_, NULL); | 889 length_ = ::GetFileSize(file_, NULL); |
956 if (length_ == INVALID_FILE_SIZE) | 890 if (length_ == INVALID_FILE_SIZE) |
957 return false; | 891 return false; |
958 | 892 |
959 // length_ value comes from GetFileSize() above. GetFileSize() returns DWORD, | 893 // length_ value comes from GetFileSize() above. GetFileSize() returns DWORD, |
960 // therefore the cast here is safe. | 894 // therefore the cast here is safe. |
961 file_mapping_ = ::CreateFileMapping(file_, NULL, PAGE_READONLY, | 895 file_mapping_ = ::CreateFileMapping(file_, NULL, PAGE_READONLY, |
(...skipping 23 matching lines...) Expand all Loading... |
985 if (file_ != INVALID_HANDLE_VALUE) | 919 if (file_ != INVALID_HANDLE_VALUE) |
986 ::CloseHandle(file_); | 920 ::CloseHandle(file_); |
987 | 921 |
988 data_ = NULL; | 922 data_ = NULL; |
989 file_mapping_ = file_ = INVALID_HANDLE_VALUE; | 923 file_mapping_ = file_ = INVALID_HANDLE_VALUE; |
990 length_ = INVALID_FILE_SIZE; | 924 length_ = INVALID_FILE_SIZE; |
991 } | 925 } |
992 | 926 |
993 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, | 927 bool HasFileBeenModifiedSince(const FileEnumerator::FindInfo& find_info, |
994 const base::Time& cutoff_time) { | 928 const base::Time& cutoff_time) { |
995 base::ThreadRestrictions::AssertIOAllowed(); | |
996 long result = CompareFileTime(&find_info.ftLastWriteTime, | 929 long result = CompareFileTime(&find_info.ftLastWriteTime, |
997 &cutoff_time.ToFileTime()); | 930 &cutoff_time.ToFileTime()); |
998 return result == 1 || result == 0; | 931 return result == 1 || result == 0; |
999 } | 932 } |
1000 | 933 |
1001 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { | 934 bool NormalizeFilePath(const FilePath& path, FilePath* real_path) { |
1002 base::ThreadRestrictions::AssertIOAllowed(); | |
1003 FilePath mapped_file; | 935 FilePath mapped_file; |
1004 if (!NormalizeToNativeFilePath(path, &mapped_file)) | 936 if (!NormalizeToNativeFilePath(path, &mapped_file)) |
1005 return false; | 937 return false; |
1006 // NormalizeToNativeFilePath() will return a path that starts with | 938 // NormalizeToNativeFilePath() will return a path that starts with |
1007 // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath() | 939 // "\Device\Harddisk...". Helper DevicePathToDriveLetterPath() |
1008 // will find a drive letter which maps to the path's device, so | 940 // will find a drive letter which maps to the path's device, so |
1009 // that we return a path starting with a drive letter. | 941 // that we return a path starting with a drive letter. |
1010 return DevicePathToDriveLetterPath(mapped_file, real_path); | 942 return DevicePathToDriveLetterPath(mapped_file, real_path); |
1011 } | 943 } |
1012 | 944 |
1013 bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) { | 945 bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path) { |
1014 base::ThreadRestrictions::AssertIOAllowed(); | |
1015 // In Vista, GetFinalPathNameByHandle() would give us the real path | 946 // In Vista, GetFinalPathNameByHandle() would give us the real path |
1016 // from a file handle. If we ever deprecate XP, consider changing the | 947 // from a file handle. If we ever deprecate XP, consider changing the |
1017 // code below to a call to GetFinalPathNameByHandle(). The method this | 948 // code below to a call to GetFinalPathNameByHandle(). The method this |
1018 // function uses is explained in the following msdn article: | 949 // function uses is explained in the following msdn article: |
1019 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx | 950 // http://msdn.microsoft.com/en-us/library/aa366789(VS.85).aspx |
1020 base::win::ScopedHandle file_handle( | 951 base::win::ScopedHandle file_handle( |
1021 ::CreateFile(path.value().c_str(), | 952 ::CreateFile(path.value().c_str(), |
1022 GENERIC_READ, | 953 GENERIC_READ, |
1023 kFileShareAll, | 954 kFileShareAll, |
1024 NULL, | 955 NULL, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { | 991 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { |
1061 *nt_path = FilePath(mapped_file_path); | 992 *nt_path = FilePath(mapped_file_path); |
1062 success = true; | 993 success = true; |
1063 } | 994 } |
1064 ::UnmapViewOfFile(file_view); | 995 ::UnmapViewOfFile(file_view); |
1065 return success; | 996 return success; |
1066 } | 997 } |
1067 | 998 |
1068 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, | 999 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, |
1069 size_t step_size) { | 1000 size_t step_size) { |
1070 base::ThreadRestrictions::AssertIOAllowed(); | |
1071 if (base::win::GetVersion() > base::win::VERSION_XP) { | 1001 if (base::win::GetVersion() > base::win::VERSION_XP) { |
1072 // Vista+ branch. On these OSes, the forced reads through the DLL actually | 1002 // Vista+ branch. On these OSes, the forced reads through the DLL actually |
1073 // slows warm starts. The solution is to sequentially read file contents | 1003 // slows warm starts. The solution is to sequentially read file contents |
1074 // with an optional cap on total amount to read. | 1004 // with an optional cap on total amount to read. |
1075 base::win::ScopedHandle file( | 1005 base::win::ScopedHandle file( |
1076 CreateFile(file_path, | 1006 CreateFile(file_path, |
1077 GENERIC_READ, | 1007 GENERIC_READ, |
1078 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, | 1008 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, |
1079 NULL, | 1009 NULL, |
1080 OPEN_EXISTING, | 1010 OPEN_EXISTING, |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1125 uint8 unused = *(touch + offset); | 1055 uint8 unused = *(touch + offset); |
1126 offset += step_size; | 1056 offset += step_size; |
1127 } | 1057 } |
1128 FreeLibrary(dll_module); | 1058 FreeLibrary(dll_module); |
1129 } | 1059 } |
1130 | 1060 |
1131 return true; | 1061 return true; |
1132 } | 1062 } |
1133 | 1063 |
1134 } // namespace file_util | 1064 } // namespace file_util |
OLD | NEW |