OLD | NEW |
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 605 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 int saved_errno = errno; | 616 int saved_errno = errno; |
617 if (!DirectoryExists(*i)) { | 617 if (!DirectoryExists(*i)) { |
618 if (error) | 618 if (error) |
619 *error = ErrnoToPlatformFileError(saved_errno); | 619 *error = ErrnoToPlatformFileError(saved_errno); |
620 return false; | 620 return false; |
621 } | 621 } |
622 } | 622 } |
623 return true; | 623 return true; |
624 } | 624 } |
625 | 625 |
| 626 bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) { |
| 627 FilePath real_path_result; |
| 628 if (!RealPath(path, &real_path_result)) |
| 629 return false; |
| 630 |
| 631 // To be consistant with windows, fail if |real_path_result| is a |
| 632 // directory. |
| 633 stat_wrapper_t file_info; |
| 634 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 || |
| 635 S_ISDIR(file_info.st_mode)) |
| 636 return false; |
| 637 |
| 638 *normalized_path = real_path_result; |
| 639 return true; |
| 640 } |
| 641 |
626 } // namespace base | 642 } // namespace base |
627 | 643 |
628 // ----------------------------------------------------------------------------- | 644 // ----------------------------------------------------------------------------- |
629 | 645 |
630 namespace file_util { | 646 namespace file_util { |
631 | 647 |
632 using base::stat_wrapper_t; | 648 using base::stat_wrapper_t; |
633 using base::CallStat; | 649 using base::CallStat; |
634 using base::CallLstat; | 650 using base::CallLstat; |
635 using base::CreateAndOpenFdForTemporaryFile; | 651 using base::CreateAndOpenFdForTemporaryFile; |
636 using base::DirectoryExists; | 652 using base::DirectoryExists; |
637 using base::FileEnumerator; | 653 using base::FileEnumerator; |
638 using base::FilePath; | 654 using base::FilePath; |
639 using base::MakeAbsoluteFilePath; | 655 using base::MakeAbsoluteFilePath; |
640 using base::RealPath; | |
641 using base::VerifySpecificPathControlledByUser; | 656 using base::VerifySpecificPathControlledByUser; |
642 | 657 |
643 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { | 658 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { |
644 const int kMaxAttempts = 20; | 659 const int kMaxAttempts = 20; |
645 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { | 660 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { |
646 int uniquifier = | 661 int uniquifier = |
647 GetUniquePathNumber(path, base::FilePath::StringType()); | 662 GetUniquePathNumber(path, base::FilePath::StringType()); |
648 if (uniquifier < 0) | 663 if (uniquifier < 0) |
649 break; | 664 break; |
650 base::FilePath test_path = (uniquifier == 0) ? path : | 665 base::FilePath test_path = (uniquifier == 0) ? path : |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
797 return true; | 812 return true; |
798 } | 813 } |
799 | 814 |
800 // Sets the current working directory for the process. | 815 // Sets the current working directory for the process. |
801 bool SetCurrentDirectory(const FilePath& path) { | 816 bool SetCurrentDirectory(const FilePath& path) { |
802 base::ThreadRestrictions::AssertIOAllowed(); | 817 base::ThreadRestrictions::AssertIOAllowed(); |
803 int ret = chdir(path.value().c_str()); | 818 int ret = chdir(path.value().c_str()); |
804 return !ret; | 819 return !ret; |
805 } | 820 } |
806 | 821 |
807 bool NormalizeFilePath(const FilePath& path, FilePath* normalized_path) { | |
808 FilePath real_path_result; | |
809 if (!RealPath(path, &real_path_result)) | |
810 return false; | |
811 | |
812 // To be consistant with windows, fail if |real_path_result| is a | |
813 // directory. | |
814 stat_wrapper_t file_info; | |
815 if (CallStat(real_path_result.value().c_str(), &file_info) != 0 || | |
816 S_ISDIR(file_info.st_mode)) | |
817 return false; | |
818 | |
819 *normalized_path = real_path_result; | |
820 return true; | |
821 } | |
822 | |
823 bool VerifyPathControlledByUser(const FilePath& base, | 822 bool VerifyPathControlledByUser(const FilePath& base, |
824 const FilePath& path, | 823 const FilePath& path, |
825 uid_t owner_uid, | 824 uid_t owner_uid, |
826 const std::set<gid_t>& group_gids) { | 825 const std::set<gid_t>& group_gids) { |
827 if (base != path && !base.IsParent(path)) { | 826 if (base != path && !base.IsParent(path)) { |
828 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" | 827 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" |
829 << base.value() << "\", path = \"" << path.value() << "\""; | 828 << base.value() << "\", path = \"" << path.value() << "\""; |
830 return false; | 829 return false; |
831 } | 830 } |
832 | 831 |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
970 result = false; | 969 result = false; |
971 if (IGNORE_EINTR(close(outfile)) < 0) | 970 if (IGNORE_EINTR(close(outfile)) < 0) |
972 result = false; | 971 result = false; |
973 | 972 |
974 return result; | 973 return result; |
975 } | 974 } |
976 #endif // !defined(OS_MACOSX) | 975 #endif // !defined(OS_MACOSX) |
977 | 976 |
978 } // namespace internal | 977 } // namespace internal |
979 } // namespace base | 978 } // namespace base |
OLD | NEW |