license_exists.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. unsigned int *timeleft, struct score *score)
  56. {
  57. char buf[PATH_MAX];
  58. ssize_t len;
  59. char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
  60. const char *expected;
  61. struct doc_section *d;
  62. d = find_license_tag(m);
  63. if (!d) {
  64. score->error = talloc_strdup(score, "No License: tag in _info");
  65. return;
  66. }
  67. m->license = which_license(d);
  68. if (m->license == LICENSE_UNKNOWN) {
  69. score_file_error(score, m->info_file, d->srcline,
  70. "WARNING: unknown License: in _info: %s",
  71. d->lines[0]);
  72. /* FIXME: For historical reasons, don't fail here. */
  73. score->pass = true;
  74. return;
  75. }
  76. /* If they have a license tag at all, we pass. */
  77. score->pass = true;
  78. expected = expected_link(m->license);
  79. len = readlink(license, buf, sizeof(buf));
  80. if (len < 0) {
  81. /* Could be a real file... OK if not a standard license. */
  82. if (errno == EINVAL) {
  83. if (!expected) {
  84. score->score = score->total;
  85. return;
  86. }
  87. score->error
  88. = talloc_asprintf(score,
  89. "License in _info is '%s',"
  90. " expect LICENSE symlink '%s'",
  91. d->lines[0], expected);
  92. return;
  93. }
  94. if (errno == ENOENT) {
  95. /* Public domain doesn't really need a file. */
  96. if (m->license == LICENSE_PUBLIC_DOMAIN) {
  97. score->score = score->total;
  98. return;
  99. }
  100. score->error = talloc_strdup(score,
  101. "LICENSE does not exist");
  102. if (expected)
  103. license_exists.handle = handle_license_link;
  104. return;
  105. }
  106. err(1, "readlink on %s", license);
  107. }
  108. if (len >= sizeof(buf))
  109. errx(1, "Reading symlink %s gave huge result", license);
  110. buf[len] = '\0';
  111. if (!strstarts(buf, "../../licenses/")) {
  112. score->error = talloc_asprintf(score,
  113. "Expected symlink to"
  114. " ../../licenses/..."
  115. " not %s", buf);
  116. return;
  117. }
  118. if (!expected) {
  119. score->error = talloc_asprintf(score,
  120. "License in _info is unknown '%s',"
  121. " but LICENSE symlink is '%s'",
  122. d->lines[0], buf);
  123. return;
  124. }
  125. if (!streq(buf, expected)) {
  126. score->error = talloc_asprintf(score,
  127. "Expected symlink to %s not %s",
  128. expected, buf);
  129. return;
  130. }
  131. score->pass = true;
  132. score->score = score->total;
  133. }
  134. struct ccanlint license_exists = {
  135. .key = "license_exists",
  136. .name = "Module has License: entry in _info, and LICENSE symlink/file",
  137. .check = check_has_license,
  138. .needs = "info_exists"
  139. };
  140. REGISTER_TEST(license_exists);