license_exists.c 3.9 KB

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