arch.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * arch.c - architecture database functions
  4. *
  5. * Copyright © 2011 Linaro Limited
  6. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  7. * Copyright © 2011-2014 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <assert.h>
  25. #include <limits.h>
  26. #include <string.h>
  27. #include <stdbool.h>
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <dpkg/i18n.h>
  31. #include <dpkg/c-ctype.h>
  32. #include <dpkg/ehandle.h>
  33. #include <dpkg/dpkg.h>
  34. #include <dpkg/dpkg-db.h>
  35. #include <dpkg/dir.h>
  36. #include <dpkg/varbuf.h>
  37. #include <dpkg/arch.h>
  38. #define DPKG_DB_ARCH_FILE "arch"
  39. /**
  40. * Verify if the architecture name is valid.
  41. *
  42. * Returns NULL if the architecture name is valid. Otherwise it returns a
  43. * string describing why it's not valid. Currently it ensures the name
  44. * starts with an alphanumeric and is then composed of a combinations of
  45. * hyphens and alphanumerics.
  46. *
  47. * The function will abort if you pass it a NULL pointer.
  48. *
  49. * @param name The architecture name to verify.
  50. */
  51. const char *
  52. dpkg_arch_name_is_illegal(const char *name)
  53. {
  54. static char buf[150];
  55. const char *p = name;
  56. assert(name);
  57. if (!*p)
  58. return _("may not be empty string");
  59. if (!c_isalnum(*p))
  60. return _("must start with an alphanumeric");
  61. while (*++p != '\0')
  62. if (!c_isalnum(*p) && *p != '-')
  63. break;
  64. if (*p == '\0')
  65. return NULL;
  66. snprintf(buf, sizeof(buf), _("character '%c' not allowed (only "
  67. "letters, digits and characters '%s')"),
  68. *p, "-");
  69. return buf;
  70. }
  71. /* This is a special architecture used to guarantee we always have a valid
  72. * structure to handle. */
  73. static struct dpkg_arch arch_item_none = {
  74. .name = "",
  75. .type = DPKG_ARCH_NONE,
  76. .next = NULL,
  77. };
  78. static struct dpkg_arch arch_item_empty = {
  79. .name = "",
  80. .type = DPKG_ARCH_EMPTY,
  81. .next = NULL,
  82. };
  83. static struct dpkg_arch arch_item_any = {
  84. .name = "any",
  85. .type = DPKG_ARCH_WILDCARD,
  86. .next = NULL,
  87. };
  88. static struct dpkg_arch arch_item_all = {
  89. .name = "all",
  90. .type = DPKG_ARCH_ALL,
  91. .next = &arch_item_any,
  92. };
  93. static struct dpkg_arch arch_item_native = {
  94. .name = ARCHITECTURE,
  95. .type = DPKG_ARCH_NATIVE,
  96. .next = &arch_item_all,
  97. };
  98. static struct dpkg_arch *arch_head = &arch_item_native;
  99. static struct dpkg_arch *arch_builtin_tail = &arch_item_any;
  100. static bool arch_list_dirty;
  101. static struct dpkg_arch *
  102. dpkg_arch_new(const char *name, enum dpkg_arch_type type)
  103. {
  104. struct dpkg_arch *new;
  105. new = nfmalloc(sizeof(*new));
  106. new->next = NULL;
  107. new->name = nfstrsave(name);
  108. new->type = type;
  109. return new;
  110. }
  111. /**
  112. * Retrieve the struct dpkg_arch for the given architecture.
  113. *
  114. * Create a new structure for the architecture if it's not yet known from
  115. * the system, in that case it will have arch->type == arch_unknown, if the
  116. * architecture is illegal it will have arch->type == arch_illegal and if
  117. * name is NULL or an empty string then it will have arch->type == arch_none.
  118. *
  119. * @param name The architecture name.
  120. */
  121. struct dpkg_arch *
  122. dpkg_arch_find(const char *name)
  123. {
  124. struct dpkg_arch *arch, *last_arch = NULL;
  125. enum dpkg_arch_type type;
  126. if (name == NULL)
  127. return &arch_item_none;
  128. if (name[0] == '\0')
  129. return &arch_item_empty;
  130. for (arch = arch_head; arch; arch = arch->next) {
  131. if (strcmp(arch->name, name) == 0)
  132. return arch;
  133. last_arch = arch;
  134. }
  135. if (dpkg_arch_name_is_illegal(name))
  136. type = DPKG_ARCH_ILLEGAL;
  137. else
  138. type = DPKG_ARCH_UNKNOWN;
  139. arch = dpkg_arch_new(name, type);
  140. last_arch->next = arch;
  141. return arch;
  142. }
  143. /**
  144. * Return the struct dpkg_arch corresponding to the architecture type.
  145. *
  146. * The function only returns instances for types which are unique. For
  147. * forward-compatibility any unknown type will return NULL.
  148. */
  149. struct dpkg_arch *
  150. dpkg_arch_get(enum dpkg_arch_type type)
  151. {
  152. switch (type) {
  153. case DPKG_ARCH_NONE:
  154. return &arch_item_none;
  155. case DPKG_ARCH_EMPTY:
  156. return &arch_item_empty;
  157. case DPKG_ARCH_WILDCARD:
  158. return &arch_item_any;
  159. case DPKG_ARCH_ALL:
  160. return &arch_item_all;
  161. case DPKG_ARCH_NATIVE:
  162. return &arch_item_native;
  163. case DPKG_ARCH_ILLEGAL:
  164. case DPKG_ARCH_FOREIGN:
  165. case DPKG_ARCH_UNKNOWN:
  166. internerr("architecture type %d is not unique", type);
  167. default:
  168. /* Ignore unknown types for forward-compatibility. */
  169. return NULL;
  170. }
  171. }
  172. /**
  173. * Return the complete list of architectures.
  174. *
  175. * In fact it returns the first item of the linked list and you can
  176. * traverse the list by following arch->next until it's NULL.
  177. */
  178. struct dpkg_arch *
  179. dpkg_arch_get_list(void)
  180. {
  181. return arch_head;
  182. }
  183. /**
  184. * Reset the list of architectures.
  185. *
  186. * Must be called before nffreeall() to ensure we don't point to
  187. * unallocated memory.
  188. */
  189. void
  190. dpkg_arch_reset_list(void)
  191. {
  192. arch_builtin_tail->next = NULL;
  193. arch_list_dirty = false;
  194. }
  195. void
  196. varbuf_add_archqual(struct varbuf *vb, const struct dpkg_arch *arch)
  197. {
  198. if (arch->type == DPKG_ARCH_NONE)
  199. return;
  200. if (arch->type == DPKG_ARCH_EMPTY)
  201. return;
  202. varbuf_add_char(vb, ':');
  203. varbuf_add_str(vb, arch->name);
  204. }
  205. /**
  206. * Return a descriptive architecture name.
  207. */
  208. const char *
  209. dpkg_arch_describe(const struct dpkg_arch *arch)
  210. {
  211. if (arch->type == DPKG_ARCH_NONE)
  212. return C_("architecture", "<none>");
  213. if (arch->type == DPKG_ARCH_EMPTY)
  214. return C_("architecture", "<empty>");
  215. return arch->name;
  216. }
  217. /**
  218. * Add a new foreign dpkg_arch architecture.
  219. */
  220. struct dpkg_arch *
  221. dpkg_arch_add(const char *name)
  222. {
  223. struct dpkg_arch *arch;
  224. arch = dpkg_arch_find(name);
  225. if (arch->type == DPKG_ARCH_UNKNOWN) {
  226. arch->type = DPKG_ARCH_FOREIGN;
  227. arch_list_dirty = true;
  228. }
  229. return arch;
  230. }
  231. /**
  232. * Unmark a foreign dpkg_arch architecture.
  233. */
  234. void
  235. dpkg_arch_unmark(struct dpkg_arch *arch_remove)
  236. {
  237. struct dpkg_arch *arch;
  238. for (arch = arch_builtin_tail->next; arch; arch = arch->next) {
  239. if (arch->type != DPKG_ARCH_FOREIGN)
  240. continue;
  241. if (arch == arch_remove) {
  242. arch->type = DPKG_ARCH_UNKNOWN;
  243. arch_list_dirty = true;
  244. return;
  245. }
  246. }
  247. }
  248. /**
  249. * Load the architecture database.
  250. */
  251. void
  252. dpkg_arch_load_list(void)
  253. {
  254. FILE *fp;
  255. char *archfile;
  256. char archname[_POSIX2_LINE_MAX];
  257. archfile = dpkg_db_get_path(DPKG_DB_ARCH_FILE);
  258. fp = fopen(archfile, "r");
  259. if (fp == NULL) {
  260. arch_list_dirty = true;
  261. free(archfile);
  262. return;
  263. }
  264. while (fgets_checked(archname, sizeof(archname), fp, archfile) >= 0)
  265. dpkg_arch_add(archname);
  266. free(archfile);
  267. fclose(fp);
  268. }
  269. /**
  270. * Save the architecture database.
  271. */
  272. void
  273. dpkg_arch_save_list(void)
  274. {
  275. struct atomic_file *file;
  276. struct dpkg_arch *arch;
  277. char *archfile;
  278. if (!arch_list_dirty)
  279. return;
  280. archfile = dpkg_db_get_path(DPKG_DB_ARCH_FILE);
  281. file = atomic_file_new(archfile, 0);
  282. atomic_file_open(file);
  283. for (arch = arch_head; arch; arch = arch->next) {
  284. if (arch->type != DPKG_ARCH_FOREIGN &&
  285. arch->type != DPKG_ARCH_NATIVE)
  286. continue;
  287. if (fprintf(file->fp, "%s\n", arch->name) < 0)
  288. ohshite(_("error writing to architecture list"));
  289. }
  290. atomic_file_sync(file);
  291. atomic_file_close(file);
  292. atomic_file_commit(file);
  293. atomic_file_free(file);
  294. dir_sync_path(dpkg_db_get_dir());
  295. arch_list_dirty = false;
  296. free(archfile);
  297. }