965a159ff8fff53f15bea96d4675c052eedbd9f0
[ccan] / tools / ccanlint / tests / license.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 struct ccanlint has_license;
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")
55             || streq(d->lines[0], "BSD-MIT")
56             || streq(d->lines[0], "MIT"))
57                 return "../../licenses/BSD-MIT";
58         return NULL;
59 }
60
61 static void handle_license_link(struct manifest *m, struct score *score)
62 {
63         const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
64         struct doc_section *d = find_license(m);
65         const char *ldest = expected_link(m, d);
66         char *q;
67
68         printf(
69         "Most modules want a copy of their license, so usually we create a\n"
70         "LICENSE symlink into ../../licenses to avoid too many copies.\n");
71
72         /* FIXME: make ask printf-like */
73         q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
74                             ldest, d->lines[0]);
75         if (ask(q)) {
76                 if (symlink(ldest, link) != 0)
77                         err(1, "Creating symlink %s -> %s", link, ldest);
78         }
79 }
80
81 static void check_has_license(struct manifest *m,
82                               bool keep,
83                               unsigned int *timeleft, struct score *score)
84 {
85         char buf[PATH_MAX];
86         ssize_t len;
87         char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
88         const char *expected;
89         struct doc_section *d;
90
91         d = find_license(m);
92         if (!d) {
93                 score->error = "No License: tag in _info";
94                 return;
95         }
96         expected = expected_link(m, d);
97
98         len = readlink(license, buf, sizeof(buf));
99         if (len < 0) {
100                 /* Could be a real file... OK if not a standard license. */
101                 if (errno == EINVAL) {
102                         if (!expected) {
103                                 score->pass = true;
104                                 return;
105                         }
106                         score->error
107                                 = talloc_asprintf(score,
108                                           "License in _info is '%s',"
109                                           " expect LICENSE symlink '%s'",
110                                           d->lines[0], expected);
111                         return;
112                 }
113                 if (errno == ENOENT) {
114                         score->error = "LICENSE does not exist";
115                         if (expected)
116                                 has_license.handle = handle_license_link;
117                         return;
118                 }
119                 err(1, "readlink on %s", license);
120         }
121         if (len >= sizeof(buf))
122                 errx(1, "Reading symlink %s gave huge result", license);
123
124         buf[len] = '\0';
125
126         if (!strstarts(buf, "../../licenses/")) {
127                 score->error = talloc_asprintf(score,
128                                                "Expected symlink to"
129                                                " ../../licenses/..."
130                                                " not %s", buf);
131                 return;
132         }
133
134         if (!expected) {
135                 score->error = talloc_asprintf(score,
136                                           "License in _info is unknown '%s',"
137                                           " but LICENSE symlink is '%s'",
138                                           d->lines[0], buf);
139                 return;
140         }
141
142         if (!streq(buf, expected)) {
143                 score->error = talloc_asprintf(score,
144                                        "Expected symlink to %s not %s",
145                                        expected, buf);
146                 return;
147         }
148         score->pass = true;
149         score->score = score->total;
150 }
151
152 struct ccanlint license_exists = {
153         .key = "license_exists",
154         .name = "Module has License: entry in _info, and LICENSE symlink/file",
155         .check = check_has_license,
156         .needs = "info_exists"
157 };
158
159 REGISTER_TEST(license_exists);