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