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/>.
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>
42 #include <dpkg/options.h>
44 #include "dpkg-split.h"
47 parse_intmax(const char *value
, const char *fn
, const char *what
)
53 r
= strtoimax(value
, &endp
, 10);
54 if (value
== endp
|| *endp
)
55 ohshit(_("file '%.250s' is corrupt - bad digit (code %d) in %s"),
57 if (r
< 0 || errno
== ERANGE
)
58 ohshit(_("file '%s' is corrupt; out of range integer in %s"), fn
, what
);
62 static char *nextline(char **ripp
, const char *fn
, const char *what
) {
67 ohshit(_("file '%.250s' is corrupt - %.250s missing"), fn
, what
);
68 newline
= strchr(rip
,'\n');
70 ohshit(_("file '%.250s' is corrupt - missing newline after %.250s"),
74 str_rtrim_spaces(rip
, newline
);
80 * Read a deb-split part archive.
82 * @return Part info (nfmalloc'd) if was an archive part and we read it,
86 read_info(struct dpkg_ar
*ar
, struct partinfo
*ir
)
88 static struct varbuf format_member
= VARBUF_INIT
;
92 char magicbuf
[sizeof(DPKG_AR_MAGIC
) - 1], *rip
, *partnums
, *slash
;
94 struct dpkg_ar_hdr arh
;
97 rc
= fd_read(ar
->fd
, magicbuf
, sizeof(magicbuf
));
98 if (rc
!= sizeof(magicbuf
)) {
100 ohshite(_("error reading %.250s"), ar
->name
);
104 if (memcmp(magicbuf
, DPKG_AR_MAGIC
, sizeof(magicbuf
)))
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)
115 if (dpkg_ar_member_is_illegal(&arh
))
116 ohshit(_("file '%.250s' is corrupt - bad magic at end of first header"),
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");
127 int c
= format_member
.buf
[thisilen
];
130 ohshit(_("file '%.250s' is corrupt - bad padding character (code %d)"),
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")));
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
,'/');
164 ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), ar
->name
);
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. */
179 ir
->arch
= nfstrsave(nextline(&rip
, ar
->name
, _("package architecture")));
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"),
192 if (strncmp(arh
.ar_name
,"data",4))
193 ohshit(_("file '%.250s' is corrupt - second member is not data member"),
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"),
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"),
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
);
225 void mustgetpartinfo(const char *filename
, struct partinfo
*ri
) {
226 struct dpkg_ar
*part
;
228 part
= dpkg_ar_open(filename
);
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
);
236 void print_info(const struct partinfo
*pi
) {
238 " Part format version: %d.%d\n"
239 " Part of package: %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"),
250 pi
->fmtversion
.major
, pi
->fmtversion
.minor
,
253 pi
->arch
? pi
->arch
: C_("architecture", "<unknown>"),
255 (intmax_t)pi
->orglength
,
256 (intmax_t)pi
->maxpartlen
,
259 (intmax_t)pi
->thispartlen
,
260 (intmax_t)pi
->thispartoffset
,
261 (intmax_t)pi
->filesize
);
265 do_info(const char *const *argv
)
270 badusage(_("--%s requires one or more part file arguments"),
273 while ((thisarg
= *argv
++)) {
274 struct partinfo
*pi
, ps
;
275 struct dpkg_ar
*part
;
277 part
= dpkg_ar_open(thisarg
);
279 ohshite(_("cannot open archive part file '%.250s'"), thisarg
);
280 pi
= read_info(part
, &ps
);
285 printf(_("file '%s' is not an archive part\n"), thisarg
);
287 m_output(stdout
, _("<standard output>"));