diff options
author | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-31 23:16:39 +0000 |
---|---|---|
committer | jam@chromium.org <jam@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-31 23:16:39 +0000 |
commit | e285afa63ad7aa2d4efdbdde45568d26c7370fb0 (patch) | |
tree | 5d82a8e576bedc37902223eaaf3a7eeb21ee1888 /base | |
parent | aac510a6d64d91040cf589c701575ca09fa3d820 (diff) | |
download | chromium_src-e285afa63ad7aa2d4efdbdde45568d26c7370fb0.zip chromium_src-e285afa63ad7aa2d4efdbdde45568d26c7370fb0.tar.gz chromium_src-e285afa63ad7aa2d4efdbdde45568d26c7370fb0.tar.bz2 |
Move common file path related methods between chrome & content to file_util. I reduced the 4 methods into 3 by only having one GetUniquePathNumber which takes an optional suffix.
BUG=98716
Review URL: https://chromiumcodereview.appspot.com/9316004
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@119984 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util.cc | 29 | ||||
-rw-r--r-- | base/file_util.h | 7 |
2 files changed, 36 insertions, 0 deletions
diff --git a/base/file_util.cc b/base/file_util.cc index dc186d9..8f67cc3 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -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 @@ namespace { 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,27 @@ bool TruncateFile(FILE* file) { return true; } +int GetUniquePathNumber( + const FilePath& path, + const FilePath::StringType& suffix) { + bool have_suffix = !suffix.empty(); + if (!PathExists(path) && + (!have_suffix || !PathExists(FilePath(path.value() + suffix)))) { + return 0; + } + + FilePath new_path; + for (int count = 1; count <= kMaxUniqueFiles; ++count) { + new_path = path.InsertBeforeExtensionASCII(StringPrintf(" (%d)", count)); + if (!PathExists(new_path) && + (!have_suffix || !PathExists(FilePath(new_path.value() + suffix)))) { + return count; + } + } + + return -1; +} + bool ContainsPath(const FilePath &parent, const FilePath& child) { FilePath abs_parent = FilePath(parent); FilePath abs_child = FilePath(child); diff --git a/base/file_util.h b/base/file_util.h index 384f394..53264c1 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -388,6 +388,13 @@ BASE_EXPORT bool GetCurrentDirectory(FilePath* path); // Sets the current working directory for the process. BASE_EXPORT bool SetCurrentDirectory(const FilePath& path); +// Attempts to find a number that can be appended to the |path| to make it +// unique. If |path| does not exist, 0 is returned. If it fails to find such +// a number, -1 is returned. If |suffix| is not empty, also checks the +// existence of it with the given suffix. +BASE_EXPORT int GetUniquePathNumber(const FilePath& path, + const FilePath::StringType& suffix); + #if defined(OS_POSIX) // Test that |path| can only be changed by a given user and members of // a given set of groups. |