1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
|
/*
* dpkg-split - splitting and joining of multipart *.deb archives
* split.c - splitting archives
*
* Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
* Copyright © 2008-2011 Guillem Jover <guillem@debian.org>
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <compat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <limits.h>
#include <fcntl.h>
#include <libgen.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <dpkg/i18n.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/path.h>
#include <dpkg/string.h>
#include <dpkg/subproc.h>
#include <dpkg/buffer.h>
#include <dpkg/ar.h>
#include <dpkg/options.h>
#include "dpkg-split.h"
static char *
deb_field(const char *filename, const char *field)
{
struct dpkg_error err;
pid_t pid;
int p[2];
struct varbuf buf = VARBUF_INIT;
char *end;
m_pipe(p);
pid = subproc_fork();
if (pid == 0) {
/* Child writes to pipe. */
m_dup2(p[1], 1);
close(p[0]);
close(p[1]);
execlp(BACKEND, BACKEND, "--field", filename, field, NULL);
ohshite(_("unable to execute %s (%s)"),
_("package field value extraction"), BACKEND);
}
close(p[1]);
/* Parant reads from pipe. */
varbuf_reset(&buf);
if (fd_vbuf_copy(p[0], &buf, -1, &err) < 0)
ohshit(_("cannot extract package field value from '%s': %s"),
filename, err.str);
varbuf_end_str(&buf);
close(p[0]);
subproc_wait_check(pid, _("package field value extraction"), PROCPIPE);
/* Trim down trailing junk. */
for (end = buf.buf + strlen(buf.buf) - 1; end - buf.buf >= 1; end--)
if (isspace(*end))
*end = '\0';
else
break;
return varbuf_detach(&buf);
}
/* Cleanup filename for use in crippled msdos systems. */
static char *
clean_msdos_filename(char *filename)
{
char *d, *s;
for (s = d = filename; *s; d++, s++) {
if (*s == '+')
*d = 'x';
else if (isupper(*s))
*d = tolower(*s);
else if (islower(*s) || isdigit(*s))
*d = *s;
else
s++;
}
return filename;
}
static int
mksplit(const char *file_src, const char *prefix, off_t maxpartsize,
bool msdos)
{
struct dpkg_error err;
int fd_src;
struct stat st;
char hash[MD5HASHLEN + 1];
char *package, *version, *arch;
int nparts, curpart;
off_t partsize;
off_t cur_partsize, last_partsize;
char *prefixdir = NULL, *msdos_prefix = NULL;
struct varbuf file_dst = VARBUF_INIT;
struct varbuf partmagic = VARBUF_INIT;
struct varbuf partname = VARBUF_INIT;
fd_src = open(file_src, O_RDONLY);
if (fd_src < 0)
ohshite(_("unable to open source file `%.250s'"), file_src);
if (fstat(fd_src, &st))
ohshite(_("unable to fstat source file"));
if (!S_ISREG(st.st_mode))
ohshit(_("source file `%.250s' not a plain file"), file_src);
if (fd_md5(fd_src, hash, -1, &err) < 0)
ohshit(_("cannot compute MD5 hash for file '%s': %s"),
file_src, err.str);
lseek(fd_src, 0, SEEK_SET);
/* FIXME: Use libdpkg-deb. */
package = deb_field(file_src, "Package");
version = deb_field(file_src, "Version");
arch = deb_field(file_src, "Architecture");
partsize = maxpartsize - HEADERALLOWANCE;
last_partsize = st.st_size % partsize;
if (last_partsize == 0)
last_partsize = partsize;
nparts = (st.st_size + partsize - 1) / partsize;
printf(P_("Splitting package %s into %d part: ",
"Splitting package %s into %d parts: ", nparts),
package, nparts);
if (msdos) {
char *t;
t = m_strdup(prefix);
prefixdir = m_strdup(dirname(t));
free(t);
msdos_prefix = m_strdup(path_basename(prefix));
prefix = clean_msdos_filename(msdos_prefix);
}
for (curpart = 1; curpart <= nparts; curpart++) {
int fd_dst;
varbuf_reset(&file_dst);
/* Generate output filename. */
if (msdos) {
char *refname;
int prefix_max;
m_asprintf(&refname, "%dof%d", curpart, nparts);
prefix_max = max(8 - strlen(refname), 0);
varbuf_printf(&file_dst, "%s/%.*s%.8s.deb",
prefixdir, prefix_max, prefix, refname);
free(refname);
} else {
varbuf_printf(&file_dst, "%s.%dof%d.deb",
prefix, curpart, nparts);
}
if (curpart == nparts)
cur_partsize = last_partsize;
else
cur_partsize = partsize;
if (cur_partsize > maxpartsize) {
ohshit(_("header is too long, making part too long; "
"the package name or version\n"
"numbers must be extraordinarily long, "
"or something; giving up"));
}
/* Split the data. */
fd_dst = creat(file_dst.buf, 0644);
if (fd_dst < 0)
ohshite(_("unable to open file '%s'"), file_dst.buf);
/* Write the ar header. */
dpkg_ar_put_magic(file_dst.buf, fd_dst);
/* Write the debian-split part. */
varbuf_printf(&partmagic,
"%s\n%s\n%s\n%s\n%jd\n%jd\n%d/%d\n%s\n",
SPLITVERSION, package, version, hash,
(intmax_t)st.st_size, (intmax_t)partsize,
curpart, nparts, arch);
dpkg_ar_member_put_mem(file_dst.buf, fd_dst, PARTMAGIC,
partmagic.buf, partmagic.used);
varbuf_reset(&partmagic);
/* Write the data part. */
varbuf_printf(&partname, "data.%d", curpart);
dpkg_ar_member_put_file(file_dst.buf, fd_dst, partname.buf,
fd_src, cur_partsize);
varbuf_reset(&partname);
close(fd_dst);
printf("%d ", curpart);
}
varbuf_destroy(&file_dst);
varbuf_destroy(&partname);
varbuf_destroy(&partmagic);
free(package);
free(version);
free(arch);
free(prefixdir);
free(msdos_prefix);
close(fd_src);
printf(_("done\n"));
return 0;
}
int
do_split(const char *const *argv)
{
const char *sourcefile, *prefix;
sourcefile = *argv++;
if (!sourcefile)
badusage(_("--split needs a source filename argument"));
prefix = *argv++;
if (prefix && *argv)
badusage(_("--split takes at most a source filename and destination prefix"));
if (!prefix) {
size_t sourcefile_len = strlen(sourcefile);
if (str_match_end(sourcefile, DEBEXT))
sourcefile_len -= strlen(DEBEXT);
prefix = nfstrnsave(sourcefile, sourcefile_len);
}
mksplit(sourcefile, prefix, opt_maxpartsize, opt_msdos);
return 0;
}
|