diff options
author | Jeffrey Stedfast <fejj@ximian.com> | 2001-08-25 06:36:52 +0800 |
---|---|---|
committer | Jeffrey Stedfast <fejj@src.gnome.org> | 2001-08-25 06:36:52 +0800 |
commit | 7c6273ec2ee915a55b2a8fbeb1c597a2a0d0282d (patch) | |
tree | 616bb8d01c626d26aca0ec2fe9f81e22503cfddb | |
parent | b74947b0a0adc405938f3ec720e9ea6e1904de94 (diff) | |
download | gsoc2013-evolution-7c6273ec2ee915a55b2a8fbeb1c597a2a0d0282d.tar.gz gsoc2013-evolution-7c6273ec2ee915a55b2a8fbeb1c597a2a0d0282d.tar.zst gsoc2013-evolution-7c6273ec2ee915a55b2a8fbeb1c597a2a0d0282d.zip |
Added e-mktemp.[c,h] to the build.
2001-08-24 Jeffrey Stedfast <fejj@ximian.com>
* Makefile.am: Added e-mktemp.[c,h] to the build.
* e-mktemp.c (e_mktemp): A new wrapper around mktemp so that we
can keep track of temp files for later cleanup.
(e_mkstemp): Same but obviously for the mkstemp call.
(e_mkdtemp): And finally a mkdtemp wrapper.
svn path=/trunk/; revision=12460
-rw-r--r-- | e-util/ChangeLog | 21 | ||||
-rw-r--r-- | e-util/Makefile.am | 2 | ||||
-rw-r--r-- | e-util/e-mktemp.c | 231 | ||||
-rw-r--r-- | e-util/e-mktemp.h | 33 |
4 files changed, 281 insertions, 6 deletions
diff --git a/e-util/ChangeLog b/e-util/ChangeLog index f8c0a19dd6..eb1c2f02ba 100644 --- a/e-util/ChangeLog +++ b/e-util/ChangeLog @@ -1,3 +1,12 @@ +2001-08-24 Jeffrey Stedfast <fejj@ximian.com> + + * Makefile.am: Added e-mktemp.[c,h] to the build. + + * e-mktemp.c (e_mktemp): A new wrapper around mktemp so that we + can keep track of temp files for later cleanup. + (e_mkstemp): Same but obviously for the mkstemp call. + (e_mkdtemp): And finally a mkdtemp wrapper. + 2001-08-24 Zbigniew Chyla <cyba@gnome.pl> * Makefile.am (libeutil_la_SOURCES): Added e-unicode-i18n.c and @@ -9,10 +18,9 @@ 2001-08-23 Zbigniew Chyla <cyba@gnome.pl> - * e-request.c (e_request_string): Modified to accept and return UTF-8 - string. - It's currently used only in shell/e-shortcuts-view.c and we need UTF-8 - there. + * e-request.c (e_request_string): Modified to accept and return + UTF-8 string. It's currently used only in + shell/e-shortcuts-view.c and we need UTF-8 there. 2001-08-20 Damon Chaplin <damon@ximian.com> @@ -24,8 +32,9 @@ * e-categories-config.c (e_categories_config_get_icon_for): return when there's no icon associated with the category - * e-categories-config.c: reworked to use the ECategoriesMasterListWombat - class. Also, load correctly images (fixes #6794) + * e-categories-config.c: reworked to use the + ECategoriesMasterListWombat class. Also, load correctly images + (fixes #6794) 2001-08-09 Federico Mena Quintero <federico@ximian.com> diff --git a/e-util/Makefile.am b/e-util/Makefile.am index 231d04b64a..3ced0ed128 100644 --- a/e-util/Makefile.am +++ b/e-util/Makefile.am @@ -46,6 +46,8 @@ libeutil_la_SOURCES = \ e-list.h \ e-memory.c \ e-memory.h \ + e-mktemp.c \ + e-mktemp.h \ e-msgport.c \ e-msgport.h \ e-path.c \ diff --git a/e-util/e-mktemp.c b/e-util/e-mktemp.c new file mode 100644 index 0000000000..8164bc145e --- /dev/null +++ b/e-util/e-mktemp.c @@ -0,0 +1,231 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2001 Ximian, Inc. (www.ximian.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#include "e-mktemp.h" + +#include <config.h> + +#include <glib.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <dirent.h> + +#ifdef ENABLE_THREADS +#include <pthread.h> +#endif + + +static gboolean initialized = FALSE; +static GSList *temp_files = NULL; +static GSList *temp_dirs = NULL; +#ifdef ENABLE_THREADS +static pthread_mutex_t temp_files_lock = PTHREAD_MUTEX_INITIALIZER; +static pthread_mutex_t temp_dirs_lock = PTHREAD_MUTEX_INITIALIZER; +#define TEMP_FILES_LOCK() pthread_mutex_lock (&temp_files_lock) +#define TEMP_FILES_UNLOCK() pthread_mutex_unlock (&temp_files_lock) +#define TEMP_DIRS_LOCK() pthread_mutex_lock (&temp_dirs_lock) +#define TEMP_DIRS_UNLOCK() pthread_mutex_unlock (&temp_dirs_lock) +#else +#define TEMP_FILES_LOCK() +#define TEMP_FILES_UNLOCK() +#define TEMP_DIRS_LOCK() +#define TEMP_DIRS_UNLOCK() +#endif /* ENABLE_THREADS */ + + +static GString * +get_path (gboolean make) +{ + GString *path; + + path = g_string_new ("/tmp/evolution-"); + g_string_sprintfa (path, "%d-%d", getuid (), getpid ()); + + if (make) + mkdir (path->str, S_IRWXU); + + return path; +} + +static void +e_mktemp_cleanup (void) +{ + GString *path; + GSList *node; + + TEMP_FILES_LOCK (); + if (temp_files) { + node = temp_files; + while (node) { + unlink (node->data); + g_free (node->data); + node = node->next; + } + g_slist_free (temp_files); + temp_files = NULL; + } + TEMP_FILES_UNLOCK (); + + TEMP_DIRS_LOCK (); + if (temp_dirs) { + node = temp_dirs; + while (node) { + /* perform the equivalent of a rm -rf */ + struct dirent *dent; + DIR *dir; + + /* first empty out this directory of it's files... */ + dir = opendir (node->data); + while ((dent = readdir (dir)) != NULL) { + /* yea...so if we contain + subdirectories this won't work, but + it shouldn't so we won't + bother caring... */ + if (strcmp (dent->d_name, ".") && strcmp (dent->d_name, "..")) + unlink (dent->d_name); + } + closedir (dir); + + /* ...then rmdir the directory */ + rmdir (node->data); + g_free (node->data); + node = node->next; + } + g_slist_free (temp_dirs); + temp_dirs = NULL; + } + TEMP_DIRS_UNLOCK (); + + path = get_path (FALSE); + rmdir (path->str); + + g_string_free (path, TRUE); +} + + +const char * +e_mktemp (const char *template) +{ + GString *path; + char *ret; + + path = get_path (TRUE); + if (!path) + return NULL; + + g_string_append_c (path, '/'); + if (template) + g_string_append (path, template); + else + g_string_append (path, "unknown-XXXXXX"); + + ret = mktemp (path->str); + if (ret) { + TEMP_FILES_LOCK (); + if (!initialized) + g_atexit (e_mktemp_cleanup); + temp_files = g_slist_prepend (temp_files, ret); + g_string_free (path, FALSE); + TEMP_FILES_UNLOCK (); + } else { + g_string_free (path, TRUE); + } + + return ret; +} + + +int +e_mkstemp (const char *template) +{ + GString *path; + int fd; + + path = get_path (TRUE); + if (!path) + return -1; + + g_string_append_c (path, '/'); + if (template) + g_string_append (path, template); + else + g_string_append (path, "unknown-XXXXXX"); + + fd = mkstemp (path->str); + if (fd != -1) { + TEMP_FILES_LOCK (); + if (!initialized) + g_atexit (e_mktemp_cleanup); + temp_files = g_slist_prepend (temp_files, path->str); + g_string_free (path, FALSE); + TEMP_FILES_UNLOCK (); + } else { + g_string_free (path, TRUE); + } + + return fd; +} + + +const char * +e_mkdtemp (const char *template) +{ + GString *path; + char *tmpdir; + + path = get_path (TRUE); + if (!path) + return NULL; + + g_string_append_c (path, '/'); + if (template) + g_string_append (path, template); + else + g_string_append (path, "unknown-XXXXXX"); + +#ifdef HAVE_MKDTEMP + tmpdir = mkdtemp (path->str); +#else + tmpdir = mktemp (path->str); + if (tmpdir) { + if (mkdir (tmpdir, S_IRWXU) == -1) + tmpdir = NULL; + } +#endif + + if (tmpdir) { + TEMP_DIRS_LOCK (); + if (!initialized) + g_atexit (e_mktemp_cleanup); + temp_dirs = g_slist_prepend (temp_dirs, tmpdir); + g_string_free (path, FALSE); + TEMP_DIRS_UNLOCK (); + } else { + g_string_free (path, TRUE); + } + + return tmpdir; +} diff --git a/e-util/e-mktemp.h b/e-util/e-mktemp.h new file mode 100644 index 0000000000..9f7831d7b5 --- /dev/null +++ b/e-util/e-mktemp.h @@ -0,0 +1,33 @@ +/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ +/* + * Authors: Jeffrey Stedfast <fejj@ximian.com> + * + * Copyright 2001 Ximian, Inc. (www.ximian.com) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA. + * + */ + + +#ifndef __E_MKTEMP_H__ +#define __E_MKTEMP_H__ + +const char *e_mktemp (const char *template); + +int e_mkstemp (const char *template); + +const char *e_mkdtemp (const char *template); + +#endif /* __E_MKTEMP_H__ */ |