]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added i_unlink() and i_unlink_if_exists()
authorTimo Sirainen <tss@iki.fi>
Tue, 8 Sep 2015 15:49:00 +0000 (18:49 +0300)
committerTimo Sirainen <tss@iki.fi>
Tue, 8 Sep 2015 15:49:00 +0000 (18:49 +0300)
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.

src/lib/lib.c
src/lib/lib.h

index 43478ddef1cb1b5f92b3fee10850720d2811f079..e06cb61eb47f491591984371e34aac9b10687c2a 100644 (file)
@@ -30,6 +30,33 @@ int close_keep_errno(int *fd)
        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);
index 2102d195f2159f9549e9249b1125fbfcec02f37a..bec259d5c22c159aaeb629b13588ee0644ca8395 100644 (file)
@@ -49,6 +49,16 @@ typedef void lib_atexit_callback_t(void);
 #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