ccanlint: Understand Creative Commons Zero license.
[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 const char *expected_link(enum license license)
15 {
16         switch (license) {
17         case LICENSE_LGPLv2_PLUS:
18         case LICENSE_LGPLv2:
19                 return "../../licenses/LGPL-2.1";
20         case LICENSE_LGPLv3:
21         case LICENSE_LGPL:
22                 return "../../licenses/LGPL-3";
23
24         case LICENSE_GPLv2_PLUS:
25         case LICENSE_GPLv2:
26                 return "../../licenses/GPL-2";
27
28         case LICENSE_GPLv3:
29         case LICENSE_GPL:
30                 return "../../licenses/GPL-3";
31
32         case LICENSE_BSD:
33                 return "../../licenses/BSD-3CLAUSE";
34
35         case LICENSE_MIT:
36                 return "../../licenses/BSD-MIT";
37
38         case LICENSE_CC0:
39                 return "../../licenses/CC0";
40
41         default:
42                 return NULL;
43         }
44 }
45
46 static void handle_license_link(struct manifest *m, struct score *score)
47 {
48         struct doc_section *d = find_license_tag(m);
49         const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
50         const char *ldest = expected_link(m->license);
51         char *q;
52
53         printf(
54         "Most modules want a copy of their license, so usually we create a\n"
55         "LICENSE symlink into ../../licenses to avoid too many copies.\n");
56
57         /* FIXME: make ask printf-like */
58         q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
59                             ldest, d->lines[0]);
60         if (ask(q)) {
61                 if (symlink(ldest, link) != 0)
62                         err(1, "Creating symlink %s -> %s", link, ldest);
63         }
64 }
65
66 extern struct ccanlint license_exists;
67
68 static void check_has_license(struct manifest *m,
69                               unsigned int *timeleft, struct score *score)
70 {
71         char buf[PATH_MAX];
72         ssize_t len;
73         char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
74         const char *expected;
75         struct doc_section *d;
76
77         d = find_license_tag(m);
78         if (!d) {
79                 score->error = talloc_strdup(score, "No License: tag in _info");
80                 return;
81         }
82
83         m->license = which_license(d);
84         if (m->license == LICENSE_UNKNOWN) {
85                 score_file_error(score, m->info_file, d->srcline,
86                                  "WARNING: unknown License: in _info: %s",
87                                  d->lines[0]);
88                 /* FIXME: For historical reasons, don't fail here. */
89                 score->pass = true;
90                 return;
91         }
92
93         /* If they have a license tag at all, we pass. */
94         score->pass = true;
95
96         expected = expected_link(m->license);
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->score = score->total;
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                         /* Public domain doesn't really need a file. */
115                         if (m->license == LICENSE_PUBLIC_DOMAIN) {
116                                 score->score = score->total;
117                                 return;
118                         }
119                         score->error = talloc_strdup(score,
120                                                      "LICENSE does not exist");
121                         if (expected)
122                                 license_exists.handle = handle_license_link;
123                         return;
124                 }
125                 err(1, "readlink on %s", license);
126         }
127         if (len >= sizeof(buf))
128                 errx(1, "Reading symlink %s gave huge result", license);
129
130         buf[len] = '\0';
131
132         if (!strstarts(buf, "../../licenses/")) {
133                 score->error = talloc_asprintf(score,
134                                                "Expected symlink to"
135                                                " ../../licenses/..."
136                                                " not %s", buf);
137                 return;
138         }
139
140         if (!expected) {
141                 score->error = talloc_asprintf(score,
142                                           "License in _info is unknown '%s',"
143                                           " but LICENSE symlink is '%s'",
144                                           d->lines[0], buf);
145                 return;
146         }
147
148         if (!streq(buf, expected)) {
149                 score->error = talloc_asprintf(score,
150                                        "Expected symlink to %s not %s",
151                                        expected, buf);
152                 return;
153         }
154         score->pass = true;
155         score->score = score->total;
156 }
157
158 struct ccanlint license_exists = {
159         .key = "license_exists",
160         .name = "Module has License: entry in _info, and LICENSE symlink/file",
161         .check = check_has_license,
162         .needs = "info_exists"
163 };
164 REGISTER_TEST(license_exists);