ccanlint: print coverage amount when -vv
[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 REGISTER_TEST(license_exists);
15
16 static struct doc_section *find_license(const struct manifest *m)
17 {
18         struct doc_section *d;
19
20         list_for_each(m->info_file->doc_sections, d, list) {
21                 if (!streq(d->function, m->basename))
22                         continue;
23                 if (streq(d->type, "license"))
24                         return d;
25         }
26         return NULL;
27 }
28
29 static const char *expected_link(const struct manifest *m,
30                                  struct doc_section *d)
31 {
32         if (streq(d->lines[0], "GPL")
33             || streq(d->lines[0], "GPLv3")
34             || streq(d->lines[0], "GPLv3 or later")
35             || streq(d->lines[0], "GPLv3 (or later)")
36             || streq(d->lines[0], "GPL (3 or any later version)"))
37                 return "../../licenses/GPL-3";
38         if (streq(d->lines[0], "GPLv2")
39             || streq(d->lines[0], "GPLv2 or later")
40             || streq(d->lines[0], "GPLv2 (or later)")
41             || streq(d->lines[0], "GPL (2 or any later version)"))
42                 return "../../licenses/GPL-3";
43         if (streq(d->lines[0], "LGPL")
44             || streq(d->lines[0], "LGPLv3")
45             || streq(d->lines[0], "LGPLv3 or later")
46             || streq(d->lines[0], "LGPLv3 (or later)")
47             || streq(d->lines[0], "LGPL (3 or any later version)"))
48                 return "../../licenses/LGPL-3";
49         if (streq(d->lines[0], "LGPLv2")
50             || streq(d->lines[0], "LGPLv2 or later")
51             || streq(d->lines[0], "LGPLv2 (or later)")
52             || streq(d->lines[0], "LGPL (2 or any later version)"))
53                 return "../../licenses/LGPL-2.1";
54         if (streq(d->lines[0], "BSD-MIT")
55             || streq(d->lines[0], "MIT"))
56                 return "../../licenses/BSD-MIT";
57         if (streq(d->lines[0], "BSD (3 clause)"))
58                 return "../../licenses/BSD-3CLAUSE";
59         return NULL;
60 }
61
62 static void handle_license_link(struct manifest *m, struct score *score)
63 {
64         const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
65         struct doc_section *d = find_license(m);
66         const char *ldest = expected_link(m, d);
67         char *q;
68
69         printf(
70         "Most modules want a copy of their license, so usually we create a\n"
71         "LICENSE symlink into ../../licenses to avoid too many copies.\n");
72
73         /* FIXME: make ask printf-like */
74         q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
75                             ldest, d->lines[0]);
76         if (ask(q)) {
77                 if (symlink(ldest, link) != 0)
78                         err(1, "Creating symlink %s -> %s", link, ldest);
79         }
80 }
81
82 static void check_has_license(struct manifest *m,
83                               bool keep,
84                               unsigned int *timeleft, struct score *score)
85 {
86         char buf[PATH_MAX];
87         ssize_t len;
88         char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
89         const char *expected;
90         struct doc_section *d;
91
92         d = find_license(m);
93         if (!d) {
94                 score->error = talloc_strdup(score, "No License: tag in _info");
95                 return;
96         }
97         expected = expected_link(m, d);
98
99         len = readlink(license, buf, sizeof(buf));
100         if (len < 0) {
101                 /* Could be a real file... OK if not a standard license. */
102                 if (errno == EINVAL) {
103                         if (!expected) {
104                                 score->pass = true;
105                                 return;
106                         }
107                         score->error
108                                 = talloc_asprintf(score,
109                                           "License in _info is '%s',"
110                                           " expect LICENSE symlink '%s'",
111                                           d->lines[0], expected);
112                         return;
113                 }
114                 if (errno == ENOENT) {
115                         score->error = talloc_strdup(score,
116                                                      "LICENSE does not exist");
117                         if (expected)
118                                 license_exists.handle = handle_license_link;
119                         return;
120                 }
121                 err(1, "readlink on %s", license);
122         }
123         if (len >= sizeof(buf))
124                 errx(1, "Reading symlink %s gave huge result", license);
125
126         buf[len] = '\0';
127
128         if (!strstarts(buf, "../../licenses/")) {
129                 score->error = talloc_asprintf(score,
130                                                "Expected symlink to"
131                                                " ../../licenses/..."
132                                                " not %s", buf);
133                 return;
134         }
135
136         if (!expected) {
137                 score->error = talloc_asprintf(score,
138                                           "License in _info is unknown '%s',"
139                                           " but LICENSE symlink is '%s'",
140                                           d->lines[0], buf);
141                 return;
142         }
143
144         if (!streq(buf, expected)) {
145                 score->error = talloc_asprintf(score,
146                                        "Expected symlink to %s not %s",
147                                        expected, buf);
148                 return;
149         }
150         score->pass = true;
151         score->score = score->total;
152 }
153
154 struct ccanlint license_exists = {
155         .key = "license_exists",
156         .name = "Module has License: entry in _info, and LICENSE symlink/file",
157         .check = check_has_license,
158         .needs = "info_exists"
159 };