aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar David Bremner <david@tethera.net>2014-01-27 10:12:12 -0400
committerGravatar David Bremner <david@tethera.net>2014-04-08 07:27:14 -0300
commitaf5c3afa91cc29268c6754ea70f15cae30f22403 (patch)
treecc1fe830310c9debb7de0a19f707c06858479697
parent134dbadaf517c6962c2588f36f1f8782277e2f95 (diff)
compat: add canonicalize_file_name
the POSIX 2008 behaviour of realpath is not available everywhere so we provide a simple wrapper function. We use (and provide) the gnu extension canonicalize_file_name to make it cleaner to test for the feature we need; otherwise we have to rely on realpath segfaulting if the second argument is null.
-rw-r--r--compat/Makefile.local4
-rw-r--r--compat/canonicalize_file_name.c18
-rw-r--r--compat/compat.h8
-rw-r--r--compat/have_canonicalize_file_name.c10
-rwxr-xr-xconfigure16
-rw-r--r--notmuch-config.c2
6 files changed, 57 insertions, 1 deletions
diff --git a/compat/Makefile.local b/compat/Makefile.local
index b0d5417f..bcb9f0ec 100644
--- a/compat/Makefile.local
+++ b/compat/Makefile.local
@@ -5,6 +5,10 @@ extra_cflags += -I$(srcdir)/$(dir)
notmuch_compat_srcs :=
+ifneq ($(HAVE_CANONICALIZE_FILE_NAME),1)
+notmuch_compat_srcs += $(dir)/canonicalize_file_name.c
+endif
+
ifneq ($(HAVE_GETLINE),1)
notmuch_compat_srcs += $(dir)/getline.c $(dir)/getdelim.c
endif
diff --git a/compat/canonicalize_file_name.c b/compat/canonicalize_file_name.c
new file mode 100644
index 00000000..e92c0f62
--- /dev/null
+++ b/compat/canonicalize_file_name.c
@@ -0,0 +1,18 @@
+#include "compat.h"
+#include <limits.h>
+#undef _GNU_SOURCE
+#include <stdlib.h>
+
+char *
+canonicalize_file_name (const char * path)
+{
+#ifdef PATH_MAX
+ char *resolved_path = malloc (PATH_MAX+1);
+ if (resolved_path == NULL)
+ return NULL;
+
+ return realpath (path, resolved_path);
+#else
+#error undefined PATH_MAX _and_ missing canonicalize_file_name not supported
+#endif
+}
diff --git a/compat/compat.h b/compat/compat.h
index 5a402d5c..634d505b 100644
--- a/compat/compat.h
+++ b/compat/compat.h
@@ -37,6 +37,14 @@ extern "C" {
#define _POSIX_PTHREAD_SEMANTICS 1
#endif
+#if !HAVE_CANONICALIZE_FILE_NAME
+/* we only call this function from C, and this makes testing easier */
+#ifndef __cplusplus
+char *
+canonicalize_file_name (const char *path);
+#endif
+#endif
+
#if !HAVE_GETLINE
#include <stdio.h>
#include <unistd.h>
diff --git a/compat/have_canonicalize_file_name.c b/compat/have_canonicalize_file_name.c
new file mode 100644
index 00000000..24c848ec
--- /dev/null
+++ b/compat/have_canonicalize_file_name.c
@@ -0,0 +1,10 @@
+#define _GNU_SOURCE
+#include <stdlib.h>
+
+int main()
+{
+ char *found;
+ char *string;
+
+ found = canonicalize_file_name (string);
+}
diff --git a/configure b/configure
index 1d430b9c..09f4650c 100755
--- a/configure
+++ b/configure
@@ -556,6 +556,18 @@ EOF
exit 1
fi
+printf "Checking for canonicalize_file_name... "
+if ${CC} -o compat/have_canonicalize_file_name "$srcdir"/compat/have_canonicalize_file_name.c > /dev/null 2>&1
+then
+ printf "Yes.\n"
+ have_canonicalize_file_name=1
+else
+ printf "No (will use our own instead).\n"
+ have_canonicalize_file_name=0
+fi
+rm -f compat/have_canonicalize_file_name
+
+
printf "Checking for getline... "
if ${CC} -o compat/have_getline "$srcdir"/compat/have_getline.c > /dev/null 2>&1
then
@@ -802,6 +814,10 @@ zsh_completion_dir = ${ZSHCOMLETIONDIR:=\$(prefix)/share/zsh/functions/Completio
# Whether the getline function is available (if not, then notmuch will
# build its own version)
+HAVE_CANONICALIZE_FILE_NAME = ${have_canonicalize_file_name}
+
+# Whether the getline function is available (if not, then notmuch will
+# build its own version)
HAVE_GETLINE = ${have_getline}
# Whether the strcasestr function is available (if not, then notmuch will
diff --git a/notmuch-config.c b/notmuch-config.c
index 8d286538..4886d366 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -454,7 +454,7 @@ notmuch_config_save (notmuch_config_t *config)
}
/* Try not to overwrite symlinks. */
- filename = realpath (config->filename, NULL);
+ filename = canonicalize_file_name (config->filename);
if (! filename) {
if (errno == ENOENT) {
filename = strdup (config->filename);