license.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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")
  50. || streq(d->lines[0], "MIT"))
  51. return "../../licenses/BSD-MIT";
  52. return NULL;
  53. }
  54. static void handle_license_link(struct manifest *m, struct score *score)
  55. {
  56. const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
  57. struct doc_section *d = find_license(m);
  58. const char *ldest = expected_link(m, d);
  59. char *q;
  60. printf(
  61. "Most modules want a copy of their license, so usually we create a\n"
  62. "LICENSE symlink into ../../licenses to avoid too many copies.\n");
  63. /* FIXME: make ask printf-like */
  64. q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
  65. ldest, d->lines[0]);
  66. if (ask(q)) {
  67. if (symlink(ldest, link) != 0)
  68. err(1, "Creating symlink %s -> %s", link, ldest);
  69. }
  70. }
  71. static void check_has_license(struct manifest *m,
  72. bool keep,
  73. unsigned int *timeleft, struct score *score)
  74. {
  75. char buf[PATH_MAX];
  76. ssize_t len;
  77. char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
  78. const char *expected;
  79. struct doc_section *d;
  80. d = find_license(m);
  81. if (!d) {
  82. score->error = "No License: tag in _info";
  83. return;
  84. }
  85. expected = expected_link(m, d);
  86. len = readlink(license, buf, sizeof(buf));
  87. if (len < 0) {
  88. /* Could be a real file... OK if not a standard license. */
  89. if (errno == EINVAL) {
  90. if (!expected) {
  91. score->pass = true;
  92. return;
  93. }
  94. score->error
  95. = talloc_asprintf(score,
  96. "License in _info is '%s',"
  97. " expect LICENSE symlink '%s'",
  98. d->lines[0], expected);
  99. return;
  100. }
  101. if (errno == ENOENT) {
  102. score->error = "LICENSE does not exist";
  103. if (expected)
  104. has_license.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 has_license = {
  136. .key = "has-license",
  137. .name = "Module has license",
  138. .check = check_has_license,
  139. };
  140. REGISTER_TEST(has_license, &has_info, NULL);