]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/license_exists.c
6305d61c02881a7fef67780d58067f1c80a2c9b0
[ccan] / tools / ccanlint / tests / license_exists.c
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
14 /* We might need more ../ for nested modules. */
15 static const char *link_prefix(struct manifest *m)
16 {
17         char *prefix = talloc_strdup(m, "../../");
18         unsigned int i;
19
20         for (i = 0; i < strcount(m->modname, "/"); i++)
21                 prefix = talloc_append_string(prefix, "../");
22
23         return talloc_append_string(prefix, "licenses/");
24 }
25
26 static const char *expected_link(const char *prefix, enum license license)
27 {
28         const char *shortname;
29
30         switch (license) {
31         case LICENSE_LGPLv2_PLUS:
32         case LICENSE_LGPLv2:
33                 shortname = "LGPL-2.1";
34                 break;
35         case LICENSE_LGPLv3:
36         case LICENSE_LGPL:
37                 shortname = "LGPL-3";
38                 break;
39
40         case LICENSE_GPLv2_PLUS:
41         case LICENSE_GPLv2:
42                 shortname = "GPL-2";
43                 break;
44
45         case LICENSE_GPLv3:
46         case LICENSE_GPL:
47                 shortname = "GPL-3";
48                 break;
49
50         case LICENSE_BSD:
51                 shortname = "BSD-3CLAUSE";
52                 break;
53
54         case LICENSE_MIT:
55                 shortname = "BSD-MIT";
56                 break;
57
58         case LICENSE_CC0:
59                 shortname = "CC0";
60                 break;
61
62         default:
63                 return NULL;
64         }
65
66         return talloc_append_string(talloc_strdup(prefix, prefix), shortname);
67 }
68
69 static void handle_license_link(struct manifest *m, struct score *score)
70 {
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);
75         char *q;
76
77         printf(
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);
80
81         /* FIXME: make ask printf-like */
82         q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
83                             ldest, d->lines[0]);
84         if (ask(q)) {
85                 if (symlink(ldest, link) != 0)
86                         err(1, "Creating symlink %s -> %s", link, ldest);
87         }
88 }
89
90 extern struct ccanlint license_exists;
91
92 static void check_has_license(struct manifest *m,
93                               unsigned int *timeleft, struct score *score)
94 {
95         char buf[PATH_MAX];
96         ssize_t len;
97         char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
98         const char *expected;
99         struct doc_section *d;
100         const char *prefix = link_prefix(m);
101
102         d = find_license_tag(m);
103         if (!d) {
104                 score->error = talloc_strdup(score, "No License: tag in _info");
105                 return;
106         }
107
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",
112                                  d->lines[0]);
113                 /* FIXME: For historical reasons, don't fail here. */
114                 score->pass = true;
115                 return;
116         }
117
118         /* If they have a license tag at all, we pass. */
119         score->pass = true;
120
121         expected = expected_link(prefix, m->license);
122
123         len = readlink(license, buf, sizeof(buf));
124         if (len < 0) {
125                 /* Could be a real file... OK if not a standard license. */
126                 if (errno == EINVAL) {
127                         if (!expected) {
128                                 score->score = score->total;
129                                 return;
130                         }
131                         score->error
132                                 = talloc_asprintf(score,
133                                           "License in _info is '%s',"
134                                           " expect LICENSE symlink '%s'",
135                                           d->lines[0], expected);
136                         return;
137                 }
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;
142                                 return;
143                         }
144                         score->error = talloc_strdup(score,
145                                                      "LICENSE does not exist");
146                         if (expected)
147                                 license_exists.handle = handle_license_link;
148                         return;
149                 }
150                 err(1, "readlink on %s", license);
151         }
152         if (len >= sizeof(buf))
153                 errx(1, "Reading symlink %s gave huge result", license);
154
155         buf[len] = '\0';
156
157         if (!strstarts(buf, prefix)) {
158                 score->error = talloc_asprintf(score,
159                                                "Expected symlink into"
160                                                " %s not %s", prefix, buf);
161                 return;
162         }
163
164         if (!expected) {
165                 score->error = talloc_asprintf(score,
166                                           "License in _info is unknown '%s',"
167                                           " but LICENSE symlink is '%s'",
168                                           d->lines[0], buf);
169                 return;
170         }
171
172         if (!streq(buf, expected)) {
173                 score->error = talloc_asprintf(score,
174                                        "Expected symlink to %s not %s",
175                                        expected, buf);
176                 return;
177         }
178         score->pass = true;
179         score->score = score->total;
180 }
181
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"
187 };
188 REGISTER_TEST(license_exists);