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