summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/scoped_temp_dir.cc18
-rw-r--r--base/scoped_temp_dir.h2
-rw-r--r--base/scoped_temp_dir_unittest.cc23
3 files changed, 37 insertions, 6 deletions
diff --git a/base/scoped_temp_dir.cc b/base/scoped_temp_dir.cc
index 5cd13b4..000ed0a 100644
--- a/base/scoped_temp_dir.cc
+++ b/base/scoped_temp_dir.cc
@@ -11,7 +11,8 @@ ScopedTempDir::ScopedTempDir() {
}
ScopedTempDir::~ScopedTempDir() {
- Delete();
+ if (!path_.empty() && !Delete())
+ LOG(WARNING) << "Could not delete temp dir in dtor.";
}
bool ScopedTempDir::CreateUniqueTempDir() {
@@ -57,10 +58,19 @@ bool ScopedTempDir::Set(const FilePath& path) {
return true;
}
-void ScopedTempDir::Delete() {
- if (!path_.empty() && !file_util::Delete(path_, true))
+bool ScopedTempDir::Delete() {
+ if (path_.empty())
+ return false;
+
+ bool ret = file_util::Delete(path_, true);
+ if (ret) {
+ // We only clear the path if deleted the directory.
+ path_.clear();
+ } else {
LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value();
- path_.clear();
+ }
+
+ return ret;
}
FilePath ScopedTempDir::Take() {
diff --git a/base/scoped_temp_dir.h b/base/scoped_temp_dir.h
index 6845562..f44bcca8 100644
--- a/base/scoped_temp_dir.h
+++ b/base/scoped_temp_dir.h
@@ -38,7 +38,7 @@ class ScopedTempDir {
bool Set(const FilePath& path);
// Deletes the temporary directory wrapped by this object.
- void Delete();
+ bool Delete();
// Caller takes ownership of the temporary directory so it won't be destroyed
// when this object goes out of scope.
diff --git a/base/scoped_temp_dir_unittest.cc b/base/scoped_temp_dir_unittest.cc
index cf5fed3..039a1ed 100644
--- a/base/scoped_temp_dir_unittest.cc
+++ b/base/scoped_temp_dir_unittest.cc
@@ -3,6 +3,7 @@
// found in the LICENSE file.
#include "base/file_util.h"
+#include "base/platform_file.h"
#include "base/scoped_temp_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
@@ -78,7 +79,7 @@ TEST(ScopedTempDir, MultipleInvocations) {
ScopedTempDir dir;
EXPECT_TRUE(dir.CreateUniqueTempDir());
EXPECT_FALSE(dir.CreateUniqueTempDir());
- dir.Delete();
+ EXPECT_TRUE(dir.Delete());
EXPECT_TRUE(dir.CreateUniqueTempDir());
EXPECT_FALSE(dir.CreateUniqueTempDir());
ScopedTempDir other_dir;
@@ -87,3 +88,23 @@ TEST(ScopedTempDir, MultipleInvocations) {
EXPECT_FALSE(dir.CreateUniqueTempDir());
EXPECT_FALSE(other_dir.CreateUniqueTempDir());
}
+
+#if defined(OS_WIN)
+TEST(ScopedTempDir, LockedTempDir) {
+ ScopedTempDir dir;
+ EXPECT_TRUE(dir.CreateUniqueTempDir());
+ int file_flags = base::PLATFORM_FILE_CREATE_ALWAYS |
+ base::PLATFORM_FILE_WRITE;
+ base::PlatformFileError error_code = base::PLATFORM_FILE_OK;
+ FilePath file_path(dir.path().Append(FILE_PATH_LITERAL("temp")));
+ base::PlatformFile file = base::CreatePlatformFile(file_path, file_flags,
+ NULL, &error_code);
+ EXPECT_NE(base::kInvalidPlatformFileValue, file);
+ EXPECT_EQ(base::PLATFORM_FILE_OK, error_code);
+ EXPECT_FALSE(dir.Delete()); // We should not be able to delete.
+ EXPECT_FALSE(dir.path().empty()); // We should still have a valid path.
+ EXPECT_TRUE(base::ClosePlatformFile(file));
+ // Now, we should be able to delete.
+ EXPECT_TRUE(dir.Delete());
+}
+#endif // defined(OS_WIN)