1 // Copyright (c) 2009 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() && !file_util::Delete(path_
, true))
15 LOG(ERROR
) << "ScopedTempDir unable to delete " << path_
.value();
18 bool ScopedTempDir::CreateUniqueTempDir() {
19 // This "scoped_dir" prefix is only used on Windows and serves as a template
20 // for the unique name.
21 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"),
28 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath
& base_path
) {
29 // If |path| does not exist, create it.
30 if (!file_util::CreateDirectory(base_path
))
33 // Create a new, uniquely named directory under |base_path|.
34 if (!file_util::CreateTemporaryDirInDir(
36 FILE_PATH_LITERAL("scoped_dir_"),
43 bool ScopedTempDir::Set(const FilePath
& path
) {
44 DCHECK(path_
.empty());
45 if (!file_util::DirectoryExists(path
) &&
46 !file_util::CreateDirectory(path
)) {
53 FilePath
ScopedTempDir::Take() {
59 bool ScopedTempDir::IsValid() const {
60 return !path_
.empty() && file_util::DirectoryExists(path_
);