license_exists.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include <tools/ccanlint/ccanlint.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <limits.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <err.h>
  11. #include <ccan/talloc/talloc.h>
  12. #include <ccan/str/str.h>
  13. #include <ccan/str_talloc/str_talloc.h>
  14. static struct doc_section *find_license_tag(const struct manifest *m)
  15. {
  16. struct doc_section *d;
  17. list_for_each(m->info_file->doc_sections, d, list) {
  18. if (!streq(d->function, m->basename))
  19. continue;
  20. if (streq(d->type, "license"))
  21. return d;
  22. }
  23. return NULL;
  24. }
  25. static enum license which_license(struct doc_section *d)
  26. {
  27. if (strstarts(d->lines[0], "GPL")) {
  28. if (strchr(d->lines[0], '3'))
  29. return LICENSE_GPLv3;
  30. else if (strchr(d->lines[0], '2')) {
  31. if (strreg(NULL, d->lines[0], "or (any )?later", NULL))
  32. return LICENSE_GPLv2_PLUS;
  33. else
  34. return LICENSE_GPLv2;
  35. }
  36. return LICENSE_GPL;
  37. }
  38. if (strstarts(d->lines[0], "LGPL")) {
  39. if (strchr(d->lines[0], '3'))
  40. return LICENSE_LGPLv3;
  41. else if (strchr(d->lines[0], '2')) {
  42. if (strreg(NULL, d->lines[0], "or (any )?later", NULL))
  43. return LICENSE_LGPLv2_PLUS;
  44. else
  45. return LICENSE_LGPLv2;
  46. }
  47. return LICENSE_LGPL;
  48. }
  49. if (streq(d->lines[0], "BSD-MIT")
  50. || streq(d->lines[0], "MIT"))
  51. return LICENSE_MIT;
  52. if (streq(d->lines[0], "BSD (3 clause)"))
  53. return LICENSE_BSD;
  54. if (strreg(NULL, d->lines[0], "[Pp]ublic [Dd]omain"))
  55. return LICENSE_PUBLIC_DOMAIN;
  56. return LICENSE_UNKNOWN;
  57. }
  58. static const char *expected_link(enum license license)
  59. {
  60. switch (license) {
  61. case LICENSE_LGPLv2_PLUS:
  62. case LICENSE_LGPLv2:
  63. return "../../licenses/LGPL-2.1";
  64. case LICENSE_LGPLv3:
  65. case LICENSE_LGPL:
  66. return "../../licenses/LGPL-3";
  67. case LICENSE_GPLv2_PLUS:
  68. case LICENSE_GPLv2:
  69. return "../../licenses/GPL-2";
  70. case LICENSE_GPLv3:
  71. case LICENSE_GPL:
  72. return "../../licenses/GPL-3";
  73. case LICENSE_BSD:
  74. return "../../licenses/BSD-3CLAUSE";
  75. case LICENSE_MIT:
  76. return "../../licenses/BSD-MIT";
  77. default:
  78. return NULL;
  79. }
  80. }
  81. static void handle_license_link(struct manifest *m, struct score *score)
  82. {
  83. struct doc_section *d = find_license_tag(m);
  84. const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
  85. const char *ldest = expected_link(m->license);
  86. char *q;
  87. printf(
  88. "Most modules want a copy of their license, so usually we create a\n"
  89. "LICENSE symlink into ../../licenses to avoid too many copies.\n");
  90. /* FIXME: make ask printf-like */
  91. q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
  92. ldest, d->lines[0]);
  93. if (ask(q)) {
  94. if (symlink(ldest, link) != 0)
  95. err(1, "Creating symlink %s -> %s", link, ldest);
  96. }
  97. }
  98. extern struct ccanlint license_exists;
  99. static void check_has_license(struct manifest *m,
  100. bool keep,
  101. unsigned int *timeleft, struct score *score)
  102. {
  103. char buf[PATH_MAX];
  104. ssize_t len;
  105. char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
  106. const char *expected;
  107. struct doc_section *d;
  108. d = find_license_tag(m);
  109. if (!d) {
  110. score->error = talloc_strdup(score, "No License: tag in _info");
  111. return;
  112. }
  113. m->license = which_license(d);
  114. /* If they have a license tag at all, we pass. */
  115. score->pass = true;
  116. expected = expected_link(m->license);
  117. len = readlink(license, buf, sizeof(buf));
  118. if (len < 0) {
  119. /* Could be a real file... OK if not a standard license. */
  120. if (errno == EINVAL) {
  121. if (!expected) {
  122. score->score = score->total;
  123. return;
  124. }
  125. score->error
  126. = talloc_asprintf(score,
  127. "License in _info is '%s',"
  128. " expect LICENSE symlink '%s'",
  129. d->lines[0], expected);
  130. return;
  131. }
  132. if (errno == ENOENT) {
  133. /* Public domain doesn't really need a file. */
  134. if (m->license == LICENSE_PUBLIC_DOMAIN) {
  135. score->score = score->total;
  136. return;
  137. }
  138. score->error = talloc_strdup(score,
  139. "LICENSE does not exist");
  140. if (expected)
  141. license_exists.handle = handle_license_link;
  142. return;
  143. }
  144. err(1, "readlink on %s", license);
  145. }
  146. if (len >= sizeof(buf))
  147. errx(1, "Reading symlink %s gave huge result", license);
  148. buf[len] = '\0';
  149. if (!strstarts(buf, "../../licenses/")) {
  150. score->error = talloc_asprintf(score,
  151. "Expected symlink to"
  152. " ../../licenses/..."
  153. " not %s", buf);
  154. return;
  155. }
  156. if (!expected) {
  157. score->error = talloc_asprintf(score,
  158. "License in _info is unknown '%s',"
  159. " but LICENSE symlink is '%s'",
  160. d->lines[0], buf);
  161. return;
  162. }
  163. if (!streq(buf, expected)) {
  164. score->error = talloc_asprintf(score,
  165. "Expected symlink to %s not %s",
  166. expected, buf);
  167. return;
  168. }
  169. score->pass = true;
  170. score->score = score->total;
  171. }
  172. struct ccanlint license_exists = {
  173. .key = "license_exists",
  174. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  175. .check = check_has_license,
  176. .needs = "info_exists"
  177. };
  178. REGISTER_TEST(license_exists);