dpkg (1.18.25) stretch; urgency=medium
[dpkg] / src / archives.c
CommitLineData
1479465f
GJ
1/*
2 * dpkg - main program for package management
3 * archives.c - actions that process archive files, mainly unpack
4 *
5 * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
7 * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
8 * Copyright © 2011 Linaro Limited
9 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
10 *
11 * This is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 */
24
25#include <config.h>
26#include <compat.h>
27
28#include <sys/types.h>
29#include <sys/time.h>
30#include <sys/stat.h>
31
32#include <assert.h>
33#include <errno.h>
34#include <string.h>
35#include <time.h>
36#include <fcntl.h>
37#include <unistd.h>
38#include <stdint.h>
39#include <stdlib.h>
40#include <stdio.h>
41#include <obstack.h>
42#define obstack_chunk_alloc m_malloc
43#define obstack_chunk_free free
44
45#include <dpkg/i18n.h>
46#include <dpkg/dpkg.h>
47#include <dpkg/dpkg-db.h>
48#include <dpkg/pkg.h>
49#include <dpkg/path.h>
50#include <dpkg/fdio.h>
51#include <dpkg/buffer.h>
52#include <dpkg/subproc.h>
53#include <dpkg/command.h>
54#include <dpkg/file.h>
55#include <dpkg/treewalk.h>
56#include <dpkg/tarfn.h>
57#include <dpkg/options.h>
58#include <dpkg/triglib.h>
59
60#include "filesdb.h"
61#include "main.h"
62#include "archives.h"
63#include "filters.h"
64#include "infodb.h"
65
66static inline void
67fd_writeback_init(int fd)
68{
69 /* Ignore the return code as it should be considered equivalent to an
70 * asynchronous hint for the kernel, we are doing an fsync() later on
71 * anyway. */
72#if defined(SYNC_FILE_RANGE_WRITE)
73 sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
74#elif defined(HAVE_POSIX_FADVISE)
75 posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
76#endif
77}
78
79static struct obstack tar_pool;
80static bool tar_pool_init = false;
81
82/**
83 * Allocate memory from the tar memory pool.
84 */
85static void *
86tar_pool_alloc(size_t size)
87{
88 if (!tar_pool_init) {
89 obstack_init(&tar_pool);
90 tar_pool_init = true;
91 }
92
93 return obstack_alloc(&tar_pool, size);
94}
95
96/**
97 * Free memory from the tar memory pool.
98 */
99static void
100tar_pool_free(void *ptr)
101{
102 obstack_free(&tar_pool, ptr);
103}
104
105/**
106 * Release the tar memory pool.
107 */
108static void
109tar_pool_release(void)
110{
111 if (tar_pool_init) {
112 obstack_free(&tar_pool, NULL);
113 tar_pool_init = false;
114 }
115}
116
117struct fileinlist *
118tar_filenamenode_queue_push(struct filenamenode_queue *queue,
119 struct filenamenode *namenode)
120{
121 struct fileinlist *node;
122
123 node = tar_pool_alloc(sizeof(*node));
124 node->namenode = namenode;
125 node->next = NULL;
126
127 *queue->tail = node;
128 queue->tail = &node->next;
129
130 return node;
131}
132
133static void
134tar_filenamenode_queue_pop(struct filenamenode_queue *queue,
135 struct fileinlist **tail_prev,
136 struct fileinlist *node)
137{
138 tar_pool_free(node);
139 queue->tail = tail_prev;
140 *tail_prev = NULL;
141}
142
143/**
144 * Check if a file or directory will save a package from disappearance.
145 *
146 * A package can only be saved by a file or directory which is part
147 * only of itself - it must be neither part of the new package being
148 * installed nor part of any 3rd package (this is important so that
149 * shared directories don't stop packages from disappearing).
150 */
151bool
152filesavespackage(struct fileinlist *file,
153 struct pkginfo *pkgtobesaved,
154 struct pkginfo *pkgbeinginstalled)
155{
156 struct filepackages_iterator *iter;
157 struct pkgset *divpkgset;
158 struct pkginfo *thirdpkg;
159
160 debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
161 file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
162
163 /* If the file is a contended one and it's overridden by either
164 * the package we're considering disappearing or the package
165 * we're installing then they're not actually the same file, so
166 * we can't disappear the package - it is saved by this file. */
167 if (file->namenode->divert && file->namenode->divert->useinstead) {
168 divpkgset = file->namenode->divert->pkgset;
169 if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
170 debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
171 return true;
172 }
173 }
174 /* Is the file in the package being installed? If so then it can't save. */
175 if (file->namenode->flags & fnnf_new_inarchive) {
176 debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
177 return false;
178 }
179 /* Look for a 3rd package which can take over the file (in case
180 * it's a directory which is shared by many packages. */
181 iter = filepackages_iter_new(file->namenode);
182 while ((thirdpkg = filepackages_iter_next(iter))) {
183 debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
184 pkg_name(thirdpkg, pnaw_always));
185
186 /* Is this not the package being installed or the one being
187 * checked for disappearance? */
188 if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
189 continue;
190
191 /* A Multi-Arch: same package can share files and their presence in a
192 * third package of the same set is not a sign that we can get rid of
193 * it. */
194 if (pkgtobesaved->installed.multiarch == PKG_MULTIARCH_SAME &&
195 thirdpkg->set == pkgtobesaved->set)
196 continue;
197
198 /* If !fileslistvalid then we've already disappeared this one, so
199 * we shouldn't try to make it take over this shared directory. */
200 debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
201
202 if (!thirdpkg->clientdata->fileslistvalid) {
203 debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
204 continue;
205 }
206
207 /* We've found a package that can take this file. */
208 debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
209 filepackages_iter_free(iter);
210 return false;
211 }
212 filepackages_iter_free(iter);
213
214 debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
215 return true;
216}
217
218static void
219md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
220 struct filenamenode *namenode)
221{
222 struct pkginfo *otherpkg;
223 struct conffile *conff;
224
225 debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
226 namenode->name);
227
228 for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
229 if (otherpkg == pkg)
230 continue;
231 /* If we are reinstalling, even if the other package is only unpacked,
232 * we can always make use of the Conffiles hash value from an initial
233 * installation, if that happened at all. */
234 if (otherpkg->status <= PKG_STAT_UNPACKED &&
235 dpkg_version_compare(&otherpkg->installed.version,
236 &otherpkg->configversion) != 0)
237 continue;
238 for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
239 if (conff->obsolete)
240 continue;
241 if (strcmp(conff->name, namenode->name) == 0)
242 break;
243 }
244 if (conff) {
245 strcpy(oldhash, conff->hash);
246 debug(dbg_conffdetail,
247 "tarobject found shared conffile, from pkg %s (%s); hash=%s",
248 pkg_name(otherpkg, pnaw_always),
249 pkg_status_name(otherpkg), oldhash);
250 break;
251 }
252 }
253
254 /* If no package was found with a valid Conffiles field, we make the
255 * risky assumption that the hash of the current .dpkg-new file is
256 * the one of the previously unpacked package. */
257 if (otherpkg == NULL) {
258 md5hash(pkg, oldhash, oldname);
259 debug(dbg_conffdetail,
260 "tarobject found shared conffile, from disk; hash=%s", oldhash);
261 }
262}
263
264void cu_pathname(int argc, void **argv) {
265 path_remove_tree((char*)(argv[0]));
266}
267
268int tarfileread(void *ud, char *buf, int len) {
269 struct tarcontext *tc= (struct tarcontext*)ud;
270 int r;
271
272 r = fd_read(tc->backendpipe, buf, len);
273 if (r < 0)
274 ohshite(_("error reading from dpkg-deb pipe"));
275 return r;
276}
277
278static void
279tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
280{
281 struct dpkg_error err;
282 size_t r;
283
284 r = te->size % TARBLKSZ;
285 if (r == 0)
286 return;
287
288 if (fd_skip(tc->backendpipe, TARBLKSZ - r, &err) < 0)
289 ohshit(_("cannot skip padding for file '%.255s': %s"), te->name, err.str);
290}
291
292static void
293tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
294{
295 /* We need to advance the tar file to the next object, so read the
296 * file data and set it to oblivion. */
297 if (ti->type == TAR_FILETYPE_FILE) {
298 struct dpkg_error err;
299 char fnamebuf[256];
300
301 if (fd_skip(tc->backendpipe, ti->size, &err) < 0)
302 ohshit(_("cannot skip file '%.255s' (replaced or excluded?) from pipe: %s"),
303 path_quote_filename(fnamebuf, ti->name, 256), err.str);
304 tarobject_skip_padding(tc, ti);
305 }
306}
307
308struct varbuf_state fname_state;
309struct varbuf fnamevb;
310struct varbuf fnametmpvb;
311struct varbuf fnamenewvb;
312struct pkg_deconf_list *deconfigure = NULL;
313
314static time_t currenttime;
315
316static int
317does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
318 struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
319{
320 struct dependency *dep;
321
322 debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
323 pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
324 pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
325 versiondescribe(&old_pkgbin->version, vdew_always));
326 for (dep = new_pkgbin->depends; dep; dep = dep->next) {
327 if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
328 continue;
329 debug(dbg_depcondetail,"does_replace ... found old, version %s",
330 versiondescribe(&dep->list->version,vdew_always));
331 if (!versionsatisfied(old_pkgbin, dep->list))
332 continue;
333 /* The test below can only trigger if dep_replaces start having
334 * arch qualifiers different from “any”. */
335 if (!archsatisfied(old_pkgbin, dep->list))
336 continue;
337 debug(dbg_depcon,"does_replace ... yes");
338 return true;
339 }
340 debug(dbg_depcon,"does_replace ... no");
341 return false;
342}
343
344static void
345tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
346 const char *path, struct file_stat *st,
347 struct filenamenode *namenode)
348{
349 static struct varbuf hardlinkfn;
350 static int fd;
351
352 struct dpkg_error err;
353 struct filenamenode *linknode;
354 char fnamebuf[256];
355 char fnamenewbuf[256];
356 char *newhash;
357
358 switch (te->type) {
359 case TAR_FILETYPE_FILE:
360 /* We create the file with mode 0 to make sure nobody can do anything with
361 * it until we apply the proper mode, which might be a statoverride. */
362 fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
363 if (fd < 0)
364 ohshite(_("unable to create '%.255s' (while processing '%.255s')"),
365 path, te->name);
366 push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
367 debug(dbg_eachfiledetail, "tarobject file open size=%jd",
368 (intmax_t)te->size);
369
370 /* We try to tell the filesystem how much disk space we are going to
371 * need to let it reduce fragmentation and possibly improve performance,
372 * as we do know the size beforehand. */
373 fd_allocate_size(fd, 0, te->size);
374
375 newhash = nfmalloc(MD5HASHLEN + 1);
376 if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
377 ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
378 path_quote_filename(fnamebuf, te->name, 256),
379 path_quote_filename(fnamenewbuf, fnamenewvb.buf, 256), err.str);
380 namenode->newhash = newhash;
381 debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
382
383 tarobject_skip_padding(tc, te);
384
385 fd_writeback_init(fd);
386
387 if (namenode->statoverride)
388 debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
389 namenode->statoverride->uid,
390 namenode->statoverride->gid,
391 namenode->statoverride->mode);
392 if (fchown(fd, st->uid, st->gid))
393 ohshite(_("error setting ownership of '%.255s'"), te->name);
394 if (fchmod(fd, st->mode & ~S_IFMT))
395 ohshite(_("error setting permissions of '%.255s'"), te->name);
396
397 /* Postpone the fsync, to try to avoid massive I/O degradation. */
398 if (!fc_unsafe_io)
399 namenode->flags |= fnnf_deferred_fsync;
400
401 pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
402 if (close(fd))
403 ohshite(_("error closing/writing '%.255s'"), te->name);
404 break;
405 case TAR_FILETYPE_FIFO:
406 if (mkfifo(path, 0))
407 ohshite(_("error creating pipe '%.255s'"), te->name);
408 debug(dbg_eachfiledetail, "tarobject fifo");
409 break;
410 case TAR_FILETYPE_CHARDEV:
411 if (mknod(path, S_IFCHR, te->dev))
412 ohshite(_("error creating device '%.255s'"), te->name);
413 debug(dbg_eachfiledetail, "tarobject chardev");
414 break;
415 case TAR_FILETYPE_BLOCKDEV:
416 if (mknod(path, S_IFBLK, te->dev))
417 ohshite(_("error creating device '%.255s'"), te->name);
418 debug(dbg_eachfiledetail, "tarobject blockdev");
419 break;
420 case TAR_FILETYPE_HARDLINK:
421 varbuf_reset(&hardlinkfn);
422 varbuf_add_str(&hardlinkfn, instdir);
423 linknode = findnamenode(te->linkname, 0);
424 varbuf_add_str(&hardlinkfn,
425 namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
426 if (linknode->flags & (fnnf_deferred_rename | fnnf_new_conff))
427 varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
428 varbuf_end_str(&hardlinkfn);
429 if (link(hardlinkfn.buf, path))
430 ohshite(_("error creating hard link '%.255s'"), te->name);
431 namenode->newhash = linknode->newhash;
432 debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
433 break;
434 case TAR_FILETYPE_SYMLINK:
435 /* We've already checked for an existing directory. */
436 if (symlink(te->linkname, path))
437 ohshite(_("error creating symbolic link '%.255s'"), te->name);
438 debug(dbg_eachfiledetail, "tarobject symlink creating");
439 break;
440 case TAR_FILETYPE_DIR:
441 /* We've already checked for an existing directory. */
442 if (mkdir(path, 0))
443 ohshite(_("error creating directory '%.255s'"), te->name);
444 debug(dbg_eachfiledetail, "tarobject directory creating");
445 break;
446 default:
447 internerr("unknown tar type '%d', but already checked", te->type);
448 }
449}
450
451static void
452tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
453 struct filenamenode *namenode)
454{
455 if (te->type == TAR_FILETYPE_FILE) {
456 struct dpkg_error err;
457 char fnamebuf[256];
458 char *newhash;
459
460 newhash = nfmalloc(MD5HASHLEN + 1);
461 if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
462 ohshit(_("cannot compute MD5 hash for tar file '%.255s': %s"),
463 path_quote_filename(fnamebuf, te->name, 256), err.str);
464 tarobject_skip_padding(tc, te);
465
466 namenode->newhash = newhash;
467 debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
468 } else if (te->type == TAR_FILETYPE_HARDLINK) {
469 struct filenamenode *linknode;
470
471 linknode = findnamenode(te->linkname, 0);
472 namenode->newhash = linknode->newhash;
473 debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
474 }
475}
476
477static void
478tarobject_set_mtime(struct tar_entry *te, const char *path)
479{
480 struct timeval tv[2];
481
482 tv[0].tv_sec = currenttime;
483 tv[0].tv_usec = 0;
484 tv[1].tv_sec = te->mtime;
485 tv[1].tv_usec = 0;
486
487 if (te->type == TAR_FILETYPE_SYMLINK) {
488#ifdef HAVE_LUTIMES
489 if (lutimes(path, tv) && errno != ENOSYS)
490 ohshite(_("error setting timestamps of '%.255s'"), path);
491#endif
492 } else {
493 if (utimes(path, tv))
494 ohshite(_("error setting timestamps of '%.255s'"), path);
495 }
496}
497
498static void
499tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
500{
501 if (te->type == TAR_FILETYPE_FILE)
502 return; /* Already handled using the file descriptor. */
503
504 if (te->type == TAR_FILETYPE_SYMLINK) {
505 if (lchown(path, st->uid, st->gid))
506 ohshite(_("error setting ownership of symlink '%.255s'"), path);
507 } else {
508 if (chown(path, st->uid, st->gid))
509 ohshite(_("error setting ownership of '%.255s'"), path);
510 if (chmod(path, st->mode & ~S_IFMT))
511 ohshite(_("error setting permissions of '%.255s'"), path);
512 }
513}
514
515static void
516tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
517{
518 dpkg_selabel_set_context(matchpath, path, mode);
519}
520
521static void
522tarobject_matches(struct tarcontext *tc,
523 const char *fn_old, struct stat *stab, char *oldhash,
524 const char *fn_new, struct tar_entry *te,
525 struct filenamenode *namenode)
526{
527 char *linkname;
528 ssize_t linksize;
529
530 debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
531
532 switch (te->type) {
533 case TAR_FILETYPE_DIR:
534 /* Nothing to check for a new directory. */
535 return;
536 case TAR_FILETYPE_SYMLINK:
537 /* Symlinks to existing dirs have already been dealt with, only
538 * remain real symlinks where we can compare the target. */
539 if (!S_ISLNK(stab->st_mode))
540 break;
541 linkname = m_malloc(stab->st_size + 1);
542 linksize = readlink(fn_old, linkname, stab->st_size + 1);
543 if (linksize < 0)
544 ohshite(_("unable to read link '%.255s'"), fn_old);
545 else if (linksize > stab->st_size)
546 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
547 fn_old, (intmax_t)stab->st_size, linksize);
548 else if (linksize < stab->st_size)
549 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
550 fn_old, (intmax_t)stab->st_size, linksize);
551 linkname[linksize] = '\0';
552 if (strcmp(linkname, te->linkname) == 0) {
553 free(linkname);
554 return;
555 } else {
556 free(linkname);
557 }
558 break;
559 case TAR_FILETYPE_CHARDEV:
560 if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
561 return;
562 break;
563 case TAR_FILETYPE_BLOCKDEV:
564 if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
565 return;
566 break;
567 case TAR_FILETYPE_FIFO:
568 if (S_ISFIFO(stab->st_mode))
569 return;
570 break;
571 case TAR_FILETYPE_HARDLINK:
572 /* Fall through. */
573 case TAR_FILETYPE_FILE:
574 /* Only check metadata for non-conffiles. */
575 if (!(namenode->flags & fnnf_new_conff) &&
576 !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
577 break;
578 if (strcmp(oldhash, namenode->newhash) == 0)
579 return;
580 break;
581 default:
582 internerr("unknown tar type '%d', but already checked", te->type);
583 }
584
585 forcibleerr(fc_overwrite,
586 _("trying to overwrite shared '%.250s', which is different "
587 "from other instances of package %.250s"),
588 namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
589}
590
591void setupfnamevbs(const char *filename) {
592 varbuf_rollback(&fnamevb, &fname_state);
593 varbuf_add_str(&fnamevb, filename);
594 varbuf_end_str(&fnamevb);
595
596 varbuf_rollback(&fnametmpvb, &fname_state);
597 varbuf_add_str(&fnametmpvb, filename);
598 varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
599 varbuf_end_str(&fnametmpvb);
600
601 varbuf_rollback(&fnamenewvb, &fname_state);
602 varbuf_add_str(&fnamenewvb, filename);
603 varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
604 varbuf_end_str(&fnamenewvb);
605
606 debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
607 fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
608}
609
610static bool
611linktosameexistingdir(const struct tar_entry *ti, const char *fname,
612 struct varbuf *symlinkfn)
613{
614 struct stat oldstab, newstab;
615 int statr;
616 const char *lastslash;
617
618 statr= stat(fname, &oldstab);
619 if (statr) {
620 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
621 ohshite(_("failed to stat (dereference) existing symlink '%.250s'"),
622 fname);
623 return false;
624 }
625 if (!S_ISDIR(oldstab.st_mode))
626 return false;
627
628 /* But is it to the same dir? */
629 varbuf_reset(symlinkfn);
630 if (ti->linkname[0] == '/') {
631 varbuf_add_str(symlinkfn, instdir);
632 } else {
633 lastslash= strrchr(fname, '/');
634 assert(lastslash);
635 varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
636 }
637 varbuf_add_str(symlinkfn, ti->linkname);
638 varbuf_end_str(symlinkfn);
639
640 statr= stat(symlinkfn->buf, &newstab);
641 if (statr) {
642 if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
643 ohshite(_("failed to stat (dereference) proposed new symlink target"
644 " '%.250s' for symlink '%.250s'"), symlinkfn->buf, fname);
645 return false;
646 }
647 if (!S_ISDIR(newstab.st_mode))
648 return false;
649 if (newstab.st_dev != oldstab.st_dev ||
650 newstab.st_ino != oldstab.st_ino)
651 return false;
652 return true;
653}
654
655int
656tarobject(void *ctx, struct tar_entry *ti)
657{
658 static struct varbuf conffderefn, symlinkfn;
659 const char *usename;
660 struct filenamenode *usenode;
661
662 struct conffile *conff;
663 struct tarcontext *tc = ctx;
664 bool existingdir, keepexisting;
665 bool refcounting;
666 char oldhash[MD5HASHLEN + 1];
667 int statr;
668 ssize_t r;
669 struct stat stab, stabtmp;
670 struct file_stat nodestat;
671 struct fileinlist *nifd, **oldnifd;
672 struct pkgset *divpkgset;
673 struct pkginfo *otherpkg;
674
675 tar_entry_update_from_system(ti);
676
677 /* Perform some sanity checks on the tar entry. */
678 if (strchr(ti->name, '\n'))
679 ohshit(_("newline not allowed in archive object name '%.255s'"), ti->name);
680
681 /* Append to list of files.
682 * The trailing ‘/’ put on the end of names in tarfiles has already
683 * been stripped by tar_extractor(). */
684 oldnifd = tc->newfiles_queue->tail;
685 nifd = tar_filenamenode_queue_push(tc->newfiles_queue,
686 findnamenode(ti->name, 0));
687 nifd->namenode->flags |= fnnf_new_inarchive;
688
689 debug(dbg_eachfile,
690 "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
691 " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
692 ti->name, (long)ti->stat.mode,
693 (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
694 ti->type,
695 ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
696 ti->linkname,
697 nifd->namenode->name, nifd->namenode->flags,
698 nifd->namenode->divert && nifd->namenode->divert->useinstead
699 ? nifd->namenode->divert->useinstead->name : "<none>");
700
701 if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
702 divpkgset = nifd->namenode->divert->pkgset;
703
704 if (divpkgset) {
705 forcibleerr(fc_overwritediverted,
706 _("trying to overwrite '%.250s', which is the "
707 "diverted version of '%.250s' (package: %.100s)"),
708 nifd->namenode->name, nifd->namenode->divert->camefrom->name,
709 divpkgset->name);
710 } else {
711 forcibleerr(fc_overwritediverted,
712 _("trying to overwrite '%.250s', which is the "
713 "diverted version of '%.250s'"),
714 nifd->namenode->name, nifd->namenode->divert->camefrom->name);
715 }
716 }
717
718 if (nifd->namenode->statoverride) {
719 nodestat = *nifd->namenode->statoverride;
720 nodestat.mode |= ti->stat.mode & S_IFMT;
721 } else {
722 nodestat = ti->stat;
723 }
724
725 usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
726 usename = usenode->name;
727
728 trig_file_activate(usenode, tc->pkg);
729
730 if (nifd->namenode->flags & fnnf_new_conff) {
731 /* If it's a conffile we have to extract it next to the installed
732 * version (i.e. we do the usual link-following). */
733 if (conffderef(tc->pkg, &conffderefn, usename))
734 usename= conffderefn.buf;
735 debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
736 }
737
738 setupfnamevbs(usename);
739
740 statr= lstat(fnamevb.buf,&stab);
741 if (statr) {
742 /* The lstat failed. */
743 if (errno != ENOENT && errno != ENOTDIR)
744 ohshite(_("unable to stat '%.255s' (which I was about to install)"),
745 ti->name);
746 /* OK, so it doesn't exist.
747 * However, it's possible that we were in the middle of some other
748 * backup/restore operation and were rudely interrupted.
749 * So, we see if we have .dpkg-tmp, and if so we restore it. */
750 if (rename(fnametmpvb.buf,fnamevb.buf)) {
751 if (errno != ENOENT && errno != ENOTDIR)
752 ohshite(_("unable to clean up mess surrounding '%.255s' before "
753 "installing another version"), ti->name);
754 debug(dbg_eachfiledetail,"tarobject nonexistent");
755 } else {
756 debug(dbg_eachfiledetail,"tarobject restored tmp to main");
757 statr= lstat(fnamevb.buf,&stab);
758 if (statr)
759 ohshite(_("unable to stat restored '%.255s' before installing"
760 " another version"), ti->name);
761 }
762 } else {
763 debug(dbg_eachfiledetail,"tarobject already exists");
764 }
765
766 /* Check to see if it's a directory or link to one and we don't need to
767 * do anything. This has to be done now so that we don't die due to
768 * a file overwriting conflict. */
769 existingdir = false;
770 switch (ti->type) {
771 case TAR_FILETYPE_SYMLINK:
772 /* If it's already an existing directory, do nothing. */
773 if (!statr && S_ISDIR(stab.st_mode)) {
774 debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
775 existingdir = true;
776 } else if (!statr && S_ISLNK(stab.st_mode)) {
777 if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
778 existingdir = true;
779 }
780 break;
781 case TAR_FILETYPE_DIR:
782 /* If it's already an existing directory, do nothing. */
783 if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
784 debug(dbg_eachfiledetail, "tarobject directory exists");
785 existingdir = true;
786 }
787 break;
788 case TAR_FILETYPE_FILE:
789 case TAR_FILETYPE_CHARDEV:
790 case TAR_FILETYPE_BLOCKDEV:
791 case TAR_FILETYPE_FIFO:
792 case TAR_FILETYPE_HARDLINK:
793 break;
794 default:
795 ohshit(_("archive contained object '%.255s' of unknown type 0x%x"),
796 ti->name, ti->type);
797 }
798
799 keepexisting = false;
800 refcounting = false;
801 if (!existingdir) {
802 struct filepackages_iterator *iter;
803
804 iter = filepackages_iter_new(nifd->namenode);
805 while ((otherpkg = filepackages_iter_next(iter))) {
806 if (otherpkg == tc->pkg)
807 continue;
808 debug(dbg_eachfile, "tarobject ... found in %s",
809 pkg_name(otherpkg, pnaw_always));
810
811 /* A pkgset can share files between its instances. Overwriting
812 * is allowed when they are not getting in sync, otherwise the
813 * file content must match the installed file. */
814 if (otherpkg->set == tc->pkg->set &&
815 otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
816 tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
817 if (statr == 0 && tc->pkgset_getting_in_sync)
818 refcounting = true;
819 debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
820 pkg_name(otherpkg, pnaw_always),
821 versiondescribe(&otherpkg->installed.version, vdew_nonambig),
822 tc->pkgset_getting_in_sync);
823 continue;
824 }
825
826 if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
827 /* Right, so we may be diverting this file. This makes the conflict
828 * OK iff one of us is the diverting package (we don't need to
829 * check for both being the diverting package, obviously). */
830 divpkgset = nifd->namenode->divert->pkgset;
831 debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
832 divpkgset ? divpkgset->name : "<none>");
833 if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
834 continue;
835 }
836
837 /* If the new object is a directory and the previous object does
838 * not exist assume it's also a directory and skip further checks.
839 * XXX: Ideally with more information about the installed files we
840 * could perform more clever checks. */
841 if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
842 debug(dbg_eachfile, "tarobject ... assuming shared directory");
843 continue;
844 }
845
846 /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
847 switch (otherpkg->clientdata->replacingfilesandsaid) {
848 case 2:
849 keepexisting = true;
850 /* Fall through. */
851 case 1:
852 continue;
853 }
854
855 /* Is the package with the conflicting file in the “config files only”
856 * state? If so it must be a config file and we can silently take it
857 * over. */
858 if (otherpkg->status == PKG_STAT_CONFIGFILES)
859 continue;
860
861 /* Perhaps we're removing a conflicting package? */
862 if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
863 continue;
864
865 /* Is the file an obsolete conffile in the other package
866 * and a conffile in the new package? */
867 if ((nifd->namenode->flags & fnnf_new_conff) &&
868 !statr && S_ISREG(stab.st_mode)) {
869 for (conff = otherpkg->installed.conffiles;
870 conff;
871 conff = conff->next) {
872 if (!conff->obsolete)
873 continue;
874 if (strcmp(conff->name, nifd->namenode->name) == 0)
875 break;
876 }
877 if (conff) {
878 debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
879 /* process_archive() will have copied its hash already. */
880 continue;
881 }
882 }
883
884 if (does_replace(tc->pkg, &tc->pkg->available,
885 otherpkg, &otherpkg->installed)) {
886 printf(_("Replacing files in old package %s (%s) ...\n"),
887 pkg_name(otherpkg, pnaw_nonambig),
888 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
889 otherpkg->clientdata->replacingfilesandsaid = 1;
890 } else if (does_replace(otherpkg, &otherpkg->installed,
891 tc->pkg, &tc->pkg->available)) {
892 printf(_("Replaced by files in installed package %s (%s) ...\n"),
893 pkg_name(otherpkg, pnaw_nonambig),
894 versiondescribe(&otherpkg->installed.version, vdew_nonambig));
895 otherpkg->clientdata->replacingfilesandsaid = 2;
896 nifd->namenode->flags &= ~fnnf_new_inarchive;
897 keepexisting = true;
898 } else {
899 /* At this point we are replacing something without a Replaces. */
900 if (!statr && S_ISDIR(stab.st_mode)) {
901 forcibleerr(fc_overwritedir,
902 _("trying to overwrite directory '%.250s' "
903 "in package %.250s %.250s with nondirectory"),
904 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
905 versiondescribe(&otherpkg->installed.version,
906 vdew_nonambig));
907 } else {
908 forcibleerr(fc_overwrite,
909 _("trying to overwrite '%.250s', "
910 "which is also in package %.250s %.250s"),
911 nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
912 versiondescribe(&otherpkg->installed.version,
913 vdew_nonambig));
914 }
915 }
916 }
917 filepackages_iter_free(iter);
918 }
919
920 if (keepexisting) {
921 if (nifd->namenode->flags & fnnf_new_conff)
922 nifd->namenode->flags |= fnnf_obs_conff;
923 tar_filenamenode_queue_pop(tc->newfiles_queue, oldnifd, nifd);
924 tarobject_skip_entry(tc, ti);
925 return 0;
926 }
927
928 if (filter_should_skip(ti)) {
929 nifd->namenode->flags &= ~fnnf_new_inarchive;
930 nifd->namenode->flags |= fnnf_filtered;
931 tarobject_skip_entry(tc, ti);
932
933 return 0;
934 }
935
936 if (existingdir)
937 return 0;
938
939 /* Compute the hash of the previous object, before we might replace it
940 * with the new version on forced overwrites. */
941 if (refcounting) {
942 debug(dbg_eachfiledetail, "tarobject hashing on-disk file '%s', refcounting",
943 fnamevb.buf);
944 if (nifd->namenode->flags & fnnf_new_conff) {
945 md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
946 } else if (S_ISREG(stab.st_mode)) {
947 md5hash(tc->pkg, oldhash, fnamevb.buf);
948 } else {
949 strcpy(oldhash, EMPTYHASHFLAG);
950 }
951 }
952
953 if (refcounting && !fc_overwrite) {
954 /* If we are not forced to overwrite the path and are refcounting,
955 * just compute the hash w/o extracting the object. */
956 tarobject_hash(tc, ti, nifd->namenode);
957 } else {
958 /* Now, at this stage we want to make sure neither of .dpkg-new and
959 * .dpkg-tmp are hanging around. */
960 path_remove_tree(fnamenewvb.buf);
961 path_remove_tree(fnametmpvb.buf);
962
963 /* Now we start to do things that we need to be able to undo
964 * if something goes wrong. Watch out for the CLEANUP comments to
965 * keep an eye on what's installed on the disk at each point. */
966 push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
967
968 /*
969 * CLEANUP: Now we either have the old file on the disk, or not, in
970 * its original filename.
971 */
972
973 /* Extract whatever it is as .dpkg-new ... */
974 tarobject_extract(tc, ti, fnamenewvb.buf, &nodestat, nifd->namenode);
975 }
976
977 /* For shared files, check now if the object matches. */
978 if (refcounting)
979 tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
980 fnamenewvb.buf, ti, nifd->namenode);
981
982 /* If we didn't extract anything, there's nothing else to do. */
983 if (refcounting && !fc_overwrite)
984 return 0;
985
986 tarobject_set_perms(ti, fnamenewvb.buf, &nodestat);
987 tarobject_set_mtime(ti, fnamenewvb.buf);
988 tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, nodestat.mode);
989
990 /*
991 * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
992 * if the file already exists as a directory and we were trying to
993 * extract a directory or symlink, we returned earlier, so we don't
994 * need to worry about that here).
995 *
996 * The old file is still in the original filename,
997 */
998
999 /* First, check to see if it's a conffile. If so we don't install
1000 * it now - we leave it in .dpkg-new for --configure to take care of. */
1001 if (nifd->namenode->flags & fnnf_new_conff) {
1002 debug(dbg_conffdetail,"tarobject conffile extracted");
1003 nifd->namenode->flags |= fnnf_elide_other_lists;
1004 return 0;
1005 }
1006
1007 /* Now we move the old file out of the way, the backup file will
1008 * be deleted later. */
1009 if (statr) {
1010 /* Don't try to back it up if it didn't exist. */
1011 debug(dbg_eachfiledetail,"tarobject new - no backup");
1012 } else {
1013 if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
1014 /* One of the two is a directory - can't do atomic install. */
1015 debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
1016 nifd->namenode->flags |= fnnf_no_atomic_overwrite;
1017 if (rename(fnamevb.buf,fnametmpvb.buf))
1018 ohshite(_("unable to move aside '%.255s' to install new version"),
1019 ti->name);
1020 } else if (S_ISLNK(stab.st_mode)) {
1021 /* We can't make a symlink with two hardlinks, so we'll have to
1022 * copy it. (Pretend that making a copy of a symlink is the same
1023 * as linking to it.) */
1024 varbuf_reset(&symlinkfn);
1025 varbuf_grow(&symlinkfn, stab.st_size + 1);
1026 r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
1027 if (r < 0)
1028 ohshite(_("unable to read link '%.255s'"), ti->name);
1029 else if (r > stab.st_size)
1030 ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1031 fnamevb.buf, (intmax_t)stab.st_size, r);
1032 else if (r < stab.st_size)
1033 warning(_("symbolic link '%.250s' size has changed from %jd to %zd"),
1034 fnamevb.buf, (intmax_t)stab.st_size, r);
1035 varbuf_trunc(&symlinkfn, r);
1036 varbuf_end_str(&symlinkfn);
1037 if (symlink(symlinkfn.buf,fnametmpvb.buf))
1038 ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
1039 if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
1040 ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
1041 tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
1042 } else {
1043 debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
1044 if (link(fnamevb.buf,fnametmpvb.buf))
1045 ohshite(_("unable to make backup link of '%.255s' before installing new version"),
1046 ti->name);
1047 }
1048 }
1049
1050 /*
1051 * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
1052 * in .dpkg-new.
1053 */
1054
1055 if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
1056 ti->type == TAR_FILETYPE_SYMLINK) {
1057 nifd->namenode->flags |= fnnf_deferred_rename;
1058
1059 debug(dbg_eachfiledetail, "tarobject done and installation deferred");
1060 } else {
1061 if (rename(fnamenewvb.buf, fnamevb.buf))
1062 ohshite(_("unable to install new version of '%.255s'"), ti->name);
1063
1064 /*
1065 * CLEANUP: Now the new file is in the destination file, and the
1066 * old file is in .dpkg-tmp to be cleaned up later. We now need
1067 * to take a different attitude to cleanup, because we need to
1068 * remove the new file.
1069 */
1070
1071 nifd->namenode->flags |= fnnf_placed_on_disk;
1072 nifd->namenode->flags |= fnnf_elide_other_lists;
1073
1074 debug(dbg_eachfiledetail, "tarobject done and installed");
1075 }
1076
1077 return 0;
1078}
1079
1080#if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
1081static void
1082tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
1083{
1084 struct fileinlist *cfile;
1085
1086 for (cfile = files; cfile; cfile = cfile->next) {
1087 struct filenamenode *usenode;
1088 int fd;
1089
1090 if (!(cfile->namenode->flags & fnnf_deferred_fsync))
1091 continue;
1092
1093 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1094
1095 setupfnamevbs(usenode->name);
1096
1097 fd = open(fnamenewvb.buf, O_WRONLY);
1098 if (fd < 0)
1099 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1100 /* Ignore the return code as it should be considered equivalent to an
1101 * asynchronous hint for the kernel, we are doing an fsync() later on
1102 * anyway. */
1103 sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
1104 if (close(fd))
1105 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1106 }
1107}
1108#else
1109static void
1110tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
1111{
1112}
1113#endif
1114
1115void
1116tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
1117{
1118 struct fileinlist *cfile;
1119 struct filenamenode *usenode;
1120
1121 tar_writeback_barrier(files, pkg);
1122
1123 for (cfile = files; cfile; cfile = cfile->next) {
1124 debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
1125
1126 if (!(cfile->namenode->flags & fnnf_deferred_rename))
1127 continue;
1128
1129 usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
1130
1131 setupfnamevbs(usenode->name);
1132
1133 if (cfile->namenode->flags & fnnf_deferred_fsync) {
1134 int fd;
1135
1136 debug(dbg_eachfiledetail, "deferred extract needs fsync");
1137
1138 fd = open(fnamenewvb.buf, O_WRONLY);
1139 if (fd < 0)
1140 ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
1141 if (fsync(fd))
1142 ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
1143 if (close(fd))
1144 ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
1145
1146 cfile->namenode->flags &= ~fnnf_deferred_fsync;
1147 }
1148
1149 debug(dbg_eachfiledetail, "deferred extract needs rename");
1150
1151 if (rename(fnamenewvb.buf, fnamevb.buf))
1152 ohshite(_("unable to install new version of '%.255s'"),
1153 cfile->namenode->name);
1154
1155 cfile->namenode->flags &= ~fnnf_deferred_rename;
1156
1157 /*
1158 * CLEANUP: Now the new file is in the destination file, and the
1159 * old file is in .dpkg-tmp to be cleaned up later. We now need
1160 * to take a different attitude to cleanup, because we need to
1161 * remove the new file.
1162 */
1163
1164 cfile->namenode->flags |= fnnf_placed_on_disk;
1165 cfile->namenode->flags |= fnnf_elide_other_lists;
1166
1167 debug(dbg_eachfiledetail, "deferred extract done and installed");
1168 }
1169}
1170
1171void
1172enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
1173{
1174 struct pkg_deconf_list *newdeconf;
1175
1176 ensure_package_clientdata(pkg);
1177 pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
1178 newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
1179 newdeconf->next = deconfigure;
1180 newdeconf->pkg = pkg;
1181 newdeconf->pkg_removal = pkg_removal;
1182 deconfigure = newdeconf;
1183}
1184
1185void
1186clear_deconfigure_queue(void)
1187{
1188 struct pkg_deconf_list *deconf, *deconf_next;
1189
1190 for (deconf = deconfigure; deconf; deconf = deconf_next) {
1191 deconf_next = deconf->next;
1192 free(deconf);
1193 }
1194 deconfigure = NULL;
1195}
1196
1197/**
1198 * Try if we can deconfigure the package and queue it if so.
1199 *
1200 * Also checks whether the pdep is forced, first, according to force_p.
1201 * force_p may be NULL in which case nothing is considered forced.
1202 *
1203 * Action is a string describing the action which causes the
1204 * deconfiguration:
1205 *
1206 * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
1207 * "installation of <package>" (due to Breaks; removal == NULL)
1208 *
1209 * @retval 0 Not possible (why is printed).
1210 * @retval 1 Deconfiguration queued ok (no message printed).
1211 * @retval 2 Forced (no deconfiguration needed, why is printed).
1212 */
1213static int
1214try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
1215 struct deppossi *pdep, const char *action,
1216 struct pkginfo *removal, const char *why)
1217{
1218 if (force_p && force_p(pdep)) {
1219 warning(_("ignoring dependency problem with %s:\n%s"), action, why);
1220 return 2;
1221 } else if (f_autodeconf) {
1222 if (pkg->installed.essential) {
1223 if (fc_removeessential) {
1224 warning(_("considering deconfiguration of essential\n"
1225 " package %s, to enable %s"),
1226 pkg_name(pkg, pnaw_nonambig), action);
1227 } else {
1228 notice(_("no, %s is essential, will not deconfigure\n"
1229 " it in order to enable %s"),
1230 pkg_name(pkg, pnaw_nonambig), action);
1231 return 0;
1232 }
1233 }
1234 enqueue_deconfigure(pkg, removal);
1235 return 1;
1236 } else {
1237 notice(_("no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
1238 action, why);
1239 return 0;
1240 }
1241}
1242
1243static int try_remove_can(struct deppossi *pdep,
1244 struct pkginfo *fixbyrm,
1245 const char *why) {
1246 char action[512];
1247 sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
1248 return try_deconfigure_can(force_depends, pdep->up->up, pdep,
1249 action, fixbyrm, why);
1250}
1251
1252void check_breaks(struct dependency *dep, struct pkginfo *pkg,
1253 const char *pfilename) {
1254 struct pkginfo *fixbydeconf;
1255 struct varbuf why = VARBUF_INIT;
1256 int ok;
1257
1258 fixbydeconf = NULL;
1259 if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
1260 varbuf_destroy(&why);
1261 return;
1262 }
1263
1264 varbuf_end_str(&why);
1265
1266 if (fixbydeconf && f_autodeconf) {
1267 char action[512];
1268
1269 ensure_package_clientdata(fixbydeconf);
1270 assert(fixbydeconf->clientdata->istobe == PKG_ISTOBE_NORMAL);
1271
1272 sprintf(action, _("installation of %.250s"),
1273 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1274 notice(_("considering deconfiguration of %s, which would be broken by %s ..."),
1275 pkg_name(fixbydeconf, pnaw_nonambig), action);
1276
1277 ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
1278 action, NULL, why.buf);
1279 if (ok == 1) {
1280 notice(_("yes, will deconfigure %s (broken by %s)"),
1281 pkg_name(fixbydeconf, pnaw_nonambig),
1282 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1283 }
1284 } else {
1285 notice(_("regarding %s containing %s:\n%s"), pfilename,
1286 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
1287 ok= 0;
1288 }
1289 varbuf_destroy(&why);
1290 if (ok > 0) return;
1291
1292 if (force_breaks(dep->list)) {
1293 warning(_("ignoring breakage, may proceed anyway!"));
1294 return;
1295 }
1296
1297 if (fixbydeconf && !f_autodeconf) {
1298 ohshit(_("installing %.250s would break %.250s, and\n"
1299 " deconfiguration is not permitted (--auto-deconfigure might help)"),
1300 pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
1301 pkg_name(fixbydeconf, pnaw_nonambig));
1302 } else {
1303 ohshit(_("installing %.250s would break existing software"),
1304 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1305 }
1306}
1307
1308void check_conflict(struct dependency *dep, struct pkginfo *pkg,
1309 const char *pfilename) {
1310 struct pkginfo *fixbyrm;
1311 struct deppossi *pdep, flagdeppossi;
1312 struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
1313 struct dependency *providecheck;
1314
1315 fixbyrm = NULL;
1316 if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
1317 varbuf_destroy(&conflictwhy);
1318 varbuf_destroy(&removalwhy);
1319 return;
1320 }
1321 if (fixbyrm) {
1322 ensure_package_clientdata(fixbyrm);
1323 if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
1324 fixbyrm= dep->up;
1325 ensure_package_clientdata(fixbyrm);
1326 }
1327 if (((pkg->available.essential && fixbyrm->installed.essential) ||
1328 (((fixbyrm->want != PKG_WANT_INSTALL &&
1329 fixbyrm->want != PKG_WANT_HOLD) ||
1330 does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
1331 (!fixbyrm->installed.essential || fc_removeessential)))) {
1332 assert(fixbyrm->clientdata->istobe == PKG_ISTOBE_NORMAL ||
1333 fixbyrm->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
1334 fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
1335 notice(_("considering removing %s in favour of %s ..."),
1336 pkg_name(fixbyrm, pnaw_nonambig),
1337 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1338 if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
1339 fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
1340 fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
1341 notice(_("%s is not properly installed; ignoring any dependencies on it"),
1342 pkg_name(fixbyrm, pnaw_nonambig));
1343 pdep = NULL;
1344 } else {
1345 for (pdep = fixbyrm->set->depended.installed;
1346 pdep;
1347 pdep = pdep->rev_next) {
1348 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1349 continue;
1350 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1351 continue;
1352 varbuf_end_str(&removalwhy);
1353 if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
1354 break;
1355 }
1356 if (!pdep) {
1357 /* If we haven't found a reason not to yet, let's look some more. */
1358 for (providecheck= fixbyrm->installed.depends;
1359 providecheck;
1360 providecheck= providecheck->next) {
1361 if (providecheck->type != dep_provides) continue;
1362 for (pdep = providecheck->list->ed->depended.installed;
1363 pdep;
1364 pdep = pdep->rev_next) {
1365 if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
1366 continue;
1367 if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
1368 continue;
1369 varbuf_end_str(&removalwhy);
1370 notice(_("may have trouble removing %s, as it provides %s ..."),
1371 pkg_name(fixbyrm, pnaw_nonambig),
1372 providecheck->list->ed->name);
1373 if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
1374 goto break_from_both_loops_at_once;
1375 }
1376 }
1377 break_from_both_loops_at_once:;
1378 }
1379 }
1380 if (!pdep && skip_due_to_hold(fixbyrm)) {
1381 pdep= &flagdeppossi;
1382 }
1383 if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
1384 if (fc_removereinstreq) {
1385 notice(_("package %s requires reinstallation, but will "
1386 "remove anyway as you requested"),
1387 pkg_name(fixbyrm, pnaw_nonambig));
1388 } else {
1389 notice(_("package %s requires reinstallation, will not remove"),
1390 pkg_name(fixbyrm, pnaw_nonambig));
1391 pdep= &flagdeppossi;
1392 }
1393 }
1394 if (!pdep) {
1395 /* This conflict is OK - we'll remove the conflictor. */
1396 enqueue_conflictor(fixbyrm);
1397 varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
1398 notice(_("yes, will remove %s in favour of %s"),
1399 pkg_name(fixbyrm, pnaw_nonambig),
1400 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1401 return;
1402 }
1403 /* Put it back. */
1404 fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
1405 }
1406 }
1407 varbuf_end_str(&conflictwhy);
1408 notice(_("regarding %s containing %s:\n%s"), pfilename,
1409 pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
1410 if (!force_conflicts(dep->list))
1411 ohshit(_("conflicting packages - not installing %.250s"),
1412 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1413 warning(_("ignoring conflict, may proceed anyway!"));
1414 varbuf_destroy(&conflictwhy);
1415
1416 return;
1417}
1418
1419void cu_cidir(int argc, void **argv) {
1420 char *cidir= (char*)argv[0];
1421 char *cidirrest= (char*)argv[1];
1422 cidirrest[-1] = '\0';
1423 path_remove_tree(cidir);
1424}
1425
1426void cu_fileslist(int argc, void **argv) {
1427 tar_pool_release();
1428}
1429
1430int
1431archivefiles(const char *const *argv)
1432{
1433 const char *const *volatile argp;
1434 const char **volatile arglist = NULL;
1435 int i;
1436 jmp_buf ejbuf;
1437 enum modstatdb_rw msdbflags;
1438
1439 trigproc_install_hooks();
1440
1441 if (f_noact)
1442 msdbflags = msdbrw_readonly;
1443 else if (cipaction->arg_int == act_avail)
1444 msdbflags = msdbrw_readonly | msdbrw_available_write;
1445 else if (fc_nonroot)
1446 msdbflags = msdbrw_write;
1447 else
1448 msdbflags = msdbrw_needsuperuser;
1449
1450 modstatdb_open(msdbflags);
1451
1452 checkpath();
1453 pkg_infodb_upgrade();
1454
1455 log_message("startup archives %s", cipaction->olong);
1456
1457 if (f_recursive) {
1458 const char *const *ap;
1459 int nfiles = 0;
1460
1461 if (!*argv)
1462 badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
1463
1464 for (ap = argv; *ap; ap++) {
1465 struct treeroot *tree;
1466 struct treenode *node;
1467
1468 tree = treewalk_open((const char *)*ap, TREEWALK_FOLLOW_LINKS, NULL);
1469
1470 while ((node = treewalk_next(tree))) {
1471 const char *nodename;
1472
1473 if (!S_ISREG(treenode_get_mode(node)))
1474 continue;
1475
1476 /* Check if it looks like a .deb file. */
1477 nodename = treenode_get_pathname(node);
1478 if (strcmp(nodename + strlen(nodename) - 4, ".deb") != 0)
1479 continue;
1480
1481 arglist = m_realloc(arglist, sizeof(char *) * (nfiles + 2));
1482 arglist[nfiles++] = m_strdup(nodename);
1483 }
1484
1485 treewalk_close(tree);
1486 }
1487
1488 if (!nfiles)
1489 ohshit(_("searched, but found no packages (files matching *.deb)"));
1490
1491 arglist[nfiles] = NULL;
1492 argp= arglist;
1493 } else {
1494 if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
1495 cipaction->olong);
1496 argp= argv;
1497 }
1498
1499 /* Perform some sanity checks on the passed archives. */
1500 for (i = 0; argp[i]; i++) {
1501 struct stat st;
1502
1503 /* We need the filename to exist. */
1504 if (stat(argp[i], &st) < 0)
1505 ohshite(_("cannot access archive '%s'"), argp[i]);
1506
1507 /* We cannot work with anything that is not a regular file. */
1508 if (!S_ISREG(st.st_mode))
1509 ohshit(_("archive '%s' is not a regular file"), argp[i]);
1510 }
1511
1512 currenttime = time(NULL);
1513
1514 /* Initialize fname variables contents. */
1515
1516 varbuf_reset(&fnamevb);
1517 varbuf_reset(&fnametmpvb);
1518 varbuf_reset(&fnamenewvb);
1519
1520 varbuf_add_str(&fnamevb, instdir);
1521 varbuf_add_str(&fnametmpvb, instdir);
1522 varbuf_add_str(&fnamenewvb, instdir);
1523
1524 varbuf_snapshot(&fnamevb, &fname_state);
1525
1526 ensure_diversions();
1527 ensure_statoverrides(STATDB_PARSE_NORMAL);
1528
1529 for (i = 0; argp[i]; i++) {
1530 if (setjmp(ejbuf)) {
1531 pop_error_context(ehflag_bombout);
1532 if (abort_processing)
1533 break;
1534 continue;
1535 }
1536 push_error_context_jump(&ejbuf, print_error_perarchive, argp[i]);
1537
1538 dpkg_selabel_load();
1539
1540 process_archive(argp[i]);
1541 onerr_abort++;
1542 m_output(stdout, _("<standard output>"));
1543 m_output(stderr, _("<standard error>"));
1544 onerr_abort--;
1545
1546 pop_error_context(ehflag_normaltidy);
1547 }
1548
1549 dpkg_selabel_close();
1550
1551 free(arglist);
1552
1553 switch (cipaction->arg_int) {
1554 case act_install:
1555 case act_configure:
1556 case act_triggers:
1557 case act_remove:
1558 case act_purge:
1559 process_queue();
1560 case act_unpack:
1561 case act_avail:
1562 break;
1563 default:
1564 internerr("unknown action '%d'", cipaction->arg_int);
1565 }
1566
1567 trigproc_run_deferred();
1568 modstatdb_shutdown();
1569
1570 return 0;
1571}
1572
1573/**
1574 * Decide whether we want to install a new version of the package.
1575 *
1576 * @param pkg The package with the version we might want to install
1577 *
1578 * @retval true If the package should be skipped.
1579 * @retval false If the package should be installed.
1580 */
1581bool
1582wanttoinstall(struct pkginfo *pkg)
1583{
1584 int rc;
1585
1586 if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
1587 if (f_alsoselect) {
1588 printf(_("Selecting previously unselected package %s.\n"),
1589 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1590 return true;
1591 } else {
1592 printf(_("Skipping unselected package %s.\n"),
1593 pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
1594 return false;
1595 }
1596 }
1597
1598 if (pkg->eflag & PKG_EFLAG_REINSTREQ)
1599 return true;
1600 if (pkg->status < PKG_STAT_UNPACKED)
1601 return true;
1602
1603 rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
1604 if (rc > 0) {
1605 return true;
1606 } else if (rc == 0) {
1607 /* Same version fully installed. */
1608 if (f_skipsame) {
1609 notice(_("version %.250s of %.250s already installed, skipping"),
1610 versiondescribe(&pkg->installed.version, vdew_nonambig),
1611 pkg_name(pkg, pnaw_nonambig));
1612 return false;
1613 } else {
1614 return true;
1615 }
1616 } else {
1617 if (fc_downgrade) {
1618 warning(_("downgrading %.250s from %.250s to %.250s"),
1619 pkg_name(pkg, pnaw_nonambig),
1620 versiondescribe(&pkg->installed.version, vdew_nonambig),
1621 versiondescribe(&pkg->available.version, vdew_nonambig));
1622 return true;
1623 } else {
1624 notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
1625 pkg_name(pkg, pnaw_nonambig),
1626 versiondescribe(&pkg->installed.version, vdew_nonambig),
1627 versiondescribe(&pkg->available.version, vdew_nonambig));
1628 return false;
1629 }
1630 }
1631}