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