gconv_parseconfdir.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Handle configuration data.
  2. Copyright (C) 2021-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <dirent.h>
  16. #include <libc-symbols.h>
  17. #include <locale.h>
  18. #include <sys/types.h>
  19. #if IS_IN (libc)
  20. # include <libio/libioP.h>
  21. # define __getdelim(line, len, c, fp) __getdelim (line, len, c, fp)
  22. # undef isspace
  23. # define isspace(__c) __isspace_l ((__c), _nl_C_locobj_ptr)
  24. # define asprintf __asprintf
  25. # define opendir __opendir
  26. # define readdir64 __readdir64
  27. # define closedir __closedir
  28. # define mempcpy __mempcpy
  29. # define struct_stat64 struct __stat64_t64
  30. # define stat64_impl __stat64_time64
  31. # define feof_unlocked __feof_unlocked
  32. #else
  33. # define stat64_impl stat64
  34. # define struct_stat64 struct stat64
  35. #endif
  36. /* Name of the file containing the module information in the directories
  37. along the path. */
  38. static const char gconv_conf_filename[] = "gconv-modules";
  39. static void add_alias (char *);
  40. static void add_module (char *, const char *, size_t, int);
  41. /* Read the next configuration file. */
  42. static bool
  43. read_conf_file (const char *filename, const char *directory, size_t dir_len)
  44. {
  45. /* Note the file is opened with cancellation in the I/O functions
  46. disabled. */
  47. FILE *fp = fopen (filename, "rce");
  48. char *line = NULL;
  49. size_t line_len = 0;
  50. static int modcounter;
  51. /* Don't complain if a file is not present or readable, simply silently
  52. ignore it. */
  53. if (fp == NULL)
  54. return false;
  55. /* No threads reading from this stream. */
  56. __fsetlocking (fp, FSETLOCKING_BYCALLER);
  57. /* Process the known entries of the file. Comments start with `#' and
  58. end with the end of the line. Empty lines are ignored. */
  59. while (!feof_unlocked (fp))
  60. {
  61. char *rp, *endp, *word;
  62. ssize_t n = __getdelim (&line, &line_len, '\n', fp);
  63. if (n < 0)
  64. /* An error occurred. */
  65. break;
  66. rp = line;
  67. /* Terminate the line (excluding comments or newline) by an NUL byte
  68. to simplify the following code. */
  69. endp = strchr (rp, '#');
  70. if (endp != NULL)
  71. *endp = '\0';
  72. else
  73. if (rp[n - 1] == '\n')
  74. rp[n - 1] = '\0';
  75. while (isspace (*rp))
  76. ++rp;
  77. /* If this is an empty line go on with the next one. */
  78. if (rp == endp)
  79. continue;
  80. word = rp;
  81. while (*rp != '\0' && !isspace (*rp))
  82. ++rp;
  83. if (rp - word == sizeof ("alias") - 1
  84. && memcmp (word, "alias", sizeof ("alias") - 1) == 0)
  85. add_alias (rp);
  86. else if (rp - word == sizeof ("module") - 1
  87. && memcmp (word, "module", sizeof ("module") - 1) == 0)
  88. add_module (rp, directory, dir_len, modcounter++);
  89. /* else */
  90. /* Otherwise ignore the line. */
  91. }
  92. free (line);
  93. fclose (fp);
  94. return true;
  95. }
  96. /* Prefix DIR (with length DIR_LEN) with PREFIX if the latter is non-NULL and
  97. parse configuration in it. */
  98. static __always_inline bool
  99. gconv_parseconfdir (const char *prefix, const char *dir, size_t dir_len)
  100. {
  101. /* No slash needs to be inserted between dir and gconv_conf_filename; dir
  102. already ends in a slash. The additional 2 is to accommodate the ".d"
  103. when looking for configuration files in gconv-modules.d. */
  104. size_t buflen = dir_len + sizeof (gconv_conf_filename) + 2;
  105. char *buf = malloc (buflen + (prefix != NULL ? strlen (prefix) : 0));
  106. char *cp = buf;
  107. bool found = false;
  108. if (buf == NULL)
  109. return false;
  110. if (prefix != NULL)
  111. cp = stpcpy (cp, prefix);
  112. cp = mempcpy (mempcpy (cp, dir, dir_len), gconv_conf_filename,
  113. sizeof (gconv_conf_filename));
  114. /* Read the gconv-modules configuration file first. */
  115. found = read_conf_file (buf, dir, dir_len);
  116. /* Next, see if there is a gconv-modules.d directory containing
  117. configuration files and if it is non-empty. */
  118. cp--;
  119. cp[0] = '.';
  120. cp[1] = 'd';
  121. cp[2] = '\0';
  122. DIR *confdir = opendir (buf);
  123. if (confdir != NULL)
  124. {
  125. struct dirent64 *ent;
  126. while ((ent = readdir64 (confdir)) != NULL)
  127. {
  128. if (ent->d_type != DT_REG && ent->d_type != DT_UNKNOWN
  129. && ent->d_type != DT_LNK)
  130. continue;
  131. size_t len = strlen (ent->d_name);
  132. const char *suffix = ".conf";
  133. if (len > strlen (suffix)
  134. && strcmp (ent->d_name + len - strlen (suffix), suffix) == 0)
  135. {
  136. char *conf;
  137. struct_stat64 st;
  138. if (asprintf (&conf, "%s/%s", buf, ent->d_name) < 0)
  139. continue;
  140. if (ent->d_type != DT_UNKNOWN
  141. || (stat64_impl (conf, &st) != -1 && S_ISREG (st.st_mode)))
  142. found |= read_conf_file (conf, dir, dir_len);
  143. free (conf);
  144. }
  145. }
  146. closedir (confdir);
  147. }
  148. free (buf);
  149. return found;
  150. }