[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 5 | #ifndef BASE_SCOPED_TEMP_DIR_H_ |
6 | #define BASE_SCOPED_TEMP_DIR_H_ | ||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 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. | ||||
[email protected] | 9fefee68 | 2010-10-23 10:06:47 | [diff] [blame] | 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. | ||||
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 17 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 18 | #include "base/base_export.h" |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 19 | #include "base/file_path.h" |
20 | |||||
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 21 | class BASE_EXPORT ScopedTempDir { |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 22 | public: |
23 | // No directory is owned/created initially. | ||||
24 | ScopedTempDir(); | ||||
25 | |||||
[email protected] | 53f4826c | 2010-08-27 01:29:28 | [diff] [blame] | 26 | // Recursively delete path. |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 27 | ~ScopedTempDir(); |
28 | |||||
29 | // Creates a unique directory in TempPath, and takes ownership of it. | ||||
30 | // See file_util::CreateNewTemporaryDirectory. | ||||
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 31 | bool CreateUniqueTempDir() WARN_UNUSED_RESULT; |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 32 | |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 33 | // Creates a unique directory under a given path, and takes ownership of it. |
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 34 | bool CreateUniqueTempDirUnderPath(const FilePath& path) WARN_UNUSED_RESULT; |
[email protected] | b0b3abd9 | 2010-04-30 17:00:09 | [diff] [blame] | 35 | |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 36 | // Takes ownership of directory at |path|, creating it if necessary. |
37 | // Don't call multiple times unless Take() has been called first. | ||||
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 38 | bool Set(const FilePath& path) WARN_UNUSED_RESULT; |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 39 | |
[email protected] | 53f4826c | 2010-08-27 01:29:28 | [diff] [blame] | 40 | // Deletes the temporary directory wrapped by this object. |
[email protected] | 2d57f5d | 2011-01-13 14:20:12 | [diff] [blame] | 41 | bool Delete() WARN_UNUSED_RESULT; |
[email protected] | 53f4826c | 2010-08-27 01:29:28 | [diff] [blame] | 42 | |
[email protected] | 4a25111 | 2009-01-28 20:49:35 | [diff] [blame] | 43 | // Caller takes ownership of the temporary directory so it won't be destroyed |
44 | // when this object goes out of scope. | ||||
45 | FilePath Take(); | ||||
46 | |||||
47 | const FilePath& path() const { return path_; } | ||||
48 | |||||
49 | // Returns true if path_ is non-empty and exists. | ||||
50 | bool IsValid() const; | ||||
51 | |||||
52 | private: | ||||
53 | FilePath path_; | ||||
54 | |||||
55 | DISALLOW_COPY_AND_ASSIGN(ScopedTempDir); | ||||
56 | }; | ||||
57 | |||||
[email protected] | e078590 | 2011-05-19 23:34:17 | [diff] [blame] | 58 | #endif // BASE_SCOPED_TEMP_DIR_H_ |