dovecot-2.2: lib: Added i_unlink() and i_unlink_if_exists()

dovecot at dovecot.org dovecot at dovecot.org
Tue Sep 8 16:07:23 UTC 2015


details:   http://hg.dovecot.org/dovecot-2.2/rev/2ec2b0a51fec
changeset: 19135:2ec2b0a51fec
user:      Timo Sirainen <tss at iki.fi>
date:      Tue Sep 08 18:49:00 2015 +0300
description:
lib: Added i_unlink() and i_unlink_if_exists()
These log the error message on a failed unlink(). They also include the
source code file and line number to make it easier to find which unlink()
actually failed if the path itself doesn't already clearly identify it.
This can be especially useful if the path is (null), "" or contains some
corrupted garbage.

diffstat:

 src/lib/lib.c |  27 +++++++++++++++++++++++++++
 src/lib/lib.h |  10 ++++++++++
 2 files changed, 37 insertions(+), 0 deletions(-)

diffs (57 lines):

diff -r 962ff5ebd807 -r 2ec2b0a51fec src/lib/lib.c
--- a/src/lib/lib.c	Tue Sep 08 18:45:57 2015 +0300
+++ b/src/lib/lib.c	Tue Sep 08 18:49:00 2015 +0300
@@ -30,6 +30,33 @@
 	return ret;
 }
 
+#undef i_unlink
+int i_unlink(const char *path, const char *source_fname,
+	     unsigned int source_linenum)
+{
+	if (unlink(path) < 0) {
+		i_error("unlink(%s) failed: %m (in %s:%u)",
+			path, source_fname, source_linenum);
+		return -1;
+	}
+	return 0;
+}
+
+#undef i_unlink_if_exists
+int i_unlink_if_exists(const char *path, const char *source_fname,
+		       unsigned int source_linenum)
+{
+	if (unlink(path) == 0)
+		return 1;
+	else if (errno == ENOENT)
+		return 0;
+	else {
+		i_error("unlink(%s) failed: %m (in %s:%u)",
+			path, source_fname, source_linenum);
+		return -1;
+	}
+}
+
 void lib_atexit(lib_atexit_callback_t *callback)
 {
 	lib_atexit_priority(callback, 0);
diff -r 962ff5ebd807 -r 2ec2b0a51fec src/lib/lib.h
--- a/src/lib/lib.h	Tue Sep 08 18:45:57 2015 +0300
+++ b/src/lib/lib.h	Tue Sep 08 18:49:00 2015 +0300
@@ -49,6 +49,16 @@
 #define LIB_ATEXIT_PRIORITY_LOW 10
 
 int close_keep_errno(int *fd);
+/* Call unlink(). If it fails, log an error including the source filename
+   and line number. */
+int i_unlink(const char *path, const char *source_fname,
+	     unsigned int source_linenum);
+#define i_unlink(path) i_unlink(path, __FILE__, __LINE__)
+/* Same as i_unlink(), but don't log an error if errno=ENOENT. Returns 1 on
+   unlink() success, 0 if errno=ENOENT, -1 on other errors. */
+int i_unlink_if_exists(const char *path, const char *source_fname,
+		       unsigned int source_linenum);
+#define i_unlink_if_exists(path) i_unlink_if_exists(path, __FILE__, __LINE__)
 
 /* Call the given callback at the beginning of lib_deinit(). The main
    difference to atexit() is that liblib's memory allocation and logging


More information about the dovecot-cvs mailing list