strlist.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * (c) 2009 Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Licensed under the GPLv2.
  5. */
  6. #include "strlist.h"
  7. #include "util.h"
  8. #include <errno.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. static
  13. struct rb_node *strlist__node_new(struct rblist *rblist, const void *entry)
  14. {
  15. const char *s = entry;
  16. struct rb_node *rc = NULL;
  17. struct strlist *strlist = container_of(rblist, struct strlist, rblist);
  18. struct str_node *snode = malloc(sizeof(*snode));
  19. if (snode != NULL) {
  20. if (strlist->dupstr) {
  21. s = strdup(s);
  22. if (s == NULL)
  23. goto out_delete;
  24. }
  25. snode->s = s;
  26. rc = &snode->rb_node;
  27. }
  28. return rc;
  29. out_delete:
  30. free(snode);
  31. return NULL;
  32. }
  33. static void str_node__delete(struct str_node *snode, bool dupstr)
  34. {
  35. if (dupstr)
  36. zfree((char **)&snode->s);
  37. free(snode);
  38. }
  39. static
  40. void strlist__node_delete(struct rblist *rblist, struct rb_node *rb_node)
  41. {
  42. struct strlist *slist = container_of(rblist, struct strlist, rblist);
  43. struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
  44. str_node__delete(snode, slist->dupstr);
  45. }
  46. static int strlist__node_cmp(struct rb_node *rb_node, const void *entry)
  47. {
  48. const char *str = entry;
  49. struct str_node *snode = container_of(rb_node, struct str_node, rb_node);
  50. return strcmp(snode->s, str);
  51. }
  52. int strlist__add(struct strlist *slist, const char *new_entry)
  53. {
  54. return rblist__add_node(&slist->rblist, new_entry);
  55. }
  56. int strlist__load(struct strlist *slist, const char *filename)
  57. {
  58. char entry[1024];
  59. int err;
  60. FILE *fp = fopen(filename, "r");
  61. if (fp == NULL)
  62. return -errno;
  63. while (fgets(entry, sizeof(entry), fp) != NULL) {
  64. const size_t len = strlen(entry);
  65. if (len == 0)
  66. continue;
  67. entry[len - 1] = '\0';
  68. err = strlist__add(slist, entry);
  69. if (err != 0)
  70. goto out;
  71. }
  72. err = 0;
  73. out:
  74. fclose(fp);
  75. return err;
  76. }
  77. void strlist__remove(struct strlist *slist, struct str_node *snode)
  78. {
  79. rblist__remove_node(&slist->rblist, &snode->rb_node);
  80. }
  81. struct str_node *strlist__find(struct strlist *slist, const char *entry)
  82. {
  83. struct str_node *snode = NULL;
  84. struct rb_node *rb_node = rblist__find(&slist->rblist, entry);
  85. if (rb_node)
  86. snode = container_of(rb_node, struct str_node, rb_node);
  87. return snode;
  88. }
  89. static int strlist__parse_list_entry(struct strlist *slist, const char *s,
  90. const char *subst_dir)
  91. {
  92. int err;
  93. char *subst = NULL;
  94. if (strncmp(s, "file://", 7) == 0)
  95. return strlist__load(slist, s + 7);
  96. if (subst_dir) {
  97. err = -ENOMEM;
  98. if (asprintf(&subst, "%s/%s", subst_dir, s) < 0)
  99. goto out;
  100. if (access(subst, F_OK) == 0) {
  101. err = strlist__load(slist, subst);
  102. goto out;
  103. }
  104. if (slist->file_only) {
  105. err = -ENOENT;
  106. goto out;
  107. }
  108. }
  109. err = strlist__add(slist, s);
  110. out:
  111. free(subst);
  112. return err;
  113. }
  114. static int strlist__parse_list(struct strlist *slist, const char *s, const char *subst_dir)
  115. {
  116. char *sep;
  117. int err;
  118. while ((sep = strchr(s, ',')) != NULL) {
  119. *sep = '\0';
  120. err = strlist__parse_list_entry(slist, s, subst_dir);
  121. *sep = ',';
  122. if (err != 0)
  123. return err;
  124. s = sep + 1;
  125. }
  126. return *s ? strlist__parse_list_entry(slist, s, subst_dir) : 0;
  127. }
  128. struct strlist *strlist__new(const char *list, const struct strlist_config *config)
  129. {
  130. struct strlist *slist = malloc(sizeof(*slist));
  131. if (slist != NULL) {
  132. bool dupstr = true;
  133. bool file_only = false;
  134. const char *dirname = NULL;
  135. if (config) {
  136. dupstr = !config->dont_dupstr;
  137. dirname = config->dirname;
  138. file_only = config->file_only;
  139. }
  140. rblist__init(&slist->rblist);
  141. slist->rblist.node_cmp = strlist__node_cmp;
  142. slist->rblist.node_new = strlist__node_new;
  143. slist->rblist.node_delete = strlist__node_delete;
  144. slist->dupstr = dupstr;
  145. slist->file_only = file_only;
  146. if (list && strlist__parse_list(slist, list, dirname) != 0)
  147. goto out_error;
  148. }
  149. return slist;
  150. out_error:
  151. free(slist);
  152. return NULL;
  153. }
  154. void strlist__delete(struct strlist *slist)
  155. {
  156. if (slist != NULL)
  157. rblist__delete(&slist->rblist);
  158. }
  159. struct str_node *strlist__entry(const struct strlist *slist, unsigned int idx)
  160. {
  161. struct str_node *snode = NULL;
  162. struct rb_node *rb_node;
  163. rb_node = rblist__entry(&slist->rblist, idx);
  164. if (rb_node)
  165. snode = container_of(rb_node, struct str_node, rb_node);
  166. return snode;
  167. }