summaryrefslogtreecommitdiffstats
path: root/base/files/scoped_temp_dir.cc
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-16 20:34:23 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-16 20:34:23 +0000
commitea1a3f60aa4527939af680eda25c7697901f643c (patch)
tree6835e11842b4ff04ec277a5f961cbf1853b95bae /base/files/scoped_temp_dir.cc
parenta628d191e68c0d1d308e1a20a5a4d7bf7884c4c5 (diff)
downloadchromium_src-ea1a3f60aa4527939af680eda25c7697901f643c.zip
chromium_src-ea1a3f60aa4527939af680eda25c7697901f643c.tar.gz
chromium_src-ea1a3f60aa4527939af680eda25c7697901f643c.tar.bz2
Move scoped_temp_dir from base to base/files
Also add to base namespace. BUG= Review URL: https://codereview.chromium.org/11359217 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@168281 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/files/scoped_temp_dir.cc')
-rw-r--r--base/files/scoped_temp_dir.cc86
1 files changed, 86 insertions, 0 deletions
diff --git a/base/files/scoped_temp_dir.cc b/base/files/scoped_temp_dir.cc
new file mode 100644
index 0000000..509f808
--- /dev/null
+++ b/base/files/scoped_temp_dir.cc
@@ -0,0 +1,86 @@
+// 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/files/scoped_temp_dir.h"
+
+#include "base/file_util.h"
+#include "base/logging.h"
+
+namespace base {
+
+ScopedTempDir::ScopedTempDir() {
+}
+
+ScopedTempDir::~ScopedTempDir() {
+ if (!path_.empty() && !Delete())
+ DLOG(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();
+ }
+
+ return ret;
+}
+
+FilePath ScopedTempDir::Take() {
+ FilePath ret = path_;
+ path_ = FilePath();
+ return ret;
+}
+
+bool ScopedTempDir::IsValid() const {
+ return !path_.empty() && file_util::DirectoryExists(path_);
+}
+
+} // namespace base