From: Thomas Roessler Date: Tue, 2 Jan 2007 17:10:34 +0000 (+0000) Subject: add debug harness to lib.c functions, and a bunch of dprint's X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=751754c26cf23fedc8511af960eb976226f0120c;p=neomutt add debug harness to lib.c functions, and a bunch of dprint's to safe_rename(). --- diff --git a/globals.h b/globals.h index 6abd75e0e..7836155b0 100644 --- a/globals.h +++ b/globals.h @@ -254,10 +254,6 @@ WHERE char *SmimeGetCertEmailCommand; -#ifdef DEBUG -WHERE FILE *debugfile INITVAL (0); -WHERE int debuglevel INITVAL (0); -#endif #ifdef MAIN_C const char *Weekdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; diff --git a/lib.c b/lib.c index d672a8a0c..e6e80c2ed 100644 --- a/lib.c +++ b/lib.c @@ -26,6 +26,8 @@ * some of our "standard" functions in external programs, too. */ +#define _LIB_C 1 + #if HAVE_CONFIG_H # include "config.h" #endif @@ -48,6 +50,7 @@ #include "lib.h" + static struct sysexits { int v; @@ -361,7 +364,7 @@ int mutt_copy_stream (FILE *fin, FILE *fout) return 0; } -static int +int compare_stat (struct stat *osb, struct stat *nsb) { if (osb->st_dev != nsb->st_dev || osb->st_ino != nsb->st_ino || @@ -412,6 +415,8 @@ int safe_symlink(const char *oldpath, const char *newpath) return 0; } + + /* * This function is supposed to do nfs-safe renaming of files. * @@ -439,10 +444,22 @@ int safe_rename (const char *src, const char *target) * to try it here. * */ + + dprint (1, (debugfile, "safe_rename: link (%s, %s) failed: %s (%d)\n", src, target, strerror (errno), errno)); if (errno == EXDEV) - return rename (src, target); + { + dprint (1, (debugfile, "safe_rename: errno was EXDEV; trying rename...\n")); + if (rename (src, target) == -1) + { + dprint (1, (debugfile, "safe_rename: rename (%s, %s) failed: %s (%d)\n", src, target, strerror (errno), errno)); + return -1; + } + dprint (1, (debugfile, "safe_rename: rename succeeded.\n")); + return 0; + } + return -1; } @@ -450,13 +467,17 @@ int safe_rename (const char *src, const char *target) * Stat both links and check if they are equal. */ - if (stat (src, &ssb) == -1) + if (lstat (src, &ssb) == -1) { + dprint (1, (debugfile, "safe_rename: can't stat %s: %s (%d)\n", + src, strerror (errno), errno)); return -1; } - if (stat (target, &tsb) == -1) + if (lstat (target, &tsb) == -1) { + dprint (1, (debugfile, "safe_rename: can't stat %s: %s (%d)\n", + src, strerror (errno), errno)); return -1; } @@ -467,6 +488,7 @@ int safe_rename (const char *src, const char *target) if (compare_stat (&ssb, &tsb) == -1) { + dprint (1, (debugfile, "safe_rename: stat blocks for %s and %s diverge; pretending EEXIST.\n", src, target)); errno = EEXIST; return -1; } @@ -476,11 +498,17 @@ int safe_rename (const char *src, const char *target) * value here? XXX */ - unlink (src); + if (unlink (src) == -1) + { + dprint (1, (debugfile, "safe_rename: unlink (%s) failed: %s (%d)\n", + src, strerror (errno), errno)); + } + return 0; } + /* Create a temporary directory next to a file name */ int mutt_mkwrapdir (const char *path, char *newfile, size_t nflen, diff --git a/lib.h b/lib.h index eb3ba33cf..952a1b4ed 100644 --- a/lib.h +++ b/lib.h @@ -98,11 +98,36 @@ * A non-mutt "implementation" (ahem) can be found in extlib.c. */ + # ifndef _EXTLIB_C extern void (*mutt_error) (const char *, ...); # endif + +# ifdef _LIB_C +# define MUTT_LIB_WHERE +# define MUTT_LIB_INITVAL(x) = x +# else +# define MUTT_LIB_WHERE extern +# define MUTT_LIB_INITVAL(x) +# endif + void mutt_exit (int); + +# ifdef DEBUG + +MUTT_LIB_WHERE FILE *debugfile MUTT_LIB_INITVAL(0); +MUTT_LIB_WHERE int debuglevel MUTT_LIB_INITVAL(0); + +# define dprint(N,X) do { if(debuglevel>=N && debugfile) fprintf X; } while (0) + +# else + +# define dprint(N,X) + +# endif + + /* Exit values used in send_msg() */ #define S_ERR 127 #define S_BKG 126 @@ -124,6 +149,7 @@ char *safe_strdup (const char *); const char *mutt_stristr (const char *, const char *); const char *mutt_basename (const char *); +int compare_stat (struct stat *, struct stat *); int mutt_copy_stream (FILE *, FILE *); int mutt_copy_bytes (FILE *, FILE *, size_t); int mutt_rx_sanitize_string (char *, size_t, const char *); @@ -133,8 +159,8 @@ int mutt_strncasecmp (const char *, const char *, size_t); int mutt_strncmp (const char *, const char *, size_t); int mutt_strcoll (const char *, const char *); int safe_open (const char *, int); -int safe_symlink (const char *, const char *); int safe_rename (const char *, const char *); +int safe_symlink (const char *, const char *); int safe_fclose (FILE **); size_t mutt_quote_filename (char *, size_t, const char *); diff --git a/mutt.h b/mutt.h index 72a9d57ed..8cb74f8b2 100644 --- a/mutt.h +++ b/mutt.h @@ -71,6 +71,8 @@ #define INITVAL(x) #endif +#define WHERE_DEFINED 1 + #include "mutt_regex.h" /* flags for mutt_copy_header() */ diff --git a/mutt_socket.c b/mutt_socket.c index cf0037f88..4c1b96e86 100644 --- a/mutt_socket.c +++ b/mutt_socket.c @@ -23,7 +23,6 @@ #endif #include "mutt.h" -#include "globals.h" #include "mutt_socket.h" #include "mutt_tunnel.h" #if defined(USE_SSL) diff --git a/muttlib.c b/muttlib.c index 7bd9c6edb..babe55adb 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1641,3 +1641,4 @@ int mutt_match_spam_list (const char *s, SPAM_LIST *l, char *text, int x) return 0; } + diff --git a/pgppacket.c b/pgppacket.c index acf617d0e..8c2d4b928 100644 --- a/pgppacket.c +++ b/pgppacket.c @@ -27,6 +27,11 @@ #include #include + + +/* yuck, we were including this one somewhere below. */ +#include "mutt.h" + #include "sha1.h" #include "lib.h" #include "pgplib.h" diff --git a/protos.h b/protos.h index 451753c85..f0456fe60 100644 --- a/protos.h +++ b/protos.h @@ -23,12 +23,6 @@ #include "mbyte.h" -#ifdef DEBUG -#define dprint(N,X) do { if(debuglevel>=N) fprintf X; } while (0) -#else -#define dprint(N,X) -#endif - #define MoreArgs(p) (*p->dptr && *p->dptr != ';' && *p->dptr != '#') #define mutt_make_string(A,B,C,D,E) _mutt_make_string(A,B,C,D,E,0)