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 #include "base/macros.h" 21 22 namespace base { 23 24 class BASE_EXPORT ScopedTempDir { 25 public: 26 // No directory is owned/created initially. 27 ScopedTempDir(); 28 29 // Recursively delete path. 30 ~ScopedTempDir(); 31 32 // Creates a unique directory in TempPath, and takes ownership of it. 33 // See file_util::CreateNewTemporaryDirectory. 34 bool CreateUniqueTempDir() WARN_UNUSED_RESULT; 35 36 // Creates a unique directory under a given path, and takes ownership of it. 37 bool CreateUniqueTempDirUnderPath(const FilePath& path) WARN_UNUSED_RESULT; 38 39 // Takes ownership of directory at |path|, creating it if necessary. 40 // Don't call multiple times unless Take() has been called first. 41 bool Set(const FilePath& path) WARN_UNUSED_RESULT; 42 43 // Deletes the temporary directory wrapped by this object. 44 bool Delete() WARN_UNUSED_RESULT; 45 46 // Caller takes ownership of the temporary directory so it won't be destroyed 47 // when this object goes out of scope. 48 FilePath Take(); 49 path()50 const FilePath& path() const { return path_; } 51 52 // Returns true if path_ is non-empty and exists. 53 bool IsValid() const; 54 55 private: 56 FilePath path_; 57 58 DISALLOW_COPY_AND_ASSIGN(ScopedTempDir); 59 }; 60 61 } // namespace base 62 63 #endif // BASE_FILES_SCOPED_TEMP_DIR_H_ 64