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 : :
234 : : /* We have all the parts. */
235 : 0 : reassemble(partlist, opt_outputfile);
236 : :
237 : : /* OK, delete all the parts (except the new one, which we never copied). */
238 : 0 : partlist[refi->thispartn-1]= otherthispart;
239 [ # # ]: 0 : for (i=0; i<refi->maxpartn; i++)
240 [ # # ]: 0 : if (partlist[i])
241 [ # # ]: 0 : if (unlink(partlist[i]->filename))
242 : 0 : ohshite(_("unable to delete used-up depot file '%.250s'"),
243 : 0 : partlist[i]->filename);
244 : :
245 : : }
246 : :
247 : 0 : m_output(stderr, _("<standard error>"));
248 : :
249 : 0 : return 0;
250 : : }
251 : :
252 : : int
253 : 0 : do_queue(const char *const *argv)
254 : : {
255 : : struct partqueue *queue;
256 : : struct partqueue *pq;
257 : : const char *head;
258 : : struct stat stab;
259 : : off_t bytes;
260 : :
261 [ # # ]: 0 : if (*argv)
262 : 0 : badusage(_("--%s takes no arguments"), cipaction->olong);
263 : :
264 : 0 : queue = scandepot();
265 : :
266 : 0 : head= N_("Junk files left around in the depot directory:\n");
267 [ # # ]: 0 : for (pq= queue; pq; pq= pq->nextinqueue) {
268 [ # # ]: 0 : if (pq->info.md5sum) continue;
269 : 0 : fputs(gettext(head),stdout); head= "";
270 [ # # ]: 0 : if (lstat(pq->info.filename,&stab))
271 : 0 : ohshit(_("unable to stat '%.250s'"), pq->info.filename);
272 [ # # ]: 0 : if (S_ISREG(stab.st_mode)) {
273 : 0 : bytes= stab.st_size;
274 : 0 : printf(_(" %s (%jd bytes)\n"), pq->info.filename, (intmax_t)bytes);
275 : : } else {
276 : 0 : printf(_(" %s (not a plain file)\n"),pq->info.filename);
277 : : }
278 : : }
279 [ # # ]: 0 : if (!*head) putchar('\n');
280 : :
281 : 0 : head= N_("Packages not yet reassembled:\n");
282 [ # # ]: 0 : for (pq= queue; pq; pq= pq->nextinqueue) {
283 : : struct partinfo ti;
284 : : int i;
285 : :
286 [ # # ]: 0 : if (!pq->info.md5sum) continue;
287 : 0 : mustgetpartinfo(pq->info.filename,&ti);
288 : 0 : fputs(gettext(head),stdout); head= "";
289 : 0 : printf(_(" Package %s: part(s) "), ti.package);
290 : 0 : bytes= 0;
291 [ # # ]: 0 : for (i=0; i<ti.maxpartn; i++) {
292 : : struct partqueue *qq;
293 : :
294 : 0 : for (qq= pq;
295 [ # # # # : 0 : qq && !(partmatches(&qq->info,&ti) && qq->info.thispartn == i+1);
# # ]
296 : 0 : qq= qq->nextinqueue);
297 [ # # ]: 0 : if (qq) {
298 : 0 : printf("%d ",i+1);
299 [ # # ]: 0 : if (lstat(qq->info.filename,&stab))
300 : 0 : ohshite(_("unable to stat '%.250s'"), qq->info.filename);
301 [ # # ]: 0 : if (!S_ISREG(stab.st_mode))
302 : 0 : ohshit(_("part file '%.250s' is not a plain file"), qq->info.filename);
303 : 0 : bytes+= stab.st_size;
304 : :
305 : : /* Don't find this package again. */
306 : 0 : qq->info.md5sum = NULL;
307 : : }
308 : : }
309 : 0 : printf(_("(total %jd bytes)\n"), (intmax_t)bytes);
310 : : }
311 : 0 : m_output(stdout, _("<standard output>"));
312 : :
313 : 0 : return 0;
314 : : }
315 : :
316 : : enum discard_which {
317 : : DISCARD_PART_JUNK,
318 : : DISCARD_PART_PACKAGE,
319 : : DISCARD_PART_ALL,
320 : : };
321 : :
322 : : static void
323 : 0 : discard_parts(struct partqueue *queue, enum discard_which which,
324 : : const char *package)
325 : : {
326 : : struct partqueue *pq;
327 : :
328 [ # # ]: 0 : for (pq= queue; pq; pq= pq->nextinqueue) {
329 [ # # # # ]: 0 : switch (which) {
330 : 0 : case DISCARD_PART_JUNK:
331 [ # # ]: 0 : if (pq->info.md5sum) continue;
332 : 0 : break;
333 : 0 : case DISCARD_PART_PACKAGE:
334 [ # # # # ]: 0 : if (!pq->info.md5sum || strcasecmp(pq->info.package,package)) continue;
335 : 0 : break;
336 : 0 : case DISCARD_PART_ALL:
337 : 0 : break;
338 : 0 : default:
339 : 0 : internerr("unknown discard_which '%d'", which);
340 : : }
341 [ # # ]: 0 : if (unlink(pq->info.filename))
342 : 0 : ohshite(_("unable to discard '%.250s'"), pq->info.filename);
343 : 0 : printf(_("Deleted %s.\n"),pq->info.filename);
344 : : }
345 : 0 : }
346 : :
347 : : int
348 : 0 : do_discard(const char *const *argv)
349 : : {
350 : : const char *thisarg;
351 : : struct partqueue *queue;
352 : : struct partqueue *pq;
353 : :
354 : 0 : queue = scandepot();
355 [ # # ]: 0 : if (*argv) {
356 [ # # ]: 0 : for (pq= queue; pq; pq= pq->nextinqueue)
357 [ # # ]: 0 : if (pq->info.md5sum)
358 : 0 : mustgetpartinfo(pq->info.filename,&pq->info);
359 : 0 : discard_parts(queue, DISCARD_PART_JUNK, NULL);
360 [ # # ]: 0 : while ((thisarg = *argv++))
361 : 0 : discard_parts(queue, DISCARD_PART_PACKAGE, thisarg);
362 : : } else {
363 : 0 : discard_parts(queue, DISCARD_PART_ALL, NULL);
364 : : }
365 : :
366 : 0 : return 0;
367 : : }
|