processarc.c 48 KB

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