]> git.ozlabs.org Git - ccan/blob - tools/ccanlint/tests/license_exists.c
ccanlint: parse --verbose before anything else.
[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         default:
39                 return NULL;
40         }
41 }
42
43 static void handle_license_link(struct manifest *m, struct score *score)
44 {
45         struct doc_section *d = find_license_tag(m);
46         const char *link = talloc_asprintf(m, "%s/LICENSE", m->dir);
47         const char *ldest = expected_link(m->license);
48         char *q;
49
50         printf(
51         "Most modules want a copy of their license, so usually we create a\n"
52         "LICENSE symlink into ../../licenses to avoid too many copies.\n");
53
54         /* FIXME: make ask printf-like */
55         q = talloc_asprintf(m, "Set up link to %s (license is %s)?",
56                             ldest, d->lines[0]);
57         if (ask(q)) {
58                 if (symlink(ldest, link) != 0)
59                         err(1, "Creating symlink %s -> %s", link, ldest);
60         }
61 }
62
63 extern struct ccanlint license_exists;
64
65 static void check_has_license(struct manifest *m,
66                               bool keep,
67                               unsigned int *timeleft, struct score *score)
68 {
69         char buf[PATH_MAX];
70         ssize_t len;
71         char *license = talloc_asprintf(m, "%s/LICENSE", m->dir);
72         const char *expected;
73         struct doc_section *d;
74
75         d = find_license_tag(m);
76         if (!d) {
77                 score->error = talloc_strdup(score, "No License: tag in _info");
78                 return;
79         }
80
81         m->license = which_license(d);
82         if (m->license == LICENSE_UNKNOWN) {
83                 score_file_error(score, m->info_file, d->srcline,
84                                  "WARNING: unknown License: in _info: %s",
85                                  d->lines[0]);
86                 /* FIXME: For historical reasons, don't fail here. */
87                 score->pass = true;
88                 return;
89         }
90
91         /* If they have a license tag at all, we pass. */
92         score->pass = true;
93
94         expected = expected_link(m->license);
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->score = score->total;
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                         /* Public domain doesn't really need a file. */
113                         if (m->license == LICENSE_PUBLIC_DOMAIN) {
114                                 score->score = score->total;
115                                 return;
116                         }
117                         score->error = talloc_strdup(score,
118                                                      "LICENSE does not exist");
119                         if (expected)
120                                 license_exists.handle = handle_license_link;
121                         return;
122                 }
123                 err(1, "readlink on %s", license);
124         }
125         if (len >= sizeof(buf))
126                 errx(1, "Reading symlink %s gave huge result", license);
127
128         buf[len] = '\0';
129
130         if (!strstarts(buf, "../../licenses/")) {
131                 score->error = talloc_asprintf(score,
132                                                "Expected symlink to"
133                                                " ../../licenses/..."
134                                                " not %s", buf);
135                 return;
136         }
137
138         if (!expected) {
139                 score->error = talloc_asprintf(score,
140                                           "License in _info is unknown '%s',"
141                                           " but LICENSE symlink is '%s'",
142                                           d->lines[0], buf);
143                 return;
144         }
145
146         if (!streq(buf, expected)) {
147                 score->error = talloc_asprintf(score,
148                                        "Expected symlink to %s not %s",
149                                        expected, buf);
150                 return;
151         }
152         score->pass = true;
153         score->score = score->total;
154 }
155
156 struct ccanlint license_exists = {
157         .key = "license_exists",
158         .name = "Module has License: entry in _info, and LICENSE symlink/file",
159         .check = check_has_license,
160         .needs = "info_exists"
161 };
162 REGISTER_TEST(license_exists);