archives.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456
  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 <ian@chiark.greenend.org.uk>
  6. * Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
  7. * Copyright © 2007-2012 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26. #include <sys/stat.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <obstack.h>
  38. #define obstack_chunk_alloc m_malloc
  39. #define obstack_chunk_free free
  40. #include <dpkg/i18n.h>
  41. #include <dpkg/dpkg.h>
  42. #include <dpkg/dpkg-db.h>
  43. #include <dpkg/pkg.h>
  44. #include <dpkg/path.h>
  45. #include <dpkg/fdio.h>
  46. #include <dpkg/buffer.h>
  47. #include <dpkg/subproc.h>
  48. #include <dpkg/command.h>
  49. #include <dpkg/file.h>
  50. #include <dpkg/tarfn.h>
  51. #include <dpkg/options.h>
  52. #include <dpkg/triglib.h>
  53. #ifdef WITH_SELINUX
  54. #include <selinux/selinux.h>
  55. #endif
  56. #include "filesdb.h"
  57. #include "main.h"
  58. #include "archives.h"
  59. #include "filters.h"
  60. static inline void
  61. fd_writeback_init(int fd)
  62. {
  63. /* Ignore the return code as it should be considered equivalent to an
  64. * asynchronous hint for the kernel, we are doing an fsync() later on
  65. * anyway. */
  66. #if defined(SYNC_FILE_RANGE_WRITE)
  67. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
  68. #elif defined(HAVE_POSIX_FADVISE)
  69. posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
  70. #endif
  71. }
  72. static struct obstack tar_obs;
  73. static bool tarobs_init = false;
  74. /**
  75. * Ensure the obstack is properly initialized.
  76. */
  77. static void ensureobstackinit(void) {
  78. if (!tarobs_init) {
  79. obstack_init(&tar_obs);
  80. tarobs_init = true;
  81. }
  82. }
  83. /**
  84. * Destroy the obstack.
  85. */
  86. static void destroyobstack(void) {
  87. if (tarobs_init) {
  88. obstack_free(&tar_obs, NULL);
  89. tarobs_init = false;
  90. }
  91. }
  92. /**
  93. * Check if a file or directory will save a package from disappearance.
  94. *
  95. * A package can only be saved by a file or directory which is part
  96. * only of itself - it must be neither part of the new package being
  97. * installed nor part of any 3rd package (this is important so that
  98. * shared directories don't stop packages from disappearing).
  99. */
  100. bool
  101. filesavespackage(struct fileinlist *file,
  102. struct pkginfo *pkgtobesaved,
  103. struct pkginfo *pkgbeinginstalled)
  104. {
  105. struct filepackages_iterator *iter;
  106. struct pkgset *divpkgset;
  107. struct pkginfo *thirdpkg;
  108. debug(dbg_eachfiledetail,"filesavespackage file `%s' package %s",
  109. file->namenode->name, pkg_name(pkgtobesaved, pnaw_nonambig));
  110. /* If the file is a contended one and it's overridden by either
  111. * the package we're considering disappearing or the package
  112. * we're installing then they're not actually the same file, so
  113. * we can't disappear the package - it is saved by this file. */
  114. if (file->namenode->divert && file->namenode->divert->useinstead) {
  115. divpkgset = file->namenode->divert->pkgset;
  116. if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
  117. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  118. return true;
  119. }
  120. }
  121. /* Is the file in the package being installed? If so then it can't save. */
  122. if (file->namenode->flags & fnnf_new_inarchive) {
  123. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  124. return false;
  125. }
  126. /* Look for a 3rd package which can take over the file (in case
  127. * it's a directory which is shared by many packages. */
  128. iter = filepackages_iter_new(file->namenode);
  129. while ((thirdpkg = filepackages_iter_next(iter))) {
  130. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  131. pkg_name(thirdpkg, pnaw_nonambig));
  132. /* Is this not the package being installed or the one being
  133. * checked for disappearance? */
  134. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
  135. continue;
  136. /* If !fileslistvalid then we've already disappeared this one, so
  137. * we shouldn't try to make it take over this shared directory. */
  138. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  139. if (!thirdpkg->clientdata->fileslistvalid) {
  140. debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
  141. continue;
  142. }
  143. /* We've found a package that can take this file. */
  144. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  145. return false;
  146. }
  147. filepackages_iter_free(iter);
  148. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  149. return true;
  150. }
  151. void cu_pathname(int argc, void **argv) {
  152. ensure_pathname_nonexisting((char*)(argv[0]));
  153. }
  154. int tarfileread(void *ud, char *buf, int len) {
  155. struct tarcontext *tc= (struct tarcontext*)ud;
  156. int r;
  157. r = fd_read(tc->backendpipe, buf, len);
  158. if (r < 0)
  159. ohshite(_("error reading from dpkg-deb pipe"));
  160. return r;
  161. }
  162. static void
  163. tarfile_skip_one_forward(struct tarcontext *tc, struct tar_entry *ti)
  164. {
  165. size_t r;
  166. char databuf[TARBLKSZ];
  167. /* We need to advance the tar file to the next object, so read the
  168. * file data and set it to oblivion. */
  169. if (ti->type == tar_filetype_file) {
  170. char fnamebuf[256];
  171. fd_skip(tc->backendpipe, ti->size,
  172. _("skipped unpacking file '%.255s' (replaced or excluded?)"),
  173. path_quote_filename(fnamebuf, ti->name, 256));
  174. r = ti->size % TARBLKSZ;
  175. if (r > 0)
  176. if (fd_read(tc->backendpipe, databuf, TARBLKSZ - r) < 0)
  177. ohshite(_("error reading from dpkg-deb pipe"));
  178. }
  179. }
  180. int fnameidlu;
  181. struct varbuf fnamevb;
  182. struct varbuf fnametmpvb;
  183. struct varbuf fnamenewvb;
  184. struct pkg_deconf_list *deconfigure = NULL;
  185. static time_t currenttime;
  186. static int
  187. does_replace(struct pkginfo *newpigp, struct pkgbin *newpifp,
  188. struct pkginfo *oldpigp, struct pkgbin *oldpifp)
  189. {
  190. struct dependency *dep;
  191. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
  192. pkgbin_name(newpigp, newpifp, pnaw_nonambig),
  193. pkgbin_name(oldpigp, oldpifp, pnaw_nonambig),
  194. versiondescribe(&oldpifp->version, vdew_always));
  195. for (dep= newpifp->depends; dep; dep= dep->next) {
  196. if (dep->type != dep_replaces || dep->list->ed != oldpigp->set)
  197. continue;
  198. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  199. versiondescribe(&dep->list->version,vdew_always));
  200. if (!versionsatisfied(oldpifp, dep->list))
  201. continue;
  202. debug(dbg_depcon,"does_replace ... yes");
  203. return true;
  204. }
  205. debug(dbg_depcon,"does_replace ... no");
  206. return false;
  207. }
  208. static void
  209. tarobject_set_mtime(struct tar_entry *te, const char *path)
  210. {
  211. struct timeval tv[2];
  212. tv[0].tv_sec = currenttime;
  213. tv[0].tv_usec = 0;
  214. tv[1].tv_sec = te->mtime;
  215. tv[1].tv_usec = 0;
  216. if (te->type == tar_filetype_symlink) {
  217. #ifdef HAVE_LUTIMES
  218. if (lutimes(path, tv) && errno != ENOSYS)
  219. ohshite(_("error setting timestamps of `%.255s'"), path);
  220. #endif
  221. } else {
  222. if (utimes(path, tv))
  223. ohshite(_("error setting timestamps of `%.255s'"), path);
  224. }
  225. }
  226. static void
  227. tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
  228. {
  229. if (te->type == tar_filetype_file)
  230. return; /* Already handled using the file descriptor. */
  231. if (te->type == tar_filetype_symlink) {
  232. if (lchown(path, st->uid, st->gid))
  233. ohshite(_("error setting ownership of symlink `%.255s'"), path);
  234. } else {
  235. if (chown(path, st->uid, st->gid))
  236. ohshite(_("error setting ownership of `%.255s'"), path);
  237. if (chmod(path, st->mode & ~S_IFMT))
  238. ohshite(_("error setting permissions of `%.255s'"), path);
  239. }
  240. }
  241. static void
  242. tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
  243. {
  244. #ifdef WITH_SELINUX
  245. static int selinux_enabled = -1;
  246. security_context_t scontext = NULL;
  247. int ret;
  248. /* If there's no file type, just give up. */
  249. if ((mode & S_IFMT) == 0)
  250. return;
  251. /* Set selinux_enabled if it is not already set (singleton). */
  252. if (selinux_enabled < 0)
  253. selinux_enabled = (is_selinux_enabled() > 0);
  254. /* If SE Linux is not enabled just do nothing. */
  255. if (!selinux_enabled)
  256. return;
  257. /* XXX: Well, we could use set_matchpathcon_printf() to redirect the
  258. * errors from the following bit, but that seems too much effort. */
  259. /* Do nothing if we can't figure out what the context is, or if it has
  260. * no context; in which case the default context shall be applied. */
  261. ret = matchpathcon(matchpath, mode & S_IFMT, &scontext);
  262. if (ret == -1 || (ret == 0 && scontext == NULL))
  263. return;
  264. if (strcmp(scontext, "<<none>>") != 0) {
  265. if (lsetfilecon(path, scontext) < 0)
  266. /* XXX: This might need to be fatal instead!? */
  267. perror("Error setting security context for next file object:");
  268. }
  269. freecon(scontext);
  270. #endif /* WITH_SELINUX */
  271. }
  272. void setupfnamevbs(const char *filename) {
  273. varbuf_trunc(&fnamevb, fnameidlu);
  274. varbuf_add_str(&fnamevb, filename);
  275. varbuf_end_str(&fnamevb);
  276. varbuf_trunc(&fnametmpvb, fnameidlu);
  277. varbuf_add_str(&fnametmpvb, filename);
  278. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  279. varbuf_end_str(&fnametmpvb);
  280. varbuf_trunc(&fnamenewvb, fnameidlu);
  281. varbuf_add_str(&fnamenewvb, filename);
  282. varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
  283. varbuf_end_str(&fnamenewvb);
  284. debug(dbg_eachfiledetail, "setupvnamevbs main=`%s' tmp=`%s' new=`%s'",
  285. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  286. }
  287. /**
  288. * Securely remove a pathname.
  289. *
  290. * This is a secure version of remove(3) using secure_unlink() instead of
  291. * unlink(2).
  292. *
  293. * @retval 0 On success.
  294. * @retval -1 On failure, just like unlink(2) & rmdir(2).
  295. */
  296. int
  297. secure_remove(const char *filename)
  298. {
  299. int r, e;
  300. if (!rmdir(filename)) {
  301. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir OK", filename);
  302. return 0;
  303. }
  304. if (errno != ENOTDIR) {
  305. e= errno;
  306. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir %s", filename,
  307. strerror(e));
  308. errno= e; return -1;
  309. }
  310. r = secure_unlink(filename);
  311. e = errno;
  312. debug(dbg_eachfiledetail, "secure_remove '%s' unlink %s",
  313. filename, r ? strerror(e) : "OK");
  314. errno= e; return r;
  315. }
  316. struct fileinlist *addfiletolist(struct tarcontext *tc,
  317. struct filenamenode *namenode) {
  318. struct fileinlist *nifd;
  319. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  320. nifd->namenode= namenode;
  321. nifd->next = NULL;
  322. *tc->newfilesp = nifd;
  323. tc->newfilesp = &nifd->next;
  324. return nifd;
  325. }
  326. static void
  327. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  328. struct fileinlist **oldnifd,
  329. struct fileinlist *nifd)
  330. {
  331. obstack_free(&tar_obs, nifd);
  332. tc->newfilesp = oldnifd;
  333. *oldnifd = NULL;
  334. }
  335. static bool
  336. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  337. struct varbuf *symlinkfn)
  338. {
  339. struct stat oldstab, newstab;
  340. int statr;
  341. const char *lastslash;
  342. statr= stat(fname, &oldstab);
  343. if (statr) {
  344. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  345. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  346. fname);
  347. return false;
  348. }
  349. if (!S_ISDIR(oldstab.st_mode))
  350. return false;
  351. /* But is it to the same dir? */
  352. varbuf_reset(symlinkfn);
  353. if (ti->linkname[0] == '/') {
  354. varbuf_add_str(symlinkfn, instdir);
  355. } else {
  356. lastslash= strrchr(fname, '/');
  357. assert(lastslash);
  358. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  359. }
  360. varbuf_add_str(symlinkfn, ti->linkname);
  361. varbuf_end_str(symlinkfn);
  362. statr= stat(symlinkfn->buf, &newstab);
  363. if (statr) {
  364. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  365. ohshite(_("failed to stat (dereference) proposed new symlink target"
  366. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  367. return false;
  368. }
  369. if (!S_ISDIR(newstab.st_mode))
  370. return false;
  371. if (newstab.st_dev != oldstab.st_dev ||
  372. newstab.st_ino != oldstab.st_ino)
  373. return false;
  374. return true;
  375. }
  376. int
  377. tarobject(void *ctx, struct tar_entry *ti)
  378. {
  379. static struct varbuf conffderefn, hardlinkfn, symlinkfn;
  380. static int fd;
  381. const char *usename;
  382. struct filenamenode *usenode;
  383. struct filenamenode *linknode;
  384. struct conffile *conff;
  385. struct tarcontext *tc = ctx;
  386. bool existingdir, keepexisting;
  387. int statr;
  388. ssize_t r;
  389. struct stat stab, stabtmp;
  390. char databuf[TARBLKSZ];
  391. struct file_stat *st;
  392. struct fileinlist *nifd, **oldnifd;
  393. struct pkgset *divpkgset;
  394. struct pkginfo *otherpkg;
  395. ensureobstackinit();
  396. /* Append to list of files.
  397. * The trailing ‘/’ put on the end of names in tarfiles has already
  398. * been stripped by tar_extractor(). */
  399. oldnifd= tc->newfilesp;
  400. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  401. nifd->namenode->flags |= fnnf_new_inarchive;
  402. debug(dbg_eachfile,
  403. "tarobject ti->name='%s' mode=%lo owner=%u.%u type=%d(%c)"
  404. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  405. ti->name, (long)ti->stat.mode,
  406. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  407. ti->type,
  408. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  409. ti->linkname,
  410. nifd->namenode->name, nifd->namenode->flags,
  411. nifd->namenode->divert && nifd->namenode->divert->useinstead
  412. ? nifd->namenode->divert->useinstead->name : "<none>");
  413. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  414. divpkgset = nifd->namenode->divert->pkgset;
  415. if (divpkgset) {
  416. forcibleerr(fc_overwritediverted,
  417. _("trying to overwrite `%.250s', which is the "
  418. "diverted version of `%.250s' (package: %.100s)"),
  419. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  420. divpkgset->name);
  421. } else {
  422. forcibleerr(fc_overwritediverted,
  423. _("trying to overwrite `%.250s', which is the "
  424. "diverted version of `%.250s'"),
  425. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  426. }
  427. }
  428. if (nifd->namenode->statoverride)
  429. st = nifd->namenode->statoverride;
  430. else
  431. st = &ti->stat;
  432. usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
  433. usename = usenode->name + 1; /* Skip the leading '/'. */
  434. trig_file_activate(usenode, tc->pkg);
  435. if (nifd->namenode->flags & fnnf_new_conff) {
  436. /* If it's a conffile we have to extract it next to the installed
  437. * version (i.e. we do the usual link-following). */
  438. if (conffderef(tc->pkg, &conffderefn, usename))
  439. usename= conffderefn.buf;
  440. debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename);
  441. }
  442. setupfnamevbs(usename);
  443. statr= lstat(fnamevb.buf,&stab);
  444. if (statr) {
  445. /* The lstat failed. */
  446. if (errno != ENOENT && errno != ENOTDIR)
  447. ohshite(_("unable to stat `%.255s' (which I was about to install)"),
  448. ti->name);
  449. /* OK, so it doesn't exist.
  450. * However, it's possible that we were in the middle of some other
  451. * backup/restore operation and were rudely interrupted.
  452. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  453. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  454. if (errno != ENOENT && errno != ENOTDIR)
  455. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  456. "installing another version"), ti->name);
  457. debug(dbg_eachfiledetail,"tarobject nonexistent");
  458. } else {
  459. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  460. statr= lstat(fnamevb.buf,&stab);
  461. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  462. " another version"), ti->name);
  463. }
  464. } else {
  465. debug(dbg_eachfiledetail,"tarobject already exists");
  466. }
  467. /* Check to see if it's a directory or link to one and we don't need to
  468. * do anything. This has to be done now so that we don't die due to
  469. * a file overwriting conflict. */
  470. existingdir = false;
  471. switch (ti->type) {
  472. case tar_filetype_symlink:
  473. /* If it's already an existing directory, do nothing. */
  474. if (!statr && S_ISDIR(stab.st_mode)) {
  475. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  476. existingdir = true;
  477. } else if (!statr && S_ISLNK(stab.st_mode)) {
  478. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  479. existingdir = true;
  480. }
  481. break;
  482. case tar_filetype_dir:
  483. /* If it's already an existing directory, do nothing. */
  484. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  485. debug(dbg_eachfiledetail, "tarobject directory exists");
  486. existingdir = true;
  487. }
  488. break;
  489. case tar_filetype_file:
  490. case tar_filetype_chardev:
  491. case tar_filetype_blockdev:
  492. case tar_filetype_fifo:
  493. case tar_filetype_hardlink:
  494. break;
  495. default:
  496. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),
  497. ti->name, ti->type);
  498. }
  499. keepexisting = false;
  500. if (!existingdir) {
  501. struct filepackages_iterator *iter;
  502. iter = filepackages_iter_new(nifd->namenode);
  503. while ((otherpkg = filepackages_iter_next(iter))) {
  504. if (otherpkg == tc->pkg)
  505. continue;
  506. debug(dbg_eachfile, "tarobject ... found in %s",
  507. pkg_name(otherpkg, pnaw_nonambig));
  508. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  509. /* Right, so we may be diverting this file. This makes the conflict
  510. * OK iff one of us is the diverting package (we don't need to
  511. * check for both being the diverting package, obviously). */
  512. divpkgset = nifd->namenode->divert->pkgset;
  513. debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
  514. divpkgset ? divpkgset->name : "<none>");
  515. if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
  516. continue;
  517. }
  518. /* If the new object is a directory and the previous object does
  519. * not exist assume it's also a directory and skip further checks.
  520. * XXX: Ideally with more information about the installed files we
  521. * could perform more clever checks. */
  522. if (statr != 0 && ti->type == tar_filetype_dir) {
  523. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  524. continue;
  525. }
  526. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  527. switch (otherpkg->clientdata->replacingfilesandsaid) {
  528. case 2:
  529. keepexisting = true;
  530. case 1:
  531. continue;
  532. }
  533. /* Is the package with the conflicting file in the “config files only”
  534. * state? If so it must be a config file and we can silenty take it
  535. * over. */
  536. if (otherpkg->status == stat_configfiles)
  537. continue;
  538. /* Perhaps we're removing a conflicting package? */
  539. if (otherpkg->clientdata->istobe == itb_remove)
  540. continue;
  541. /* Is the file an obsolete conffile in the other package
  542. * and a conffile in the new package? */
  543. if ((nifd->namenode->flags & fnnf_new_conff) &&
  544. !statr && S_ISREG(stab.st_mode)) {
  545. for (conff = otherpkg->installed.conffiles;
  546. conff;
  547. conff = conff->next) {
  548. if (!conff->obsolete)
  549. continue;
  550. if (stat(conff->name, &stabtmp)) {
  551. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  552. continue;
  553. else
  554. ohshite(_("cannot stat file '%s'"), conff->name);
  555. }
  556. if (stabtmp.st_dev == stab.st_dev &&
  557. stabtmp.st_ino == stab.st_ino)
  558. break;
  559. }
  560. if (conff) {
  561. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  562. /* process_archive() will have copied its hash already. */
  563. continue;
  564. }
  565. }
  566. if (does_replace(tc->pkg, &tc->pkg->available,
  567. otherpkg, &otherpkg->installed)) {
  568. printf(_("Replacing files in old package %s ...\n"),
  569. pkg_name(otherpkg, pnaw_nonambig));
  570. otherpkg->clientdata->replacingfilesandsaid = 1;
  571. } else if (does_replace(otherpkg, &otherpkg->installed,
  572. tc->pkg, &tc->pkg->available)) {
  573. printf(_("Replaced by files in installed package %s ...\n"),
  574. pkg_name(otherpkg, pnaw_nonambig));
  575. otherpkg->clientdata->replacingfilesandsaid = 2;
  576. nifd->namenode->flags &= ~fnnf_new_inarchive;
  577. keepexisting = true;
  578. } else {
  579. /* At this point we are replacing something without a Replaces. */
  580. if (!statr && S_ISDIR(stab.st_mode)) {
  581. forcibleerr(fc_overwritedir,
  582. _("trying to overwrite directory '%.250s' "
  583. "in package %.250s %.250s with nondirectory"),
  584. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  585. versiondescribe(&otherpkg->installed.version,
  586. vdew_nonambig));
  587. } else {
  588. forcibleerr(fc_overwrite,
  589. _("trying to overwrite '%.250s', "
  590. "which is also in package %.250s %.250s"),
  591. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  592. versiondescribe(&otherpkg->installed.version,
  593. vdew_nonambig));
  594. }
  595. }
  596. }
  597. filepackages_iter_free(iter);
  598. }
  599. if (keepexisting) {
  600. remove_file_from_list(tc, ti, oldnifd, nifd);
  601. tarfile_skip_one_forward(tc, ti);
  602. return 0;
  603. }
  604. if (filter_should_skip(ti)) {
  605. nifd->namenode->flags &= ~fnnf_new_inarchive;
  606. nifd->namenode->flags |= fnnf_filtered;
  607. tarfile_skip_one_forward(tc, ti);
  608. return 0;
  609. }
  610. if (existingdir)
  611. return 0;
  612. /* Now, at this stage we want to make sure neither of .dpkg-new and
  613. * .dpkg-tmp are hanging around. */
  614. ensure_pathname_nonexisting(fnamenewvb.buf);
  615. ensure_pathname_nonexisting(fnametmpvb.buf);
  616. /* Now we start to do things that we need to be able to undo
  617. * if something goes wrong. Watch out for the CLEANUP comments to
  618. * keep an eye on what's installed on the disk at each point. */
  619. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, (void *)nifd);
  620. /*
  621. * CLEANUP: Now we either have the old file on the disk, or not, in
  622. * its original filename.
  623. */
  624. /* Extract whatever it is as .dpkg-new ... */
  625. switch (ti->type) {
  626. case tar_filetype_file:
  627. /* We create the file with mode 0 to make sure nobody can do anything with
  628. * it until we apply the proper mode, which might be a statoverride. */
  629. fd= open(fnamenewvb.buf, (O_CREAT|O_EXCL|O_WRONLY), 0);
  630. if (fd < 0)
  631. ohshite(_("unable to create `%.255s' (while processing `%.255s')"),
  632. fnamenewvb.buf, ti->name);
  633. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  634. debug(dbg_eachfiledetail, "tarobject file open size=%jd",
  635. (intmax_t)ti->size);
  636. { char fnamebuf[256];
  637. fd_fd_copy(tc->backendpipe, fd, ti->size,
  638. _("backend dpkg-deb during `%.255s'"),
  639. path_quote_filename(fnamebuf, ti->name, 256));
  640. }
  641. r = ti->size % TARBLKSZ;
  642. if (r > 0)
  643. if (fd_read(tc->backendpipe, databuf, TARBLKSZ - r) < 0)
  644. ohshite(_("error reading from dpkg-deb pipe"));
  645. fd_writeback_init(fd);
  646. if (nifd->namenode->statoverride)
  647. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  648. nifd->namenode->statoverride->uid,
  649. nifd->namenode->statoverride->gid,
  650. nifd->namenode->statoverride->mode);
  651. if (fchown(fd, st->uid, st->gid))
  652. ohshite(_("error setting ownership of `%.255s'"), ti->name);
  653. if (fchmod(fd, st->mode & ~S_IFMT))
  654. ohshite(_("error setting permissions of `%.255s'"), ti->name);
  655. /* Postpone the fsync, to try to avoid massive I/O degradation. */
  656. if (!fc_unsafe_io)
  657. nifd->namenode->flags |= fnnf_deferred_fsync;
  658. pop_cleanup(ehflag_normaltidy); /* fd = open(fnamenewvb.buf) */
  659. if (close(fd))
  660. ohshite(_("error closing/writing `%.255s'"), ti->name);
  661. break;
  662. case tar_filetype_fifo:
  663. if (mkfifo(fnamenewvb.buf,0))
  664. ohshite(_("error creating pipe `%.255s'"), ti->name);
  665. debug(dbg_eachfiledetail, "tarobject fifo");
  666. break;
  667. case tar_filetype_chardev:
  668. if (mknod(fnamenewvb.buf, S_IFCHR, ti->dev))
  669. ohshite(_("error creating device `%.255s'"), ti->name);
  670. debug(dbg_eachfiledetail, "tarobject chardev");
  671. break;
  672. case tar_filetype_blockdev:
  673. if (mknod(fnamenewvb.buf, S_IFBLK, ti->dev))
  674. ohshite(_("error creating device `%.255s'"), ti->name);
  675. debug(dbg_eachfiledetail, "tarobject blockdev");
  676. break;
  677. case tar_filetype_hardlink:
  678. varbuf_reset(&hardlinkfn);
  679. varbuf_add_str(&hardlinkfn, instdir);
  680. varbuf_add_char(&hardlinkfn, '/');
  681. linknode = findnamenode(ti->linkname, 0);
  682. varbuf_add_str(&hardlinkfn,
  683. namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
  684. if (linknode->flags & (fnnf_deferred_rename|fnnf_new_conff))
  685. varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
  686. varbuf_end_str(&hardlinkfn);
  687. if (link(hardlinkfn.buf,fnamenewvb.buf))
  688. ohshite(_("error creating hard link `%.255s'"), ti->name);
  689. debug(dbg_eachfiledetail, "tarobject hardlink");
  690. break;
  691. case tar_filetype_symlink:
  692. /* We've already checked for an existing directory. */
  693. if (symlink(ti->linkname, fnamenewvb.buf))
  694. ohshite(_("error creating symbolic link `%.255s'"), ti->name);
  695. debug(dbg_eachfiledetail, "tarobject symlink creating");
  696. break;
  697. case tar_filetype_dir:
  698. /* We've already checked for an existing directory. */
  699. if (mkdir(fnamenewvb.buf,0))
  700. ohshite(_("error creating directory `%.255s'"), ti->name);
  701. debug(dbg_eachfiledetail, "tarobject directory creating");
  702. break;
  703. default:
  704. internerr("unknown tar type '%d', but already checked", ti->type);
  705. }
  706. tarobject_set_perms(ti, fnamenewvb.buf, st);
  707. tarobject_set_mtime(ti, fnamenewvb.buf);
  708. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, st->mode);
  709. /*
  710. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  711. * if the file already exists as a directory and we were trying to
  712. * extract a directory or symlink, we returned earlier, so we don't
  713. * need to worry about that here).
  714. *
  715. * The old file is still in the original filename,
  716. */
  717. /* First, check to see if it's a conffile. If so we don't install
  718. * it now - we leave it in .dpkg-new for --configure to take care of. */
  719. if (nifd->namenode->flags & fnnf_new_conff) {
  720. debug(dbg_conffdetail,"tarobject conffile extracted");
  721. nifd->namenode->flags |= fnnf_elide_other_lists;
  722. return 0;
  723. }
  724. /* Now we move the old file out of the way, the backup file will
  725. * be deleted later. */
  726. if (statr) {
  727. /* Don't try to back it up if it didn't exist. */
  728. debug(dbg_eachfiledetail,"tarobject new - no backup");
  729. } else {
  730. if (ti->type == tar_filetype_dir || S_ISDIR(stab.st_mode)) {
  731. /* One of the two is a directory - can't do atomic install. */
  732. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  733. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  734. if (rename(fnamevb.buf,fnametmpvb.buf))
  735. ohshite(_("unable to move aside `%.255s' to install new version"),
  736. ti->name);
  737. } else if (S_ISLNK(stab.st_mode)) {
  738. /* We can't make a symlink with two hardlinks, so we'll have to
  739. * copy it. (Pretend that making a copy of a symlink is the same
  740. * as linking to it.) */
  741. varbuf_reset(&symlinkfn);
  742. varbuf_grow(&symlinkfn, stab.st_size + 1);
  743. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  744. if (r < 0)
  745. ohshite(_("unable to read link `%.255s'"), ti->name);
  746. else if (r != stab.st_size)
  747. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  748. fnamevb.buf, stab.st_size, r);
  749. varbuf_trunc(&symlinkfn, r);
  750. varbuf_end_str(&symlinkfn);
  751. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  752. ohshite(_("unable to make backup symlink for `%.255s'"), ti->name);
  753. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  754. ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name);
  755. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  756. } else {
  757. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  758. if (link(fnamevb.buf,fnametmpvb.buf))
  759. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  760. ti->name);
  761. }
  762. }
  763. /*
  764. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  765. * in .dpkg-new.
  766. */
  767. if (ti->type == tar_filetype_file || ti->type == tar_filetype_hardlink ||
  768. ti->type == tar_filetype_symlink) {
  769. nifd->namenode->flags |= fnnf_deferred_rename;
  770. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  771. } else {
  772. if (rename(fnamenewvb.buf, fnamevb.buf))
  773. ohshite(_("unable to install new version of `%.255s'"), ti->name);
  774. /*
  775. * CLEANUP: Now the new file is in the destination file, and the
  776. * old file is in .dpkg-tmp to be cleaned up later. We now need
  777. * to take a different attitude to cleanup, because we need to
  778. * remove the new file.
  779. */
  780. nifd->namenode->flags |= fnnf_placed_on_disk;
  781. nifd->namenode->flags |= fnnf_elide_other_lists;
  782. debug(dbg_eachfiledetail, "tarobject done and installed");
  783. }
  784. return 0;
  785. }
  786. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  787. static void
  788. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  789. {
  790. struct fileinlist *cfile;
  791. for (cfile = files; cfile; cfile = cfile->next) {
  792. struct filenamenode *usenode;
  793. const char *usename;
  794. int fd;
  795. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  796. continue;
  797. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  798. usename = usenode->name + 1; /* Skip the leading '/'. */
  799. setupfnamevbs(usename);
  800. fd = open(fnamenewvb.buf, O_WRONLY);
  801. if (fd < 0)
  802. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  803. /* Ignore the return code as it should be considered equivalent to an
  804. * asynchronous hint for the kernel, we are doing an fsync() later on
  805. * anyway. */
  806. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  807. if (close(fd))
  808. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  809. }
  810. }
  811. #else
  812. static void
  813. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  814. {
  815. }
  816. #endif
  817. void
  818. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  819. {
  820. struct fileinlist *cfile;
  821. struct filenamenode *usenode;
  822. const char *usename;
  823. tar_writeback_barrier(files, pkg);
  824. for (cfile = files; cfile; cfile = cfile->next) {
  825. debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
  826. if (!(cfile->namenode->flags & fnnf_deferred_rename))
  827. continue;
  828. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  829. usename = usenode->name + 1; /* Skip the leading '/'. */
  830. setupfnamevbs(usename);
  831. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  832. int fd;
  833. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  834. fd = open(fnamenewvb.buf, O_WRONLY);
  835. if (fd < 0)
  836. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  837. if (fsync(fd))
  838. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  839. if (close(fd))
  840. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  841. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  842. }
  843. debug(dbg_eachfiledetail, "deferred extract needs rename");
  844. if (rename(fnamenewvb.buf, fnamevb.buf))
  845. ohshite(_("unable to install new version of `%.255s'"),
  846. cfile->namenode->name);
  847. cfile->namenode->flags &= ~fnnf_deferred_rename;
  848. /*
  849. * CLEANUP: Now the new file is in the destination file, and the
  850. * old file is in .dpkg-tmp to be cleaned up later. We now need
  851. * to take a different attitude to cleanup, because we need to
  852. * remove the new file.
  853. */
  854. cfile->namenode->flags |= fnnf_placed_on_disk;
  855. cfile->namenode->flags |= fnnf_elide_other_lists;
  856. debug(dbg_eachfiledetail, "deferred extract done and installed");
  857. }
  858. }
  859. void
  860. enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
  861. {
  862. struct pkg_deconf_list *newdeconf;
  863. ensure_package_clientdata(pkg);
  864. pkg->clientdata->istobe = itb_deconfigure;
  865. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  866. newdeconf->next = deconfigure;
  867. newdeconf->pkg = pkg;
  868. newdeconf->pkg_removal = pkg_removal;
  869. deconfigure = newdeconf;
  870. }
  871. /**
  872. * Try if we can deconfigure the package and queue it if so.
  873. *
  874. * Also checks whether the pdep is forced, first, according to force_p.
  875. * force_p may be NULL in which case nothing is considered forced.
  876. *
  877. * Action is a string describing the action which causes the
  878. * deconfiguration:
  879. *
  880. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  881. * "installation of <package>" (due to Breaks; removal == NULL)
  882. *
  883. * @retval 0 Not possible (why is printed).
  884. * @retval 1 Deconfiguration queued ok (no message printed).
  885. * @retval 2 Forced (no deconfiguration needed, why is printed).
  886. */
  887. static int
  888. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  889. struct deppossi *pdep, const char *action,
  890. struct pkginfo *removal, const char *why)
  891. {
  892. if (force_p && force_p(pdep)) {
  893. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  894. return 2;
  895. } else if (f_autodeconf) {
  896. if (pkg->installed.essential) {
  897. if (fc_removeessential) {
  898. warning(_("considering deconfiguration of essential\n"
  899. " package %s, to enable %s."),
  900. pkg_name(pkg, pnaw_nonambig), action);
  901. } else {
  902. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  903. " it in order to enable %s.\n"),
  904. pkg_name(pkg, pnaw_nonambig), action);
  905. return 0;
  906. }
  907. }
  908. enqueue_deconfigure(pkg, removal);
  909. return 1;
  910. } else {
  911. fprintf(stderr, _("dpkg: no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  912. action, why);
  913. return 0;
  914. }
  915. }
  916. static int try_remove_can(struct deppossi *pdep,
  917. struct pkginfo *fixbyrm,
  918. const char *why) {
  919. char action[512];
  920. sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
  921. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  922. action, fixbyrm, why);
  923. }
  924. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  925. const char *pfilename) {
  926. struct pkginfo *fixbydeconf;
  927. struct varbuf why = VARBUF_INIT;
  928. int ok;
  929. fixbydeconf = NULL;
  930. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  931. varbuf_destroy(&why);
  932. return;
  933. }
  934. varbuf_end_str(&why);
  935. if (fixbydeconf && f_autodeconf) {
  936. char action[512];
  937. ensure_package_clientdata(fixbydeconf);
  938. assert(fixbydeconf->clientdata->istobe == itb_normal);
  939. sprintf(action, _("installation of %.250s"),
  940. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  941. fprintf(stderr, _("dpkg: considering deconfiguration of %s,"
  942. " which would be broken by %s ...\n"),
  943. pkg_name(fixbydeconf, pnaw_nonambig), action);
  944. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  945. action, NULL, why.buf);
  946. if (ok == 1) {
  947. fprintf(stderr, _("dpkg: yes, will deconfigure %s (broken by %s).\n"),
  948. pkg_name(fixbydeconf, pnaw_nonambig),
  949. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  950. }
  951. } else {
  952. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  953. pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  954. why.buf);
  955. ok= 0;
  956. }
  957. varbuf_destroy(&why);
  958. if (ok > 0) return;
  959. if (force_breaks(dep->list)) {
  960. warning(_("ignoring breakage, may proceed anyway!"));
  961. return;
  962. }
  963. if (fixbydeconf && !f_autodeconf) {
  964. ohshit(_("installing %.250s would break %.250s, and\n"
  965. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  966. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  967. pkg_name(fixbydeconf, pnaw_nonambig));
  968. } else {
  969. ohshit(_("installing %.250s would break existing software"),
  970. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  971. }
  972. }
  973. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  974. const char *pfilename) {
  975. struct pkginfo *fixbyrm;
  976. struct deppossi *pdep, flagdeppossi;
  977. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  978. struct dependency *providecheck;
  979. fixbyrm = NULL;
  980. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  981. varbuf_destroy(&conflictwhy);
  982. varbuf_destroy(&removalwhy);
  983. return;
  984. }
  985. if (fixbyrm) {
  986. ensure_package_clientdata(fixbyrm);
  987. if (fixbyrm->clientdata->istobe == itb_installnew) {
  988. fixbyrm= dep->up;
  989. ensure_package_clientdata(fixbyrm);
  990. }
  991. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  992. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  993. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  994. (!fixbyrm->installed.essential || fc_removeessential)))) {
  995. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  996. fixbyrm->clientdata->istobe= itb_remove;
  997. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  998. pkg_name(fixbyrm, pnaw_nonambig),
  999. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1000. if (!(fixbyrm->status == stat_installed ||
  1001. fixbyrm->status == stat_triggerspending ||
  1002. fixbyrm->status == stat_triggersawaited)) {
  1003. fprintf(stderr,
  1004. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  1005. pkg_name(fixbyrm, pnaw_nonambig));
  1006. pdep = NULL;
  1007. } else {
  1008. for (pdep = fixbyrm->set->depended.installed;
  1009. pdep;
  1010. pdep = pdep->rev_next) {
  1011. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1012. continue;
  1013. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1014. continue;
  1015. varbuf_end_str(&removalwhy);
  1016. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1017. break;
  1018. }
  1019. if (!pdep) {
  1020. /* If we haven't found a reason not to yet, let's look some more. */
  1021. for (providecheck= fixbyrm->installed.depends;
  1022. providecheck;
  1023. providecheck= providecheck->next) {
  1024. if (providecheck->type != dep_provides) continue;
  1025. for (pdep = providecheck->list->ed->depended.installed;
  1026. pdep;
  1027. pdep = pdep->rev_next) {
  1028. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1029. continue;
  1030. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1031. continue;
  1032. varbuf_end_str(&removalwhy);
  1033. fprintf(stderr, _("dpkg"
  1034. ": may have trouble removing %s, as it provides %s ...\n"),
  1035. pkg_name(fixbyrm, pnaw_nonambig),
  1036. providecheck->list->ed->name);
  1037. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1038. goto break_from_both_loops_at_once;
  1039. }
  1040. }
  1041. break_from_both_loops_at_once:;
  1042. }
  1043. }
  1044. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1045. pdep= &flagdeppossi;
  1046. }
  1047. if (!pdep && (fixbyrm->eflag & eflag_reinstreq)) {
  1048. if (fc_removereinstreq) {
  1049. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  1050. " remove anyway as you requested.\n"),
  1051. pkg_name(fixbyrm, pnaw_nonambig));
  1052. } else {
  1053. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  1054. "will not remove.\n"), pkg_name(fixbyrm, pnaw_nonambig));
  1055. pdep= &flagdeppossi;
  1056. }
  1057. }
  1058. if (!pdep) {
  1059. /* This conflict is OK - we'll remove the conflictor. */
  1060. enqueue_conflictor(pkg, fixbyrm);
  1061. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1062. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  1063. pkg_name(fixbyrm, pnaw_nonambig),
  1064. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1065. return;
  1066. }
  1067. /* Put it back. */
  1068. fixbyrm->clientdata->istobe = itb_normal;
  1069. }
  1070. }
  1071. varbuf_end_str(&conflictwhy);
  1072. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  1073. pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1074. conflictwhy.buf);
  1075. if (!force_conflicts(dep->list))
  1076. ohshit(_("conflicting packages - not installing %.250s"),
  1077. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1078. warning(_("ignoring conflict, may proceed anyway!"));
  1079. varbuf_destroy(&conflictwhy);
  1080. return;
  1081. }
  1082. void cu_cidir(int argc, void **argv) {
  1083. char *cidir= (char*)argv[0];
  1084. char *cidirrest= (char*)argv[1];
  1085. cidirrest[-1] = '\0';
  1086. ensure_pathname_nonexisting(cidir);
  1087. }
  1088. void cu_fileslist(int argc, void **argv) {
  1089. destroyobstack();
  1090. }
  1091. int
  1092. archivefiles(const char *const *argv)
  1093. {
  1094. const char *volatile thisarg;
  1095. const char *const *volatile argp;
  1096. jmp_buf ejbuf;
  1097. trigproc_install_hooks();
  1098. modstatdb_open(f_noact ? msdbrw_readonly :
  1099. (cipaction->arg_int == act_avail ? msdbrw_readonly :
  1100. fc_nonroot ? msdbrw_write :
  1101. msdbrw_needsuperuser) |
  1102. msdbrw_available_write);
  1103. checkpath();
  1104. log_message("startup archives %s", cipaction->olong);
  1105. if (f_recursive) {
  1106. int pi[2], nfiles, c, i, r;
  1107. pid_t pid;
  1108. FILE *pf;
  1109. static struct varbuf findoutput;
  1110. const char **arglist;
  1111. char *p;
  1112. if (!*argv)
  1113. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1114. m_pipe(pi);
  1115. pid = subproc_fork();
  1116. if (pid == 0) {
  1117. struct command cmd;
  1118. const char *const *ap;
  1119. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1120. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1121. command_add_args(&cmd, FIND, "-L", NULL);
  1122. for (ap = argv; *ap; ap++) {
  1123. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1124. char *a;
  1125. m_asprintf(&a, "./%s", *ap);
  1126. command_add_arg(&cmd, a);
  1127. } else {
  1128. command_add_arg(&cmd, (const char *)*ap);
  1129. }
  1130. }
  1131. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1132. command_exec(&cmd);
  1133. }
  1134. close(pi[1]);
  1135. nfiles= 0;
  1136. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1137. varbuf_reset(&findoutput);
  1138. while ((c= fgetc(pf)) != EOF) {
  1139. varbuf_add_char(&findoutput, c);
  1140. if (!c) nfiles++;
  1141. }
  1142. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1143. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1144. r = subproc_wait_check(pid, "find", PROCNOERR);
  1145. if (r != 0)
  1146. ohshit(_("find for --recursive returned unhandled error %i"),r);
  1147. if (!nfiles)
  1148. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1149. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1150. p = findoutput.buf;
  1151. for (i = 0; i < nfiles; i++) {
  1152. arglist[i] = p;
  1153. while (*p++ != '\0') ;
  1154. }
  1155. arglist[i] = NULL;
  1156. argp= arglist;
  1157. } else {
  1158. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1159. cipaction->olong);
  1160. argp= argv;
  1161. }
  1162. currenttime = time(NULL);
  1163. /* Initialize fname variables contents. */
  1164. varbuf_reset(&fnamevb);
  1165. varbuf_reset(&fnametmpvb);
  1166. varbuf_reset(&fnamenewvb);
  1167. varbuf_add_str(&fnamevb, instdir);
  1168. varbuf_add_char(&fnamevb, '/');
  1169. varbuf_add_str(&fnametmpvb, instdir);
  1170. varbuf_add_char(&fnametmpvb, '/');
  1171. varbuf_add_str(&fnamenewvb, instdir);
  1172. varbuf_add_char(&fnamenewvb, '/');
  1173. fnameidlu= fnamevb.used;
  1174. ensure_diversions();
  1175. ensure_statoverrides();
  1176. while ((thisarg = *argp++) != NULL) {
  1177. if (setjmp(ejbuf)) {
  1178. pop_error_context(ehflag_bombout);
  1179. if (abort_processing)
  1180. break;
  1181. continue;
  1182. }
  1183. push_error_context_jump(&ejbuf, print_error_perpackage, thisarg);
  1184. process_archive(thisarg);
  1185. onerr_abort++;
  1186. m_output(stdout, _("<standard output>"));
  1187. m_output(stderr, _("<standard error>"));
  1188. onerr_abort--;
  1189. pop_error_context(ehflag_normaltidy);
  1190. }
  1191. switch (cipaction->arg_int) {
  1192. case act_install:
  1193. case act_configure:
  1194. case act_triggers:
  1195. case act_remove:
  1196. case act_purge:
  1197. process_queue();
  1198. case act_unpack:
  1199. case act_avail:
  1200. break;
  1201. default:
  1202. internerr("unknown action '%d'", cipaction->arg_int);
  1203. }
  1204. trigproc_run_deferred();
  1205. modstatdb_shutdown();
  1206. return 0;
  1207. }
  1208. /**
  1209. * Decide whether we want to install a new version of the package.
  1210. *
  1211. * @param pkg The package with the version we might want to install
  1212. *
  1213. * @retval true If the package should be skipped.
  1214. * @retval false If the package should be installed.
  1215. */
  1216. bool
  1217. wanttoinstall(struct pkginfo *pkg)
  1218. {
  1219. int r;
  1220. if (pkg->want != want_install && pkg->want != want_hold) {
  1221. if (f_alsoselect) {
  1222. printf(_("Selecting previously unselected package %s.\n"),
  1223. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1224. pkg_set_want(pkg, want_install);
  1225. return true;
  1226. } else {
  1227. printf(_("Skipping unselected package %s.\n"),
  1228. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1229. return false;
  1230. }
  1231. }
  1232. if (pkg->eflag & eflag_reinstreq)
  1233. return true;
  1234. if (pkg->status < stat_unpacked)
  1235. return true;
  1236. r = versioncompare(&pkg->available.version, &pkg->installed.version);
  1237. if (r > 0) {
  1238. return true;
  1239. } else if (r == 0) {
  1240. /* Same version fully installed. */
  1241. if (f_skipsame) {
  1242. fprintf(stderr, _("Version %.250s of %.250s already installed, "
  1243. "skipping.\n"),
  1244. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1245. pkg_name(pkg, pnaw_nonambig));
  1246. return false;
  1247. } else {
  1248. return true;
  1249. }
  1250. } else {
  1251. if (fc_downgrade) {
  1252. warning(_("downgrading %.250s from %.250s to %.250s."),
  1253. pkg_name(pkg, pnaw_nonambig),
  1254. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1255. versiondescribe(&pkg->available.version, vdew_nonambig));
  1256. return true;
  1257. } else {
  1258. fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  1259. "to %.250s, skipping.\n"),
  1260. pkg_name(pkg, pnaw_nonambig),
  1261. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1262. versiondescribe(&pkg->available.version, vdew_nonambig));
  1263. return false;
  1264. }
  1265. }
  1266. }
  1267. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1268. struct filenamenode *namenode) {
  1269. struct fileinlist *newconff;
  1270. newconff= m_malloc(sizeof(struct fileinlist));
  1271. newconff->next = NULL;
  1272. newconff->namenode= namenode;
  1273. **newconffileslastp_io= newconff;
  1274. *newconffileslastp_io= &newconff->next;
  1275. return newconff;
  1276. }
  1277. /* vi: ts=8 sw=2
  1278. */