Chromium Code Reviews| Index: base/file_util.cc |
| =================================================================== |
| --- base/file_util.cc (revision 119906) |
| +++ base/file_util.cc (working copy) |
| @@ -13,6 +13,7 @@ |
| #include "base/file_path.h" |
| #include "base/logging.h" |
| +#include "base/stringprintf.h" |
| #include "base/string_piece.h" |
| #include "base/string_util.h" |
| #include "base/utf_string_conversions.h" |
| @@ -21,6 +22,13 @@ |
| const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); |
| +// The maximum number of 'uniquified' files we will try to create. |
| +// This is used when the filename we're trying to download is already in use, |
| +// so we create a new unique filename by appending " (nnn)" before the |
| +// extension, where 1 <= nnn <= kMaxUniqueFiles. |
| +// Also used by code that cleans up said files. |
| +static const int kMaxUniqueFiles = 100; |
| + |
| } // namespace |
| namespace file_util { |
| @@ -237,6 +245,29 @@ |
| return true; |
| } |
| +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
|
| + const FilePath& path, |
| + const FilePath::StringType& suffix) { |
| + bool have_suffix = !suffix.empty(); |
| + if (!PathExists(path) && |
| + (!have_suffix || !PathExists(AppendSuffixToPath(path, suffix)))) { |
| + return 0; |
| + } |
| + |
| + FilePath new_path; |
| + for (int count = 1; count <= kMaxUniqueFiles; ++count) { |
| + new_path = FilePath(path); |
| + AppendNumberToPath(&new_path, count); |
| + |
| + if (!PathExists(new_path) && |
| + (!have_suffix || !PathExists(AppendSuffixToPath(new_path, suffix)))) { |
| + return count; |
| + } |
| + } |
| + |
| + return -1; |
| +} |
| + |
| bool ContainsPath(const FilePath &parent, const FilePath& child) { |
| FilePath abs_parent = FilePath(parent); |
| FilePath abs_child = FilePath(child); |
| @@ -264,6 +295,20 @@ |
| return true; |
| } |
| +void AppendNumberToPath(FilePath* path, int number) { |
| + *path = path->InsertBeforeExtensionASCII(StringPrintf(" (%d)", number)); |
| +} |
| + |
| +FilePath AppendSuffixToPath( |
| + const FilePath& path, |
| + const FilePath::StringType& suffix) { |
| + FilePath::StringType file_name; |
| + base::SStringPrintf( |
| + &file_name, PRFilePathLiteral PRFilePathLiteral, path.value().c_str(), |
| + 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
|
| + return FilePath(file_name); |
| +} |
| + |
| int64 ComputeDirectorySize(const FilePath& root_path) { |
| int64 running_size = 0; |
| FileEnumerator file_iter(root_path, true, FileEnumerator::FILES); |