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