summaryrefslogtreecommitdiffstats
path: root/base/scoped_temp_dir.cc
diff options
context:
space:
mode:
authorthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-19 23:34:17 +0000
committerthakis@chromium.org <thakis@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-05-19 23:34:17 +0000
commite078590661444968a2c49a3e5464413714471dca (patch)
tree13a4dbe4dedccc8057361bd86c89e065390247fc /base/scoped_temp_dir.cc
parentd9f76627b14170b95639619089139b6f0493ae8c (diff)
downloadchromium_src-e078590661444968a2c49a3e5464413714471dca.zip
chromium_src-e078590661444968a2c49a3e5464413714471dca.tar.gz
chromium_src-e078590661444968a2c49a3e5464413714471dca.tar.bz2
Move scoped_temp_dir and scoped_native_library back from base/memory to base.
It looks like they got moved accidentally in http://codereview.chromium.org/6714032 BUG=none TEST=none Review URL: http://codereview.chromium.org/7048007 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@86010 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/scoped_temp_dir.cc')
-rw-r--r--base/scoped_temp_dir.cc84
1 files changed, 84 insertions, 0 deletions
diff --git a/base/scoped_temp_dir.cc b/base/scoped_temp_dir.cc
new file mode 100644
index 0000000..fc886e5a
--- /dev/null
+++ b/base/scoped_temp_dir.cc
@@ -0,0 +1,84 @@
+// Copyright (c) 2011 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"
+
+ScopedTempDir::ScopedTempDir() {
+}
+
+ScopedTempDir::~ScopedTempDir() {
+ if (!path_.empty() && !Delete())
+ LOG(WARNING) << "Could not delete temp dir in dtor.";
+}
+
+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"),
+ &path_))
+ return false;
+
+ return true;
+}
+
+bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) {
+ if (!path_.empty())
+ return false;
+
+ // If |base_path| does not exist, create it.
+ if (!file_util::CreateDirectory(base_path))
+ return false;
+
+ // Create a new, uniquely named directory under |base_path|.
+ if (!file_util::CreateTemporaryDirInDir(
+ base_path,
+ FILE_PATH_LITERAL("scoped_dir_"),
+ &path_))
+ return false;
+
+ return true;
+}
+
+bool ScopedTempDir::Set(const FilePath& path) {
+ if (!path_.empty())
+ return false;
+
+ if (!file_util::DirectoryExists(path) &&
+ !file_util::CreateDirectory(path))
+ return false;
+
+ path_ = path;
+ return 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();
+ }
+
+ return ret;
+}
+
+FilePath ScopedTempDir::Take() {
+ FilePath ret = path_;
+ path_ = FilePath();
+ return ret;
+}
+
+bool ScopedTempDir::IsValid() const {
+ return !path_.empty() && file_util::DirectoryExists(path_);
+}