scoped_temp_dir.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef BASE_FILES_SCOPED_TEMP_DIR_H_
  5. #define BASE_FILES_SCOPED_TEMP_DIR_H_
  6. // An object representing a temporary / scratch directory that should be
  7. // cleaned up (recursively) when this object goes out of scope. Since deletion
  8. // occurs during the destructor, no further error handling is possible if the
  9. // directory fails to be deleted. As a result, deletion is not guaranteed by
  10. // this class. (However note that, whenever possible, by default
  11. // CreateUniqueTempDir creates the directory in a location that is
  12. // automatically cleaned up on reboot, or at other appropriate times.)
  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. #include "base/base_export.h"
  18. #include "base/compiler_specific.h"
  19. #include "base/files/file_path.h"
  20. namespace base {
  21. class BASE_EXPORT ScopedTempDir {
  22. public:
  23. // No directory is owned/created initially.
  24. ScopedTempDir();
  25. ScopedTempDir(ScopedTempDir&&) noexcept;
  26. ScopedTempDir& operator=(ScopedTempDir&&);
  27. // Recursively delete path.
  28. ~ScopedTempDir();
  29. // Creates a unique directory in TempPath, and takes ownership of it.
  30. // See file_util::CreateNewTemporaryDirectory.
  31. bool CreateUniqueTempDir() WARN_UNUSED_RESULT;
  32. // Creates a unique directory under a given path, and takes ownership of it.
  33. bool CreateUniqueTempDirUnderPath(const FilePath& path) WARN_UNUSED_RESULT;
  34. // Takes ownership of directory at |path|, creating it if necessary.
  35. // Don't call multiple times unless Take() has been called first.
  36. bool Set(const FilePath& path) WARN_UNUSED_RESULT;
  37. // Deletes the temporary directory wrapped by this object.
  38. bool Delete() WARN_UNUSED_RESULT;
  39. // Caller takes ownership of the temporary directory so it won't be destroyed
  40. // when this object goes out of scope.
  41. FilePath Take();
  42. // Returns the path to the created directory. Call one of the
  43. // CreateUniqueTempDir* methods before getting the path.
  44. const FilePath& GetPath() const;
  45. // Returns true if path_ is non-empty and exists.
  46. bool IsValid() const;
  47. // Returns the prefix used for temp directory names generated by
  48. // ScopedTempDirs.
  49. static const FilePath::CharType* GetTempDirPrefix();
  50. private:
  51. FilePath path_;
  52. };
  53. } // namespace base
  54. #endif // BASE_FILES_SCOPED_TEMP_DIR_H_