1 #include <tools/ccanlint/ccanlint.h>
11 #include <ccan/talloc/talloc.h>
12 #include <ccan/str/str.h>
14 /* We might need more ../ for nested modules. */
15 static const char *link_prefix(struct manifest *m)
17 char *prefix = talloc_strdup(m, "../../");
20 for (i = 0; i < strcount(m->modname, "/"); i++)
21 prefix = talloc_append_string(prefix, "../");
23 return talloc_append_string(prefix, "licenses/");
26 static const char *expected_link(const char *prefix, enum license license)
28 const char *shortname;
31 case LICENSE_LGPLv2_PLUS:
33 shortname = "LGPL-2.1";
40 case LICENSE_GPLv2_PLUS:
51 shortname = "BSD-3CLAUSE";
55 shortname = "BSD-MIT";
66 return talloc_append_string(talloc_strdup(prefix, prefix), shortname);
69 static void handle_license_link(struct manifest *m, struct score *score)
71 struct doc_section *d = find_license_tag(m);
72 const char *prefix = link_prefix(m);
73 const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
74 const char *ldest = expected_link(prefix, m->license);
78 "Most modules want a copy of their license, so usually we create a\n"
79 "LICENSE symlink into %s to avoid too many copies.\n", prefix);
81 /* FIXME: make ask printf-like */
82 q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
85 if (symlink(ldest, link) != 0)
86 err(1, "Creating symlink %s -> %s", link, ldest);
90 extern struct ccanlint license_exists;
92 static void check_has_license(struct manifest *m,
93 unsigned int *timeleft, struct score *score)
97 char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
99 struct doc_section *d;
100 const char *prefix = link_prefix(m);
102 d = find_license_tag(m);
104 score->error = talloc_strdup(score, "No License: tag in _info");
108 m->license = which_license(d);
109 if (m->license == LICENSE_UNKNOWN) {
110 score_file_error(score, m->info_file, d->srcline,
111 "WARNING: unknown License: in _info: %s",
113 /* FIXME: For historical reasons, don't fail here. */
118 /* If they have a license tag at all, we pass. */
121 expected = expected_link(prefix, m->license);
123 len = readlink(license, buf, sizeof(buf));
125 /* Could be a real file... OK if not a standard license. */
126 if (errno == EINVAL) {
128 score->score = score->total;
132 = talloc_asprintf(score,
133 "License in _info is '%s',"
134 " expect LICENSE symlink '%s'",
135 d->lines[0], expected);
138 if (errno == ENOENT) {
139 /* Public domain doesn't really need a file. */
140 if (m->license == LICENSE_PUBLIC_DOMAIN) {
141 score->score = score->total;
144 score->error = talloc_strdup(score,
145 "LICENSE does not exist");
147 license_exists.handle = handle_license_link;
150 err(1, "readlink on %s", license);
152 if (len >= sizeof(buf))
153 errx(1, "Reading symlink %s gave huge result", license);
157 if (!strstarts(buf, prefix)) {
158 score->error = talloc_asprintf(score,
159 "Expected symlink into"
160 " %s not %s", prefix, buf);
165 score->error = talloc_asprintf(score,
166 "License in _info is unknown '%s',"
167 " but LICENSE symlink is '%s'",
172 if (!streq(buf, expected)) {
173 score->error = talloc_asprintf(score,
174 "Expected symlink to %s not %s",
179 score->score = score->total;
182 struct ccanlint license_exists = {
183 .key = "license_exists",
184 .name = "Module has License: entry in _info, and LICENSE symlink/file",
185 .check = check_has_license,
186 .needs = "info_exists"
188 REGISTER_TEST(license_exists);