unpack.c 53 KB

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