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