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

Side by Side Diff: base/file_util_posix.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.cc ('k') | base/file_util_unittest.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 <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <libgen.h> 10 #include <libgen.h>
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 581
582 bool CreateNewTempDirectory(const FilePath::StringType& prefix, 582 bool CreateNewTempDirectory(const FilePath::StringType& prefix,
583 FilePath* new_temp_path) { 583 FilePath* new_temp_path) {
584 FilePath tmpdir; 584 FilePath tmpdir;
585 if (!GetTempDir(&tmpdir)) 585 if (!GetTempDir(&tmpdir))
586 return false; 586 return false;
587 587
588 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path); 588 return CreateTemporaryDirInDirImpl(tmpdir, TempFileName(), new_temp_path);
589 } 589 }
590 590
591
592 } // namespace base
593
594 // -----------------------------------------------------------------------------
595
596 namespace file_util {
597
598 using base::stat_wrapper_t;
599 using base::CallStat;
600 using base::CallLstat;
601 using base::CreateAndOpenFdForTemporaryFile;
602 using base::DirectoryExists;
603 using base::FileEnumerator;
604 using base::FilePath;
605 using base::MakeAbsoluteFilePath;
606 using base::RealPath;
607 using base::VerifySpecificPathControlledByUser;
608
609 bool CreateDirectoryAndGetError(const FilePath& full_path, 591 bool CreateDirectoryAndGetError(const FilePath& full_path,
610 base::PlatformFileError* error) { 592 PlatformFileError* error) {
611 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkdir(). 593 ThreadRestrictions::AssertIOAllowed(); // For call to mkdir().
612 std::vector<FilePath> subpaths; 594 std::vector<FilePath> subpaths;
613 595
614 // Collect a list of all parent directories. 596 // Collect a list of all parent directories.
615 FilePath last_path = full_path; 597 FilePath last_path = full_path;
616 subpaths.push_back(full_path); 598 subpaths.push_back(full_path);
617 for (FilePath path = full_path.DirName(); 599 for (FilePath path = full_path.DirName();
618 path.value() != last_path.value(); path = path.DirName()) { 600 path.value() != last_path.value(); path = path.DirName()) {
619 subpaths.push_back(path); 601 subpaths.push_back(path);
620 last_path = path; 602 last_path = path;
621 } 603 }
622 604
623 // Iterate through the parents and create the missing ones. 605 // Iterate through the parents and create the missing ones.
624 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin(); 606 for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
625 i != subpaths.rend(); ++i) { 607 i != subpaths.rend(); ++i) {
626 if (DirectoryExists(*i)) 608 if (DirectoryExists(*i))
627 continue; 609 continue;
628 if (mkdir(i->value().c_str(), 0700) == 0) 610 if (mkdir(i->value().c_str(), 0700) == 0)
629 continue; 611 continue;
630 // Mkdir failed, but it might have failed with EEXIST, or some other error 612 // Mkdir failed, but it might have failed with EEXIST, or some other error
631 // due to the the directory appearing out of thin air. This can occur if 613 // due to the the directory appearing out of thin air. This can occur if
632 // two processes are trying to create the same file system tree at the same 614 // two processes are trying to create the same file system tree at the same
633 // time. Check to see if it exists and make sure it is a directory. 615 // time. Check to see if it exists and make sure it is a directory.
634 int saved_errno = errno; 616 int saved_errno = errno;
635 if (!DirectoryExists(*i)) { 617 if (!DirectoryExists(*i)) {
636 if (error) 618 if (error)
637 *error = base::ErrnoToPlatformFileError(saved_errno); 619 *error = ErrnoToPlatformFileError(saved_errno);
638 return false; 620 return false;
639 } 621 }
640 } 622 }
641 return true; 623 return true;
642 } 624 }
643 625
626 } // namespace base
627
628 // -----------------------------------------------------------------------------
629
630 namespace file_util {
631
632 using base::stat_wrapper_t;
633 using base::CallStat;
634 using base::CallLstat;
635 using base::CreateAndOpenFdForTemporaryFile;
636 using base::DirectoryExists;
637 using base::FileEnumerator;
638 using base::FilePath;
639 using base::MakeAbsoluteFilePath;
640 using base::RealPath;
641 using base::VerifySpecificPathControlledByUser;
642
644 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { 643 base::FilePath MakeUniqueDirectory(const base::FilePath& path) {
645 const int kMaxAttempts = 20; 644 const int kMaxAttempts = 20;
646 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { 645 for (int attempts = 0; attempts < kMaxAttempts; attempts++) {
647 int uniquifier = 646 int uniquifier =
648 GetUniquePathNumber(path, base::FilePath::StringType()); 647 GetUniquePathNumber(path, base::FilePath::StringType());
649 if (uniquifier < 0) 648 if (uniquifier < 0)
650 break; 649 break;
651 base::FilePath test_path = (uniquifier == 0) ? path : 650 base::FilePath test_path = (uniquifier == 0) ? path :
652 path.InsertBeforeExtensionASCII( 651 path.InsertBeforeExtensionASCII(
653 base::StringPrintf(" (%d)", uniquifier)); 652 base::StringPrintf(" (%d)", uniquifier));
(...skipping 317 matching lines...) Expand 10 before | Expand all | Expand 10 after
971 result = false; 970 result = false;
972 if (IGNORE_EINTR(close(outfile)) < 0) 971 if (IGNORE_EINTR(close(outfile)) < 0)
973 result = false; 972 result = false;
974 973
975 return result; 974 return result;
976 } 975 }
977 #endif // !defined(OS_MACOSX) 976 #endif // !defined(OS_MACOSX)
978 977
979 } // namespace internal 978 } // namespace internal
980 } // namespace base 979 } // namespace base
OLDNEW
« no previous file with comments | « base/file_util.cc ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698