license.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. struct ccanlint has_license;
  14. static struct doc_section *find_license(const struct manifest *m)
  15. {
  16. struct doc_section *d;
  17. list_for_each(m->info_file->doc_sections, d, list) {
  18. if (!streq(d->function, m->basename))
  19. continue;
  20. if (streq(d->type, "license"))
  21. return d;
  22. }
  23. return NULL;
  24. }
  25. static const char *expected_link(const struct manifest *m,
  26. struct doc_section *d)
  27. {
  28. if (streq(d->lines[0], "GPL")
  29. || streq(d->lines[0], "GPLv3")
  30. || streq(d->lines[0], "GPLv3 or later")
  31. || streq(d->lines[0], "GPLv3 (or later)")
  32. || streq(d->lines[0], "GPL (3 or any later version)"))
  33. return "../../licenses/GPL-3";
  34. if (streq(d->lines[0], "GPLv2")
  35. || streq(d->lines[0], "GPLv2 or later")
  36. || streq(d->lines[0], "GPLv2 (or later)")
  37. || streq(d->lines[0], "GPL (2 or any later version)"))
  38. return "../../licenses/GPL-3";
  39. if (streq(d->lines[0], "LGPL")
  40. || streq(d->lines[0], "LGPLv3")
  41. || streq(d->lines[0], "LGPLv3 or later")
  42. || streq(d->lines[0], "LGPLv3 (or later)")
  43. || streq(d->lines[0], "LGPL (3 or any later version)"))
  44. return "../../licenses/LGPL-3";
  45. if (streq(d->lines[0], "LGPLv2")
  46. || streq(d->lines[0], "LGPLv2 or later")
  47. || streq(d->lines[0], "LGPLv2 (or later)")
  48. || streq(d->lines[0], "LGPL (2 or any later version)"))
  49. return "../../licenses/LGPL-2.1";
  50. if (streq(d->lines[0], "BSD")
  51. || streq(d->lines[0], "BSD-MIT")
  52. || streq(d->lines[0], "MIT"))
  53. return "../../licenses/BSD-MIT";
  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. static void check_has_license(struct manifest *m,
  74. bool keep,
  75. unsigned int *timeleft, struct score *score)
  76. {
  77. char buf[PATH_MAX];
  78. ssize_t len;
  79. char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
  80. const char *expected;
  81. struct doc_section *d;
  82. d = find_license(m);
  83. if (!d) {
  84. score->error = "No License: tag in _info";
  85. return;
  86. }
  87. expected = expected_link(m, d);
  88. len = readlink(license, buf, sizeof(buf));
  89. if (len < 0) {
  90. /* Could be a real file... OK if not a standard license. */
  91. if (errno == EINVAL) {
  92. if (!expected) {
  93. score->pass = true;
  94. return;
  95. }
  96. score->error
  97. = talloc_asprintf(score,
  98. "License in _info is '%s',"
  99. " expect LICENSE symlink '%s'",
  100. d->lines[0], expected);
  101. return;
  102. }
  103. if (errno == ENOENT) {
  104. score->error = "LICENSE does not exist";
  105. if (expected)
  106. has_license.handle = handle_license_link;
  107. return;
  108. }
  109. err(1, "readlink on %s", license);
  110. }
  111. if (len >= sizeof(buf))
  112. errx(1, "Reading symlink %s gave huge result", license);
  113. buf[len] = '\0';
  114. if (!strstarts(buf, "../../licenses/")) {
  115. score->error = talloc_asprintf(score,
  116. "Expected symlink to"
  117. " ../../licenses/..."
  118. " not %s", buf);
  119. return;
  120. }
  121. if (!expected) {
  122. score->error = talloc_asprintf(score,
  123. "License in _info is unknown '%s',"
  124. " but LICENSE symlink is '%s'",
  125. d->lines[0], buf);
  126. return;
  127. }
  128. if (!streq(buf, expected)) {
  129. score->error = talloc_asprintf(score,
  130. "Expected symlink to %s not %s",
  131. expected, buf);
  132. return;
  133. }
  134. score->pass = true;
  135. score->score = score->total;
  136. }
  137. struct ccanlint has_license = {
  138. .key = "license_exists",
  139. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  140. .check = check_has_license,
  141. .needs = "info_exists"
  142. };
  143. REGISTER_TEST(has_license);