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

Side by Side Diff: base/file_util_posix.cc

Issue 13196006: Move path functions from file_util to FilePath object. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: git try Created 7 years, 8 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
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::FilePath; 61 using base::FilePath;
62 using base::MakeAbsoluteFilePath;
63
64 namespace base {
65
66 FilePath MakeAbsoluteFilePath(const FilePath& input) {
67 base::ThreadRestrictions::AssertIOAllowed();
68 char full_path[PATH_MAX];
69 if (realpath(input.value().c_str(), full_path) == NULL)
70 return FilePath();
71 return FilePath(full_path);
72 }
73
74 } // namespace base
62 75
63 namespace file_util { 76 namespace file_util {
64 77
65 namespace { 78 namespace {
66 79
67 #if defined(OS_BSD) || defined(OS_MACOSX) 80 #if defined(OS_BSD) || defined(OS_MACOSX)
68 typedef struct stat stat_wrapper_t; 81 typedef struct stat stat_wrapper_t;
69 static int CallStat(const char *path, stat_wrapper_t *sb) { 82 static int CallStat(const char *path, stat_wrapper_t *sb) {
70 base::ThreadRestrictions::AssertIOAllowed(); 83 base::ThreadRestrictions::AssertIOAllowed();
71 return stat(path, sb); 84 return stat(path, sb);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 return base::StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID()); 156 return base::StringPrintf(".%s.XXXXXX", base::mac::BaseBundleID());
144 #endif 157 #endif
145 158
146 #if defined(GOOGLE_CHROME_BUILD) 159 #if defined(GOOGLE_CHROME_BUILD)
147 return std::string(".com.google.Chrome.XXXXXX"); 160 return std::string(".com.google.Chrome.XXXXXX");
148 #else 161 #else
149 return std::string(".org.chromium.Chromium.XXXXXX"); 162 return std::string(".org.chromium.Chromium.XXXXXX");
150 #endif 163 #endif
151 } 164 }
152 165
153 bool AbsolutePath(FilePath* path) {
154 base::ThreadRestrictions::AssertIOAllowed(); // For realpath().
155 char full_path[PATH_MAX];
156 if (realpath(path->value().c_str(), full_path) == NULL)
157 return false;
158 *path = FilePath(full_path);
159 return true;
160 }
161
162 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*" 166 // TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
163 // which works both with and without the recursive flag. I'm not sure we need 167 // which works both with and without the recursive flag. I'm not sure we need
164 // that functionality. If not, remove from file_util_win.cc, otherwise add it 168 // that functionality. If not, remove from file_util_win.cc, otherwise add it
165 // here. 169 // here.
166 bool Delete(const FilePath& path, bool recursive) { 170 bool Delete(const FilePath& path, bool recursive) {
167 base::ThreadRestrictions::AssertIOAllowed(); 171 base::ThreadRestrictions::AssertIOAllowed();
168 const char* path_str = path.value().c_str(); 172 const char* path_str = path.value().c_str();
169 stat_wrapper_t file_info; 173 stat_wrapper_t file_info;
170 int test = CallLstat(path_str, &file_info); 174 int test = CallLstat(path_str, &file_info);
171 if (test != 0) { 175 if (test != 0) {
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 250
247 char top_dir[PATH_MAX]; 251 char top_dir[PATH_MAX];
248 if (base::strlcpy(top_dir, from_path.value().c_str(), 252 if (base::strlcpy(top_dir, from_path.value().c_str(),
249 arraysize(top_dir)) >= arraysize(top_dir)) { 253 arraysize(top_dir)) >= arraysize(top_dir)) {
250 return false; 254 return false;
251 } 255 }
252 256
253 // This function does not properly handle destinations within the source 257 // This function does not properly handle destinations within the source
254 FilePath real_to_path = to_path; 258 FilePath real_to_path = to_path;
255 if (PathExists(real_to_path)) { 259 if (PathExists(real_to_path)) {
256 if (!AbsolutePath(&real_to_path)) 260 real_to_path = MakeAbsoluteFilePath(real_to_path);
261 if (real_to_path.empty())
257 return false; 262 return false;
258 } else { 263 } else {
259 real_to_path = real_to_path.DirName(); 264 real_to_path = MakeAbsoluteFilePath(real_to_path.DirName());
260 if (!AbsolutePath(&real_to_path)) 265 if (real_to_path.empty())
261 return false; 266 return false;
262 } 267 }
263 FilePath real_from_path = from_path; 268 FilePath real_from_path = MakeAbsoluteFilePath(from_path);
264 if (!AbsolutePath(&real_from_path)) 269 if (real_from_path.empty())
265 return false; 270 return false;
266 if (real_to_path.value().size() >= real_from_path.value().size() && 271 if (real_to_path.value().size() >= real_from_path.value().size() &&
267 real_to_path.value().compare(0, real_from_path.value().size(), 272 real_to_path.value().compare(0, real_from_path.value().size(),
268 real_from_path.value()) == 0) 273 real_from_path.value()) == 0)
269 return false; 274 return false;
270 275
271 bool success = true; 276 bool success = true;
272 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS; 277 int traverse_type = FileEnumerator::FILES | FileEnumerator::SHOW_SYM_LINKS;
273 if (recursive) 278 if (recursive)
274 traverse_type |= FileEnumerator::DIRECTORIES; 279 traverse_type |= FileEnumerator::DIRECTORIES;
(...skipping 781 matching lines...) Expand 10 before | Expand all | Expand 10 after
1056 kFileSystemRoot, path, kRootUid, allowed_group_ids); 1061 kFileSystemRoot, path, kRootUid, allowed_group_ids);
1057 } 1062 }
1058 #endif // defined(OS_MACOSX) && !defined(OS_IOS) 1063 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
1059 1064
1060 int GetMaximumPathComponentLength(const FilePath& path) { 1065 int GetMaximumPathComponentLength(const FilePath& path) {
1061 base::ThreadRestrictions::AssertIOAllowed(); 1066 base::ThreadRestrictions::AssertIOAllowed();
1062 return pathconf(path.value().c_str(), _PC_NAME_MAX); 1067 return pathconf(path.value().c_str(), _PC_NAME_MAX);
1063 } 1068 }
1064 1069
1065 } // namespace file_util 1070 } // namespace file_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698