- fixed docu
[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).
35      *
36      * Principally serves as base class, but standalone usable.
37      **/
38     class TmpPath
39     {
40       public:
41         /**
42          * Default Ctor. An empty Pathname.
43          **/
44         TmpPath();
45
46         /**
47          * Ctor. Takes a Pathname.
48          **/
49         explicit
50         TmpPath( const Pathname & tmpPath_r );
51
52         /**
53          * Dtor.
54          **/
55         virtual
56         ~TmpPath();
57
58         /**
59          * Test whether the Pathname is valid (i.e. not empty. NOT whether
60          * it realy denotes an existing file or directory).
61          **/
62         operator const void *const() const;
63
64         /**
65          * @return The Pathname.
66          **/
67         Pathname
68         path() const;
69
70         /**
71          * Type conversion to Pathname.
72          **/
73         operator Pathname() const
74         { return path(); }
75
76       public:
77         /**
78          * @return The default directory where temporary
79          * files should be are created (/var/tmp).
80          **/
81         static const Pathname &
82         defaultLocation();
83
84       protected:
85         class Impl;
86         RW_pointer<Impl> _impl;
87
88     };
89     ///////////////////////////////////////////////////////////////////
90
91     /**
92      * Stream output as pathname.
93      **/
94     inline std::ostream &
95     operator<<( std::ostream & str, const TmpPath & obj )
96     { return str << static_cast<Pathname>(obj); }
97
98     ///////////////////////////////////////////////////////////////////
99
100     ///////////////////////////////////////////////////////////////////
101     //
102     //  CLASS NAME : TmpFile
103     /**
104      * @short Provide a new empty temporary file and delete it when no
105      * longer needed.
106      *
107      * The temporary file is per default created in '/var/tmp' and named
108      * 'TmpFile.XXXXXX', with XXXXXX replaced by a string which makes the
109      * name unique. Different location and file prefix may be passed to
110      * the ctor. TmpFile is created with mode 0600.
111      *
112      * The directory where the temporary file is to be created must exist.
113      * TmpFile provides the Pathname of the temporary file, or an empty
114      * path in case of any error.
115      **/
116     class TmpFile : public TmpPath
117     {
118       public:
119         /**
120          * Ctor. Takes a Pathname.
121          **/
122         explicit
123         TmpFile( const Pathname & inParentDir_r = defaultLocation(),
124                  const std::string & prefix_r = defaultPrefix() );
125
126         /** Provide a new empty temporary directory as sibling.
127          * \code
128          *   TmpFile s = makeSibling( "/var/lib/myfile" );
129          *   // returns: /var/lib/myfile.XXXXXX
130          * \endcode
131          * If \c sibling_r exists, sibling is created using the same mode.
132          */
133         static TmpFile makeSibling( const Pathname & sibling_r );
134
135       public:
136         /**
137          * @return The default prefix for temporary files (TmpFile.)
138          **/
139         static const std::string &
140         defaultPrefix();
141
142     };
143     ///////////////////////////////////////////////////////////////////
144
145     ///////////////////////////////////////////////////////////////////
146     //
147     //  CLASS NAME : TmpDir
148     /**
149      * @short Provide a new empty temporary directory and recursively
150      * delete it when no longer needed.
151      *
152      * The temporary directory is per default created in '/var/tmp' and
153      * named 'TmpDir.XXXXXX', with XXXXXX replaced by a string which makes
154      * the  name unique. Different location and file prefix may be passed
155      * to the ctor. TmpDir is created with mode 0700.
156      *
157      * The directory where the temporary directory is to be created must exist.
158      * TmpDir provides the Pathname of the temporary directory , or an empty
159      * path in case of any error.
160      **/
161     class TmpDir : public TmpPath
162     {
163       public:
164         /**
165          * Ctor. Takes a Pathname.
166          **/
167         explicit
168         TmpDir( const Pathname & inParentDir_r = defaultLocation(),
169                 const std::string & prefix_r = defaultPrefix() );
170
171         /** Provide a new empty temporary directory as sibling.
172          * \code
173          *   TmpDir s = makeSibling( "/var/lib/mydir" );
174          *   // returns: /var/lib/mydir.XXXXXX
175          * \endcode
176          * If \c sibling_r exists, sibling is created using the same mode.
177          */
178         static TmpDir makeSibling( const Pathname & sibling_r );
179
180       public:
181         /**
182          * @return The default prefix for temporary directories (TmpDir.)
183          **/
184         static const std::string &
185         defaultPrefix();
186     };
187     ///////////////////////////////////////////////////////////////////
188
189   } // namespace filesystem
190 } // namespace zypp
191
192 #endif // ZYPP_TMPPATH_H