b6c903
commit d8e8097f3be5b3c49fc741fa19e1da0b0431384c
b6c903
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
b6c903
Date:   Thu Jun 10 14:07:27 2021 +0530
b6c903
b6c903
    gconv_conf: Split out configuration file processing
b6c903
    
b6c903
    Split configuration file processing into a separate header file and
b6c903
    include it.  Macroize all calls that need to go through internal
b6c903
    interfaces so that iconvconfig can also use them.
b6c903
    
b6c903
    Reviewed-by: DJ Delorie <dj@redhat.com>
b6c903
b6c903
# Conflicts:
b6c903
#	iconv/gconv_conf.c
b6c903
b6c903
diff --git a/iconv/gconv_conf.c b/iconv/gconv_conf.c
b6c903
index dc12ce24844474cc..ce64faa928dc1c52 100644
b6c903
--- a/iconv/gconv_conf.c
b6c903
+++ b/iconv/gconv_conf.c
b6c903
@@ -19,7 +19,6 @@
b6c903
 
b6c903
 #include <assert.h>
b6c903
 #include <ctype.h>
b6c903
-#include <dirent.h>
b6c903
 #include <errno.h>
b6c903
 #include <limits.h>
b6c903
 #include <locale.h>
b6c903
@@ -31,11 +30,10 @@
b6c903
 #include <string.h>
b6c903
 #include <unistd.h>
b6c903
 #include <sys/param.h>
b6c903
-#include <sys/types.h>
b6c903
 
b6c903
 #include <libc-lock.h>
b6c903
 #include <gconv_int.h>
b6c903
-
b6c903
+#include <gconv_parseconfdir.h>
b6c903
 
b6c903
 /* This is the default path where we look for module lists.  */
b6c903
 static const char default_gconv_path[] = GCONV_PATH;
b6c903
@@ -49,11 +47,6 @@ size_t __gconv_max_path_elem_len;
b6c903
 /* We use the following struct if we couldn't allocate memory.  */
b6c903
 static const struct path_elem empty_path_elem = { NULL, 0 };
b6c903
 
b6c903
-/* Name of the file containing the module information in the directories
b6c903
-   along the path.  */
b6c903
-static const char gconv_conf_filename[] = "gconv-modules";
b6c903
-static const char gconv_conf_dirname[] = "gconv-modules.d";
b6c903
-
b6c903
 /* Filename extension for the modules.  */
b6c903
 #ifndef MODULE_EXT
b6c903
 # define MODULE_EXT ".so"
b6c903
@@ -92,9 +85,6 @@ static const char builtin_aliases[] =
b6c903
 #undef BUILTIN_ALIAS
b6c903
 };
b6c903
 
b6c903
-#include <libio/libioP.h>
b6c903
-#define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
b6c903
-
b6c903
 
b6c903
 /* Value of the GCONV_PATH environment variable.  */
b6c903
 const char *__gconv_path_envvar;
b6c903
@@ -354,72 +344,6 @@ add_module (char *rp, const char *directory, size_t dir_len, int modcounter)
b6c903
 }
b6c903
 
b6c903
 
b6c903
-/* Read the next configuration file.  */
b6c903
-static void
b6c903
-read_conf_file (const char *filename, const char *directory, size_t dir_len)
b6c903
-{
b6c903
-  /* Note the file is opened with cancellation in the I/O functions
b6c903
-     disabled.  */
b6c903
-  FILE *fp = fopen (filename, "rce");
b6c903
-  char *line = NULL;
b6c903
-  size_t line_len = 0;
b6c903
-  static int modcounter;
b6c903
-
b6c903
-  /* Don't complain if a file is not present or readable, simply silently
b6c903
-     ignore it.  */
b6c903
-  if (fp == NULL)
b6c903
-    return;
b6c903
-
b6c903
-  /* No threads reading from this stream.  */
b6c903
-  __fsetlocking (fp, FSETLOCKING_BYCALLER);
b6c903
-
b6c903
-  /* Process the known entries of the file.  Comments start with `#' and
b6c903
-     end with the end of the line.  Empty lines are ignored.  */
b6c903
-  while (!__feof_unlocked (fp))
b6c903
-    {
b6c903
-      char *rp, *endp, *word;
b6c903
-      ssize_t n = __getdelim (&line, &line_len, '\n', fp);
b6c903
-      if (n < 0)
b6c903
-	/* An error occurred.  */
b6c903
-	break;
b6c903
-
b6c903
-      rp = line;
b6c903
-      /* Terminate the line (excluding comments or newline) by an NUL byte
b6c903
-	 to simplify the following code.  */
b6c903
-      endp = strchr (rp, '#');
b6c903
-      if (endp != NULL)
b6c903
-	*endp = '\0';
b6c903
-      else
b6c903
-	if (rp[n - 1] == '\n')
b6c903
-	  rp[n - 1] = '\0';
b6c903
-
b6c903
-      while (__isspace_l (*rp, _nl_C_locobj_ptr))
b6c903
-	++rp;
b6c903
-
b6c903
-      /* If this is an empty line go on with the next one.  */
b6c903
-      if (rp == endp)
b6c903
-	continue;
b6c903
-
b6c903
-      word = rp;
b6c903
-      while (*rp != '\0' && !__isspace_l (*rp, _nl_C_locobj_ptr))
b6c903
-	++rp;
b6c903
-
b6c903
-      if (rp - word == sizeof ("alias") - 1
b6c903
-	  && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
b6c903
-	add_alias (rp);
b6c903
-      else if (rp - word == sizeof ("module") - 1
b6c903
-	       && memcmp (word, "module", sizeof ("module") - 1) == 0)
b6c903
-	add_module (rp, directory, dir_len, modcounter++);
b6c903
-      /* else */
b6c903
-	/* Otherwise ignore the line.  */
b6c903
-    }
b6c903
-
b6c903
-  free (line);
b6c903
-
b6c903
-  fclose (fp);
b6c903
-}
b6c903
-
b6c903
-
b6c903
 /* Determine the directories we are looking for data in.  */
b6c903
 void
b6c903
 __gconv_get_path (void)
b6c903
@@ -552,55 +476,8 @@ __gconv_read_conf (void)
b6c903
     __gconv_get_path ();
b6c903
 
b6c903
   for (cnt = 0; __gconv_path_elem[cnt].name != NULL; ++cnt)
b6c903
-    {
b6c903
-      const char *elem = __gconv_path_elem[cnt].name;
b6c903
-      size_t elem_len = __gconv_path_elem[cnt].len;
b6c903
-
b6c903
-      /* No slash needs to be inserted between elem and gconv_conf_filename;
b6c903
-	 elem already ends in a slash.  */
b6c903
-      char *buf = malloc (elem_len + sizeof (gconv_conf_dirname));
b6c903
-      if (buf == NULL)
b6c903
-	continue;
b6c903
-
b6c903
-      char *cp = __mempcpy (__mempcpy (buf, elem, elem_len),
b6c903
-			    gconv_conf_filename, sizeof (gconv_conf_filename));
b6c903
-
b6c903
-      /* Read the gconv-modules configuration file first.  */
b6c903
-      read_conf_file (buf, elem, elem_len);
b6c903
-
b6c903
-      /* Next, see if there is a gconv-modules.d directory containing
b6c903
-	 configuration files and if it is non-empty.  */
b6c903
-      cp--;
b6c903
-      cp[0] = '.';
b6c903
-      cp[1] = 'd';
b6c903
-      cp[2] = '\0';
b6c903
-
b6c903
-      DIR *confdir = __opendir (buf);
b6c903
-      if (confdir != NULL)
b6c903
-	{
b6c903
-	  struct dirent *ent;
b6c903
-	  while ((ent = __readdir (confdir)) != NULL)
b6c903
-	    {
b6c903
-	      if (ent->d_type != DT_REG)
b6c903
-		continue;
b6c903
-
b6c903
-	      size_t len = strlen (ent->d_name);
b6c903
-	      const char *suffix = ".conf";
b6c903
-
b6c903
-	      if (len > strlen (suffix)
b6c903
-		  && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
b6c903
-		{
b6c903
-		  char *conf;
b6c903
-		  if (__asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
b6c903
-		    continue;
b6c903
-		  read_conf_file (conf, elem, elem_len);
b6c903
-		  free (conf);
b6c903
-		}
b6c903
-	    }
b6c903
-	  __closedir (confdir);
b6c903
-	}
b6c903
-      free (buf);
b6c903
-    }
b6c903
+    gconv_parseconfdir (__gconv_path_elem[cnt].name,
b6c903
+			__gconv_path_elem[cnt].len);
b6c903
 #endif
b6c903
 
b6c903
   /* Add the internal modules.  */
b6c903
diff --git a/iconv/gconv_parseconfdir.h b/iconv/gconv_parseconfdir.h
b6c903
new file mode 100644
b6c903
index 0000000000000000..3d4d58d4be10a250
b6c903
--- /dev/null
b6c903
+++ b/iconv/gconv_parseconfdir.h
b6c903
@@ -0,0 +1,161 @@
b6c903
+/* Handle configuration data.
b6c903
+   Copyright (C) 2021 Free Software Foundation, Inc.
b6c903
+   This file is part of the GNU C Library.
b6c903
+
b6c903
+   The GNU C Library is free software; you can redistribute it and/or
b6c903
+   modify it under the terms of the GNU Lesser General Public
b6c903
+   License as published by the Free Software Foundation; either
b6c903
+   version 2.1 of the License, or (at your option) any later version.
b6c903
+
b6c903
+   The GNU C Library is distributed in the hope that it will be useful,
b6c903
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
b6c903
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b6c903
+   Lesser General Public License for more details.
b6c903
+
b6c903
+   You should have received a copy of the GNU Lesser General Public
b6c903
+   License along with the GNU C Library; if not, see
b6c903
+   <https://www.gnu.org/licenses/>.  */
b6c903
+
b6c903
+#include <dirent.h>
b6c903
+#include <libc-symbols.h>
b6c903
+#include <locale.h>
b6c903
+#include <sys/types.h>
b6c903
+
b6c903
+#if IS_IN (libc)
b6c903
+# include <libio/libioP.h>
b6c903
+# define __getdelim(line, len, c, fp) _IO_getdelim (line, len, c, fp)
b6c903
+
b6c903
+# undef isspace
b6c903
+# define isspace(__c) __isspace_l ((__c), _nl_C_locobj_ptr)
b6c903
+# define asprintf __asprintf
b6c903
+# define opendir __opendir
b6c903
+# define readdir __readdir
b6c903
+# define closedir __closedir
b6c903
+# define mempcpy __mempcpy
b6c903
+#endif
b6c903
+
b6c903
+/* Name of the file containing the module information in the directories
b6c903
+   along the path.  */
b6c903
+static const char gconv_conf_filename[] = "gconv-modules";
b6c903
+static const char gconv_conf_dirname[] = "gconv-modules.d";
b6c903
+
b6c903
+static void add_alias (char *);
b6c903
+static void add_module (char *, const char *, size_t, int);
b6c903
+
b6c903
+/* Read the next configuration file.  */
b6c903
+static bool
b6c903
+read_conf_file (const char *filename, const char *directory, size_t dir_len)
b6c903
+{
b6c903
+  /* Note the file is opened with cancellation in the I/O functions
b6c903
+     disabled.  */
b6c903
+  FILE *fp = fopen (filename, "rce");
b6c903
+  char *line = NULL;
b6c903
+  size_t line_len = 0;
b6c903
+  static int modcounter;
b6c903
+
b6c903
+  /* Don't complain if a file is not present or readable, simply silently
b6c903
+     ignore it.  */
b6c903
+  if (fp == NULL)
b6c903
+    return false;
b6c903
+
b6c903
+  /* No threads reading from this stream.  */
b6c903
+  __fsetlocking (fp, FSETLOCKING_BYCALLER);
b6c903
+
b6c903
+  /* Process the known entries of the file.  Comments start with `#' and
b6c903
+     end with the end of the line.  Empty lines are ignored.  */
b6c903
+  while (!__feof_unlocked (fp))
b6c903
+    {
b6c903
+      char *rp, *endp, *word;
b6c903
+      ssize_t n = __getdelim (&line, &line_len, '\n', fp);
b6c903
+      if (n < 0)
b6c903
+	/* An error occurred.  */
b6c903
+	break;
b6c903
+
b6c903
+      rp = line;
b6c903
+      /* Terminate the line (excluding comments or newline) by an NUL byte
b6c903
+	 to simplify the following code.  */
b6c903
+      endp = strchr (rp, '#');
b6c903
+      if (endp != NULL)
b6c903
+	*endp = '\0';
b6c903
+      else
b6c903
+	if (rp[n - 1] == '\n')
b6c903
+	  rp[n - 1] = '\0';
b6c903
+
b6c903
+      while (isspace (*rp))
b6c903
+	++rp;
b6c903
+
b6c903
+      /* If this is an empty line go on with the next one.  */
b6c903
+      if (rp == endp)
b6c903
+	continue;
b6c903
+
b6c903
+      word = rp;
b6c903
+      while (*rp != '\0' && !isspace (*rp))
b6c903
+	++rp;
b6c903
+
b6c903
+      if (rp - word == sizeof ("alias") - 1
b6c903
+	  && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
b6c903
+	add_alias (rp);
b6c903
+      else if (rp - word == sizeof ("module") - 1
b6c903
+	       && memcmp (word, "module", sizeof ("module") - 1) == 0)
b6c903
+	add_module (rp, directory, dir_len, modcounter++);
b6c903
+      /* else */
b6c903
+	/* Otherwise ignore the line.  */
b6c903
+    }
b6c903
+
b6c903
+  free (line);
b6c903
+
b6c903
+  fclose (fp);
b6c903
+  return true;
b6c903
+}
b6c903
+
b6c903
+static __always_inline bool
b6c903
+gconv_parseconfdir (const char *dir, size_t dir_len)
b6c903
+{
b6c903
+  /* No slash needs to be inserted between dir and gconv_conf_filename;
b6c903
+     dir already ends in a slash.  */
b6c903
+  char *buf = malloc (dir_len + sizeof (gconv_conf_dirname));
b6c903
+  bool found = false;
b6c903
+
b6c903
+  if (buf == NULL)
b6c903
+    return false;
b6c903
+
b6c903
+  char *cp = mempcpy (mempcpy (buf, dir, dir_len), gconv_conf_filename,
b6c903
+		      sizeof (gconv_conf_filename));
b6c903
+
b6c903
+  /* Read the gconv-modules configuration file first.  */
b6c903
+  found = read_conf_file (buf, dir, dir_len);
b6c903
+
b6c903
+  /* Next, see if there is a gconv-modules.d directory containing
b6c903
+     configuration files and if it is non-empty.  */
b6c903
+  cp--;
b6c903
+  cp[0] = '.';
b6c903
+  cp[1] = 'd';
b6c903
+  cp[2] = '\0';
b6c903
+
b6c903
+  DIR *confdir = opendir (buf);
b6c903
+  if (confdir != NULL)
b6c903
+    {
b6c903
+      struct dirent *ent;
b6c903
+      while ((ent = readdir (confdir)) != NULL)
b6c903
+	{
b6c903
+	  if (ent->d_type != DT_REG)
b6c903
+	    continue;
b6c903
+
b6c903
+	  size_t len = strlen (ent->d_name);
b6c903
+	  const char *suffix = ".conf";
b6c903
+
b6c903
+	  if (len > strlen (suffix)
b6c903
+	      && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
b6c903
+	    {
b6c903
+	      char *conf;
b6c903
+	      if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
b6c903
+		continue;
b6c903
+	      found |= read_conf_file (conf, dir, dir_len);
b6c903
+	      free (conf);
b6c903
+	    }
b6c903
+	}
b6c903
+      closedir (confdir);
b6c903
+    }
b6c903
+  free (buf);
b6c903
+  return found;
b6c903
+}