| 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 754 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 765 return true; | 765 return true; |
| 766 } | 766 } |
| 767 | 767 |
| 768 // Sets the current working directory for the process. | 768 // Sets the current working directory for the process. |
| 769 bool SetCurrentDirectory(const FilePath& path) { | 769 bool SetCurrentDirectory(const FilePath& path) { |
| 770 ThreadRestrictions::AssertIOAllowed(); | 770 ThreadRestrictions::AssertIOAllowed(); |
| 771 int ret = chdir(path.value().c_str()); | 771 int ret = chdir(path.value().c_str()); |
| 772 return !ret; | 772 return !ret; |
| 773 } | 773 } |
| 774 | 774 |
| 775 } // namespace base | |
| 776 | |
| 777 // ----------------------------------------------------------------------------- | |
| 778 | |
| 779 namespace file_util { | |
| 780 | |
| 781 using base::stat_wrapper_t; | |
| 782 using base::CallStat; | |
| 783 using base::CallLstat; | |
| 784 using base::CreateAndOpenFdForTemporaryFile; | |
| 785 using base::DirectoryExists; | |
| 786 using base::FileEnumerator; | |
| 787 using base::FilePath; | |
| 788 using base::MakeAbsoluteFilePath; | |
| 789 using base::VerifySpecificPathControlledByUser; | |
| 790 | |
| 791 base::FilePath MakeUniqueDirectory(const base::FilePath& path) { | |
| 792 const int kMaxAttempts = 20; | |
| 793 for (int attempts = 0; attempts < kMaxAttempts; attempts++) { | |
| 794 int uniquifier = | |
| 795 GetUniquePathNumber(path, base::FilePath::StringType()); | |
| 796 if (uniquifier < 0) | |
| 797 break; | |
| 798 base::FilePath test_path = (uniquifier == 0) ? path : | |
| 799 path.InsertBeforeExtensionASCII( | |
| 800 base::StringPrintf(" (%d)", uniquifier)); | |
| 801 if (mkdir(test_path.value().c_str(), 0777) == 0) | |
| 802 return test_path; | |
| 803 else if (errno != EEXIST) | |
| 804 break; | |
| 805 } | |
| 806 return base::FilePath(); | |
| 807 } | |
| 808 | |
| 809 FILE* OpenFile(const std::string& filename, const char* mode) { | |
| 810 return OpenFile(FilePath(filename), mode); | |
| 811 } | |
| 812 | |
| 813 bool VerifyPathControlledByUser(const FilePath& base, | 775 bool VerifyPathControlledByUser(const FilePath& base, |
| 814 const FilePath& path, | 776 const FilePath& path, |
| 815 uid_t owner_uid, | 777 uid_t owner_uid, |
| 816 const std::set<gid_t>& group_gids) { | 778 const std::set<gid_t>& group_gids) { |
| 817 if (base != path && !base.IsParent(path)) { | 779 if (base != path && !base.IsParent(path)) { |
| 818 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" | 780 DLOG(ERROR) << "|base| must be a subdirectory of |path|. base = \"" |
| 819 << base.value() << "\", path = \"" << path.value() << "\""; | 781 << base.value() << "\", path = \"" << path.value() << "\""; |
| 820 return false; | 782 return false; |
| 821 } | 783 } |
| 822 | 784 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 854 const unsigned kRootUid = 0; | 816 const unsigned kRootUid = 0; |
| 855 const FilePath kFileSystemRoot("/"); | 817 const FilePath kFileSystemRoot("/"); |
| 856 | 818 |
| 857 // The name of the administrator group on mac os. | 819 // The name of the administrator group on mac os. |
| 858 const char* const kAdminGroupNames[] = { | 820 const char* const kAdminGroupNames[] = { |
| 859 "admin", | 821 "admin", |
| 860 "wheel" | 822 "wheel" |
| 861 }; | 823 }; |
| 862 | 824 |
| 863 // Reading the groups database may touch the file system. | 825 // Reading the groups database may touch the file system. |
| 864 base::ThreadRestrictions::AssertIOAllowed(); | 826 ThreadRestrictions::AssertIOAllowed(); |
| 865 | 827 |
| 866 std::set<gid_t> allowed_group_ids; | 828 std::set<gid_t> allowed_group_ids; |
| 867 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) { | 829 for (int i = 0, ie = arraysize(kAdminGroupNames); i < ie; ++i) { |
| 868 struct group *group_record = getgrnam(kAdminGroupNames[i]); | 830 struct group *group_record = getgrnam(kAdminGroupNames[i]); |
| 869 if (!group_record) { | 831 if (!group_record) { |
| 870 DPLOG(ERROR) << "Could not get the group ID of group \"" | 832 DPLOG(ERROR) << "Could not get the group ID of group \"" |
| 871 << kAdminGroupNames[i] << "\"."; | 833 << kAdminGroupNames[i] << "\"."; |
| 872 continue; | 834 continue; |
| 873 } | 835 } |
| 874 | 836 |
| 875 allowed_group_ids.insert(group_record->gr_gid); | 837 allowed_group_ids.insert(group_record->gr_gid); |
| 876 } | 838 } |
| 877 | 839 |
| 878 return VerifyPathControlledByUser( | 840 return VerifyPathControlledByUser( |
| 879 kFileSystemRoot, path, kRootUid, allowed_group_ids); | 841 kFileSystemRoot, path, kRootUid, allowed_group_ids); |
| 880 } | 842 } |
| 881 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 843 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 882 | 844 |
| 883 int GetMaximumPathComponentLength(const FilePath& path) { | 845 int GetMaximumPathComponentLength(const FilePath& path) { |
| 884 base::ThreadRestrictions::AssertIOAllowed(); | 846 ThreadRestrictions::AssertIOAllowed(); |
| 885 return pathconf(path.value().c_str(), _PC_NAME_MAX); | 847 return pathconf(path.value().c_str(), _PC_NAME_MAX); |
| 886 } | 848 } |
| 887 | 849 |
| 888 } // namespace file_util | 850 // ----------------------------------------------------------------------------- |
| 889 | 851 |
| 890 namespace base { | |
| 891 namespace internal { | 852 namespace internal { |
| 892 | 853 |
| 893 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { | 854 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
| 894 ThreadRestrictions::AssertIOAllowed(); | 855 ThreadRestrictions::AssertIOAllowed(); |
| 895 // Windows compatibility: if to_path exists, from_path and to_path | 856 // Windows compatibility: if to_path exists, from_path and to_path |
| 896 // must be the same type, either both files, or both directories. | 857 // must be the same type, either both files, or both directories. |
| 897 stat_wrapper_t to_file_info; | 858 stat_wrapper_t to_file_info; |
| 898 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { | 859 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
| 899 stat_wrapper_t from_file_info; | 860 stat_wrapper_t from_file_info; |
| 900 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { | 861 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 960 result = false; | 921 result = false; |
| 961 if (IGNORE_EINTR(close(outfile)) < 0) | 922 if (IGNORE_EINTR(close(outfile)) < 0) |
| 962 result = false; | 923 result = false; |
| 963 | 924 |
| 964 return result; | 925 return result; |
| 965 } | 926 } |
| 966 #endif // !defined(OS_MACOSX) | 927 #endif // !defined(OS_MACOSX) |
| 967 | 928 |
| 968 } // namespace internal | 929 } // namespace internal |
| 969 } // namespace base | 930 } // namespace base |
| OLD | NEW |