archives.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759
  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 == 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 < 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 == 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. newhash = nfmalloc(MD5HASHLEN + 1);
  301. if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
  302. ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
  303. path_quote_filename(fnamebuf, te->name, 256),
  304. path_quote_filename(fnamenewbuf, fnamenewvb.buf, 256), err.str);
  305. namenode->newhash = newhash;
  306. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  307. tarobject_skip_padding(tc, te);
  308. fd_writeback_init(fd);
  309. if (namenode->statoverride)
  310. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  311. namenode->statoverride->uid,
  312. namenode->statoverride->gid,
  313. namenode->statoverride->mode);
  314. if (fchown(fd, st->uid, st->gid))
  315. ohshite(_("error setting ownership of `%.255s'"), te->name);
  316. if (fchmod(fd, st->mode & ~S_IFMT))
  317. ohshite(_("error setting permissions of `%.255s'"), te->name);
  318. /* Postpone the fsync, to try to avoid massive I/O degradation. */
  319. if (!fc_unsafe_io)
  320. namenode->flags |= fnnf_deferred_fsync;
  321. pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
  322. if (close(fd))
  323. ohshite(_("error closing/writing `%.255s'"), te->name);
  324. break;
  325. case TAR_FILETYPE_FIFO:
  326. if (mkfifo(path, 0))
  327. ohshite(_("error creating pipe `%.255s'"), te->name);
  328. debug(dbg_eachfiledetail, "tarobject fifo");
  329. break;
  330. case TAR_FILETYPE_CHARDEV:
  331. if (mknod(path, S_IFCHR, te->dev))
  332. ohshite(_("error creating device `%.255s'"), te->name);
  333. debug(dbg_eachfiledetail, "tarobject chardev");
  334. break;
  335. case TAR_FILETYPE_BLOCKDEV:
  336. if (mknod(path, S_IFBLK, te->dev))
  337. ohshite(_("error creating device `%.255s'"), te->name);
  338. debug(dbg_eachfiledetail, "tarobject blockdev");
  339. break;
  340. case TAR_FILETYPE_HARDLINK:
  341. varbuf_reset(&hardlinkfn);
  342. varbuf_add_str(&hardlinkfn, instdir);
  343. varbuf_add_char(&hardlinkfn, '/');
  344. linknode = findnamenode(te->linkname, 0);
  345. varbuf_add_str(&hardlinkfn,
  346. namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
  347. if (linknode->flags & (fnnf_deferred_rename | fnnf_new_conff))
  348. varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
  349. varbuf_end_str(&hardlinkfn);
  350. if (link(hardlinkfn.buf, path))
  351. ohshite(_("error creating hard link `%.255s'"), te->name);
  352. namenode->newhash = linknode->newhash;
  353. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  354. break;
  355. case TAR_FILETYPE_SYMLINK:
  356. /* We've already checked for an existing directory. */
  357. if (symlink(te->linkname, path))
  358. ohshite(_("error creating symbolic link `%.255s'"), te->name);
  359. debug(dbg_eachfiledetail, "tarobject symlink creating");
  360. break;
  361. case TAR_FILETYPE_DIR:
  362. /* We've already checked for an existing directory. */
  363. if (mkdir(path, 0))
  364. ohshite(_("error creating directory `%.255s'"), te->name);
  365. debug(dbg_eachfiledetail, "tarobject directory creating");
  366. break;
  367. default:
  368. internerr("unknown tar type '%d', but already checked", te->type);
  369. }
  370. }
  371. static void
  372. tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
  373. struct filenamenode *namenode)
  374. {
  375. if (te->type == TAR_FILETYPE_FILE) {
  376. struct dpkg_error err;
  377. char fnamebuf[256];
  378. char *newhash;
  379. newhash = nfmalloc(MD5HASHLEN + 1);
  380. if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
  381. ohshit(_("cannot compute MD5 hash for tar file '%.255s': %s"),
  382. path_quote_filename(fnamebuf, te->name, 256), err.str);
  383. tarobject_skip_padding(tc, te);
  384. namenode->newhash = newhash;
  385. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  386. } else if (te->type == TAR_FILETYPE_HARDLINK) {
  387. struct filenamenode *linknode;
  388. linknode = findnamenode(te->linkname, 0);
  389. namenode->newhash = linknode->newhash;
  390. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  391. }
  392. }
  393. static void
  394. tarobject_set_mtime(struct tar_entry *te, const char *path)
  395. {
  396. struct timeval tv[2];
  397. tv[0].tv_sec = currenttime;
  398. tv[0].tv_usec = 0;
  399. tv[1].tv_sec = te->mtime;
  400. tv[1].tv_usec = 0;
  401. if (te->type == TAR_FILETYPE_SYMLINK) {
  402. #ifdef HAVE_LUTIMES
  403. if (lutimes(path, tv) && errno != ENOSYS)
  404. ohshite(_("error setting timestamps of `%.255s'"), path);
  405. #endif
  406. } else {
  407. if (utimes(path, tv))
  408. ohshite(_("error setting timestamps of `%.255s'"), path);
  409. }
  410. }
  411. static void
  412. tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
  413. {
  414. if (te->type == TAR_FILETYPE_FILE)
  415. return; /* Already handled using the file descriptor. */
  416. if (te->type == TAR_FILETYPE_SYMLINK) {
  417. if (lchown(path, st->uid, st->gid))
  418. ohshite(_("error setting ownership of symlink `%.255s'"), path);
  419. } else {
  420. if (chown(path, st->uid, st->gid))
  421. ohshite(_("error setting ownership of `%.255s'"), path);
  422. if (chmod(path, st->mode & ~S_IFMT))
  423. ohshite(_("error setting permissions of `%.255s'"), path);
  424. }
  425. }
  426. #ifdef WITH_SELINUX
  427. static struct selabel_handle *dpkg_sehandle;
  428. static struct selabel_handle *
  429. dpkg_selabel_get_handle(void)
  430. {
  431. return dpkg_sehandle;
  432. }
  433. #endif
  434. static void
  435. dpkg_selabel_load(void)
  436. {
  437. #ifdef WITH_SELINUX
  438. static int selinux_enabled = -1;
  439. if (selinux_enabled < 0) {
  440. int rc;
  441. /* Set selinux_enabled if it is not already set (singleton). */
  442. selinux_enabled = (is_selinux_enabled() > 0);
  443. if (!selinux_enabled)
  444. return;
  445. /* Open the SELinux status notification channel, with fallback enabled
  446. * for older kernels. */
  447. rc = selinux_status_open(1);
  448. if (rc < 0)
  449. ohshit(_("cannot open security status notification channel"));
  450. /* XXX: We could use selinux_set_callback() to redirect the errors from
  451. * the other SELinux calls, but that does not seem worth it right now. */
  452. } else if (selinux_enabled && selinux_status_updated()) {
  453. /* The SELinux policy got updated in the kernel, usually after upgrading
  454. * the package shipping it, we need to reload. */
  455. selabel_close(dpkg_sehandle);
  456. } else {
  457. /* SELinux is either disabled or it does not need a reload. */
  458. return;
  459. }
  460. dpkg_sehandle = selabel_open(SELABEL_CTX_FILE, NULL, 0);
  461. if (dpkg_sehandle == NULL)
  462. ohshite(_("cannot get security labeling handle"));
  463. #endif
  464. }
  465. static void
  466. dpkg_selabel_close(void)
  467. {
  468. #ifdef WITH_SELINUX
  469. if (dpkg_sehandle == NULL)
  470. return;
  471. selinux_status_close();
  472. selabel_close(dpkg_sehandle);
  473. dpkg_sehandle = NULL;
  474. #endif
  475. }
  476. static void
  477. tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
  478. {
  479. #ifdef WITH_SELINUX
  480. struct selabel_handle *sehandle;
  481. security_context_t scontext = NULL;
  482. int ret;
  483. /* If there's no file type, just give up. */
  484. if ((mode & S_IFMT) == 0)
  485. return;
  486. /* If SELinux is not enabled just do nothing. */
  487. sehandle = dpkg_selabel_get_handle();
  488. if (sehandle == NULL)
  489. return;
  490. /*
  491. * We use the _raw function variants here so that no translation happens
  492. * from computer to human readable forms, to avoid issues when mcstransd
  493. * has disappeared during the unpack process.
  494. */
  495. /* Do nothing if we can't figure out what the context is, or if it has
  496. * no context; in which case the default context shall be applied. */
  497. ret = selabel_lookup_raw(sehandle, &scontext, matchpath, mode & S_IFMT);
  498. if (ret == -1 || (ret == 0 && scontext == NULL))
  499. return;
  500. ret = lsetfilecon_raw(path, scontext);
  501. if (ret < 0 && errno != ENOTSUP)
  502. ohshite(_("cannot set security context for file object '%s'"), path);
  503. freecon(scontext);
  504. #endif /* WITH_SELINUX */
  505. }
  506. static void
  507. tarobject_matches(struct tarcontext *tc,
  508. const char *fn_old, struct stat *stab, char *oldhash,
  509. const char *fn_new, struct tar_entry *te,
  510. struct filenamenode *namenode)
  511. {
  512. char *linkname;
  513. ssize_t linksize;
  514. debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
  515. switch (te->type) {
  516. case TAR_FILETYPE_DIR:
  517. /* Nothing to check for a new directory. */
  518. return;
  519. case TAR_FILETYPE_SYMLINK:
  520. /* Symlinks to existing dirs have already been dealt with, only
  521. * reamin real symlinks where we can compare the target. */
  522. if (!S_ISLNK(stab->st_mode))
  523. break;
  524. linkname = m_malloc(stab->st_size + 1);
  525. linksize = readlink(fn_old, linkname, stab->st_size + 1);
  526. if (linksize < 0)
  527. ohshite(_("unable to read link `%.255s'"), fn_old);
  528. else if (linksize != stab->st_size)
  529. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  530. fn_old, stab->st_size, linksize);
  531. linkname[linksize] = '\0';
  532. if (strcmp(linkname, te->linkname) == 0) {
  533. free(linkname);
  534. return;
  535. } else {
  536. free(linkname);
  537. }
  538. break;
  539. case TAR_FILETYPE_CHARDEV:
  540. if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
  541. return;
  542. break;
  543. case TAR_FILETYPE_BLOCKDEV:
  544. if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
  545. return;
  546. break;
  547. case TAR_FILETYPE_FIFO:
  548. if (S_ISFIFO(stab->st_mode))
  549. return;
  550. break;
  551. case TAR_FILETYPE_HARDLINK:
  552. /* Fall through. */
  553. case TAR_FILETYPE_FILE:
  554. /* Only check metadata for non-conffiles. */
  555. if (!(namenode->flags & fnnf_new_conff) &&
  556. !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
  557. break;
  558. if (strcmp(oldhash, namenode->newhash) == 0)
  559. return;
  560. break;
  561. default:
  562. internerr("unknown tar type '%d', but already checked", te->type);
  563. }
  564. forcibleerr(fc_overwrite,
  565. _("trying to overwrite shared '%.250s', which is different "
  566. "from other instances of package %.250s"),
  567. namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
  568. }
  569. void setupfnamevbs(const char *filename) {
  570. varbuf_trunc(&fnamevb, fnameidlu);
  571. varbuf_add_str(&fnamevb, filename);
  572. varbuf_end_str(&fnamevb);
  573. varbuf_trunc(&fnametmpvb, fnameidlu);
  574. varbuf_add_str(&fnametmpvb, filename);
  575. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  576. varbuf_end_str(&fnametmpvb);
  577. varbuf_trunc(&fnamenewvb, fnameidlu);
  578. varbuf_add_str(&fnamenewvb, filename);
  579. varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
  580. varbuf_end_str(&fnamenewvb);
  581. debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
  582. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  583. }
  584. /**
  585. * Securely remove a pathname.
  586. *
  587. * This is a secure version of remove(3) using secure_unlink() instead of
  588. * unlink(2).
  589. *
  590. * @retval 0 On success.
  591. * @retval -1 On failure, just like unlink(2) & rmdir(2).
  592. */
  593. int
  594. secure_remove(const char *filename)
  595. {
  596. int rc, e;
  597. if (!rmdir(filename)) {
  598. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir OK", filename);
  599. return 0;
  600. }
  601. if (errno != ENOTDIR) {
  602. e= errno;
  603. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir %s", filename,
  604. strerror(e));
  605. errno= e; return -1;
  606. }
  607. rc = secure_unlink(filename);
  608. e = errno;
  609. debug(dbg_eachfiledetail, "secure_remove '%s' unlink %s",
  610. filename, rc ? strerror(e) : "OK");
  611. errno = e;
  612. return rc;
  613. }
  614. struct fileinlist *addfiletolist(struct tarcontext *tc,
  615. struct filenamenode *namenode) {
  616. struct fileinlist *nifd;
  617. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  618. nifd->namenode= namenode;
  619. nifd->next = NULL;
  620. *tc->newfilesp = nifd;
  621. tc->newfilesp = &nifd->next;
  622. return nifd;
  623. }
  624. static void
  625. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  626. struct fileinlist **oldnifd,
  627. struct fileinlist *nifd)
  628. {
  629. obstack_free(&tar_obs, nifd);
  630. tc->newfilesp = oldnifd;
  631. *oldnifd = NULL;
  632. }
  633. static bool
  634. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  635. struct varbuf *symlinkfn)
  636. {
  637. struct stat oldstab, newstab;
  638. int statr;
  639. const char *lastslash;
  640. statr= stat(fname, &oldstab);
  641. if (statr) {
  642. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  643. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  644. fname);
  645. return false;
  646. }
  647. if (!S_ISDIR(oldstab.st_mode))
  648. return false;
  649. /* But is it to the same dir? */
  650. varbuf_reset(symlinkfn);
  651. if (ti->linkname[0] == '/') {
  652. varbuf_add_str(symlinkfn, instdir);
  653. } else {
  654. lastslash= strrchr(fname, '/');
  655. assert(lastslash);
  656. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  657. }
  658. varbuf_add_str(symlinkfn, ti->linkname);
  659. varbuf_end_str(symlinkfn);
  660. statr= stat(symlinkfn->buf, &newstab);
  661. if (statr) {
  662. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  663. ohshite(_("failed to stat (dereference) proposed new symlink target"
  664. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  665. return false;
  666. }
  667. if (!S_ISDIR(newstab.st_mode))
  668. return false;
  669. if (newstab.st_dev != oldstab.st_dev ||
  670. newstab.st_ino != oldstab.st_ino)
  671. return false;
  672. return true;
  673. }
  674. int
  675. tarobject(void *ctx, struct tar_entry *ti)
  676. {
  677. static struct varbuf conffderefn, symlinkfn;
  678. const char *usename;
  679. struct filenamenode *usenode;
  680. struct conffile *conff;
  681. struct tarcontext *tc = ctx;
  682. bool existingdir, keepexisting;
  683. bool refcounting;
  684. char oldhash[MD5HASHLEN + 1];
  685. int statr;
  686. ssize_t r;
  687. struct stat stab, stabtmp;
  688. struct file_stat *st;
  689. struct fileinlist *nifd, **oldnifd;
  690. struct pkgset *divpkgset;
  691. struct pkginfo *otherpkg;
  692. ensureobstackinit();
  693. /* Append to list of files.
  694. * The trailing ‘/’ put on the end of names in tarfiles has already
  695. * been stripped by tar_extractor(). */
  696. oldnifd= tc->newfilesp;
  697. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  698. nifd->namenode->flags |= fnnf_new_inarchive;
  699. debug(dbg_eachfile,
  700. "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
  701. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  702. ti->name, (long)ti->stat.mode,
  703. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  704. ti->type,
  705. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  706. ti->linkname,
  707. nifd->namenode->name, nifd->namenode->flags,
  708. nifd->namenode->divert && nifd->namenode->divert->useinstead
  709. ? nifd->namenode->divert->useinstead->name : "<none>");
  710. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  711. divpkgset = nifd->namenode->divert->pkgset;
  712. if (divpkgset) {
  713. forcibleerr(fc_overwritediverted,
  714. _("trying to overwrite `%.250s', which is the "
  715. "diverted version of `%.250s' (package: %.100s)"),
  716. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  717. divpkgset->name);
  718. } else {
  719. forcibleerr(fc_overwritediverted,
  720. _("trying to overwrite `%.250s', which is the "
  721. "diverted version of `%.250s'"),
  722. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  723. }
  724. }
  725. if (nifd->namenode->statoverride)
  726. st = nifd->namenode->statoverride;
  727. else
  728. st = &ti->stat;
  729. usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
  730. usename = usenode->name + 1; /* Skip the leading '/'. */
  731. trig_file_activate(usenode, tc->pkg);
  732. if (nifd->namenode->flags & fnnf_new_conff) {
  733. /* If it's a conffile we have to extract it next to the installed
  734. * version (i.e. we do the usual link-following). */
  735. if (conffderef(tc->pkg, &conffderefn, usename))
  736. usename= conffderefn.buf;
  737. debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
  738. }
  739. setupfnamevbs(usename);
  740. statr= lstat(fnamevb.buf,&stab);
  741. if (statr) {
  742. /* The lstat failed. */
  743. if (errno != ENOENT && errno != ENOTDIR)
  744. ohshite(_("unable to stat `%.255s' (which I was about to install)"),
  745. ti->name);
  746. /* OK, so it doesn't exist.
  747. * However, it's possible that we were in the middle of some other
  748. * backup/restore operation and were rudely interrupted.
  749. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  750. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  751. if (errno != ENOENT && errno != ENOTDIR)
  752. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  753. "installing another version"), ti->name);
  754. debug(dbg_eachfiledetail,"tarobject nonexistent");
  755. } else {
  756. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  757. statr= lstat(fnamevb.buf,&stab);
  758. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  759. " another version"), ti->name);
  760. }
  761. } else {
  762. debug(dbg_eachfiledetail,"tarobject already exists");
  763. }
  764. /* Check to see if it's a directory or link to one and we don't need to
  765. * do anything. This has to be done now so that we don't die due to
  766. * a file overwriting conflict. */
  767. existingdir = false;
  768. switch (ti->type) {
  769. case TAR_FILETYPE_SYMLINK:
  770. /* If it's already an existing directory, do nothing. */
  771. if (!statr && S_ISDIR(stab.st_mode)) {
  772. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  773. existingdir = true;
  774. } else if (!statr && S_ISLNK(stab.st_mode)) {
  775. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  776. existingdir = true;
  777. }
  778. break;
  779. case TAR_FILETYPE_DIR:
  780. /* If it's already an existing directory, do nothing. */
  781. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  782. debug(dbg_eachfiledetail, "tarobject directory exists");
  783. existingdir = true;
  784. }
  785. break;
  786. case TAR_FILETYPE_FILE:
  787. case TAR_FILETYPE_CHARDEV:
  788. case TAR_FILETYPE_BLOCKDEV:
  789. case TAR_FILETYPE_FIFO:
  790. case TAR_FILETYPE_HARDLINK:
  791. break;
  792. default:
  793. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),
  794. ti->name, ti->type);
  795. }
  796. keepexisting = false;
  797. refcounting = false;
  798. if (!existingdir) {
  799. struct filepackages_iterator *iter;
  800. iter = filepackages_iter_new(nifd->namenode);
  801. while ((otherpkg = filepackages_iter_next(iter))) {
  802. if (otherpkg == tc->pkg)
  803. continue;
  804. debug(dbg_eachfile, "tarobject ... found in %s",
  805. pkg_name(otherpkg, pnaw_always));
  806. /* A pkgset can share files between its instances. Overwriting
  807. * is allowed when they are not getting in sync, otherwise the
  808. * file content must match the installed file. */
  809. if (otherpkg->set == tc->pkg->set &&
  810. otherpkg->installed.multiarch == multiarch_same &&
  811. tc->pkg->available.multiarch == multiarch_same) {
  812. if (statr == 0 && tc->pkgset_getting_in_sync)
  813. refcounting = true;
  814. debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
  815. pkg_name(otherpkg, pnaw_always),
  816. versiondescribe(&otherpkg->installed.version, vdew_nonambig),
  817. tc->pkgset_getting_in_sync);
  818. continue;
  819. }
  820. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  821. /* Right, so we may be diverting this file. This makes the conflict
  822. * OK iff one of us is the diverting package (we don't need to
  823. * check for both being the diverting package, obviously). */
  824. divpkgset = nifd->namenode->divert->pkgset;
  825. debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
  826. divpkgset ? divpkgset->name : "<none>");
  827. if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
  828. continue;
  829. }
  830. /* If the new object is a directory and the previous object does
  831. * not exist assume it's also a directory and skip further checks.
  832. * XXX: Ideally with more information about the installed files we
  833. * could perform more clever checks. */
  834. if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
  835. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  836. continue;
  837. }
  838. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  839. switch (otherpkg->clientdata->replacingfilesandsaid) {
  840. case 2:
  841. keepexisting = true;
  842. case 1:
  843. continue;
  844. }
  845. /* Is the package with the conflicting file in the “config files only”
  846. * state? If so it must be a config file and we can silenty take it
  847. * over. */
  848. if (otherpkg->status == stat_configfiles)
  849. continue;
  850. /* Perhaps we're removing a conflicting package? */
  851. if (otherpkg->clientdata->istobe == itb_remove)
  852. continue;
  853. /* Is the file an obsolete conffile in the other package
  854. * and a conffile in the new package? */
  855. if ((nifd->namenode->flags & fnnf_new_conff) &&
  856. !statr && S_ISREG(stab.st_mode)) {
  857. for (conff = otherpkg->installed.conffiles;
  858. conff;
  859. conff = conff->next) {
  860. if (!conff->obsolete)
  861. continue;
  862. if (stat(conff->name, &stabtmp)) {
  863. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  864. continue;
  865. else
  866. ohshite(_("cannot stat file '%s'"), conff->name);
  867. }
  868. if (stabtmp.st_dev == stab.st_dev &&
  869. stabtmp.st_ino == stab.st_ino)
  870. break;
  871. }
  872. if (conff) {
  873. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  874. /* process_archive() will have copied its hash already. */
  875. continue;
  876. }
  877. }
  878. if (does_replace(tc->pkg, &tc->pkg->available,
  879. otherpkg, &otherpkg->installed)) {
  880. printf(_("Replacing files in old package %s (%s) ...\n"),
  881. pkg_name(otherpkg, pnaw_nonambig),
  882. versiondescribe(&otherpkg->installed.version, vdew_nonambig));
  883. otherpkg->clientdata->replacingfilesandsaid = 1;
  884. } else if (does_replace(otherpkg, &otherpkg->installed,
  885. tc->pkg, &tc->pkg->available)) {
  886. printf(_("Replaced by files in installed package %s (%s) ...\n"),
  887. pkg_name(otherpkg, pnaw_nonambig),
  888. versiondescribe(&otherpkg->installed.version, vdew_nonambig));
  889. otherpkg->clientdata->replacingfilesandsaid = 2;
  890. nifd->namenode->flags &= ~fnnf_new_inarchive;
  891. keepexisting = true;
  892. } else {
  893. /* At this point we are replacing something without a Replaces. */
  894. if (!statr && S_ISDIR(stab.st_mode)) {
  895. forcibleerr(fc_overwritedir,
  896. _("trying to overwrite directory '%.250s' "
  897. "in package %.250s %.250s with nondirectory"),
  898. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  899. versiondescribe(&otherpkg->installed.version,
  900. vdew_nonambig));
  901. } else {
  902. forcibleerr(fc_overwrite,
  903. _("trying to overwrite '%.250s', "
  904. "which is also in package %.250s %.250s"),
  905. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  906. versiondescribe(&otherpkg->installed.version,
  907. vdew_nonambig));
  908. }
  909. }
  910. }
  911. filepackages_iter_free(iter);
  912. }
  913. if (keepexisting) {
  914. if (nifd->namenode->flags & fnnf_new_conff)
  915. nifd->namenode->flags |= fnnf_obs_conff;
  916. remove_file_from_list(tc, ti, oldnifd, nifd);
  917. tarobject_skip_entry(tc, ti);
  918. return 0;
  919. }
  920. if (filter_should_skip(ti)) {
  921. nifd->namenode->flags &= ~fnnf_new_inarchive;
  922. nifd->namenode->flags |= fnnf_filtered;
  923. tarobject_skip_entry(tc, ti);
  924. return 0;
  925. }
  926. if (existingdir)
  927. return 0;
  928. /* Compute the hash of the previous object, before we might replace it
  929. * with the new version on forced overwrites. */
  930. if (refcounting) {
  931. debug(dbg_eachfiledetail, "tarobject hashing on-disk file '%s', refcounting",
  932. fnamevb.buf);
  933. if (nifd->namenode->flags & fnnf_new_conff) {
  934. md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
  935. } else if (S_ISREG(stab.st_mode)) {
  936. md5hash(tc->pkg, oldhash, fnamevb.buf);
  937. } else {
  938. strcpy(oldhash, EMPTYHASHFLAG);
  939. }
  940. }
  941. if (refcounting && !fc_overwrite) {
  942. /* If we are not forced to overwrite the path and are refcounting,
  943. * just compute the hash w/o extracting the object. */
  944. tarobject_hash(tc, ti, nifd->namenode);
  945. } else {
  946. /* Now, at this stage we want to make sure neither of .dpkg-new and
  947. * .dpkg-tmp are hanging around. */
  948. ensure_pathname_nonexisting(fnamenewvb.buf);
  949. ensure_pathname_nonexisting(fnametmpvb.buf);
  950. /* Now we start to do things that we need to be able to undo
  951. * if something goes wrong. Watch out for the CLEANUP comments to
  952. * keep an eye on what's installed on the disk at each point. */
  953. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
  954. /*
  955. * CLEANUP: Now we either have the old file on the disk, or not, in
  956. * its original filename.
  957. */
  958. /* Extract whatever it is as .dpkg-new ... */
  959. tarobject_extract(tc, ti, fnamenewvb.buf, st, nifd->namenode);
  960. }
  961. /* For shared files, check now if the object matches. */
  962. if (refcounting)
  963. tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
  964. fnamenewvb.buf, ti, nifd->namenode);
  965. /* If we didn't extract anything, there's nothing else to do. */
  966. if (refcounting && !fc_overwrite)
  967. return 0;
  968. tarobject_set_perms(ti, fnamenewvb.buf, st);
  969. tarobject_set_mtime(ti, fnamenewvb.buf);
  970. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, st->mode);
  971. /*
  972. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  973. * if the file already exists as a directory and we were trying to
  974. * extract a directory or symlink, we returned earlier, so we don't
  975. * need to worry about that here).
  976. *
  977. * The old file is still in the original filename,
  978. */
  979. /* First, check to see if it's a conffile. If so we don't install
  980. * it now - we leave it in .dpkg-new for --configure to take care of. */
  981. if (nifd->namenode->flags & fnnf_new_conff) {
  982. debug(dbg_conffdetail,"tarobject conffile extracted");
  983. nifd->namenode->flags |= fnnf_elide_other_lists;
  984. return 0;
  985. }
  986. /* Now we move the old file out of the way, the backup file will
  987. * be deleted later. */
  988. if (statr) {
  989. /* Don't try to back it up if it didn't exist. */
  990. debug(dbg_eachfiledetail,"tarobject new - no backup");
  991. } else {
  992. if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
  993. /* One of the two is a directory - can't do atomic install. */
  994. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  995. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  996. if (rename(fnamevb.buf,fnametmpvb.buf))
  997. ohshite(_("unable to move aside `%.255s' to install new version"),
  998. ti->name);
  999. } else if (S_ISLNK(stab.st_mode)) {
  1000. /* We can't make a symlink with two hardlinks, so we'll have to
  1001. * copy it. (Pretend that making a copy of a symlink is the same
  1002. * as linking to it.) */
  1003. varbuf_reset(&symlinkfn);
  1004. varbuf_grow(&symlinkfn, stab.st_size + 1);
  1005. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  1006. if (r < 0)
  1007. ohshite(_("unable to read link `%.255s'"), ti->name);
  1008. else if (r != stab.st_size)
  1009. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  1010. fnamevb.buf, stab.st_size, r);
  1011. varbuf_trunc(&symlinkfn, r);
  1012. varbuf_end_str(&symlinkfn);
  1013. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  1014. ohshite(_("unable to make backup symlink for `%.255s'"), ti->name);
  1015. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  1016. ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name);
  1017. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  1018. } else {
  1019. debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
  1020. if (link(fnamevb.buf,fnametmpvb.buf))
  1021. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  1022. ti->name);
  1023. }
  1024. }
  1025. /*
  1026. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  1027. * in .dpkg-new.
  1028. */
  1029. if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
  1030. ti->type == TAR_FILETYPE_SYMLINK) {
  1031. nifd->namenode->flags |= fnnf_deferred_rename;
  1032. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  1033. } else {
  1034. if (rename(fnamenewvb.buf, fnamevb.buf))
  1035. ohshite(_("unable to install new version of `%.255s'"), ti->name);
  1036. /*
  1037. * CLEANUP: Now the new file is in the destination file, and the
  1038. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1039. * to take a different attitude to cleanup, because we need to
  1040. * remove the new file.
  1041. */
  1042. nifd->namenode->flags |= fnnf_placed_on_disk;
  1043. nifd->namenode->flags |= fnnf_elide_other_lists;
  1044. debug(dbg_eachfiledetail, "tarobject done and installed");
  1045. }
  1046. return 0;
  1047. }
  1048. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  1049. static void
  1050. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1051. {
  1052. struct fileinlist *cfile;
  1053. for (cfile = files; cfile; cfile = cfile->next) {
  1054. struct filenamenode *usenode;
  1055. const char *usename;
  1056. int fd;
  1057. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  1058. continue;
  1059. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  1060. usename = usenode->name + 1; /* Skip the leading '/'. */
  1061. setupfnamevbs(usename);
  1062. fd = open(fnamenewvb.buf, O_WRONLY);
  1063. if (fd < 0)
  1064. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1065. /* Ignore the return code as it should be considered equivalent to an
  1066. * asynchronous hint for the kernel, we are doing an fsync() later on
  1067. * anyway. */
  1068. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  1069. if (close(fd))
  1070. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1071. }
  1072. }
  1073. #else
  1074. static void
  1075. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1076. {
  1077. }
  1078. #endif
  1079. void
  1080. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  1081. {
  1082. struct fileinlist *cfile;
  1083. struct filenamenode *usenode;
  1084. const char *usename;
  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. usename = usenode->name + 1; /* Skip the leading '/'. */
  1092. setupfnamevbs(usename);
  1093. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  1094. int fd;
  1095. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  1096. fd = open(fnamenewvb.buf, O_WRONLY);
  1097. if (fd < 0)
  1098. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1099. if (fsync(fd))
  1100. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  1101. if (close(fd))
  1102. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1103. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  1104. }
  1105. debug(dbg_eachfiledetail, "deferred extract needs rename");
  1106. if (rename(fnamenewvb.buf, fnamevb.buf))
  1107. ohshite(_("unable to install new version of `%.255s'"),
  1108. cfile->namenode->name);
  1109. cfile->namenode->flags &= ~fnnf_deferred_rename;
  1110. /*
  1111. * CLEANUP: Now the new file is in the destination file, and the
  1112. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1113. * to take a different attitude to cleanup, because we need to
  1114. * remove the new file.
  1115. */
  1116. cfile->namenode->flags |= fnnf_placed_on_disk;
  1117. cfile->namenode->flags |= fnnf_elide_other_lists;
  1118. debug(dbg_eachfiledetail, "deferred extract done and installed");
  1119. }
  1120. }
  1121. void
  1122. enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
  1123. {
  1124. struct pkg_deconf_list *newdeconf;
  1125. ensure_package_clientdata(pkg);
  1126. pkg->clientdata->istobe = itb_deconfigure;
  1127. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  1128. newdeconf->next = deconfigure;
  1129. newdeconf->pkg = pkg;
  1130. newdeconf->pkg_removal = pkg_removal;
  1131. deconfigure = newdeconf;
  1132. }
  1133. void
  1134. clear_deconfigure_queue(void)
  1135. {
  1136. struct pkg_deconf_list *deconf, *deconf_next;
  1137. for (deconf = deconfigure; deconf; deconf = deconf_next) {
  1138. deconf_next = deconf->next;
  1139. free(deconf);
  1140. }
  1141. deconfigure = NULL;
  1142. }
  1143. /**
  1144. * Try if we can deconfigure the package and queue it if so.
  1145. *
  1146. * Also checks whether the pdep is forced, first, according to force_p.
  1147. * force_p may be NULL in which case nothing is considered forced.
  1148. *
  1149. * Action is a string describing the action which causes the
  1150. * deconfiguration:
  1151. *
  1152. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  1153. * "installation of <package>" (due to Breaks; removal == NULL)
  1154. *
  1155. * @retval 0 Not possible (why is printed).
  1156. * @retval 1 Deconfiguration queued ok (no message printed).
  1157. * @retval 2 Forced (no deconfiguration needed, why is printed).
  1158. */
  1159. static int
  1160. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  1161. struct deppossi *pdep, const char *action,
  1162. struct pkginfo *removal, const char *why)
  1163. {
  1164. if (force_p && force_p(pdep)) {
  1165. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  1166. return 2;
  1167. } else if (f_autodeconf) {
  1168. if (pkg->installed.essential) {
  1169. if (fc_removeessential) {
  1170. warning(_("considering deconfiguration of essential\n"
  1171. " package %s, to enable %s"),
  1172. pkg_name(pkg, pnaw_nonambig), action);
  1173. } else {
  1174. notice(_("no, %s is essential, will not deconfigure\n"
  1175. " it in order to enable %s"),
  1176. pkg_name(pkg, pnaw_nonambig), action);
  1177. return 0;
  1178. }
  1179. }
  1180. enqueue_deconfigure(pkg, removal);
  1181. return 1;
  1182. } else {
  1183. notice(_("no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  1184. action, why);
  1185. return 0;
  1186. }
  1187. }
  1188. static int try_remove_can(struct deppossi *pdep,
  1189. struct pkginfo *fixbyrm,
  1190. const char *why) {
  1191. char action[512];
  1192. sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
  1193. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  1194. action, fixbyrm, why);
  1195. }
  1196. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  1197. const char *pfilename) {
  1198. struct pkginfo *fixbydeconf;
  1199. struct varbuf why = VARBUF_INIT;
  1200. int ok;
  1201. fixbydeconf = NULL;
  1202. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  1203. varbuf_destroy(&why);
  1204. return;
  1205. }
  1206. varbuf_end_str(&why);
  1207. if (fixbydeconf && f_autodeconf) {
  1208. char action[512];
  1209. ensure_package_clientdata(fixbydeconf);
  1210. assert(fixbydeconf->clientdata->istobe == itb_normal);
  1211. sprintf(action, _("installation of %.250s"),
  1212. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1213. notice(_("considering deconfiguration of %s, which would be broken by %s ..."),
  1214. pkg_name(fixbydeconf, pnaw_nonambig), action);
  1215. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  1216. action, NULL, why.buf);
  1217. if (ok == 1) {
  1218. notice(_("yes, will deconfigure %s (broken by %s)"),
  1219. pkg_name(fixbydeconf, pnaw_nonambig),
  1220. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1221. }
  1222. } else {
  1223. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1224. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
  1225. ok= 0;
  1226. }
  1227. varbuf_destroy(&why);
  1228. if (ok > 0) return;
  1229. if (force_breaks(dep->list)) {
  1230. warning(_("ignoring breakage, may proceed anyway!"));
  1231. return;
  1232. }
  1233. if (fixbydeconf && !f_autodeconf) {
  1234. ohshit(_("installing %.250s would break %.250s, and\n"
  1235. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  1236. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1237. pkg_name(fixbydeconf, pnaw_nonambig));
  1238. } else {
  1239. ohshit(_("installing %.250s would break existing software"),
  1240. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1241. }
  1242. }
  1243. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  1244. const char *pfilename) {
  1245. struct pkginfo *fixbyrm;
  1246. struct deppossi *pdep, flagdeppossi;
  1247. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  1248. struct dependency *providecheck;
  1249. fixbyrm = NULL;
  1250. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  1251. varbuf_destroy(&conflictwhy);
  1252. varbuf_destroy(&removalwhy);
  1253. return;
  1254. }
  1255. if (fixbyrm) {
  1256. ensure_package_clientdata(fixbyrm);
  1257. if (fixbyrm->clientdata->istobe == itb_installnew) {
  1258. fixbyrm= dep->up;
  1259. ensure_package_clientdata(fixbyrm);
  1260. }
  1261. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  1262. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  1263. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  1264. (!fixbyrm->installed.essential || fc_removeessential)))) {
  1265. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  1266. fixbyrm->clientdata->istobe= itb_remove;
  1267. notice(_("considering removing %s in favour of %s ..."),
  1268. pkg_name(fixbyrm, pnaw_nonambig),
  1269. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1270. if (!(fixbyrm->status == stat_installed ||
  1271. fixbyrm->status == stat_triggerspending ||
  1272. fixbyrm->status == stat_triggersawaited)) {
  1273. notice(_("%s is not properly installed; ignoring any dependencies on it"),
  1274. pkg_name(fixbyrm, pnaw_nonambig));
  1275. pdep = NULL;
  1276. } else {
  1277. for (pdep = fixbyrm->set->depended.installed;
  1278. pdep;
  1279. pdep = pdep->rev_next) {
  1280. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1281. continue;
  1282. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1283. continue;
  1284. varbuf_end_str(&removalwhy);
  1285. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1286. break;
  1287. }
  1288. if (!pdep) {
  1289. /* If we haven't found a reason not to yet, let's look some more. */
  1290. for (providecheck= fixbyrm->installed.depends;
  1291. providecheck;
  1292. providecheck= providecheck->next) {
  1293. if (providecheck->type != dep_provides) continue;
  1294. for (pdep = providecheck->list->ed->depended.installed;
  1295. pdep;
  1296. pdep = pdep->rev_next) {
  1297. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1298. continue;
  1299. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1300. continue;
  1301. varbuf_end_str(&removalwhy);
  1302. notice(_("may have trouble removing %s, as it provides %s ..."),
  1303. pkg_name(fixbyrm, pnaw_nonambig),
  1304. providecheck->list->ed->name);
  1305. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1306. goto break_from_both_loops_at_once;
  1307. }
  1308. }
  1309. break_from_both_loops_at_once:;
  1310. }
  1311. }
  1312. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1313. pdep= &flagdeppossi;
  1314. }
  1315. if (!pdep && (fixbyrm->eflag & eflag_reinstreq)) {
  1316. if (fc_removereinstreq) {
  1317. notice(_("package %s requires reinstallation, but will "
  1318. "remove anyway as you requested"),
  1319. pkg_name(fixbyrm, pnaw_nonambig));
  1320. } else {
  1321. notice(_("package %s requires reinstallation, will not remove"),
  1322. pkg_name(fixbyrm, pnaw_nonambig));
  1323. pdep= &flagdeppossi;
  1324. }
  1325. }
  1326. if (!pdep) {
  1327. /* This conflict is OK - we'll remove the conflictor. */
  1328. enqueue_conflictor(fixbyrm);
  1329. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1330. notice(_("yes, will remove %s in favour of %s"),
  1331. pkg_name(fixbyrm, pnaw_nonambig),
  1332. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1333. return;
  1334. }
  1335. /* Put it back. */
  1336. fixbyrm->clientdata->istobe = itb_normal;
  1337. }
  1338. }
  1339. varbuf_end_str(&conflictwhy);
  1340. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1341. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
  1342. if (!force_conflicts(dep->list))
  1343. ohshit(_("conflicting packages - not installing %.250s"),
  1344. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1345. warning(_("ignoring conflict, may proceed anyway!"));
  1346. varbuf_destroy(&conflictwhy);
  1347. return;
  1348. }
  1349. void cu_cidir(int argc, void **argv) {
  1350. char *cidir= (char*)argv[0];
  1351. char *cidirrest= (char*)argv[1];
  1352. cidirrest[-1] = '\0';
  1353. ensure_pathname_nonexisting(cidir);
  1354. }
  1355. void cu_fileslist(int argc, void **argv) {
  1356. destroyobstack();
  1357. }
  1358. int
  1359. archivefiles(const char *const *argv)
  1360. {
  1361. const char *volatile thisarg;
  1362. const char *const *volatile argp;
  1363. jmp_buf ejbuf;
  1364. trigproc_install_hooks();
  1365. modstatdb_open(f_noact ? msdbrw_readonly :
  1366. (cipaction->arg_int == act_avail ? msdbrw_readonly :
  1367. fc_nonroot ? msdbrw_write :
  1368. msdbrw_needsuperuser) |
  1369. msdbrw_available_write);
  1370. checkpath();
  1371. pkg_infodb_upgrade();
  1372. log_message("startup archives %s", cipaction->olong);
  1373. if (f_recursive) {
  1374. int pi[2], nfiles, c, i, rc;
  1375. pid_t pid;
  1376. FILE *pf;
  1377. static struct varbuf findoutput;
  1378. const char **arglist;
  1379. char *p;
  1380. if (!*argv)
  1381. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1382. m_pipe(pi);
  1383. pid = subproc_fork();
  1384. if (pid == 0) {
  1385. struct command cmd;
  1386. const char *const *ap;
  1387. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1388. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1389. command_add_args(&cmd, FIND, "-L", NULL);
  1390. for (ap = argv; *ap; ap++) {
  1391. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1392. char *a;
  1393. m_asprintf(&a, "./%s", *ap);
  1394. command_add_arg(&cmd, a);
  1395. } else {
  1396. command_add_arg(&cmd, (const char *)*ap);
  1397. }
  1398. }
  1399. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1400. command_exec(&cmd);
  1401. }
  1402. close(pi[1]);
  1403. nfiles= 0;
  1404. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1405. varbuf_reset(&findoutput);
  1406. while ((c= fgetc(pf)) != EOF) {
  1407. varbuf_add_char(&findoutput, c);
  1408. if (!c) nfiles++;
  1409. }
  1410. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1411. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1412. rc = subproc_wait_check(pid, "find", PROCNOERR);
  1413. if (rc != 0)
  1414. ohshit(_("find for --recursive returned unhandled error %i"), rc);
  1415. if (!nfiles)
  1416. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1417. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1418. p = findoutput.buf;
  1419. for (i = 0; i < nfiles; i++) {
  1420. arglist[i] = p;
  1421. while (*p++ != '\0') ;
  1422. }
  1423. arglist[i] = NULL;
  1424. argp= arglist;
  1425. } else {
  1426. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1427. cipaction->olong);
  1428. argp= argv;
  1429. }
  1430. currenttime = time(NULL);
  1431. /* Initialize fname variables contents. */
  1432. varbuf_reset(&fnamevb);
  1433. varbuf_reset(&fnametmpvb);
  1434. varbuf_reset(&fnamenewvb);
  1435. varbuf_add_str(&fnamevb, instdir);
  1436. varbuf_add_char(&fnamevb, '/');
  1437. varbuf_add_str(&fnametmpvb, instdir);
  1438. varbuf_add_char(&fnametmpvb, '/');
  1439. varbuf_add_str(&fnamenewvb, instdir);
  1440. varbuf_add_char(&fnamenewvb, '/');
  1441. fnameidlu= fnamevb.used;
  1442. ensure_diversions();
  1443. ensure_statoverrides();
  1444. while ((thisarg = *argp++) != NULL) {
  1445. if (setjmp(ejbuf)) {
  1446. pop_error_context(ehflag_bombout);
  1447. if (abort_processing)
  1448. break;
  1449. continue;
  1450. }
  1451. push_error_context_jump(&ejbuf, print_error_perarchive, thisarg);
  1452. dpkg_selabel_load();
  1453. process_archive(thisarg);
  1454. onerr_abort++;
  1455. m_output(stdout, _("<standard output>"));
  1456. m_output(stderr, _("<standard error>"));
  1457. onerr_abort--;
  1458. pop_error_context(ehflag_normaltidy);
  1459. }
  1460. dpkg_selabel_close();
  1461. switch (cipaction->arg_int) {
  1462. case act_install:
  1463. case act_configure:
  1464. case act_triggers:
  1465. case act_remove:
  1466. case act_purge:
  1467. process_queue();
  1468. case act_unpack:
  1469. case act_avail:
  1470. break;
  1471. default:
  1472. internerr("unknown action '%d'", cipaction->arg_int);
  1473. }
  1474. trigproc_run_deferred();
  1475. modstatdb_shutdown();
  1476. return 0;
  1477. }
  1478. /**
  1479. * Decide whether we want to install a new version of the package.
  1480. *
  1481. * @param pkg The package with the version we might want to install
  1482. *
  1483. * @retval true If the package should be skipped.
  1484. * @retval false If the package should be installed.
  1485. */
  1486. bool
  1487. wanttoinstall(struct pkginfo *pkg)
  1488. {
  1489. int rc;
  1490. if (pkg->want != want_install && pkg->want != want_hold) {
  1491. if (f_alsoselect) {
  1492. printf(_("Selecting previously unselected package %s.\n"),
  1493. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1494. return true;
  1495. } else {
  1496. printf(_("Skipping unselected package %s.\n"),
  1497. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1498. return false;
  1499. }
  1500. }
  1501. if (pkg->eflag & eflag_reinstreq)
  1502. return true;
  1503. if (pkg->status < stat_unpacked)
  1504. return true;
  1505. rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
  1506. if (rc > 0) {
  1507. return true;
  1508. } else if (rc == 0) {
  1509. /* Same version fully installed. */
  1510. if (f_skipsame) {
  1511. notice(_("version %.250s of %.250s already installed, skipping"),
  1512. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1513. pkg_name(pkg, pnaw_nonambig));
  1514. return false;
  1515. } else {
  1516. return true;
  1517. }
  1518. } else {
  1519. if (fc_downgrade) {
  1520. warning(_("downgrading %.250s from %.250s to %.250s"),
  1521. pkg_name(pkg, pnaw_nonambig),
  1522. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1523. versiondescribe(&pkg->available.version, vdew_nonambig));
  1524. return true;
  1525. } else {
  1526. notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
  1527. pkg_name(pkg, pnaw_nonambig),
  1528. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1529. versiondescribe(&pkg->available.version, vdew_nonambig));
  1530. return false;
  1531. }
  1532. }
  1533. }
  1534. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1535. struct filenamenode *namenode) {
  1536. struct fileinlist *newconff;
  1537. newconff= m_malloc(sizeof(struct fileinlist));
  1538. newconff->next = NULL;
  1539. newconff->namenode= namenode;
  1540. **newconffileslastp_io= newconff;
  1541. *newconffileslastp_io= &newconff->next;
  1542. return newconff;
  1543. }