]> git.cworth.org Git - notmuch/commitdiff
compat: rename {,notmuch_}canonicalize_file_name
authorĐoàn Trần Công Danh <congdanhqx@gmail.com>
Sat, 24 Apr 2021 01:05:37 +0000 (08:05 +0700)
committerDavid Bremner <david@tethera.net>
Sat, 24 Apr 2021 11:07:00 +0000 (08:07 -0300)
When compat canonicalize_file_name was introduced, it was limited to
C code only because it was used by C code only during that time.

>From 5ec6fd4d, (lib/open: check for split configuration when creating
database., 2021-02-16), lib/open.cc, which is C++, relies on the
existent of canonicalize_file_name.

However, we can't blindly enable canonicalize_file_name for C++ code,
because different implementation has different additional signature for
C++ and users can arbitrarily add -DHAVE_CANONICALIZE_FILE_NAME=0 to
{C,CXX}FLAGS.

Let's move our implementation into a util library.

Helped-by: Tomi Ollila <tomi.ollila@iki.fi>
Signed-off-by: Đoàn Trần Công Danh <congdanhqx@gmail.com>
compat/Makefile.local
compat/canonicalize_file_name.c [deleted file]
compat/compat.h
lib/open.cc
notmuch-config.c
util/Makefile.local
util/path-util.c [new file with mode: 0644]
util/path-util.h [new file with mode: 0644]

index 2ee1b3996f23f17ca980affdf1bf8cbae763029e..c58ca746c5dd8f954d62b652c14df9c65d14078c 100644 (file)
@@ -5,10 +5,6 @@ extra_cflags += -I$(srcdir)/$(dir)
 
 notmuch_compat_srcs :=
 
 
 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
 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
deleted file mode 100644 (file)
index 000f9e7..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-#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
-}
index 8f15e585fda82221eaeed22bfc0834b4ccd9fa67..59e9161830667c229ac936994f4a2bfb60c95dc2 100644 (file)
@@ -37,14 +37,6 @@ extern "C" {
 #define _POSIX_PTHREAD_SEMANTICS 1
 #endif
 
 #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>
 #if ! HAVE_GETLINE
 #include <stdio.h>
 #include <unistd.h>
index 5d80a884fb35aecbc6f084505ccb80ce85c8fec7..bdb695fe3c671ddc8bb9a28e032c0bda69d4616e 100644 (file)
@@ -3,6 +3,7 @@
 
 #include "database-private.h"
 #include "parse-time-vrp.h"
 
 #include "database-private.h"
 #include "parse-time-vrp.h"
+#include "path-util.h"
 
 #if HAVE_XAPIAN_DB_RETRY_LOCK
 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
 
 #if HAVE_XAPIAN_DB_RETRY_LOCK
 #define DB_ACTION (Xapian::DB_CREATE_OR_OPEN | Xapian::DB_RETRY_LOCK)
@@ -612,9 +613,9 @@ notmuch_database_create_with_config (const char *database_path,
     _set_database_path (notmuch, database_path);
 
     if (key_file && ! split) {
     _set_database_path (notmuch, database_path);
 
     if (key_file && ! split) {
-       char *mail_root = canonicalize_file_name (
+       char *mail_root = notmuch_canonicalize_file_name (
            g_key_file_get_value (key_file, "database", "mail_root", NULL));
            g_key_file_get_value (key_file, "database", "mail_root", NULL));
-       char *db_path = canonicalize_file_name (database_path);
+       char *db_path = notmuch_canonicalize_file_name (database_path);
 
        split = (mail_root && (0 != strcmp (mail_root, db_path)));
 
 
        split = (mail_root && (0 != strcmp (mail_root, db_path)));
 
index 16e869162e5397e4972b9d2e444a8b5c8822b1f1..d9390c4d81ea10ff7e82196a5d034025b4915250 100644 (file)
@@ -24,6 +24,7 @@
 #include <netdb.h>
 #include <assert.h>
 
 #include <netdb.h>
 #include <assert.h>
 
+#include "path-util.h"
 #include "unicode-util.h"
 
 static const char toplevel_config_comment[] =
 #include "unicode-util.h"
 
 static const char toplevel_config_comment[] =
@@ -327,7 +328,7 @@ notmuch_conffile_save (notmuch_conffile_t *config)
     }
 
     /* Try not to overwrite symlinks. */
     }
 
     /* Try not to overwrite symlinks. */
-    filename = canonicalize_file_name (config->filename);
+    filename = notmuch_canonicalize_file_name (config->filename);
     if (! filename) {
        if (errno == ENOENT) {
            filename = strdup (config->filename);
     if (! filename) {
        if (errno == ENOENT) {
            filename = strdup (config->filename);
index 7ef029a566d2067c357d72cb5f39da1537de7583..8a0b9bc3777ca9487ed9a3854a91c0ad77a91395 100644 (file)
@@ -6,7 +6,7 @@ extra_cflags += -I$(srcdir)/$(dir)
 libnotmuch_util_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \
                  $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \
                $(dir)/util.c $(dir)/gmime-extra.c $(dir)/crypto.c \
 libnotmuch_util_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \
                  $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \
                $(dir)/util.c $(dir)/gmime-extra.c $(dir)/crypto.c \
-               $(dir)/repair.c \
+               $(dir)/repair.c $(dir)/path-util.c \
                $(dir)/unicode-util.c
 
 libnotmuch_util_modules := $(libnotmuch_util_c_srcs:.c=.o)
                $(dir)/unicode-util.c
 
 libnotmuch_util_modules := $(libnotmuch_util_c_srcs:.c=.o)
diff --git a/util/path-util.c b/util/path-util.c
new file mode 100644 (file)
index 0000000..3267a96
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define _GNU_SOURCE
+
+#include "path-util.h"
+
+#include <limits.h>
+#include <stdlib.h>
+
+
+char *
+notmuch_canonicalize_file_name (const char *path)
+{
+#if HAVE_CANONICALIZE_FILE_NAME
+    return canonicalize_file_name (path);
+#elif defined(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/util/path-util.h b/util/path-util.h
new file mode 100644 (file)
index 0000000..ac85f69
--- /dev/null
@@ -0,0 +1,19 @@
+/*
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#ifndef NOTMUCH_UTIL_PATH_UTIL_H_
+#define NOTMUCH_UTIL_PATH_UTIL_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+char *
+notmuch_canonicalize_file_name (const char *path);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* NOTMUCH_UTIL_PATH_UTIL_H_ */