license_exists.c 3.8 KB

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