diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/scoped_temp_dir.cc | 20 | ||||
-rw-r--r-- | base/scoped_temp_dir.h | 4 | ||||
-rw-r--r-- | base/scoped_temp_dir_unittest.cc | 14 |
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()); +} |