processarc.c 52 KB

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