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 3 // found in the LICENSE file. 4 5 #ifndef BASE_FILES_SCOPED_TEMP_DIR_H_ 6 #define BASE_FILES_SCOPED_TEMP_DIR_H_ 7 8 // An object representing a temporary / scratch directory that should be cleaned 9 // up (recursively) when this object goes out of scope. Note that since 10 // deletion occurs during the destructor, no further error handling is possible 11 // if the directory fails to be deleted. As a result, deletion is not 12 // guaranteed by this class. 13 // 14 // Multiple calls to the methods which establish a temporary directory 15 // (CreateUniqueTempDir, CreateUniqueTempDirUnderPath, and Set) must have 16 // intervening calls to Delete or Take, or the calls will fail. 17 18 #include "base/base_export.h" 19 #include "base/files/file_path.h" 20 21 namespace base { 22 23 class BASE_EXPORT ScopedTempDir { 24 public: 25 // No directory is owned/created initially. 26 ScopedTempDir(); 27 28 // Recursively delete path. 29 ~ScopedTempDir(); 30 31 // Creates a unique directory in TempPath, and takes ownership of it. 32 // See file_util::CreateNewTemporaryDirectory. 33 bool CreateUniqueTempDir() WARN_UNUSED_RESULT; 34 35 // Creates a unique directory under a given path, and takes ownership of it. 36 bool CreateUniqueTempDirUnderPath(const FilePath& path) WARN_UNUSED_RESULT; 37 38 // Takes ownership of directory at |path|, creating it if necessary. 39 // Don't call multiple times unless Take() has been called first. 40 bool Set(const FilePath& path) WARN_UNUSED_RESULT; 41 42 // Deletes the temporary directory wrapped by this object. 43 bool Delete() WARN_UNUSED_RESULT; 44 45 // Caller takes ownership of the temporary directory so it won't be destroyed 46 // when this object goes out of scope. 47 FilePath Take(); 48 49 const FilePath& path() const { return path_; } 50 51 // Returns true if path_ is non-empty and exists. 52 bool IsValid() const; 53 54 private: 55 FilePath path_; 56 57 DISALLOW_COPY_AND_ASSIGN(ScopedTempDir); 58 }; 59 60 } // namespace base 61 62 #endif // BASE_FILES_SCOPED_TEMP_DIR_H_ 63