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 #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 Loading... |
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 Loading... |
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; |
(...skipping 26 matching lines...) Expand all Loading... |
355 // FileEnumerator | 306 // FileEnumerator |
356 // | 307 // |
357 // Note: the main logic is in file_util_<platform>.cc | 308 // Note: the main logic is in file_util_<platform>.cc |
358 | 309 |
359 bool FileEnumerator::ShouldSkip(const FilePath& path) { | 310 bool FileEnumerator::ShouldSkip(const FilePath& path) { |
360 FilePath::StringType basename = path.BaseName().value(); | 311 FilePath::StringType basename = path.BaseName().value(); |
361 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); | 312 return IsDot(path) || (IsDotDot(path) && !(INCLUDE_DOT_DOT & file_type_)); |
362 } | 313 } |
363 | 314 |
364 } // namespace | 315 } // namespace |
OLD | NEW |