json_out: make json_out_finished finish buffer.
[ccan] / tools / ccanlint / tests / license_exists.c
1 #include <tools/ccanlint/ccanlint.h>
2 #include <ccan/tal/tal.h>
3 #include <ccan/tal/str/str.h>
4 #include <ccan/tal/path/path.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <limits.h>
10 #include <errno.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <err.h>
14 #include <ccan/str/str.h>
15 #include <ccan/take/take.h>
16
17 /* We might need more ../ for nested modules. */
18 static const char *link_prefix(struct manifest *m)
19 {
20         char *prefix = tal_strdup(m, "../../");
21         unsigned int i;
22
23         for (i = 0; i < strcount(m->modname, "/"); i++)
24                 prefix = tal_strcat(m, take(prefix), "../");
25
26         return tal_strcat(m, take(prefix), "licenses/");
27 }
28
29 static const char *expected_link(const tal_t *ctx,
30                                  const char *prefix, enum license license)
31 {
32         const char *shortname;
33
34         switch (license) {
35         case LICENSE_LGPLv2_PLUS:
36         case LICENSE_LGPLv2:
37                 shortname = "LGPL-2.1";
38                 break;
39         case LICENSE_LGPLv3:
40         case LICENSE_LGPL:
41                 shortname = "LGPL-3";
42                 break;
43
44         case LICENSE_GPLv2_PLUS:
45         case LICENSE_GPLv2:
46                 shortname = "GPL-2";
47                 break;
48
49         case LICENSE_GPLv3:
50         case LICENSE_GPL:
51                 shortname = "GPL-3";
52                 break;
53
54         case LICENSE_BSD:
55                 shortname = "BSD-3CLAUSE";
56                 break;
57
58         case LICENSE_MIT:
59                 shortname = "BSD-MIT";
60                 break;
61
62         case LICENSE_CC0:
63                 shortname = "CC0";
64                 break;
65
66         default:
67                 return NULL;
68         }
69
70         return tal_strcat(ctx, prefix, shortname);
71 }
72
73 static void handle_license_link(struct manifest *m, struct score *score)
74 {
75         struct doc_section *d = find_license_tag(m);
76         const char *prefix = link_prefix(m);
77         const char *link = path_join(m, m->dir, "LICENSE");
78         const char *ldest = expected_link(score, prefix, m->license);
79         char *q;
80
81         printf(
82         "Most modules want a copy of their license, so usually we create a\n"
83         "LICENSE symlink into %s to avoid too many copies.\n", prefix);
84
85         /* FIXME: make ask printf-like */
86         q = tal_fmt(m, "Set up link to %s (license is %s)?",
87                     ldest, d->lines[0]);
88         if (ask(q)) {
89                 if (symlink(ldest, link) != 0)
90                         err(1, "Creating symlink %s -> %s", link, ldest);
91         }
92 }
93
94 extern struct ccanlint license_exists;
95
96 static void check_has_license(struct manifest *m,
97                               unsigned int *timeleft UNNEEDED,
98                               struct score *score)
99 {
100         char buf[PATH_MAX];
101         ssize_t len;
102         char *license = path_join(m, m->dir, "LICENSE");
103         const char *expected;
104         struct doc_section *d;
105         const char *prefix = link_prefix(m);
106
107         d = find_license_tag(m);
108         if (!d) {
109                 score->error = tal_strdup(score, "No License: tag in _info");
110                 return;
111         }
112
113         m->license = which_license(d);
114         if (m->license == LICENSE_UNKNOWN) {
115                 score_file_error(score, m->info_file, d->srcline,
116                                  "WARNING: unknown License: in _info: %s",
117                                  d->lines[0]);
118                 /* FIXME: For historical reasons, don't fail here. */
119                 score->pass = true;
120                 return;
121         }
122
123         /* If they have a license tag at all, we pass. */
124         score->pass = true;
125
126         expected = expected_link(m, prefix, m->license);
127
128         len = readlink(license, buf, sizeof(buf));
129         if (len < 0) {
130                 /* Could be a real file... OK if not a standard license. */
131                 if (errno == EINVAL) {
132                         if (!expected) {
133                                 score->score = score->total;
134                                 return;
135                         }
136                         score->error
137                                 = tal_fmt(score,
138                                           "License in _info is '%s',"
139                                           " expect LICENSE symlink '%s'",
140                                           d->lines[0], expected);
141                         return;
142                 }
143                 if (errno == ENOENT) {
144                         /* Public domain doesn't really need a file. */
145                         if (m->license == LICENSE_PUBLIC_DOMAIN) {
146                                 score->score = score->total;
147                                 return;
148                         }
149                         score->error = tal_strdup(score,
150                                                      "LICENSE does not exist");
151                         if (expected)
152                                 license_exists.handle = handle_license_link;
153                         return;
154                 }
155                 err(1, "readlink on %s", license);
156         }
157         if (len >= sizeof(buf))
158                 errx(1, "Reading symlink %s gave huge result", license);
159
160         buf[len] = '\0';
161
162         if (!strstarts(buf, prefix)) {
163                 score->error = tal_fmt(score,
164                                        "Expected symlink into %s not %s",
165                                        prefix, buf);
166                 return;
167         }
168
169         if (!expected) {
170                 score->error = tal_fmt(score,
171                                        "License in _info is unknown '%s',"
172                                        " but LICENSE symlink is '%s'",
173                                        d->lines[0], buf);
174                 return;
175         }
176
177         if (!streq(buf, expected)) {
178                 score->error = tal_fmt(score,
179                                        "Expected symlink to %s not %s",
180                                        expected, buf);
181                 return;
182         }
183         score->pass = true;
184         score->score = score->total;
185 }
186
187 struct ccanlint license_exists = {
188         .key = "license_exists",
189         .name = "Module has License: entry in _info, and LICENSE symlink/file",
190         .check = check_has_license,
191         .needs = "info_exists"
192 };
193 REGISTER_TEST(license_exists);