arch: Restrict kopensolaris ports to amd64 and i386
[dpkg.git] / src / split / info.c
blob24c91d81d5fad3c11422a9f91c8ca606d0acc8b8
1 /*
2 * dpkg-split - splitting and joining of multipart *.deb archives
3 * info.c - information about split archives
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
8 * This is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <sys/stat.h>
27 #include <errno.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <inttypes.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <stdio.h>
36 #include <dpkg/i18n.h>
37 #include <dpkg/c-ctype.h>
38 #include <dpkg/dpkg.h>
39 #include <dpkg/dpkg-db.h>
40 #include <dpkg/fdio.h>
41 #include <dpkg/ar.h>
42 #include <dpkg/options.h>
44 #include "dpkg-split.h"
46 static intmax_t
47 parse_intmax(const char *value, const char *fn, const char *what)
49 intmax_t r;
50 char *endp;
52 errno = 0;
53 r = strtoimax(value, &endp, 10);
54 if (value == endp || *endp)
55 ohshit(_("file '%.250s' is corrupt - bad digit (code %d) in %s"),
56 fn, *endp, what);
57 if (r < 0 || errno == ERANGE)
58 ohshit(_("file '%s' is corrupt; out of range integer in %s"), fn, what);
59 return r;
62 static char *nextline(char **ripp, const char *fn, const char *what) {
63 char *newline, *rip;
65 rip= *ripp;
66 if (!rip)
67 ohshit(_("file '%.250s' is corrupt - %.250s missing"), fn, what);
68 newline= strchr(rip,'\n');
69 if (!newline)
70 ohshit(_("file '%.250s' is corrupt - missing newline after %.250s"),
71 fn, what);
72 *ripp= newline+1;
74 str_rtrim_spaces(rip, newline);
76 return rip;
79 /**
80 * Read a deb-split part archive.
82 * @return Part info (nfmalloc'd) if was an archive part and we read it,
83 * NULL if it wasn't.
85 struct partinfo *
86 read_info(struct dpkg_ar *ar, struct partinfo *ir)
88 static struct varbuf format_member = VARBUF_INIT;
90 size_t thisilen;
91 intmax_t templong;
92 char magicbuf[sizeof(DPKG_AR_MAGIC) - 1], *rip, *partnums, *slash;
93 const char *err;
94 struct dpkg_ar_hdr arh;
95 ssize_t rc;
97 rc = fd_read(ar->fd, magicbuf, sizeof(magicbuf));
98 if (rc != sizeof(magicbuf)) {
99 if (rc < 0)
100 ohshite(_("error reading %.250s"), ar->name);
101 else
102 return NULL;
104 if (memcmp(magicbuf, DPKG_AR_MAGIC, sizeof(magicbuf)))
105 return NULL;
107 rc = fd_read(ar->fd, &arh, sizeof(arh));
108 if (rc != sizeof(arh))
109 read_fail(rc, ar->name, "ar header");
111 dpkg_ar_normalize_name(&arh);
113 if (strncmp(arh.ar_name, PARTMAGIC, sizeof(arh.ar_name)) != 0)
114 return NULL;
115 if (dpkg_ar_member_is_illegal(&arh))
116 ohshit(_("file '%.250s' is corrupt - bad magic at end of first header"),
117 ar->name);
118 thisilen = dpkg_ar_member_get_size(ar, &arh);
120 varbuf_reset(&format_member);
121 varbuf_grow(&format_member, thisilen + 2);
123 rc = fd_read(ar->fd, format_member.buf, thisilen + (thisilen & 1));
124 if (rc != (ssize_t)(thisilen + (thisilen & 1)))
125 read_fail(rc, ar->name, "reading header member");
126 if (thisilen & 1) {
127 int c = format_member.buf[thisilen];
129 if (c != '\n')
130 ohshit(_("file '%.250s' is corrupt - bad padding character (code %d)"),
131 ar->name, c);
133 varbuf_trunc(&format_member, thisilen);
134 if (memchr(format_member.buf, 0, thisilen))
135 ohshit(_("file '%.250s' is corrupt - nulls in info section"), ar->name);
137 ir->filename = ar->name;
139 rip = format_member.buf;
140 err = deb_version_parse(&ir->fmtversion,
141 nextline(&rip, ar->name, _("format version number")));
142 if (err)
143 ohshit(_("file '%.250s' has invalid format version: %s"), ar->name, err);
144 if (ir->fmtversion.major != 2)
145 ohshit(_("file '%.250s' is format version %d.%d; get a newer dpkg-split"),
146 ar->name, ir->fmtversion.major, ir->fmtversion.minor);
148 ir->package = nfstrsave(nextline(&rip, ar->name, _("package name")));
149 ir->version = nfstrsave(nextline(&rip, ar->name, _("package version number")));
150 ir->md5sum = nfstrsave(nextline(&rip, ar->name, _("package file MD5 checksum")));
151 if (strlen(ir->md5sum) != MD5HASHLEN ||
152 strspn(ir->md5sum, "0123456789abcdef") != MD5HASHLEN)
153 ohshit(_("file '%.250s' is corrupt - bad MD5 checksum '%.250s'"),
154 ar->name, ir->md5sum);
156 ir->orglength = parse_intmax(nextline(&rip, ar->name, _("archive total size")),
157 ar->name, _("archive total size"));
158 ir->maxpartlen = parse_intmax(nextline(&rip, ar->name, _("archive part offset")),
159 ar->name, _("archive part offset"));
161 partnums = nextline(&rip, ar->name, _("archive part numbers"));
162 slash= strchr(partnums,'/');
163 if (!slash)
164 ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), ar->name);
165 *slash++ = '\0';
167 templong = parse_intmax(slash, ar->name, _("number of archive parts"));
168 if (templong <= 0 || templong > INT_MAX)
169 ohshit(_("file '%.250s' is corrupt - bad number of archive parts"), ar->name);
170 ir->maxpartn= templong;
171 templong = parse_intmax(partnums, ar->name, _("archive parts number"));
172 if (templong <= 0 || templong > ir->maxpartn)
173 ohshit(_("file '%.250s' is corrupt - bad archive part number"), ar->name);
174 ir->thispartn= templong;
176 /* If the package was created with dpkg 1.16.1 or later it will include
177 * the architecture. */
178 if (*rip != '\0')
179 ir->arch = nfstrsave(nextline(&rip, ar->name, _("package architecture")));
180 else
181 ir->arch = NULL;
183 rc = fd_read(ar->fd, &arh, sizeof(arh));
184 if (rc != sizeof(arh))
185 read_fail(rc, ar->name, "reading data part member ar header");
187 dpkg_ar_normalize_name(&arh);
189 if (dpkg_ar_member_is_illegal(&arh))
190 ohshit(_("file '%.250s' is corrupt - bad magic at end of second header"),
191 ar->name);
192 if (strncmp(arh.ar_name,"data",4))
193 ohshit(_("file '%.250s' is corrupt - second member is not data member"),
194 ar->name);
196 ir->thispartlen = dpkg_ar_member_get_size(ar, &arh);
197 ir->thispartoffset= (ir->thispartn-1)*ir->maxpartlen;
199 if (ir->maxpartn != (ir->orglength+ir->maxpartlen-1)/ir->maxpartlen)
200 ohshit(_("file '%.250s' is corrupt - wrong number of parts for quoted sizes"),
201 ar->name);
202 if (ir->thispartlen !=
203 (ir->thispartn == ir->maxpartn
204 ? ir->orglength - ir->thispartoffset : ir->maxpartlen))
205 ohshit(_("file '%.250s' is corrupt - size is wrong for quoted part number"),
206 ar->name);
208 ir->filesize = (strlen(DPKG_AR_MAGIC) +
209 sizeof(arh) + thisilen + (thisilen & 1) +
210 sizeof(arh) + ir->thispartlen + (ir->thispartlen & 1));
212 if (S_ISREG(ar->mode)) {
213 /* Don't do this check if it's coming from a pipe or something. It's
214 * only an extra sanity check anyway. */
215 if (ar->size < ir->filesize)
216 ohshit(_("file '%.250s' is corrupt - too short"), ar->name);
219 ir->headerlen = strlen(DPKG_AR_MAGIC) +
220 sizeof(arh) + thisilen + (thisilen & 1) + sizeof(arh);
222 return ir;
225 void mustgetpartinfo(const char *filename, struct partinfo *ri) {
226 struct dpkg_ar *part;
228 part = dpkg_ar_open(filename);
229 if (!part)
230 ohshite(_("cannot open archive part file '%.250s'"), filename);
231 if (!read_info(part, ri))
232 ohshite(_("file '%.250s' is not an archive part"), filename);
233 dpkg_ar_close(part);
236 void print_info(const struct partinfo *pi) {
237 printf(_("%s:\n"
238 " Part format version: %d.%d\n"
239 " Part of package: %s\n"
240 " ... version: %s\n"
241 " ... architecture: %s\n"
242 " ... MD5 checksum: %s\n"
243 " ... length: %jd bytes\n"
244 " ... split every: %jd bytes\n"
245 " Part number: %d/%d\n"
246 " Part length: %jd bytes\n"
247 " Part offset: %jd bytes\n"
248 " Part file size (used portion): %jd bytes\n\n"),
249 pi->filename,
250 pi->fmtversion.major, pi->fmtversion.minor,
251 pi->package,
252 pi->version,
253 pi->arch ? pi->arch : C_("architecture", "<unknown>"),
254 pi->md5sum,
255 (intmax_t)pi->orglength,
256 (intmax_t)pi->maxpartlen,
257 pi->thispartn,
258 pi->maxpartn,
259 (intmax_t)pi->thispartlen,
260 (intmax_t)pi->thispartoffset,
261 (intmax_t)pi->filesize);
265 do_info(const char *const *argv)
267 const char *thisarg;
269 if (!*argv)
270 badusage(_("--%s requires one or more part file arguments"),
271 cipaction->olong);
273 while ((thisarg= *argv++)) {
274 struct partinfo *pi, ps;
275 struct dpkg_ar *part;
277 part = dpkg_ar_open(thisarg);
278 if (!part)
279 ohshite(_("cannot open archive part file '%.250s'"), thisarg);
280 pi = read_info(part, &ps);
281 dpkg_ar_close(part);
282 if (pi) {
283 print_info(pi);
284 } else {
285 printf(_("file '%s' is not an archive part\n"), thisarg);
287 m_output(stdout, _("<standard output>"));
290 return 0;