1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/scoped_temp_dir.h"
7 #include "base/file_util.h"
8 #include "base/logging.h"
10 ScopedTempDir::ScopedTempDir() {
13 ScopedTempDir::~ScopedTempDir() {
14 if (!path_
.empty() && !Delete())
15 DLOG(WARNING
) << "Could not delete temp dir in dtor.";
18 bool ScopedTempDir::CreateUniqueTempDir() {
22 // 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"),
31 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath
& base_path
) {
35 // If |base_path| does not exist, create it.
36 if (!file_util::CreateDirectory(base_path
))
39 // Create a new, uniquely named directory under |base_path|.
40 if (!file_util::CreateTemporaryDirInDir(
42 FILE_PATH_LITERAL("scoped_dir_"),
49 bool ScopedTempDir::Set(const FilePath
& path
) {
53 if (!file_util::DirectoryExists(path
) &&
54 !file_util::CreateDirectory(path
))
61 bool ScopedTempDir::Delete() {
65 bool ret
= file_util::Delete(path_
, true);
67 // We only clear the path if deleted the directory.
74 FilePath
ScopedTempDir::Take() {
80 bool ScopedTempDir::IsValid() const {
81 return !path_
.empty() && file_util::DirectoryExists(path_
);