unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Subject: [PATCH] compat: add canonicalize_file_name
Date: Mon, 27 Jan 2014 10:12:12 -0400	[thread overview]
Message-ID: <1390831932-7865-1-git-send-email-david@tethera.net> (raw)
In-Reply-To: <8761p6n0wc.fsf@zancas.localnet>

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.
---
 compat/Makefile.local                |  4 ++++
 compat/canonicalize_file_name.c      | 18 ++++++++++++++++++
 compat/compat.h                      |  8 ++++++++
 compat/have_canonicalize_file_name.c | 10 ++++++++++
 configure                            | 16 ++++++++++++++++
 notmuch-config.c                     |  2 +-
 6 files changed, 57 insertions(+), 1 deletion(-)
 create mode 100644 compat/canonicalize_file_name.c
 create mode 100644 compat/have_canonicalize_file_name.c

diff --git a/compat/Makefile.local b/compat/Makefile.local
index b0d5417..bcb9f0e 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 0000000..e92c0f6
--- /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 5a402d5..634d505 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 0000000..24c848e
--- /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 13b6062..5b7c941 100755
--- a/configure
+++ b/configure
@@ -526,6 +526,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
@@ -751,6 +763,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 8d28653..4886d36 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);
-- 
1.8.5.2

  reply	other threads:[~2014-01-27 14:12 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-01-25 21:59 [RFC PATCH] configure: check for POSIX.1-2008 realpath(3) implementation Tomi Ollila
2014-01-25 22:22 ` Tomi Ollila
2014-01-26  0:55 ` David Bremner
2014-01-26 11:18   ` Tomi Ollila
2014-01-26 16:49     ` David Bremner
2014-01-27 14:12       ` David Bremner [this message]
2014-01-27 19:07         ` [PATCH] compat: add canonicalize_file_name Tomi Ollila
2014-04-08 11:22         ` David Bremner
2014-04-09 11:24           ` [PATCH 1/2] build: add canonicalize_file_name to symbols exported from libnotmuch.so David Bremner
2014-04-09 11:24             ` [PATCH 2/2] configure: fix comment, pass HAVE_CANONICALIZE_FILE_NAME to build David Bremner
2014-04-17 22:24               ` [PATCH] " David Bremner
2014-04-14 12:21             ` [PATCH 1/2] build: add canonicalize_file_name to symbols exported from libnotmuch.so Tomi Ollila
2014-04-18 21:05             ` David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1390831932-7865-1-git-send-email-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).