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