• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
9 // cleaned up (recursively) when this object goes out of scope.  Since deletion
10 // occurs during the destructor, no further error handling is possible if the
11 // directory fails to be deleted.  As a result, deletion is not guaranteed by
12 // this class.  (However note that, whenever possible, by default
13 // CreateUniqueTempDir creates the directory in a location that is
14 // automatically cleaned up on reboot, or at other appropriate times.)
15 //
16 // Multiple calls to the methods which establish a temporary directory
17 // (CreateUniqueTempDir, CreateUniqueTempDirUnderPath, and Set) must have
18 // intervening calls to Delete or Take, or the calls will fail.
19 
20 #include "base/files/file_path.h"
21 #include "base/macros.h"
22 
23 namespace base {
24 
25 class ScopedTempDir {
26  public:
27   // No directory is owned/created initially.
28   ScopedTempDir();
29 
30   // Recursively delete path.
31   ~ScopedTempDir();
32 
33   // Creates a unique directory in TempPath, and takes ownership of it.
34   // See file_util::CreateNewTemporaryDirectory.
35   bool CreateUniqueTempDir() WARN_UNUSED_RESULT;
36 
37   // Creates a unique directory under a given path, and takes ownership of it.
38   bool CreateUniqueTempDirUnderPath(const FilePath& path) WARN_UNUSED_RESULT;
39 
40   // Takes ownership of directory at |path|, creating it if necessary.
41   // Don't call multiple times unless Take() has been called first.
42   bool Set(const FilePath& path) WARN_UNUSED_RESULT;
43 
44   // Deletes the temporary directory wrapped by this object.
45   bool Delete() WARN_UNUSED_RESULT;
46 
47   // Caller takes ownership of the temporary directory so it won't be destroyed
48   // when this object goes out of scope.
49   FilePath Take();
50 
51   // Returns the path to the created directory. Call one of the
52   // CreateUniqueTempDir* methods before getting the path.
53   const FilePath& GetPath() const;
54 
55   // Returns true if path_ is non-empty and exists.
56   bool IsValid() const;
57 
58   // Returns the prefix used for temp directory names generated by
59   // ScopedTempDirs.
60   static const FilePath::CharType* GetTempDirPrefix();
61 
62  private:
63   FilePath path_;
64 
65   DISALLOW_COPY_AND_ASSIGN(ScopedTempDir);
66 };
67 
68 }  // namespace base
69 
70 #endif  // BASE_FILES_SCOPED_TEMP_DIR_H_
71