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