From 65e4f8b89682ae5986cc63345d4b837e6f1b6fe8 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sun, 9 Jan 2011 18:55:54 +0200 Subject: [PATCH] * add mu_util_create_tmpdir --- src/mu-util.c | 21 +++++++++++++++++++++ src/mu-util.h | 12 +++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/mu-util.c b/src/mu-util.c index c2d3ac3f..4008e2c5 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -111,6 +111,27 @@ mu_util_dir_expand (const char *path) return g_strdup (resolved); } + +char* +mu_util_create_tmpdir (void) +{ + gchar *dirname; + dirname = g_strdup_printf ("%s%cmu-%d%c%x", g_get_tmp_dir(), + G_DIR_SEPARATOR, + getuid(), + G_DIR_SEPARATOR, + (int)random()*getpid()*(int)time(NULL)); + + if (!mu_util_create_dir_maybe (dirname)) { + g_free (dirname); + return NULL; + } + + return dirname; +} + + + gboolean mu_util_init_system (void) { diff --git a/src/mu-util.h b/src/mu-util.h index 3ab0711d..6b72e0d7 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -81,6 +81,16 @@ gboolean mu_util_create_dir_maybe (const gchar *path) G_GNUC_WARN_UNUSED_RESULT; +/** + * create a temporary dir (typically, in /tmp) with a unique name, + * such that files can be saved there. + * + * @return the name of the new dir, or NULL in case of error. Free with g_free + */ +char* mu_util_create_tmpdir (void) + G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; + + /** * check whether path is a directory, and optionally, if it's readable * and/or writeable @@ -178,7 +188,7 @@ gboolean mu_util_clear_database (const gchar *xpath); * @return a newly allocated string */ gchar* mu_util_str_from_strv (const gchar **params) - G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; + G_GNUC_MALLOC G_GNUC_WARN_UNUSED_RESULT; /* * for OSs with out support for direntry->d_type, like Solaris