diff options
| author | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2022-04-09 19:14:48 +0300 |
|---|---|---|
| committer | Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> | 2022-04-14 17:06:33 +0300 |
| commit | a7e6d57286997a9516e89d3a144032c08b222bd7 (patch) | |
| tree | 9986f27c71b90c7685b81b824487f8d6f4764481 /lib/utils/mu-utils.cc | |
| parent | 100b41b257e295f94a086fede004333d87349406 (diff) | |
utils: add TempDir RAII class
For tests
Diffstat (limited to 'lib/utils/mu-utils.cc')
| -rw-r--r-- | lib/utils/mu-utils.cc | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/utils/mu-utils.cc b/lib/utils/mu-utils.cc index ce4c4cf..1c02ed0 100644 --- a/lib/utils/mu-utils.cc +++ b/lib/utils/mu-utils.cc @@ -33,6 +33,7 @@ #include <iostream> #include <algorithm> #include <numeric> +#include <functional> #include <glib.h> #include <glib/gprintf.h> @@ -40,6 +41,7 @@ #include "mu-utils.hh" #include "mu-util.h" #include "mu-str.h" +#include "mu-error.hh" using namespace Mu; @@ -582,6 +584,7 @@ Mu::canonicalize_filename(const std::string& path, const std::string& relative_t return rv; } + void Mu::allow_warnings() { @@ -589,3 +592,31 @@ Mu::allow_warnings() [](const char*, GLogLevelFlags, const char*, gpointer) { return FALSE; }, {}); } + + + +Mu::TempDir::TempDir() +{ + GError *err{}; + gchar *tmpdir = g_dir_make_tmp("mu-tmp-XXXXXX", &err); + if (!tmpdir) + throw Mu::Error(Error::Code::File, &err, + "failed to create temporary directory"); + + path_ = tmpdir; + g_free(tmpdir); + + g_debug("created '%s'", path_.c_str()); +} + +Mu::TempDir::~TempDir() +{ + if (::access(path_.c_str(), F_OK) != 0) + return; /* nothing to do */ + + /* ugly */ + const auto cmd{format("/bin/rm -rf '%s'", path_.c_str())}; + ::system(cmd.c_str()); + + g_debug("removed '%s'", path_.c_str()); +} |
