45d3ff6925b0242c30f91eb2eea5289728d71bde
[dpkg] / src / unpack.c
1 /*
2 * dpkg - main program for package management
3 * unpack.c - .deb archive unpacking
4 *
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
7 * Copyright © 2011 Linaro Limited
8 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
9 *
10 * This is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24 #include <config.h>
25 #include <compat.h>
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30
31 #include <assert.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <time.h>
35 #include <utime.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <unistd.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <stdio.h>
42
43 #include <dpkg/i18n.h>
44 #include <dpkg/c-ctype.h>
45 #include <dpkg/dpkg.h>
46 #include <dpkg/dpkg-db.h>
47 #include <dpkg/pkg.h>
48 #include <dpkg/pkg-queue.h>
49 #include <dpkg/path.h>
50 #include <dpkg/buffer.h>
51 #include <dpkg/subproc.h>
52 #include <dpkg/dir.h>
53 #include <dpkg/tarfn.h>
54 #include <dpkg/options.h>
55 #include <dpkg/triglib.h>
56
57 #include "filesdb.h"
58 #include "file-match.h"
59 #include "infodb.h"
60 #include "main.h"
61 #include "archives.h"
62
63 static const char *
64 summarize_filename(const char *filename)
65 {
66 const char *pfilename;
67 char *pfilenamebuf;
68
69 for (pfilename = filename;
70 pfilename && strlen(pfilename) > 30 && strchr(pfilename, '/') != NULL;
71 pfilename++)
72 pfilename = strchr(pfilename, '/');
73
74 if (pfilename && pfilename != filename) {
75 pfilenamebuf = nfmalloc(strlen(pfilename) + 5);
76 sprintf(pfilenamebuf, _(".../%s"), pfilename);
77 pfilename = pfilenamebuf;
78 } else {
79 pfilename = filename;
80 }
81
82 return pfilename;
83 }
84
85 static bool
86 deb_reassemble(const char **filename, const char **pfilename)
87 {
88 static char *reasmbuf = NULL;
89 struct stat stab;
90 int status;
91 pid_t pid;
92
93 if (!reasmbuf)
94 reasmbuf = dpkg_db_get_path(REASSEMBLETMP);
95 if (unlink(reasmbuf) && errno != ENOENT)
96 ohshite(_("error ensuring '%.250s' doesn't exist"), reasmbuf);
97
98 push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf);
99
100 pid = subproc_fork();
101 if (!pid) {
102 execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, *filename, NULL);
103 ohshite(_("unable to execute %s (%s)"),
104 _("split package reassembly"), SPLITTER);
105 }
106 status = subproc_reap(pid, SPLITTER, SUBPROC_RETERROR);
107 switch (status) {
108 case 0:
109 /* It was a part - is it complete? */
110 if (!stat(reasmbuf, &stab)) {
111 /* Yes. */
112 *filename = reasmbuf;
113 *pfilename = _("reassembled package file");
114 break;
115 } else if (errno == ENOENT) {
116 /* No. That's it, we skip it. */
117 return false;
118 }
119 case 1:
120 /* No, it wasn't a part. */
121 break;
122 default:
123 ohshit(_("subprocess %s returned error exit status %d"), SPLITTER, status);
124 }
125
126 return true;
127 }
128
129 static void
130 deb_verify(const char *filename)
131 {
132 pid_t pid;
133
134 /* We have to check on every unpack, in case the debsig-verify package
135 * gets installed or removed. */
136 if (!find_command(DEBSIGVERIFY))
137 return;
138
139 printf(_("Authenticating %s ...\n"), filename);
140 fflush(stdout);
141 pid = subproc_fork();
142 if (!pid) {
143 execlp(DEBSIGVERIFY, DEBSIGVERIFY, "-q", filename, NULL);
144 ohshite(_("unable to execute %s (%s)"),
145 _("package signature verification"), DEBSIGVERIFY);
146 } else {
147 int status;
148
149 status = subproc_reap(pid, "debsig-verify", SUBPROC_NOCHECK);
150 if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
151 if (!fc_badverify)
152 ohshit(_("verification on package %s failed!"), filename);
153 else
154 notice(_("verification on package %s failed; "
155 "but installing anyway as you requested"), filename);
156 } else {
157 printf(_("passed\n"));
158 }
159 }
160 }
161
162 static char *
163 get_control_dir(char *cidir)
164 {
165 if (f_noact) {
166 char *tmpdir;
167
168 tmpdir = mkdtemp(path_make_temp_template("dpkg"));
169 if (tmpdir == NULL)
170 ohshite(_("unable to create temporary directory"));
171
172 cidir = m_realloc(cidir, strlen(tmpdir) + MAXCONTROLFILENAME + 10);
173
174 strcpy(cidir, tmpdir);
175
176 free(tmpdir);
177 } else {
178 const char *admindir;
179
180 admindir = dpkg_db_get_dir();
181
182 /* The admindir length is always constant on a dpkg execution run. */
183 if (cidir == NULL)
184 cidir = m_malloc(strlen(admindir) + sizeof(CONTROLDIRTMP) +
185 MAXCONTROLFILENAME + 10);
186
187 /* We want it to be on the same filesystem so that we can
188 * use rename(2) to install the postinst &c. */
189 strcpy(cidir, admindir);
190 strcat(cidir, "/" CONTROLDIRTMP);
191
192 /* Make sure the control information directory is empty. */
193 path_remove_tree(cidir);
194 }
195
196 strcat(cidir, "/");
197
198 return cidir;
199 }
200
201 static void
202 pkg_check_depcon(struct pkginfo *pkg, const char *pfilename)
203 {
204 struct dependency *dsearch;
205 struct deppossi *psearch;
206 struct pkginfo *fixbytrigaw;
207 static struct varbuf depprobwhy;
208
209 /* Check if anything is installed that we conflict with, or not installed
210 * that we need. */
211 pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
212
213 for (dsearch = pkg->available.depends; dsearch; dsearch = dsearch->next) {
214 switch (dsearch->type) {
215 case dep_conflicts:
216 /* Look for things we conflict with. */
217 check_conflict(dsearch, pkg, pfilename);
218 break;
219 case dep_breaks:
220 /* Look for things we break. */
221 check_breaks(dsearch, pkg, pfilename);
222 break;
223 case dep_provides:
224 /* Look for things that conflict with what we provide. */
225 for (psearch = dsearch->list->ed->depended.installed;
226 psearch;
227 psearch = psearch->rev_next) {
228 if (psearch->up->type != dep_conflicts)
229 continue;
230 check_conflict(psearch->up, pkg, pfilename);
231 }
232 break;
233 case dep_suggests:
234 case dep_recommends:
235 case dep_depends:
236 case dep_replaces:
237 case dep_enhances:
238 /* Ignore these here. */
239 break;
240 case dep_predepends:
241 if (!depisok(dsearch, &depprobwhy, NULL, &fixbytrigaw, true)) {
242 if (fixbytrigaw) {
243 while (fixbytrigaw->trigaw.head)
244 trigproc(fixbytrigaw->trigaw.head->pend, TRIGPROC_REQUIRED);
245 } else {
246 varbuf_end_str(&depprobwhy);
247 notice(_("regarding %s containing %s, pre-dependency problem:\n%s"),
248 pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
249 depprobwhy.buf);
250 if (!force_depends(dsearch->list))
251 ohshit(_("pre-dependency problem - not installing %.250s"),
252 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
253 warning(_("ignoring pre-dependency problem!"));
254 }
255 }
256 }
257 }
258
259 /* Look for things that conflict with us. */
260 for (psearch = pkg->set->depended.installed; psearch; psearch = psearch->rev_next) {
261 if (psearch->up->type != dep_conflicts)
262 continue;
263
264 check_conflict(psearch->up, pkg, pfilename);
265 }
266 }
267
268 static void
269 pkg_deconfigure_others(struct pkginfo *pkg)
270 {
271 struct pkg_deconf_list *deconpil;
272
273 for (deconpil = deconfigure; deconpil; deconpil = deconpil->next) {
274 struct pkginfo *removing = deconpil->pkg_removal;
275
276 if (removing)
277 printf(_("De-configuring %s (%s), to allow removal of %s (%s) ...\n"),
278 pkg_name(deconpil->pkg, pnaw_nonambig),
279 versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig),
280 pkg_name(removing, pnaw_nonambig),
281 versiondescribe(&removing->installed.version, vdew_nonambig));
282 else
283 printf(_("De-configuring %s (%s) ...\n"),
284 pkg_name(deconpil->pkg, pnaw_nonambig),
285 versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig));
286
287 trig_activate_packageprocessing(deconpil->pkg);
288 pkg_set_status(deconpil->pkg, PKG_STAT_HALFCONFIGURED);
289 modstatdb_note(deconpil->pkg);
290
291 /* This means that we *either* go and run postinst abort-deconfigure,
292 * *or* queue the package for later configure processing, depending
293 * on which error cleanup route gets taken. */
294 push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy,
295 ok_prermdeconfigure, ehflag_normaltidy,
296 3, (void *)deconpil->pkg, (void *)removing, (void *)pkg);
297
298 if (removing) {
299 maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
300 "deconfigure", "in-favour",
301 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
302 versiondescribe(&pkg->available.version,
303 vdew_nonambig),
304 "removing",
305 pkg_name(removing, pnaw_nonambig),
306 versiondescribe(&removing->installed.version,
307 vdew_nonambig),
308 NULL);
309 } else {
310 maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
311 "deconfigure", "in-favour",
312 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
313 versiondescribe(&pkg->available.version,
314 vdew_nonambig),
315 NULL);
316 }
317 }
318 }
319
320 /**
321 * Read the conffiles, and copy the hashes across.
322 */
323 static void
324 deb_parse_conffiles(struct pkginfo *pkg, const char *control_conffiles,
325 struct filenamenode_queue *newconffiles)
326 {
327 FILE *conff;
328 char conffilenamebuf[MAXCONFFILENAME];
329
330 conff = fopen(control_conffiles, "r");
331 if (conff == NULL) {
332 if (errno == ENOENT)
333 return;
334 ohshite(_("error trying to open %.250s"), control_conffiles);
335 }
336
337 push_cleanup(cu_closestream, ehflag_bombout, NULL, 0, 1, conff);
338
339 while (fgets(conffilenamebuf, MAXCONFFILENAME - 2, conff)) {
340 struct pkginfo *otherpkg;
341 struct filepackages_iterator *iter;
342 struct filenamenode *namenode;
343 struct fileinlist *newconff;
344 struct conffile *searchconff;
345 char *p;
346
347 p = conffilenamebuf + strlen(conffilenamebuf);
348 assert(p != conffilenamebuf);
349 if (p[-1] != '\n')
350 ohshit(_("conffile name '%s' is too long, or missing final newline"),
351 conffilenamebuf);
352 while (p > conffilenamebuf && c_isspace(p[-1]))
353 --p;
354 if (p == conffilenamebuf)
355 continue;
356 *p = '\0';
357
358 namenode = findnamenode(conffilenamebuf, 0);
359 namenode->oldhash = NEWCONFFILEFLAG;
360 newconff = tar_filenamenode_queue_push(newconffiles, namenode);
361
362 /*
363 * Let's see if any packages have this file.
364 *
365 * If they do we check to see if they listed it as a conffile,
366 * and if they did we copy the hash across. Since (for plain
367 * file conffiles, which is the only kind we are supposed to
368 * have) there will only be one package which ‘has’ the file,
369 * this will usually mean we only look in the package which
370 * we are installing now.
371 *
372 * The ‘conffiles’ data in the status file is ignored when a
373 * package is not also listed in the file ownership database as
374 * having that file. If several packages are listed as owning
375 * the file we pick one at random.
376 */
377 searchconff = NULL;
378
379 iter = filepackages_iter_new(newconff->namenode);
380 while ((otherpkg = filepackages_iter_next(iter))) {
381 debug(dbg_conffdetail,
382 "process_archive conffile '%s' in package %s - conff ?",
383 newconff->namenode->name, pkg_name(otherpkg, pnaw_always));
384 for (searchconff = otherpkg->installed.conffiles;
385 searchconff && strcmp(newconff->namenode->name, searchconff->name);
386 searchconff = searchconff->next)
387 debug(dbg_conffdetail,
388 "process_archive conffile '%s' in package %s - conff ? not '%s'",
389 newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
390 searchconff->name);
391 if (searchconff) {
392 debug(dbg_conff,
393 "process_archive conffile '%s' package=%s %s hash=%s",
394 newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
395 otherpkg == pkg ? "same" : "different!",
396 searchconff->hash);
397 if (otherpkg == pkg)
398 break;
399 }
400 }
401 filepackages_iter_free(iter);
402
403 if (searchconff) {
404 /* We don't copy ‘obsolete’; it's not obsolete in the new package. */
405 newconff->namenode->oldhash = searchconff->hash;
406 } else {
407 debug(dbg_conff, "process_archive conffile '%s' no package, no hash",
408 newconff->namenode->name);
409 }
410 newconff->namenode->flags |= fnnf_new_conff;
411 }
412
413 if (ferror(conff))
414 ohshite(_("read error in %.250s"), control_conffiles);
415 pop_cleanup(ehflag_normaltidy); /* conff = fopen() */
416 if (fclose(conff))
417 ohshite(_("error closing %.250s"), control_conffiles);
418 }
419
420 static struct pkg_queue conflictors = PKG_QUEUE_INIT;
421
422 void
423 enqueue_conflictor(struct pkginfo *pkg)
424 {
425 pkg_queue_push(&conflictors, pkg);
426 }
427
428 static void
429 pkg_infodb_remove_file(const char *filename, const char *filetype)
430 {
431 if (unlink(filename))
432 ohshite(_("unable to delete control info file '%.250s'"), filename);
433
434 debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
435 }
436
437 static struct match_node *match_head = NULL;
438
439 static void
440 pkg_infodb_update_file(const char *filename, const char *filetype)
441 {
442 if (strlen(filetype) > MAXCONTROLFILENAME)
443 ohshit(_("old version of package has overly-long info file name starting '%.250s'"),
444 filename);
445
446 /* We do the list separately. */
447 if (strcmp(filetype, LISTFILE) == 0)
448 return;
449
450 /* We keep files to rename in a list as doing the rename immediately
451 * might influence the current readdir(), the just renamed file might
452 * be returned a second time as it's actually a new file from the
453 * point of view of the filesystem. */
454 match_head = match_node_new(filename, filetype, match_head);
455 }
456
457 static void
458 pkg_infodb_update(struct pkginfo *pkg, char *cidir, char *cidirrest)
459 {
460 struct match_node *match_node;
461 DIR *dsd;
462 struct dirent *de;
463
464 /* Deallocate the match list in case we aborted previously. */
465 while ((match_node = match_head)) {
466 match_head = match_node->next;
467 match_node_free(match_node);
468 }
469
470 pkg_infodb_foreach(pkg, &pkg->available, pkg_infodb_update_file);
471
472 while ((match_node = match_head)) {
473 strcpy(cidirrest, match_node->filetype);
474
475 if (!rename(cidir, match_node->filename)) {
476 debug(dbg_scripts, "process_archive info installed %s as %s",
477 cidir, match_node->filename);
478 } else if (errno == ENOENT) {
479 /* Right, no new version. */
480 if (unlink(match_node->filename))
481 ohshite(_("unable to remove obsolete info file '%.250s'"),
482 match_node->filename);
483 debug(dbg_scripts, "process_archive info unlinked %s",
484 match_node->filename);
485 } else {
486 ohshite(_("unable to install (supposed) new info file '%.250s'"), cidir);
487 }
488 match_head = match_node->next;
489 match_node_free(match_node);
490 }
491
492 /* The control directory itself. */
493 cidirrest[0] = '\0';
494 dsd = opendir(cidir);
495 if (!dsd)
496 ohshite(_("unable to open temp control directory"));
497 push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
498 while ((de = readdir(dsd))) {
499 const char *newinfofilename;
500
501 if (strchr(de->d_name, '.')) {
502 debug(dbg_scripts, "process_archive tmp.ci script/file '%s' contains dot",
503 de->d_name);
504 continue;
505 }
506 if (strlen(de->d_name) > MAXCONTROLFILENAME)
507 ohshit(_("package contains overly-long control info file name (starting '%.50s')"),
508 de->d_name);
509
510 strcpy(cidirrest, de->d_name);
511
512 /* First we check it's not a directory. */
513 if (rmdir(cidir) == 0)
514 ohshit(_("package control info contained directory '%.250s'"), cidir);
515 else if (errno != ENOTDIR)
516 ohshite(_("package control info rmdir of '%.250s' didn't say not a dir"),
517 de->d_name);
518
519 /* Ignore the control file. */
520 if (strcmp(de->d_name, CONTROLFILE) == 0) {
521 debug(dbg_scripts, "process_archive tmp.ci script/file '%s' is control",
522 cidir);
523 continue;
524 }
525 if (strcmp(de->d_name, LISTFILE) == 0) {
526 warning(_("package %s contained list as info file"),
527 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
528 continue;
529 }
530
531 /* Right, install it */
532 newinfofilename = pkg_infodb_get_file(pkg, &pkg->available, de->d_name);
533 if (rename(cidir, newinfofilename))
534 ohshite(_("unable to install new info file '%.250s' as '%.250s'"),
535 cidir, newinfofilename);
536
537 debug(dbg_scripts,
538 "process_archive tmp.ci script/file '%s' installed as '%s'",
539 cidir, newinfofilename);
540 }
541 pop_cleanup(ehflag_normaltidy); /* closedir */
542
543 /* If the old and new versions use a different infodb layout, get rid
544 * of the files using the old layout. */
545 if (pkg->installed.multiarch != pkg->available.multiarch &&
546 (pkg->installed.multiarch == PKG_MULTIARCH_SAME ||
547 pkg->available.multiarch == PKG_MULTIARCH_SAME)) {
548 debug(dbg_scripts,
549 "process_archive remove old info files after db layout switch");
550 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
551 }
552
553 dir_sync_path(pkg_infodb_get_dir());
554 }
555
556 static void
557 pkg_remove_old_files(struct pkginfo *pkg,
558 struct filenamenode_queue *newfiles_queue,
559 struct filenamenode_queue *newconffiles)
560 {
561 struct reversefilelistiter rev_iter;
562 struct filenamenode *namenode;
563 struct stat stab, oldfs;
564
565 reversefilelist_init(&rev_iter, pkg->clientdata->files);
566
567 while ((namenode = reversefilelist_next(&rev_iter))) {
568 struct filenamenode *usenode;
569
570 if ((namenode->flags & fnnf_new_conff) ||
571 (namenode->flags & fnnf_new_inarchive))
572 continue;
573
574 usenode = namenodetouse(namenode, pkg, &pkg->installed);
575
576 varbuf_rollback(&fnamevb, &fname_state);
577 varbuf_add_str(&fnamevb, usenode->name);
578 varbuf_end_str(&fnamevb);
579
580 if (!stat(namenode->name, &stab) && S_ISDIR(stab.st_mode)) {
581 debug(dbg_eachfiledetail, "process_archive: %s is a directory",
582 namenode->name);
583 if (dir_is_used_by_others(namenode, pkg))
584 continue;
585 }
586
587 if (lstat(fnamevb.buf, &oldfs)) {
588 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
589 warning(_("could not stat old file '%.250s' so not deleting it: %s"),
590 fnamevb.buf, strerror(errno));
591 continue;
592 }
593 if (S_ISDIR(oldfs.st_mode)) {
594 trig_path_activate(usenode, pkg);
595
596 /* Do not try to remove the root directory. */
597 if (strcmp(usenode->name, "/.") == 0)
598 continue;
599
600 if (rmdir(fnamevb.buf)) {
601 warning(_("unable to delete old directory '%.250s': %s"),
602 namenode->name, strerror(errno));
603 } else if ((namenode->flags & fnnf_old_conff)) {
604 warning(_("old conffile '%.250s' was an empty directory "
605 "(and has now been deleted)"), namenode->name);
606 }
607 } else {
608 struct fileinlist *sameas = NULL;
609 struct fileinlist *cfile;
610 static struct stat empty_stat;
611 struct varbuf cfilename = VARBUF_INIT;
612
613 /*
614 * Ok, it's an old file, but is it really not in the new package?
615 * It might be known by a different name because of symlinks.
616 *
617 * We need to check to make sure, so we stat the file, then compare
618 * it to the new list. If we find a dev/inode match, we assume they
619 * are the same file, and leave it alone. NOTE: we don't check in
620 * other packages for sanity reasons (we don't want to stat _all_
621 * the files on the system).
622 *
623 * We run down the list of _new_ files in this package. This keeps
624 * the process a little leaner. We are only worried about new ones
625 * since ones that stayed the same don't really apply here.
626 */
627
628 /* If we can't stat the old or new file, or it's a directory,
629 * we leave it up to the normal code. */
630 debug(dbg_eachfile, "process_archive: checking %s for same files on "
631 "upgrade/downgrade", fnamevb.buf);
632
633 for (cfile = newfiles_queue->head; cfile; cfile = cfile->next) {
634 /* If the file has been filtered then treat it as if it didn't exist
635 * on the file system. */
636 if (cfile->namenode->flags & fnnf_filtered)
637 continue;
638
639 if (!cfile->namenode->filestat) {
640 struct stat tmp_stat;
641
642 varbuf_reset(&cfilename);
643 varbuf_add_str(&cfilename, instdir);
644 varbuf_add_str(&cfilename, cfile->namenode->name);
645 varbuf_end_str(&cfilename);
646
647 if (lstat(cfilename.buf, &tmp_stat) == 0) {
648 cfile->namenode->filestat = nfmalloc(sizeof(struct stat));
649 memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat));
650 } else {
651 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
652 ohshite(_("unable to stat other new file '%.250s'"),
653 cfile->namenode->name);
654 cfile->namenode->filestat = &empty_stat;
655 continue;
656 }
657 }
658
659 if (cfile->namenode->filestat == &empty_stat)
660 continue;
661
662 if (oldfs.st_dev == cfile->namenode->filestat->st_dev &&
663 oldfs.st_ino == cfile->namenode->filestat->st_ino) {
664 if (sameas)
665 warning(_("old file '%.250s' is the same as several new files! "
666 "(both '%.250s' and '%.250s')"), fnamevb.buf,
667 sameas->namenode->name, cfile->namenode->name);
668 sameas = cfile;
669 debug(dbg_eachfile, "process_archive: not removing %s, "
670 "since it matches %s", fnamevb.buf, cfile->namenode->name);
671 }
672 }
673
674 varbuf_destroy(&cfilename);
675
676 if ((namenode->flags & fnnf_old_conff)) {
677 if (sameas) {
678 if (sameas->namenode->flags & fnnf_new_conff) {
679 if (strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG) == 0) {
680 sameas->namenode->oldhash = namenode->oldhash;
681 debug(dbg_eachfile, "process_archive: old conff %s "
682 "is same as new conff %s, copying hash",
683 namenode->name, sameas->namenode->name);
684 } else {
685 debug(dbg_eachfile, "process_archive: old conff %s "
686 "is same as new conff %s but latter already has hash",
687 namenode->name, sameas->namenode->name);
688 }
689 }
690 } else {
691 debug(dbg_eachfile, "process_archive: old conff %s "
692 "is disappearing", namenode->name);
693 namenode->flags |= fnnf_obs_conff;
694 tar_filenamenode_queue_push(newconffiles, namenode);
695 tar_filenamenode_queue_push(newfiles_queue, namenode);
696 }
697 continue;
698 }
699
700 if (sameas)
701 continue;
702
703 trig_path_activate(usenode, pkg);
704
705 if (secure_unlink_statted(fnamevb.buf, &oldfs)) {
706 warning(_("unable to securely remove old file '%.250s': %s"),
707 namenode->name, strerror(errno));
708 }
709 } /* !S_ISDIR */
710 }
711 }
712
713 static void
714 pkg_update_fields(struct pkginfo *pkg, struct filenamenode_queue *newconffiles)
715 {
716 struct dependency *newdeplist, **newdeplistlastp;
717 struct dependency *newdep, *dep;
718 struct deppossi **newpossilastp, *possi, *newpossi;
719 struct conffile **iconffileslastp, *newiconff;
720 struct fileinlist *cfile;
721
722 /* The dependencies are the most difficult. We have to build
723 * a whole new forward dependency tree. At least the reverse
724 * links (linking our deppossi's into the reverse chains)
725 * can be done by copy_dependency_links. */
726 newdeplist = NULL;
727 newdeplistlastp = &newdeplist;
728 for (dep = pkg->available.depends; dep; dep = dep->next) {
729 newdep = nfmalloc(sizeof(struct dependency));
730 newdep->up = pkg;
731 newdep->next = NULL;
732 newdep->list = NULL;
733 newpossilastp = &newdep->list;
734
735 for (possi = dep->list; possi; possi = possi->next) {
736 newpossi = nfmalloc(sizeof(struct deppossi));
737 newpossi->up = newdep;
738 newpossi->ed = possi->ed;
739 newpossi->next = NULL;
740 newpossi->rev_next = newpossi->rev_prev = NULL;
741 newpossi->arch_is_implicit = possi->arch_is_implicit;
742 newpossi->arch = possi->arch;
743 newpossi->verrel = possi->verrel;
744 if (possi->verrel != DPKG_RELATION_NONE)
745 newpossi->version = possi->version;
746 else
747 dpkg_version_blank(&newpossi->version);
748 newpossi->cyclebreak = false;
749 *newpossilastp = newpossi;
750 newpossilastp = &newpossi->next;
751 }
752 newdep->type = dep->type;
753 *newdeplistlastp = newdep;
754 newdeplistlastp = &newdep->next;
755 }
756
757 /* Right, now we've replicated the forward tree, we
758 * get copy_dependency_links to remove all the old dependency
759 * structures from the reverse links and add the new dependency
760 * structures in instead. It also copies the new dependency
761 * structure pointer for this package into the right field. */
762 copy_dependency_links(pkg, &pkg->installed.depends, newdeplist, 0);
763
764 /* We copy the text fields. */
765 pkg->installed.essential = pkg->available.essential;
766 pkg->installed.multiarch = pkg->available.multiarch;
767 pkg->installed.description = pkg->available.description;
768 pkg->installed.maintainer = pkg->available.maintainer;
769 pkg->installed.source = pkg->available.source;
770 pkg->installed.arch = pkg->available.arch;
771 pkg->installed.pkgname_archqual = pkg->available.pkgname_archqual;
772 pkg->installed.installedsize = pkg->available.installedsize;
773 pkg->installed.version = pkg->available.version;
774 pkg->installed.origin = pkg->available.origin;
775 pkg->installed.bugs = pkg->available.bugs;
776
777 /* We have to generate our own conffiles structure. */
778 pkg->installed.conffiles = NULL;
779 iconffileslastp = &pkg->installed.conffiles;
780 for (cfile = newconffiles->head; cfile; cfile = cfile->next) {
781 newiconff = nfmalloc(sizeof(struct conffile));
782 newiconff->next = NULL;
783 newiconff->name = nfstrsave(cfile->namenode->name);
784 newiconff->hash = nfstrsave(cfile->namenode->oldhash);
785 newiconff->obsolete = !!(cfile->namenode->flags & fnnf_obs_conff);
786 *iconffileslastp = newiconff;
787 iconffileslastp = &newiconff->next;
788 }
789
790 /* We can just copy the arbitrary fields list, because it is
791 * never even rearranged. Phew! */
792 pkg->installed.arbs = pkg->available.arbs;
793 }
794
795 static void
796 pkg_disappear(struct pkginfo *pkg, struct pkginfo *infavour)
797 {
798 printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
799 pkg_name(pkg, pnaw_nonambig));
800 log_action("disappear", pkg, &pkg->installed);
801 debug(dbg_general, "pkg_disappear disappearing %s",
802 pkg_name(pkg, pnaw_always));
803
804 trig_activate_packageprocessing(pkg);
805 maintscript_installed(pkg, POSTRMFILE,
806 "post-removal script (for disappearance)",
807 "disappear",
808 pkgbin_name(infavour, &infavour->available,
809 pnaw_nonambig),
810 versiondescribe(&infavour->available.version,
811 vdew_nonambig),
812 NULL);
813
814 /* OK, now we delete all the stuff in the ‘info’ directory ... */
815 debug(dbg_general, "pkg_disappear cleaning info directory");
816 pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
817 dir_sync_path(pkg_infodb_get_dir());
818
819 pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
820 pkg_set_want(pkg, PKG_WANT_UNKNOWN);
821 pkg_reset_eflags(pkg);
822
823 dpkg_version_blank(&pkg->configversion);
824 pkgbin_blank(&pkg->installed);
825
826 pkg->clientdata->fileslistvalid = false;
827
828 modstatdb_note(pkg);
829 }
830
831 static void
832 pkg_disappear_others(struct pkginfo *pkg)
833 {
834 struct pkgiterator *iter;
835 struct pkginfo *otherpkg;
836 struct fileinlist *cfile;
837 struct deppossi *pdep;
838 struct dependency *providecheck;
839 struct varbuf depprobwhy = VARBUF_INIT;
840
841 iter = pkg_db_iter_new();
842 while ((otherpkg = pkg_db_iter_next_pkg(iter)) != NULL) {
843 ensure_package_clientdata(otherpkg);
844
845 if (otherpkg == pkg ||
846 otherpkg->status == PKG_STAT_NOTINSTALLED ||
847 otherpkg->status == PKG_STAT_CONFIGFILES ||
848 otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE ||
849 !otherpkg->clientdata->files)
850 continue;
851
852 /* Do not try to disappear other packages from the same set
853 * if they are Multi-Arch: same */
854 if (pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
855 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
856 otherpkg->set == pkg->set)
857 continue;
858
859 debug(dbg_veryverbose, "process_archive checking disappearance %s",
860 pkg_name(otherpkg, pnaw_always));
861 assert(otherpkg->clientdata->istobe == PKG_ISTOBE_NORMAL ||
862 otherpkg->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
863
864 for (cfile = otherpkg->clientdata->files;
865 cfile && strcmp(cfile->namenode->name, "/.") == 0;
866 cfile = cfile->next);
867 if (!cfile) {
868 debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
869 continue;
870 }
871 for (cfile = otherpkg->clientdata->files;
872 cfile && !filesavespackage(cfile, otherpkg, pkg);
873 cfile = cfile->next);
874 if (cfile)
875 continue;
876
877 /* So dependency things will give right answers ... */
878 otherpkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
879 debug(dbg_veryverbose, "process_archive disappear checking dependencies");
880 for (pdep = otherpkg->set->depended.installed;
881 pdep;
882 pdep = pdep->rev_next) {
883 if (pdep->up->type != dep_depends &&
884 pdep->up->type != dep_predepends &&
885 pdep->up->type != dep_recommends)
886 continue;
887
888 if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
889 continue;
890
891 varbuf_end_str(&depprobwhy);
892 debug(dbg_veryverbose,"process_archive cannot disappear: %s",
893 depprobwhy.buf);
894 break;
895 }
896 if (!pdep) {
897 /* If we haven't found a reason not to yet, let's look some more. */
898 for (providecheck = otherpkg->installed.depends;
899 providecheck;
900 providecheck = providecheck->next) {
901 if (providecheck->type != dep_provides)
902 continue;
903
904 for (pdep = providecheck->list->ed->depended.installed;
905 pdep;
906 pdep = pdep->rev_next) {
907 if (pdep->up->type != dep_depends &&
908 pdep->up->type != dep_predepends &&
909 pdep->up->type != dep_recommends)
910 continue;
911
912 if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
913 continue;
914
915 varbuf_end_str(&depprobwhy);
916 debug(dbg_veryverbose,
917 "process_archive cannot disappear (provides %s): %s",
918 providecheck->list->ed->name, depprobwhy.buf);
919 goto break_from_both_loops_at_once;
920 }
921 }
922 break_from_both_loops_at_once:;
923 }
924 otherpkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
925 if (pdep)
926 continue;
927
928 /* No, we're disappearing it. This is the wrong time to go and
929 * run maintainer scripts and things, as we can't back out. But
930 * what can we do ? It has to be run this late. */
931 pkg_disappear(otherpkg, pkg);
932 } /* while (otherpkg= ... */
933 pkg_db_iter_free(iter);
934 }
935
936 /**
937 * Check if all instances of a pkgset are getting in sync.
938 *
939 * If that's the case, the extraction is going to ensure consistency
940 * of shared files.
941 */
942 static bool
943 pkgset_getting_in_sync(struct pkginfo *pkg)
944 {
945 struct pkginfo *otherpkg;
946
947 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
948 if (otherpkg == pkg)
949 continue;
950 if (otherpkg->status <= PKG_STAT_CONFIGFILES)
951 continue;
952 if (dpkg_version_compare(&pkg->available.version,
953 &otherpkg->installed.version)) {
954 return false;
955 }
956 }
957
958 return true;
959 }
960
961 static void
962 pkg_remove_files_from_others(struct pkginfo *pkg, struct fileinlist *newfileslist)
963 {
964 struct fileinlist *cfile;
965 struct pkginfo *otherpkg;
966
967 for (cfile = newfileslist; cfile; cfile = cfile->next) {
968 struct filepackages_iterator *iter;
969 struct pkgset *divpkgset;
970
971 if (!(cfile->namenode->flags & fnnf_elide_other_lists))
972 continue;
973
974 if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
975 divpkgset = cfile->namenode->divert->pkgset;
976 if (divpkgset == pkg->set) {
977 debug(dbg_eachfile,
978 "process_archive not overwriting any '%s' (overriding, '%s')",
979 cfile->namenode->name, cfile->namenode->divert->useinstead->name);
980 continue;
981 } else {
982 debug(dbg_eachfile,
983 "process_archive looking for overwriting '%s' (overridden by %s)",
984 cfile->namenode->name, divpkgset ? divpkgset->name : "<local>");
985 }
986 } else {
987 divpkgset = NULL;
988 debug(dbg_eachfile, "process_archive looking for overwriting '%s'",
989 cfile->namenode->name);
990 }
991
992 iter = filepackages_iter_new(cfile->namenode);
993 while ((otherpkg = filepackages_iter_next(iter))) {
994 debug(dbg_eachfiledetail, "process_archive ... found in %s",
995 pkg_name(otherpkg, pnaw_always));
996
997 /* A pkgset can share files between instances, so there's no point
998 * in rewriting the file that's already in place. */
999 if (otherpkg->set == pkg->set)
1000 continue;
1001
1002 if (otherpkg->set == divpkgset) {
1003 debug(dbg_eachfiledetail, "process_archive ... diverted, skipping");
1004 continue;
1005 }
1006
1007 if (cfile->namenode->flags & fnnf_new_conff)
1008 conffile_mark_obsolete(otherpkg, cfile->namenode);
1009
1010 /* If !fileslistvalid then it's one of the disappeared packages above
1011 * or we have already updated the files list file, and we don't bother
1012 * with it here, clearly. */
1013 if (!otherpkg->clientdata->fileslistvalid)
1014 continue;
1015
1016 /* Found one. We delete the list entry for this file,
1017 * (and any others in the same package) and then mark the package
1018 * as requiring a reread. */
1019 write_filelist_except(otherpkg, &otherpkg->installed,
1020 otherpkg->clientdata->files, fnnf_elide_other_lists);
1021 ensure_package_clientdata(otherpkg);
1022 debug(dbg_veryverbose, "process_archive overwrote from %s",
1023 pkg_name(otherpkg, pnaw_always));
1024 }
1025 filepackages_iter_free(iter);
1026 }
1027 }
1028
1029 static void
1030 pkg_remove_backup_files(struct pkginfo *pkg, struct fileinlist *newfileslist)
1031 {
1032 struct fileinlist *cfile;
1033
1034 for (cfile = newfileslist; cfile; cfile = cfile->next) {
1035 struct filenamenode *usenode;
1036
1037 if (cfile->namenode->flags & fnnf_new_conff)
1038 continue;
1039
1040 usenode = namenodetouse(cfile->namenode, pkg, &pkg->installed);
1041
1042 /* Do not try to remove backups for the root directory. */
1043 if (strcmp(usenode->name, "/.") == 0)
1044 continue;
1045
1046 varbuf_rollback(&fnametmpvb, &fname_state);
1047 varbuf_add_str(&fnametmpvb, usenode->name);
1048 varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
1049 varbuf_end_str(&fnametmpvb);
1050 path_remove_tree(fnametmpvb.buf);
1051 }
1052 }
1053
1054 void process_archive(const char *filename) {
1055 static const struct tar_operations tf = {
1056 .read = tarfileread,
1057 .extract_file = tarobject,
1058 .link = tarobject,
1059 .symlink = tarobject,
1060 .mkdir = tarobject,
1061 .mknod = tarobject,
1062 };
1063
1064 /* These need to be static so that we can pass their addresses to
1065 * push_cleanup as arguments to the cu_xxx routines; if an error occurs
1066 * we unwind the stack before processing the cleanup list, and these
1067 * variables had better still exist ... */
1068 static int p1[2];
1069 static enum pkgstatus oldversionstatus;
1070 static struct tarcontext tc;
1071
1072 struct dpkg_error err;
1073 enum parsedbflags parsedb_flags;
1074 int rc;
1075 pid_t pid;
1076 struct pkginfo *pkg, *otherpkg;
1077 struct pkg_list *conflictor_iter;
1078 char *cidir = NULL;
1079 char *cidirrest;
1080 char *psize;
1081 const char *pfilename;
1082 struct filenamenode_queue newconffiles, newfiles_queue;
1083 struct stat stab;
1084
1085 cleanup_pkg_failed= cleanup_conflictor_failed= 0;
1086
1087 pfilename = summarize_filename(filename);
1088
1089 if (stat(filename, &stab))
1090 ohshite(_("cannot access archive '%s'"), filename);
1091
1092 /* We can't ‘tentatively-reassemble’ packages. */
1093 if (!f_noact) {
1094 if (!deb_reassemble(&filename, &pfilename))
1095 return;
1096 }
1097
1098 /* Verify the package. */
1099 if (!f_nodebsig)
1100 deb_verify(filename);
1101
1102 /* Get the control information directory. */
1103 cidir = get_control_dir(cidir);
1104 cidirrest = cidir + strlen(cidir);
1105 push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest);
1106
1107 pid = subproc_fork();
1108 if (pid == 0) {
1109 cidirrest[-1] = '\0';
1110 execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL);
1111 ohshite(_("unable to execute %s (%s)"),
1112 _("package control information extraction"), BACKEND);
1113 }
1114 subproc_reap(pid, BACKEND " --control", 0);
1115
1116 /* We want to guarantee the extracted files are on the disk, so that the
1117 * subsequent renames to the info database do not end up with old or zero
1118 * length files in case of a system crash. As neither dpkg-deb nor tar do
1119 * explicit fsync()s, we have to do them here.
1120 * XXX: This could be avoided by switching to an internal tar extractor. */
1121 dir_sync_contents(cidir);
1122
1123 strcpy(cidirrest,CONTROLFILE);
1124
1125 if (cipaction->arg_int == act_avail)
1126 parsedb_flags = pdb_parse_available;
1127 else
1128 parsedb_flags = pdb_parse_binary;
1129 parsedb_flags |= pdb_ignorefiles;
1130 if (fc_badversion)
1131 parsedb_flags |= pdb_lax_version_parser;
1132
1133 parsedb(cidir, parsedb_flags, &pkg);
1134
1135 if (!pkg->files) {
1136 pkg->files= nfmalloc(sizeof(struct filedetails));
1137 pkg->files->next = NULL;
1138 pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL;
1139 }
1140 /* Always nfmalloc. Otherwise, we may overwrite some other field (like
1141 * md5sum). */
1142 psize = nfmalloc(30);
1143 sprintf(psize, "%jd", (intmax_t)stab.st_size);
1144 pkg->files->size = psize;
1145
1146 if (cipaction->arg_int == act_avail) {
1147 printf(_("Recorded info about %s from %s.\n"),
1148 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename);
1149 pop_cleanup(ehflag_normaltidy);
1150 return;
1151 }
1152
1153 if (pkg->available.arch->type != DPKG_ARCH_ALL &&
1154 pkg->available.arch->type != DPKG_ARCH_NATIVE &&
1155 pkg->available.arch->type != DPKG_ARCH_FOREIGN)
1156 forcibleerr(fc_architecture,
1157 _("package architecture (%s) does not match system (%s)"),
1158 pkg->available.arch->name,
1159 dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
1160
1161 clear_deconfigure_queue();
1162 clear_istobes();
1163
1164 if (wanttoinstall(pkg)) {
1165 pkg_set_want(pkg, PKG_WANT_INSTALL);
1166 } else {
1167 pop_cleanup(ehflag_normaltidy);
1168 return;
1169 }
1170
1171 /* Deconfigure other instances from a pkgset if they are not in sync. */
1172 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1173 if (otherpkg == pkg)
1174 continue;
1175 if (otherpkg->status <= PKG_STAT_HALFCONFIGURED)
1176 continue;
1177
1178 if (dpkg_version_compare(&pkg->available.version,
1179 &otherpkg->installed.version))
1180 enqueue_deconfigure(otherpkg, NULL);
1181 }
1182
1183 pkg_check_depcon(pkg, pfilename);
1184
1185 ensure_allinstfiles_available();
1186 filesdbinit();
1187 trig_file_interests_ensure();
1188
1189 printf(_("Preparing to unpack %s ...\n"), pfilename);
1190
1191 if (pkg->status != PKG_STAT_NOTINSTALLED &&
1192 pkg->status != PKG_STAT_CONFIGFILES) {
1193 log_action("upgrade", pkg, &pkg->installed);
1194 } else {
1195 log_action("install", pkg, &pkg->available);
1196 }
1197
1198 if (f_noact) {
1199 pop_cleanup(ehflag_normaltidy);
1200 return;
1201 }
1202
1203 /*
1204 * OK, we're going ahead.
1205 */
1206
1207 trig_activate_packageprocessing(pkg);
1208 strcpy(cidirrest, TRIGGERSCIFILE);
1209 trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg, &pkg->available);
1210
1211 /* Read the conffiles, and copy the hashes across. */
1212 newconffiles.head = NULL;
1213 newconffiles.tail = &newconffiles.head;
1214 push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
1215 strcpy(cidirrest,CONFFILESFILE);
1216 deb_parse_conffiles(pkg, cidir, &newconffiles);
1217
1218 /* All the old conffiles are marked with a flag, so that we don't delete
1219 * them if they seem to disappear completely. */
1220 pkg_conffiles_mark_old(pkg);
1221 for (conflictor_iter = conflictors.head;
1222 conflictor_iter;
1223 conflictor_iter = conflictor_iter->next)
1224 pkg_conffiles_mark_old(conflictor_iter->pkg);
1225
1226 oldversionstatus= pkg->status;
1227
1228 assert(oldversionstatus <= PKG_STAT_INSTALLED);
1229 debug(dbg_general,"process_archive oldversionstatus=%s",
1230 statusstrings[oldversionstatus]);
1231
1232 if (oldversionstatus == PKG_STAT_HALFCONFIGURED ||
1233 oldversionstatus == PKG_STAT_TRIGGERSAWAITED ||
1234 oldversionstatus == PKG_STAT_TRIGGERSPENDING ||
1235 oldversionstatus == PKG_STAT_INSTALLED) {
1236 pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
1237 pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
1238 modstatdb_note(pkg);
1239 push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
1240 if (dpkg_version_compare(&pkg->available.version,
1241 &pkg->installed.version) >= 0)
1242 /* Upgrade or reinstall. */
1243 maintscript_fallback(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
1244 "upgrade", "failed-upgrade");
1245 else /* Downgrade => no fallback */
1246 maintscript_installed(pkg, PRERMFILE, "pre-removal",
1247 "upgrade",
1248 versiondescribe(&pkg->available.version,
1249 vdew_nonambig),
1250 NULL);
1251 pkg_set_status(pkg, PKG_STAT_UNPACKED);
1252 oldversionstatus = PKG_STAT_UNPACKED;
1253 modstatdb_note(pkg);
1254 }
1255
1256 pkg_deconfigure_others(pkg);
1257
1258 for (conflictor_iter = conflictors.head;
1259 conflictor_iter;
1260 conflictor_iter = conflictor_iter->next) {
1261 struct pkginfo *conflictor = conflictor_iter->pkg;
1262
1263 if (!(conflictor->status == PKG_STAT_HALFCONFIGURED ||
1264 conflictor->status == PKG_STAT_TRIGGERSAWAITED ||
1265 conflictor->status == PKG_STAT_TRIGGERSPENDING ||
1266 conflictor->status == PKG_STAT_INSTALLED))
1267 continue;
1268
1269 trig_activate_packageprocessing(conflictor);
1270 pkg_set_status(conflictor, PKG_STAT_HALFCONFIGURED);
1271 modstatdb_note(conflictor);
1272 push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0,
1273 2, conflictor, pkg);
1274 maintscript_installed(conflictor, PRERMFILE, "pre-removal",
1275 "remove", "in-favour",
1276 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1277 versiondescribe(&pkg->available.version,
1278 vdew_nonambig),
1279 NULL);
1280 pkg_set_status(conflictor, PKG_STAT_HALFINSTALLED);
1281 modstatdb_note(conflictor);
1282 }
1283
1284 pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
1285 if (pkg->status == PKG_STAT_NOTINSTALLED) {
1286 pkg->installed.version= pkg->available.version;
1287 pkg->installed.multiarch = pkg->available.multiarch;
1288 }
1289 pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
1290 modstatdb_note(pkg);
1291 if (oldversionstatus == PKG_STAT_NOTINSTALLED) {
1292 push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0,
1293 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
1294 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1295 "install", NULL);
1296 } else if (oldversionstatus == PKG_STAT_CONFIGFILES) {
1297 push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0,
1298 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
1299 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1300 "install",
1301 versiondescribe(&pkg->installed.version, vdew_nonambig),
1302 versiondescribe(&pkg->available.version, vdew_nonambig),
1303 NULL);
1304 } else {
1305 push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0,
1306 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
1307 maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
1308 "upgrade",
1309 versiondescribe(&pkg->installed.version, vdew_nonambig),
1310 versiondescribe(&pkg->available.version, vdew_nonambig),
1311 NULL);
1312 }
1313
1314 if (oldversionstatus == PKG_STAT_NOTINSTALLED ||
1315 oldversionstatus == PKG_STAT_CONFIGFILES) {
1316 printf(_("Unpacking %s (%s) ...\n"),
1317 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1318 versiondescribe(&pkg->available.version, vdew_nonambig));
1319 } else {
1320 printf(_("Unpacking %s (%s) over (%s) ...\n"),
1321 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1322 versiondescribe(&pkg->available.version, vdew_nonambig),
1323 versiondescribe(&pkg->installed.version, vdew_nonambig));
1324 }
1325
1326 /*
1327 * Now we unpack the archive, backing things up as we go.
1328 * For each file, we check to see if it already exists.
1329 * There are several possibilities:
1330 *
1331 * + We are trying to install a non-directory ...
1332 * - It doesn't exist. In this case we simply extract it.
1333 * - It is a plain file, device, symlink, &c. We do an ‘atomic
1334 * overwrite’ using link() and rename(), but leave a backup copy.
1335 * Later, when we delete the backup, we remove it from any other
1336 * packages' lists.
1337 * - It is a directory. In this case it depends on whether we're
1338 * trying to install a symlink or something else.
1339 * = If we're not trying to install a symlink we move the directory
1340 * aside and extract the node. Later, when we recursively remove
1341 * the backed-up directory, we remove it from any other packages'
1342 * lists.
1343 * = If we are trying to install a symlink we do nothing - ie,
1344 * dpkg will never replace a directory tree with a symlink. This
1345 * is to avoid embarrassing effects such as replacing a directory
1346 * tree with a link to a link to the original directory tree.
1347 * + We are trying to install a directory ...
1348 * - It doesn't exist. We create it with the appropriate modes.
1349 * - It exists as a directory or a symlink to one. We do nothing.
1350 * - It is a plain file or a symlink (other than to a directory).
1351 * We move it aside and create the directory. Later, when we
1352 * delete the backup, we remove it from any other packages' lists.
1353 *
1354 * Install non-dir Install symlink Install dir
1355 * Exists not X X X
1356 * File/node/symlink LXR LXR BXR
1357 * Directory BXR - -
1358 *
1359 * X: extract file/node/link/directory
1360 * LX: atomic overwrite leaving backup
1361 * B: ordinary backup
1362 * R: later remove from other packages' lists
1363 * -: do nothing
1364 *
1365 * After we've done this we go through the remaining things in the
1366 * lists of packages we're trying to remove (including the old
1367 * version of the current package). This happens in reverse order,
1368 * so that we process files before the directories (or symlinks-to-
1369 * directories) containing them.
1370 *
1371 * + If the thing is a conffile then we leave it alone for the purge
1372 * operation.
1373 * + Otherwise, there are several possibilities too:
1374 * - The listed thing does not exist. We ignore it.
1375 * - The listed thing is a directory or a symlink to a directory.
1376 * We delete it only if it isn't listed in any other package.
1377 * - The listed thing is not a directory, but was part of the package
1378 * that was upgraded, we check to make sure the files aren't the
1379 * same ones from the old package by checking dev/inode
1380 * - The listed thing is not a directory or a symlink to one (ie,
1381 * it's a plain file, device, pipe, &c, or a symlink to one, or a
1382 * dangling symlink). We delete it.
1383 *
1384 * The removed packages' list becomes empty (of course, the new
1385 * version of the package we're installing will have a new list,
1386 * which replaces the old version's list).
1387 *
1388 * If at any stage we remove a file from a package's list, and the
1389 * package isn't one we're already processing, and the package's
1390 * list becomes empty as a result, we ‘vanish’ the package. This
1391 * means that we run its postrm with the ‘disappear’ argument, and
1392 * put the package in the ‘not-installed’ state. If it had any
1393 * conffiles, their hashes and ownership will have been transferred
1394 * already, so we just ignore those and forget about them from the
1395 * point of view of the disappearing package.
1396 *
1397 * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
1398 * files get replaced ‘as we go’.
1399 */
1400
1401 m_pipe(p1);
1402 push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]);
1403 pid = subproc_fork();
1404 if (pid == 0) {
1405 m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
1406 execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL);
1407 ohshite(_("unable to execute %s (%s)"),
1408 _("package filesystem archive extraction"), BACKEND);
1409 }
1410 close(p1[1]);
1411 p1[1] = -1;
1412
1413 newfiles_queue.head = NULL;
1414 newfiles_queue.tail = &newfiles_queue.head;
1415 tc.newfiles_queue = &newfiles_queue;
1416 push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
1417 tc.pkg= pkg;
1418 tc.backendpipe= p1[0];
1419 tc.pkgset_getting_in_sync = pkgset_getting_in_sync(pkg);
1420
1421 rc = tar_extractor(&tc, &tf);
1422 if (rc) {
1423 if (errno) {
1424 ohshite(_("error reading dpkg-deb tar output"));
1425 } else {
1426 ohshit(_("corrupted filesystem tarfile - corrupted package archive"));
1427 }
1428 }
1429 if (fd_skip(p1[0], -1, &err) < 0)
1430 ohshit(_("cannot zap possible trailing zeros from dpkg-deb: %s"), err.str);
1431 close(p1[0]);
1432 p1[0] = -1;
1433 subproc_reap(pid, BACKEND " --fsys-tarfile", SUBPROC_NOPIPE);
1434
1435 tar_deferred_extract(newfiles_queue.head, pkg);
1436
1437 if (oldversionstatus == PKG_STAT_HALFINSTALLED ||
1438 oldversionstatus == PKG_STAT_UNPACKED) {
1439 /* Packages that were in ‘installed’ and ‘postinstfailed’ have been
1440 * reduced to ‘unpacked’ by now, by the running of the prerm script. */
1441 pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
1442 modstatdb_note(pkg);
1443 push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
1444 maintscript_fallback(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
1445 "upgrade", "failed-upgrade");
1446 }
1447
1448 /* If anything goes wrong while tidying up it's a bit late to do
1449 * anything about it. However, we don't install the new status
1450 * info yet, so that a future dpkg installation will put everything
1451 * right (we hope).
1452 *
1453 * If something does go wrong later the ‘conflictor’ package will be
1454 * left in the ‘removal_failed’ state. Removing or installing it
1455 * will be impossible if it was required because of the conflict with
1456 * the package we're installing now and (presumably) the dependency
1457 * by other packages. This means that the files it contains in
1458 * common with this package will hang around until we successfully
1459 * get this package installed, after which point we can trust the
1460 * conflicting package's file list, which will have been updated to
1461 * remove any files in this package. */
1462 push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
1463
1464 /* Now we delete all the files that were in the old version of
1465 * the package only, except (old or new) conffiles, which we leave
1466 * alone. */
1467 pkg_remove_old_files(pkg, &newfiles_queue, &newconffiles);
1468
1469 /* OK, now we can write the updated files-in-this package list,
1470 * since we've done away (hopefully) with all the old junk. */
1471 write_filelist_except(pkg, &pkg->available, newfiles_queue.head, 0);
1472
1473 /* Trigger interests may have changed.
1474 * Firstly we go through the old list of interests deleting them.
1475 * Then we go through the new list adding them. */
1476 strcpy(cidirrest, TRIGGERSCIFILE);
1477 trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
1478 trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
1479 trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg, &pkg->available);
1480 trig_file_interests_save();
1481
1482 /* We also install the new maintainer scripts, and any other
1483 * cruft that may have come along with the package. First
1484 * we go through the existing scripts replacing or removing
1485 * them as appropriate; then we go through the new scripts
1486 * (any that are left) and install them. */
1487 debug(dbg_general, "process_archive updating info directory");
1488 pkg_infodb_update(pkg, cidir, cidirrest);
1489
1490 /* We store now the checksums dynamically computed while unpacking. */
1491 write_filehash_except(pkg, &pkg->available, newfiles_queue.head, 0);
1492
1493 /*
1494 * Update the status database.
1495 *
1496 * This involves copying each field across from the ‘available’
1497 * to the ‘installed’ half of the pkg structure.
1498 * For some of the fields we have to do a complicated construction
1499 * operation; for others we can just copy the value.
1500 * We tackle the fields in the order they appear, so that
1501 * we don't miss any out :-).
1502 * At least we don't have to copy any strings that are referred
1503 * to, because these are never modified and never freed.
1504 */
1505 pkg_update_fields(pkg, &newconffiles);
1506
1507 /* In case this was an architecture cross-grade, the in-core pkgset might
1508 * be in an inconsistent state, with two pkginfo entries having the same
1509 * architecture, so let's fix that. Note that this does not lose data,
1510 * as the pkg.available member parsed from the binary should replace the
1511 * to be cleared duplicate in the other instance. */
1512 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
1513 if (otherpkg == pkg)
1514 continue;
1515 if (otherpkg->installed.arch != pkg->installed.arch)
1516 continue;
1517
1518 assert(otherpkg->status == PKG_STAT_NOTINSTALLED);
1519
1520 pkg_blank(otherpkg);
1521 }
1522
1523 /* Check for disappearing packages:
1524 * We go through all the packages on the system looking for ones
1525 * whose files are entirely part of the one we've just unpacked
1526 * (and which actually *have* some files!).
1527 *
1528 * Any that we find are removed - we run the postrm with ‘disappear’
1529 * as an argument, and remove their info/... files and status info.
1530 * Conffiles are ignored (the new package had better do something
1531 * with them!). */
1532 pkg_disappear_others(pkg);
1533
1534 /* Delete files from any other packages' lists.
1535 * We have to do this before we claim this package is in any
1536 * sane kind of state, as otherwise we might delete by mistake
1537 * a file that we overwrote, when we remove the package which
1538 * had the version we overwrote. To prevent this we make
1539 * sure that we don't claim this package is OK until we
1540 * have claimed ‘ownership’ of all its files. */
1541 pkg_remove_files_from_others(pkg, newfiles_queue.head);
1542
1543 /* Right, the package we've unpacked is now in a reasonable state.
1544 * The only thing that we have left to do with it is remove
1545 * backup files, and we can leave the user to fix that if and when
1546 * it happens (we leave the reinstall required flag, of course). */
1547 pkg_set_status(pkg, PKG_STAT_UNPACKED);
1548 modstatdb_note(pkg);
1549
1550 /* Now we delete all the backup files that we made when
1551 * extracting the archive - except for files listed as conffiles
1552 * in the new package.
1553 * This time we count it as an error if something goes wrong.
1554 *
1555 * Note that we don't ever delete things that were in the old
1556 * package as a conffile and don't appear at all in the new.
1557 * They stay recorded as obsolete conffiles and will eventually
1558 * (if not taken over by another package) be forgotten. */
1559 pkg_remove_backup_files(pkg, newfiles_queue.head);
1560
1561 /* OK, we're now fully done with the main package.
1562 * This is quite a nice state, so we don't unwind past here. */
1563 pkg_reset_eflags(pkg);
1564 modstatdb_note(pkg);
1565 push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
1566
1567 /* Only the removal of the conflictor left to do.
1568 * The files list for the conflictor is still a little inconsistent in-core,
1569 * as we have not yet updated the filename->packages mappings; however,
1570 * the package->filenames mapping is. */
1571 while (!pkg_queue_is_empty(&conflictors)) {
1572 struct pkginfo *conflictor = pkg_queue_pop(&conflictors);
1573
1574 /* We need to have the most up-to-date info about which files are
1575 * what ... */
1576 ensure_allinstfiles_available();
1577 removal_bulk(conflictor);
1578 }
1579
1580 if (cipaction->arg_int == act_install)
1581 enqueue_package_mark_seen(pkg);
1582 }