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

Side by Side Diff: base/file_util.cc

Issue 9316004: Move common file path related methods between chrome & content to file_util. I reduced the 4 meth... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 8 years, 10 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <io.h> 8 #include <io.h>
9 #endif 9 #endif
10 #include <stdio.h> 10 #include <stdio.h>
11 11
12 #include <fstream> 12 #include <fstream>
13 13
14 #include "base/file_path.h" 14 #include "base/file_path.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/stringprintf.h"
16 #include "base/string_piece.h" 17 #include "base/string_piece.h"
17 #include "base/string_util.h" 18 #include "base/string_util.h"
18 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
19 20
20 namespace { 21 namespace {
21 22
22 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); 23 const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.');
23 24
25 // The maximum number of 'uniquified' files we will try to create.
26 // This is used when the filename we're trying to download is already in use,
27 // so we create a new unique filename by appending " (nnn)" before the
28 // extension, where 1 <= nnn <= kMaxUniqueFiles.
29 // Also used by code that cleans up said files.
30 static const int kMaxUniqueFiles = 100;
31
24 } // namespace 32 } // namespace
25 33
26 namespace file_util { 34 namespace file_util {
27 35
28 bool EndsWithSeparator(const FilePath& path) { 36 bool EndsWithSeparator(const FilePath& path) {
29 FilePath::StringType value = path.value(); 37 FilePath::StringType value = path.value();
30 if (value.empty()) 38 if (value.empty())
31 return false; 39 return false;
32 40
33 return FilePath::IsSeparator(value[value.size() - 1]); 41 return FilePath::IsSeparator(value[value.size() - 1]);
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 if (_chsize(fd, current_offset) != 0) 238 if (_chsize(fd, current_offset) != 0)
231 return false; 239 return false;
232 #else 240 #else
233 int fd = fileno(file); 241 int fd = fileno(file);
234 if (ftruncate(fd, current_offset) != 0) 242 if (ftruncate(fd, current_offset) != 0)
235 return false; 243 return false;
236 #endif 244 #endif
237 return true; 245 return true;
238 } 246 }
239 247
248 int GetUniquePathNumber(
Evan Martin 2012/01/31 21:23:44 Should this be in the anon namespace?
jam 2012/01/31 21:54:30 no it's in the header, it's used by a few places
249 const FilePath& path,
250 const FilePath::StringType& suffix) {
251 bool have_suffix = !suffix.empty();
252 if (!PathExists(path) &&
253 (!have_suffix || !PathExists(AppendSuffixToPath(path, suffix)))) {
254 return 0;
255 }
256
257 FilePath new_path;
258 for (int count = 1; count <= kMaxUniqueFiles; ++count) {
259 new_path = FilePath(path);
260 AppendNumberToPath(&new_path, count);
261
262 if (!PathExists(new_path) &&
263 (!have_suffix || !PathExists(AppendSuffixToPath(new_path, suffix)))) {
264 return count;
265 }
266 }
267
268 return -1;
269 }
270
240 bool ContainsPath(const FilePath &parent, const FilePath& child) { 271 bool ContainsPath(const FilePath &parent, const FilePath& child) {
241 FilePath abs_parent = FilePath(parent); 272 FilePath abs_parent = FilePath(parent);
242 FilePath abs_child = FilePath(child); 273 FilePath abs_child = FilePath(child);
243 274
244 if (!file_util::AbsolutePath(&abs_parent) || 275 if (!file_util::AbsolutePath(&abs_parent) ||
245 !file_util::AbsolutePath(&abs_child)) 276 !file_util::AbsolutePath(&abs_child))
246 return false; 277 return false;
247 278
248 #if defined(OS_WIN) 279 #if defined(OS_WIN)
249 // file_util::AbsolutePath() does not flatten case on Windows, so we must do 280 // file_util::AbsolutePath() does not flatten case on Windows, so we must do
250 // a case-insensitive compare. 281 // a case-insensitive compare.
251 if (!StartsWith(abs_child.value(), abs_parent.value(), false)) 282 if (!StartsWith(abs_child.value(), abs_parent.value(), false))
252 #else 283 #else
253 if (!StartsWithASCII(abs_child.value(), abs_parent.value(), true)) 284 if (!StartsWithASCII(abs_child.value(), abs_parent.value(), true))
254 #endif 285 #endif
255 return false; 286 return false;
256 287
257 // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need 288 // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need
258 // to check kSeparators[0]. 289 // to check kSeparators[0].
259 if (abs_child.value().length() <= abs_parent.value().length() || 290 if (abs_child.value().length() <= abs_parent.value().length() ||
260 abs_child.value()[abs_parent.value().length()] != 291 abs_child.value()[abs_parent.value().length()] !=
261 FilePath::kSeparators[0]) 292 FilePath::kSeparators[0])
262 return false; 293 return false;
263 294
264 return true; 295 return true;
265 } 296 }
266 297
298 void AppendNumberToPath(FilePath* path, int number) {
299 *path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number));
300 }
301
302 FilePath AppendSuffixToPath(
303 const FilePath& path,
304 const FilePath::StringType& suffix) {
305 FilePath::StringType file_name;
306 base::SStringPrintf(
307 &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(),
308 suffix.c_str());
Evan Martin 2012/01/31 21:23:44 Simpler: return FilePath(path.value() + suffix)
jam 2012/01/31 21:54:30 good point, done! also same for AppendNumberToPat
309 return FilePath(file_name);
310 }
311
267 int64 ComputeDirectorySize(const FilePath& root_path) { 312 int64 ComputeDirectorySize(const FilePath& root_path) {
268 int64 running_size = 0; 313 int64 running_size = 0;
269 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); 314 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES);
270 for (FilePath current = file_iter.Next(); !current.empty(); 315 for (FilePath current = file_iter.Next(); !current.empty();
271 current = file_iter.Next()) { 316 current = file_iter.Next()) {
272 FileEnumerator::FindInfo info; 317 FileEnumerator::FindInfo info;
273 file_iter.GetFindInfo(&info); 318 file_iter.GetFindInfo(&info);
274 #if defined(OS_WIN) 319 #if defined(OS_WIN)
275 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; 320 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh };
276 running_size += li.QuadPart; 321 running_size += li.QuadPart;
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 // FileEnumerator 438 // FileEnumerator
394 // 439 //
395 // Note: the main logic is in file_util_<platform>.cc 440 // Note: the main logic is in file_util_<platform>.cc
396 441
397 bool FileEnumerator::ShouldSkip(const FilePath& path) { 442 bool FileEnumerator::ShouldSkip(const FilePath& path) {
398 FilePath::StringType basename = path.BaseName().value(); 443 FilePath::StringType basename = path.BaseName().value();
399 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); 444 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_));
400 } 445 }
401 446
402 } // namespace 447 } // namespace
OLDNEW
« base/file_util.h ('K') | « base/file_util.h ('k') | chrome/browser/DEPS » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698