]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/license_exists.c
tools: use tal/path instead of opencoding most paths.
[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, struct score *score)
98 {
99         char buf[PATH_MAX];
100         ssize_t len;
101         char *license = path_join(m, m->dir, "LICENSE");
102         const char *expected;
103         struct doc_section *d;
104         const char *prefix = link_prefix(m);
105
106         d = find_license_tag(m);
107         if (!d) {
108                 score->error = tal_strdup(score, "No License: tag in _info");
109                 return;
110         }
111
112         m->license = which_license(d);
113         if (m->license == LICENSE_UNKNOWN) {
114                 score_file_error(score, m->info_file, d->srcline,
115                                  "WARNING: unknown License: in _info: %s",
116                                  d->lines[0]);
117                 /* FIXME: For historical reasons, don't fail here. */
118                 score->pass = true;
119                 return;
120         }
121
122         /* If they have a license tag at all, we pass. */
123         score->pass = true;
124
125         expected = expected_link(m, prefix, m->license);
126
127         len = readlink(license, buf, sizeof(buf));
128         if (len < 0) {
129                 /* Could be a real file... OK if not a standard license. */
130                 if (errno == EINVAL) {
131                         if (!expected) {
132                                 score->score = score->total;
133                                 return;
134                         }
135                         score->error
136                                 = tal_fmt(score,
137                                           "License in _info is '%s',"
138                                           " expect LICENSE symlink '%s'",
139                                           d->lines[0], expected);
140                         return;
141                 }
142                 if (errno == ENOENT) {
143                         /* Public domain doesn't really need a file. */
144                         if (m->license == LICENSE_PUBLIC_DOMAIN) {
145                                 score->score = score->total;
146                                 return;
147                         }
148                         score->error = tal_strdup(score,
149                                                      "LICENSE does not exist");
150                         if (expected)
151                                 license_exists.handle = handle_license_link;
152                         return;
153                 }
154                 err(1, "readlink on %s", license);
155         }
156         if (len >= sizeof(buf))
157                 errx(1, "Reading symlink %s gave huge result", license);
158
159         buf[len] = '\0';
160
161         if (!strstarts(buf, prefix)) {
162                 score->error = tal_fmt(score,
163                                        "Expected symlink into %s not %s",
164                                        prefix, buf);
165                 return;
166         }
167
168         if (!expected) {
169                 score->error = tal_fmt(score,
170                                        "License in _info is unknown '%s',"
171                                        " but LICENSE symlink is '%s'",
172                                        d->lines[0], buf);
173                 return;
174         }
175
176         if (!streq(buf, expected)) {
177                 score->error = tal_fmt(score,
178                                        "Expected symlink to %s not %s",
179                                        expected, buf);
180                 return;
181         }
182         score->pass = true;
183         score->score = score->total;
184 }
185
186 struct ccanlint license_exists = {
187         .key = "license_exists",
188         .name = "Module has License: entry in _info, and LICENSE symlink/file",
189         .check = check_has_license,
190         .needs = "info_exists"
191 };
192 REGISTER_TEST(license_exists);