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/files/scoped_temp_dir.h"
7 #include "base/files/file_util.h"
8 #include "base/logging.h"
12 ScopedTempDir::ScopedTempDir() {
15 ScopedTempDir::~ScopedTempDir() {
16 if (!path_
.empty() && !Delete())
17 DLOG(WARNING
) << "Could not delete temp dir in dtor.";
20 bool ScopedTempDir::CreateUniqueTempDir() {
24 // This "scoped_dir" prefix is only used on Windows and serves as a template
25 // for the unique name.
26 if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), &path_
))
32 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath
& base_path
) {
36 // If |base_path| does not exist, create it.
37 if (!base::CreateDirectory(base_path
))
40 // Create a new, uniquely named directory under |base_path|.
41 if (!base::CreateTemporaryDirInDir(base_path
,
42 FILE_PATH_LITERAL("scoped_dir_"),
49 bool ScopedTempDir::Set(const FilePath
& path
) {
53 if (!DirectoryExists(path
) && !base::CreateDirectory(path
))
60 bool ScopedTempDir::Delete() {
64 bool ret
= base::DeleteFile(path_
, true);
66 // We only clear the path if deleted the directory.
73 FilePath
ScopedTempDir::Take() {
79 bool ScopedTempDir::IsValid() const {
80 return !path_
.empty() && DirectoryExists(path_
);