license_exists.c 4.2 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. REGISTER_TEST(license_exists);
  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-MIT")
  51. || streq(d->lines[0], "MIT"))
  52. return "../../licenses/BSD-MIT";
  53. if (streq(d->lines[0], "BSD (3 clause)"))
  54. return "../../licenses/BSD-3CLAUSE";
  55. return NULL;
  56. }
  57. static void handle_license_link(struct manifest *m, struct score *score)
  58. {
  59. const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
  60. struct doc_section *d = find_license(m);
  61. const char *ldest = expected_link(m, d);
  62. char *q;
  63. printf(
  64. "Most modules want a copy of their license, so usually we create a\n"
  65. "LICENSE symlink into ../../licenses to avoid too many copies.\n");
  66. /* FIXME: make ask printf-like */
  67. q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
  68. ldest, d->lines[0]);
  69. if (ask(q)) {
  70. if (symlink(ldest, link) != 0)
  71. err(1, "Creating symlink %s -> %s", link, ldest);
  72. }
  73. }
  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. expected = expected_link(m, d);
  89. len = readlink(license, buf, sizeof(buf));
  90. if (len < 0) {
  91. /* Could be a real file... OK if not a standard license. */
  92. if (errno == EINVAL) {
  93. if (!expected) {
  94. score->pass = true;
  95. return;
  96. }
  97. score->error
  98. = talloc_asprintf(score,
  99. "License in _info is '%s',"
  100. " expect LICENSE symlink '%s'",
  101. d->lines[0], expected);
  102. return;
  103. }
  104. if (errno == ENOENT) {
  105. score->error = talloc_strdup(score,
  106. "LICENSE does not exist");
  107. if (expected)
  108. license_exists.handle = handle_license_link;
  109. return;
  110. }
  111. err(1, "readlink on %s", license);
  112. }
  113. if (len >= sizeof(buf))
  114. errx(1, "Reading symlink %s gave huge result", license);
  115. buf[len] = '\0';
  116. if (!strstarts(buf, "../../licenses/")) {
  117. score->error = talloc_asprintf(score,
  118. "Expected symlink to"
  119. " ../../licenses/..."
  120. " not %s", buf);
  121. return;
  122. }
  123. if (!expected) {
  124. score->error = talloc_asprintf(score,
  125. "License in _info is unknown '%s',"
  126. " but LICENSE symlink is '%s'",
  127. d->lines[0], buf);
  128. return;
  129. }
  130. if (!streq(buf, expected)) {
  131. score->error = talloc_asprintf(score,
  132. "Expected symlink to %s not %s",
  133. expected, buf);
  134. return;
  135. }
  136. score->pass = true;
  137. score->score = score->total;
  138. }
  139. struct ccanlint license_exists = {
  140. .key = "license_exists",
  141. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  142. .check = check_has_license,
  143. .needs = "info_exists"
  144. };