unpack.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  1. /*
  2. * dpkg - main program for package management
  3. * unpack.c - the huge function process_archive
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006-2014 Guillem Jover <guillem@debian.org>
  7. * Copyright © 2011 Linaro Limited
  8. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  9. *
  10. * This is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. */
  23. #include <config.h>
  24. #include <compat.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/wait.h>
  28. #include <assert.h>
  29. #include <errno.h>
  30. #include <ctype.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <utime.h>
  34. #include <fcntl.h>
  35. #include <dirent.h>
  36. #include <unistd.h>
  37. #include <stdint.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <dpkg/i18n.h>
  41. #include <dpkg/dpkg.h>
  42. #include <dpkg/dpkg-db.h>
  43. #include <dpkg/pkg.h>
  44. #include <dpkg/pkg-queue.h>
  45. #include <dpkg/path.h>
  46. #include <dpkg/buffer.h>
  47. #include <dpkg/subproc.h>
  48. #include <dpkg/dir.h>
  49. #include <dpkg/tarfn.h>
  50. #include <dpkg/options.h>
  51. #include <dpkg/triglib.h>
  52. #include "filesdb.h"
  53. #include "file-match.h"
  54. #include "infodb.h"
  55. #include "main.h"
  56. #include "archives.h"
  57. static const char *
  58. summarize_filename(const char *filename)
  59. {
  60. const char *pfilename;
  61. char *pfilenamebuf;
  62. for (pfilename = filename;
  63. pfilename && strlen(pfilename) > 30 && strchr(pfilename, '/') != NULL;
  64. pfilename++)
  65. pfilename = strchr(pfilename, '/');
  66. if (pfilename && pfilename != filename) {
  67. pfilenamebuf = nfmalloc(strlen(pfilename) + 5);
  68. sprintf(pfilenamebuf, _(".../%s"), pfilename);
  69. pfilename = pfilenamebuf;
  70. } else {
  71. pfilename = filename;
  72. }
  73. return pfilename;
  74. }
  75. static bool
  76. deb_reassemble(const char **filename, const char **pfilename)
  77. {
  78. static char *reasmbuf = NULL;
  79. struct stat stab;
  80. int status;
  81. pid_t pid;
  82. if (!reasmbuf)
  83. reasmbuf = dpkg_db_get_path(REASSEMBLETMP);
  84. if (unlink(reasmbuf) && errno != ENOENT)
  85. ohshite(_("error ensuring `%.250s' doesn't exist"), reasmbuf);
  86. push_cleanup(cu_pathname, ~0, NULL, 0, 1, (void *)reasmbuf);
  87. pid = subproc_fork();
  88. if (!pid) {
  89. execlp(SPLITTER, SPLITTER, "-Qao", reasmbuf, *filename, NULL);
  90. ohshite(_("unable to execute %s (%s)"),
  91. _("split package reassembly"), SPLITTER);
  92. }
  93. status = subproc_wait(pid, SPLITTER);
  94. switch (WIFEXITED(status) ? WEXITSTATUS(status) : -1) {
  95. case 0:
  96. /* It was a part - is it complete? */
  97. if (!stat(reasmbuf, &stab)) {
  98. /* Yes. */
  99. *filename = reasmbuf;
  100. *pfilename = _("reassembled package file");
  101. break;
  102. } else if (errno == ENOENT) {
  103. /* No. That's it, we skip it. */
  104. return false;
  105. }
  106. case 1:
  107. /* No, it wasn't a part. */
  108. break;
  109. default:
  110. subproc_check(status, SPLITTER, 0);
  111. }
  112. return true;
  113. }
  114. static void
  115. deb_verify(const char *filename)
  116. {
  117. struct stat stab;
  118. pid_t pid;
  119. if (stat(DEBSIGVERIFY, &stab) < 0)
  120. return;
  121. printf(_("Authenticating %s ...\n"), filename);
  122. fflush(stdout);
  123. pid = subproc_fork();
  124. if (!pid) {
  125. execl(DEBSIGVERIFY, DEBSIGVERIFY, "-q", filename, NULL);
  126. ohshite(_("unable to execute %s (%s)"),
  127. _("package signature verification"), DEBSIGVERIFY);
  128. } else {
  129. int status;
  130. status = subproc_wait(pid, "debsig-verify");
  131. if (!(WIFEXITED(status) && WEXITSTATUS(status) == 0)) {
  132. if (!fc_badverify)
  133. ohshit(_("verification on package %s failed!"), filename);
  134. else
  135. notice(_("verification on package %s failed; "
  136. "but installing anyway as you requested"), filename);
  137. } else {
  138. printf(_("passed\n"));
  139. }
  140. }
  141. }
  142. static char *
  143. get_control_dir(char *cidir)
  144. {
  145. if (f_noact) {
  146. char *tmpdir;
  147. tmpdir = mkdtemp(path_make_temp_template("dpkg"));
  148. if (tmpdir == NULL)
  149. ohshite(_("unable to create temporary directory"));
  150. cidir = m_realloc(cidir, strlen(tmpdir) + MAXCONTROLFILENAME + 10);
  151. strcpy(cidir, tmpdir);
  152. free(tmpdir);
  153. } else {
  154. const char *admindir;
  155. admindir = dpkg_db_get_dir();
  156. /* The admindir length is always constant on a dpkg execution run. */
  157. if (cidir == NULL)
  158. cidir = m_malloc(strlen(admindir) + sizeof(CONTROLDIRTMP) +
  159. MAXCONTROLFILENAME + 10);
  160. /* We want it to be on the same filesystem so that we can
  161. * use rename(2) to install the postinst &c. */
  162. strcpy(cidir, admindir);
  163. strcat(cidir, "/" CONTROLDIRTMP);
  164. /* Make sure the control information directory is empty. */
  165. ensure_pathname_nonexisting(cidir);
  166. }
  167. strcat(cidir, "/");
  168. return cidir;
  169. }
  170. static struct pkg_queue conflictors = PKG_QUEUE_INIT;
  171. void
  172. enqueue_conflictor(struct pkginfo *pkg)
  173. {
  174. pkg_queue_push(&conflictors, pkg);
  175. }
  176. static void
  177. pkg_infodb_remove_file(const char *filename, const char *filetype)
  178. {
  179. if (unlink(filename))
  180. ohshite(_("unable to delete control info file `%.250s'"), filename);
  181. debug(dbg_scripts, "removal_bulk info unlinked %s", filename);
  182. }
  183. static struct match_node *match_head = NULL;
  184. static void
  185. pkg_infodb_update_file(const char *filename, const char *filetype)
  186. {
  187. if (strlen(filetype) > MAXCONTROLFILENAME)
  188. ohshit(_("old version of package has overly-long info file name starting `%.250s'"),
  189. filename);
  190. /* We do the list separately. */
  191. if (strcmp(filetype, LISTFILE) == 0)
  192. return;
  193. /* We keep files to rename in a list as doing the rename immediately
  194. * might influence the current readdir(), the just renamed file might
  195. * be returned a second time as it's actually a new file from the
  196. * point of view of the filesystem. */
  197. match_head = match_node_new(filename, filetype, match_head);
  198. }
  199. static void
  200. pkg_infodb_update(struct pkginfo *pkg, char *cidir, char *cidirrest)
  201. {
  202. struct match_node *match_node;
  203. DIR *dsd;
  204. struct dirent *de;
  205. /* Deallocate the match list in case we aborted previously. */
  206. while ((match_node = match_head)) {
  207. match_head = match_node->next;
  208. match_node_free(match_node);
  209. }
  210. pkg_infodb_foreach(pkg, &pkg->available, pkg_infodb_update_file);
  211. while ((match_node = match_head)) {
  212. strcpy(cidirrest, match_node->filetype);
  213. if (!rename(cidir, match_node->filename)) {
  214. debug(dbg_scripts, "process_archive info installed %s as %s",
  215. cidir, match_node->filename);
  216. } else if (errno == ENOENT) {
  217. /* Right, no new version. */
  218. if (unlink(match_node->filename))
  219. ohshite(_("unable to remove obsolete info file `%.250s'"),
  220. match_node->filename);
  221. debug(dbg_scripts, "process_archive info unlinked %s",
  222. match_node->filename);
  223. } else {
  224. ohshite(_("unable to install (supposed) new info file `%.250s'"), cidir);
  225. }
  226. match_head = match_node->next;
  227. match_node_free(match_node);
  228. }
  229. /* The control directory itself. */
  230. cidirrest[0] = '\0';
  231. dsd = opendir(cidir);
  232. if (!dsd)
  233. ohshite(_("unable to open temp control directory"));
  234. push_cleanup(cu_closedir, ~0, NULL, 0, 1, (void *)dsd);
  235. while ((de = readdir(dsd))) {
  236. const char *newinfofilename;
  237. if (strchr(de->d_name, '.')) {
  238. debug(dbg_scripts, "process_archive tmp.ci script/file '%s' contains dot",
  239. de->d_name);
  240. continue;
  241. }
  242. if (strlen(de->d_name) > MAXCONTROLFILENAME)
  243. ohshit(_("package contains overly-long control info file name (starting `%.50s')"),
  244. de->d_name);
  245. strcpy(cidirrest, de->d_name);
  246. /* First we check it's not a directory. */
  247. if (rmdir(cidir) == 0)
  248. ohshit(_("package control info contained directory `%.250s'"), cidir);
  249. else if (errno != ENOTDIR)
  250. ohshite(_("package control info rmdir of `%.250s' didn't say not a dir"),
  251. de->d_name);
  252. /* Ignore the control file. */
  253. if (strcmp(de->d_name, CONTROLFILE) == 0) {
  254. debug(dbg_scripts, "process_archive tmp.ci script/file '%s' is control",
  255. cidir);
  256. continue;
  257. }
  258. if (strcmp(de->d_name, LISTFILE) == 0) {
  259. warning(_("package %s contained list as info file"),
  260. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  261. continue;
  262. }
  263. /* Right, install it */
  264. newinfofilename = pkg_infodb_get_file(pkg, &pkg->available, de->d_name);
  265. if (rename(cidir, newinfofilename))
  266. ohshite(_("unable to install new info file `%.250s' as `%.250s'"),
  267. cidir, newinfofilename);
  268. debug(dbg_scripts,
  269. "process_archive tmp.ci script/file '%s' installed as '%s'",
  270. cidir, newinfofilename);
  271. }
  272. pop_cleanup(ehflag_normaltidy); /* closedir */
  273. /* If the old and new versions use a different infodb layout, get rid
  274. * of the files using the old layout. */
  275. if (pkg->installed.multiarch != pkg->available.multiarch &&
  276. (pkg->installed.multiarch == PKG_MULTIARCH_SAME ||
  277. pkg->available.multiarch == PKG_MULTIARCH_SAME)) {
  278. debug(dbg_scripts,
  279. "process_archive remove old info files after db layout switch");
  280. pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
  281. }
  282. dir_sync_path(pkg_infodb_get_dir());
  283. }
  284. static void
  285. pkg_disappear(struct pkginfo *pkg, struct pkginfo *infavour)
  286. {
  287. printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
  288. pkg_name(pkg, pnaw_nonambig));
  289. log_action("disappear", pkg, &pkg->installed);
  290. debug(dbg_general, "pkg_disappear disappearing %s",
  291. pkg_name(pkg, pnaw_always));
  292. trig_activate_packageprocessing(pkg);
  293. maintscript_installed(pkg, POSTRMFILE,
  294. "post-removal script (for disappearance)",
  295. "disappear",
  296. pkgbin_name(infavour, &infavour->available,
  297. pnaw_nonambig),
  298. versiondescribe(&infavour->available.version,
  299. vdew_nonambig),
  300. NULL);
  301. /* OK, now we delete all the stuff in the ‘info’ directory ... */
  302. debug(dbg_general, "pkg_disappear cleaning info directory");
  303. pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
  304. dir_sync_path(pkg_infodb_get_dir());
  305. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  306. pkg_set_want(pkg, PKG_WANT_UNKNOWN);
  307. pkg_reset_eflags(pkg);
  308. dpkg_version_blank(&pkg->configversion);
  309. pkgbin_blank(&pkg->installed);
  310. pkg->clientdata->fileslistvalid = false;
  311. modstatdb_note(pkg);
  312. }
  313. /**
  314. * Check if all instances of a pkgset are getting in sync.
  315. *
  316. * If that's the case, the extraction is going to ensure consistency
  317. * of shared files.
  318. */
  319. static bool
  320. pkgset_getting_in_sync(struct pkginfo *pkg)
  321. {
  322. struct pkginfo *otherpkg;
  323. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  324. if (otherpkg == pkg)
  325. continue;
  326. if (otherpkg->status <= PKG_STAT_CONFIGFILES)
  327. continue;
  328. if (dpkg_version_compare(&pkg->available.version,
  329. &otherpkg->installed.version)) {
  330. return false;
  331. }
  332. }
  333. return true;
  334. }
  335. void process_archive(const char *filename) {
  336. static const struct tar_operations tf = {
  337. .read = tarfileread,
  338. .extract_file = tarobject,
  339. .link = tarobject,
  340. .symlink = tarobject,
  341. .mkdir = tarobject,
  342. .mknod = tarobject,
  343. };
  344. /* These need to be static so that we can pass their addresses to
  345. * push_cleanup as arguments to the cu_xxx routines; if an error occurs
  346. * we unwind the stack before processing the cleanup list, and these
  347. * variables had better still exist ... */
  348. static int p1[2];
  349. static char *cidir = NULL;
  350. static struct fileinlist *newconffiles, *newfileslist;
  351. static enum pkgstatus oldversionstatus;
  352. static struct varbuf depprobwhy;
  353. static struct tarcontext tc;
  354. struct dpkg_error err;
  355. enum parsedbflags parsedb_flags;
  356. int rc;
  357. pid_t pid;
  358. struct pkgiterator *it;
  359. struct pkginfo *pkg, *otherpkg;
  360. struct pkg_list *conflictor_iter;
  361. char *cidirrest, *p;
  362. char conffilenamebuf[MAXCONFFILENAME];
  363. char *psize;
  364. const char *pfilename;
  365. struct fileinlist *newconff, **newconffileslastp;
  366. struct fileinlist *cfile;
  367. struct reversefilelistiter rlistit;
  368. struct conffile *searchconff, **iconffileslastp, *newiconff;
  369. struct dependency *dsearch, *newdeplist, **newdeplistlastp;
  370. struct dependency *newdep, *dep, *providecheck;
  371. struct deppossi *psearch, **newpossilastp, *possi, *newpossi, *pdep;
  372. FILE *conff;
  373. struct filenamenode *namenode;
  374. struct stat stab, oldfs;
  375. struct pkg_deconf_list *deconpil;
  376. struct pkginfo *fixbytrigaw;
  377. cleanup_pkg_failed= cleanup_conflictor_failed= 0;
  378. pfilename = summarize_filename(filename);
  379. if (stat(filename,&stab)) ohshite(_("cannot access archive"));
  380. /* We can't ‘tentatively-reassemble’ packages. */
  381. if (!f_noact) {
  382. if (!deb_reassemble(&filename, &pfilename))
  383. return;
  384. }
  385. /* Verify the package. */
  386. if (!f_nodebsig)
  387. deb_verify(filename);
  388. /* Get the control information directory. */
  389. cidir = get_control_dir(cidir);
  390. cidirrest = cidir + strlen(cidir);
  391. push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest);
  392. pid = subproc_fork();
  393. if (pid == 0) {
  394. cidirrest[-1] = '\0';
  395. execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL);
  396. ohshite(_("unable to execute %s (%s)"),
  397. _("package control information extraction"), BACKEND);
  398. }
  399. subproc_wait_check(pid, BACKEND " --control", 0);
  400. /* We want to guarantee the extracted files are on the disk, so that the
  401. * subsequent renames to the info database do not end up with old or zero
  402. * length files in case of a system crash. As neither dpkg-deb nor tar do
  403. * explicit fsync()s, we have to do them here.
  404. * XXX: This could be avoided by switching to an internal tar extractor. */
  405. dir_sync_contents(cidir);
  406. strcpy(cidirrest,CONTROLFILE);
  407. if (cipaction->arg_int == act_avail)
  408. parsedb_flags = pdb_parse_available;
  409. else
  410. parsedb_flags = pdb_parse_binary;
  411. parsedb_flags |= pdb_ignorefiles;
  412. if (fc_badversion)
  413. parsedb_flags |= pdb_lax_version_parser;
  414. parsedb(cidir, parsedb_flags, &pkg);
  415. if (!pkg->files) {
  416. pkg->files= nfmalloc(sizeof(struct filedetails));
  417. pkg->files->next = NULL;
  418. pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL;
  419. }
  420. /* Always nfmalloc. Otherwise, we may overwrite some other field (like
  421. * md5sum). */
  422. psize = nfmalloc(30);
  423. sprintf(psize, "%jd", (intmax_t)stab.st_size);
  424. pkg->files->size = psize;
  425. if (cipaction->arg_int == act_avail) {
  426. printf(_("Recorded info about %s from %s.\n"),
  427. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename);
  428. pop_cleanup(ehflag_normaltidy);
  429. return;
  430. }
  431. if (pkg->available.arch->type != DPKG_ARCH_ALL &&
  432. pkg->available.arch->type != DPKG_ARCH_NATIVE &&
  433. pkg->available.arch->type != DPKG_ARCH_FOREIGN)
  434. forcibleerr(fc_architecture,
  435. _("package architecture (%s) does not match system (%s)"),
  436. pkg->available.arch->name,
  437. dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
  438. clear_deconfigure_queue();
  439. clear_istobes();
  440. if (wanttoinstall(pkg)) {
  441. pkg_set_want(pkg, PKG_WANT_INSTALL);
  442. } else {
  443. pop_cleanup(ehflag_normaltidy);
  444. return;
  445. }
  446. /* Deconfigure other instances from a pkgset if they are not in sync. */
  447. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  448. if (otherpkg == pkg)
  449. continue;
  450. if (otherpkg->status <= PKG_STAT_HALFCONFIGURED)
  451. continue;
  452. if (dpkg_version_compare(&pkg->available.version,
  453. &otherpkg->installed.version))
  454. enqueue_deconfigure(otherpkg, NULL);
  455. }
  456. /* Check if anything is installed that we conflict with, or not installed
  457. * that we need. */
  458. pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
  459. for (dsearch= pkg->available.depends; dsearch; dsearch= dsearch->next) {
  460. switch (dsearch->type) {
  461. case dep_conflicts:
  462. /* Look for things we conflict with. */
  463. check_conflict(dsearch, pkg, pfilename);
  464. break;
  465. case dep_breaks:
  466. /* Look for things we break. */
  467. check_breaks(dsearch, pkg, pfilename);
  468. break;
  469. case dep_provides:
  470. /* Look for things that conflict with what we provide. */
  471. for (psearch = dsearch->list->ed->depended.installed;
  472. psearch;
  473. psearch = psearch->rev_next) {
  474. if (psearch->up->type != dep_conflicts)
  475. continue;
  476. check_conflict(psearch->up, pkg, pfilename);
  477. }
  478. break;
  479. case dep_suggests:
  480. case dep_recommends:
  481. case dep_depends:
  482. case dep_replaces:
  483. case dep_enhances:
  484. /* Ignore these here. */
  485. break;
  486. case dep_predepends:
  487. if (!depisok(dsearch, &depprobwhy, NULL, &fixbytrigaw, true)) {
  488. if (fixbytrigaw) {
  489. while (fixbytrigaw->trigaw.head)
  490. trigproc(fixbytrigaw->trigaw.head->pend);
  491. } else {
  492. varbuf_end_str(&depprobwhy);
  493. notice(_("regarding %s containing %s, pre-dependency problem:\n%s"),
  494. pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  495. depprobwhy.buf);
  496. if (!force_depends(dsearch->list))
  497. ohshit(_("pre-dependency problem - not installing %.250s"),
  498. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  499. warning(_("ignoring pre-dependency problem!"));
  500. }
  501. }
  502. }
  503. }
  504. /* Look for things that conflict with us. */
  505. for (psearch = pkg->set->depended.installed; psearch; psearch = psearch->rev_next) {
  506. if (psearch->up->type != dep_conflicts) continue;
  507. check_conflict(psearch->up, pkg, pfilename);
  508. }
  509. ensure_allinstfiles_available();
  510. filesdbinit();
  511. trig_file_interests_ensure();
  512. printf(_("Preparing to unpack %s ...\n"), pfilename);
  513. if (pkg->status != PKG_STAT_NOTINSTALLED &&
  514. pkg->status != PKG_STAT_CONFIGFILES) {
  515. log_action("upgrade", pkg, &pkg->installed);
  516. } else {
  517. log_action("install", pkg, &pkg->available);
  518. }
  519. if (f_noact) {
  520. pop_cleanup(ehflag_normaltidy);
  521. return;
  522. }
  523. /*
  524. * OK, we're going ahead.
  525. */
  526. trig_activate_packageprocessing(pkg);
  527. strcpy(cidirrest, TRIGGERSCIFILE);
  528. trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg, &pkg->available);
  529. /* Read the conffiles, and copy the hashes across. */
  530. newconffiles = NULL;
  531. newconffileslastp = &newconffiles;
  532. push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
  533. strcpy(cidirrest,CONFFILESFILE);
  534. conff= fopen(cidir,"r");
  535. if (conff) {
  536. push_cleanup(cu_closestream, ehflag_bombout, NULL, 0, 1, (void *)conff);
  537. while (fgets(conffilenamebuf,MAXCONFFILENAME-2,conff)) {
  538. struct filepackages_iterator *iter;
  539. p= conffilenamebuf + strlen(conffilenamebuf);
  540. assert(p != conffilenamebuf);
  541. if (p[-1] != '\n')
  542. ohshit(_("conffile name '%s' is too long, or missing final newline"),
  543. conffilenamebuf);
  544. while (p > conffilenamebuf && isspace(p[-1])) --p;
  545. if (p == conffilenamebuf) continue;
  546. *p = '\0';
  547. namenode= findnamenode(conffilenamebuf, 0);
  548. namenode->oldhash= NEWCONFFILEFLAG;
  549. newconff= newconff_append(&newconffileslastp, namenode);
  550. /* Let's see if any packages have this file. If they do we
  551. * check to see if they listed it as a conffile, and if they did
  552. * we copy the hash across. Since (for plain file conffiles,
  553. * which is the only kind we are supposed to have) there will
  554. * only be one package which ‘has’ the file, this will usually
  555. * mean we only look in the package which we're installing now.
  556. * The ‘conffiles’ data in the status file is ignored when a
  557. * package isn't also listed in the file ownership database as
  558. * having that file. If several packages are listed as owning
  559. * the file we pick one at random. */
  560. searchconff = NULL;
  561. iter = filepackages_iter_new(newconff->namenode);
  562. while ((otherpkg = filepackages_iter_next(iter))) {
  563. debug(dbg_conffdetail,
  564. "process_archive conffile '%s' in package %s - conff ?",
  565. newconff->namenode->name, pkg_name(otherpkg, pnaw_always));
  566. for (searchconff = otherpkg->installed.conffiles;
  567. searchconff && strcmp(newconff->namenode->name, searchconff->name);
  568. searchconff = searchconff->next)
  569. debug(dbg_conffdetail,
  570. "process_archive conffile '%s' in package %s - conff ? not '%s'",
  571. newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
  572. searchconff->name);
  573. if (searchconff) {
  574. debug(dbg_conff,
  575. "process_archive conffile '%s' package=%s %s hash=%s",
  576. newconff->namenode->name, pkg_name(otherpkg, pnaw_always),
  577. otherpkg == pkg ? "same" : "different!",
  578. searchconff->hash);
  579. if (otherpkg == pkg)
  580. break;
  581. }
  582. }
  583. filepackages_iter_free(iter);
  584. if (searchconff) {
  585. newconff->namenode->oldhash= searchconff->hash;
  586. /* We don't copy ‘obsolete’; it's not obsolete in the new package. */
  587. } else {
  588. debug(dbg_conff, "process_archive conffile '%s' no package, no hash",
  589. newconff->namenode->name);
  590. }
  591. newconff->namenode->flags |= fnnf_new_conff;
  592. }
  593. if (ferror(conff)) ohshite(_("read error in %.250s"),cidir);
  594. pop_cleanup(ehflag_normaltidy); /* conff = fopen() */
  595. if (fclose(conff)) ohshite(_("error closing %.250s"),cidir);
  596. } else {
  597. if (errno != ENOENT) ohshite(_("error trying to open %.250s"),cidir);
  598. }
  599. /* All the old conffiles are marked with a flag, so that we don't delete
  600. * them if they seem to disappear completely. */
  601. oldconffsetflags(pkg->installed.conffiles);
  602. for (conflictor_iter = conflictors.head;
  603. conflictor_iter;
  604. conflictor_iter = conflictor_iter->next)
  605. oldconffsetflags(conflictor_iter->pkg->installed.conffiles);
  606. oldversionstatus= pkg->status;
  607. assert(oldversionstatus <= PKG_STAT_INSTALLED);
  608. debug(dbg_general,"process_archive oldversionstatus=%s",
  609. statusstrings[oldversionstatus]);
  610. if (oldversionstatus == PKG_STAT_HALFCONFIGURED ||
  611. oldversionstatus == PKG_STAT_TRIGGERSAWAITED ||
  612. oldversionstatus == PKG_STAT_TRIGGERSPENDING ||
  613. oldversionstatus == PKG_STAT_INSTALLED) {
  614. pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
  615. pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
  616. modstatdb_note(pkg);
  617. push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
  618. if (dpkg_version_compare(&pkg->available.version,
  619. &pkg->installed.version) >= 0)
  620. /* Upgrade or reinstall. */
  621. maintscript_fallback(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
  622. "upgrade", "failed-upgrade");
  623. else /* Downgrade => no fallback */
  624. maintscript_installed(pkg, PRERMFILE, "pre-removal",
  625. "upgrade",
  626. versiondescribe(&pkg->available.version,
  627. vdew_nonambig),
  628. NULL);
  629. pkg_set_status(pkg, PKG_STAT_UNPACKED);
  630. oldversionstatus = PKG_STAT_UNPACKED;
  631. modstatdb_note(pkg);
  632. }
  633. for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) {
  634. struct pkginfo *removing = deconpil->pkg_removal;
  635. if (removing)
  636. printf(_("De-configuring %s (%s), to allow removal of %s (%s) ...\n"),
  637. pkg_name(deconpil->pkg, pnaw_nonambig),
  638. versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig),
  639. pkg_name(removing, pnaw_nonambig),
  640. versiondescribe(&removing->installed.version, vdew_nonambig));
  641. else
  642. printf(_("De-configuring %s (%s) ...\n"),
  643. pkg_name(deconpil->pkg, pnaw_nonambig),
  644. versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig));
  645. trig_activate_packageprocessing(deconpil->pkg);
  646. pkg_set_status(deconpil->pkg, PKG_STAT_HALFCONFIGURED);
  647. modstatdb_note(deconpil->pkg);
  648. /* This means that we *either* go and run postinst abort-deconfigure,
  649. * *or* queue the package for later configure processing, depending
  650. * on which error cleanup route gets taken. */
  651. push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy,
  652. ok_prermdeconfigure, ehflag_normaltidy,
  653. 3, (void*)deconpil->pkg, (void*)removing, (void*)pkg);
  654. if (removing) {
  655. maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  656. "deconfigure", "in-favour",
  657. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  658. versiondescribe(&pkg->available.version,
  659. vdew_nonambig),
  660. "removing",
  661. pkg_name(removing, pnaw_nonambig),
  662. versiondescribe(&removing->installed.version,
  663. vdew_nonambig),
  664. NULL);
  665. } else {
  666. maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  667. "deconfigure", "in-favour",
  668. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  669. versiondescribe(&pkg->available.version,
  670. vdew_nonambig),
  671. NULL);
  672. }
  673. }
  674. for (conflictor_iter = conflictors.head;
  675. conflictor_iter;
  676. conflictor_iter = conflictor_iter->next) {
  677. struct pkginfo *conflictor = conflictor_iter->pkg;
  678. if (!(conflictor->status == PKG_STAT_HALFCONFIGURED ||
  679. conflictor->status == PKG_STAT_TRIGGERSAWAITED ||
  680. conflictor->status == PKG_STAT_TRIGGERSPENDING ||
  681. conflictor->status == PKG_STAT_INSTALLED))
  682. continue;
  683. trig_activate_packageprocessing(conflictor);
  684. pkg_set_status(conflictor, PKG_STAT_HALFCONFIGURED);
  685. modstatdb_note(conflictor);
  686. push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0,
  687. 2, conflictor, pkg);
  688. maintscript_installed(conflictor, PRERMFILE, "pre-removal",
  689. "remove", "in-favour",
  690. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  691. versiondescribe(&pkg->available.version,
  692. vdew_nonambig),
  693. NULL);
  694. pkg_set_status(conflictor, PKG_STAT_HALFINSTALLED);
  695. modstatdb_note(conflictor);
  696. }
  697. pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
  698. if (pkg->status == PKG_STAT_NOTINSTALLED) {
  699. pkg->installed.version= pkg->available.version;
  700. pkg->installed.multiarch = pkg->available.multiarch;
  701. }
  702. pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
  703. modstatdb_note(pkg);
  704. if (oldversionstatus == PKG_STAT_NOTINSTALLED) {
  705. push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0,
  706. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  707. maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  708. "install", NULL);
  709. } else if (oldversionstatus == PKG_STAT_CONFIGFILES) {
  710. push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0,
  711. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  712. maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  713. "install",
  714. versiondescribe(&pkg->installed.version, vdew_nonambig),
  715. NULL);
  716. } else {
  717. push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0,
  718. 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
  719. maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  720. "upgrade",
  721. versiondescribe(&pkg->installed.version, vdew_nonambig),
  722. NULL);
  723. }
  724. if (oldversionstatus == PKG_STAT_NOTINSTALLED ||
  725. oldversionstatus == PKG_STAT_CONFIGFILES) {
  726. printf(_("Unpacking %s (%s) ...\n"),
  727. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  728. versiondescribe(&pkg->available.version, vdew_nonambig));
  729. } else {
  730. printf(_("Unpacking %s (%s) over (%s) ...\n"),
  731. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  732. versiondescribe(&pkg->available.version, vdew_nonambig),
  733. versiondescribe(&pkg->installed.version, vdew_nonambig));
  734. }
  735. /*
  736. * Now we unpack the archive, backing things up as we go.
  737. * For each file, we check to see if it already exists.
  738. * There are several possibilities:
  739. *
  740. * + We are trying to install a non-directory ...
  741. * - It doesn't exist. In this case we simply extract it.
  742. * - It is a plain file, device, symlink, &c. We do an ‘atomic
  743. * overwrite’ using link() and rename(), but leave a backup copy.
  744. * Later, when we delete the backup, we remove it from any other
  745. * packages' lists.
  746. * - It is a directory. In this case it depends on whether we're
  747. * trying to install a symlink or something else.
  748. * = If we're not trying to install a symlink we move the directory
  749. * aside and extract the node. Later, when we recursively remove
  750. * the backed-up directory, we remove it from any other packages'
  751. * lists.
  752. * = If we are trying to install a symlink we do nothing - ie,
  753. * dpkg will never replace a directory tree with a symlink. This
  754. * is to avoid embarrassing effects such as replacing a directory
  755. * tree with a link to a link to the original directory tree.
  756. * + We are trying to install a directory ...
  757. * - It doesn't exist. We create it with the appropriate modes.
  758. * - It exists as a directory or a symlink to one. We do nothing.
  759. * - It is a plain file or a symlink (other than to a directory).
  760. * We move it aside and create the directory. Later, when we
  761. * delete the backup, we remove it from any other packages' lists.
  762. *
  763. * Install non-dir Install symlink Install dir
  764. * Exists not X X X
  765. * File/node/symlink LXR LXR BXR
  766. * Directory BXR - -
  767. *
  768. * X: extract file/node/link/directory
  769. * LX: atomic overwrite leaving backup
  770. * B: ordinary backup
  771. * R: later remove from other packages' lists
  772. * -: do nothing
  773. *
  774. * After we've done this we go through the remaining things in the
  775. * lists of packages we're trying to remove (including the old
  776. * version of the current package). This happens in reverse order,
  777. * so that we process files before the directories (or symlinks-to-
  778. * directories) containing them.
  779. *
  780. * + If the thing is a conffile then we leave it alone for the purge
  781. * operation.
  782. * + Otherwise, there are several possibilities too:
  783. * - The listed thing does not exist. We ignore it.
  784. * - The listed thing is a directory or a symlink to a directory.
  785. * We delete it only if it isn't listed in any other package.
  786. * - The listed thing is not a directory, but was part of the package
  787. * that was upgraded, we check to make sure the files aren't the
  788. * same ones from the old package by checking dev/inode
  789. * - The listed thing is not a directory or a symlink to one (ie,
  790. * it's a plain file, device, pipe, &c, or a symlink to one, or a
  791. * dangling symlink). We delete it.
  792. *
  793. * The removed packages' list becomes empty (of course, the new
  794. * version of the package we're installing will have a new list,
  795. * which replaces the old version's list).
  796. *
  797. * If at any stage we remove a file from a package's list, and the
  798. * package isn't one we're already processing, and the package's
  799. * list becomes empty as a result, we ‘vanish’ the package. This
  800. * means that we run its postrm with the ‘disappear’ argument, and
  801. * put the package in the ‘not-installed’ state. If it had any
  802. * conffiles, their hashes and ownership will have been transferred
  803. * already, so we just ignore those and forget about them from the
  804. * point of view of the disappearing package.
  805. *
  806. * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
  807. * files get replaced ‘as we go’.
  808. */
  809. m_pipe(p1);
  810. push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]);
  811. pid = subproc_fork();
  812. if (pid == 0) {
  813. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  814. execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL);
  815. ohshite(_("unable to execute %s (%s)"),
  816. _("package filesystem archive extraction"), BACKEND);
  817. }
  818. close(p1[1]);
  819. p1[1] = -1;
  820. newfileslist = NULL;
  821. tc.newfilesp = &newfileslist;
  822. push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
  823. tc.pkg= pkg;
  824. tc.backendpipe= p1[0];
  825. tc.pkgset_getting_in_sync = pkgset_getting_in_sync(pkg);
  826. rc = tar_extractor(&tc, &tf);
  827. if (rc) {
  828. if (errno) {
  829. ohshite(_("error reading dpkg-deb tar output"));
  830. } else {
  831. ohshit(_("corrupted filesystem tarfile - corrupted package archive"));
  832. }
  833. }
  834. if (fd_skip(p1[0], -1, &err) < 0)
  835. ohshit(_("cannot zap possible trailing zeros from dpkg-deb: %s"), err.str);
  836. close(p1[0]);
  837. p1[0] = -1;
  838. subproc_wait_check(pid, BACKEND " --fsys-tarfile", PROCPIPE);
  839. tar_deferred_extract(newfileslist, pkg);
  840. if (oldversionstatus == PKG_STAT_HALFINSTALLED ||
  841. oldversionstatus == PKG_STAT_UNPACKED) {
  842. /* Packages that were in ‘installed’ and ‘postinstfailed’ have been
  843. * reduced to ‘unpacked’ by now, by the running of the prerm script. */
  844. pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
  845. modstatdb_note(pkg);
  846. push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
  847. maintscript_fallback(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  848. "upgrade", "failed-upgrade");
  849. }
  850. /* If anything goes wrong while tidying up it's a bit late to do
  851. * anything about it. However, we don't install the new status
  852. * info yet, so that a future dpkg installation will put everything
  853. * right (we hope).
  854. *
  855. * If something does go wrong later the ‘conflictor’ package will be
  856. * left in the ‘removal_failed’ state. Removing or installing it
  857. * will be impossible if it was required because of the conflict with
  858. * the package we're installing now and (presumably) the dependency
  859. * by other packages. This means that the files it contains in
  860. * common with this package will hang around until we successfully
  861. * get this package installed, after which point we can trust the
  862. * conflicting package's file list, which will have been updated to
  863. * remove any files in this package. */
  864. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  865. /* Now we delete all the files that were in the old version of
  866. * the package only, except (old or new) conffiles, which we leave
  867. * alone. */
  868. reversefilelist_init(&rlistit,pkg->clientdata->files);
  869. while ((namenode= reversefilelist_next(&rlistit))) {
  870. struct filenamenode *usenode;
  871. if ((namenode->flags & fnnf_new_conff) ||
  872. (namenode->flags & fnnf_new_inarchive))
  873. continue;
  874. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  875. varbuf_trunc(&fnamevb, fnameidlu);
  876. varbuf_add_str(&fnamevb, usenode->name);
  877. varbuf_end_str(&fnamevb);
  878. if (!stat(namenode->name,&stab) && S_ISDIR(stab.st_mode)) {
  879. debug(dbg_eachfiledetail, "process_archive: %s is a directory",
  880. namenode->name);
  881. if (dir_is_used_by_others(namenode, pkg))
  882. continue;
  883. }
  884. if (lstat(fnamevb.buf, &oldfs)) {
  885. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  886. warning(_("could not stat old file '%.250s' so not deleting it: %s"),
  887. fnamevb.buf, strerror(errno));
  888. continue;
  889. }
  890. if (S_ISDIR(oldfs.st_mode)) {
  891. trig_path_activate(usenode, pkg);
  892. if (rmdir(fnamevb.buf)) {
  893. warning(_("unable to delete old directory '%.250s': %s"),
  894. namenode->name, strerror(errno));
  895. } else if ((namenode->flags & fnnf_old_conff)) {
  896. warning(_("old conffile '%.250s' was an empty directory "
  897. "(and has now been deleted)"), namenode->name);
  898. }
  899. } else {
  900. struct fileinlist *sameas = NULL;
  901. static struct stat empty_stat;
  902. struct varbuf cfilename = VARBUF_INIT;
  903. /*
  904. * Ok, it's an old file, but is it really not in the new package?
  905. * It might be known by a different name because of symlinks.
  906. *
  907. * We need to check to make sure, so we stat the file, then compare
  908. * it to the new list. If we find a dev/inode match, we assume they
  909. * are the same file, and leave it alone. NOTE: we don't check in
  910. * other packages for sanity reasons (we don't want to stat _all_
  911. * the files on the system).
  912. *
  913. * We run down the list of _new_ files in this package. This keeps
  914. * the process a little leaner. We are only worried about new ones
  915. * since ones that stayed the same don't really apply here.
  916. */
  917. /* If we can't stat the old or new file, or it's a directory,
  918. * we leave it up to the normal code. */
  919. debug(dbg_eachfile, "process_archive: checking %s for same files on "
  920. "upgrade/downgrade", fnamevb.buf);
  921. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  922. /* If the file has been filtered then treat it as if it didn't exist
  923. * on the file system. */
  924. if (cfile->namenode->flags & fnnf_filtered)
  925. continue;
  926. if (!cfile->namenode->filestat) {
  927. struct stat tmp_stat;
  928. varbuf_reset(&cfilename);
  929. varbuf_add_str(&cfilename, instdir);
  930. varbuf_add_char(&cfilename, '/');
  931. varbuf_add_str(&cfilename, cfile->namenode->name);
  932. varbuf_end_str(&cfilename);
  933. if (lstat(cfilename.buf, &tmp_stat) == 0) {
  934. cfile->namenode->filestat = nfmalloc(sizeof(struct stat));
  935. memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat));
  936. } else {
  937. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  938. ohshite(_("unable to stat other new file `%.250s'"),
  939. cfile->namenode->name);
  940. cfile->namenode->filestat = &empty_stat;
  941. continue;
  942. }
  943. }
  944. if (cfile->namenode->filestat == &empty_stat)
  945. continue;
  946. if (oldfs.st_dev == cfile->namenode->filestat->st_dev &&
  947. oldfs.st_ino == cfile->namenode->filestat->st_ino) {
  948. if (sameas)
  949. warning(_("old file '%.250s' is the same as several new files! "
  950. "(both '%.250s' and '%.250s')"), fnamevb.buf,
  951. sameas->namenode->name, cfile->namenode->name);
  952. sameas= cfile;
  953. debug(dbg_eachfile, "process_archive: not removing %s,"
  954. " since it matches %s", fnamevb.buf, cfile->namenode->name);
  955. }
  956. }
  957. varbuf_destroy(&cfilename);
  958. if ((namenode->flags & fnnf_old_conff)) {
  959. if (sameas) {
  960. if (sameas->namenode->flags & fnnf_new_conff) {
  961. if (strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG) == 0) {
  962. sameas->namenode->oldhash= namenode->oldhash;
  963. debug(dbg_eachfile, "process_archive: old conff %s"
  964. " is same as new conff %s, copying hash",
  965. namenode->name, sameas->namenode->name);
  966. } else {
  967. debug(dbg_eachfile, "process_archive: old conff %s"
  968. " is same as new conff %s but latter already has hash",
  969. namenode->name, sameas->namenode->name);
  970. }
  971. }
  972. } else {
  973. debug(dbg_eachfile, "process_archive: old conff %s"
  974. " is disappearing", namenode->name);
  975. namenode->flags |= fnnf_obs_conff;
  976. newconff_append(&newconffileslastp, namenode);
  977. addfiletolist(&tc, namenode);
  978. }
  979. continue;
  980. }
  981. if (sameas)
  982. continue;
  983. trig_path_activate(usenode, pkg);
  984. if (secure_unlink_statted(fnamevb.buf, &oldfs)) {
  985. warning(_("unable to securely remove old file '%.250s': %s"),
  986. namenode->name, strerror(errno));
  987. }
  988. } /* !S_ISDIR */
  989. }
  990. /* OK, now we can write the updated files-in-this package list,
  991. * since we've done away (hopefully) with all the old junk. */
  992. write_filelist_except(pkg, &pkg->available, newfileslist, 0);
  993. /* Trigger interests may have changed.
  994. * Firstly we go through the old list of interests deleting them.
  995. * Then we go through the new list adding them. */
  996. strcpy(cidirrest, TRIGGERSCIFILE);
  997. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  998. trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
  999. trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg, &pkg->available);
  1000. trig_file_interests_save();
  1001. /* We also install the new maintainer scripts, and any other
  1002. * cruft that may have come along with the package. First
  1003. * we go through the existing scripts replacing or removing
  1004. * them as appropriate; then we go through the new scripts
  1005. * (any that are left) and install them. */
  1006. debug(dbg_general, "process_archive updating info directory");
  1007. pkg_infodb_update(pkg, cidir, cidirrest);
  1008. /* We store now the checksums dynamically computed while unpacking. */
  1009. write_filehash_except(pkg, &pkg->available, newfileslist, 0);
  1010. /*
  1011. * Update the status database.
  1012. *
  1013. * This involves copying each field across from the ‘available’
  1014. * to the ‘installed’ half of the pkg structure.
  1015. * For some of the fields we have to do a complicated construction
  1016. * operation; for others we can just copy the value.
  1017. * We tackle the fields in the order they appear, so that
  1018. * we don't miss any out :-).
  1019. * At least we don't have to copy any strings that are referred
  1020. * to, because these are never modified and never freed.
  1021. */
  1022. /* The dependencies are the most difficult. We have to build
  1023. * a whole new forward dependency tree. At least the reverse
  1024. * links (linking our deppossi's into the reverse chains)
  1025. * can be done by copy_dependency_links. */
  1026. newdeplist = NULL;
  1027. newdeplistlastp = &newdeplist;
  1028. for (dep= pkg->available.depends; dep; dep= dep->next) {
  1029. newdep= nfmalloc(sizeof(struct dependency));
  1030. newdep->up= pkg;
  1031. newdep->next = NULL;
  1032. newdep->list = NULL;
  1033. newpossilastp = &newdep->list;
  1034. for (possi= dep->list; possi; possi= possi->next) {
  1035. newpossi= nfmalloc(sizeof(struct deppossi));
  1036. newpossi->up= newdep;
  1037. newpossi->ed= possi->ed;
  1038. newpossi->next = NULL;
  1039. newpossi->rev_next = newpossi->rev_prev = NULL;
  1040. newpossi->arch_is_implicit = possi->arch_is_implicit;
  1041. newpossi->arch = possi->arch;
  1042. newpossi->verrel= possi->verrel;
  1043. if (possi->verrel != DPKG_RELATION_NONE)
  1044. newpossi->version= possi->version;
  1045. else
  1046. dpkg_version_blank(&newpossi->version);
  1047. newpossi->cyclebreak = false;
  1048. *newpossilastp= newpossi;
  1049. newpossilastp= &newpossi->next;
  1050. }
  1051. newdep->type= dep->type;
  1052. *newdeplistlastp= newdep;
  1053. newdeplistlastp= &newdep->next;
  1054. }
  1055. /* Right, now we've replicated the forward tree, we
  1056. * get copy_dependency_links to remove all the old dependency
  1057. * structures from the reverse links and add the new dependency
  1058. * structures in instead. It also copies the new dependency
  1059. * structure pointer for this package into the right field. */
  1060. copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0);
  1061. /* We copy the text fields. */
  1062. pkg->installed.essential= pkg->available.essential;
  1063. pkg->installed.multiarch = pkg->available.multiarch;
  1064. pkg->installed.description= pkg->available.description;
  1065. pkg->installed.maintainer= pkg->available.maintainer;
  1066. pkg->installed.source= pkg->available.source;
  1067. pkg->installed.arch = pkg->available.arch;
  1068. pkg->installed.installedsize= pkg->available.installedsize;
  1069. pkg->installed.version= pkg->available.version;
  1070. pkg->installed.origin = pkg->available.origin;
  1071. pkg->installed.bugs = pkg->available.bugs;
  1072. /* We have to generate our own conffiles structure. */
  1073. pkg->installed.conffiles = NULL;
  1074. iconffileslastp = &pkg->installed.conffiles;
  1075. for (cfile= newconffiles; cfile; cfile= cfile->next) {
  1076. newiconff= nfmalloc(sizeof(struct conffile));
  1077. newiconff->next = NULL;
  1078. newiconff->name= nfstrsave(cfile->namenode->name);
  1079. newiconff->hash= nfstrsave(cfile->namenode->oldhash);
  1080. newiconff->obsolete= !!(cfile->namenode->flags & fnnf_obs_conff);
  1081. *iconffileslastp= newiconff;
  1082. iconffileslastp= &newiconff->next;
  1083. }
  1084. /* We can just copy the arbitrary fields list, because it is
  1085. * never even rearranged. Phew! */
  1086. pkg->installed.arbs= pkg->available.arbs;
  1087. /* In case this was an architecture cross-grade, the in-core pkgset might
  1088. * be in an inconsistent state, with two pkginfo entries having the same
  1089. * architecture, so let's fix that. Note that this does not lose data,
  1090. * as the pkg.available member parsed from the binary should replace the
  1091. * to be cleared duplicate in the other instance. */
  1092. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  1093. if (otherpkg == pkg)
  1094. continue;
  1095. if (otherpkg->installed.arch != pkg->installed.arch)
  1096. continue;
  1097. assert(otherpkg->status == PKG_STAT_NOTINSTALLED);
  1098. pkg_blank(otherpkg);
  1099. }
  1100. /* Check for disappearing packages:
  1101. * We go through all the packages on the system looking for ones
  1102. * whose files are entirely part of the one we've just unpacked
  1103. * (and which actually *have* some files!).
  1104. *
  1105. * Any that we find are removed - we run the postrm with ‘disappear’
  1106. * as an argument, and remove their info/... files and status info.
  1107. * Conffiles are ignored (the new package had better do something
  1108. * with them!). */
  1109. it = pkg_db_iter_new();
  1110. while ((otherpkg = pkg_db_iter_next_pkg(it)) != NULL) {
  1111. ensure_package_clientdata(otherpkg);
  1112. if (otherpkg == pkg ||
  1113. otherpkg->status == PKG_STAT_NOTINSTALLED ||
  1114. otherpkg->status == PKG_STAT_CONFIGFILES ||
  1115. otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE ||
  1116. !otherpkg->clientdata->files) continue;
  1117. /* Do not try to disappear other packages from the same set
  1118. * if they are Multi-Arch: same */
  1119. if (pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  1120. otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  1121. otherpkg->set == pkg->set)
  1122. continue;
  1123. debug(dbg_veryverbose, "process_archive checking disappearance %s",
  1124. pkg_name(otherpkg, pnaw_always));
  1125. assert(otherpkg->clientdata->istobe == PKG_ISTOBE_NORMAL ||
  1126. otherpkg->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
  1127. for (cfile= otherpkg->clientdata->files;
  1128. cfile && strcmp(cfile->namenode->name, "/.") == 0;
  1129. cfile= cfile->next);
  1130. if (!cfile) {
  1131. debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
  1132. continue;
  1133. }
  1134. for (cfile= otherpkg->clientdata->files;
  1135. cfile && !filesavespackage(cfile,otherpkg,pkg);
  1136. cfile= cfile->next);
  1137. if (cfile) continue;
  1138. /* So dependency things will give right answers ... */
  1139. otherpkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
  1140. debug(dbg_veryverbose, "process_archive disappear checking dependencies");
  1141. for (pdep = otherpkg->set->depended.installed;
  1142. pdep;
  1143. pdep = pdep->rev_next) {
  1144. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  1145. pdep->up->type != dep_recommends) continue;
  1146. if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
  1147. continue;
  1148. varbuf_end_str(&depprobwhy);
  1149. debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf);
  1150. break;
  1151. }
  1152. if (!pdep) {
  1153. /* If we haven't found a reason not to yet, let's look some more. */
  1154. for (providecheck= otherpkg->installed.depends;
  1155. providecheck;
  1156. providecheck= providecheck->next) {
  1157. if (providecheck->type != dep_provides) continue;
  1158. for (pdep = providecheck->list->ed->depended.installed;
  1159. pdep;
  1160. pdep = pdep->rev_next) {
  1161. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  1162. pdep->up->type != dep_recommends)
  1163. continue;
  1164. if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
  1165. continue;
  1166. varbuf_end_str(&depprobwhy);
  1167. debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s",
  1168. providecheck->list->ed->name, depprobwhy.buf);
  1169. goto break_from_both_loops_at_once;
  1170. }
  1171. }
  1172. break_from_both_loops_at_once:;
  1173. }
  1174. otherpkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  1175. if (pdep) continue;
  1176. /* No, we're disappearing it. This is the wrong time to go and
  1177. * run maintainer scripts and things, as we can't back out. But
  1178. * what can we do ? It has to be run this late. */
  1179. pkg_disappear(otherpkg, pkg);
  1180. } /* while (otherpkg= ... */
  1181. pkg_db_iter_free(it);
  1182. /* Delete files from any other packages' lists.
  1183. * We have to do this before we claim this package is in any
  1184. * sane kind of state, as otherwise we might delete by mistake
  1185. * a file that we overwrote, when we remove the package which
  1186. * had the version we overwrote. To prevent this we make
  1187. * sure that we don't claim this package is OK until we
  1188. * have claimed ‘ownership’ of all its files. */
  1189. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  1190. struct filepackages_iterator *iter;
  1191. struct pkgset *divpkgset;
  1192. if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue;
  1193. if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
  1194. divpkgset = cfile->namenode->divert->pkgset;
  1195. if (divpkgset == pkg->set) {
  1196. debug(dbg_eachfile,
  1197. "process_archive not overwriting any '%s' (overriding, '%s')",
  1198. cfile->namenode->name, cfile->namenode->divert->useinstead->name);
  1199. continue;
  1200. } else {
  1201. debug(dbg_eachfile,
  1202. "process_archive looking for overwriting '%s' (overridden by %s)",
  1203. cfile->namenode->name, divpkgset ? divpkgset->name : "<local>");
  1204. }
  1205. } else {
  1206. divpkgset = NULL;
  1207. debug(dbg_eachfile, "process_archive looking for overwriting '%s'",
  1208. cfile->namenode->name);
  1209. }
  1210. iter = filepackages_iter_new(cfile->namenode);
  1211. while ((otherpkg = filepackages_iter_next(iter))) {
  1212. debug(dbg_eachfiledetail, "process_archive ... found in %s",
  1213. pkg_name(otherpkg, pnaw_always));
  1214. /* A pkgset can share files between instances, so there's no point
  1215. * in rewriting the file that's already in place. */
  1216. if (otherpkg->set == pkg->set)
  1217. continue;
  1218. if (otherpkg->set == divpkgset) {
  1219. debug(dbg_eachfiledetail, "process_archive ... diverted, skipping");
  1220. continue;
  1221. }
  1222. if (cfile->namenode->flags & fnnf_new_conff)
  1223. conffile_mark_obsolete(otherpkg, cfile->namenode);
  1224. /* If !fileslistvalid then it's one of the disappeared packages above
  1225. * or we have already updated the files list file, and we don't bother
  1226. * with it here, clearly. */
  1227. if (!otherpkg->clientdata->fileslistvalid)
  1228. continue;
  1229. /* Found one. We delete the list entry for this file,
  1230. * (and any others in the same package) and then mark the package
  1231. * as requiring a reread. */
  1232. write_filelist_except(otherpkg, &otherpkg->installed,
  1233. otherpkg->clientdata->files, fnnf_elide_other_lists);
  1234. ensure_package_clientdata(otherpkg);
  1235. debug(dbg_veryverbose, "process_archive overwrote from %s",
  1236. pkg_name(otherpkg, pnaw_always));
  1237. }
  1238. filepackages_iter_free(iter);
  1239. }
  1240. /* Right, the package we've unpacked is now in a reasonable state.
  1241. * The only thing that we have left to do with it is remove
  1242. * backup files, and we can leave the user to fix that if and when
  1243. * it happens (we leave the reinstall required flag, of course). */
  1244. pkg_set_status(pkg, PKG_STAT_UNPACKED);
  1245. modstatdb_note(pkg);
  1246. /* Now we delete all the backup files that we made when
  1247. * extracting the archive - except for files listed as conffiles
  1248. * in the new package.
  1249. * This time we count it as an error if something goes wrong.
  1250. *
  1251. * Note that we don't ever delete things that were in the old
  1252. * package as a conffile and don't appear at all in the new.
  1253. * They stay recorded as obsolete conffiles and will eventually
  1254. * (if not taken over by another package) be forgotten. */
  1255. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  1256. if (cfile->namenode->flags & fnnf_new_conff) continue;
  1257. varbuf_trunc(&fnametmpvb, fnameidlu);
  1258. varbuf_add_str(&fnametmpvb,
  1259. namenodetouse(cfile->namenode, pkg, &pkg->installed)->name);
  1260. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  1261. varbuf_end_str(&fnametmpvb);
  1262. ensure_pathname_nonexisting(fnametmpvb.buf);
  1263. }
  1264. /* OK, we're now fully done with the main package.
  1265. * This is quite a nice state, so we don't unwind past here. */
  1266. pkg_reset_eflags(pkg);
  1267. modstatdb_note(pkg);
  1268. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  1269. /* Only the removal of the conflictor left to do.
  1270. * The files list for the conflictor is still a little inconsistent in-core,
  1271. * as we have not yet updated the filename->packages mappings; however,
  1272. * the package->filenames mapping is. */
  1273. while (!pkg_queue_is_empty(&conflictors)) {
  1274. struct pkginfo *conflictor = pkg_queue_pop(&conflictors);
  1275. /* We need to have the most up-to-date info about which files are
  1276. * what ... */
  1277. ensure_allinstfiles_available();
  1278. removal_bulk(conflictor);
  1279. }
  1280. if (cipaction->arg_int == act_install)
  1281. enqueue_package(pkg);
  1282. }