license_exists.c 4.3 KB

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