Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(281)

Side by Side Diff: base/file_util_win.cc

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

Powered by Google App Engine
This is Rietveld 408576698