| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 | 9 |
| 10 namespace base { | 10 namespace base { |
| 11 | 11 |
| 12 namespace { |
| 13 |
| 14 constexpr FilePath::CharType kScopedDirPrefix[] = |
| 15 FILE_PATH_LITERAL("scoped_dir"); |
| 16 |
| 17 } // namespace |
| 18 |
| 12 ScopedTempDir::ScopedTempDir() { | 19 ScopedTempDir::ScopedTempDir() { |
| 13 } | 20 } |
| 14 | 21 |
| 15 ScopedTempDir::~ScopedTempDir() { | 22 ScopedTempDir::~ScopedTempDir() { |
| 16 if (!path_.empty() && !Delete()) | 23 if (!path_.empty() && !Delete()) |
| 17 DLOG(WARNING) << "Could not delete temp dir in dtor."; | 24 DLOG(WARNING) << "Could not delete temp dir in dtor."; |
| 18 } | 25 } |
| 19 | 26 |
| 20 bool ScopedTempDir::CreateUniqueTempDir() { | 27 bool ScopedTempDir::CreateUniqueTempDir() { |
| 21 if (!path_.empty()) | 28 if (!path_.empty()) |
| 22 return false; | 29 return false; |
| 23 | 30 |
| 24 // This "scoped_dir" prefix is only used on Windows and serves as a template | 31 // This "scoped_dir" prefix is only used on Windows and serves as a template |
| 25 // for the unique name. | 32 // for the unique name. |
| 26 if (!base::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), &path_)) | 33 if (!base::CreateNewTempDirectory(kScopedDirPrefix, &path_)) |
| 27 return false; | 34 return false; |
| 28 | 35 |
| 29 return true; | 36 return true; |
| 30 } | 37 } |
| 31 | 38 |
| 32 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) { | 39 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) { |
| 33 if (!path_.empty()) | 40 if (!path_.empty()) |
| 34 return false; | 41 return false; |
| 35 | 42 |
| 36 // If |base_path| does not exist, create it. | 43 // If |base_path| does not exist, create it. |
| 37 if (!base::CreateDirectory(base_path)) | 44 if (!base::CreateDirectory(base_path)) |
| 38 return false; | 45 return false; |
| 39 | 46 |
| 40 // Create a new, uniquely named directory under |base_path|. | 47 // Create a new, uniquely named directory under |base_path|. |
| 41 if (!base::CreateTemporaryDirInDir(base_path, | 48 if (!base::CreateTemporaryDirInDir(base_path, kScopedDirPrefix, &path_)) |
| 42 FILE_PATH_LITERAL("scoped_dir_"), | |
| 43 &path_)) | |
| 44 return false; | 49 return false; |
| 45 | 50 |
| 46 return true; | 51 return true; |
| 47 } | 52 } |
| 48 | 53 |
| 49 bool ScopedTempDir::Set(const FilePath& path) { | 54 bool ScopedTempDir::Set(const FilePath& path) { |
| 50 if (!path_.empty()) | 55 if (!path_.empty()) |
| 51 return false; | 56 return false; |
| 52 | 57 |
| 53 if (!DirectoryExists(path) && !base::CreateDirectory(path)) | 58 if (!DirectoryExists(path) && !base::CreateDirectory(path)) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 78 | 83 |
| 79 const FilePath& ScopedTempDir::GetPath() const { | 84 const FilePath& ScopedTempDir::GetPath() const { |
| 80 DCHECK(!path_.empty()) << "Did you call CreateUniqueTempDir* before?"; | 85 DCHECK(!path_.empty()) << "Did you call CreateUniqueTempDir* before?"; |
| 81 return path_; | 86 return path_; |
| 82 } | 87 } |
| 83 | 88 |
| 84 bool ScopedTempDir::IsValid() const { | 89 bool ScopedTempDir::IsValid() const { |
| 85 return !path_.empty() && DirectoryExists(path_); | 90 return !path_.empty() && DirectoryExists(path_); |
| 86 } | 91 } |
| 87 | 92 |
| 93 // static |
| 94 const FilePath::CharType* ScopedTempDir::GetTempDirPrefix() { |
| 95 return kScopedDirPrefix; |
| 96 } |
| 97 |
| 88 } // namespace base | 98 } // namespace base |
| OLD | NEW |