From 05477f4de600d5e9247707024d162924d73c1a4b Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Fri, 6 Oct 2006 21:37:28 +0000 Subject: [PATCH] Return an error indicator. --- lib/ChangeLog | 10 ++++++++ lib/clean-temp.c | 70 ++++++++++++++++++++++++++++++++++++++------------------ lib/clean-temp.h | 24 +++++++++++-------- 3 files changed, 72 insertions(+), 32 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index b9e32d669..b947fe9ad 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,5 +1,15 @@ 2006-10-06 Bruno Haible + * clean-temp.h (cleanup_temp_file, cleanup_temp_subdir, + cleanup_temp_dir_contents, cleanup_temp_dir): Change return type to + int. + * clean-temp.c (do_unlink, do_rmdir, cleanup_temp_file, + cleanup_temp_subdir, cleanup_temp_dir_contents, cleanup_temp_dir): + Return an error indicator. + Suggested by Eric Blake. + +2006-10-06 Bruno Haible + * clean-temp.c (PATH_MAX): Provide a fallback for GNU Hurd. Reported by Eric Blake. diff --git a/lib/clean-temp.c b/lib/clean-temp.c index 12b9b6ce2..29a27ba21 100644 --- a/lib/clean-temp.c +++ b/lib/clean-temp.c @@ -408,48 +408,70 @@ unregister_temp_subdir (struct temp_dir *dir, } } -/* Remove a file, with optional error message. */ -static void +/* Remove a file, with optional error message. + Return 0 upon success, or -1 if there was some problem. */ +static int do_unlink (struct temp_dir *dir, const char *absolute_file_name) { if (unlink (absolute_file_name) < 0 && dir->cleanup_verbose && errno != ENOENT) - error (0, errno, _("cannot remove temporary file %s"), absolute_file_name); + { + error (0, errno, _("cannot remove temporary file %s"), absolute_file_name); + return -1; + } + return 0; } -/* Remove a directory, with optional error message. */ -static void +/* Remove a directory, with optional error message. + Return 0 upon success, or -1 if there was some problem. */ +static int do_rmdir (struct temp_dir *dir, const char *absolute_dir_name) { if (rmdir (absolute_dir_name) < 0 && dir->cleanup_verbose && errno != ENOENT) - error (0, errno, - _("cannot remove temporary directory %s"), absolute_dir_name); + { + error (0, errno, + _("cannot remove temporary directory %s"), absolute_dir_name); + return -1; + } + return 0; } -/* Remove the given ABSOLUTE_FILE_NAME and unregister it. */ -void +/* Remove the given ABSOLUTE_FILE_NAME and unregister it. + Return 0 upon success, or -1 if there was some problem. */ +int cleanup_temp_file (struct temp_dir *dir, const char *absolute_file_name) { - do_unlink (dir, absolute_file_name); + int err; + + err = do_unlink (dir, absolute_file_name); unregister_temp_file (dir, absolute_file_name); + + return err; } -/* Remove the given ABSOLUTE_DIR_NAME and unregister it. */ -void +/* Remove the given ABSOLUTE_DIR_NAME and unregister it. + Return 0 upon success, or -1 if there was some problem. */ +int cleanup_temp_subdir (struct temp_dir *dir, const char *absolute_dir_name) { - do_rmdir (dir, absolute_dir_name); + int err; + + err = do_rmdir (dir, absolute_dir_name); unregister_temp_subdir (dir, absolute_dir_name); + + return err; } -/* Remove all registered files and subdirectories inside DIR. */ -void +/* Remove all registered files and subdirectories inside DIR. + Return 0 upon success, or -1 if there was some problem. */ +int cleanup_temp_dir_contents (struct temp_dir *dir) { struct tempdir *tmpdir = (struct tempdir *)dir; + int err = 0; gl_list_t list; gl_list_iterator_t iter; const void *element; @@ -462,7 +484,7 @@ cleanup_temp_dir_contents (struct temp_dir *dir) { char *file = (char *) element; - do_unlink (dir, file); + err |= do_unlink (dir, file); gl_list_remove_node (list, node); /* Now only we can free file. */ free (file); @@ -476,24 +498,28 @@ cleanup_temp_dir_contents (struct temp_dir *dir) { char *subdir = (char *) element; - do_rmdir (dir, subdir); + err |= do_rmdir (dir, subdir); gl_list_remove_node (list, node); /* Now only we can free subdir. */ free (subdir); } gl_list_iterator_free (&iter); + + return err; } /* Remove all registered files and subdirectories inside DIR and DIR itself. - DIR cannot be used any more after this call. */ -void + DIR cannot be used any more after this call. + Return 0 upon success, or -1 if there was some problem. */ +int cleanup_temp_dir (struct temp_dir *dir) { struct tempdir *tmpdir = (struct tempdir *)dir; + int err = 0; size_t i; - cleanup_temp_dir_contents (dir); - do_rmdir (dir, tmpdir->dirname); + err |= cleanup_temp_dir_contents (dir); + err |= do_rmdir (dir, tmpdir->dirname); for (i = 0; i < cleanup_list.tempdir_count; i++) if (cleanup_list.tempdir_list[i] == tmpdir) @@ -510,7 +536,7 @@ cleanup_temp_dir (struct temp_dir *dir) /* Now only we can free the tmpdir->dirname and tmpdir itself. */ free (tmpdir->dirname); free (tmpdir); - return; + return err; } /* The user passed an invalid DIR argument. */ diff --git a/lib/clean-temp.h b/lib/clean-temp.h index aa5e8d3d5..25030080e 100644 --- a/lib/clean-temp.h +++ b/lib/clean-temp.h @@ -89,20 +89,24 @@ extern void register_temp_subdir (struct temp_dir *dir, extern void unregister_temp_subdir (struct temp_dir *dir, const char *absolute_dir_name); -/* Remove the given ABSOLUTE_FILE_NAME and unregister it. */ -extern void cleanup_temp_file (struct temp_dir *dir, - const char *absolute_file_name); +/* Remove the given ABSOLUTE_FILE_NAME and unregister it. + Return 0 upon success, or -1 if there was some problem. */ +extern int cleanup_temp_file (struct temp_dir *dir, + const char *absolute_file_name); -/* Remove the given ABSOLUTE_DIR_NAME and unregister it. */ -extern void cleanup_temp_subdir (struct temp_dir *dir, - const char *absolute_dir_name); +/* Remove the given ABSOLUTE_DIR_NAME and unregister it. + Return 0 upon success, or -1 if there was some problem. */ +extern int cleanup_temp_subdir (struct temp_dir *dir, + const char *absolute_dir_name); -/* Remove all registered files and subdirectories inside DIR. */ -extern void cleanup_temp_dir_contents (struct temp_dir *dir); +/* Remove all registered files and subdirectories inside DIR. + Return 0 upon success, or -1 if there was some problem. */ +extern int cleanup_temp_dir_contents (struct temp_dir *dir); /* Remove all registered files and subdirectories inside DIR and DIR itself. - DIR cannot be used any more after this call. */ -extern void cleanup_temp_dir (struct temp_dir *dir); + DIR cannot be used any more after this call. + Return 0 upon success, or -1 if there was some problem. */ +extern int cleanup_temp_dir (struct temp_dir *dir); /* Open a temporary file in a temporary directory. Registers the resulting file descriptor to be closed. */ -- 2.11.0