TempFile: clean-up interface by using internal ref-counted class member. Uncomp: add interface to clear cache

This commit is contained in:
Jean-Francois Dockes
2018-05-17 10:24:01 +02:00
parent 40fd63662b
commit d69d2abbde
12 changed files with 147 additions and 96 deletions
+49 -3
View File
@@ -43,6 +43,7 @@
#include "wipedir.h"
#include "transcode.h"
#include "md5ut.h"
#include "log.h"
using namespace std;
@@ -281,8 +282,51 @@ bool maketmpdir(string& tdir, string& reason)
return true;
}
TempFileInternal::TempFileInternal(const string& suffix)
: m_noremove(false)
class TempFile::Internal {
public:
Internal(const std::string& suffix);
~Internal();
friend class TempFile;
private:
std::string m_filename;
std::string m_reason;
bool m_noremove{false};
};
TempFile::TempFile(const string& suffix)
: m(new Internal(suffix))
{
}
TempFile::TempFile()
{
m = std::shared_ptr<Internal>();
}
const char *TempFile::filename() const
{
return m ? m->m_filename.c_str() : "";
}
const std::string& TempFile::getreason() const
{
static string fatal{"fatal error"};
return m ? m->m_reason : fatal;
}
void TempFile::setnoremove(bool onoff)
{
if (m)
m->m_noremove = onoff;
}
bool TempFile::ok() const
{
return m ? !m->m_filename.empty() : false;
}
TempFile::Internal::Internal(const string& suffix)
{
// Because we need a specific suffix, can't use mkstemp
// well. There is a race condition between name computation and
@@ -322,7 +366,7 @@ TempFileInternal::TempFileInternal(const string& suffix)
}
}
TempFileInternal::~TempFileInternal()
TempFile::Internal::~Internal()
{
if (!m_filename.empty() && !m_noremove) {
unlink(m_filename.c_str());
@@ -335,11 +379,13 @@ TempDir::TempDir()
m_dirname.erase();
return;
}
LOGDEB("TempDir::TempDir: -> " << m_dirname << endl);
}
TempDir::~TempDir()
{
if (!m_dirname.empty()) {
LOGDEB("TempDir::~TempDir: erasing " << m_dirname << endl);
(void)wipedir(m_dirname, true, true);
m_dirname.erase();
}
+9 -20
View File
@@ -48,30 +48,19 @@ extern const std::string& tmplocation();
extern bool maketmpdir(std::string& tdir, std::string& reason);
/// Temporary file class
class TempFileInternal {
class TempFile {
public:
TempFileInternal(const std::string& suffix);
~TempFileInternal();
const char *filename() {
return m_filename.c_str();
}
const std::string& getreason() {
return m_reason;
}
void setnoremove(bool onoff) {
m_noremove = onoff;
}
bool ok() {
return !m_filename.empty();
}
TempFile(const std::string& suffix);
TempFile();
const char *filename() const;
const std::string& getreason() const;
void setnoremove(bool onoff);
bool ok() const;
class Internal;
private:
std::string m_filename;
std::string m_reason;
bool m_noremove;
std::shared_ptr<Internal> m;
};
typedef std::shared_ptr<TempFileInternal> TempFile;
/// Temporary directory class. Recursively deleted by destructor.
class TempDir {
public: