* some of our "standard" functions in external programs, too.
*/
+#define _LIB_C 1
+
#if HAVE_CONFIG_H
# include "config.h"
#endif
#include "lib.h"
+
static struct sysexits
{
int v;
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 ||
return 0;
}
+
+
/*
* This function is supposed to do nfs-safe renaming of files.
*
* 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;
}
* 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;
}
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;
}
* 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,
* 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
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 *);
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 *);