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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 #endif | 51 #endif |
52 | 52 |
53 #if !defined(OS_IOS) | 53 #if !defined(OS_IOS) |
54 #include <grp.h> | 54 #include <grp.h> |
55 #endif | 55 #endif |
56 | 56 |
57 #if defined(OS_CHROMEOS) | 57 #if defined(OS_CHROMEOS) |
58 #include "base/chromeos/chromeos_version.h" | 58 #include "base/chromeos/chromeos_version.h" |
59 #endif | 59 #endif |
60 | 60 |
61 using base::FileEnumerator; | |
62 using base::FilePath; | |
63 using base::MakeAbsoluteFilePath; | |
64 | |
65 namespace base { | 61 namespace base { |
66 | 62 |
67 FilePath MakeAbsoluteFilePath(const FilePath& input) { | |
68 base::ThreadRestrictions::AssertIOAllowed(); | |
69 char full_path[PATH_MAX]; | |
70 if (realpath(input.value().c_str(), full_path) == NULL) | |
71 return FilePath(); | |
72 return FilePath(full_path); | |
73 } | |
74 | |
75 } // namespace base | |
76 | |
77 namespace file_util { | |
78 | |
79 namespace { | 63 namespace { |
80 | 64 |
81 #if defined(OS_BSD) || defined(OS_MACOSX) | 65 #if defined(OS_BSD) || defined(OS_MACOSX) |
82 typedef struct stat stat_wrapper_t; | 66 typedef struct stat stat_wrapper_t; |
83 static int CallStat(const char *path, stat_wrapper_t *sb) { | 67 static int CallStat(const char *path, stat_wrapper_t *sb) { |
84 base::ThreadRestrictions::AssertIOAllowed(); | 68 base::ThreadRestrictions::AssertIOAllowed(); |
85 return stat(path, sb); | 69 return stat(path, sb); |
86 } | 70 } |
87 static int CallLstat(const char *path, stat_wrapper_t *sb) { | 71 static int CallLstat(const char *path, stat_wrapper_t *sb) { |
88 base::ThreadRestrictions::AssertIOAllowed(); | 72 base::ThreadRestrictions::AssertIOAllowed(); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 DLOG(ERROR) << "Path " << path.value() | 129 DLOG(ERROR) << "Path " << path.value() |
146 << " is writable by any user."; | 130 << " is writable by any user."; |
147 return false; | 131 return false; |
148 } | 132 } |
149 | 133 |
150 return true; | 134 return true; |
151 } | 135 } |
152 | 136 |
153 } // namespace | 137 } // namespace |
154 | 138 |
155 static std::string TempFileName() { | 139 FilePath MakeAbsoluteFilePath(const FilePath& input) { |
156 #if defined(OS_MACOSX) | 140 ThreadRestrictions::AssertIOAllowed(); |
157 return base::StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID()); | 141 char full_path[PATH_MAX]; |
158 #endif | 142 if (realpath(input.value().c_str(), full_path) == NULL) |
159 | 143 return FilePath(); |
160 #if defined(GOOGLE_CHROME_BUILD) | 144 return FilePath(full_path); |
161 return std::string(".com.google.Chrome.XXXXXX"); | |
162 #else | |
163 return std::string(".org.chromium.Chromium.XXXXXX"); | |
164 #endif | |
165 } | 145 } |
166 | 146 |
167 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" | 147 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" |
168 // which works both with and without the recursive flag. I'm not sure we need | 148 // which works both with and without the recursive flag. I'm not sure we need |
169 // that functionality. If not, remove from file_util_win.cc, otherwise add it | 149 // that functionality. If not, remove from file_util_win.cc, otherwise add it |
170 // here. | 150 // here. |
171 bool Delete(const FilePath& path, bool recursive) { | 151 bool Delete(const FilePath& path, bool recursive) { |
172 base::ThreadRestrictions::AssertIOAllowed(); | 152 ThreadRestrictions::AssertIOAllowed(); |
173 const char* path_str = path.value().c_str(); | 153 const char* path_str = path.value().c_str(); |
174 stat_wrapper_t file_info; | 154 stat_wrapper_t file_info; |
175 int test = CallLstat(path_str, &file_info); | 155 int test = CallLstat(path_str, &file_info); |
176 if (test != 0) { | 156 if (test != 0) { |
177 // The Windows version defines this condition as success. | 157 // The Windows version defines this condition as success. |
178 bool ret = (errno == ENOENT || errno == ENOTDIR); | 158 bool ret = (errno == ENOENT || errno == ENOTDIR); |
179 return ret; | 159 return ret; |
180 } | 160 } |
181 if (!S_ISDIR(file_info.st_mode)) | 161 if (!S_ISDIR(file_info.st_mode)) |
182 return (unlink(path_str) == 0); | 162 return (unlink(path_str) == 0); |
(...skipping 15 matching lines...) Expand all Loading... |
198 } | 178 } |
199 | 179 |
200 while (success && !directories.empty()) { | 180 while (success && !directories.empty()) { |
201 FilePath dir = FilePath(directories.top()); | 181 FilePath dir = FilePath(directories.top()); |
202 directories.pop(); | 182 directories.pop(); |
203 success = (rmdir(dir.value().c_str()) == 0); | 183 success = (rmdir(dir.value().c_str()) == 0); |
204 } | 184 } |
205 return success; | 185 return success; |
206 } | 186 } |
207 | 187 |
| 188 } // namespace base |
| 189 |
| 190 // ----------------------------------------------------------------------------- |
| 191 |
| 192 namespace file_util { |
| 193 |
| 194 using base::stat_wrapper_t; |
| 195 using base::CallStat; |
| 196 using base::CallLstat; |
| 197 using base::FileEnumerator; |
| 198 using base::FilePath; |
| 199 using base::MakeAbsoluteFilePath; |
| 200 using base::RealPath; |
| 201 using base::VerifySpecificPathControlledByUser; |
| 202 |
| 203 static std::string TempFileName() { |
| 204 #if defined(OS_MACOSX) |
| 205 return base::StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID()); |
| 206 #endif |
| 207 |
| 208 #if defined(GOOGLE_CHROME_BUILD) |
| 209 return std::string(".com.google.Chrome.XXXXXX"); |
| 210 #else |
| 211 return std::string(".org.chromium.Chromium.XXXXXX"); |
| 212 #endif |
| 213 } |
| 214 |
208 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { | 215 bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { |
209 base::ThreadRestrictions::AssertIOAllowed(); | 216 base::ThreadRestrictions::AssertIOAllowed(); |
210 // Windows compatibility: if to_path exists, from_path and to_path | 217 // Windows compatibility: if to_path exists, from_path and to_path |
211 // must be the same type, either both files, or both directories. | 218 // must be the same type, either both files, or both directories. |
212 stat_wrapper_t to_file_info; | 219 stat_wrapper_t to_file_info; |
213 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { | 220 if (CallStat(to_path.value().c_str(), &to_file_info) == 0) { |
214 stat_wrapper_t from_file_info; | 221 stat_wrapper_t from_file_info; |
215 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { | 222 if (CallStat(from_path.value().c_str(), &from_file_info) == 0) { |
216 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) | 223 if (S_ISDIR(to_file_info.st_mode) != S_ISDIR(from_file_info.st_mode)) |
217 return false; | 224 return false; |
(...skipping 713 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
931 kFileSystemRoot, path, kRootUid, allowed_group_ids); | 938 kFileSystemRoot, path, kRootUid, allowed_group_ids); |
932 } | 939 } |
933 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 940 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
934 | 941 |
935 int GetMaximumPathComponentLength(const FilePath& path) { | 942 int GetMaximumPathComponentLength(const FilePath& path) { |
936 base::ThreadRestrictions::AssertIOAllowed(); | 943 base::ThreadRestrictions::AssertIOAllowed(); |
937 return pathconf(path.value().c_str(), _PC_NAME_MAX); | 944 return pathconf(path.value().c_str(), _PC_NAME_MAX); |
938 } | 945 } |
939 | 946 |
940 } // namespace file_util | 947 } // namespace file_util |
OLD | NEW |