OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/scoped_temp_dir.h" | 5 #include "base/scoped_temp_dir.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 | 10 |
11 ScopedTempDir::ScopedTempDir() { | 11 ScopedTempDir::ScopedTempDir() { |
12 } | 12 } |
13 | 13 |
14 ScopedTempDir::~ScopedTempDir() { | 14 ScopedTempDir::~ScopedTempDir() { |
15 if (!path_.empty() && !file_util::Delete(path_, true)) | 15 if (!path_.empty() && !file_util::Delete(path_, true)) |
16 LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value(); | 16 LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value(); |
17 } | 17 } |
18 | 18 |
19 bool ScopedTempDir::CreateUniqueTempDir() { | 19 bool ScopedTempDir::CreateUniqueTempDir() { |
20 // This "scoped_dir" prefix is only used on Windows and serves as a template | 20 // This "scoped_dir" prefix is only used on Windows and serves as a template |
21 // for the unique name. | 21 // for the unique name. |
22 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), | 22 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), |
23 &path_)) | 23 &path_)) |
24 return false; | 24 return false; |
25 | 25 |
26 return true; | 26 return true; |
27 } | 27 } |
28 | 28 |
| 29 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) { |
| 30 // If |path| does not exist, create it. |
| 31 if (!file_util::CreateDirectory(base_path)) |
| 32 return false; |
| 33 |
| 34 // Create a new, uniquly named directory under |base_path|. |
| 35 if (!file_util::CreateTemporaryDirInDir( |
| 36 base_path, |
| 37 FILE_PATH_LITERAL("scoped_dir_"), |
| 38 &path_)) { |
| 39 return false; |
| 40 } |
| 41 return true; |
| 42 } |
| 43 |
29 bool ScopedTempDir::Set(const FilePath& path) { | 44 bool ScopedTempDir::Set(const FilePath& path) { |
30 DCHECK(path_.empty()); | 45 DCHECK(path_.empty()); |
31 if (!file_util::DirectoryExists(path) && | 46 if (!file_util::DirectoryExists(path) && |
32 !file_util::CreateDirectory(path)) { | 47 !file_util::CreateDirectory(path)) { |
33 return false; | 48 return false; |
34 } | 49 } |
35 path_ = path; | 50 path_ = path; |
36 return true; | 51 return true; |
37 } | 52 } |
38 | 53 |
39 FilePath ScopedTempDir::Take() { | 54 FilePath ScopedTempDir::Take() { |
40 FilePath ret = path_; | 55 FilePath ret = path_; |
41 path_ = FilePath(); | 56 path_ = FilePath(); |
42 return ret; | 57 return ret; |
43 } | 58 } |
44 | 59 |
45 bool ScopedTempDir::IsValid() const { | 60 bool ScopedTempDir::IsValid() const { |
46 return !path_.empty() && file_util::DirectoryExists(path_); | 61 return !path_.empty() && file_util::DirectoryExists(path_); |
47 } | 62 } |
OLD | NEW |