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.
5 #ifndef BASE_FILES_SCOPED_TEMP_DIR_H_
6 #define BASE_FILES_SCOPED_TEMP_DIR_H_
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.
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.
18 #include "base/base_export.h"
19 #include "base/files/file_path.h"
23 class BASE_EXPORT ScopedTempDir
{
25 // No directory is owned/created initially.
28 // Recursively delete path.
31 // Creates a unique directory in TempPath, and takes ownership of it.
32 // See file_util::CreateNewTemporaryDirectory.
33 bool CreateUniqueTempDir() WARN_UNUSED_RESULT
;
35 // Creates a unique directory under a given path, and takes ownership of it.
36 bool CreateUniqueTempDirUnderPath(const FilePath
& path
) WARN_UNUSED_RESULT
;
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
;
42 // Deletes the temporary directory wrapped by this object.
43 bool Delete() WARN_UNUSED_RESULT
;
45 // Caller takes ownership of the temporary directory so it won't be destroyed
46 // when this object goes out of scope.
49 const FilePath
& path() const { return path_
; }
51 // Returns true if path_ is non-empty and exists.
57 DISALLOW_COPY_AND_ASSIGN(ScopedTempDir
);
62 #endif // BASE_FILES_SCOPED_TEMP_DIR_H_