unpack.c 55 KB

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