libzypp 17.32.2
TmpPath.cc
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\---------------------------------------------------------------------*/
13#include <cstdlib>
14#include <cstring>
15#include <cerrno>
16
17#include <iostream>
18#include <utility>
19
20#include <zypp-core/base/ReferenceCounted.h>
21#include <zypp-core/base/NonCopyable.h>
22#include <zypp-core/base/Logger.h>
23#include <zypp-core/fs/PathInfo.h>
24#include <zypp-core/fs/TmpPath.h>
25
26using std::endl;
27
28namespace zypp {
29 namespace filesystem {
30
32 //
33 // CLASS NAME : TmpPath::Impl
38 {
39 public:
40
41 enum Flags
42 {
43 NoOp = 0,
44 Autodelete = 1L << 0,
45 KeepTopdir = 1L << 1,
46 //
48 };
49
50 public:
53
54 Impl(const Impl &) = delete;
55 Impl(Impl &&) = delete;
56 Impl &operator=(const Impl &) = delete;
57 Impl &operator=(Impl &&) = delete;
58
59 ~Impl() override
60 {
61 if ( ! (_flags & Autodelete) || _path.empty() )
62 return;
63
65 if ( ! p.isExist() )
66 return;
67
68 int res = 0;
69 if ( p.isDir() )
70 {
71 if ( _flags & KeepTopdir )
72 res = clean_dir( _path );
73 else
75 }
76 else
77 res = unlink( _path );
78
79 if ( res )
80 INT << "TmpPath cleanup error (" << res << ") " << p << endl;
81 else
82 DBG << "TmpPath cleaned up " << p << endl;
83 }
84
85 const Pathname &
86 path() const
87 { return _path; }
88
89 bool autoCleanup() const
90 { return( _flags & Autodelete ); }
91
92 void autoCleanup( bool yesno_r )
94
95 private:
98 };
100
102 //
103 // CLASS NAME : TmpPath
104 //
106
108 //
109 // METHOD NAME : TmpPath::TmpPath
110 // METHOD TYPE : Constructor
111 //
114
116 //
117 // METHOD NAME : TmpPath::TmpPath
118 // METHOD TYPE : Constructor
119 //
121 :_impl( tmpPath_r.empty() ? nullptr : new Impl( std::move(tmpPath_r) ) )
122 {}
123
125 //
126 // METHOD NAME : TmpPath::~TmpPath
127 // METHOD TYPE : Destructor
128 //
130 {
131 // virtual not inlined dtor.
132 }
133
135 //
136 // METHOD NAME : TmpPath::operator const void *
137 // METHOD TYPE :
138 //
139 TmpPath::operator bool() const
140 {
141 return _impl.get();
142 }
143
145 //
146 // METHOD NAME : TmpPath::path
147 // METHOD TYPE : Pathname
148 //
151 {
152 return _impl.get() ? _impl->path() : Pathname();
153 }
154
156 //
157 // METHOD NAME : TmpPath::defaultLocation
158 // METHOD TYPE : const Pathname &
159 //
160 const Pathname &
162 {
163 static Pathname p( getenv("ZYPPTMPDIR") ? getenv("ZYPPTMPDIR") : "/var/tmp" );
164 return p;
165 }
166
168 { return _impl.get() ? _impl->autoCleanup() : false; }
169
171 { if ( _impl.get() ) _impl->autoCleanup( yesno_r ); }
172
174 //
175 // CLASS NAME : TmpFile
176 //
178
179
181 //
182 // METHOD NAME : TmpFile::TmpFile
183 // METHOD TYPE : Constructor
184 //
186 const std::string & prefix_r )
187 {
188 // parent dir must exist
190 {
191 ERR << "Parent directory '" << inParentDir_r << "' can't be created." << endl;
192 return;
193 }
194
195 // create the temp file
196 Pathname tmpPath = (inParentDir_r + prefix_r).extend( "XXXXXX");
197 char * buf = ::strdup( tmpPath.asString().c_str() );
198 if ( ! buf )
199 {
200 ERR << "Out of memory" << endl;
201 return;
202 }
203
204 int tmpFd = ::mkostemp( buf, O_CLOEXEC );
205 if ( tmpFd != -1 )
206 {
207 // success; create _impl
208 ::close( tmpFd );
209 _impl = RW_pointer<Impl>( new Impl( buf ) );
210 }
211 else
212 ERR << "Cant create '" << buf << "' " << ::strerror( errno ) << endl;
213
214 ::free( buf );
215 }
216
218 //
219 // METHOD NAME : TmpFile::makeSibling
220 // METHOD TYPE : TmpFile
221 //
223 {
224 TmpFile ret( sibling_r.dirname(), sibling_r.basename() );
225 // clone mode if sibling_r exists
227 if ( p.isFile() )
228 {
229 ::chmod( ret.path().c_str(), p.st_mode() );
230 }
231 return ret;
232 }
233
235 {
238 tmpFile.autoCleanup(false); //cleaned up by ManagedFile
239 return mFile;
240 }
241
243 //
244 // METHOD NAME : TmpFile::defaultPrefix
245 // METHOD TYPE : const std::string &
246 //
247 const std::string &
249 {
250 static std::string p( "TmpFile." );
251 return p;
252 }
253
255 //
256 // CLASS NAME : TmpDir
257 //
259
261 //
262 // METHOD NAME : TmpDir::TmpDir
263 // METHOD TYPE : Constructor
264 //
266 const std::string & prefix_r )
267 {
268 // parent dir must exist
270 {
271 ERR << "Parent directory '" << inParentDir_r << "' can't be created." << endl;
272 return;
273 }
274
275 // create the temp dir
276 Pathname tmpPath = (inParentDir_r + prefix_r).extend( "XXXXXX");
277 char * buf = ::strdup( tmpPath.asString().c_str() );
278 if ( ! buf )
279 {
280 ERR << "Out of memory" << endl;
281 return;
282 }
283
284 char * tmp = ::mkdtemp( buf );
285 if ( tmp )
286 // success; create _impl
287 _impl = RW_pointer<Impl>( new Impl( tmp ) );
288 else
289 ERR << "Cant create '" << tmpPath << "' " << ::strerror( errno ) << endl;
290
291 ::free( buf );
292 }
293
295 //
296 // METHOD NAME : TmpDir::makeSibling
297 // METHOD TYPE : TmpDir
298 //
300 {
301 TmpDir ret( sibling_r.dirname(), sibling_r.basename() );
302 // clone mode if sibling_r exists
304 if ( p.isDir() )
305 {
306 ::chmod( ret.path().c_str(), p.st_mode() );
307 }
308 return ret;
309 }
310
312 //
313 // METHOD NAME : TmpDir::defaultPrefix
314 // METHOD TYPE : const std::string &
315 //
316 const std::string &
318 {
319 static std::string p( "TmpDir." );
320 return p;
321 }
322
323 } // namespace filesystem
324} // namespace zypp
Reference counted access to a Tp object calling a custom Dispose function when the last AutoDispose h...
Definition AutoDispose.h:95
Base class for reference counted objects.
Wrapper class for stat/lstat.
Definition PathInfo.h:222
const std::string & asString() const
String representation.
Definition Pathname.h:91
bool empty() const
Test for an empty path.
Definition Pathname.h:114
Provide a new empty temporary directory and recursively delete it when no longer needed.
Definition TmpPath.h:178
static const std::string & defaultPrefix()
Definition TmpPath.cc:317
TmpDir(const Pathname &inParentDir_r=defaultLocation(), const std::string &prefix_r=defaultPrefix())
Ctor.
Definition TmpPath.cc:265
static TmpDir makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition TmpPath.cc:299
Provide a new empty temporary file and delete it when no longer needed.
Definition TmpPath.h:128
TmpFile(const Pathname &inParentDir_r=defaultLocation(), const std::string &prefix_r=defaultPrefix())
Ctor.
Definition TmpPath.cc:185
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
Definition TmpPath.cc:222
static ManagedFile asManagedFile()
Create a temporary file and convert it to a automatically cleaned up ManagedFile.
Definition TmpPath.cc:234
static const std::string & defaultPrefix()
Definition TmpPath.cc:248
Clean or delete a directory on destruction.
Definition TmpPath.cc:38
Impl & operator=(const Impl &)=delete
void autoCleanup(bool yesno_r)
Definition TmpPath.cc:92
Impl(Pathname &&path_r, Flags flags_r=CtorDefault)
Definition TmpPath.cc:51
Impl & operator=(Impl &&)=delete
Impl(const Impl &)=delete
const Pathname & path() const
Definition TmpPath.cc:86
static const Pathname & defaultLocation()
Definition TmpPath.cc:161
RW_pointer< Impl > _impl
Definition TmpPath.h:98
virtual ~TmpPath()
Dtor.
Definition TmpPath.cc:129
Pathname path() const
Definition TmpPath.cc:150
TmpPath()
Default Ctor.
Definition TmpPath.cc:112
bool autoCleanup() const
Whether path is valid and deleted when the last reference drops.
Definition TmpPath.cc:167
Definition Arch.h:364
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Definition NonCopyable.h:26
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Definition PathInfo.cc:1093
int unlink(const Pathname &path)
Like 'unlink'.
Definition PathInfo.cc:701
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Definition PathInfo.cc:413
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Definition PathInfo.cc:320
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
Definition PathInfo.cc:443
Easy-to use interface to the ZYPP dependency resolver.
#define nullptr
Definition Easy.h:55
#define DBG
Definition Logger.h:95
#define ERR
Definition Logger.h:98
#define INT
Definition Logger.h:100