00db10
Adds --install-langs support build-locale-archive.
00db10
00db10
commit 0457f649e3fe6299efe384da13dfc923bbe65707
00db10
Author: Carlos O'Donell <carlos@systemhalted.org>
00db10
Date:   Thu Sep 17 12:24:49 2015 -0400
00db10
00db10
[snip]
00db10
    - Fix --install-langs bug which causes SIGABRT (#1262040).
00db10
00db10
commit 91764bd9ec690d4b8a886c0a3a104aac12d340d2
00db10
Author: Carlos O'Donell <carlos@systemhalted.org>
00db10
Date:   Thu Mar 5 16:05:43 2015 -0500
00db10
00db10
    Resolves: #156477
00db10
    
00db10
    - Support installing only those locales specified by the RPM macro
00db10
      %%_install_langs (#156477).
00db10
00db10
Index: glibc-2.17-c758a686/releng/build-locale-archive.c
00db10
===================================================================
00db10
--- glibc-2.17-c758a686.orig/releng/build-locale-archive.c
00db10
+++ glibc-2.17-c758a686/releng/build-locale-archive.c
00db10
@@ -8,6 +8,7 @@
00db10
 #include <stdbool.h>
00db10
 #include <stdio.h>
00db10
 #include <stdlib.h>
00db10
+#include <getopt.h>
00db10
 #include <string.h>
00db10
 #include <sys/mman.h>
00db10
 #include <sys/stat.h>
00db10
@@ -21,6 +22,7 @@ const char *alias_file = DATADIR "/local
00db10
 const char *locar_file = PREFIX "/lib/locale/locale-archive";
00db10
 const char *tmpl_file = PREFIX "/lib/locale/locale-archive.tmpl";
00db10
 const char *loc_path = PREFIX "/lib/locale/";
00db10
+/* Flags set by `--verbose` option.  */
00db10
 int be_quiet = 1;
00db10
 int verbose = 0;
00db10
 int max_locarchive_open_retry = 10;
00db10
@@ -96,7 +98,7 @@ open_tmpl_archive (struct locarhandle *a
00db10
   struct stat64 st;
00db10
   int fd;
00db10
   struct locarhead head;
00db10
-  const char *archivefname = tmpl_file;
00db10
+  const char *archivefname = ah->fname == NULL ? tmpl_file : ah->fname;
00db10
 
00db10
   /* Open the archive.  We must have exclusive write access.  */
00db10
   fd = open64 (archivefname, O_RDONLY);
00db10
@@ -116,7 +118,7 @@ open_tmpl_archive (struct locarhandle *a
00db10
   ah->mmaped = (head.sumhash_offset
00db10
 		+ head.sumhash_size * sizeof (struct sumhashent));
00db10
   if (ah->mmaped > (unsigned long) st.st_size)
00db10
-    error (EXIT_FAILURE, 0, "locale archite template file truncated");
00db10
+    error (EXIT_FAILURE, 0, "locale archive template file truncated");
00db10
   ah->mmaped = st.st_size;
00db10
   ah->reserved = st.st_size;
00db10
 
00db10
@@ -250,7 +252,10 @@ compute_data (struct locarhandle *ah, st
00db10
 }
00db10
 
00db10
 static int
00db10
-fill_archive (struct locarhandle *tmpl_ah, size_t nlist, char *list[],
00db10
+fill_archive (struct locarhandle *tmpl_ah,
00db10
+	      const char *fname,
00db10
+	      size_t install_langs_count, char *install_langs_list[],
00db10
+	      size_t nlist, char *list[],
00db10
 	      const char *primary)
00db10
 {
00db10
   struct locarhandle ah;
00db10
@@ -281,11 +286,40 @@ fill_archive (struct locarhandle *tmpl_a
00db10
   for (cnt = used = 0; cnt < head->namehash_size; ++cnt)
00db10
     if (namehashtab[cnt].locrec_offset != 0)
00db10
       {
00db10
+	char * name;
00db10
+	int i;
00db10
 	assert (used < head->namehash_used);
00db10
-	names[used].name = tmpl_ah->addr + namehashtab[cnt].name_offset;
00db10
-	names[used++].locrec
00db10
-	  = (struct locrecent *) ((char *) tmpl_ah->addr +
00db10
-				  namehashtab[cnt].locrec_offset);
00db10
+        name = tmpl_ah->addr + namehashtab[cnt].name_offset;
00db10
+        if (install_langs_count == 0)
00db10
+          {
00db10
+	    /* Always intstall the entry.  */
00db10
+            names[used].name = name;
00db10
+            names[used++].locrec
00db10
+                = (struct locrecent *) ((char *) tmpl_ah->addr +
00db10
+                                        namehashtab[cnt].locrec_offset);
00db10
+          }
00db10
+        else
00db10
+          {
00db10
+	    /* Only install the entry if the user asked for it via
00db10
+	       --install-langs.  */
00db10
+            for (i = 0; i < install_langs_count; i++)
00db10
+              {
00db10
+		/* Add one for "_" and one for the null terminator.  */
00db10
+		size_t len = strlen (install_langs_list[i]) + 2;
00db10
+		char *install_lang = (char *)xmalloc (len);
00db10
+                strcpy (install_lang, install_langs_list[i]);
00db10
+                if (strchr (install_lang, '_') == NULL)
00db10
+                  strcat (install_lang, "_");
00db10
+                if (strncmp (name, install_lang, strlen (install_lang)) == 0)
00db10
+                  {
00db10
+                    names[used].name = name;
00db10
+                    names[used++].locrec
00db10
+		      = (struct locrecent *) ((char *)tmpl_ah->addr
00db10
+					      + namehashtab[cnt].locrec_offset);
00db10
+                  }
00db10
+		free (install_lang);
00db10
+              }
00db10
+          }
00db10
       }
00db10
 
00db10
   /* Sort the names.  */
00db10
@@ -304,6 +338,9 @@ fill_archive (struct locarhandle *tmpl_a
00db10
 
00db10
   /* Open the archive.  This call never returns if we cannot
00db10
      successfully open the archive.  */
00db10
+  ah.fname = NULL;
00db10
+  if (fname != NULL)
00db10
+    ah.fname = fname;
00db10
   open_archive (&ah, false);
00db10
 
00db10
   if (primary != NULL)
00db10
@@ -532,24 +569,197 @@ fill_archive (struct locarhandle *tmpl_a
00db10
   return result;
00db10
 }
00db10
 
00db10
-int main ()
00db10
+void usage()
00db10
+{
00db10
+  printf ("\
00db10
+Usage: build-locale-archive [OPTION]... [TEMPLATE-FILE] [ARCHIVE-FILE]\n\
00db10
+ Builds a locale archive from a template file.\n\
00db10
+ Options:\n\
00db10
+  -h, --help                 Print this usage message.\n\
00db10
+  -v, --verbose              Verbose execution.\n\
00db10
+  -l, --install-langs=LIST   Only include locales given in LIST into the \n\
00db10
+                             locale archive.  LIST is a colon separated list\n\
00db10
+                             of locale prefixes, for example \"de:en:ja\".\n\
00db10
+                             The special argument \"all\" means to install\n\
00db10
+                             all languages and it must be present by itself.\n\
00db10
+                             If \"all\" is present with any other language it\n\
00db10
+                             will be treated as the name of a locale.\n\
00db10
+                             If the --install-langs option is missing, all\n\
00db10
+                             locales are installed. The colon separated list\n\
00db10
+                             can contain any strings matching the beginning of\n\
00db10
+                             locale names.\n\
00db10
+                             If a string does not contain a \"_\", it is added.\n\
00db10
+                             Examples:\n\
00db10
+                               --install-langs=\"en\"\n\
00db10
+                                 installs en_US, en_US.iso88591,\n\
00db10
+                                 en_US.iso885915, en_US.utf8,\n\
00db10
+                                 en_GB ...\n\
00db10
+                               --install-langs=\"en_US.utf8\"\n\
00db10
+                                 installs only en_US.utf8.\n\
00db10
+                               --install-langs=\"ko\"\n\
00db10
+                                 installs ko_KR, ko_KR.euckr,\n\
00db10
+                                 ko_KR.utf8 but *not* kok_IN\n\
00db10
+                                 because \"ko\" does not contain\n\
00db10
+                                 \"_\" and it is silently added\n\
00db10
+                               --install-langs\"ko:kok\"\n\
00db10
+                                 installs ko_KR, ko_KR.euckr,\n\
00db10
+                                 ko_KR.utf8, kok_IN, and\n\
00db10
+                                 kok_IN.utf8.\n\
00db10
+                               --install-langs=\"POSIX\" will\n\
00db10
+                                 installs *no* locales at all\n\
00db10
+                                 because POSIX matches none of\n\
00db10
+                                 the locales. Actually, any string\n\
00db10
+                                 matching nothing will do that.\n\
00db10
+                                 POSIX and C will always be\n\
00db10
+                                 available because they are\n\
00db10
+                                 builtin.\n\
00db10
+                             Aliases are installed as well,\n\
00db10
+                             i.e. --install-langs=\"de\"\n\
00db10
+                             will install not only every locale starting with\n\
00db10
+                             \"de\" but also the aliases \"deutsch\"\n\
00db10
+                             and and \"german\" although the latter does not\n\
00db10
+                             start with \"de\".\n\
00db10
+\n\
00db10
+  If the arguments TEMPLATE-FILE and ARCHIVE-FILE are not given the locations\n\
00db10
+  where the glibc used expects these files are used by default.\n\
00db10
+");
00db10
+}
00db10
+
00db10
+int main (int argc, char *argv[])
00db10
 {
00db10
   char path[4096];
00db10
   DIR *dirp;
00db10
   struct dirent64 *d;
00db10
   struct stat64 st;
00db10
   char *list[16384], *primary;
00db10
+  char *lang;
00db10
+  int install_langs_count = 0;
00db10
+  int i;
00db10
+  char *install_langs_arg, *ila_start;
00db10
+  char **install_langs_list;
00db10
   unsigned int cnt = 0;
00db10
   struct locarhandle tmpl_ah;
00db10
+  char *new_locar_fname = NULL;
00db10
   size_t loc_path_len = strlen (loc_path);
00db10
 
00db10
+  while (1)
00db10
+    {
00db10
+      int c;
00db10
+
00db10
+      static struct option long_options[] =
00db10
+        {
00db10
+            {"help",            no_argument,       0, 'h'},
00db10
+            {"verbose",         no_argument,       0, 'v'},
00db10
+            {"install-langs",   required_argument, 0, 'l'},
00db10
+            {0, 0, 0, 0}
00db10
+        };
00db10
+      /* getopt_long stores the option index here. */
00db10
+      int option_index = 0;
00db10
+
00db10
+      c = getopt_long (argc, argv, "vhl:",
00db10
+                       long_options, &option_index);
00db10
+
00db10
+      /* Detect the end of the options. */
00db10
+      if (c == -1)
00db10
+        break;
00db10
+
00db10
+      switch (c)
00db10
+        {
00db10
+        case 0:
00db10
+          printf ("unknown option %s", long_options[option_index].name);
00db10
+          if (optarg)
00db10
+            printf (" with arg %s", optarg);
00db10
+          printf ("\n");
00db10
+          usage ();
00db10
+          exit (1);
00db10
+
00db10
+        case 'v':
00db10
+          verbose = 1;
00db10
+          be_quiet = 0;
00db10
+          break;
00db10
+
00db10
+        case 'h':
00db10
+          usage ();
00db10
+          exit (0);
00db10
+
00db10
+        case 'l':
00db10
+          install_langs_arg = ila_start = strdup (optarg);
00db10
+          /* If the argument to --install-lang is "all", do
00db10
+             not limit the list of languages to install and install
00db10
+             them all.  We do not support installing a single locale
00db10
+	     called "all".  */
00db10
+#define MAGIC_INSTALL_ALL "all"
00db10
+          if (install_langs_arg != NULL
00db10
+	      && install_langs_arg[0] != '\0'
00db10
+	      && !(strncmp(install_langs_arg, MAGIC_INSTALL_ALL,
00db10
+			   strlen(MAGIC_INSTALL_ALL)) == 0
00db10
+		   && strlen (install_langs_arg) == 3))
00db10
+            {
00db10
+	      /* Count the number of languages we will install.  */
00db10
+              while (true)
00db10
+                {
00db10
+                  lang = strtok(install_langs_arg, ":;,");
00db10
+                  if (lang == NULL)
00db10
+                    break;
00db10
+                  install_langs_count++;
00db10
+                  install_langs_arg = NULL;
00db10
+                }
00db10
+	      free (ila_start);
00db10
+	      /* Copy the list.  */
00db10
+	      install_langs_list = (char **)xmalloc (sizeof(char *) * install_langs_count);
00db10
+	      install_langs_arg = ila_start = strdup (optarg);
00db10
+	      install_langs_count = 0;
00db10
+	      while (true)
00db10
+                {
00db10
+                  lang = strtok(install_langs_arg, ":;,");
00db10
+                  if (lang == NULL)
00db10
+                    break;
00db10
+                  install_langs_list[install_langs_count] = lang;
00db10
+		  install_langs_count++;
00db10
+                  install_langs_arg = NULL;
00db10
+                }
00db10
+            }
00db10
+          break;
00db10
+
00db10
+        case '?':
00db10
+          /* getopt_long already printed an error message. */
00db10
+          usage ();
00db10
+          exit (0);
00db10
+
00db10
+        default:
00db10
+          abort ();
00db10
+        }
00db10
+    }
00db10
+  tmpl_ah.fname = NULL;
00db10
+  if (optind < argc)
00db10
+    tmpl_ah.fname = argv[optind];
00db10
+  if (optind + 1 < argc)
00db10
+    new_locar_fname = argv[optind + 1];
00db10
+  if (verbose)
00db10
+    {
00db10
+      if (tmpl_ah.fname)
00db10
+        printf("input archive file specified on command line: %s\n",
00db10
+               tmpl_ah.fname);
00db10
+      else
00db10
+        printf("using default input archive file.\n");
00db10
+      if (new_locar_fname)
00db10
+        printf("output archive file specified on command line: %s\n",
00db10
+               new_locar_fname);
00db10
+      else
00db10
+        printf("using default output archive file.\n");
00db10
+    }
00db10
+
00db10
   dirp = opendir (loc_path);
00db10
   if (dirp == NULL)
00db10
     error (EXIT_FAILURE, errno, "cannot open directory \"%s\"", loc_path);
00db10
 
00db10
   open_tmpl_archive (&tmpl_ah);
00db10
 
00db10
-  unlink (locar_file);
00db10
+  if (new_locar_fname)
00db10
+    unlink (new_locar_fname);
00db10
+  else
00db10
+    unlink (locar_file);
00db10
+
00db10
   primary = getenv ("LC_ALL");
00db10
   if (primary == NULL)
00db10
     primary = getenv ("LANG");
00db10
@@ -560,7 +770,8 @@ int main ()
00db10
 	  && strncmp (primary, "zh", 2) != 0)
00db10
 	{
00db10
 	  char *ptr = malloc (strlen (primary) + strlen (".utf8") + 1), *p, *q;
00db10
-
00db10
+	  /* This leads to invalid locales sometimes:
00db10
+	     de_DE.iso885915@euro -> de_DE.utf8@euro */
00db10
 	  if (ptr != NULL)
00db10
 	    {
00db10
 	      p = ptr;
00db10
@@ -623,10 +834,19 @@ int main ()
00db10
       cnt++;
00db10
     }
00db10
   closedir (dirp);
00db10
-  fill_archive (&tmpl_ah, cnt, list, primary);
00db10
+  /* Store the archive to the file specified as the second argument on the
00db10
+     command line or the default locale archive.  */
00db10
+  fill_archive (&tmpl_ah, new_locar_fname,
00db10
+                install_langs_count, install_langs_list,
00db10
+                cnt, list, primary);
00db10
   close_archive (&tmpl_ah);
00db10
   truncate (tmpl_file, 0);
00db10
-  char *argv[] = { "/usr/sbin/tzdata-update", NULL };
00db10
-  execve (argv[0], (char *const *)argv, (char *const *)&argv[1]);
00db10
+  if (install_langs_count > 0)
00db10
+    {
00db10
+      free (ila_start);
00db10
+      free (install_langs_list);
00db10
+    }
00db10
+  char *tz_argv[] = { "/usr/sbin/tzdata-update", NULL };
00db10
+  execve (tz_argv[0], (char *const *)tz_argv, (char *const *)&tz_argv[1]);
00db10
   exit (0);
00db10
 }