unpack.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. /*
  2. * dpkg - main program for package management
  3. * unpack.c - the huge function process_archive
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2006-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. internerr("unexpected exit status %d from %s", status, SPLITTER);
  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 = 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_disappear(struct pkginfo *pkg, struct pkginfo *infavour)
  376. {
  377. printf(_("(Noting disappearance of %s, which has been completely replaced.)\n"),
  378. pkg_name(pkg, pnaw_nonambig));
  379. log_action("disappear", pkg, &pkg->installed);
  380. debug(dbg_general, "pkg_disappear disappearing %s",
  381. pkg_name(pkg, pnaw_always));
  382. trig_activate_packageprocessing(pkg);
  383. maintscript_installed(pkg, POSTRMFILE,
  384. "post-removal script (for disappearance)",
  385. "disappear",
  386. pkgbin_name(infavour, &infavour->available,
  387. pnaw_nonambig),
  388. versiondescribe(&infavour->available.version,
  389. vdew_nonambig),
  390. NULL);
  391. /* OK, now we delete all the stuff in the ‘info’ directory ... */
  392. debug(dbg_general, "pkg_disappear cleaning info directory");
  393. pkg_infodb_foreach(pkg, &pkg->installed, pkg_infodb_remove_file);
  394. dir_sync_path(pkg_infodb_get_dir());
  395. pkg_set_status(pkg, PKG_STAT_NOTINSTALLED);
  396. pkg_set_want(pkg, PKG_WANT_UNKNOWN);
  397. pkg_reset_eflags(pkg);
  398. dpkg_version_blank(&pkg->configversion);
  399. pkgbin_blank(&pkg->installed);
  400. pkg->clientdata->fileslistvalid = false;
  401. modstatdb_note(pkg);
  402. }
  403. /**
  404. * Check if all instances of a pkgset are getting in sync.
  405. *
  406. * If that's the case, the extraction is going to ensure consistency
  407. * of shared files.
  408. */
  409. static bool
  410. pkgset_getting_in_sync(struct pkginfo *pkg)
  411. {
  412. struct pkginfo *otherpkg;
  413. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  414. if (otherpkg == pkg)
  415. continue;
  416. if (otherpkg->status <= PKG_STAT_CONFIGFILES)
  417. continue;
  418. if (dpkg_version_compare(&pkg->available.version,
  419. &otherpkg->installed.version)) {
  420. return false;
  421. }
  422. }
  423. return true;
  424. }
  425. void process_archive(const char *filename) {
  426. static const struct tar_operations tf = {
  427. .read = tarfileread,
  428. .extract_file = tarobject,
  429. .link = tarobject,
  430. .symlink = tarobject,
  431. .mkdir = tarobject,
  432. .mknod = tarobject,
  433. };
  434. /* These need to be static so that we can pass their addresses to
  435. * push_cleanup as arguments to the cu_xxx routines; if an error occurs
  436. * we unwind the stack before processing the cleanup list, and these
  437. * variables had better still exist ... */
  438. static int p1[2];
  439. static enum pkgstatus oldversionstatus;
  440. static struct varbuf depprobwhy;
  441. static struct tarcontext tc;
  442. struct dpkg_error err;
  443. enum parsedbflags parsedb_flags;
  444. int rc;
  445. pid_t pid;
  446. struct pkgiterator *it;
  447. struct pkginfo *pkg, *otherpkg;
  448. struct pkg_list *conflictor_iter;
  449. char *cidir = NULL;
  450. char *cidirrest;
  451. char *psize;
  452. const char *pfilename;
  453. struct fileinlist *newfileslist;
  454. struct fileinlist *cfile;
  455. struct filenamenode_queue newconffiles;
  456. struct reversefilelistiter rlistit;
  457. struct conffile **iconffileslastp, *newiconff;
  458. struct dependency *dsearch, *newdeplist, **newdeplistlastp;
  459. struct dependency *newdep, *dep, *providecheck;
  460. struct deppossi *psearch, **newpossilastp, *possi, *newpossi, *pdep;
  461. struct filenamenode *namenode;
  462. struct stat stab, oldfs;
  463. struct pkg_deconf_list *deconpil;
  464. struct pkginfo *fixbytrigaw;
  465. cleanup_pkg_failed= cleanup_conflictor_failed= 0;
  466. pfilename = summarize_filename(filename);
  467. if (stat(filename,&stab)) ohshite(_("cannot access archive"));
  468. /* We can't ‘tentatively-reassemble’ packages. */
  469. if (!f_noact) {
  470. if (!deb_reassemble(&filename, &pfilename))
  471. return;
  472. }
  473. /* Verify the package. */
  474. if (!f_nodebsig)
  475. deb_verify(filename);
  476. /* Get the control information directory. */
  477. cidir = get_control_dir(cidir);
  478. cidirrest = cidir + strlen(cidir);
  479. push_cleanup(cu_cidir, ~0, NULL, 0, 2, (void *)cidir, (void *)cidirrest);
  480. pid = subproc_fork();
  481. if (pid == 0) {
  482. cidirrest[-1] = '\0';
  483. execlp(BACKEND, BACKEND, "--control", filename, cidir, NULL);
  484. ohshite(_("unable to execute %s (%s)"),
  485. _("package control information extraction"), BACKEND);
  486. }
  487. subproc_reap(pid, BACKEND " --control", 0);
  488. /* We want to guarantee the extracted files are on the disk, so that the
  489. * subsequent renames to the info database do not end up with old or zero
  490. * length files in case of a system crash. As neither dpkg-deb nor tar do
  491. * explicit fsync()s, we have to do them here.
  492. * XXX: This could be avoided by switching to an internal tar extractor. */
  493. dir_sync_contents(cidir);
  494. strcpy(cidirrest,CONTROLFILE);
  495. if (cipaction->arg_int == act_avail)
  496. parsedb_flags = pdb_parse_available;
  497. else
  498. parsedb_flags = pdb_parse_binary;
  499. parsedb_flags |= pdb_ignorefiles;
  500. if (fc_badversion)
  501. parsedb_flags |= pdb_lax_version_parser;
  502. parsedb(cidir, parsedb_flags, &pkg);
  503. if (!pkg->files) {
  504. pkg->files= nfmalloc(sizeof(struct filedetails));
  505. pkg->files->next = NULL;
  506. pkg->files->name = pkg->files->msdosname = pkg->files->md5sum = NULL;
  507. }
  508. /* Always nfmalloc. Otherwise, we may overwrite some other field (like
  509. * md5sum). */
  510. psize = nfmalloc(30);
  511. sprintf(psize, "%jd", (intmax_t)stab.st_size);
  512. pkg->files->size = psize;
  513. if (cipaction->arg_int == act_avail) {
  514. printf(_("Recorded info about %s from %s.\n"),
  515. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), pfilename);
  516. pop_cleanup(ehflag_normaltidy);
  517. return;
  518. }
  519. if (pkg->available.arch->type != DPKG_ARCH_ALL &&
  520. pkg->available.arch->type != DPKG_ARCH_NATIVE &&
  521. pkg->available.arch->type != DPKG_ARCH_FOREIGN)
  522. forcibleerr(fc_architecture,
  523. _("package architecture (%s) does not match system (%s)"),
  524. pkg->available.arch->name,
  525. dpkg_arch_get(DPKG_ARCH_NATIVE)->name);
  526. clear_deconfigure_queue();
  527. clear_istobes();
  528. if (wanttoinstall(pkg)) {
  529. pkg_set_want(pkg, PKG_WANT_INSTALL);
  530. } else {
  531. pop_cleanup(ehflag_normaltidy);
  532. return;
  533. }
  534. /* Deconfigure other instances from a pkgset if they are not in sync. */
  535. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  536. if (otherpkg == pkg)
  537. continue;
  538. if (otherpkg->status <= PKG_STAT_HALFCONFIGURED)
  539. continue;
  540. if (dpkg_version_compare(&pkg->available.version,
  541. &otherpkg->installed.version))
  542. enqueue_deconfigure(otherpkg, NULL);
  543. }
  544. /* Check if anything is installed that we conflict with, or not installed
  545. * that we need. */
  546. pkg->clientdata->istobe = PKG_ISTOBE_INSTALLNEW;
  547. for (dsearch= pkg->available.depends; dsearch; dsearch= dsearch->next) {
  548. switch (dsearch->type) {
  549. case dep_conflicts:
  550. /* Look for things we conflict with. */
  551. check_conflict(dsearch, pkg, pfilename);
  552. break;
  553. case dep_breaks:
  554. /* Look for things we break. */
  555. check_breaks(dsearch, pkg, pfilename);
  556. break;
  557. case dep_provides:
  558. /* Look for things that conflict with what we provide. */
  559. for (psearch = dsearch->list->ed->depended.installed;
  560. psearch;
  561. psearch = psearch->rev_next) {
  562. if (psearch->up->type != dep_conflicts)
  563. continue;
  564. check_conflict(psearch->up, pkg, pfilename);
  565. }
  566. break;
  567. case dep_suggests:
  568. case dep_recommends:
  569. case dep_depends:
  570. case dep_replaces:
  571. case dep_enhances:
  572. /* Ignore these here. */
  573. break;
  574. case dep_predepends:
  575. if (!depisok(dsearch, &depprobwhy, NULL, &fixbytrigaw, true)) {
  576. if (fixbytrigaw) {
  577. while (fixbytrigaw->trigaw.head)
  578. trigproc(fixbytrigaw->trigaw.head->pend, TRIGPROC_REQUIRED);
  579. } else {
  580. varbuf_end_str(&depprobwhy);
  581. notice(_("regarding %s containing %s, pre-dependency problem:\n%s"),
  582. pfilename, pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  583. depprobwhy.buf);
  584. if (!force_depends(dsearch->list))
  585. ohshit(_("pre-dependency problem - not installing %.250s"),
  586. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  587. warning(_("ignoring pre-dependency problem!"));
  588. }
  589. }
  590. }
  591. }
  592. /* Look for things that conflict with us. */
  593. for (psearch = pkg->set->depended.installed; psearch; psearch = psearch->rev_next) {
  594. if (psearch->up->type != dep_conflicts) continue;
  595. check_conflict(psearch->up, pkg, pfilename);
  596. }
  597. ensure_allinstfiles_available();
  598. filesdbinit();
  599. trig_file_interests_ensure();
  600. printf(_("Preparing to unpack %s ...\n"), pfilename);
  601. if (pkg->status != PKG_STAT_NOTINSTALLED &&
  602. pkg->status != PKG_STAT_CONFIGFILES) {
  603. log_action("upgrade", pkg, &pkg->installed);
  604. } else {
  605. log_action("install", pkg, &pkg->available);
  606. }
  607. if (f_noact) {
  608. pop_cleanup(ehflag_normaltidy);
  609. return;
  610. }
  611. /*
  612. * OK, we're going ahead.
  613. */
  614. trig_activate_packageprocessing(pkg);
  615. strcpy(cidirrest, TRIGGERSCIFILE);
  616. trig_parse_ci(cidir, NULL, trig_cicb_statuschange_activate, pkg, &pkg->available);
  617. /* Read the conffiles, and copy the hashes across. */
  618. newconffiles.head = NULL;
  619. newconffiles.tail = &newconffiles.head;
  620. push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
  621. strcpy(cidirrest,CONFFILESFILE);
  622. deb_parse_conffiles(pkg, cidir, &newconffiles);
  623. /* All the old conffiles are marked with a flag, so that we don't delete
  624. * them if they seem to disappear completely. */
  625. pkg_conffiles_mark_old(pkg);
  626. for (conflictor_iter = conflictors.head;
  627. conflictor_iter;
  628. conflictor_iter = conflictor_iter->next)
  629. pkg_conffiles_mark_old(conflictor_iter->pkg);
  630. oldversionstatus= pkg->status;
  631. assert(oldversionstatus <= PKG_STAT_INSTALLED);
  632. debug(dbg_general,"process_archive oldversionstatus=%s",
  633. statusstrings[oldversionstatus]);
  634. if (oldversionstatus == PKG_STAT_HALFCONFIGURED ||
  635. oldversionstatus == PKG_STAT_TRIGGERSAWAITED ||
  636. oldversionstatus == PKG_STAT_TRIGGERSPENDING ||
  637. oldversionstatus == PKG_STAT_INSTALLED) {
  638. pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
  639. pkg_set_status(pkg, PKG_STAT_HALFCONFIGURED);
  640. modstatdb_note(pkg);
  641. push_cleanup(cu_prermupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
  642. if (dpkg_version_compare(&pkg->available.version,
  643. &pkg->installed.version) >= 0)
  644. /* Upgrade or reinstall. */
  645. maintscript_fallback(pkg, PRERMFILE, "pre-removal", cidir, cidirrest,
  646. "upgrade", "failed-upgrade");
  647. else /* Downgrade => no fallback */
  648. maintscript_installed(pkg, PRERMFILE, "pre-removal",
  649. "upgrade",
  650. versiondescribe(&pkg->available.version,
  651. vdew_nonambig),
  652. NULL);
  653. pkg_set_status(pkg, PKG_STAT_UNPACKED);
  654. oldversionstatus = PKG_STAT_UNPACKED;
  655. modstatdb_note(pkg);
  656. }
  657. for (deconpil= deconfigure; deconpil; deconpil= deconpil->next) {
  658. struct pkginfo *removing = deconpil->pkg_removal;
  659. if (removing)
  660. printf(_("De-configuring %s (%s), to allow removal of %s (%s) ...\n"),
  661. pkg_name(deconpil->pkg, pnaw_nonambig),
  662. versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig),
  663. pkg_name(removing, pnaw_nonambig),
  664. versiondescribe(&removing->installed.version, vdew_nonambig));
  665. else
  666. printf(_("De-configuring %s (%s) ...\n"),
  667. pkg_name(deconpil->pkg, pnaw_nonambig),
  668. versiondescribe(&deconpil->pkg->installed.version, vdew_nonambig));
  669. trig_activate_packageprocessing(deconpil->pkg);
  670. pkg_set_status(deconpil->pkg, PKG_STAT_HALFCONFIGURED);
  671. modstatdb_note(deconpil->pkg);
  672. /* This means that we *either* go and run postinst abort-deconfigure,
  673. * *or* queue the package for later configure processing, depending
  674. * on which error cleanup route gets taken. */
  675. push_cleanup(cu_prermdeconfigure, ~ehflag_normaltidy,
  676. ok_prermdeconfigure, ehflag_normaltidy,
  677. 3, (void*)deconpil->pkg, (void*)removing, (void*)pkg);
  678. if (removing) {
  679. maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  680. "deconfigure", "in-favour",
  681. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  682. versiondescribe(&pkg->available.version,
  683. vdew_nonambig),
  684. "removing",
  685. pkg_name(removing, pnaw_nonambig),
  686. versiondescribe(&removing->installed.version,
  687. vdew_nonambig),
  688. NULL);
  689. } else {
  690. maintscript_installed(deconpil->pkg, PRERMFILE, "pre-removal",
  691. "deconfigure", "in-favour",
  692. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  693. versiondescribe(&pkg->available.version,
  694. vdew_nonambig),
  695. NULL);
  696. }
  697. }
  698. for (conflictor_iter = conflictors.head;
  699. conflictor_iter;
  700. conflictor_iter = conflictor_iter->next) {
  701. struct pkginfo *conflictor = conflictor_iter->pkg;
  702. if (!(conflictor->status == PKG_STAT_HALFCONFIGURED ||
  703. conflictor->status == PKG_STAT_TRIGGERSAWAITED ||
  704. conflictor->status == PKG_STAT_TRIGGERSPENDING ||
  705. conflictor->status == PKG_STAT_INSTALLED))
  706. continue;
  707. trig_activate_packageprocessing(conflictor);
  708. pkg_set_status(conflictor, PKG_STAT_HALFCONFIGURED);
  709. modstatdb_note(conflictor);
  710. push_cleanup(cu_prerminfavour, ~ehflag_normaltidy, NULL, 0,
  711. 2, conflictor, pkg);
  712. maintscript_installed(conflictor, PRERMFILE, "pre-removal",
  713. "remove", "in-favour",
  714. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  715. versiondescribe(&pkg->available.version,
  716. vdew_nonambig),
  717. NULL);
  718. pkg_set_status(conflictor, PKG_STAT_HALFINSTALLED);
  719. modstatdb_note(conflictor);
  720. }
  721. pkg_set_eflags(pkg, PKG_EFLAG_REINSTREQ);
  722. if (pkg->status == PKG_STAT_NOTINSTALLED) {
  723. pkg->installed.version= pkg->available.version;
  724. pkg->installed.multiarch = pkg->available.multiarch;
  725. }
  726. pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
  727. modstatdb_note(pkg);
  728. if (oldversionstatus == PKG_STAT_NOTINSTALLED) {
  729. push_cleanup(cu_preinstverynew, ~ehflag_normaltidy, NULL, 0,
  730. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  731. maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  732. "install", NULL);
  733. } else if (oldversionstatus == PKG_STAT_CONFIGFILES) {
  734. push_cleanup(cu_preinstnew, ~ehflag_normaltidy, NULL, 0,
  735. 3,(void*)pkg,(void*)cidir,(void*)cidirrest);
  736. maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  737. "install",
  738. versiondescribe(&pkg->installed.version, vdew_nonambig),
  739. NULL);
  740. } else {
  741. push_cleanup(cu_preinstupgrade, ~ehflag_normaltidy, NULL, 0,
  742. 4,(void*)pkg,(void*)cidir,(void*)cidirrest,(void*)&oldversionstatus);
  743. maintscript_new(pkg, PREINSTFILE, "pre-installation", cidir, cidirrest,
  744. "upgrade",
  745. versiondescribe(&pkg->installed.version, vdew_nonambig),
  746. NULL);
  747. }
  748. if (oldversionstatus == PKG_STAT_NOTINSTALLED ||
  749. oldversionstatus == PKG_STAT_CONFIGFILES) {
  750. printf(_("Unpacking %s (%s) ...\n"),
  751. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  752. versiondescribe(&pkg->available.version, vdew_nonambig));
  753. } else {
  754. printf(_("Unpacking %s (%s) over (%s) ...\n"),
  755. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  756. versiondescribe(&pkg->available.version, vdew_nonambig),
  757. versiondescribe(&pkg->installed.version, vdew_nonambig));
  758. }
  759. /*
  760. * Now we unpack the archive, backing things up as we go.
  761. * For each file, we check to see if it already exists.
  762. * There are several possibilities:
  763. *
  764. * + We are trying to install a non-directory ...
  765. * - It doesn't exist. In this case we simply extract it.
  766. * - It is a plain file, device, symlink, &c. We do an ‘atomic
  767. * overwrite’ using link() and rename(), but leave a backup copy.
  768. * Later, when we delete the backup, we remove it from any other
  769. * packages' lists.
  770. * - It is a directory. In this case it depends on whether we're
  771. * trying to install a symlink or something else.
  772. * = If we're not trying to install a symlink we move the directory
  773. * aside and extract the node. Later, when we recursively remove
  774. * the backed-up directory, we remove it from any other packages'
  775. * lists.
  776. * = If we are trying to install a symlink we do nothing - ie,
  777. * dpkg will never replace a directory tree with a symlink. This
  778. * is to avoid embarrassing effects such as replacing a directory
  779. * tree with a link to a link to the original directory tree.
  780. * + We are trying to install a directory ...
  781. * - It doesn't exist. We create it with the appropriate modes.
  782. * - It exists as a directory or a symlink to one. We do nothing.
  783. * - It is a plain file or a symlink (other than to a directory).
  784. * We move it aside and create the directory. Later, when we
  785. * delete the backup, we remove it from any other packages' lists.
  786. *
  787. * Install non-dir Install symlink Install dir
  788. * Exists not X X X
  789. * File/node/symlink LXR LXR BXR
  790. * Directory BXR - -
  791. *
  792. * X: extract file/node/link/directory
  793. * LX: atomic overwrite leaving backup
  794. * B: ordinary backup
  795. * R: later remove from other packages' lists
  796. * -: do nothing
  797. *
  798. * After we've done this we go through the remaining things in the
  799. * lists of packages we're trying to remove (including the old
  800. * version of the current package). This happens in reverse order,
  801. * so that we process files before the directories (or symlinks-to-
  802. * directories) containing them.
  803. *
  804. * + If the thing is a conffile then we leave it alone for the purge
  805. * operation.
  806. * + Otherwise, there are several possibilities too:
  807. * - The listed thing does not exist. We ignore it.
  808. * - The listed thing is a directory or a symlink to a directory.
  809. * We delete it only if it isn't listed in any other package.
  810. * - The listed thing is not a directory, but was part of the package
  811. * that was upgraded, we check to make sure the files aren't the
  812. * same ones from the old package by checking dev/inode
  813. * - The listed thing is not a directory or a symlink to one (ie,
  814. * it's a plain file, device, pipe, &c, or a symlink to one, or a
  815. * dangling symlink). We delete it.
  816. *
  817. * The removed packages' list becomes empty (of course, the new
  818. * version of the package we're installing will have a new list,
  819. * which replaces the old version's list).
  820. *
  821. * If at any stage we remove a file from a package's list, and the
  822. * package isn't one we're already processing, and the package's
  823. * list becomes empty as a result, we ‘vanish’ the package. This
  824. * means that we run its postrm with the ‘disappear’ argument, and
  825. * put the package in the ‘not-installed’ state. If it had any
  826. * conffiles, their hashes and ownership will have been transferred
  827. * already, so we just ignore those and forget about them from the
  828. * point of view of the disappearing package.
  829. *
  830. * NOTE THAT THE OLD POSTRM IS RUN AFTER THE NEW PREINST, since the
  831. * files get replaced ‘as we go’.
  832. */
  833. m_pipe(p1);
  834. push_cleanup(cu_closepipe, ehflag_bombout, NULL, 0, 1, (void *)&p1[0]);
  835. pid = subproc_fork();
  836. if (pid == 0) {
  837. m_dup2(p1[1],1); close(p1[0]); close(p1[1]);
  838. execlp(BACKEND, BACKEND, "--fsys-tarfile", filename, NULL);
  839. ohshite(_("unable to execute %s (%s)"),
  840. _("package filesystem archive extraction"), BACKEND);
  841. }
  842. close(p1[1]);
  843. p1[1] = -1;
  844. newfileslist = NULL;
  845. tc.newfilesp = &newfileslist;
  846. push_cleanup(cu_fileslist, ~0, NULL, 0, 0);
  847. tc.pkg= pkg;
  848. tc.backendpipe= p1[0];
  849. tc.pkgset_getting_in_sync = pkgset_getting_in_sync(pkg);
  850. rc = tar_extractor(&tc, &tf);
  851. if (rc) {
  852. if (errno) {
  853. ohshite(_("error reading dpkg-deb tar output"));
  854. } else {
  855. ohshit(_("corrupted filesystem tarfile - corrupted package archive"));
  856. }
  857. }
  858. if (fd_skip(p1[0], -1, &err) < 0)
  859. ohshit(_("cannot zap possible trailing zeros from dpkg-deb: %s"), err.str);
  860. close(p1[0]);
  861. p1[0] = -1;
  862. subproc_reap(pid, BACKEND " --fsys-tarfile", SUBPROC_NOPIPE);
  863. tar_deferred_extract(newfileslist, pkg);
  864. if (oldversionstatus == PKG_STAT_HALFINSTALLED ||
  865. oldversionstatus == PKG_STAT_UNPACKED) {
  866. /* Packages that were in ‘installed’ and ‘postinstfailed’ have been
  867. * reduced to ‘unpacked’ by now, by the running of the prerm script. */
  868. pkg_set_status(pkg, PKG_STAT_HALFINSTALLED);
  869. modstatdb_note(pkg);
  870. push_cleanup(cu_postrmupgrade, ~ehflag_normaltidy, NULL, 0, 1, (void *)pkg);
  871. maintscript_fallback(pkg, POSTRMFILE, "post-removal", cidir, cidirrest,
  872. "upgrade", "failed-upgrade");
  873. }
  874. /* If anything goes wrong while tidying up it's a bit late to do
  875. * anything about it. However, we don't install the new status
  876. * info yet, so that a future dpkg installation will put everything
  877. * right (we hope).
  878. *
  879. * If something does go wrong later the ‘conflictor’ package will be
  880. * left in the ‘removal_failed’ state. Removing or installing it
  881. * will be impossible if it was required because of the conflict with
  882. * the package we're installing now and (presumably) the dependency
  883. * by other packages. This means that the files it contains in
  884. * common with this package will hang around until we successfully
  885. * get this package installed, after which point we can trust the
  886. * conflicting package's file list, which will have been updated to
  887. * remove any files in this package. */
  888. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  889. /* Now we delete all the files that were in the old version of
  890. * the package only, except (old or new) conffiles, which we leave
  891. * alone. */
  892. reversefilelist_init(&rlistit,pkg->clientdata->files);
  893. while ((namenode= reversefilelist_next(&rlistit))) {
  894. struct filenamenode *usenode;
  895. if ((namenode->flags & fnnf_new_conff) ||
  896. (namenode->flags & fnnf_new_inarchive))
  897. continue;
  898. usenode = namenodetouse(namenode, pkg, &pkg->installed);
  899. varbuf_trunc(&fnamevb, fnameidlu);
  900. varbuf_add_str(&fnamevb, usenode->name);
  901. varbuf_end_str(&fnamevb);
  902. if (!stat(namenode->name,&stab) && S_ISDIR(stab.st_mode)) {
  903. debug(dbg_eachfiledetail, "process_archive: %s is a directory",
  904. namenode->name);
  905. if (dir_is_used_by_others(namenode, pkg))
  906. continue;
  907. }
  908. if (lstat(fnamevb.buf, &oldfs)) {
  909. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  910. warning(_("could not stat old file '%.250s' so not deleting it: %s"),
  911. fnamevb.buf, strerror(errno));
  912. continue;
  913. }
  914. if (S_ISDIR(oldfs.st_mode)) {
  915. trig_path_activate(usenode, pkg);
  916. /* Do not try to remove the root directory. */
  917. if (strcmp(usenode->name, "/.") == 0)
  918. continue;
  919. if (rmdir(fnamevb.buf)) {
  920. warning(_("unable to delete old directory '%.250s': %s"),
  921. namenode->name, strerror(errno));
  922. } else if ((namenode->flags & fnnf_old_conff)) {
  923. warning(_("old conffile '%.250s' was an empty directory "
  924. "(and has now been deleted)"), namenode->name);
  925. }
  926. } else {
  927. struct fileinlist *sameas = NULL;
  928. static struct stat empty_stat;
  929. struct varbuf cfilename = VARBUF_INIT;
  930. /*
  931. * Ok, it's an old file, but is it really not in the new package?
  932. * It might be known by a different name because of symlinks.
  933. *
  934. * We need to check to make sure, so we stat the file, then compare
  935. * it to the new list. If we find a dev/inode match, we assume they
  936. * are the same file, and leave it alone. NOTE: we don't check in
  937. * other packages for sanity reasons (we don't want to stat _all_
  938. * the files on the system).
  939. *
  940. * We run down the list of _new_ files in this package. This keeps
  941. * the process a little leaner. We are only worried about new ones
  942. * since ones that stayed the same don't really apply here.
  943. */
  944. /* If we can't stat the old or new file, or it's a directory,
  945. * we leave it up to the normal code. */
  946. debug(dbg_eachfile, "process_archive: checking %s for same files on "
  947. "upgrade/downgrade", fnamevb.buf);
  948. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  949. /* If the file has been filtered then treat it as if it didn't exist
  950. * on the file system. */
  951. if (cfile->namenode->flags & fnnf_filtered)
  952. continue;
  953. if (!cfile->namenode->filestat) {
  954. struct stat tmp_stat;
  955. varbuf_reset(&cfilename);
  956. varbuf_add_str(&cfilename, instdir);
  957. varbuf_add_str(&cfilename, cfile->namenode->name);
  958. varbuf_end_str(&cfilename);
  959. if (lstat(cfilename.buf, &tmp_stat) == 0) {
  960. cfile->namenode->filestat = nfmalloc(sizeof(struct stat));
  961. memcpy(cfile->namenode->filestat, &tmp_stat, sizeof(struct stat));
  962. } else {
  963. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  964. ohshite(_("unable to stat other new file '%.250s'"),
  965. cfile->namenode->name);
  966. cfile->namenode->filestat = &empty_stat;
  967. continue;
  968. }
  969. }
  970. if (cfile->namenode->filestat == &empty_stat)
  971. continue;
  972. if (oldfs.st_dev == cfile->namenode->filestat->st_dev &&
  973. oldfs.st_ino == cfile->namenode->filestat->st_ino) {
  974. if (sameas)
  975. warning(_("old file '%.250s' is the same as several new files! "
  976. "(both '%.250s' and '%.250s')"), fnamevb.buf,
  977. sameas->namenode->name, cfile->namenode->name);
  978. sameas= cfile;
  979. debug(dbg_eachfile, "process_archive: not removing %s,"
  980. " since it matches %s", fnamevb.buf, cfile->namenode->name);
  981. }
  982. }
  983. varbuf_destroy(&cfilename);
  984. if ((namenode->flags & fnnf_old_conff)) {
  985. if (sameas) {
  986. if (sameas->namenode->flags & fnnf_new_conff) {
  987. if (strcmp(sameas->namenode->oldhash, NEWCONFFILEFLAG) == 0) {
  988. sameas->namenode->oldhash= namenode->oldhash;
  989. debug(dbg_eachfile, "process_archive: old conff %s"
  990. " is same as new conff %s, copying hash",
  991. namenode->name, sameas->namenode->name);
  992. } else {
  993. debug(dbg_eachfile, "process_archive: old conff %s"
  994. " is same as new conff %s but latter already has hash",
  995. namenode->name, sameas->namenode->name);
  996. }
  997. }
  998. } else {
  999. debug(dbg_eachfile, "process_archive: old conff %s"
  1000. " is disappearing", namenode->name);
  1001. namenode->flags |= fnnf_obs_conff;
  1002. filenamenode_queue_push(&newconffiles, namenode);
  1003. addfiletolist(&tc, namenode);
  1004. }
  1005. continue;
  1006. }
  1007. if (sameas)
  1008. continue;
  1009. trig_path_activate(usenode, pkg);
  1010. if (secure_unlink_statted(fnamevb.buf, &oldfs)) {
  1011. warning(_("unable to securely remove old file '%.250s': %s"),
  1012. namenode->name, strerror(errno));
  1013. }
  1014. } /* !S_ISDIR */
  1015. }
  1016. /* OK, now we can write the updated files-in-this package list,
  1017. * since we've done away (hopefully) with all the old junk. */
  1018. write_filelist_except(pkg, &pkg->available, newfileslist, 0);
  1019. /* Trigger interests may have changed.
  1020. * Firstly we go through the old list of interests deleting them.
  1021. * Then we go through the new list adding them. */
  1022. strcpy(cidirrest, TRIGGERSCIFILE);
  1023. trig_parse_ci(pkg_infodb_get_file(pkg, &pkg->installed, TRIGGERSCIFILE),
  1024. trig_cicb_interest_delete, NULL, pkg, &pkg->installed);
  1025. trig_parse_ci(cidir, trig_cicb_interest_add, NULL, pkg, &pkg->available);
  1026. trig_file_interests_save();
  1027. /* We also install the new maintainer scripts, and any other
  1028. * cruft that may have come along with the package. First
  1029. * we go through the existing scripts replacing or removing
  1030. * them as appropriate; then we go through the new scripts
  1031. * (any that are left) and install them. */
  1032. debug(dbg_general, "process_archive updating info directory");
  1033. pkg_infodb_update(pkg, cidir, cidirrest);
  1034. /* We store now the checksums dynamically computed while unpacking. */
  1035. write_filehash_except(pkg, &pkg->available, newfileslist, 0);
  1036. /*
  1037. * Update the status database.
  1038. *
  1039. * This involves copying each field across from the ‘available’
  1040. * to the ‘installed’ half of the pkg structure.
  1041. * For some of the fields we have to do a complicated construction
  1042. * operation; for others we can just copy the value.
  1043. * We tackle the fields in the order they appear, so that
  1044. * we don't miss any out :-).
  1045. * At least we don't have to copy any strings that are referred
  1046. * to, because these are never modified and never freed.
  1047. */
  1048. /* The dependencies are the most difficult. We have to build
  1049. * a whole new forward dependency tree. At least the reverse
  1050. * links (linking our deppossi's into the reverse chains)
  1051. * can be done by copy_dependency_links. */
  1052. newdeplist = NULL;
  1053. newdeplistlastp = &newdeplist;
  1054. for (dep= pkg->available.depends; dep; dep= dep->next) {
  1055. newdep= nfmalloc(sizeof(struct dependency));
  1056. newdep->up= pkg;
  1057. newdep->next = NULL;
  1058. newdep->list = NULL;
  1059. newpossilastp = &newdep->list;
  1060. for (possi= dep->list; possi; possi= possi->next) {
  1061. newpossi= nfmalloc(sizeof(struct deppossi));
  1062. newpossi->up= newdep;
  1063. newpossi->ed= possi->ed;
  1064. newpossi->next = NULL;
  1065. newpossi->rev_next = newpossi->rev_prev = NULL;
  1066. newpossi->arch_is_implicit = possi->arch_is_implicit;
  1067. newpossi->arch = possi->arch;
  1068. newpossi->verrel= possi->verrel;
  1069. if (possi->verrel != DPKG_RELATION_NONE)
  1070. newpossi->version= possi->version;
  1071. else
  1072. dpkg_version_blank(&newpossi->version);
  1073. newpossi->cyclebreak = false;
  1074. *newpossilastp= newpossi;
  1075. newpossilastp= &newpossi->next;
  1076. }
  1077. newdep->type= dep->type;
  1078. *newdeplistlastp= newdep;
  1079. newdeplistlastp= &newdep->next;
  1080. }
  1081. /* Right, now we've replicated the forward tree, we
  1082. * get copy_dependency_links to remove all the old dependency
  1083. * structures from the reverse links and add the new dependency
  1084. * structures in instead. It also copies the new dependency
  1085. * structure pointer for this package into the right field. */
  1086. copy_dependency_links(pkg,&pkg->installed.depends,newdeplist,0);
  1087. /* We copy the text fields. */
  1088. pkg->installed.essential= pkg->available.essential;
  1089. pkg->installed.multiarch = pkg->available.multiarch;
  1090. pkg->installed.description= pkg->available.description;
  1091. pkg->installed.maintainer= pkg->available.maintainer;
  1092. pkg->installed.source= pkg->available.source;
  1093. pkg->installed.arch = pkg->available.arch;
  1094. pkg->installed.installedsize= pkg->available.installedsize;
  1095. pkg->installed.version= pkg->available.version;
  1096. pkg->installed.origin = pkg->available.origin;
  1097. pkg->installed.bugs = pkg->available.bugs;
  1098. /* We have to generate our own conffiles structure. */
  1099. pkg->installed.conffiles = NULL;
  1100. iconffileslastp = &pkg->installed.conffiles;
  1101. for (cfile = newconffiles.head; cfile; cfile = cfile->next) {
  1102. newiconff= nfmalloc(sizeof(struct conffile));
  1103. newiconff->next = NULL;
  1104. newiconff->name= nfstrsave(cfile->namenode->name);
  1105. newiconff->hash= nfstrsave(cfile->namenode->oldhash);
  1106. newiconff->obsolete= !!(cfile->namenode->flags & fnnf_obs_conff);
  1107. *iconffileslastp= newiconff;
  1108. iconffileslastp= &newiconff->next;
  1109. }
  1110. /* We can just copy the arbitrary fields list, because it is
  1111. * never even rearranged. Phew! */
  1112. pkg->installed.arbs= pkg->available.arbs;
  1113. /* In case this was an architecture cross-grade, the in-core pkgset might
  1114. * be in an inconsistent state, with two pkginfo entries having the same
  1115. * architecture, so let's fix that. Note that this does not lose data,
  1116. * as the pkg.available member parsed from the binary should replace the
  1117. * to be cleared duplicate in the other instance. */
  1118. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  1119. if (otherpkg == pkg)
  1120. continue;
  1121. if (otherpkg->installed.arch != pkg->installed.arch)
  1122. continue;
  1123. assert(otherpkg->status == PKG_STAT_NOTINSTALLED);
  1124. pkg_blank(otherpkg);
  1125. }
  1126. /* Check for disappearing packages:
  1127. * We go through all the packages on the system looking for ones
  1128. * whose files are entirely part of the one we've just unpacked
  1129. * (and which actually *have* some files!).
  1130. *
  1131. * Any that we find are removed - we run the postrm with ‘disappear’
  1132. * as an argument, and remove their info/... files and status info.
  1133. * Conffiles are ignored (the new package had better do something
  1134. * with them!). */
  1135. it = pkg_db_iter_new();
  1136. while ((otherpkg = pkg_db_iter_next_pkg(it)) != NULL) {
  1137. ensure_package_clientdata(otherpkg);
  1138. if (otherpkg == pkg ||
  1139. otherpkg->status == PKG_STAT_NOTINSTALLED ||
  1140. otherpkg->status == PKG_STAT_CONFIGFILES ||
  1141. otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE ||
  1142. !otherpkg->clientdata->files) continue;
  1143. /* Do not try to disappear other packages from the same set
  1144. * if they are Multi-Arch: same */
  1145. if (pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  1146. otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  1147. otherpkg->set == pkg->set)
  1148. continue;
  1149. debug(dbg_veryverbose, "process_archive checking disappearance %s",
  1150. pkg_name(otherpkg, pnaw_always));
  1151. assert(otherpkg->clientdata->istobe == PKG_ISTOBE_NORMAL ||
  1152. otherpkg->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
  1153. for (cfile= otherpkg->clientdata->files;
  1154. cfile && strcmp(cfile->namenode->name, "/.") == 0;
  1155. cfile= cfile->next);
  1156. if (!cfile) {
  1157. debug(dbg_stupidlyverbose, "process_archive no non-root, no disappear");
  1158. continue;
  1159. }
  1160. for (cfile= otherpkg->clientdata->files;
  1161. cfile && !filesavespackage(cfile,otherpkg,pkg);
  1162. cfile= cfile->next);
  1163. if (cfile) continue;
  1164. /* So dependency things will give right answers ... */
  1165. otherpkg->clientdata->istobe = PKG_ISTOBE_REMOVE;
  1166. debug(dbg_veryverbose, "process_archive disappear checking dependencies");
  1167. for (pdep = otherpkg->set->depended.installed;
  1168. pdep;
  1169. pdep = pdep->rev_next) {
  1170. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  1171. pdep->up->type != dep_recommends) continue;
  1172. if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
  1173. continue;
  1174. varbuf_end_str(&depprobwhy);
  1175. debug(dbg_veryverbose,"process_archive cannot disappear: %s",depprobwhy.buf);
  1176. break;
  1177. }
  1178. if (!pdep) {
  1179. /* If we haven't found a reason not to yet, let's look some more. */
  1180. for (providecheck= otherpkg->installed.depends;
  1181. providecheck;
  1182. providecheck= providecheck->next) {
  1183. if (providecheck->type != dep_provides) continue;
  1184. for (pdep = providecheck->list->ed->depended.installed;
  1185. pdep;
  1186. pdep = pdep->rev_next) {
  1187. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends &&
  1188. pdep->up->type != dep_recommends)
  1189. continue;
  1190. if (depisok(pdep->up, &depprobwhy, NULL, NULL, false))
  1191. continue;
  1192. varbuf_end_str(&depprobwhy);
  1193. debug(dbg_veryverbose,"process_archive cannot disappear (provides %s): %s",
  1194. providecheck->list->ed->name, depprobwhy.buf);
  1195. goto break_from_both_loops_at_once;
  1196. }
  1197. }
  1198. break_from_both_loops_at_once:;
  1199. }
  1200. otherpkg->clientdata->istobe = PKG_ISTOBE_NORMAL;
  1201. if (pdep) continue;
  1202. /* No, we're disappearing it. This is the wrong time to go and
  1203. * run maintainer scripts and things, as we can't back out. But
  1204. * what can we do ? It has to be run this late. */
  1205. pkg_disappear(otherpkg, pkg);
  1206. } /* while (otherpkg= ... */
  1207. pkg_db_iter_free(it);
  1208. /* Delete files from any other packages' lists.
  1209. * We have to do this before we claim this package is in any
  1210. * sane kind of state, as otherwise we might delete by mistake
  1211. * a file that we overwrote, when we remove the package which
  1212. * had the version we overwrote. To prevent this we make
  1213. * sure that we don't claim this package is OK until we
  1214. * have claimed ‘ownership’ of all its files. */
  1215. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  1216. struct filepackages_iterator *iter;
  1217. struct pkgset *divpkgset;
  1218. if (!(cfile->namenode->flags & fnnf_elide_other_lists)) continue;
  1219. if (cfile->namenode->divert && cfile->namenode->divert->useinstead) {
  1220. divpkgset = cfile->namenode->divert->pkgset;
  1221. if (divpkgset == pkg->set) {
  1222. debug(dbg_eachfile,
  1223. "process_archive not overwriting any '%s' (overriding, '%s')",
  1224. cfile->namenode->name, cfile->namenode->divert->useinstead->name);
  1225. continue;
  1226. } else {
  1227. debug(dbg_eachfile,
  1228. "process_archive looking for overwriting '%s' (overridden by %s)",
  1229. cfile->namenode->name, divpkgset ? divpkgset->name : "<local>");
  1230. }
  1231. } else {
  1232. divpkgset = NULL;
  1233. debug(dbg_eachfile, "process_archive looking for overwriting '%s'",
  1234. cfile->namenode->name);
  1235. }
  1236. iter = filepackages_iter_new(cfile->namenode);
  1237. while ((otherpkg = filepackages_iter_next(iter))) {
  1238. debug(dbg_eachfiledetail, "process_archive ... found in %s",
  1239. pkg_name(otherpkg, pnaw_always));
  1240. /* A pkgset can share files between instances, so there's no point
  1241. * in rewriting the file that's already in place. */
  1242. if (otherpkg->set == pkg->set)
  1243. continue;
  1244. if (otherpkg->set == divpkgset) {
  1245. debug(dbg_eachfiledetail, "process_archive ... diverted, skipping");
  1246. continue;
  1247. }
  1248. if (cfile->namenode->flags & fnnf_new_conff)
  1249. conffile_mark_obsolete(otherpkg, cfile->namenode);
  1250. /* If !fileslistvalid then it's one of the disappeared packages above
  1251. * or we have already updated the files list file, and we don't bother
  1252. * with it here, clearly. */
  1253. if (!otherpkg->clientdata->fileslistvalid)
  1254. continue;
  1255. /* Found one. We delete the list entry for this file,
  1256. * (and any others in the same package) and then mark the package
  1257. * as requiring a reread. */
  1258. write_filelist_except(otherpkg, &otherpkg->installed,
  1259. otherpkg->clientdata->files, fnnf_elide_other_lists);
  1260. ensure_package_clientdata(otherpkg);
  1261. debug(dbg_veryverbose, "process_archive overwrote from %s",
  1262. pkg_name(otherpkg, pnaw_always));
  1263. }
  1264. filepackages_iter_free(iter);
  1265. }
  1266. /* Right, the package we've unpacked is now in a reasonable state.
  1267. * The only thing that we have left to do with it is remove
  1268. * backup files, and we can leave the user to fix that if and when
  1269. * it happens (we leave the reinstall required flag, of course). */
  1270. pkg_set_status(pkg, PKG_STAT_UNPACKED);
  1271. modstatdb_note(pkg);
  1272. /* Now we delete all the backup files that we made when
  1273. * extracting the archive - except for files listed as conffiles
  1274. * in the new package.
  1275. * This time we count it as an error if something goes wrong.
  1276. *
  1277. * Note that we don't ever delete things that were in the old
  1278. * package as a conffile and don't appear at all in the new.
  1279. * They stay recorded as obsolete conffiles and will eventually
  1280. * (if not taken over by another package) be forgotten. */
  1281. for (cfile= newfileslist; cfile; cfile= cfile->next) {
  1282. struct filenamenode *usenode;
  1283. if (cfile->namenode->flags & fnnf_new_conff) continue;
  1284. usenode = namenodetouse(cfile->namenode, pkg, &pkg->installed);
  1285. /* Do not try to remove backups for the root directory. */
  1286. if (strcmp(usenode->name, "/.") == 0)
  1287. continue;
  1288. varbuf_trunc(&fnametmpvb, fnameidlu);
  1289. varbuf_add_str(&fnametmpvb, usenode->name);
  1290. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  1291. varbuf_end_str(&fnametmpvb);
  1292. path_remove_tree(fnametmpvb.buf);
  1293. }
  1294. /* OK, we're now fully done with the main package.
  1295. * This is quite a nice state, so we don't unwind past here. */
  1296. pkg_reset_eflags(pkg);
  1297. modstatdb_note(pkg);
  1298. push_checkpoint(~ehflag_bombout, ehflag_normaltidy);
  1299. /* Only the removal of the conflictor left to do.
  1300. * The files list for the conflictor is still a little inconsistent in-core,
  1301. * as we have not yet updated the filename->packages mappings; however,
  1302. * the package->filenames mapping is. */
  1303. while (!pkg_queue_is_empty(&conflictors)) {
  1304. struct pkginfo *conflictor = pkg_queue_pop(&conflictors);
  1305. /* We need to have the most up-to-date info about which files are
  1306. * what ... */
  1307. ensure_allinstfiles_available();
  1308. removal_bulk(conflictor);
  1309. }
  1310. if (cipaction->arg_int == act_install)
  1311. enqueue_package_mark_seen(pkg);
  1312. }