unpack.c 56 KB

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