16d528ed9Sopenharmony_ci// Copyright (c) 2011 The Chromium Authors. All rights reserved. 26d528ed9Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be 36d528ed9Sopenharmony_ci// found in the LICENSE file. 46d528ed9Sopenharmony_ci 56d528ed9Sopenharmony_ci#ifndef BASE_FILES_SCOPED_TEMP_DIR_H_ 66d528ed9Sopenharmony_ci#define BASE_FILES_SCOPED_TEMP_DIR_H_ 76d528ed9Sopenharmony_ci 86d528ed9Sopenharmony_ci// An object representing a temporary / scratch directory that should be 96d528ed9Sopenharmony_ci// cleaned up (recursively) when this object goes out of scope. Since deletion 106d528ed9Sopenharmony_ci// occurs during the destructor, no further error handling is possible if the 116d528ed9Sopenharmony_ci// directory fails to be deleted. As a result, deletion is not guaranteed by 126d528ed9Sopenharmony_ci// this class. (However note that, whenever possible, by default 136d528ed9Sopenharmony_ci// CreateUniqueTempDir creates the directory in a location that is 146d528ed9Sopenharmony_ci// automatically cleaned up on reboot, or at other appropriate times.) 156d528ed9Sopenharmony_ci// 166d528ed9Sopenharmony_ci// Multiple calls to the methods which establish a temporary directory 176d528ed9Sopenharmony_ci// (CreateUniqueTempDir, CreateUniqueTempDirUnderPath, and Set) must have 186d528ed9Sopenharmony_ci// intervening calls to Delete or Take, or the calls will fail. 196d528ed9Sopenharmony_ci 206d528ed9Sopenharmony_ci#include "base/files/file_path.h" 216d528ed9Sopenharmony_ci 226d528ed9Sopenharmony_cinamespace base { 236d528ed9Sopenharmony_ci 246d528ed9Sopenharmony_ciclass ScopedTempDir { 256d528ed9Sopenharmony_ci public: 266d528ed9Sopenharmony_ci // No directory is owned/created initially. 276d528ed9Sopenharmony_ci ScopedTempDir(); 286d528ed9Sopenharmony_ci 296d528ed9Sopenharmony_ci // Recursively delete path. 306d528ed9Sopenharmony_ci ~ScopedTempDir(); 316d528ed9Sopenharmony_ci 326d528ed9Sopenharmony_ci // Creates a unique directory in TempPath, and takes ownership of it. 336d528ed9Sopenharmony_ci // See file_util::CreateNewTemporaryDirectory. 346d528ed9Sopenharmony_ci [[nodiscard]] bool CreateUniqueTempDir(); 356d528ed9Sopenharmony_ci 366d528ed9Sopenharmony_ci // Creates a unique directory under a given path, and takes ownership of it. 376d528ed9Sopenharmony_ci [[nodiscard]] bool CreateUniqueTempDirUnderPath(const FilePath& path); 386d528ed9Sopenharmony_ci 396d528ed9Sopenharmony_ci // Takes ownership of directory at |path|, creating it if necessary. 406d528ed9Sopenharmony_ci // Don't call multiple times unless Take() has been called first. 416d528ed9Sopenharmony_ci [[nodiscard]] bool Set(const FilePath& path); 426d528ed9Sopenharmony_ci 436d528ed9Sopenharmony_ci // Deletes the temporary directory wrapped by this object. 446d528ed9Sopenharmony_ci [[nodiscard]] bool Delete(); 456d528ed9Sopenharmony_ci 466d528ed9Sopenharmony_ci // Caller takes ownership of the temporary directory so it won't be destroyed 476d528ed9Sopenharmony_ci // when this object goes out of scope. 486d528ed9Sopenharmony_ci FilePath Take(); 496d528ed9Sopenharmony_ci 506d528ed9Sopenharmony_ci // Returns the path to the created directory. Call one of the 516d528ed9Sopenharmony_ci // CreateUniqueTempDir* methods before getting the path. 526d528ed9Sopenharmony_ci const FilePath& GetPath() const; 536d528ed9Sopenharmony_ci 546d528ed9Sopenharmony_ci // Returns true if path_ is non-empty and exists. 556d528ed9Sopenharmony_ci bool IsValid() const; 566d528ed9Sopenharmony_ci 576d528ed9Sopenharmony_ci // Returns the prefix used for temp directory names generated by 586d528ed9Sopenharmony_ci // ScopedTempDirs. 596d528ed9Sopenharmony_ci static const FilePath::CharType* GetTempDirPrefix(); 606d528ed9Sopenharmony_ci 616d528ed9Sopenharmony_ci private: 626d528ed9Sopenharmony_ci FilePath path_; 636d528ed9Sopenharmony_ci 646d528ed9Sopenharmony_ci ScopedTempDir(const ScopedTempDir&) = delete; 656d528ed9Sopenharmony_ci ScopedTempDir& operator=(const ScopedTempDir&) = delete; 666d528ed9Sopenharmony_ci}; 676d528ed9Sopenharmony_ci 686d528ed9Sopenharmony_ci} // namespace base 696d528ed9Sopenharmony_ci 706d528ed9Sopenharmony_ci#endif // BASE_FILES_SCOPED_TEMP_DIR_H_ 71