Imported Upstream version 16.0.3
[platform/upstream/libzypp.git] / zypp / TmpPath.h
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/TmpPath.h
10  *
11 */
12 #ifndef ZYPP_TMPPATH_H
13 #define ZYPP_TMPPATH_H
14
15 #include <iosfwd>
16
17 #include "zypp/Pathname.h"
18 #include "zypp/base/PtrTypes.h"
19
20 namespace zypp {
21   namespace filesystem {
22
23     ///////////////////////////////////////////////////////////////////
24     //
25     //  CLASS NAME : TmpPath
26     /**
27      * @short Automaticaly deletes files or directories when no longer needed.
28      *
29      * TmpPath is constructed from a Pathname. Multiple TmpPath instances
30      * created by copy and assign, share the same reference counted internal
31      * repesentation.
32      *
33      * When the last reference drops any file or directory located at the path
34      * passed to the ctor is deleted (recursivly in case of directories). This
35      * behavior can be canged by calling \ref autoCleanup.
36      *
37      * Principally serves as base class, but standalone usable.
38      **/
39     class TmpPath
40     {
41       public:
42         /**
43          * Default Ctor. An empty Pathname.
44          **/
45         TmpPath();
46
47         /**
48          * Ctor. Takes a Pathname.
49          **/
50         explicit
51         TmpPath( const Pathname & tmpPath_r );
52
53         /**
54          * Dtor.
55          **/
56         virtual
57         ~TmpPath();
58
59         /**
60          * Test whether the Pathname is valid (i.e. not empty. NOT whether
61          * it really denotes an existing file or directory).
62          **/
63         explicit operator bool() const;
64
65         /**
66          * @return The Pathname.
67          **/
68         Pathname
69         path() const;
70
71         /**
72          * Type conversion to Pathname.
73          **/
74         operator Pathname() const
75         { return path(); }
76
77         /**
78          * Whether path is valid and deleted when the last reference drops.
79          */
80         bool autoCleanup() const;
81
82         /**
83          * Turn \ref autoCleanup on/off if path is valid.
84          */
85         void autoCleanup( bool yesno_r );
86
87       public:
88         /**
89          * @return The default directory where temporary
90          * files should be are created (/var/tmp).
91          **/
92         static const Pathname &
93         defaultLocation();
94
95       protected:
96         class Impl;
97         RW_pointer<Impl> _impl;
98
99     };
100     ///////////////////////////////////////////////////////////////////
101
102     /**
103      * Stream output as pathname.
104      **/
105     inline std::ostream &
106     operator<<( std::ostream & str, const TmpPath & obj )
107     { return str << static_cast<Pathname>(obj); }
108
109     ///////////////////////////////////////////////////////////////////
110
111     ///////////////////////////////////////////////////////////////////
112     //
113     //  CLASS NAME : TmpFile
114     /**
115      * @short Provide a new empty temporary file and delete it when no
116      * longer needed.
117      *
118      * The temporary file is per default created in '/var/tmp' and named
119      * 'TmpFile.XXXXXX', with XXXXXX replaced by a string which makes the
120      * name unique. Different location and file prefix may be passed to
121      * the ctor. TmpFile is created with mode 0600.
122      *
123      * TmpFile provides the Pathname of the temporary file, or an empty
124      * path in case of any error.
125      **/
126     class TmpFile : public TmpPath
127     {
128       public:
129         /**
130          * Ctor. Takes a Pathname.
131          **/
132         explicit
133         TmpFile( const Pathname & inParentDir_r = defaultLocation(),
134                  const std::string & prefix_r = defaultPrefix() );
135
136         /** Provide a new empty temporary directory as sibling.
137          * \code
138          *   TmpFile s = makeSibling( "/var/lib/myfile" );
139          *   // returns: /var/lib/myfile.XXXXXX
140          * \endcode
141          * If \c sibling_r exists, sibling is created using the same mode.
142          */
143         static TmpFile makeSibling( const Pathname & sibling_r );
144
145       public:
146         /**
147          * @return The default prefix for temporary files (TmpFile.)
148          **/
149         static const std::string &
150         defaultPrefix();
151
152     };
153     ///////////////////////////////////////////////////////////////////
154
155     ///////////////////////////////////////////////////////////////////
156     //
157     //  CLASS NAME : TmpDir
158     /**
159      * @short Provide a new empty temporary directory and recursively
160      * delete it when no longer needed.
161      *
162      * The temporary directory is per default created in '/var/tmp' and
163      * named 'TmpDir.XXXXXX', with XXXXXX replaced by a string which makes
164      * the  name unique. Different location and file prefix may be passed
165      * to the ctor. TmpDir is created with mode 0700.
166      *
167      * TmpDir provides the Pathname of the temporary directory , or an empty
168      * path in case of any error.
169      **/
170     class TmpDir : public TmpPath
171     {
172       public:
173         /**
174          * Ctor. Takes a Pathname.
175          **/
176         explicit
177         TmpDir( const Pathname & inParentDir_r = defaultLocation(),
178                 const std::string & prefix_r = defaultPrefix() );
179
180         /** Provide a new empty temporary directory as sibling.
181          * \code
182          *   TmpDir s = makeSibling( "/var/lib/mydir" );
183          *   // returns: /var/lib/mydir.XXXXXX
184          * \endcode
185          * If \c sibling_r exists, sibling is created using the same mode.
186          */
187         static TmpDir makeSibling( const Pathname & sibling_r );
188
189       public:
190         /**
191          * @return The default prefix for temporary directories (TmpDir.)
192          **/
193         static const std::string &
194         defaultPrefix();
195     };
196     ///////////////////////////////////////////////////////////////////
197
198   } // namespace filesystem
199 } // namespace zypp
200
201 #endif // ZYPP_TMPPATH_H