blob: 9f4bcf40c6db07f5174675087b51916c09246f52 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]4a251112009-01-28 20:49:352// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e0785902011-05-19 23:34:175#include "base/scoped_temp_dir.h"
[email protected]4a251112009-01-28 20:49:356
7#include "base/file_util.h"
8#include "base/logging.h"
[email protected]4a251112009-01-28 20:49:359
10ScopedTempDir::ScopedTempDir() {
11}
12
13ScopedTempDir::~ScopedTempDir() {
[email protected]f11c99a2010-11-29 17:49:3214 if (!path_.empty() && !Delete())
[email protected]a42d4632011-10-26 21:48:0015 DLOG(WARNING) << "Could not delete temp dir in dtor.";
[email protected]4a251112009-01-28 20:49:3516}
17
18bool ScopedTempDir::CreateUniqueTempDir() {
[email protected]9fefee682010-10-23 10:06:4719 if (!path_.empty())
20 return false;
21
[email protected]4a251112009-01-28 20:49:3522 // This "scoped_dir" prefix is only used on Windows and serves as a template
23 // for the unique name.
24 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"),
25 &path_))
26 return false;
27
28 return true;
29}
30
[email protected]bd781d892010-07-30 15:33:2831bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) {
[email protected]9fefee682010-10-23 10:06:4732 if (!path_.empty())
33 return false;
34
35 // If |base_path| does not exist, create it.
[email protected]bd781d892010-07-30 15:33:2836 if (!file_util::CreateDirectory(base_path))
[email protected]b0b3abd92010-04-30 17:00:0937 return false;
38
[email protected]4cd2d5202010-07-01 20:24:0939 // Create a new, uniquely named directory under |base_path|.
[email protected]b0b3abd92010-04-30 17:00:0940 if (!file_util::CreateTemporaryDirInDir(
41 base_path,
42 FILE_PATH_LITERAL("scoped_dir_"),
[email protected]9fefee682010-10-23 10:06:4743 &path_))
[email protected]b0b3abd92010-04-30 17:00:0944 return false;
[email protected]9fefee682010-10-23 10:06:4745
[email protected]b0b3abd92010-04-30 17:00:0946 return true;
47}
48
[email protected]4a251112009-01-28 20:49:3549bool ScopedTempDir::Set(const FilePath& path) {
[email protected]9fefee682010-10-23 10:06:4750 if (!path_.empty())
[email protected]4a251112009-01-28 20:49:3551 return false;
[email protected]9fefee682010-10-23 10:06:4752
53 if (!file_util::DirectoryExists(path) &&
54 !file_util::CreateDirectory(path))
55 return false;
56
[email protected]4a251112009-01-28 20:49:3557 path_ = path;
58 return true;
59}
60
[email protected]f11c99a2010-11-29 17:49:3261bool ScopedTempDir::Delete() {
62 if (path_.empty())
63 return false;
64
65 bool ret = file_util::Delete(path_, true);
66 if (ret) {
67 // We only clear the path if deleted the directory.
68 path_.clear();
[email protected]f11c99a2010-11-29 17:49:3269 }
70
71 return ret;
[email protected]53f4826c2010-08-27 01:29:2872}
73
[email protected]4a251112009-01-28 20:49:3574FilePath ScopedTempDir::Take() {
75 FilePath ret = path_;
76 path_ = FilePath();
77 return ret;
78}
79
80bool ScopedTempDir::IsValid() const {
81 return !path_.empty() && file_util::DirectoryExists(path_);
82}