license_exists.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 struct doc_section *find_license(const struct manifest *m)
  14. {
  15. struct doc_section *d;
  16. list_for_each(m->info_file->doc_sections, d, list) {
  17. if (!streq(d->function, m->basename))
  18. continue;
  19. if (streq(d->type, "license"))
  20. return d;
  21. }
  22. return NULL;
  23. }
  24. static const char *expected_link(const struct manifest *m,
  25. struct doc_section *d)
  26. {
  27. if (streq(d->lines[0], "GPL")
  28. || streq(d->lines[0], "GPLv3")
  29. || streq(d->lines[0], "GPLv3 or later")
  30. || streq(d->lines[0], "GPLv3 (or later)")
  31. || streq(d->lines[0], "GPL (3 or any later version)"))
  32. return "../../licenses/GPL-3";
  33. if (streq(d->lines[0], "GPLv2")
  34. || streq(d->lines[0], "GPLv2 or later")
  35. || streq(d->lines[0], "GPLv2 (or later)")
  36. || streq(d->lines[0], "GPL (2 or any later version)"))
  37. return "../../licenses/GPL-3";
  38. if (streq(d->lines[0], "LGPL")
  39. || streq(d->lines[0], "LGPLv3")
  40. || streq(d->lines[0], "LGPLv3 or later")
  41. || streq(d->lines[0], "LGPLv3 (or later)")
  42. || streq(d->lines[0], "LGPL (3 or any later version)"))
  43. return "../../licenses/LGPL-3";
  44. if (streq(d->lines[0], "LGPLv2")
  45. || streq(d->lines[0], "LGPLv2 or later")
  46. || streq(d->lines[0], "LGPLv2 (or later)")
  47. || streq(d->lines[0], "LGPL (2 or any later version)"))
  48. return "../../licenses/LGPL-2.1";
  49. if (streq(d->lines[0], "BSD-MIT")
  50. || streq(d->lines[0], "MIT"))
  51. return "../../licenses/BSD-MIT";
  52. if (streq(d->lines[0], "BSD (3 clause)"))
  53. return "../../licenses/BSD-3CLAUSE";
  54. return NULL;
  55. }
  56. static void handle_license_link(struct manifest *m, struct score *score)
  57. {
  58. const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
  59. struct doc_section *d = find_license(m);
  60. const char *ldest = expected_link(m, d);
  61. char *q;
  62. printf(
  63. "Most modules want a copy of their license, so usually we create a\n"
  64. "LICENSE symlink into ../../licenses to avoid too many copies.\n");
  65. /* FIXME: make ask printf-like */
  66. q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
  67. ldest, d->lines[0]);
  68. if (ask(q)) {
  69. if (symlink(ldest, link) != 0)
  70. err(1, "Creating symlink %s -> %s", link, ldest);
  71. }
  72. }
  73. extern struct ccanlint license_exists;
  74. static void check_has_license(struct manifest *m,
  75. bool keep,
  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. d = find_license(m);
  84. if (!d) {
  85. score->error = talloc_strdup(score, "No License: tag in _info");
  86. return;
  87. }
  88. /* If they have a license tag at all, we pass. */
  89. score->pass = true;
  90. expected = expected_link(m, d);
  91. len = readlink(license, buf, sizeof(buf));
  92. if (len < 0) {
  93. /* Could be a real file... OK if not a standard license. */
  94. if (errno == EINVAL) {
  95. if (!expected) {
  96. score->pass = true;
  97. return;
  98. }
  99. score->error
  100. = talloc_asprintf(score,
  101. "License in _info is '%s',"
  102. " expect LICENSE symlink '%s'",
  103. d->lines[0], expected);
  104. return;
  105. }
  106. if (errno == ENOENT) {
  107. score->error = talloc_strdup(score,
  108. "LICENSE does not exist");
  109. if (expected)
  110. license_exists.handle = handle_license_link;
  111. return;
  112. }
  113. err(1, "readlink on %s", license);
  114. }
  115. if (len >= sizeof(buf))
  116. errx(1, "Reading symlink %s gave huge result", license);
  117. buf[len] = '\0';
  118. if (!strstarts(buf, "../../licenses/")) {
  119. score->error = talloc_asprintf(score,
  120. "Expected symlink to"
  121. " ../../licenses/..."
  122. " not %s", buf);
  123. return;
  124. }
  125. if (!expected) {
  126. score->error = talloc_asprintf(score,
  127. "License in _info is unknown '%s',"
  128. " but LICENSE symlink is '%s'",
  129. d->lines[0], buf);
  130. return;
  131. }
  132. if (!streq(buf, expected)) {
  133. score->error = talloc_asprintf(score,
  134. "Expected symlink to %s not %s",
  135. expected, buf);
  136. return;
  137. }
  138. score->pass = true;
  139. score->score = score->total;
  140. }
  141. struct ccanlint license_exists = {
  142. .key = "license_exists",
  143. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  144. .check = check_has_license,
  145. .needs = "info_exists"
  146. };
  147. REGISTER_TEST(license_exists);