1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/TmpPath.h
12 #ifndef ZYPP_TMPPATH_H
13 #define ZYPP_TMPPATH_H
17 #include "zypp/Pathname.h"
18 #include "zypp/base/PtrTypes.h"
21 namespace filesystem {
23 ///////////////////////////////////////////////////////////////////
25 // CLASS NAME : TmpPath
27 * @short Automaticaly deletes files or directories when no longer needed.
29 * TmpPath is constructed from a Pathname. Multiple TmpPath instances
30 * created by copy and assign, share the same reference counted internal
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.
37 * Principally serves as base class, but standalone usable.
43 * Default Ctor. An empty Pathname.
48 * Ctor. Takes a Pathname.
51 TmpPath( const Pathname & tmpPath_r );
60 * Test whether the Pathname is valid (i.e. not empty. NOT whether
61 * it really denotes an existing file or directory).
63 explicit operator bool() const;
66 * @return The Pathname.
72 * Type conversion to Pathname.
74 operator Pathname() const
78 * Whether path is valid and deleted when the last reference drops.
80 bool autoCleanup() const;
83 * Turn \ref autoCleanup on/off if path is valid.
85 void autoCleanup( bool yesno_r );
89 * @return The default directory where temporary
90 * files should be are created (/var/tmp).
92 static const Pathname &
97 RW_pointer<Impl> _impl;
100 ///////////////////////////////////////////////////////////////////
103 * Stream output as pathname.
105 inline std::ostream &
106 operator<<( std::ostream & str, const TmpPath & obj )
107 { return str << static_cast<Pathname>(obj); }
109 ///////////////////////////////////////////////////////////////////
111 ///////////////////////////////////////////////////////////////////
113 // CLASS NAME : TmpFile
115 * @short Provide a new empty temporary file and delete it when no
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.
123 * TmpFile provides the Pathname of the temporary file, or an empty
124 * path in case of any error.
126 class TmpFile : public TmpPath
130 * Ctor. Takes a Pathname.
133 TmpFile( const Pathname & inParentDir_r = defaultLocation(),
134 const std::string & prefix_r = defaultPrefix() );
136 /** Provide a new empty temporary directory as sibling.
138 * TmpFile s = makeSibling( "/var/lib/myfile" );
139 * // returns: /var/lib/myfile.XXXXXX
141 * If \c sibling_r exists, sibling is created using the same mode.
143 static TmpFile makeSibling( const Pathname & sibling_r );
147 * @return The default prefix for temporary files (TmpFile.)
149 static const std::string &
153 ///////////////////////////////////////////////////////////////////
155 ///////////////////////////////////////////////////////////////////
157 // CLASS NAME : TmpDir
159 * @short Provide a new empty temporary directory and recursively
160 * delete it when no longer needed.
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.
167 * TmpDir provides the Pathname of the temporary directory , or an empty
168 * path in case of any error.
170 class TmpDir : public TmpPath
174 * Ctor. Takes a Pathname.
177 TmpDir( const Pathname & inParentDir_r = defaultLocation(),
178 const std::string & prefix_r = defaultPrefix() );
180 /** Provide a new empty temporary directory as sibling.
182 * TmpDir s = makeSibling( "/var/lib/mydir" );
183 * // returns: /var/lib/mydir.XXXXXX
185 * If \c sibling_r exists, sibling is created using the same mode.
187 static TmpDir makeSibling( const Pathname & sibling_r );
191 * @return The default prefix for temporary directories (TmpDir.)
193 static const std::string &
196 ///////////////////////////////////////////////////////////////////
198 } // namespace filesystem
201 #endif // ZYPP_TMPPATH_H