1 /*
2 * dpkg-split - splitting and joining of multipart *.deb archives
3 * queue.c - queue management
4 *
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2014 Guillem Jover <guillem@debian.org>
7 *
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.
12 *
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.
17 *
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/>.
20 */
21
22 #include <config.h>
23 #include <compat.h>
24
25 #include <sys/stat.h>
26
27 #include <errno.h>
28 #include <limits.h>
29 #include <inttypes.h>
30 #include <string.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <unistd.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37
38 #include <dpkg/i18n.h>
39 #include <dpkg/dpkg.h>
40 #include <dpkg/dpkg-db.h>
41 #include <dpkg/dir.h>
42 #include <dpkg/buffer.h>
43 #include <dpkg/options.h>
44
45 #include "dpkg-split.h"
46
47 /*
48 * The queue, by default located in /var/lib/dpkg/parts/, is a plain
49 * directory with one file per part.
50 *
51 * Each part is named “<md5sum>.<maxpartlen>.<thispartn>.<maxpartn>”,
52 * with all numbers in hex.
53 */
54
55
56 static bool
decompose_filename(const char * filename,struct partqueue * pq)57 decompose_filename(const char *filename, struct partqueue *pq)
58 {
59 const char *p;
60 char *q;
61
62 if (strspn(filename, "0123456789abcdef") != MD5HASHLEN ||
63 filename[MD5HASHLEN] != '.')
64 return false;
65
66 pq->info.md5sum = nfstrnsave(filename, MD5HASHLEN);
67
68 p = filename + MD5HASHLEN + 1;
69 errno = 0;
70 pq->info.maxpartlen = strtoimax(p, &q, 16);
71 if (q == p || *q++ != '.' || errno != 0)
72 return false;
73
74 p = q;
75 pq->info.thispartn = (int)strtol(p, &q, 16);
76 if (q == p || *q++ != '.' || errno != 0)
77 return false;
78
79 p = q;
80 pq->info.maxpartn = (int)strtol(p, &q, 16);
81 if (q == p || *q || errno != 0)
82 return false;
83
84 return true;
85 }
86
87 static struct partqueue *
scandepot(void)88 scandepot(void)
89 {
90 DIR *depot;
91 struct dirent *de;
92 struct partqueue *queue = NULL;
93
94 depot = opendir(opt_depotdir);
95 if (!depot)
96 ohshite(_("unable to read depot directory '%.250s'"), opt_depotdir);
97 while ((de= readdir(depot))) {
98 struct partqueue *pq;
99 char *p;
100
101 if (de->d_name[0] == '.') continue;
102 pq = nfmalloc(sizeof(*pq));
103 pq->info.fmtversion.major = 0;
104 pq->info.fmtversion.minor = 0;
105 pq->info.package = NULL;
106 pq->info.version = NULL;
107 pq->info.arch = NULL;
108 pq->info.orglength= pq->info.thispartoffset= pq->info.thispartlen= 0;
109 pq->info.headerlen= 0;
110 p = nfmalloc(strlen(opt_depotdir) + 1 + strlen(de->d_name) + 1);
111 sprintf(p, "%s/%s", opt_depotdir, de->d_name);
112 pq->info.filename= p;
113 if (!decompose_filename(de->d_name,pq)) {
114 pq->info.md5sum= NULL;
115 pq->info.maxpartlen= pq->info.thispartn= pq->info.maxpartn= 0;
116 }
117 pq->nextinqueue= queue;
118 queue= pq;
119 }
120 closedir(depot);
121
122 return queue;
123 }
124
125 static bool
partmatches(struct partinfo * pi,struct partinfo * refi)126 partmatches(struct partinfo *pi, struct partinfo *refi)
127 {
128 return (pi->md5sum &&
129 strcmp(pi->md5sum, refi->md5sum) == 0 &&
130 pi->maxpartn == refi->maxpartn &&
131 pi->maxpartlen == refi->maxpartlen);
132 }
133
134 int
do_auto(const char * const * argv)135 do_auto(const char *const *argv)
136 {
137 const char *partfile;
138 struct partinfo *refi, **partlist, *otherthispart;
139 struct partqueue *queue;
140 struct partqueue *pq;
141 struct dpkg_ar *part;
142 unsigned int i;
143 int j;
144
145 if (!opt_outputfile)
146 badusage(_("--auto requires the use of the --output option"));
147 partfile = *argv++;
148 if (partfile == NULL || *argv)
149 badusage(_("--auto requires exactly one part file argument"));
150
151 refi = nfmalloc(sizeof(*refi));
152 part = dpkg_ar_open(partfile);
153 if (!part)
154 ohshite(_("unable to read part file '%.250s'"), partfile);
155 if (!read_info(part, refi)) {
156 if (!opt_npquiet)
157 printf(_("File '%.250s' is not part of a multipart archive.\n"), partfile);
158 m_output(stdout, _("<standard output>"));
159 return 1;
160 }
161 dpkg_ar_close(part);
162
163 queue = scandepot();
164 partlist = nfmalloc(sizeof(*partlist) * refi->maxpartn);
165 for (i = 0; i < refi->maxpartn; i++)
166 partlist[i] = NULL;
167 for (pq= queue; pq; pq= pq->nextinqueue) {
168 struct partinfo *npi, *pi = &pq->info;
169
170 if (!partmatches(pi,refi)) continue;
171 npi = nfmalloc(sizeof(*npi));
172 mustgetpartinfo(pi->filename,npi);
173 addtopartlist(partlist,npi,refi);
174 }
175 /* If we already have a copy of this version we ignore it and prefer the
176 * new one, but we still want to delete the one in the depot, so we
177 * save its partinfo (with the filename) for later. This also prevents
178 * us from accidentally deleting the source file. */
179 otherthispart= partlist[refi->thispartn-1];
180 partlist[refi->thispartn-1]= refi;
181 for (j=refi->maxpartn-1; j>=0 && partlist[j]; j--);
182
183 if (j>=0) {
184 struct dpkg_error err;
185 int fd_src, fd_dst;
186 int ap;
187 char *p, *q;
188
189 p = str_fmt("%s/t.%lx", opt_depotdir, (long)getpid());
190 q = str_fmt("%s/%s.%jx.%x.%x", opt_depotdir, refi->md5sum,
191 (intmax_t)refi->maxpartlen, refi->thispartn, refi->maxpartn);
192
193 fd_src = open(partfile, O_RDONLY);
194 if (fd_src < 0)
195 ohshite(_("unable to reopen part file '%.250s'"), partfile);
196 fd_dst = creat(p, 0644);
197 if (fd_dst < 0)
198 ohshite(_("unable to open new depot file '%.250s'"), p);
199
200 if (fd_fd_copy(fd_src, fd_dst, refi->filesize, &err) < 0)
201 ohshit(_("cannot extract split package part '%s': %s"),
202 partfile, err.str);
203
204 if (fsync(fd_dst))
205 ohshite(_("unable to sync file '%s'"), p);
206 if (close(fd_dst))
207 ohshite(_("unable to close file '%s'"), p);
208 close(fd_src);
209
210 if (rename(p, q))
211 ohshite(_("unable to rename new depot file '%.250s' to '%.250s'"), p, q);
212 free(q);
213 free(p);
214
215 printf(_("Part %d of package %s filed (still want "),refi->thispartn,refi->package);
216 /* There are still some parts missing. */
217 for (i=0, ap=0; i<refi->maxpartn; i++)
218 if (!partlist[i])
219 printf("%s%d", !ap++ ? "" : i == (unsigned int)j ? _(" and ") : ", ", i + 1);
220 printf(").\n");
221
222 dir_sync_path(opt_depotdir);
223 } else {
224
225 /* We have all the parts. */
226 reassemble(partlist, opt_outputfile);
227
228 /* OK, delete all the parts (except the new one, which we never copied). */
229 partlist[refi->thispartn-1]= otherthispart;
230 for (i=0; i<refi->maxpartn; i++)
231 if (partlist[i])
232 if (unlink(partlist[i]->filename))
233 ohshite(_("unable to delete used-up depot file '%.250s'"),
234 partlist[i]->filename);
235
236 }
237
238 m_output(stderr, _("<standard error>"));
239
240 return 0;
241 }
242
243 int
do_queue(const char * const * argv)244 do_queue(const char *const *argv)
245 {
246 struct partqueue *queue;
247 struct partqueue *pq;
248 const char *head;
249 struct stat stab;
250 off_t bytes;
251
252 if (*argv)
253 badusage(_("--%s takes no arguments"), cipaction->olong);
254
255 queue = scandepot();
256
257 head= N_("Junk files left around in the depot directory:\n");
258 for (pq= queue; pq; pq= pq->nextinqueue) {
259 if (pq->info.md5sum) continue;
260 fputs(gettext(head),stdout); head= "";
261 if (lstat(pq->info.filename,&stab))
262 ohshit(_("unable to stat '%.250s'"), pq->info.filename);
263 if (S_ISREG(stab.st_mode)) {
264 bytes= stab.st_size;
265 printf(_(" %s (%jd bytes)\n"), pq->info.filename, (intmax_t)bytes);
266 } else {
267 printf(_(" %s (not a plain file)\n"),pq->info.filename);
268 }
269 }
270 if (!*head) putchar('\n');
271
272 head= N_("Packages not yet reassembled:\n");
273 for (pq= queue; pq; pq= pq->nextinqueue) {
274 struct partinfo ti;
275 unsigned int i;
276
277 if (!pq->info.md5sum) continue;
278 mustgetpartinfo(pq->info.filename,&ti);
279 fputs(gettext(head),stdout); head= "";
280 printf(_(" Package %s: part(s) "), ti.package);
281 bytes= 0;
282 for (i=0; i<ti.maxpartn; i++) {
283 struct partqueue *qq;
284
285 for (qq= pq;
286 qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
287 qq= qq->nextinqueue);
288 if (qq) {
289 printf("%d ",i+1);
290 if (lstat(qq->info.filename,&stab))
291 ohshite(_("unable to stat '%.250s'"), qq->info.filename);
292 if (!S_ISREG(stab.st_mode))
293 ohshit(_("part file '%.250s' is not a plain file"), qq->info.filename);
294 bytes+= stab.st_size;
295
296 /* Don't find this package again. */
297 qq->info.md5sum = NULL;
298 }
299 }
300 printf(_("(total %jd bytes)\n"), (intmax_t)bytes);
301 }
302 m_output(stdout, _("<standard output>"));
303
304 return 0;
305 }
306
307 enum discard_which {
308 DISCARD_PART_JUNK,
309 DISCARD_PART_PACKAGE,
310 DISCARD_PART_ALL,
311 };
312
313 static void
discard_parts(struct partqueue * queue,enum discard_which which,const char * package)314 discard_parts(struct partqueue *queue, enum discard_which which,
315 const char *package)
316 {
317 struct partqueue *pq;
318
319 for (pq= queue; pq; pq= pq->nextinqueue) {
320 switch (which) {
321 case DISCARD_PART_JUNK:
322 if (pq->info.md5sum) continue;
323 break;
324 case DISCARD_PART_PACKAGE:
325 if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
326 break;
327 case DISCARD_PART_ALL:
328 break;
329 default:
330 internerr("unknown discard_which '%d'", which);
331 }
332 if (unlink(pq->info.filename))
333 ohshite(_("unable to discard '%.250s'"), pq->info.filename);
334 printf(_("Deleted %s.\n"),pq->info.filename);
335 }
336 }
337
338 int
do_discard(const char * const * argv)339 do_discard(const char *const *argv)
340 {
341 const char *thisarg;
342 struct partqueue *queue;
343 struct partqueue *pq;
344
345 queue = scandepot();
346 if (*argv) {
347 for (pq= queue; pq; pq= pq->nextinqueue)
348 if (pq->info.md5sum)
349 mustgetpartinfo(pq->info.filename,&pq->info);
350 discard_parts(queue, DISCARD_PART_JUNK, NULL);
351 while ((thisarg = *argv++))
352 discard_parts(queue, DISCARD_PART_PACKAGE, thisarg);
353 } else {
354 discard_parts(queue, DISCARD_PART_ALL, NULL);
355 }
356
357 return 0;
358 }
359