1*4f7b9bd3SJayanth Othayoth #pragma once
2*4f7b9bd3SJayanth Othayoth 
3*4f7b9bd3SJayanth Othayoth #include <filesystem>
4*4f7b9bd3SJayanth Othayoth #include <utility>
5*4f7b9bd3SJayanth Othayoth 
6*4f7b9bd3SJayanth Othayoth namespace openpower::util
7*4f7b9bd3SJayanth Othayoth {
8*4f7b9bd3SJayanth Othayoth 
9*4f7b9bd3SJayanth Othayoth namespace fs = std::filesystem;
10*4f7b9bd3SJayanth Othayoth 
11*4f7b9bd3SJayanth Othayoth /**
12*4f7b9bd3SJayanth Othayoth  * @class TemporaryFile
13*4f7b9bd3SJayanth Othayoth  *
14*4f7b9bd3SJayanth Othayoth  * A temporary file in the file system.
15*4f7b9bd3SJayanth Othayoth  *
16*4f7b9bd3SJayanth Othayoth  * The temporary file is created by the constructor.  The absolute path to the
17*4f7b9bd3SJayanth Othayoth  * file can be obtained using getPath().
18*4f7b9bd3SJayanth Othayoth  *
19*4f7b9bd3SJayanth Othayoth  * The temporary file can be deleted by calling remove().  Otherwise the file
20*4f7b9bd3SJayanth Othayoth  * will be deleted by the destructor.
21*4f7b9bd3SJayanth Othayoth  *
22*4f7b9bd3SJayanth Othayoth  */
23*4f7b9bd3SJayanth Othayoth class TemporaryFile
24*4f7b9bd3SJayanth Othayoth {
25*4f7b9bd3SJayanth Othayoth   public:
26*4f7b9bd3SJayanth Othayoth     // Specify which compiler-generated methods we want
27*4f7b9bd3SJayanth Othayoth     TemporaryFile(const TemporaryFile&) = delete;
28*4f7b9bd3SJayanth Othayoth     TemporaryFile(TemporaryFile&&) = delete;
29*4f7b9bd3SJayanth Othayoth     TemporaryFile& operator=(const TemporaryFile&) = delete;
30*4f7b9bd3SJayanth Othayoth 
31*4f7b9bd3SJayanth Othayoth     /**
32*4f7b9bd3SJayanth Othayoth      * Constructor.
33*4f7b9bd3SJayanth Othayoth      *
34*4f7b9bd3SJayanth Othayoth      * Creates a temporary file in the temporary directory (normally /tmp).
35*4f7b9bd3SJayanth Othayoth      *
36*4f7b9bd3SJayanth Othayoth      * Throws an exception if the file cannot be created.
37*4f7b9bd3SJayanth Othayoth      */
38*4f7b9bd3SJayanth Othayoth     TemporaryFile();
39*4f7b9bd3SJayanth Othayoth 
40*4f7b9bd3SJayanth Othayoth     /**
41*4f7b9bd3SJayanth Othayoth      * Destructor.
42*4f7b9bd3SJayanth Othayoth      *
43*4f7b9bd3SJayanth Othayoth      * Deletes the temporary file if necessary.
44*4f7b9bd3SJayanth Othayoth      */
~TemporaryFile()45*4f7b9bd3SJayanth Othayoth     ~TemporaryFile()
46*4f7b9bd3SJayanth Othayoth     {
47*4f7b9bd3SJayanth Othayoth         try
48*4f7b9bd3SJayanth Othayoth         {
49*4f7b9bd3SJayanth Othayoth             remove();
50*4f7b9bd3SJayanth Othayoth         }
51*4f7b9bd3SJayanth Othayoth         catch (...)
52*4f7b9bd3SJayanth Othayoth         {
53*4f7b9bd3SJayanth Othayoth             // Destructors should not throw exceptions
54*4f7b9bd3SJayanth Othayoth         }
55*4f7b9bd3SJayanth Othayoth     }
56*4f7b9bd3SJayanth Othayoth 
57*4f7b9bd3SJayanth Othayoth     /**
58*4f7b9bd3SJayanth Othayoth      * Deletes the temporary file.
59*4f7b9bd3SJayanth Othayoth      *
60*4f7b9bd3SJayanth Othayoth      * Does nothing if the file has already been deleted.
61*4f7b9bd3SJayanth Othayoth      *
62*4f7b9bd3SJayanth Othayoth      * Log error message if an error occurs during the deletion.
63*4f7b9bd3SJayanth Othayoth      */
64*4f7b9bd3SJayanth Othayoth     void remove();
65*4f7b9bd3SJayanth Othayoth 
66*4f7b9bd3SJayanth Othayoth     /**
67*4f7b9bd3SJayanth Othayoth      * Returns the absolute path to the temporary file.
68*4f7b9bd3SJayanth Othayoth      *
69*4f7b9bd3SJayanth Othayoth      * Returns an empty path if the file has been deleted.
70*4f7b9bd3SJayanth Othayoth      *
71*4f7b9bd3SJayanth Othayoth      * @return temporary file path
72*4f7b9bd3SJayanth Othayoth      */
getPath() const73*4f7b9bd3SJayanth Othayoth     const fs::path& getPath() const
74*4f7b9bd3SJayanth Othayoth     {
75*4f7b9bd3SJayanth Othayoth         return path;
76*4f7b9bd3SJayanth Othayoth     }
77*4f7b9bd3SJayanth Othayoth 
78*4f7b9bd3SJayanth Othayoth   private:
79*4f7b9bd3SJayanth Othayoth     /**
80*4f7b9bd3SJayanth Othayoth      * Absolute path to the temporary file.
81*4f7b9bd3SJayanth Othayoth      *
82*4f7b9bd3SJayanth Othayoth      * Empty when file has been deleted.
83*4f7b9bd3SJayanth Othayoth      */
84*4f7b9bd3SJayanth Othayoth     fs::path path{};
85*4f7b9bd3SJayanth Othayoth };
86*4f7b9bd3SJayanth Othayoth 
87*4f7b9bd3SJayanth Othayoth } // namespace openpower::util
88