license_exists.c 4.4 KB

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