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

Side by Side Diff: base/file_util_posix.cc

Issue 15812007: Make file_util::CreateDirectory return an error, not just a bool (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: xml file; use the new errors Created 7 years, 6 months 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
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 <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <fnmatch.h> 10 #include <fnmatch.h>
(...skipping 501 matching lines...) Expand 10 before | Expand all | Expand 10 after
512 512
513 bool CreateNewTempDirectory(const FilePath::StringType& prefix, 513 bool CreateNewTempDirectory(const FilePath::StringType& prefix,
514 FilePath* new_temp_path) { 514 FilePath* new_temp_path) {
515 FilePath tmpdir; 515 FilePath tmpdir;
516 if (!GetTempDir(&tmpdir)) 516 if (!GetTempDir(&tmpdir))
517 return false; 517 return false;
518 518
519 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); 519 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
520 } 520 }
521 521
522 bool CreateDirectory(const FilePath& full_path) { 522 bool CreateDirectoryAndGetError(const FilePath& full_path,
523 base::PlatformFileError* error) {
523 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). 524 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
524 std::vector<FilePath> subpaths; 525 std::vector<FilePath> subpaths;
525 526
526 // Collect a list of all parent directories. 527 // Collect a list of all parent directories.
527 FilePath last_path = full_path; 528 FilePath last_path = full_path;
528 subpaths.push_back(full_path); 529 subpaths.push_back(full_path);
529 for (FilePath path = full_path.DirName(); 530 for (FilePath path = full_path.DirName();
530 path.value() != last_path.value(); path = path.DirName()) { 531 path.value() != last_path.value(); path = path.DirName()) {
531 subpaths.push_back(path); 532 subpaths.push_back(path);
532 last_path = path; 533 last_path = path;
533 } 534 }
534 535
535 // Iterate through the parents and create the missing ones. 536 // Iterate through the parents and create the missing ones.
536 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); 537 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
537 i != subpaths.rend(); ++i) { 538 i != subpaths.rend(); ++i) {
538 if (DirectoryExists(*i)) 539 if (DirectoryExists(*i))
539 continue; 540 continue;
540 if (mkdir(i->value().c_str(), 0700) == 0) 541 if (mkdir(i->value().c_str(), 0700) == 0)
541 continue; 542 continue;
542 // Mkdir failed, but it might have failed with EEXIST, or some other error 543 // Mkdir failed, but it might have failed with EEXIST, or some other error
543 // due to the the directory appearing out of thin air. This can occur if 544 // due to the the directory appearing out of thin air. This can occur if
544 // two processes are trying to create the same file system tree at the same 545 // two processes are trying to create the same file system tree at the same
545 // time. Check to see if it exists and make sure it is a directory. 546 // time. Check to see if it exists and make sure it is a directory.
546 if (!DirectoryExists(*i)) 547 int saved_errno = errno;
548 if (!DirectoryExists(*i)) {
549 if (error)
550 *error = base::ErrnoToPlatformFileError(saved_errno);
547 return false; 551 return false;
552 }
548 } 553 }
549 return true; 554 return true;
550 } 555 }
551 556
552 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { 557 base::FilePath MakeUniqueDirectory(const base::FilePath& path) {
553 const int kMaxAttempts = 20; 558 const int kMaxAttempts = 20;
554 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { 559 for (int attempts = 0; attempts < kMaxAttempts; attempts++) {
555 int uniquifier = 560 int uniquifier =
556 GetUniquePathNumber(path, base::FilePath::StringType()); 561 GetUniquePathNumber(path, base::FilePath::StringType());
557 if (uniquifier < 0) 562 if (uniquifier < 0)
(...skipping 509 matching lines...) Expand 10 before | Expand all | Expand 10 after
1067 kFileSystemRoot, path, kRootUid, allowed_group_ids); 1072 kFileSystemRoot, path, kRootUid, allowed_group_ids);
1068 } 1073 }
1069 #endif // defined(OS_MACOSX) && !defined(OS_IOS) 1074 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
1070 1075
1071 int GetMaximumPathComponentLength(const FilePath& path) { 1076 int GetMaximumPathComponentLength(const FilePath& path) {
1072 base::ThreadRestrictions::AssertIOAllowed(); 1077 base::ThreadRestrictions::AssertIOAllowed();
1073 return pathconf(path.value().c_str(), _PC_NAME_MAX); 1078 return pathconf(path.value().c_str(), _PC_NAME_MAX);
1074 } 1079 }
1075 1080
1076 } // namespace file_util 1081 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util.cc ('k') | base/file_util_win.cc » ('j') | base/file_util_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698