1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/TmpPath.cc
19 #include "zypp/base/ReferenceCounted.h"
20 #include "zypp/base/NonCopyable.h"
21 #include "zypp/base/Logger.h"
22 #include "zypp/PathInfo.h"
23 #include "zypp/TmpPath.h"
28 namespace filesystem {
30 ///////////////////////////////////////////////////////////////////
32 // CLASS NAME : TmpPath::Impl
34 * Clean or delete a directory on destruction.
36 class TmpPath::Impl : public base::ReferenceCounted, private base::NonCopyable
46 CtorDefault = Autodelete
51 Impl( const Pathname & path_r, Flags flags_r = CtorDefault )
52 : _path( path_r ), _flags( flags_r )
57 if ( ! (_flags & Autodelete) || _path.empty() )
60 PathInfo p( _path, PathInfo::LSTAT );
67 if ( _flags & KeepTopdir )
68 res = clean_dir( _path );
70 res = recursive_rmdir( _path );
73 res = unlink( _path );
76 INT << "TmpPath cleanup error (" << res << ") " << p << endl;
78 DBG << "TmpPath cleaned up " << p << endl;
89 ///////////////////////////////////////////////////////////////////
91 ///////////////////////////////////////////////////////////////////
93 // CLASS NAME : TmpPath
95 ///////////////////////////////////////////////////////////////////
97 ///////////////////////////////////////////////////////////////////
99 // METHOD NAME : TmpPath::TmpPath
100 // METHOD TYPE : Constructor
103 :_impl( 0 ) // empty Pathname
107 ///////////////////////////////////////////////////////////////////
109 // METHOD NAME : TmpPath::TmpPath
110 // METHOD TYPE : Constructor
112 TmpPath::TmpPath( const Pathname & tmpPath_r )
113 :_impl( tmpPath_r.empty() ? 0 : new Impl( tmpPath_r ) )
117 ///////////////////////////////////////////////////////////////////
119 // METHOD NAME : TmpPath::~TmpPath
120 // METHOD TYPE : Destructor
124 // virtual not inlined dtor.
127 ///////////////////////////////////////////////////////////////////
129 // METHOD NAME : TmpPath::operator const void *const
132 TmpPath::operator const void *const() const
137 ///////////////////////////////////////////////////////////////////
139 // METHOD NAME : TmpPath::path
140 // METHOD TYPE : Pathname
143 TmpPath::path() const
145 return _impl.get() ? _impl->path() : Pathname();
148 ///////////////////////////////////////////////////////////////////
150 // METHOD NAME : TmpPath::defaultLocation
151 // METHOD TYPE : const Pathname &
154 TmpPath::defaultLocation()
156 static Pathname p( "/var/tmp" );
159 ///////////////////////////////////////////////////////////////////
161 // CLASS NAME : TmpFile
163 ///////////////////////////////////////////////////////////////////
166 ///////////////////////////////////////////////////////////////////
168 // METHOD NAME : TmpFile::TmpFile
169 // METHOD TYPE : Constructor
171 TmpFile::TmpFile( const Pathname & inParentDir_r,
172 const std::string & prefix_r )
174 // parent dir must exist
175 PathInfo p( inParentDir_r );
178 ERR << "Parent directory does not exist: " << p << endl;
182 // create the temp file
183 Pathname tmpPath = (inParentDir_r + prefix_r).extend( "XXXXXX");
184 char * buf = ::strdup( tmpPath.asString().c_str() );
187 ERR << "Out of memory" << endl;
191 int tmpFd = ::mkstemp( buf );
194 // success; create _impl
196 _impl = RW_pointer<Impl>( new Impl( buf ) );
199 ERR << "Cant create '" << buf << "' " << ::strerror( errno ) << endl;
204 ///////////////////////////////////////////////////////////////////
206 // METHOD NAME : TmpFile::defaultPrefix
207 // METHOD TYPE : const std::string &
210 TmpFile::defaultPrefix()
212 static string p( "TmpFile." );
216 ///////////////////////////////////////////////////////////////////
218 // CLASS NAME : TmpDir
220 ///////////////////////////////////////////////////////////////////
222 ///////////////////////////////////////////////////////////////////
224 // METHOD NAME : TmpDir::TmpDir
225 // METHOD TYPE : Constructor
227 TmpDir::TmpDir( const Pathname & inParentDir_r,
228 const std::string & prefix_r )
230 // parent dir must exist
231 PathInfo p( inParentDir_r );
234 ERR << "Parent directory does not exist: " << p << endl;
238 // create the temp dir
239 Pathname tmpPath = (inParentDir_r + prefix_r).extend( "XXXXXX");
240 char * buf = ::strdup( tmpPath.asString().c_str() );
243 ERR << "Out of memory" << endl;
247 char * tmp = ::mkdtemp( buf );
249 // success; create _impl
250 _impl = RW_pointer<Impl>( new Impl( tmp ) );
252 ERR << "Cant create '" << tmpPath << "' " << ::strerror( errno ) << endl;
257 ///////////////////////////////////////////////////////////////////
259 // METHOD NAME : TmpDir::defaultPrefix
260 // METHOD TYPE : const std::string &
263 TmpDir::defaultPrefix()
265 static string p( "TmpDir." );
269 } // namespace filesystem