license_exists.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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, struct score *score)
  81. {
  82. char buf[PATH_MAX];
  83. ssize_t len;
  84. char *license = path_join(m, m->dir, "LICENSE");
  85. const char *expected;
  86. struct doc_section *d;
  87. const char *prefix = link_prefix(m);
  88. d = find_license_tag(m);
  89. if (!d) {
  90. score->error = tal_strdup(score, "No License: tag in _info");
  91. return;
  92. }
  93. m->license = which_license(d);
  94. if (m->license == LICENSE_UNKNOWN) {
  95. score_file_error(score, m->info_file, d->srcline,
  96. "WARNING: unknown License: in _info: %s",
  97. d->lines[0]);
  98. /* FIXME: For historical reasons, don't fail here. */
  99. score->pass = true;
  100. return;
  101. }
  102. /* If they have a license tag at all, we pass. */
  103. score->pass = true;
  104. expected = expected_link(m, prefix, m->license);
  105. len = readlink(license, buf, sizeof(buf));
  106. if (len < 0) {
  107. /* Could be a real file... OK if not a standard license. */
  108. if (errno == EINVAL) {
  109. if (!expected) {
  110. score->score = score->total;
  111. return;
  112. }
  113. score->error
  114. = tal_fmt(score,
  115. "License in _info is '%s',"
  116. " expect LICENSE symlink '%s'",
  117. d->lines[0], expected);
  118. return;
  119. }
  120. if (errno == ENOENT) {
  121. /* Public domain doesn't really need a file. */
  122. if (m->license == LICENSE_PUBLIC_DOMAIN) {
  123. score->score = score->total;
  124. return;
  125. }
  126. score->error = tal_strdup(score,
  127. "LICENSE does not exist");
  128. if (expected)
  129. license_exists.handle = handle_license_link;
  130. return;
  131. }
  132. err(1, "readlink on %s", license);
  133. }
  134. if (len >= sizeof(buf))
  135. errx(1, "Reading symlink %s gave huge result", license);
  136. buf[len] = '\0';
  137. if (!strstarts(buf, prefix)) {
  138. score->error = tal_fmt(score,
  139. "Expected symlink into %s not %s",
  140. prefix, buf);
  141. return;
  142. }
  143. if (!expected) {
  144. score->error = tal_fmt(score,
  145. "License in _info is unknown '%s',"
  146. " but LICENSE symlink is '%s'",
  147. d->lines[0], buf);
  148. return;
  149. }
  150. if (!streq(buf, expected)) {
  151. score->error = tal_fmt(score,
  152. "Expected symlink to %s not %s",
  153. expected, buf);
  154. return;
  155. }
  156. score->pass = true;
  157. score->score = score->total;
  158. }
  159. struct ccanlint license_exists = {
  160. .key = "license_exists",
  161. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  162. .check = check_has_license,
  163. .needs = "info_exists"
  164. };
  165. REGISTER_TEST(license_exists);