[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 5 | #include "base/scoped_temp_dir.h" |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 6 | |
7 | #include "base/file_util.h" | ||||
8 | #include "base/logging.h" | ||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 9 | |
10 | ScopedTempDir::ScopedTempDir() { | ||||
11 | } | ||||
12 | |||||
13 | ScopedTempDir::~ScopedTempDir() { | ||||
[email protected] | f11c99a | 2010-11-29 17:49:32 | [diff] [blame] | 14 | if (!path_.empty() && !Delete()) |
[email protected] | a42d463 | 2011-10-26 21:48:00 | [diff] [blame] | 15 | DLOG(WARNING) << "Could not delete temp dir in dtor."; |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 16 | } |
17 | |||||
18 | bool ScopedTempDir::CreateUniqueTempDir() { | ||||
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 19 | if (!path_.empty()) |
20 | return false; | ||||
21 | |||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 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"), | ||||
25 | &path_)) | ||||
26 | return false; | ||||
27 | |||||
28 | return true; | ||||
29 | } | ||||
30 | |||||
[email protected] | bd781d89 | 2010-07-30 15:33:28 | [diff] [blame] | 31 | bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) { |
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 32 | if (!path_.empty()) |
33 | return false; | ||||
34 | |||||
35 | // If |base_path| does not exist, create it. | ||||
[email protected] | bd781d89 | 2010-07-30 15:33:28 | [diff] [blame] | 36 | if (!file_util::CreateDirectory(base_path)) |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 37 | return false; |
38 | |||||
[email protected] | 4cd2d520 | 2010-07-01 20:24:09 | [diff] [blame] | 39 | // Create a new, uniquely named directory under |base_path|. |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 40 | if (!file_util::CreateTemporaryDirInDir( |
41 | base_path, | ||||
42 | FILE_PATH_LITERAL("scoped_dir_"), | ||||
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 43 | &path_)) |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 44 | return false; |
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 45 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 46 | return true; |
47 | } | ||||
48 | |||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 49 | bool ScopedTempDir::Set(const FilePath& path) { |
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 50 | if (!path_.empty()) |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 51 | return false; |
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 52 | |
53 | if (!file_util::DirectoryExists(path) && | ||||
54 | !file_util::CreateDirectory(path)) | ||||
55 | return false; | ||||
56 | |||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 57 | path_ = path; |
58 | return true; | ||||
59 | } | ||||
60 | |||||
[email protected] | f11c99a | 2010-11-29 17:49:32 | [diff] [blame] | 61 | bool 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] | f11c99a | 2010-11-29 17:49:32 | [diff] [blame] | 69 | } |
70 | |||||
71 | return ret; | ||||
[email protected] | 53f4826c | 2010-08-27 01:29:28 | [diff] [blame] | 72 | } |
73 | |||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 74 | FilePath ScopedTempDir::Take() { |
75 | FilePath ret = path_; | ||||
76 | path_ = FilePath(); | ||||
77 | return ret; | ||||
78 | } | ||||
79 | |||||
80 | bool ScopedTempDir::IsValid() const { | ||||
81 | return !path_.empty() && file_util::DirectoryExists(path_); | ||||
82 | } |