summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorerikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-28 20:49:35 +0000
committererikkay@google.com <erikkay@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-28 20:49:35 +0000
commit4a251111c2d7754fb99f49e1c7da01dc2813d05f (patch)
tree75168f66fb01c306baa5be1210354b55eaf6d303
parent6a9a660a03de66f8ed1e2d75ea56efcde9ad6d74 (diff)
downloadchromium_src-4a251111c2d7754fb99f49e1c7da01dc2813d05f.zip
chromium_src-4a251111c2d7754fb99f49e1c7da01dc2813d05f.tar.gz
chromium_src-4a251111c2d7754fb99f49e1c7da01dc2813d05f.tar.bz2
Add ScopedTempDir - a class that manages the lifetime of a temporary directory.
Review URL: http://codereview.chromium.org/19411 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8824 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/base.xcodeproj/project.pbxproj10
-rw-r--r--base/base_lib.scons2
-rw-r--r--base/build/base.vcproj8
-rw-r--r--base/build/base_unittests.vcproj4
-rw-r--r--base/scoped_temp_dir.cc48
-rw-r--r--base/scoped_temp_dir.h47
-rw-r--r--base/scoped_temp_dir_unittest.cc58
7 files changed, 177 insertions, 0 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj
index 5c9ab7a..693a873 100644
--- a/base/base.xcodeproj/project.pbxproj
+++ b/base/base.xcodeproj/project.pbxproj
@@ -37,6 +37,7 @@
/* Begin PBXBuildFile section */
141593B80EA63EBE00E32418 /* thread_collision_warner_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 146C6A6E0EA63D970029E7B6 /* thread_collision_warner_unittest.cc */; };
146C6A6B0EA63D4F0029E7B6 /* thread_collision_warner.cc in Sources */ = {isa = PBXBuildFile; fileRef = 146C6A620EA63CAE0029E7B6 /* thread_collision_warner.cc */; };
+ 232269C2037712E93DA5CC83 /* scoped_temp_dir.cc in Sources */ = {isa = PBXBuildFile; fileRef = 119C3753E442CB1E1212F584 /* scoped_temp_dir.cc */; };
32AC71B80F2E5321002BDDC8 /* jpeg_codec_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 32AC71B50F2E52FC002BDDC8 /* jpeg_codec_unittest.cc */; };
32AC71BF0F2E5421002BDDC8 /* jpeg_codec.cc in Sources */ = {isa = PBXBuildFile; fileRef = 32AC71B60F2E530F002BDDC8 /* jpeg_codec.cc */; };
32AC72300F2E64F7002BDDC8 /* libjpeg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 32AC718E0F2E4F62002BDDC8 /* libjpeg.a */; };
@@ -49,6 +50,7 @@
4D4C5B5E0EF1B7C1002CA805 /* watchdog_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D4C5B5D0EF1B7C1002CA805 /* watchdog_unittest.cc */; };
536092FA0ECE474500D1B91E /* dtoa.cc in Sources */ = {isa = PBXBuildFile; fileRef = 536092F80ECE474500D1B91E /* dtoa.cc */; settings = {COMPILER_FLAGS = "-Wno-write-strings -Wno-all"; }; };
536092FB0ECE474500D1B91E /* g_fmt.cc in Sources */ = {isa = PBXBuildFile; fileRef = 536092F90ECE474500D1B91E /* g_fmt.cc */; settings = {COMPILER_FLAGS = "-Wno-write-strings -Wno-all"; }; };
+ 75578FC5288BDE1C2C16D391 /* scoped_temp_dir_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 66782460675DA17C9A985B70 /* scoped_temp_dir_unittest.cc */; };
7B26302F0E82F218001CE27F /* message_pump_libevent.cc in Sources */ = {isa = PBXBuildFile; fileRef = 7B26302D0E82F218001CE27F /* message_pump_libevent.cc */; };
7B2630330E82F258001CE27F /* libevent.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 7B2630240E82F1E6001CE27F /* libevent.a */; };
7B4C5F4A0E4B6BF900679E8F /* sys_string_conversions_mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C5F480E4B6BF900679E8F /* sys_string_conversions_mac.mm */; };
@@ -396,6 +398,8 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 10894DCE53EF606747078F94 /* scoped_temp_dir.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scoped_temp_dir.h; sourceTree = "<group>"; };
+ 119C3753E442CB1E1212F584 /* scoped_temp_dir.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = scoped_temp_dir.cc; sourceTree = "<group>"; };
146C6A610EA63C9F0029E7B6 /* thread_collision_warner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = thread_collision_warner.h; sourceTree = "<group>"; };
146C6A620EA63CAE0029E7B6 /* thread_collision_warner.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_collision_warner.cc; sourceTree = "<group>"; };
146C6A6E0EA63D970029E7B6 /* thread_collision_warner_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_collision_warner_unittest.cc; sourceTree = "<group>"; };
@@ -416,6 +420,7 @@
536092F70ECE474500D1B91E /* dmg_fp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = dmg_fp.h; path = third_party/dmg_fp/dmg_fp.h; sourceTree = "<group>"; };
536092F80ECE474500D1B91E /* dtoa.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = dtoa.cc; path = third_party/dmg_fp/dtoa.cc; sourceTree = "<group>"; };
536092F90ECE474500D1B91E /* g_fmt.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = g_fmt.cc; path = third_party/dmg_fp/g_fmt.cc; sourceTree = "<group>"; };
+ 66782460675DA17C9A985B70 /* scoped_temp_dir_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = scoped_temp_dir_unittest.cc; sourceTree = "<group>"; };
7B1435DF0E78419700901940 /* native_widget_types.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = native_widget_types.h; sourceTree = "<group>"; };
7B26301F0E82F1E6001CE27F /* libevent.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = libevent.xcodeproj; path = third_party/libevent/libevent.xcodeproj; sourceTree = "<group>"; };
7B26302D0E82F218001CE27F /* message_pump_libevent.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = message_pump_libevent.cc; sourceTree = "<group>"; };
@@ -1008,6 +1013,9 @@
7BA35DD20E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm */,
825403610D92D27C0006B936 /* scoped_ptr.h */,
7BD8F4CF0E65B4A900034DE9 /* scoped_ptr_unittest.cc */,
+ 119C3753E442CB1E1212F584 /* scoped_temp_dir.cc */,
+ 10894DCE53EF606747078F94 /* scoped_temp_dir.h */,
+ 66782460675DA17C9A985B70 /* scoped_temp_dir_unittest.cc */,
825403620D92D27C0006B936 /* sha2.cc */,
825403630D92D27C0006B936 /* sha2.h */,
8254036F0D92D2840006B936 /* sha256.h */,
@@ -1451,6 +1459,7 @@
8246548C0DC259DB007C2BAA /* revocable_store.cc in Sources */,
B5E8F6CC0EBFB38E008DD1E9 /* scoped_clipboard_writer.cc in Sources */,
7BA35DD30E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm in Sources */,
+ 232269C2037712E93DA5CC83 /* scoped_temp_dir.cc in Sources */,
ABF4B9BC0DC2BD1000A6E319 /* sha2.cc in Sources */,
ABF4B9BE0DC2BD1500A6E319 /* sha512.cc in Sources */,
824653740DC12D0E007C2BAA /* shared_memory_posix.cc in Sources */,
@@ -1533,6 +1542,7 @@
7B78D3990E54FE0100609465 /* ref_counted_unittest.cc in Sources */,
7B78D39A0E54FE0100609465 /* run_all_unittests.cc in Sources */,
7BD8F4D50E65B55000034DE9 /* scoped_ptr_unittest.cc in Sources */,
+ 75578FC5288BDE1C2C16D391 /* scoped_temp_dir_unittest.cc in Sources */,
7B78D39B0E54FE0100609465 /* sha2_unittest.cc in Sources */,
BA5CC5840E788093004EDD45 /* shared_memory_unittest.cc in Sources */,
7BAE30E60E6D939F00C3F750 /* simple_thread_unittest.cc in Sources */,
diff --git a/base/base_lib.scons b/base/base_lib.scons
index 397c53a..02467e3 100644
--- a/base/base_lib.scons
+++ b/base/base_lib.scons
@@ -190,6 +190,8 @@ input_files = ChromeFileList([
'scoped_handle.h',
'scoped_nsautorelease_pool.h',
'scoped_ptr.h',
+ 'scoped_temp_dir.cc',
+ 'scoped_temp_dir.h',
'sha2.cc',
'sha2.h',
'third_party/nss/sha256.h',
diff --git a/base/build/base.vcproj b/base/build/base.vcproj
index cb41422..ae9ba94 100644
--- a/base/build/base.vcproj
+++ b/base/build/base.vcproj
@@ -726,6 +726,14 @@
>
</File>
<File
+ RelativePath="..\scoped_temp_dir.cc"
+ >
+ </File>
+ <File
+ RelativePath="..\scoped_temp_dir.h"
+ >
+ </File>
+ <File
RelativePath="..\sha2.cc"
>
</File>
diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj
index 22df2c8..7d80df8 100644
--- a/base/build/base_unittests.vcproj
+++ b/base/build/base_unittests.vcproj
@@ -296,6 +296,10 @@
>
</File>
<File
+ RelativePath="..\scoped_temp_dir_unittest.cc"
+ >
+ </File>
+ <File
RelativePath="..\sha2_unittest.cc"
>
</File>
diff --git a/base/scoped_temp_dir.cc b/base/scoped_temp_dir.cc
new file mode 100644
index 0000000..2eee63b
--- /dev/null
+++ b/base/scoped_temp_dir.cc
@@ -0,0 +1,48 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/scoped_temp_dir.h"
+
+#include "base/file_util.h"
+#include "base/logging.h"
+#include "base/string_util.h"
+
+ScopedTempDir::ScopedTempDir() {
+}
+
+ScopedTempDir::~ScopedTempDir() {
+ if (!path_.empty() && !file_util::Delete(path_, true))
+ LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value();
+}
+
+bool ScopedTempDir::CreateUniqueTempDir() {
+ // 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"),
+ &path_))
+ return false;
+
+ return true;
+}
+
+bool ScopedTempDir::Set(const FilePath& path) {
+ DCHECK(path_.empty());
+ if (!file_util::DirectoryExists(path) &&
+ !file_util::CreateDirectory(path)) {
+ return false;
+ }
+ path_ = path;
+ return true;
+}
+
+FilePath ScopedTempDir::Take() {
+ FilePath ret = path_;
+ path_ = FilePath();
+ return ret;
+}
+
+bool ScopedTempDir::IsValid() const {
+ return !path_.empty() && file_util::DirectoryExists(path_);
+}
+
diff --git a/base/scoped_temp_dir.h b/base/scoped_temp_dir.h
new file mode 100644
index 0000000..e9d45b9
--- /dev/null
+++ b/base/scoped_temp_dir.h
@@ -0,0 +1,47 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_SCOPED_TEMP_DIR_H_
+#define BASE_SCOPED_TEMP_DIR_H_
+
+// An object representing a temporary / scratch directory that should be cleaned
+// up (recursively) when this object goes out of scope. Note that since
+// 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.
+
+#include "base/file_path.h"
+
+class ScopedTempDir {
+ public:
+ // No directory is owned/created initially.
+ ScopedTempDir();
+
+ // Recursively delete path_
+ ~ScopedTempDir();
+
+ // Creates a unique directory in TempPath, and takes ownership of it.
+ // See file_util::CreateNewTemporaryDirectory.
+ bool CreateUniqueTempDir();
+
+ // Takes ownership of directory at |path|, creating it if necessary.
+ // Don't call multiple times unless Take() has been called first.
+ bool Set(const FilePath& path);
+
+ // Caller takes ownership of the temporary directory so it won't be destroyed
+ // when this object goes out of scope.
+ FilePath Take();
+
+ const FilePath& path() const { return path_; }
+
+ // Returns true if path_ is non-empty and exists.
+ bool IsValid() const;
+
+ private:
+ FilePath path_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedTempDir);
+};
+
+#endif // BASE_SCOPED_TEMP_DIR_H_
diff --git a/base/scoped_temp_dir_unittest.cc b/base/scoped_temp_dir_unittest.cc
new file mode 100644
index 0000000..903646d
--- /dev/null
+++ b/base/scoped_temp_dir_unittest.cc
@@ -0,0 +1,58 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/file_util.h"
+#include "base/scoped_temp_dir.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(ScopedTempDir, FullPath) {
+ FilePath test_path;
+ file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_temp_dir"),
+ &test_path);
+
+ // Against an existing dir, it should get destroyed when leaving scope.
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.Set(test_path));
+ EXPECT_TRUE(dir.IsValid());
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+
+ {
+ ScopedTempDir dir;
+ dir.Set(test_path);
+ // Now the dir doesn't exist, so ensure that it gets created.
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ // When we call Release(), it shouldn't get destroyed when leaving scope.
+ FilePath path = dir.Take();
+ EXPECT_EQ(path.value(), test_path.value());
+ EXPECT_FALSE(dir.IsValid());
+ }
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+
+ // Clean up.
+ {
+ ScopedTempDir dir;
+ dir.Set(test_path);
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+}
+
+TEST(ScopedTempDir, TempDir) {
+ // In this case, just verify that a directory was created and that it's a
+ // child of TempDir.
+ FilePath test_path;
+ {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ test_path = dir.path();
+ EXPECT_TRUE(file_util::DirectoryExists(test_path));
+ FilePath tmp_dir;
+ EXPECT_TRUE(file_util::GetTempDir(&tmp_dir));
+ EXPECT_TRUE(test_path.value().find(tmp_dir.value()) != std::string::npos);
+ }
+ EXPECT_FALSE(file_util::DirectoryExists(test_path));
+}
+