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

Side by Side Diff: base/file_util_win.cc

Issue 100573002: Move directory creation functions to base namespace. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years 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/file_util_unittest.cc ('k') | base/files/file_path_watcher_browsertest.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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <psapi.h> 8 #include <psapi.h>
9 #include <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 171
172 // The following code assumes that from path is a directory. 172 // The following code assumes that from path is a directory.
173 DCHECK(DirectoryExists(from_path)); 173 DCHECK(DirectoryExists(from_path));
174 174
175 // Instead of creating a new directory, we copy the old one to include the 175 // Instead of creating a new directory, we copy the old one to include the
176 // security information of the folder as part of the copy. 176 // security information of the folder as part of the copy.
177 if (!PathExists(to_path)) { 177 if (!PathExists(to_path)) {
178 // Except that Vista fails to do that, and instead do a recursive copy if 178 // Except that Vista fails to do that, and instead do a recursive copy if
179 // the target directory doesn't exist. 179 // the target directory doesn't exist.
180 if (base::win::GetVersion() >= base::win::VERSION_VISTA) 180 if (base::win::GetVersion() >= base::win::VERSION_VISTA)
181 file_util::CreateDirectory(to_path); 181 CreateDirectory(to_path);
182 else 182 else
183 ShellCopy(from_path, to_path, false); 183 ShellCopy(from_path, to_path, false);
184 } 184 }
185 185
186 FilePath directory = from_path.Append(L"*.*"); 186 FilePath directory = from_path.Append(L"*.*");
187 return ShellCopy(directory, to_path, false); 187 return ShellCopy(directory, to_path, false);
188 } 188 }
189 189
190 bool PathExists(const FilePath& path) { 190 bool PathExists(const FilePath& path) {
191 ThreadRestrictions::AssertIOAllowed(); 191 ThreadRestrictions::AssertIOAllowed();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 FilePath* new_temp_path) { 322 FilePath* new_temp_path) {
323 ThreadRestrictions::AssertIOAllowed(); 323 ThreadRestrictions::AssertIOAllowed();
324 324
325 FilePath system_temp_dir; 325 FilePath system_temp_dir;
326 if (!GetTempDir(&system_temp_dir)) 326 if (!GetTempDir(&system_temp_dir))
327 return false; 327 return false;
328 328
329 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path); 329 return CreateTemporaryDirInDir(system_temp_dir, prefix, new_temp_path);
330 } 330 }
331 331
332 } // namespace base
333
334 // -----------------------------------------------------------------------------
335
336 namespace file_util {
337
338 using base::DirectoryExists;
339 using base::FilePath;
340 using base::kFileShareAll;
341
342 bool CreateDirectoryAndGetError(const FilePath& full_path, 332 bool CreateDirectoryAndGetError(const FilePath& full_path,
343 base::PlatformFileError* error) { 333 PlatformFileError* error) {
344 base::ThreadRestrictions::AssertIOAllowed(); 334 ThreadRestrictions::AssertIOAllowed();
345 335
346 // If the path exists, we've succeeded if it's a directory, failed otherwise. 336 // If the path exists, we've succeeded if it's a directory, failed otherwise.
347 const wchar_t* full_path_str = full_path.value().c_str(); 337 const wchar_t* full_path_str = full_path.value().c_str();
348 DWORD fileattr = ::GetFileAttributes(full_path_str); 338 DWORD fileattr = ::GetFileAttributes(full_path_str);
349 if (fileattr != INVALID_FILE_ATTRIBUTES) { 339 if (fileattr != INVALID_FILE_ATTRIBUTES) {
350 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) { 340 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
351 DVLOG(1) << "CreateDirectory(" << full_path_str << "), " 341 DVLOG(1) << "CreateDirectory(" << full_path_str << "), "
352 << "directory already exists."; 342 << "directory already exists.";
353 return true; 343 return true;
354 } 344 }
355 DLOG(WARNING) << "CreateDirectory(" << full_path_str << "), " 345 DLOG(WARNING) << "CreateDirectory(" << full_path_str << "), "
356 << "conflicts with existing file."; 346 << "conflicts with existing file.";
357 if (error) { 347 if (error) {
358 *error = base::PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; 348 *error = PLATFORM_FILE_ERROR_NOT_A_DIRECTORY;
359 } 349 }
360 return false; 350 return false;
361 } 351 }
362 352
363 // Invariant: Path does not exist as file or directory. 353 // Invariant: Path does not exist as file or directory.
364 354
365 // Attempt to create the parent recursively. This will immediately return 355 // Attempt to create the parent recursively. This will immediately return
366 // true if it already exists, otherwise will create all required parent 356 // true if it already exists, otherwise will create all required parent
367 // directories starting with the highest-level missing parent. 357 // directories starting with the highest-level missing parent.
368 FilePath parent_path(full_path.DirName()); 358 FilePath parent_path(full_path.DirName());
369 if (parent_path.value() == full_path.value()) { 359 if (parent_path.value() == full_path.value()) {
370 if (error) { 360 if (error) {
371 *error = base::PLATFORM_FILE_ERROR_NOT_FOUND; 361 *error = PLATFORM_FILE_ERROR_NOT_FOUND;
372 } 362 }
373 return false; 363 return false;
374 } 364 }
375 if (!CreateDirectoryAndGetError(parent_path, error)) { 365 if (!CreateDirectoryAndGetError(parent_path, error)) {
376 DLOG(WARNING) << "Failed to create one of the parent directories."; 366 DLOG(WARNING) << "Failed to create one of the parent directories.";
377 if (error) { 367 if (error) {
378 DCHECK(*error != base::PLATFORM_FILE_OK); 368 DCHECK(*error != PLATFORM_FILE_OK);
379 } 369 }
380 return false; 370 return false;
381 } 371 }
382 372
383 if (!::CreateDirectory(full_path_str, NULL)) { 373 if (!::CreateDirectory(full_path_str, NULL)) {
384 DWORD error_code = ::GetLastError(); 374 DWORD error_code = ::GetLastError();
385 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) { 375 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) {
386 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we 376 // This error code ERROR_ALREADY_EXISTS doesn't indicate whether we
387 // were racing with someone creating the same directory, or a file 377 // were racing with someone creating the same directory, or a file
388 // with the same path. If DirectoryExists() returns true, we lost the 378 // with the same path. If DirectoryExists() returns true, we lost the
389 // race to create the same directory. 379 // race to create the same directory.
390 return true; 380 return true;
391 } else { 381 } else {
392 if (error) 382 if (error)
393 *error = base::LastErrorToPlatformFileError(error_code); 383 *error = LastErrorToPlatformFileError(error_code);
394 DLOG(WARNING) << "Failed to create directory " << full_path_str 384 DLOG(WARNING) << "Failed to create directory " << full_path_str
395 << ", last error is " << error_code << "."; 385 << ", last error is " << error_code << ".";
396 return false; 386 return false;
397 } 387 }
398 } else { 388 } else {
399 return true; 389 return true;
400 } 390 }
401 } 391 }
402 392
393 } // namespace base
394
395 // -----------------------------------------------------------------------------
396
397 namespace file_util {
398
399 using base::DirectoryExists;
400 using base::FilePath;
401 using base::kFileShareAll;
402
403 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle 403 // TODO(rkc): Work out if we want to handle NTFS junctions here or not, handle
404 // them if we do decide to. 404 // them if we do decide to.
405 bool IsLink(const FilePath& file_path) { 405 bool IsLink(const FilePath& file_path) {
406 return false; 406 return false;
407 } 407 }
408 408
409 bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) { 409 bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) {
410 base::ThreadRestrictions::AssertIOAllowed(); 410 base::ThreadRestrictions::AssertIOAllowed();
411 411
412 WIN32_FILE_ATTRIBUTE_DATA attr; 412 WIN32_FILE_ATTRIBUTE_DATA attr;
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 // Like Move, this function is not transactional, so we just 744 // Like Move, this function is not transactional, so we just
745 // leave the copied bits behind if deleting from_path fails. 745 // leave the copied bits behind if deleting from_path fails.
746 // If to_path exists previously then we have already overwritten 746 // If to_path exists previously then we have already overwritten
747 // it by now, we don't get better off by deleting the new bits. 747 // it by now, we don't get better off by deleting the new bits.
748 } 748 }
749 return false; 749 return false;
750 } 750 }
751 751
752 } // namespace internal 752 } // namespace internal
753 } // namespace base 753 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util_unittest.cc ('k') | base/files/file_path_watcher_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698