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
|
/*
* dpkg-split - splitting and joining of multipart *.deb archives
* split.c - splitting archives
*
* Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
* Copyright © 2010 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 <http://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/subproc.h>
#include <dpkg/buffer.h>
#include <dpkg/ar.h>
#include <dpkg/myopt.h>
#include "dpkg-split.h"
static char *
deb_field(const char *filename, const char *field)
{
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);
fd_vbuf_copy(p[0], &buf, -1, _("package field value extraction"));
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)
{
int fd_src;
struct stat st;
char hash[MD5HASHLEN + 1];
char *package, *version;
int nparts, curpart;
off_t partsize;
off_t 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);
fd_md5(fd_src, hash, -1, "md5hash");
lseek(fd_src, 0, SEEK_SET);
/* FIXME: Use libdpkg-deb. */
package = deb_field(file_src, "Package");
version = deb_field(file_src, "Version");
partsize = maxpartsize - HEADERALLOWANCE;
last_partsize = st.st_size % partsize;
if (last_partsize == 0)
last_partsize = partsize;
nparts = (st.st_size + partsize - 1) / partsize;
setvbuf(stdout, NULL, _IONBF, 0);
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);
t = m_strdup(prefix);
msdos_prefix = m_strdup(basename(t));
free(t);
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)
partsize = last_partsize;
if (partsize > maxpartsize) {
ohshit(_("Header is too long, making part too long. "
"Your package name or version\n"
"numbers must be extraordinarily long, "
"or something. Giving up.\n"));
}
/* 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",
SPLITVERSION, package, version, hash,
(intmax_t)st.st_size, (intmax_t)partsize,
curpart, nparts);
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, partsize);
varbuf_reset(&partname);
close(fd_dst);
printf("%d ", curpart);
}
varbuf_destroy(&file_dst);
varbuf_destroy(&partname);
varbuf_destroy(&partmagic);
free(prefixdir);
free(msdos_prefix);
close(fd_src);
printf(_("done\n"));
return 0;
}
void
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) {
char *palloc;
int l;
l = strlen(sourcefile);
palloc = nfmalloc(l + 1);
strcpy(palloc, sourcefile);
if (!strcmp(palloc + l - (sizeof(DEBEXT) - 1), DEBEXT)) {
l -= (sizeof(DEBEXT) - 1);
palloc[l] = '\0';
}
prefix = palloc;
}
mksplit(sourcefile, prefix, opt_maxpartsize, opt_msdos);
exit(0);
}
|