1 #include <tools/ccanlint/ccanlint.h>
11 #include <ccan/talloc/talloc.h>
12 #include <ccan/str/str.h>
14 static const char *expected_link(enum license license)
17 case LICENSE_LGPLv2_PLUS:
19 return "../../licenses/LGPL-2.1";
22 return "../../licenses/LGPL-3";
24 case LICENSE_GPLv2_PLUS:
26 return "../../licenses/GPL-2";
30 return "../../licenses/GPL-3";
33 return "../../licenses/BSD-3CLAUSE";
36 return "../../licenses/BSD-MIT";
43 static void handle_license_link(struct manifest *m, struct score *score)
45 struct doc_section *d = find_license_tag(m);
46 const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
47 const char *ldest = expected_link(m->license);
51 "Most modules want a copy of their license, so usually we create a\n"
52 "LICENSE symlink into ../../licenses to avoid too many copies.\n");
54 /* FIXME: make ask printf-like */
55 q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
58 if (symlink(ldest, link) != 0)
59 err(1, "Creating symlink %s -> %s", link, ldest);
63 extern struct ccanlint license_exists;
65 static void check_has_license(struct manifest *m,
66 unsigned int *timeleft, struct score *score)
70 char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
72 struct doc_section *d;
74 d = find_license_tag(m);
76 score->error = talloc_strdup(score, "No License: tag in _info");
80 m->license = which_license(d);
81 if (m->license == LICENSE_UNKNOWN) {
82 score_file_error(score, m->info_file, d->srcline,
83 "WARNING: unknown License: in _info: %s",
85 /* FIXME: For historical reasons, don't fail here. */
90 /* If they have a license tag at all, we pass. */
93 expected = expected_link(m->license);
95 len = readlink(license, buf, sizeof(buf));
97 /* Could be a real file... OK if not a standard license. */
98 if (errno == EINVAL) {
100 score->score = score->total;
104 = talloc_asprintf(score,
105 "License in _info is '%s',"
106 " expect LICENSE symlink '%s'",
107 d->lines[0], expected);
110 if (errno == ENOENT) {
111 /* Public domain doesn't really need a file. */
112 if (m->license == LICENSE_PUBLIC_DOMAIN) {
113 score->score = score->total;
116 score->error = talloc_strdup(score,
117 "LICENSE does not exist");
119 license_exists.handle = handle_license_link;
122 err(1, "readlink on %s", license);
124 if (len >= sizeof(buf))
125 errx(1, "Reading symlink %s gave huge result", license);
129 if (!strstarts(buf, "../../licenses/")) {
130 score->error = talloc_asprintf(score,
131 "Expected symlink to"
132 " ../../licenses/..."
138 score->error = talloc_asprintf(score,
139 "License in _info is unknown '%s',"
140 " but LICENSE symlink is '%s'",
145 if (!streq(buf, expected)) {
146 score->error = talloc_asprintf(score,
147 "Expected symlink to %s not %s",
152 score->score = score->total;
155 struct ccanlint license_exists = {
156 .key = "license_exists",
157 .name = "Module has License: entry in _info, and LICENSE symlink/file",
158 .check = check_has_license,
159 .needs = "info_exists"
161 REGISTER_TEST(license_exists);