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