28 namespace filesystem {
46 CtorDefault = Autodelete
52 : _path( path_r ), _flags( flags_r )
57 if ( ! (_flags & Autodelete) || _path.empty() )
60 PathInfo p( _path, PathInfo::LSTAT );
67 if ( _flags & KeepTopdir )
76 INT <<
"TmpPath cleanup error (" << res <<
") " << p << endl;
78 DBG <<
"TmpPath cleaned up " << p << endl;
86 {
return( _flags & Autodelete ); }
89 { _flags = yesno_r ? CtorDefault : NoOp; }
117 :_impl( tmpPath_r.empty() ?
nullptr : new
Impl( tmpPath_r ) )
135 TmpPath::operator bool()
const
159 static Pathname p( getenv(
"ZYPPTMPDIR") ? getenv(
"ZYPPTMPDIR") :
"/var/tmp" );
164 {
return _impl.get() ?
_impl->autoCleanup() :
false; }
167 {
if (
_impl.get() )
_impl->autoCleanup( yesno_r ); }
182 const std::string & prefix_r )
187 ERR <<
"Parent directory '" << inParentDir_r <<
"' can't be created." << endl;
192 Pathname tmpPath = (inParentDir_r + prefix_r).extend(
"XXXXXX");
193 char * buf = ::strdup( tmpPath.
asString().c_str() );
196 ERR <<
"Out of memory" << endl;
200 int tmpFd = ::mkostemp( buf, O_CLOEXEC );
208 ERR <<
"Cant create '" << buf <<
"' " <<
::strerror( errno ) << endl;
225 ::chmod( ret.path().c_str(), p.st_mode() );
238 static string p(
"TmpFile." );
254 const std::string & prefix_r )
259 ERR <<
"Parent directory '" << inParentDir_r <<
"' can't be created." << endl;
264 Pathname tmpPath = (inParentDir_r + prefix_r).extend(
"XXXXXX");
265 char * buf = ::strdup( tmpPath.
asString().c_str() );
268 ERR <<
"Out of memory" << endl;
272 char * tmp = ::mkdtemp( buf );
277 ERR <<
"Cant create '" << tmpPath <<
"' " <<
::strerror( errno ) << endl;
294 ::chmod( ret.path().c_str(), p.st_mode() );
307 static string p(
"TmpDir." );
int assert_dir(const Pathname &path, unsigned mode)
Like 'mkdir -p'.
Impl(const Pathname &path_r, Flags flags_r=CtorDefault)
TmpFile(const Pathname &inParentDir_r=defaultLocation(), const std::string &prefix_r=defaultPrefix())
Ctor.
std::string basename() const
Return the last component of this path.
static TmpDir makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
bool isExist() const
Return whether valid stat info exists.
const std::string & asString() const
String representation.
int chmod(const Pathname &path, mode_t mode)
Like 'chmod'.
Pathname dirname() const
Return all but the last component od this path.
int clean_dir(const Pathname &path)
Like 'rm -r DIR/ *'.
static const Pathname & defaultLocation()
TmpDir(const Pathname &inParentDir_r=defaultLocation(), const std::string &prefix_r=defaultPrefix())
Ctor.
Provide a new empty temporary file and delete it when no longer needed.
const Pathname & path() const
boost::noncopyable NonCopyable
Ensure derived classes cannot be copied.
Clean or delete a directory on destruction.
Provide a new empty temporary directory and recursively delete it when no longer needed.
static const std::string & defaultPrefix()
int unlink(const Pathname &path)
Like 'unlink'.
int recursive_rmdir(const Pathname &path)
Like 'rm -r DIR'.
Base class for reference counted objects.
Wrapper for const correct access via Smart pointer types.
Wrapper class for ::stat/::lstat.
static TmpFile makeSibling(const Pathname &sibling_r)
Provide a new empty temporary directory as sibling.
bool autoCleanup() const
Whether path is valid and deleted when the last reference drops.
std::string strerror(int errno_r)
Return string describing the error_r code.
void autoCleanup(bool yesno_r)
static const std::string & defaultPrefix()