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