michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/scoped_temp_dir.h" michael@0: michael@0: #include "base/file_util.h" michael@0: #include "base/logging.h" michael@0: #include "base/string_util.h" michael@0: michael@0: ScopedTempDir::ScopedTempDir() { michael@0: } michael@0: michael@0: ScopedTempDir::~ScopedTempDir() { michael@0: if (!path_.empty() && !file_util::Delete(path_, true)) michael@0: CHROMIUM_LOG(ERROR) << "ScopedTempDir unable to delete " << path_.value(); michael@0: } michael@0: michael@0: bool ScopedTempDir::CreateUniqueTempDir() { michael@0: // This "scoped_dir" prefix is only used on Windows and serves as a template michael@0: // for the unique name. michael@0: if (!file_util::CreateNewTempDirectory(FILE_PATH_LITERAL("scoped_dir"), michael@0: &path_)) michael@0: return false; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool ScopedTempDir::Set(const FilePath& path) { michael@0: DCHECK(path_.empty()); michael@0: if (!file_util::DirectoryExists(path) && michael@0: !file_util::CreateDirectory(path)) { michael@0: return false; michael@0: } michael@0: path_ = path; michael@0: return true; michael@0: } michael@0: michael@0: FilePath ScopedTempDir::Take() { michael@0: FilePath ret = path_; michael@0: path_ = FilePath(); michael@0: return ret; michael@0: } michael@0: michael@0: bool ScopedTempDir::IsValid() const { michael@0: return !path_.empty() && file_util::DirectoryExists(path_); michael@0: }