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