archives.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768
  1. /*
  2. * dpkg - main program for package management
  3. * archives.c - actions that process archive files, mainly unpack
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
  7. * Copyright © 2007-2014 Guillem Jover <guillem@debian.org>
  8. * Copyright © 2011 Linaro Limited
  9. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. #include <config.h>
  25. #include <compat.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/stat.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <ctype.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <stdint.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <obstack.h>
  40. #define obstack_chunk_alloc m_malloc
  41. #define obstack_chunk_free free
  42. #include <dpkg/i18n.h>
  43. #include <dpkg/dpkg.h>
  44. #include <dpkg/dpkg-db.h>
  45. #include <dpkg/pkg.h>
  46. #include <dpkg/path.h>
  47. #include <dpkg/fdio.h>
  48. #include <dpkg/buffer.h>
  49. #include <dpkg/subproc.h>
  50. #include <dpkg/command.h>
  51. #include <dpkg/file.h>
  52. #include <dpkg/tarfn.h>
  53. #include <dpkg/options.h>
  54. #include <dpkg/triglib.h>
  55. #ifdef WITH_SELINUX
  56. #include <selinux/selinux.h>
  57. #include <selinux/avc.h>
  58. #include <selinux/label.h>
  59. #endif
  60. #include "filesdb.h"
  61. #include "main.h"
  62. #include "archives.h"
  63. #include "filters.h"
  64. #include "infodb.h"
  65. static inline void
  66. fd_writeback_init(int fd)
  67. {
  68. /* Ignore the return code as it should be considered equivalent to an
  69. * asynchronous hint for the kernel, we are doing an fsync() later on
  70. * anyway. */
  71. #if defined(SYNC_FILE_RANGE_WRITE)
  72. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
  73. #elif defined(HAVE_POSIX_FADVISE)
  74. posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
  75. #endif
  76. }
  77. static struct obstack tar_obs;
  78. static bool tarobs_init = false;
  79. /**
  80. * Ensure the obstack is properly initialized.
  81. */
  82. static void ensureobstackinit(void) {
  83. if (!tarobs_init) {
  84. obstack_init(&tar_obs);
  85. tarobs_init = true;
  86. }
  87. }
  88. /**
  89. * Destroy the obstack.
  90. */
  91. static void destroyobstack(void) {
  92. if (tarobs_init) {
  93. obstack_free(&tar_obs, NULL);
  94. tarobs_init = false;
  95. }
  96. }
  97. /**
  98. * Check if a file or directory will save a package from disappearance.
  99. *
  100. * A package can only be saved by a file or directory which is part
  101. * only of itself - it must be neither part of the new package being
  102. * installed nor part of any 3rd package (this is important so that
  103. * shared directories don't stop packages from disappearing).
  104. */
  105. bool
  106. filesavespackage(struct fileinlist *file,
  107. struct pkginfo *pkgtobesaved,
  108. struct pkginfo *pkgbeinginstalled)
  109. {
  110. struct filepackages_iterator *iter;
  111. struct pkgset *divpkgset;
  112. struct pkginfo *thirdpkg;
  113. debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
  114. file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
  115. /* If the file is a contended one and it's overridden by either
  116. * the package we're considering disappearing or the package
  117. * we're installing then they're not actually the same file, so
  118. * we can't disappear the package - it is saved by this file. */
  119. if (file->namenode->divert && file->namenode->divert->useinstead) {
  120. divpkgset = file->namenode->divert->pkgset;
  121. if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
  122. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  123. return true;
  124. }
  125. }
  126. /* Is the file in the package being installed? If so then it can't save. */
  127. if (file->namenode->flags & fnnf_new_inarchive) {
  128. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  129. return false;
  130. }
  131. /* Look for a 3rd package which can take over the file (in case
  132. * it's a directory which is shared by many packages. */
  133. iter = filepackages_iter_new(file->namenode);
  134. while ((thirdpkg = filepackages_iter_next(iter))) {
  135. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  136. pkg_name(thirdpkg, pnaw_always));
  137. /* Is this not the package being installed or the one being
  138. * checked for disappearance? */
  139. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
  140. continue;
  141. /* A Multi-Arch: same package can share files and their presence in a
  142. * third package of the same set is not a sign that we can get rid of
  143. * it. */
  144. if (pkgtobesaved->installed.multiarch == PKG_MULTIARCH_SAME &&
  145. thirdpkg->set == pkgtobesaved->set)
  146. continue;
  147. /* If !fileslistvalid then we've already disappeared this one, so
  148. * we shouldn't try to make it take over this shared directory. */
  149. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  150. if (!thirdpkg->clientdata->fileslistvalid) {
  151. debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
  152. continue;
  153. }
  154. /* We've found a package that can take this file. */
  155. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  156. filepackages_iter_free(iter);
  157. return false;
  158. }
  159. filepackages_iter_free(iter);
  160. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  161. return true;
  162. }
  163. static void
  164. md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
  165. struct filenamenode *namenode)
  166. {
  167. struct pkginfo *otherpkg;
  168. struct conffile *conff;
  169. debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
  170. namenode->name);
  171. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  172. if (otherpkg == pkg)
  173. continue;
  174. /* The hash in the Conffiles is only meaningful if the package
  175. * configuration has been at least tried. */
  176. if (otherpkg->status < PKG_STAT_UNPACKED)
  177. continue;
  178. /* If we are reinstalling, even if the other package is only unpacked,
  179. * we can always make use of the Conffiles hash value from an initial
  180. * installation, if that happened at all. */
  181. if (otherpkg->status == PKG_STAT_UNPACKED &&
  182. dpkg_version_compare(&otherpkg->installed.version,
  183. &otherpkg->configversion) != 0)
  184. continue;
  185. for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
  186. if (conff->obsolete)
  187. continue;
  188. if (strcmp(conff->name, namenode->name) == 0)
  189. break;
  190. }
  191. if (conff) {
  192. strcpy(oldhash, conff->hash);
  193. debug(dbg_conffdetail,
  194. "tarobject found shared conffile, from pkg %s (%s); hash=%s",
  195. pkg_name(otherpkg, pnaw_always),
  196. pkg_status_name(otherpkg), oldhash);
  197. break;
  198. }
  199. }
  200. /* If no package was found with a valid Conffiles field, we make the
  201. * risky assumption that the hash of the current .dpkg-new file is
  202. * the one of the previously unpacked package. */
  203. if (otherpkg == NULL) {
  204. md5hash(pkg, oldhash, oldname);
  205. debug(dbg_conffdetail,
  206. "tarobject found shared conffile, from disk; hash=%s", oldhash);
  207. }
  208. }
  209. void cu_pathname(int argc, void **argv) {
  210. ensure_pathname_nonexisting((char*)(argv[0]));
  211. }
  212. int tarfileread(void *ud, char *buf, int len) {
  213. struct tarcontext *tc= (struct tarcontext*)ud;
  214. int r;
  215. r = fd_read(tc->backendpipe, buf, len);
  216. if (r < 0)
  217. ohshite(_("error reading from dpkg-deb pipe"));
  218. return r;
  219. }
  220. static void
  221. tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
  222. {
  223. struct dpkg_error err;
  224. size_t r;
  225. r = te->size % TARBLKSZ;
  226. if (r == 0)
  227. return;
  228. if (fd_skip(tc->backendpipe, TARBLKSZ - r, &err) < 0)
  229. ohshit(_("cannot skip padding for file '%.255s': %s"), te->name, err.str);
  230. }
  231. static void
  232. tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
  233. {
  234. /* We need to advance the tar file to the next object, so read the
  235. * file data and set it to oblivion. */
  236. if (ti->type == TAR_FILETYPE_FILE) {
  237. struct dpkg_error err;
  238. char fnamebuf[256];
  239. if (fd_skip(tc->backendpipe, ti->size, &err) < 0)
  240. ohshit(_("cannot skip file '%.255s' (replaced or excluded?) from pipe: %s"),
  241. path_quote_filename(fnamebuf, ti->name, 256), err.str);
  242. tarobject_skip_padding(tc, ti);
  243. }
  244. }
  245. int fnameidlu;
  246. struct varbuf fnamevb;
  247. struct varbuf fnametmpvb;
  248. struct varbuf fnamenewvb;
  249. struct pkg_deconf_list *deconfigure = NULL;
  250. static time_t currenttime;
  251. static int
  252. does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
  253. struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
  254. {
  255. struct dependency *dep;
  256. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
  257. pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
  258. pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
  259. versiondescribe(&old_pkgbin->version, vdew_always));
  260. for (dep = new_pkgbin->depends; dep; dep = dep->next) {
  261. if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
  262. continue;
  263. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  264. versiondescribe(&dep->list->version,vdew_always));
  265. if (!versionsatisfied(old_pkgbin, dep->list))
  266. continue;
  267. /* The test below can only trigger if dep_replaces start having
  268. * arch qualifiers different from “any”. */
  269. if (!archsatisfied(old_pkgbin, dep->list))
  270. continue;
  271. debug(dbg_depcon,"does_replace ... yes");
  272. return true;
  273. }
  274. debug(dbg_depcon,"does_replace ... no");
  275. return false;
  276. }
  277. static void
  278. tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
  279. const char *path, struct file_stat *st,
  280. struct filenamenode *namenode)
  281. {
  282. static struct varbuf hardlinkfn;
  283. static int fd;
  284. struct dpkg_error err;
  285. struct filenamenode *linknode;
  286. char fnamebuf[256];
  287. char fnamenewbuf[256];
  288. char *newhash;
  289. switch (te->type) {
  290. case TAR_FILETYPE_FILE:
  291. /* We create the file with mode 0 to make sure nobody can do anything with
  292. * it until we apply the proper mode, which might be a statoverride. */
  293. fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
  294. if (fd < 0)
  295. ohshite(_("unable to create `%.255s' (while processing `%.255s')"),
  296. path, te->name);
  297. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  298. debug(dbg_eachfiledetail, "tarobject file open size=%jd",
  299. (intmax_t)te->size);
  300. /* We try to tell the filesystem how much disk space we are going to
  301. * need to let it reduce fragmentation and possibly improve performance,
  302. * as we do know the size beforehand. */
  303. fd_allocate_size(fd, 0, te->size);
  304. newhash = nfmalloc(MD5HASHLEN + 1);
  305. if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
  306. ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
  307. path_quote_filename(fnamebuf, te->name, 256),
  308. path_quote_filename(fnamenewbuf, fnamenewvb.buf, 256), err.str);
  309. namenode->newhash = newhash;
  310. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  311. tarobject_skip_padding(tc, te);
  312. fd_writeback_init(fd);
  313. if (namenode->statoverride)
  314. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  315. namenode->statoverride->uid,
  316. namenode->statoverride->gid,
  317. namenode->statoverride->mode);
  318. if (fchown(fd, st->uid, st->gid))
  319. ohshite(_("error setting ownership of `%.255s'"), te->name);
  320. if (fchmod(fd, st->mode & ~S_IFMT))
  321. ohshite(_("error setting permissions of `%.255s'"), te->name);
  322. /* Postpone the fsync, to try to avoid massive I/O degradation. */
  323. if (!fc_unsafe_io)
  324. namenode->flags |= fnnf_deferred_fsync;
  325. pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
  326. if (close(fd))
  327. ohshite(_("error closing/writing `%.255s'"), te->name);
  328. break;
  329. case TAR_FILETYPE_FIFO:
  330. if (mkfifo(path, 0))
  331. ohshite(_("error creating pipe `%.255s'"), te->name);
  332. debug(dbg_eachfiledetail, "tarobject fifo");
  333. break;
  334. case TAR_FILETYPE_CHARDEV:
  335. if (mknod(path, S_IFCHR, te->dev))
  336. ohshite(_("error creating device `%.255s'"), te->name);
  337. debug(dbg_eachfiledetail, "tarobject chardev");
  338. break;
  339. case TAR_FILETYPE_BLOCKDEV:
  340. if (mknod(path, S_IFBLK, te->dev))
  341. ohshite(_("error creating device `%.255s'"), te->name);
  342. debug(dbg_eachfiledetail, "tarobject blockdev");
  343. break;
  344. case TAR_FILETYPE_HARDLINK:
  345. varbuf_reset(&hardlinkfn);
  346. varbuf_add_str(&hardlinkfn, instdir);
  347. linknode = findnamenode(te->linkname, 0);
  348. varbuf_add_str(&hardlinkfn,
  349. namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
  350. if (linknode->flags & (fnnf_deferred_rename | fnnf_new_conff))
  351. varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
  352. varbuf_end_str(&hardlinkfn);
  353. if (link(hardlinkfn.buf, path))
  354. ohshite(_("error creating hard link `%.255s'"), te->name);
  355. namenode->newhash = linknode->newhash;
  356. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  357. break;
  358. case TAR_FILETYPE_SYMLINK:
  359. /* We've already checked for an existing directory. */
  360. if (symlink(te->linkname, path))
  361. ohshite(_("error creating symbolic link `%.255s'"), te->name);
  362. debug(dbg_eachfiledetail, "tarobject symlink creating");
  363. break;
  364. case TAR_FILETYPE_DIR:
  365. /* We've already checked for an existing directory. */
  366. if (mkdir(path, 0))
  367. ohshite(_("error creating directory `%.255s'"), te->name);
  368. debug(dbg_eachfiledetail, "tarobject directory creating");
  369. break;
  370. default:
  371. internerr("unknown tar type '%d', but already checked", te->type);
  372. }
  373. }
  374. static void
  375. tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
  376. struct filenamenode *namenode)
  377. {
  378. if (te->type == TAR_FILETYPE_FILE) {
  379. struct dpkg_error err;
  380. char fnamebuf[256];
  381. char *newhash;
  382. newhash = nfmalloc(MD5HASHLEN + 1);
  383. if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
  384. ohshit(_("cannot compute MD5 hash for tar file '%.255s': %s"),
  385. path_quote_filename(fnamebuf, te->name, 256), err.str);
  386. tarobject_skip_padding(tc, te);
  387. namenode->newhash = newhash;
  388. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  389. } else if (te->type == TAR_FILETYPE_HARDLINK) {
  390. struct filenamenode *linknode;
  391. linknode = findnamenode(te->linkname, 0);
  392. namenode->newhash = linknode->newhash;
  393. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  394. }
  395. }
  396. static void
  397. tarobject_set_mtime(struct tar_entry *te, const char *path)
  398. {
  399. struct timeval tv[2];
  400. tv[0].tv_sec = currenttime;
  401. tv[0].tv_usec = 0;
  402. tv[1].tv_sec = te->mtime;
  403. tv[1].tv_usec = 0;
  404. if (te->type == TAR_FILETYPE_SYMLINK) {
  405. #ifdef HAVE_LUTIMES
  406. if (lutimes(path, tv) && errno != ENOSYS)
  407. ohshite(_("error setting timestamps of `%.255s'"), path);
  408. #endif
  409. } else {
  410. if (utimes(path, tv))
  411. ohshite(_("error setting timestamps of `%.255s'"), path);
  412. }
  413. }
  414. static void
  415. tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
  416. {
  417. if (te->type == TAR_FILETYPE_FILE)
  418. return; /* Already handled using the file descriptor. */
  419. if (te->type == TAR_FILETYPE_SYMLINK) {
  420. if (lchown(path, st->uid, st->gid))
  421. ohshite(_("error setting ownership of symlink `%.255s'"), path);
  422. } else {
  423. if (chown(path, st->uid, st->gid))
  424. ohshite(_("error setting ownership of `%.255s'"), path);
  425. if (chmod(path, st->mode & ~S_IFMT))
  426. ohshite(_("error setting permissions of `%.255s'"), path);
  427. }
  428. }
  429. #ifdef WITH_SELINUX
  430. static struct selabel_handle *dpkg_sehandle;
  431. static struct selabel_handle *
  432. dpkg_selabel_get_handle(void)
  433. {
  434. return dpkg_sehandle;
  435. }
  436. #endif
  437. static void
  438. dpkg_selabel_load(void)
  439. {
  440. #ifdef WITH_SELINUX
  441. static int selinux_enabled = -1;
  442. if (selinux_enabled < 0) {
  443. int rc;
  444. /* Set selinux_enabled if it is not already set (singleton). */
  445. selinux_enabled = (is_selinux_enabled() > 0);
  446. if (!selinux_enabled)
  447. return;
  448. /* Open the SELinux status notification channel, with fallback enabled
  449. * for older kernels. */
  450. rc = selinux_status_open(1);
  451. if (rc < 0)
  452. ohshit(_("cannot open security status notification channel"));
  453. /* XXX: We could use selinux_set_callback() to redirect the errors from
  454. * the other SELinux calls, but that does not seem worth it right now. */
  455. } else if (selinux_enabled && selinux_status_updated()) {
  456. /* The SELinux policy got updated in the kernel, usually after upgrading
  457. * the package shipping it, we need to reload. */
  458. selabel_close(dpkg_sehandle);
  459. } else {
  460. /* SELinux is either disabled or it does not need a reload. */
  461. return;
  462. }
  463. dpkg_sehandle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
  464. if (dpkg_sehandle == NULL)
  465. ohshite(_("cannot get security labeling handle"));
  466. #endif
  467. }
  468. static void
  469. dpkg_selabel_close(void)
  470. {
  471. #ifdef WITH_SELINUX
  472. if (dpkg_sehandle == NULL)
  473. return;
  474. selinux_status_close();
  475. selabel_close(dpkg_sehandle);
  476. dpkg_sehandle = NULL;
  477. #endif
  478. }
  479. static void
  480. tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
  481. {
  482. #ifdef WITH_SELINUX
  483. struct selabel_handle *sehandle;
  484. security_context_t scontext = NULL;
  485. int ret;
  486. /* If there's no file type, just give up. */
  487. if ((mode & S_IFMT) == 0)
  488. return;
  489. /* If SELinux is not enabled just do nothing. */
  490. sehandle = dpkg_selabel_get_handle();
  491. if (sehandle == NULL)
  492. return;
  493. /*
  494. * We use the _raw function variants here so that no translation happens
  495. * from computer to human readable forms, to avoid issues when mcstransd
  496. * has disappeared during the unpack process.
  497. */
  498. /* Do nothing if we can't figure out what the context is, or if it has
  499. * no context; in which case the default context shall be applied. */
  500. ret = selabel_lookup_raw(sehandle, &scontext, matchpath, mode & S_IFMT);
  501. if (ret == -1 || (ret == 0 && scontext == NULL))
  502. return;
  503. ret = lsetfilecon_raw(path, scontext);
  504. if (ret < 0 && errno != ENOTSUP)
  505. ohshite(_("cannot set security context for file object '%s'"), path);
  506. freecon(scontext);
  507. #endif /* WITH_SELINUX */
  508. }
  509. static void
  510. tarobject_matches(struct tarcontext *tc,
  511. const char *fn_old, struct stat *stab, char *oldhash,
  512. const char *fn_new, struct tar_entry *te,
  513. struct filenamenode *namenode)
  514. {
  515. char *linkname;
  516. ssize_t linksize;
  517. debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
  518. switch (te->type) {
  519. case TAR_FILETYPE_DIR:
  520. /* Nothing to check for a new directory. */
  521. return;
  522. case TAR_FILETYPE_SYMLINK:
  523. /* Symlinks to existing dirs have already been dealt with, only
  524. * reamin real symlinks where we can compare the target. */
  525. if (!S_ISLNK(stab->st_mode))
  526. break;
  527. linkname = m_malloc(stab->st_size + 1);
  528. linksize = readlink(fn_old, linkname, stab->st_size + 1);
  529. if (linksize < 0)
  530. ohshite(_("unable to read link `%.255s'"), fn_old);
  531. else if (linksize != stab->st_size)
  532. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  533. fn_old, stab->st_size, linksize);
  534. linkname[linksize] = '\0';
  535. if (strcmp(linkname, te->linkname) == 0) {
  536. free(linkname);
  537. return;
  538. } else {
  539. free(linkname);
  540. }
  541. break;
  542. case TAR_FILETYPE_CHARDEV:
  543. if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
  544. return;
  545. break;
  546. case TAR_FILETYPE_BLOCKDEV:
  547. if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
  548. return;
  549. break;
  550. case TAR_FILETYPE_FIFO:
  551. if (S_ISFIFO(stab->st_mode))
  552. return;
  553. break;
  554. case TAR_FILETYPE_HARDLINK:
  555. /* Fall through. */
  556. case TAR_FILETYPE_FILE:
  557. /* Only check metadata for non-conffiles. */
  558. if (!(namenode->flags & fnnf_new_conff) &&
  559. !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
  560. break;
  561. if (strcmp(oldhash, namenode->newhash) == 0)
  562. return;
  563. break;
  564. default:
  565. internerr("unknown tar type '%d', but already checked", te->type);
  566. }
  567. forcibleerr(fc_overwrite,
  568. _("trying to overwrite shared '%.250s', which is different "
  569. "from other instances of package %.250s"),
  570. namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
  571. }
  572. void setupfnamevbs(const char *filename) {
  573. varbuf_trunc(&fnamevb, fnameidlu);
  574. varbuf_add_str(&fnamevb, filename);
  575. varbuf_end_str(&fnamevb);
  576. varbuf_trunc(&fnametmpvb, fnameidlu);
  577. varbuf_add_str(&fnametmpvb, filename);
  578. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  579. varbuf_end_str(&fnametmpvb);
  580. varbuf_trunc(&fnamenewvb, fnameidlu);
  581. varbuf_add_str(&fnamenewvb, filename);
  582. varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
  583. varbuf_end_str(&fnamenewvb);
  584. debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
  585. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  586. }
  587. /**
  588. * Securely remove a pathname.
  589. *
  590. * This is a secure version of remove(3) using secure_unlink() instead of
  591. * unlink(2).
  592. *
  593. * @retval 0 On success.
  594. * @retval -1 On failure, just like unlink(2) & rmdir(2).
  595. */
  596. int
  597. secure_remove(const char *filename)
  598. {
  599. int rc, e;
  600. if (!rmdir(filename)) {
  601. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir OK", filename);
  602. return 0;
  603. }
  604. if (errno != ENOTDIR) {
  605. e= errno;
  606. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir %s", filename,
  607. strerror(e));
  608. errno= e; return -1;
  609. }
  610. rc = secure_unlink(filename);
  611. e = errno;
  612. debug(dbg_eachfiledetail, "secure_remove '%s' unlink %s",
  613. filename, rc ? strerror(e) : "OK");
  614. errno = e;
  615. return rc;
  616. }
  617. struct fileinlist *addfiletolist(struct tarcontext *tc,
  618. struct filenamenode *namenode) {
  619. struct fileinlist *nifd;
  620. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  621. nifd->namenode= namenode;
  622. nifd->next = NULL;
  623. *tc->newfilesp = nifd;
  624. tc->newfilesp = &nifd->next;
  625. return nifd;
  626. }
  627. static void
  628. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  629. struct fileinlist **oldnifd,
  630. struct fileinlist *nifd)
  631. {
  632. obstack_free(&tar_obs, nifd);
  633. tc->newfilesp = oldnifd;
  634. *oldnifd = NULL;
  635. }
  636. static bool
  637. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  638. struct varbuf *symlinkfn)
  639. {
  640. struct stat oldstab, newstab;
  641. int statr;
  642. const char *lastslash;
  643. statr= stat(fname, &oldstab);
  644. if (statr) {
  645. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  646. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  647. fname);
  648. return false;
  649. }
  650. if (!S_ISDIR(oldstab.st_mode))
  651. return false;
  652. /* But is it to the same dir? */
  653. varbuf_reset(symlinkfn);
  654. if (ti->linkname[0] == '/') {
  655. varbuf_add_str(symlinkfn, instdir);
  656. } else {
  657. lastslash= strrchr(fname, '/');
  658. assert(lastslash);
  659. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  660. }
  661. varbuf_add_str(symlinkfn, ti->linkname);
  662. varbuf_end_str(symlinkfn);
  663. statr= stat(symlinkfn->buf, &newstab);
  664. if (statr) {
  665. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  666. ohshite(_("failed to stat (dereference) proposed new symlink target"
  667. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  668. return false;
  669. }
  670. if (!S_ISDIR(newstab.st_mode))
  671. return false;
  672. if (newstab.st_dev != oldstab.st_dev ||
  673. newstab.st_ino != oldstab.st_ino)
  674. return false;
  675. return true;
  676. }
  677. int
  678. tarobject(void *ctx, struct tar_entry *ti)
  679. {
  680. static struct varbuf conffderefn, symlinkfn;
  681. const char *usename;
  682. struct filenamenode *usenode;
  683. struct conffile *conff;
  684. struct tarcontext *tc = ctx;
  685. bool existingdir, keepexisting;
  686. bool refcounting;
  687. char oldhash[MD5HASHLEN + 1];
  688. int statr;
  689. ssize_t r;
  690. struct stat stab, stabtmp;
  691. struct file_stat *st;
  692. struct fileinlist *nifd, **oldnifd;
  693. struct pkgset *divpkgset;
  694. struct pkginfo *otherpkg;
  695. ensureobstackinit();
  696. /* Append to list of files.
  697. * The trailing ‘/’ put on the end of names in tarfiles has already
  698. * been stripped by tar_extractor(). */
  699. oldnifd= tc->newfilesp;
  700. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  701. nifd->namenode->flags |= fnnf_new_inarchive;
  702. debug(dbg_eachfile,
  703. "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
  704. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  705. ti->name, (long)ti->stat.mode,
  706. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  707. ti->type,
  708. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  709. ti->linkname,
  710. nifd->namenode->name, nifd->namenode->flags,
  711. nifd->namenode->divert && nifd->namenode->divert->useinstead
  712. ? nifd->namenode->divert->useinstead->name : "<none>");
  713. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  714. divpkgset = nifd->namenode->divert->pkgset;
  715. if (divpkgset) {
  716. forcibleerr(fc_overwritediverted,
  717. _("trying to overwrite `%.250s', which is the "
  718. "diverted version of `%.250s' (package: %.100s)"),
  719. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  720. divpkgset->name);
  721. } else {
  722. forcibleerr(fc_overwritediverted,
  723. _("trying to overwrite `%.250s', which is the "
  724. "diverted version of `%.250s'"),
  725. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  726. }
  727. }
  728. if (nifd->namenode->statoverride)
  729. st = nifd->namenode->statoverride;
  730. else
  731. st = &ti->stat;
  732. usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
  733. usename = usenode->name;
  734. trig_file_activate(usenode, tc->pkg);
  735. if (nifd->namenode->flags & fnnf_new_conff) {
  736. /* If it's a conffile we have to extract it next to the installed
  737. * version (i.e. we do the usual link-following). */
  738. if (conffderef(tc->pkg, &conffderefn, usename))
  739. usename= conffderefn.buf;
  740. debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
  741. }
  742. setupfnamevbs(usename);
  743. statr= lstat(fnamevb.buf,&stab);
  744. if (statr) {
  745. /* The lstat failed. */
  746. if (errno != ENOENT && errno != ENOTDIR)
  747. ohshite(_("unable to stat `%.255s' (which I was about to install)"),
  748. ti->name);
  749. /* OK, so it doesn't exist.
  750. * However, it's possible that we were in the middle of some other
  751. * backup/restore operation and were rudely interrupted.
  752. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  753. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  754. if (errno != ENOENT && errno != ENOTDIR)
  755. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  756. "installing another version"), ti->name);
  757. debug(dbg_eachfiledetail,"tarobject nonexistent");
  758. } else {
  759. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  760. statr= lstat(fnamevb.buf,&stab);
  761. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  762. " another version"), ti->name);
  763. }
  764. } else {
  765. debug(dbg_eachfiledetail,"tarobject already exists");
  766. }
  767. /* Check to see if it's a directory or link to one and we don't need to
  768. * do anything. This has to be done now so that we don't die due to
  769. * a file overwriting conflict. */
  770. existingdir = false;
  771. switch (ti->type) {
  772. case TAR_FILETYPE_SYMLINK:
  773. /* If it's already an existing directory, do nothing. */
  774. if (!statr && S_ISDIR(stab.st_mode)) {
  775. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  776. existingdir = true;
  777. } else if (!statr && S_ISLNK(stab.st_mode)) {
  778. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  779. existingdir = true;
  780. }
  781. break;
  782. case TAR_FILETYPE_DIR:
  783. /* If it's already an existing directory, do nothing. */
  784. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  785. debug(dbg_eachfiledetail, "tarobject directory exists");
  786. existingdir = true;
  787. }
  788. break;
  789. case TAR_FILETYPE_FILE:
  790. case TAR_FILETYPE_CHARDEV:
  791. case TAR_FILETYPE_BLOCKDEV:
  792. case TAR_FILETYPE_FIFO:
  793. case TAR_FILETYPE_HARDLINK:
  794. break;
  795. default:
  796. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),
  797. ti->name, ti->type);
  798. }
  799. keepexisting = false;
  800. refcounting = false;
  801. if (!existingdir) {
  802. struct filepackages_iterator *iter;
  803. iter = filepackages_iter_new(nifd->namenode);
  804. while ((otherpkg = filepackages_iter_next(iter))) {
  805. if (otherpkg == tc->pkg)
  806. continue;
  807. debug(dbg_eachfile, "tarobject ... found in %s",
  808. pkg_name(otherpkg, pnaw_always));
  809. /* A pkgset can share files between its instances. Overwriting
  810. * is allowed when they are not getting in sync, otherwise the
  811. * file content must match the installed file. */
  812. if (otherpkg->set == tc->pkg->set &&
  813. otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  814. tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
  815. if (statr == 0 && tc->pkgset_getting_in_sync)
  816. refcounting = true;
  817. debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
  818. pkg_name(otherpkg, pnaw_always),
  819. versiondescribe(&otherpkg->installed.version, vdew_nonambig),
  820. tc->pkgset_getting_in_sync);
  821. continue;
  822. }
  823. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  824. /* Right, so we may be diverting this file. This makes the conflict
  825. * OK iff one of us is the diverting package (we don't need to
  826. * check for both being the diverting package, obviously). */
  827. divpkgset = nifd->namenode->divert->pkgset;
  828. debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
  829. divpkgset ? divpkgset->name : "<none>");
  830. if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
  831. continue;
  832. }
  833. /* If the new object is a directory and the previous object does
  834. * not exist assume it's also a directory and skip further checks.
  835. * XXX: Ideally with more information about the installed files we
  836. * could perform more clever checks. */
  837. if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
  838. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  839. continue;
  840. }
  841. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  842. switch (otherpkg->clientdata->replacingfilesandsaid) {
  843. case 2:
  844. keepexisting = true;
  845. case 1:
  846. continue;
  847. }
  848. /* Is the package with the conflicting file in the “config files only”
  849. * state? If so it must be a config file and we can silenty take it
  850. * over. */
  851. if (otherpkg->status == PKG_STAT_CONFIGFILES)
  852. continue;
  853. /* Perhaps we're removing a conflicting package? */
  854. if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
  855. continue;
  856. /* Is the file an obsolete conffile in the other package
  857. * and a conffile in the new package? */
  858. if ((nifd->namenode->flags & fnnf_new_conff) &&
  859. !statr && S_ISREG(stab.st_mode)) {
  860. for (conff = otherpkg->installed.conffiles;
  861. conff;
  862. conff = conff->next) {
  863. if (!conff->obsolete)
  864. continue;
  865. if (stat(conff->name, &stabtmp)) {
  866. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  867. continue;
  868. else
  869. ohshite(_("cannot stat file '%s'"), conff->name);
  870. }
  871. if (stabtmp.st_dev == stab.st_dev &&
  872. stabtmp.st_ino == stab.st_ino)
  873. break;
  874. }
  875. if (conff) {
  876. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  877. /* process_archive() will have copied its hash already. */
  878. continue;
  879. }
  880. }
  881. if (does_replace(tc->pkg, &tc->pkg->available,
  882. otherpkg, &otherpkg->installed)) {
  883. printf(_("Replacing files in old package %s (%s) ...\n"),
  884. pkg_name(otherpkg, pnaw_nonambig),
  885. versiondescribe(&otherpkg->installed.version, vdew_nonambig));
  886. otherpkg->clientdata->replacingfilesandsaid = 1;
  887. } else if (does_replace(otherpkg, &otherpkg->installed,
  888. tc->pkg, &tc->pkg->available)) {
  889. printf(_("Replaced by files in installed package %s (%s) ...\n"),
  890. pkg_name(otherpkg, pnaw_nonambig),
  891. versiondescribe(&otherpkg->installed.version, vdew_nonambig));
  892. otherpkg->clientdata->replacingfilesandsaid = 2;
  893. nifd->namenode->flags &= ~fnnf_new_inarchive;
  894. keepexisting = true;
  895. } else {
  896. /* At this point we are replacing something without a Replaces. */
  897. if (!statr && S_ISDIR(stab.st_mode)) {
  898. forcibleerr(fc_overwritedir,
  899. _("trying to overwrite directory '%.250s' "
  900. "in package %.250s %.250s with nondirectory"),
  901. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  902. versiondescribe(&otherpkg->installed.version,
  903. vdew_nonambig));
  904. } else {
  905. forcibleerr(fc_overwrite,
  906. _("trying to overwrite '%.250s', "
  907. "which is also in package %.250s %.250s"),
  908. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  909. versiondescribe(&otherpkg->installed.version,
  910. vdew_nonambig));
  911. }
  912. }
  913. }
  914. filepackages_iter_free(iter);
  915. }
  916. if (keepexisting) {
  917. if (nifd->namenode->flags & fnnf_new_conff)
  918. nifd->namenode->flags |= fnnf_obs_conff;
  919. remove_file_from_list(tc, ti, oldnifd, nifd);
  920. tarobject_skip_entry(tc, ti);
  921. return 0;
  922. }
  923. if (filter_should_skip(ti)) {
  924. nifd->namenode->flags &= ~fnnf_new_inarchive;
  925. nifd->namenode->flags |= fnnf_filtered;
  926. tarobject_skip_entry(tc, ti);
  927. return 0;
  928. }
  929. if (existingdir)
  930. return 0;
  931. /* Compute the hash of the previous object, before we might replace it
  932. * with the new version on forced overwrites. */
  933. if (refcounting) {
  934. debug(dbg_eachfiledetail, "tarobject hashing on-disk file '%s', refcounting",
  935. fnamevb.buf);
  936. if (nifd->namenode->flags & fnnf_new_conff) {
  937. md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
  938. } else if (S_ISREG(stab.st_mode)) {
  939. md5hash(tc->pkg, oldhash, fnamevb.buf);
  940. } else {
  941. strcpy(oldhash, EMPTYHASHFLAG);
  942. }
  943. }
  944. if (refcounting && !fc_overwrite) {
  945. /* If we are not forced to overwrite the path and are refcounting,
  946. * just compute the hash w/o extracting the object. */
  947. tarobject_hash(tc, ti, nifd->namenode);
  948. } else {
  949. /* Now, at this stage we want to make sure neither of .dpkg-new and
  950. * .dpkg-tmp are hanging around. */
  951. ensure_pathname_nonexisting(fnamenewvb.buf);
  952. ensure_pathname_nonexisting(fnametmpvb.buf);
  953. /* Now we start to do things that we need to be able to undo
  954. * if something goes wrong. Watch out for the CLEANUP comments to
  955. * keep an eye on what's installed on the disk at each point. */
  956. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
  957. /*
  958. * CLEANUP: Now we either have the old file on the disk, or not, in
  959. * its original filename.
  960. */
  961. /* Extract whatever it is as .dpkg-new ... */
  962. tarobject_extract(tc, ti, fnamenewvb.buf, st, nifd->namenode);
  963. }
  964. /* For shared files, check now if the object matches. */
  965. if (refcounting)
  966. tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
  967. fnamenewvb.buf, ti, nifd->namenode);
  968. /* If we didn't extract anything, there's nothing else to do. */
  969. if (refcounting && !fc_overwrite)
  970. return 0;
  971. tarobject_set_perms(ti, fnamenewvb.buf, st);
  972. tarobject_set_mtime(ti, fnamenewvb.buf);
  973. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, st->mode);
  974. /*
  975. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  976. * if the file already exists as a directory and we were trying to
  977. * extract a directory or symlink, we returned earlier, so we don't
  978. * need to worry about that here).
  979. *
  980. * The old file is still in the original filename,
  981. */
  982. /* First, check to see if it's a conffile. If so we don't install
  983. * it now - we leave it in .dpkg-new for --configure to take care of. */
  984. if (nifd->namenode->flags & fnnf_new_conff) {
  985. debug(dbg_conffdetail,"tarobject conffile extracted");
  986. nifd->namenode->flags |= fnnf_elide_other_lists;
  987. return 0;
  988. }
  989. /* Now we move the old file out of the way, the backup file will
  990. * be deleted later. */
  991. if (statr) {
  992. /* Don't try to back it up if it didn't exist. */
  993. debug(dbg_eachfiledetail,"tarobject new - no backup");
  994. } else {
  995. if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
  996. /* One of the two is a directory - can't do atomic install. */
  997. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  998. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  999. if (rename(fnamevb.buf,fnametmpvb.buf))
  1000. ohshite(_("unable to move aside `%.255s' to install new version"),
  1001. ti->name);
  1002. } else if (S_ISLNK(stab.st_mode)) {
  1003. /* We can't make a symlink with two hardlinks, so we'll have to
  1004. * copy it. (Pretend that making a copy of a symlink is the same
  1005. * as linking to it.) */
  1006. varbuf_reset(&symlinkfn);
  1007. varbuf_grow(&symlinkfn, stab.st_size + 1);
  1008. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  1009. if (r < 0)
  1010. ohshite(_("unable to read link `%.255s'"), ti->name);
  1011. else if (r != stab.st_size)
  1012. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  1013. fnamevb.buf, stab.st_size, r);
  1014. varbuf_trunc(&symlinkfn, r);
  1015. varbuf_end_str(&symlinkfn);
  1016. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  1017. ohshite(_("unable to make backup symlink for `%.255s'"), ti->name);
  1018. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  1019. ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name);
  1020. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  1021. } else {
  1022. debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
  1023. if (link(fnamevb.buf,fnametmpvb.buf))
  1024. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  1025. ti->name);
  1026. }
  1027. }
  1028. /*
  1029. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  1030. * in .dpkg-new.
  1031. */
  1032. if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
  1033. ti->type == TAR_FILETYPE_SYMLINK) {
  1034. nifd->namenode->flags |= fnnf_deferred_rename;
  1035. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  1036. } else {
  1037. if (rename(fnamenewvb.buf, fnamevb.buf))
  1038. ohshite(_("unable to install new version of `%.255s'"), ti->name);
  1039. /*
  1040. * CLEANUP: Now the new file is in the destination file, and the
  1041. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1042. * to take a different attitude to cleanup, because we need to
  1043. * remove the new file.
  1044. */
  1045. nifd->namenode->flags |= fnnf_placed_on_disk;
  1046. nifd->namenode->flags |= fnnf_elide_other_lists;
  1047. debug(dbg_eachfiledetail, "tarobject done and installed");
  1048. }
  1049. return 0;
  1050. }
  1051. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  1052. static void
  1053. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1054. {
  1055. struct fileinlist *cfile;
  1056. for (cfile = files; cfile; cfile = cfile->next) {
  1057. struct filenamenode *usenode;
  1058. int fd;
  1059. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  1060. continue;
  1061. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  1062. setupfnamevbs(usenode->name);
  1063. fd = open(fnamenewvb.buf, O_WRONLY);
  1064. if (fd < 0)
  1065. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1066. /* Ignore the return code as it should be considered equivalent to an
  1067. * asynchronous hint for the kernel, we are doing an fsync() later on
  1068. * anyway. */
  1069. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  1070. if (close(fd))
  1071. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1072. }
  1073. }
  1074. #else
  1075. static void
  1076. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1077. {
  1078. }
  1079. #endif
  1080. void
  1081. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  1082. {
  1083. struct fileinlist *cfile;
  1084. struct filenamenode *usenode;
  1085. tar_writeback_barrier(files, pkg);
  1086. for (cfile = files; cfile; cfile = cfile->next) {
  1087. debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
  1088. if (!(cfile->namenode->flags & fnnf_deferred_rename))
  1089. continue;
  1090. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  1091. setupfnamevbs(usenode->name);
  1092. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  1093. int fd;
  1094. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  1095. fd = open(fnamenewvb.buf, O_WRONLY);
  1096. if (fd < 0)
  1097. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1098. if (fsync(fd))
  1099. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  1100. if (close(fd))
  1101. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1102. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  1103. }
  1104. debug(dbg_eachfiledetail, "deferred extract needs rename");
  1105. if (rename(fnamenewvb.buf, fnamevb.buf))
  1106. ohshite(_("unable to install new version of `%.255s'"),
  1107. cfile->namenode->name);
  1108. cfile->namenode->flags &= ~fnnf_deferred_rename;
  1109. /*
  1110. * CLEANUP: Now the new file is in the destination file, and the
  1111. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1112. * to take a different attitude to cleanup, because we need to
  1113. * remove the new file.
  1114. */
  1115. cfile->namenode->flags |= fnnf_placed_on_disk;
  1116. cfile->namenode->flags |= fnnf_elide_other_lists;
  1117. debug(dbg_eachfiledetail, "deferred extract done and installed");
  1118. }
  1119. }
  1120. void
  1121. enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
  1122. {
  1123. struct pkg_deconf_list *newdeconf;
  1124. ensure_package_clientdata(pkg);
  1125. pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
  1126. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  1127. newdeconf->next = deconfigure;
  1128. newdeconf->pkg = pkg;
  1129. newdeconf->pkg_removal = pkg_removal;
  1130. deconfigure = newdeconf;
  1131. }
  1132. void
  1133. clear_deconfigure_queue(void)
  1134. {
  1135. struct pkg_deconf_list *deconf, *deconf_next;
  1136. for (deconf = deconfigure; deconf; deconf = deconf_next) {
  1137. deconf_next = deconf->next;
  1138. free(deconf);
  1139. }
  1140. deconfigure = NULL;
  1141. }
  1142. /**
  1143. * Try if we can deconfigure the package and queue it if so.
  1144. *
  1145. * Also checks whether the pdep is forced, first, according to force_p.
  1146. * force_p may be NULL in which case nothing is considered forced.
  1147. *
  1148. * Action is a string describing the action which causes the
  1149. * deconfiguration:
  1150. *
  1151. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  1152. * "installation of <package>" (due to Breaks; removal == NULL)
  1153. *
  1154. * @retval 0 Not possible (why is printed).
  1155. * @retval 1 Deconfiguration queued ok (no message printed).
  1156. * @retval 2 Forced (no deconfiguration needed, why is printed).
  1157. */
  1158. static int
  1159. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  1160. struct deppossi *pdep, const char *action,
  1161. struct pkginfo *removal, const char *why)
  1162. {
  1163. if (force_p && force_p(pdep)) {
  1164. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  1165. return 2;
  1166. } else if (f_autodeconf) {
  1167. if (pkg->installed.essential) {
  1168. if (fc_removeessential) {
  1169. warning(_("considering deconfiguration of essential\n"
  1170. " package %s, to enable %s"),
  1171. pkg_name(pkg, pnaw_nonambig), action);
  1172. } else {
  1173. notice(_("no, %s is essential, will not deconfigure\n"
  1174. " it in order to enable %s"),
  1175. pkg_name(pkg, pnaw_nonambig), action);
  1176. return 0;
  1177. }
  1178. }
  1179. enqueue_deconfigure(pkg, removal);
  1180. return 1;
  1181. } else {
  1182. notice(_("no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  1183. action, why);
  1184. return 0;
  1185. }
  1186. }
  1187. static int try_remove_can(struct deppossi *pdep,
  1188. struct pkginfo *fixbyrm,
  1189. const char *why) {
  1190. char action[512];
  1191. sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
  1192. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  1193. action, fixbyrm, why);
  1194. }
  1195. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  1196. const char *pfilename) {
  1197. struct pkginfo *fixbydeconf;
  1198. struct varbuf why = VARBUF_INIT;
  1199. int ok;
  1200. fixbydeconf = NULL;
  1201. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  1202. varbuf_destroy(&why);
  1203. return;
  1204. }
  1205. varbuf_end_str(&why);
  1206. if (fixbydeconf && f_autodeconf) {
  1207. char action[512];
  1208. ensure_package_clientdata(fixbydeconf);
  1209. assert(fixbydeconf->clientdata->istobe == PKG_ISTOBE_NORMAL);
  1210. sprintf(action, _("installation of %.250s"),
  1211. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1212. notice(_("considering deconfiguration of %s, which would be broken by %s ..."),
  1213. pkg_name(fixbydeconf, pnaw_nonambig), action);
  1214. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  1215. action, NULL, why.buf);
  1216. if (ok == 1) {
  1217. notice(_("yes, will deconfigure %s (broken by %s)"),
  1218. pkg_name(fixbydeconf, pnaw_nonambig),
  1219. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1220. }
  1221. } else {
  1222. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1223. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
  1224. ok= 0;
  1225. }
  1226. varbuf_destroy(&why);
  1227. if (ok > 0) return;
  1228. if (force_breaks(dep->list)) {
  1229. warning(_("ignoring breakage, may proceed anyway!"));
  1230. return;
  1231. }
  1232. if (fixbydeconf && !f_autodeconf) {
  1233. ohshit(_("installing %.250s would break %.250s, and\n"
  1234. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  1235. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1236. pkg_name(fixbydeconf, pnaw_nonambig));
  1237. } else {
  1238. ohshit(_("installing %.250s would break existing software"),
  1239. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1240. }
  1241. }
  1242. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  1243. const char *pfilename) {
  1244. struct pkginfo *fixbyrm;
  1245. struct deppossi *pdep, flagdeppossi;
  1246. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  1247. struct dependency *providecheck;
  1248. fixbyrm = NULL;
  1249. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  1250. varbuf_destroy(&conflictwhy);
  1251. varbuf_destroy(&removalwhy);
  1252. return;
  1253. }
  1254. if (fixbyrm) {
  1255. ensure_package_clientdata(fixbyrm);
  1256. if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
  1257. fixbyrm= dep->up;
  1258. ensure_package_clientdata(fixbyrm);
  1259. }
  1260. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  1261. (((fixbyrm->want != PKG_WANT_INSTALL &&
  1262. fixbyrm->want != PKG_WANT_HOLD) ||
  1263. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  1264. (!fixbyrm->installed.essential || fc_removeessential)))) {
  1265. assert(fixbyrm->clientdata->istobe == PKG_ISTOBE_NORMAL ||
  1266. fixbyrm->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
  1267. fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
  1268. notice(_("considering removing %s in favour of %s ..."),
  1269. pkg_name(fixbyrm, pnaw_nonambig),
  1270. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1271. if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
  1272. fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
  1273. fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
  1274. notice(_("%s is not properly installed; ignoring any dependencies on it"),
  1275. pkg_name(fixbyrm, pnaw_nonambig));
  1276. pdep = NULL;
  1277. } else {
  1278. for (pdep = fixbyrm->set->depended.installed;
  1279. pdep;
  1280. pdep = pdep->rev_next) {
  1281. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1282. continue;
  1283. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1284. continue;
  1285. varbuf_end_str(&removalwhy);
  1286. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1287. break;
  1288. }
  1289. if (!pdep) {
  1290. /* If we haven't found a reason not to yet, let's look some more. */
  1291. for (providecheck= fixbyrm->installed.depends;
  1292. providecheck;
  1293. providecheck= providecheck->next) {
  1294. if (providecheck->type != dep_provides) continue;
  1295. for (pdep = providecheck->list->ed->depended.installed;
  1296. pdep;
  1297. pdep = pdep->rev_next) {
  1298. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1299. continue;
  1300. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1301. continue;
  1302. varbuf_end_str(&removalwhy);
  1303. notice(_("may have trouble removing %s, as it provides %s ..."),
  1304. pkg_name(fixbyrm, pnaw_nonambig),
  1305. providecheck->list->ed->name);
  1306. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1307. goto break_from_both_loops_at_once;
  1308. }
  1309. }
  1310. break_from_both_loops_at_once:;
  1311. }
  1312. }
  1313. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1314. pdep= &flagdeppossi;
  1315. }
  1316. if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
  1317. if (fc_removereinstreq) {
  1318. notice(_("package %s requires reinstallation, but will "
  1319. "remove anyway as you requested"),
  1320. pkg_name(fixbyrm, pnaw_nonambig));
  1321. } else {
  1322. notice(_("package %s requires reinstallation, will not remove"),
  1323. pkg_name(fixbyrm, pnaw_nonambig));
  1324. pdep= &flagdeppossi;
  1325. }
  1326. }
  1327. if (!pdep) {
  1328. /* This conflict is OK - we'll remove the conflictor. */
  1329. enqueue_conflictor(fixbyrm);
  1330. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1331. notice(_("yes, will remove %s in favour of %s"),
  1332. pkg_name(fixbyrm, pnaw_nonambig),
  1333. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1334. return;
  1335. }
  1336. /* Put it back. */
  1337. fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
  1338. }
  1339. }
  1340. varbuf_end_str(&conflictwhy);
  1341. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1342. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
  1343. if (!force_conflicts(dep->list))
  1344. ohshit(_("conflicting packages - not installing %.250s"),
  1345. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1346. warning(_("ignoring conflict, may proceed anyway!"));
  1347. varbuf_destroy(&conflictwhy);
  1348. return;
  1349. }
  1350. void cu_cidir(int argc, void **argv) {
  1351. char *cidir= (char*)argv[0];
  1352. char *cidirrest= (char*)argv[1];
  1353. cidirrest[-1] = '\0';
  1354. ensure_pathname_nonexisting(cidir);
  1355. }
  1356. void cu_fileslist(int argc, void **argv) {
  1357. destroyobstack();
  1358. }
  1359. int
  1360. archivefiles(const char *const *argv)
  1361. {
  1362. const char *volatile thisarg;
  1363. const char *const *volatile argp;
  1364. jmp_buf ejbuf;
  1365. enum modstatdb_rw msdbflags;
  1366. trigproc_install_hooks();
  1367. if (f_noact)
  1368. msdbflags = msdbrw_readonly;
  1369. else if (cipaction->arg_int == act_avail)
  1370. msdbflags = msdbrw_readonly | msdbrw_available_write;
  1371. else if (fc_nonroot)
  1372. msdbflags = msdbrw_write;
  1373. else
  1374. msdbflags = msdbrw_needsuperuser;
  1375. modstatdb_open(msdbflags);
  1376. checkpath();
  1377. pkg_infodb_upgrade();
  1378. log_message("startup archives %s", cipaction->olong);
  1379. if (f_recursive) {
  1380. int pi[2], nfiles, c, i, rc;
  1381. pid_t pid;
  1382. FILE *pf;
  1383. static struct varbuf findoutput;
  1384. const char **arglist;
  1385. char *p;
  1386. if (!*argv)
  1387. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1388. m_pipe(pi);
  1389. pid = subproc_fork();
  1390. if (pid == 0) {
  1391. struct command cmd;
  1392. const char *const *ap;
  1393. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1394. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1395. command_add_args(&cmd, FIND, "-L", NULL);
  1396. for (ap = argv; *ap; ap++) {
  1397. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1398. char *a;
  1399. m_asprintf(&a, "./%s", *ap);
  1400. command_add_arg(&cmd, a);
  1401. } else {
  1402. command_add_arg(&cmd, (const char *)*ap);
  1403. }
  1404. }
  1405. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1406. command_exec(&cmd);
  1407. }
  1408. close(pi[1]);
  1409. nfiles= 0;
  1410. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1411. varbuf_reset(&findoutput);
  1412. while ((c= fgetc(pf)) != EOF) {
  1413. varbuf_add_char(&findoutput, c);
  1414. if (!c) nfiles++;
  1415. }
  1416. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1417. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1418. rc = subproc_reap(pid, "find", SUBPROC_RETERROR);
  1419. if (rc != 0)
  1420. ohshit(_("find for --recursive returned unhandled error %i"), rc);
  1421. if (!nfiles)
  1422. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1423. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1424. p = findoutput.buf;
  1425. for (i = 0; i < nfiles; i++) {
  1426. arglist[i] = p;
  1427. while (*p++ != '\0') ;
  1428. }
  1429. arglist[i] = NULL;
  1430. argp= arglist;
  1431. } else {
  1432. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1433. cipaction->olong);
  1434. argp= argv;
  1435. }
  1436. currenttime = time(NULL);
  1437. /* Initialize fname variables contents. */
  1438. varbuf_reset(&fnamevb);
  1439. varbuf_reset(&fnametmpvb);
  1440. varbuf_reset(&fnamenewvb);
  1441. varbuf_add_str(&fnamevb, instdir);
  1442. varbuf_add_str(&fnametmpvb, instdir);
  1443. varbuf_add_str(&fnamenewvb, instdir);
  1444. fnameidlu= fnamevb.used;
  1445. ensure_diversions();
  1446. ensure_statoverrides(STATDB_PARSE_NORMAL);
  1447. while ((thisarg = *argp++) != NULL) {
  1448. if (setjmp(ejbuf)) {
  1449. pop_error_context(ehflag_bombout);
  1450. if (abort_processing)
  1451. break;
  1452. continue;
  1453. }
  1454. push_error_context_jump(&ejbuf, print_error_perarchive, thisarg);
  1455. dpkg_selabel_load();
  1456. process_archive(thisarg);
  1457. onerr_abort++;
  1458. m_output(stdout, _("<standard output>"));
  1459. m_output(stderr, _("<standard error>"));
  1460. onerr_abort--;
  1461. pop_error_context(ehflag_normaltidy);
  1462. }
  1463. dpkg_selabel_close();
  1464. switch (cipaction->arg_int) {
  1465. case act_install:
  1466. case act_configure:
  1467. case act_triggers:
  1468. case act_remove:
  1469. case act_purge:
  1470. process_queue();
  1471. case act_unpack:
  1472. case act_avail:
  1473. break;
  1474. default:
  1475. internerr("unknown action '%d'", cipaction->arg_int);
  1476. }
  1477. trigproc_run_deferred();
  1478. modstatdb_shutdown();
  1479. return 0;
  1480. }
  1481. /**
  1482. * Decide whether we want to install a new version of the package.
  1483. *
  1484. * @param pkg The package with the version we might want to install
  1485. *
  1486. * @retval true If the package should be skipped.
  1487. * @retval false If the package should be installed.
  1488. */
  1489. bool
  1490. wanttoinstall(struct pkginfo *pkg)
  1491. {
  1492. int rc;
  1493. if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
  1494. if (f_alsoselect) {
  1495. printf(_("Selecting previously unselected package %s.\n"),
  1496. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1497. return true;
  1498. } else {
  1499. printf(_("Skipping unselected package %s.\n"),
  1500. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1501. return false;
  1502. }
  1503. }
  1504. if (pkg->eflag & PKG_EFLAG_REINSTREQ)
  1505. return true;
  1506. if (pkg->status < PKG_STAT_UNPACKED)
  1507. return true;
  1508. rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
  1509. if (rc > 0) {
  1510. return true;
  1511. } else if (rc == 0) {
  1512. /* Same version fully installed. */
  1513. if (f_skipsame) {
  1514. notice(_("version %.250s of %.250s already installed, skipping"),
  1515. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1516. pkg_name(pkg, pnaw_nonambig));
  1517. return false;
  1518. } else {
  1519. return true;
  1520. }
  1521. } else {
  1522. if (fc_downgrade) {
  1523. warning(_("downgrading %.250s from %.250s to %.250s"),
  1524. pkg_name(pkg, pnaw_nonambig),
  1525. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1526. versiondescribe(&pkg->available.version, vdew_nonambig));
  1527. return true;
  1528. } else {
  1529. notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
  1530. pkg_name(pkg, pnaw_nonambig),
  1531. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1532. versiondescribe(&pkg->available.version, vdew_nonambig));
  1533. return false;
  1534. }
  1535. }
  1536. }
  1537. struct fileinlist *
  1538. filenamenode_queue_push(struct filenamenode_queue *queue,
  1539. struct filenamenode *namenode)
  1540. {
  1541. struct fileinlist *node;
  1542. node = m_malloc(sizeof(*node));
  1543. node->next = NULL;
  1544. node->namenode = namenode;
  1545. *queue->tail = node;
  1546. queue->tail = &node->next;
  1547. return node;
  1548. }