6c0556
commit 3979c3e1bae20459d9b6d424bdb49927d9cd6fec
6c0556
Author: Siddhesh Poyarekar <siddhesh@sourceware.org>
6c0556
Date:   Mon Jun 7 14:22:18 2021 +0530
6c0556
6c0556
    iconvconfig: Read configuration from gconv-modules.d subdirectory
6c0556
    
6c0556
    In addition to GCONV_PATH/gconv-modules, also read module
6c0556
    configuration from *.conf files in GCONV_PATH/gconv-modules.d.  This
6c0556
    allows a single gconv directory to have multiple sets of gconv modules
6c0556
    but at the same time, a single modules cache.
6c0556
    
6c0556
    With this feature, one could separate the glibc supported gconv
6c0556
    modules into a minimal essential set (ISO-8859-*, UTF, etc.) from the
6c0556
    remaining modules.  In future, these could be further segregated into
6c0556
    langpack-associated sets with their own
6c0556
    gconv-modules.d/someconfig.conf.
6c0556
    
6c0556
    Reviewed-by: DJ Delorie <dj@redhat.com>
6c0556
6c0556
diff --git a/iconv/iconvconfig.c b/iconv/iconvconfig.c
6c0556
index 2b3c587bc77cfdcd..fafc686ae25fb5c1 100644
6c0556
--- a/iconv/iconvconfig.c
6c0556
+++ b/iconv/iconvconfig.c
6c0556
@@ -18,6 +18,7 @@
6c0556
 
6c0556
 #include <argp.h>
6c0556
 #include <assert.h>
6c0556
+#include <dirent.h>
6c0556
 #include <error.h>
6c0556
 #include <errno.h>
6c0556
 #include <fcntl.h>
6c0556
@@ -33,6 +34,7 @@
6c0556
 #include <string.h>
6c0556
 #include <unistd.h>
6c0556
 #include <sys/cdefs.h>
6c0556
+#include <sys/types.h>
6c0556
 #include <sys/uio.h>
6c0556
 
6c0556
 #include "iconvconfig.h"
6c0556
@@ -710,6 +712,7 @@ handle_file (const char *dir, const char *infile)
6c0556
 static int
6c0556
 handle_dir (const char *dir)
6c0556
 {
6c0556
+#define BUF_LEN prefix_len + dirlen + sizeof "gconv-modules.d"
6c0556
   char *cp;
6c0556
   size_t dirlen = strlen (dir);
6c0556
   bool found = false;
6c0556
@@ -722,20 +725,55 @@ handle_dir (const char *dir)
6c0556
       newp[dirlen] = '\0';
6c0556
     }
6c0556
 
6c0556
-  char infile[prefix_len + dirlen + sizeof "gconv-modules"];
6c0556
-  cp = infile;
6c0556
+  /* First, look for a gconv-modules file.  */
6c0556
+  char buf[BUF_LEN];
6c0556
+  cp = buf;
6c0556
   if (dir[0] == '/')
6c0556
     cp = mempcpy (cp, prefix, prefix_len);
6c0556
-  strcpy (mempcpy (cp, dir, dirlen), "gconv-modules");
6c0556
+  cp = mempcpy (cp, dir, dirlen);
6c0556
+  cp = stpcpy (cp, "gconv-modules");
6c0556
 
6c0556
-  found |= handle_file (dir, infile);
6c0556
+  found |= handle_file (dir, buf);
6c0556
+
6c0556
+  /* Next, see if there is a gconv-modules.d directory containing configuration
6c0556
+     files and if it is non-empty.  */
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
+	      /* LEN <= PATH_MAX so this alloca is not unbounded.  */
6c0556
+	      char *conf = alloca (BUF_LEN + len + 1);
6c0556
+	      cp = stpcpy (conf, buf);
6c0556
+	      sprintf (cp, "/%s", ent->d_name);
6c0556
+	      found |= handle_file (dir, conf);
6c0556
+	    }
6c0556
+	}
6c0556
+      closedir (confdir);
6c0556
+    }
6c0556
 
6c0556
   if (!found)
6c0556
     {
6c0556
-      error (0, errno, "failed to open gconv configuration file in `%s'",
6c0556
+      error (0, errno, "failed to open gconv configuration files in `%s'",
6c0556
 	     dir);
6c0556
       error (0, 0,
6c0556
-	     "ensure that the directory contains a valid gconv-modules file.");
6c0556
+	     "ensure that the directory contains either a valid "
6c0556
+	     "gconv-modules file or a gconv-modules.d directory with "
6c0556
+	     "configuration files with names ending in .conf.");
6c0556
     }
6c0556
 
6c0556
   return found ? 0 : 1;