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

Side by Side Diff: base/file_util.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 #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>
(...skipping 19 matching lines...) Expand all
30 // extension, where 1 <= nnn <= kMaxUniqueFiles. 30 // extension, where 1 <= nnn <= kMaxUniqueFiles.
31 // Also used by code that cleans up said files. 31 // Also used by code that cleans up said files.
32 static const int kMaxUniqueFiles = 100; 32 static const int kMaxUniqueFiles = 100;
33 33
34 } // namespace 34 } // namespace
35 35
36 namespace file_util { 36 namespace file_util {
37 37
38 bool g_bug108724_debug = false; 38 bool g_bug108724_debug = false;
39 39
40 bool EndsWithSeparator(const FilePath& path) {
41 FilePath::StringType value = path.value();
42 if (value.empty())
43 return false;
44
45 return FilePath::IsSeparator(value[value.size() - 1]);
46 }
47
48 bool EnsureEndsWithSeparator(FilePath* path) {
49 if (!DirectoryExists(*path))
50 return false;
51
52 if (EndsWithSeparator(*path))
53 return true;
54
55 FilePath::StringType& path_str =
56 const_cast<FilePath::StringType&>(path->value());
57 path_str.append(&FilePath::kSeparators[0], 1);
58
59 return true;
60 }
61
62 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) { 40 void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) {
63 FilePath::StringType& value = 41 FilePath::StringType& value =
64 const_cast<FilePath::StringType&>(path->value()); 42 const_cast<FilePath::StringType&>(path->value());
65 43
66 const FilePath::StringType::size_type last_dot = 44 const FilePath::StringType::size_type last_dot =
67 value.rfind(kExtensionSeparator); 45 value.rfind(kExtensionSeparator);
68 const FilePath::StringType::size_type last_separator = 46 const FilePath::StringType::size_type last_separator =
69 value.find_last_of(FilePath::StringType(FilePath::kSeparators)); 47 value.find_last_of(FilePath::StringType(FilePath::kSeparators));
70 48
71 if (last_dot == FilePath::StringType::npos || 49 if (last_dot == FilePath::StringType::npos ||
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count)); 260 path.InsertBeforeExtensionASCII(base::StringPrintf(" (%d)", count));
283 if (!PathExists(new_path) && 261 if (!PathExists(new_path) &&
284 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { 262 (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) {
285 return count; 263 return count;
286 } 264 }
287 } 265 }
288 266
289 return -1; 267 return -1;
290 } 268 }
291 269
292 bool ContainsPath(const FilePath &parent, const FilePath& child) {
293 FilePath abs_parent = FilePath(parent);
294 FilePath abs_child = FilePath(child);
295
296 if (!file_util::AbsolutePath(&abs_parent) ||
297 !file_util::AbsolutePath(&abs_child))
298 return false;
299
300 #if defined(OS_WIN)
301 // file_util::AbsolutePath() does not flatten case on Windows, so we must do
302 // a case-insensitive compare.
303 if (!StartsWith(abs_child.value(), abs_parent.value(), false))
304 #else
305 if (!StartsWithASCII(abs_child.value(), abs_parent.value(), true))
306 #endif
307 return false;
308
309 // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we only need
310 // to check kSeparators[0].
311 if (abs_child.value().length() <= abs_parent.value().length() ||
312 abs_child.value()[abs_parent.value().length()] !=
313 FilePath::kSeparators[0])
314 return false;
315
316 return true;
317 }
318
319 int64 ComputeDirectorySize(const FilePath& root_path) { 270 int64 ComputeDirectorySize(const FilePath& root_path) {
320 int64 running_size = 0; 271 int64 running_size = 0;
321 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); 272 FileEnumerator file_iter(root_path, true, FileEnumerator::FILES);
322 for (FilePath current = file_iter.Next(); !current.empty(); 273 for (FilePath current = file_iter.Next(); !current.empty();
323 current = file_iter.Next()) { 274 current = file_iter.Next()) {
324 FileEnumerator::FindInfo info; 275 FileEnumerator::FindInfo info;
325 file_iter.GetFindInfo(&info); 276 file_iter.GetFindInfo(&info);
326 #if defined(OS_WIN) 277 #if defined(OS_WIN)
327 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh }; 278 LARGE_INTEGER li = { info.nFileSizeLow, info.nFileSizeHigh };
328 running_size += li.QuadPart; 279 running_size += li.QuadPart;
329 #else 280 #else
330 running_size += info.stat.st_size; 281 running_size += info.stat.st_size;
331 #endif 282 #endif
332 } 283 }
333 return running_size; 284 return running_size;
334 } 285 }
335 286
336 /////////////////////////////////////////////// 287 ///////////////////////////////////////////////
337 // FileEnumerator 288 // FileEnumerator
338 // 289 //
339 // Note: the main logic is in file_util_<platform>.cc 290 // Note: the main logic is in file_util_<platform>.cc
340 291
341 bool FileEnumerator::ShouldSkip(const FilePath& path) { 292 bool FileEnumerator::ShouldSkip(const FilePath& path) {
342 FilePath::StringType basename = path.BaseName().value(); 293 FilePath::StringType basename = path.BaseName().value();
343 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); 294 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_));
344 } 295 }
345 296
346 } // namespace 297 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698