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

Side by Side Diff: base/file_util_win.cc

Issue 437090: Fix race in directory creation on Windows. The... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 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') | no next file » | 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 <shellapi.h> 9 #include <shellapi.h>
10 #include <shlobj.h> 10 #include <shlobj.h>
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
502 502
503 if (count == 50) { 503 if (count == 50) {
504 return false; 504 return false;
505 } 505 }
506 506
507 *new_temp_path = path_to_create; 507 *new_temp_path = path_to_create;
508 return true; 508 return true;
509 } 509 }
510 510
511 bool CreateDirectory(const FilePath& full_path) { 511 bool CreateDirectory(const FilePath& full_path) {
512 if (DirectoryExists(full_path)) 512 // If the path exists, we've succeeded if it's a directory, failed otherwise.
513 const wchar_t* full_path_str = full_path.value().c_str();
514 DWORD fileattr = ::GetFileAttributes(full_path_str);
515 if (fileattr != INVALID_FILE_ATTRIBUTES) {
516 if ((fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0) {
517 DLOG(INFO) << "CreateDirectory(" << full_path_str << "), " <<
518 "directory already exists.";
519 return true;
520 } else {
521 LOG(WARNING) << "CreateDirectory(" << full_path_str << "), " <<
522 "conflicts with existing file.";
523 }
524 }
525
526 // Invariant: Path does not exist as file or directory.
527
528 // Attempt to create the parent recursively. This will immediately return
529 // true if it already exists, otherwise will create all required parent
530 // directories starting with the highest-level missing parent.
531 if (!CreateDirectory(full_path.DirName())) {
532 DLOG(WARNING) << "Failed to create one of the parent directories.";
533 return false;
534 }
535
536 if (!::CreateDirectory(full_path_str, NULL)) {
537 DWORD error_code = ::GetLastError();
538 if (error_code == ERROR_ALREADY_EXISTS && DirectoryExists(full_path)) {
539 // This error code doesn't indicate whether we were racing with someone
540 // creating the same directory, or a file with the same path, therefore
541 // we check.
542 return true;
543 } else {
544 LOG(WARNING) << "Failed to create directory " << full_path_str <<
545 ", le=" << error_code;
546 return false;
547 }
548 } else {
513 return true; 549 return true;
514 int err = SHCreateDirectoryEx(NULL, full_path.value().c_str(), NULL); 550 }
515 return err == ERROR_SUCCESS;
516 } 551 }
517 552
518 bool GetFileInfo(const FilePath& file_path, FileInfo* results) { 553 bool GetFileInfo(const FilePath& file_path, FileInfo* results) {
519 WIN32_FILE_ATTRIBUTE_DATA attr; 554 WIN32_FILE_ATTRIBUTE_DATA attr;
520 if (!GetFileAttributesEx(file_path.ToWStringHack().c_str(), 555 if (!GetFileAttributesEx(file_path.ToWStringHack().c_str(),
521 GetFileExInfoStandard, &attr)) { 556 GetFileExInfoStandard, &attr)) {
522 return false; 557 return false;
523 } 558 }
524 559
525 ULARGE_INTEGER size; 560 ULARGE_INTEGER size;
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
787 ::CloseHandle(file_mapping_); 822 ::CloseHandle(file_mapping_);
788 if (file_ != INVALID_HANDLE_VALUE) 823 if (file_ != INVALID_HANDLE_VALUE)
789 ::CloseHandle(file_); 824 ::CloseHandle(file_);
790 825
791 data_ = NULL; 826 data_ = NULL;
792 file_mapping_ = file_ = INVALID_HANDLE_VALUE; 827 file_mapping_ = file_ = INVALID_HANDLE_VALUE;
793 length_ = INVALID_FILE_SIZE; 828 length_ = INVALID_FILE_SIZE;
794 } 829 }
795 830
796 } // namespace file_util 831 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698