| 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/command_line.h" | |
| 8 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 11 | 10 |
| 12 ScopedTempDir::ScopedTempDir() { | 11 ScopedTempDir::ScopedTempDir() { |
| 13 } | 12 } |
| 14 | 13 |
| 15 ScopedTempDir::~ScopedTempDir() { | 14 ScopedTempDir::~ScopedTempDir() { |
| 16 if (!path_.empty() && !file_util::Delete(path_, true)) | 15 if (!path_.empty() && !file_util::Delete(path_, true)) |
| 17 LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value(); | 16 LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value(); |
| 18 } | 17 } |
| 19 | 18 |
| 20 bool ScopedTempDir::CreateUniqueTempDir() { | 19 bool ScopedTempDir::CreateUniqueTempDir() { |
| 21 // 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 |
| 22 // for the unique name. | 21 // for the unique name. |
| 23 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), | 22 if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), |
| 24 &path_)) | 23 &path_)) |
| 25 return false; | 24 return false; |
| 26 | 25 |
| 27 return true; | 26 return true; |
| 28 } | 27 } |
| 29 | 28 |
| 30 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path, | 29 bool ScopedTempDir::CreateUniqueTempDirUnderPath(const FilePath& base_path) { |
| 31 bool loose_permissions) { | |
| 32 // If |path| does not exist, create it. | 30 // If |path| does not exist, create it. |
| 33 if (!file_util::CreateDirectory(base_path)) { | 31 if (!file_util::CreateDirectory(base_path)) |
| 34 LOG(ERROR) << "Failed to create base directory " << base_path.value(); | |
| 35 return false; | 32 return false; |
| 36 } | |
| 37 | 33 |
| 38 // Create a new, uniquely named directory under |base_path|. | 34 // Create a new, uniquly named directory under |base_path|. |
| 39 if (!file_util::CreateTemporaryDirInDir( | 35 if (!file_util::CreateTemporaryDirInDir( |
| 40 base_path, | 36 base_path, |
| 41 FILE_PATH_LITERAL("scoped_dir_"), | 37 FILE_PATH_LITERAL("scoped_dir_"), |
| 42 &path_, | 38 &path_)) { |
| 43 loose_permissions)) { | |
| 44 return false; | 39 return false; |
| 45 } | 40 } |
| 46 return true; | 41 return true; |
| 47 } | 42 } |
| 48 | 43 |
| 49 bool ScopedTempDir::Set(const FilePath& path) { | 44 bool ScopedTempDir::Set(const FilePath& path) { |
| 50 DCHECK(path_.empty()); | 45 DCHECK(path_.empty()); |
| 51 if (!file_util::DirectoryExists(path) && | 46 if (!file_util::DirectoryExists(path) && |
| 52 !file_util::CreateDirectory(path)) { | 47 !file_util::CreateDirectory(path)) { |
| 53 return false; | 48 return false; |
| 54 } | 49 } |
| 55 path_ = path; | 50 path_ = path; |
| 56 return true; | 51 return true; |
| 57 } | 52 } |
| 58 | 53 |
| 59 FilePath ScopedTempDir::Take() { | 54 FilePath ScopedTempDir::Take() { |
| 60 FilePath ret = path_; | 55 FilePath ret = path_; |
| 61 path_ = FilePath(); | 56 path_ = FilePath(); |
| 62 return ret; | 57 return ret; |
| 63 } | 58 } |
| 64 | 59 |
| 65 bool ScopedTempDir::IsValid() const { | 60 bool ScopedTempDir::IsValid() const { |
| 66 return !path_.empty() && file_util::DirectoryExists(path_); | 61 return !path_.empty() && file_util::DirectoryExists(path_); |
| 67 } | 62 } |
| OLD | NEW |