archives.c 45 KB

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