summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorcbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-23 10:06:47 +0000
committercbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-10-23 10:06:47 +0000
commit9fefee68b27573a37c00bd428d62b72eecbc8993 (patch)
treeec3769c452ad84dc1f347b8b1d116d1e6979b5b6 /base
parent27c8112304862f884ad63f90b192a86866d4deda (diff)
downloadchromium_src-9fefee68b27573a37c00bd428d62b72eecbc8993.zip
chromium_src-9fefee68b27573a37c00bd428d62b72eecbc8993.tar.gz
chromium_src-9fefee68b27573a37c00bd428d62b72eecbc8993.tar.bz2
ScopedTempDir does not allow multiple Create* or Set calls without intervening Delete/Take calls.
BUG=None TEST=base_unittests --gtest_filter="*ScopedTempDir*", all other tests pass. Review URL: http://codereview.chromium.org/3980006 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63641 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/scoped_temp_dir.cc20
-rw-r--r--base/scoped_temp_dir.h4
-rw-r--r--base/scoped_temp_dir_unittest.cc14
3 files changed, 32 insertions, 6 deletions
diff --git a/base/scoped_temp_dir.cc b/base/scoped_temp_dir.cc
index a510ddf..5cd13b4 100644
--- a/base/scoped_temp_dir.cc
+++ b/base/scoped_temp_dir.cc
@@ -15,6 +15,9 @@ ScopedTempDir::~ScopedTempDir() {
}
bool ScopedTempDir::CreateUniqueTempDir() {
+ if (!path_.empty())
+ return false;
+
// This "scoped_dir" prefix is only used on Windows and serves as a template
// for the unique name.
if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"),
@@ -25,7 +28,10 @@ bool ScopedTempDir::CreateUniqueTempDir() {
}
bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) {
- // If |path| does not exist, create it.
+ if (!path_.empty())
+ return false;
+
+ // If |base_path| does not exist, create it.
if (!file_util::CreateDirectory(base_path))
return false;
@@ -33,18 +39,20 @@ bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) {
if (!file_util::CreateTemporaryDirInDir(
base_path,
FILE_PATH_LITERAL("scoped_dir_"),
- &path_)) {
+ &path_))
return false;
- }
+
return true;
}
bool ScopedTempDir::Set(const FilePath& path) {
- DCHECK(path_.empty());
+ if (!path_.empty())
+ return false;
+
if (!file_util::DirectoryExists(path) &&
- !file_util::CreateDirectory(path)) {
+ !file_util::CreateDirectory(path))
return false;
- }
+
path_ = path;
return true;
}
diff --git a/base/scoped_temp_dir.h b/base/scoped_temp_dir.h
index 66d52f6..6845562 100644
--- a/base/scoped_temp_dir.h
+++ b/base/scoped_temp_dir.h
@@ -11,6 +11,10 @@
// deletion occurs during the destructor, no further error handling is possible
// if the directory fails to be deleted. As a result, deletion is not
// guaranteed by this class.
+//
+// Multiple calls to the methods which establish a temporary directory
+// (CreateUniqueTempDir, CreateUniqueTempDirUnderPath, and Set) must have
+// intervening calls to Delete or Take, or the calls will fail.
#include "base/file_path.h"
diff --git a/base/scoped_temp_dir_unittest.cc b/base/scoped_temp_dir_unittest.cc
index 4be0d07..cf5fed3 100644
--- a/base/scoped_temp_dir_unittest.cc
+++ b/base/scoped_temp_dir_unittest.cc
@@ -73,3 +73,17 @@ TEST(ScopedTempDir, UniqueTempDirUnderPath) {
}
EXPECT_FALSE(file_util::DirectoryExists(test_path));
}
+
+TEST(ScopedTempDir, MultipleInvocations) {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(dir.CreateUniqueTempDir());
+ dir.Delete();
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(dir.CreateUniqueTempDir());
+ ScopedTempDir other_dir;
+ other_dir.Set(dir.Take());
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(dir.CreateUniqueTempDir());
+ EXPECT_FALSE(other_dir.CreateUniqueTempDir());
+}