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

Unified 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, 11 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 side-by-side diff with in-line comments
Download patch
« base/file_util.h ('K') | « base/file_util.h ('k') | chrome/browser/DEPS » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
« 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