/
TmpPath.cc
312 lines (270 loc) · 8.32 KB
/
TmpPath.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*---------------------------------------------------------------------\
| ____ _ __ __ ___ |
| |__ / \ / / . \ . \ |
| / / \ V /| _/ _/ |
| / /__ | | | | | | |
| /_____||_| |_| |_| |
| |
\---------------------------------------------------------------------*/
/** \file zypp/TmpPath.cc
*
*/
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <iostream>
#include "zypp/base/ReferenceCounted.h"
#include "zypp/base/NonCopyable.h"
#include "zypp/base/Logger.h"
#include "zypp/PathInfo.h"
#include "zypp/TmpPath.h"
using namespace std;
namespace zypp {
namespace filesystem {
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : TmpPath::Impl
/**
* Clean or delete a directory on destruction.
**/
class TmpPath::Impl : public base::ReferenceCounted, private base::NonCopyable
{
public:
enum Flags
{
NoOp = 0,
Autodelete = 1L << 0,
KeepTopdir = 1L << 1,
//
CtorDefault = Autodelete
};
public:
Impl( const Pathname & path_r, Flags flags_r = CtorDefault )
: _path( path_r ), _flags( flags_r )
{}
~Impl()
{
if ( ! (_flags & Autodelete) || _path.empty() )
return;
PathInfo p( _path, PathInfo::LSTAT );
if ( ! p.isExist() )
return;
int res = 0;
if ( p.isDir() )
{
if ( _flags & KeepTopdir )
res = clean_dir( _path );
else
res = recursive_rmdir( _path );
}
else
res = unlink( _path );
if ( res )
INT << "TmpPath cleanup error (" << res << ") " << p << endl;
else
DBG << "TmpPath cleaned up " << p << endl;
}
const Pathname &
path() const
{ return _path; }
bool autoCleanup() const
{ return( _flags & Autodelete ); }
void autoCleanup( bool yesno_r )
{ _flags = yesno_r ? CtorDefault : NoOp; }
private:
Pathname _path;
Flags _flags;
};
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : TmpPath
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpPath::TmpPath
// METHOD TYPE : Constructor
//
TmpPath::TmpPath()
{}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpPath::TmpPath
// METHOD TYPE : Constructor
//
TmpPath::TmpPath( const Pathname & tmpPath_r )
:_impl( tmpPath_r.empty() ? nullptr : new Impl( tmpPath_r ) )
{}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpPath::~TmpPath
// METHOD TYPE : Destructor
//
TmpPath::~TmpPath()
{
// virtual not inlined dtor.
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpPath::operator const void *
// METHOD TYPE :
//
TmpPath::operator bool() const
{
return _impl.get();
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpPath::path
// METHOD TYPE : Pathname
//
Pathname
TmpPath::path() const
{
return _impl.get() ? _impl->path() : Pathname();
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpPath::defaultLocation
// METHOD TYPE : const Pathname &
//
const Pathname &
TmpPath::defaultLocation()
{
static Pathname p( getenv("ZYPPTMPDIR") ? getenv("ZYPPTMPDIR") : "/var/tmp" );
return p;
}
bool TmpPath::autoCleanup() const
{ return _impl.get() ? _impl->autoCleanup() : false; }
void TmpPath::autoCleanup( bool yesno_r )
{ if ( _impl.get() ) _impl->autoCleanup( yesno_r ); }
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : TmpFile
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpFile::TmpFile
// METHOD TYPE : Constructor
//
TmpFile::TmpFile( const Pathname & inParentDir_r,
const std::string & prefix_r )
{
// parent dir must exist
if ( filesystem::assert_dir( inParentDir_r ) != 0 )
{
ERR << "Parent directory '" << inParentDir_r << "' can't be created." << endl;
return;
}
// create the temp file
Pathname tmpPath = (inParentDir_r + prefix_r).extend( "XXXXXX");
char * buf = ::strdup( tmpPath.asString().c_str() );
if ( ! buf )
{
ERR << "Out of memory" << endl;
return;
}
int tmpFd = ::mkostemp( buf, O_CLOEXEC );
if ( tmpFd != -1 )
{
// success; create _impl
::close( tmpFd );
_impl = RW_pointer<Impl>( new Impl( buf ) );
}
else
ERR << "Cant create '" << buf << "' " << ::strerror( errno ) << endl;
::free( buf );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpFile::makeSibling
// METHOD TYPE : TmpFile
//
TmpFile TmpFile::makeSibling( const Pathname & sibling_r )
{
TmpFile ret( sibling_r.dirname(), sibling_r.basename() );
// clone mode if sibling_r exists
PathInfo p( sibling_r );
if ( p.isFile() )
{
::chmod( ret.path().c_str(), p.st_mode() );
}
return ret;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpFile::defaultPrefix
// METHOD TYPE : const std::string &
//
const std::string &
TmpFile::defaultPrefix()
{
static string p( "TmpFile." );
return p;
}
///////////////////////////////////////////////////////////////////
//
// CLASS NAME : TmpDir
//
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpDir::TmpDir
// METHOD TYPE : Constructor
//
TmpDir::TmpDir( const Pathname & inParentDir_r,
const std::string & prefix_r )
{
// parent dir must exist
if ( filesystem::assert_dir( inParentDir_r ) != 0 )
{
ERR << "Parent directory '" << inParentDir_r << "' can't be created." << endl;
return;
}
// create the temp dir
Pathname tmpPath = (inParentDir_r + prefix_r).extend( "XXXXXX");
char * buf = ::strdup( tmpPath.asString().c_str() );
if ( ! buf )
{
ERR << "Out of memory" << endl;
return;
}
char * tmp = ::mkdtemp( buf );
if ( tmp )
// success; create _impl
_impl = RW_pointer<Impl>( new Impl( tmp ) );
else
ERR << "Cant create '" << tmpPath << "' " << ::strerror( errno ) << endl;
::free( buf );
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpDir::makeSibling
// METHOD TYPE : TmpDir
//
TmpDir TmpDir::makeSibling( const Pathname & sibling_r )
{
TmpDir ret( sibling_r.dirname(), sibling_r.basename() );
// clone mode if sibling_r exists
PathInfo p( sibling_r );
if ( p.isDir() )
{
::chmod( ret.path().c_str(), p.st_mode() );
}
return ret;
}
///////////////////////////////////////////////////////////////////
//
// METHOD NAME : TmpDir::defaultPrefix
// METHOD TYPE : const std::string &
//
const std::string &
TmpDir::defaultPrefix()
{
static string p( "TmpDir." );
return p;
}
} // namespace filesystem
} // namespace zypp