archives.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753
  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-2012 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 <http://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. statusinfos[otherpkg->status].name, 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 r, 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. r = secure_unlink(filename);
  608. e = errno;
  609. debug(dbg_eachfiledetail, "secure_remove '%s' unlink %s",
  610. filename, r ? strerror(e) : "OK");
  611. errno= e; return r;
  612. }
  613. struct fileinlist *addfiletolist(struct tarcontext *tc,
  614. struct filenamenode *namenode) {
  615. struct fileinlist *nifd;
  616. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  617. nifd->namenode= namenode;
  618. nifd->next = NULL;
  619. *tc->newfilesp = nifd;
  620. tc->newfilesp = &nifd->next;
  621. return nifd;
  622. }
  623. static void
  624. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  625. struct fileinlist **oldnifd,
  626. struct fileinlist *nifd)
  627. {
  628. obstack_free(&tar_obs, nifd);
  629. tc->newfilesp = oldnifd;
  630. *oldnifd = NULL;
  631. }
  632. static bool
  633. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  634. struct varbuf *symlinkfn)
  635. {
  636. struct stat oldstab, newstab;
  637. int statr;
  638. const char *lastslash;
  639. statr= stat(fname, &oldstab);
  640. if (statr) {
  641. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  642. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  643. fname);
  644. return false;
  645. }
  646. if (!S_ISDIR(oldstab.st_mode))
  647. return false;
  648. /* But is it to the same dir? */
  649. varbuf_reset(symlinkfn);
  650. if (ti->linkname[0] == '/') {
  651. varbuf_add_str(symlinkfn, instdir);
  652. } else {
  653. lastslash= strrchr(fname, '/');
  654. assert(lastslash);
  655. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  656. }
  657. varbuf_add_str(symlinkfn, ti->linkname);
  658. varbuf_end_str(symlinkfn);
  659. statr= stat(symlinkfn->buf, &newstab);
  660. if (statr) {
  661. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  662. ohshite(_("failed to stat (dereference) proposed new symlink target"
  663. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  664. return false;
  665. }
  666. if (!S_ISDIR(newstab.st_mode))
  667. return false;
  668. if (newstab.st_dev != oldstab.st_dev ||
  669. newstab.st_ino != oldstab.st_ino)
  670. return false;
  671. return true;
  672. }
  673. int
  674. tarobject(void *ctx, struct tar_entry *ti)
  675. {
  676. static struct varbuf conffderefn, symlinkfn;
  677. const char *usename;
  678. struct filenamenode *usenode;
  679. struct conffile *conff;
  680. struct tarcontext *tc = ctx;
  681. bool existingdir, keepexisting;
  682. bool refcounting;
  683. char oldhash[MD5HASHLEN + 1];
  684. int statr;
  685. ssize_t r;
  686. struct stat stab, stabtmp;
  687. struct file_stat *st;
  688. struct fileinlist *nifd, **oldnifd;
  689. struct pkgset *divpkgset;
  690. struct pkginfo *otherpkg;
  691. ensureobstackinit();
  692. /* Append to list of files.
  693. * The trailing ‘/’ put on the end of names in tarfiles has already
  694. * been stripped by tar_extractor(). */
  695. oldnifd= tc->newfilesp;
  696. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  697. nifd->namenode->flags |= fnnf_new_inarchive;
  698. debug(dbg_eachfile,
  699. "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
  700. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  701. ti->name, (long)ti->stat.mode,
  702. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  703. ti->type,
  704. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  705. ti->linkname,
  706. nifd->namenode->name, nifd->namenode->flags,
  707. nifd->namenode->divert && nifd->namenode->divert->useinstead
  708. ? nifd->namenode->divert->useinstead->name : "<none>");
  709. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  710. divpkgset = nifd->namenode->divert->pkgset;
  711. if (divpkgset) {
  712. forcibleerr(fc_overwritediverted,
  713. _("trying to overwrite `%.250s', which is the "
  714. "diverted version of `%.250s' (package: %.100s)"),
  715. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  716. divpkgset->name);
  717. } else {
  718. forcibleerr(fc_overwritediverted,
  719. _("trying to overwrite `%.250s', which is the "
  720. "diverted version of `%.250s'"),
  721. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  722. }
  723. }
  724. if (nifd->namenode->statoverride)
  725. st = nifd->namenode->statoverride;
  726. else
  727. st = &ti->stat;
  728. usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
  729. usename = usenode->name + 1; /* Skip the leading '/'. */
  730. trig_file_activate(usenode, tc->pkg);
  731. if (nifd->namenode->flags & fnnf_new_conff) {
  732. /* If it's a conffile we have to extract it next to the installed
  733. * version (i.e. we do the usual link-following). */
  734. if (conffderef(tc->pkg, &conffderefn, usename))
  735. usename= conffderefn.buf;
  736. debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
  737. }
  738. setupfnamevbs(usename);
  739. statr= lstat(fnamevb.buf,&stab);
  740. if (statr) {
  741. /* The lstat failed. */
  742. if (errno != ENOENT && errno != ENOTDIR)
  743. ohshite(_("unable to stat `%.255s' (which I was about to install)"),
  744. ti->name);
  745. /* OK, so it doesn't exist.
  746. * However, it's possible that we were in the middle of some other
  747. * backup/restore operation and were rudely interrupted.
  748. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  749. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  750. if (errno != ENOENT && errno != ENOTDIR)
  751. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  752. "installing another version"), ti->name);
  753. debug(dbg_eachfiledetail,"tarobject nonexistent");
  754. } else {
  755. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  756. statr= lstat(fnamevb.buf,&stab);
  757. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  758. " another version"), ti->name);
  759. }
  760. } else {
  761. debug(dbg_eachfiledetail,"tarobject already exists");
  762. }
  763. /* Check to see if it's a directory or link to one and we don't need to
  764. * do anything. This has to be done now so that we don't die due to
  765. * a file overwriting conflict. */
  766. existingdir = false;
  767. switch (ti->type) {
  768. case tar_filetype_symlink:
  769. /* If it's already an existing directory, do nothing. */
  770. if (!statr && S_ISDIR(stab.st_mode)) {
  771. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  772. existingdir = true;
  773. } else if (!statr && S_ISLNK(stab.st_mode)) {
  774. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  775. existingdir = true;
  776. }
  777. break;
  778. case tar_filetype_dir:
  779. /* If it's already an existing directory, do nothing. */
  780. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  781. debug(dbg_eachfiledetail, "tarobject directory exists");
  782. existingdir = true;
  783. }
  784. break;
  785. case tar_filetype_file:
  786. case tar_filetype_chardev:
  787. case tar_filetype_blockdev:
  788. case tar_filetype_fifo:
  789. case tar_filetype_hardlink:
  790. break;
  791. default:
  792. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),
  793. ti->name, ti->type);
  794. }
  795. keepexisting = false;
  796. refcounting = false;
  797. if (!existingdir) {
  798. struct filepackages_iterator *iter;
  799. iter = filepackages_iter_new(nifd->namenode);
  800. while ((otherpkg = filepackages_iter_next(iter))) {
  801. if (otherpkg == tc->pkg)
  802. continue;
  803. debug(dbg_eachfile, "tarobject ... found in %s",
  804. pkg_name(otherpkg, pnaw_always));
  805. /* A pkgset can share files between its instances. Overwriting
  806. * is allowed when they are not getting in sync, otherwise the
  807. * file content must match the installed file. */
  808. if (otherpkg->set == tc->pkg->set &&
  809. otherpkg->installed.multiarch == multiarch_same &&
  810. tc->pkg->available.multiarch == multiarch_same) {
  811. if (statr == 0 && tc->pkgset_getting_in_sync)
  812. refcounting = true;
  813. debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
  814. pkg_name(otherpkg, pnaw_always),
  815. versiondescribe(&otherpkg->installed.version, vdew_nonambig),
  816. tc->pkgset_getting_in_sync);
  817. continue;
  818. }
  819. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  820. /* Right, so we may be diverting this file. This makes the conflict
  821. * OK iff one of us is the diverting package (we don't need to
  822. * check for both being the diverting package, obviously). */
  823. divpkgset = nifd->namenode->divert->pkgset;
  824. debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
  825. divpkgset ? divpkgset->name : "<none>");
  826. if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
  827. continue;
  828. }
  829. /* If the new object is a directory and the previous object does
  830. * not exist assume it's also a directory and skip further checks.
  831. * XXX: Ideally with more information about the installed files we
  832. * could perform more clever checks. */
  833. if (statr != 0 && ti->type == tar_filetype_dir) {
  834. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  835. continue;
  836. }
  837. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  838. switch (otherpkg->clientdata->replacingfilesandsaid) {
  839. case 2:
  840. keepexisting = true;
  841. case 1:
  842. continue;
  843. }
  844. /* Is the package with the conflicting file in the “config files only”
  845. * state? If so it must be a config file and we can silenty take it
  846. * over. */
  847. if (otherpkg->status == stat_configfiles)
  848. continue;
  849. /* Perhaps we're removing a conflicting package? */
  850. if (otherpkg->clientdata->istobe == itb_remove)
  851. continue;
  852. /* Is the file an obsolete conffile in the other package
  853. * and a conffile in the new package? */
  854. if ((nifd->namenode->flags & fnnf_new_conff) &&
  855. !statr && S_ISREG(stab.st_mode)) {
  856. for (conff = otherpkg->installed.conffiles;
  857. conff;
  858. conff = conff->next) {
  859. if (!conff->obsolete)
  860. continue;
  861. if (stat(conff->name, &stabtmp)) {
  862. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  863. continue;
  864. else
  865. ohshite(_("cannot stat file '%s'"), conff->name);
  866. }
  867. if (stabtmp.st_dev == stab.st_dev &&
  868. stabtmp.st_ino == stab.st_ino)
  869. break;
  870. }
  871. if (conff) {
  872. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  873. /* process_archive() will have copied its hash already. */
  874. continue;
  875. }
  876. }
  877. if (does_replace(tc->pkg, &tc->pkg->available,
  878. otherpkg, &otherpkg->installed)) {
  879. printf(_("Replacing files in old package %s ...\n"),
  880. pkg_name(otherpkg, pnaw_nonambig));
  881. otherpkg->clientdata->replacingfilesandsaid = 1;
  882. } else if (does_replace(otherpkg, &otherpkg->installed,
  883. tc->pkg, &tc->pkg->available)) {
  884. printf(_("Replaced by files in installed package %s ...\n"),
  885. pkg_name(otherpkg, pnaw_nonambig));
  886. otherpkg->clientdata->replacingfilesandsaid = 2;
  887. nifd->namenode->flags &= ~fnnf_new_inarchive;
  888. keepexisting = true;
  889. } else {
  890. /* At this point we are replacing something without a Replaces. */
  891. if (!statr && S_ISDIR(stab.st_mode)) {
  892. forcibleerr(fc_overwritedir,
  893. _("trying to overwrite directory '%.250s' "
  894. "in package %.250s %.250s with nondirectory"),
  895. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  896. versiondescribe(&otherpkg->installed.version,
  897. vdew_nonambig));
  898. } else {
  899. forcibleerr(fc_overwrite,
  900. _("trying to overwrite '%.250s', "
  901. "which is also in package %.250s %.250s"),
  902. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  903. versiondescribe(&otherpkg->installed.version,
  904. vdew_nonambig));
  905. }
  906. }
  907. }
  908. filepackages_iter_free(iter);
  909. }
  910. if (keepexisting) {
  911. if (nifd->namenode->flags & fnnf_new_conff)
  912. nifd->namenode->flags |= fnnf_obs_conff;
  913. remove_file_from_list(tc, ti, oldnifd, nifd);
  914. tarobject_skip_entry(tc, ti);
  915. return 0;
  916. }
  917. if (filter_should_skip(ti)) {
  918. nifd->namenode->flags &= ~fnnf_new_inarchive;
  919. nifd->namenode->flags |= fnnf_filtered;
  920. tarobject_skip_entry(tc, ti);
  921. return 0;
  922. }
  923. if (existingdir)
  924. return 0;
  925. /* Compute the hash of the previous object, before we might replace it
  926. * with the new version on forced overwrites. */
  927. if (refcounting) {
  928. if (nifd->namenode->flags & fnnf_new_conff) {
  929. md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
  930. } else if (S_ISREG(stab.st_mode)) {
  931. md5hash(tc->pkg, oldhash, fnamevb.buf);
  932. } else {
  933. strcpy(oldhash, EMPTYHASHFLAG);
  934. }
  935. }
  936. if (refcounting && !fc_overwrite) {
  937. /* If we are not forced to overwrite the path and are refcounting,
  938. * just compute the hash w/o extracting the object. */
  939. tarobject_hash(tc, ti, nifd->namenode);
  940. } else {
  941. /* Now, at this stage we want to make sure neither of .dpkg-new and
  942. * .dpkg-tmp are hanging around. */
  943. ensure_pathname_nonexisting(fnamenewvb.buf);
  944. ensure_pathname_nonexisting(fnametmpvb.buf);
  945. /* Now we start to do things that we need to be able to undo
  946. * if something goes wrong. Watch out for the CLEANUP comments to
  947. * keep an eye on what's installed on the disk at each point. */
  948. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
  949. /*
  950. * CLEANUP: Now we either have the old file on the disk, or not, in
  951. * its original filename.
  952. */
  953. /* Extract whatever it is as .dpkg-new ... */
  954. tarobject_extract(tc, ti, fnamenewvb.buf, st, nifd->namenode);
  955. }
  956. /* For shared files, check now if the object matches. */
  957. if (refcounting)
  958. tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
  959. fnamenewvb.buf, ti, nifd->namenode);
  960. /* If we didn't extract anything, there's nothing else to do. */
  961. if (refcounting && !fc_overwrite)
  962. return 0;
  963. tarobject_set_perms(ti, fnamenewvb.buf, st);
  964. tarobject_set_mtime(ti, fnamenewvb.buf);
  965. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, st->mode);
  966. /*
  967. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  968. * if the file already exists as a directory and we were trying to
  969. * extract a directory or symlink, we returned earlier, so we don't
  970. * need to worry about that here).
  971. *
  972. * The old file is still in the original filename,
  973. */
  974. /* First, check to see if it's a conffile. If so we don't install
  975. * it now - we leave it in .dpkg-new for --configure to take care of. */
  976. if (nifd->namenode->flags & fnnf_new_conff) {
  977. debug(dbg_conffdetail,"tarobject conffile extracted");
  978. nifd->namenode->flags |= fnnf_elide_other_lists;
  979. return 0;
  980. }
  981. /* Now we move the old file out of the way, the backup file will
  982. * be deleted later. */
  983. if (statr) {
  984. /* Don't try to back it up if it didn't exist. */
  985. debug(dbg_eachfiledetail,"tarobject new - no backup");
  986. } else {
  987. if (ti->type == tar_filetype_dir || S_ISDIR(stab.st_mode)) {
  988. /* One of the two is a directory - can't do atomic install. */
  989. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  990. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  991. if (rename(fnamevb.buf,fnametmpvb.buf))
  992. ohshite(_("unable to move aside `%.255s' to install new version"),
  993. ti->name);
  994. } else if (S_ISLNK(stab.st_mode)) {
  995. /* We can't make a symlink with two hardlinks, so we'll have to
  996. * copy it. (Pretend that making a copy of a symlink is the same
  997. * as linking to it.) */
  998. varbuf_reset(&symlinkfn);
  999. varbuf_grow(&symlinkfn, stab.st_size + 1);
  1000. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  1001. if (r < 0)
  1002. ohshite(_("unable to read link `%.255s'"), ti->name);
  1003. else if (r != stab.st_size)
  1004. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  1005. fnamevb.buf, stab.st_size, r);
  1006. varbuf_trunc(&symlinkfn, r);
  1007. varbuf_end_str(&symlinkfn);
  1008. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  1009. ohshite(_("unable to make backup symlink for `%.255s'"), ti->name);
  1010. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  1011. ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name);
  1012. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  1013. } else {
  1014. debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
  1015. if (link(fnamevb.buf,fnametmpvb.buf))
  1016. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  1017. ti->name);
  1018. }
  1019. }
  1020. /*
  1021. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  1022. * in .dpkg-new.
  1023. */
  1024. if (ti->type == tar_filetype_file || ti->type == tar_filetype_hardlink ||
  1025. ti->type == tar_filetype_symlink) {
  1026. nifd->namenode->flags |= fnnf_deferred_rename;
  1027. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  1028. } else {
  1029. if (rename(fnamenewvb.buf, fnamevb.buf))
  1030. ohshite(_("unable to install new version of `%.255s'"), ti->name);
  1031. /*
  1032. * CLEANUP: Now the new file is in the destination file, and the
  1033. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1034. * to take a different attitude to cleanup, because we need to
  1035. * remove the new file.
  1036. */
  1037. nifd->namenode->flags |= fnnf_placed_on_disk;
  1038. nifd->namenode->flags |= fnnf_elide_other_lists;
  1039. debug(dbg_eachfiledetail, "tarobject done and installed");
  1040. }
  1041. return 0;
  1042. }
  1043. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  1044. static void
  1045. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1046. {
  1047. struct fileinlist *cfile;
  1048. for (cfile = files; cfile; cfile = cfile->next) {
  1049. struct filenamenode *usenode;
  1050. const char *usename;
  1051. int fd;
  1052. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  1053. continue;
  1054. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  1055. usename = usenode->name + 1; /* Skip the leading '/'. */
  1056. setupfnamevbs(usename);
  1057. fd = open(fnamenewvb.buf, O_WRONLY);
  1058. if (fd < 0)
  1059. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1060. /* Ignore the return code as it should be considered equivalent to an
  1061. * asynchronous hint for the kernel, we are doing an fsync() later on
  1062. * anyway. */
  1063. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  1064. if (close(fd))
  1065. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1066. }
  1067. }
  1068. #else
  1069. static void
  1070. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  1071. {
  1072. }
  1073. #endif
  1074. void
  1075. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  1076. {
  1077. struct fileinlist *cfile;
  1078. struct filenamenode *usenode;
  1079. const char *usename;
  1080. tar_writeback_barrier(files, pkg);
  1081. for (cfile = files; cfile; cfile = cfile->next) {
  1082. debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
  1083. if (!(cfile->namenode->flags & fnnf_deferred_rename))
  1084. continue;
  1085. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  1086. usename = usenode->name + 1; /* Skip the leading '/'. */
  1087. setupfnamevbs(usename);
  1088. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  1089. int fd;
  1090. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  1091. fd = open(fnamenewvb.buf, O_WRONLY);
  1092. if (fd < 0)
  1093. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  1094. if (fsync(fd))
  1095. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  1096. if (close(fd))
  1097. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  1098. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  1099. }
  1100. debug(dbg_eachfiledetail, "deferred extract needs rename");
  1101. if (rename(fnamenewvb.buf, fnamevb.buf))
  1102. ohshite(_("unable to install new version of `%.255s'"),
  1103. cfile->namenode->name);
  1104. cfile->namenode->flags &= ~fnnf_deferred_rename;
  1105. /*
  1106. * CLEANUP: Now the new file is in the destination file, and the
  1107. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1108. * to take a different attitude to cleanup, because we need to
  1109. * remove the new file.
  1110. */
  1111. cfile->namenode->flags |= fnnf_placed_on_disk;
  1112. cfile->namenode->flags |= fnnf_elide_other_lists;
  1113. debug(dbg_eachfiledetail, "deferred extract done and installed");
  1114. }
  1115. }
  1116. void
  1117. enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
  1118. {
  1119. struct pkg_deconf_list *newdeconf;
  1120. ensure_package_clientdata(pkg);
  1121. pkg->clientdata->istobe = itb_deconfigure;
  1122. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  1123. newdeconf->next = deconfigure;
  1124. newdeconf->pkg = pkg;
  1125. newdeconf->pkg_removal = pkg_removal;
  1126. deconfigure = newdeconf;
  1127. }
  1128. void
  1129. clear_deconfigure_queue(void)
  1130. {
  1131. struct pkg_deconf_list *deconf, *deconf_next;
  1132. for (deconf = deconfigure; deconf; deconf = deconf_next) {
  1133. deconf_next = deconf->next;
  1134. free(deconf);
  1135. }
  1136. deconfigure = NULL;
  1137. }
  1138. /**
  1139. * Try if we can deconfigure the package and queue it if so.
  1140. *
  1141. * Also checks whether the pdep is forced, first, according to force_p.
  1142. * force_p may be NULL in which case nothing is considered forced.
  1143. *
  1144. * Action is a string describing the action which causes the
  1145. * deconfiguration:
  1146. *
  1147. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  1148. * "installation of <package>" (due to Breaks; removal == NULL)
  1149. *
  1150. * @retval 0 Not possible (why is printed).
  1151. * @retval 1 Deconfiguration queued ok (no message printed).
  1152. * @retval 2 Forced (no deconfiguration needed, why is printed).
  1153. */
  1154. static int
  1155. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  1156. struct deppossi *pdep, const char *action,
  1157. struct pkginfo *removal, const char *why)
  1158. {
  1159. if (force_p && force_p(pdep)) {
  1160. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  1161. return 2;
  1162. } else if (f_autodeconf) {
  1163. if (pkg->installed.essential) {
  1164. if (fc_removeessential) {
  1165. warning(_("considering deconfiguration of essential\n"
  1166. " package %s, to enable %s"),
  1167. pkg_name(pkg, pnaw_nonambig), action);
  1168. } else {
  1169. notice(_("no, %s is essential, will not deconfigure\n"
  1170. " it in order to enable %s"),
  1171. pkg_name(pkg, pnaw_nonambig), action);
  1172. return 0;
  1173. }
  1174. }
  1175. enqueue_deconfigure(pkg, removal);
  1176. return 1;
  1177. } else {
  1178. notice(_("no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  1179. action, why);
  1180. return 0;
  1181. }
  1182. }
  1183. static int try_remove_can(struct deppossi *pdep,
  1184. struct pkginfo *fixbyrm,
  1185. const char *why) {
  1186. char action[512];
  1187. sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
  1188. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  1189. action, fixbyrm, why);
  1190. }
  1191. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  1192. const char *pfilename) {
  1193. struct pkginfo *fixbydeconf;
  1194. struct varbuf why = VARBUF_INIT;
  1195. int ok;
  1196. fixbydeconf = NULL;
  1197. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  1198. varbuf_destroy(&why);
  1199. return;
  1200. }
  1201. varbuf_end_str(&why);
  1202. if (fixbydeconf && f_autodeconf) {
  1203. char action[512];
  1204. ensure_package_clientdata(fixbydeconf);
  1205. assert(fixbydeconf->clientdata->istobe == itb_normal);
  1206. sprintf(action, _("installation of %.250s"),
  1207. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1208. notice(_("considering deconfiguration of %s, which would be broken by %s ..."),
  1209. pkg_name(fixbydeconf, pnaw_nonambig), action);
  1210. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  1211. action, NULL, why.buf);
  1212. if (ok == 1) {
  1213. notice(_("yes, will deconfigure %s (broken by %s)"),
  1214. pkg_name(fixbydeconf, pnaw_nonambig),
  1215. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1216. }
  1217. } else {
  1218. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1219. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
  1220. ok= 0;
  1221. }
  1222. varbuf_destroy(&why);
  1223. if (ok > 0) return;
  1224. if (force_breaks(dep->list)) {
  1225. warning(_("ignoring breakage, may proceed anyway!"));
  1226. return;
  1227. }
  1228. if (fixbydeconf && !f_autodeconf) {
  1229. ohshit(_("installing %.250s would break %.250s, and\n"
  1230. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  1231. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1232. pkg_name(fixbydeconf, pnaw_nonambig));
  1233. } else {
  1234. ohshit(_("installing %.250s would break existing software"),
  1235. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1236. }
  1237. }
  1238. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  1239. const char *pfilename) {
  1240. struct pkginfo *fixbyrm;
  1241. struct deppossi *pdep, flagdeppossi;
  1242. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  1243. struct dependency *providecheck;
  1244. fixbyrm = NULL;
  1245. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  1246. varbuf_destroy(&conflictwhy);
  1247. varbuf_destroy(&removalwhy);
  1248. return;
  1249. }
  1250. if (fixbyrm) {
  1251. ensure_package_clientdata(fixbyrm);
  1252. if (fixbyrm->clientdata->istobe == itb_installnew) {
  1253. fixbyrm= dep->up;
  1254. ensure_package_clientdata(fixbyrm);
  1255. }
  1256. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  1257. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  1258. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  1259. (!fixbyrm->installed.essential || fc_removeessential)))) {
  1260. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  1261. fixbyrm->clientdata->istobe= itb_remove;
  1262. notice(_("considering removing %s in favour of %s ..."),
  1263. pkg_name(fixbyrm, pnaw_nonambig),
  1264. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1265. if (!(fixbyrm->status == stat_installed ||
  1266. fixbyrm->status == stat_triggerspending ||
  1267. fixbyrm->status == stat_triggersawaited)) {
  1268. notice(_("%s is not properly installed; ignoring any dependencies on it"),
  1269. pkg_name(fixbyrm, pnaw_nonambig));
  1270. pdep = NULL;
  1271. } else {
  1272. for (pdep = fixbyrm->set->depended.installed;
  1273. pdep;
  1274. pdep = pdep->rev_next) {
  1275. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1276. continue;
  1277. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1278. continue;
  1279. varbuf_end_str(&removalwhy);
  1280. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1281. break;
  1282. }
  1283. if (!pdep) {
  1284. /* If we haven't found a reason not to yet, let's look some more. */
  1285. for (providecheck= fixbyrm->installed.depends;
  1286. providecheck;
  1287. providecheck= providecheck->next) {
  1288. if (providecheck->type != dep_provides) continue;
  1289. for (pdep = providecheck->list->ed->depended.installed;
  1290. pdep;
  1291. pdep = pdep->rev_next) {
  1292. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1293. continue;
  1294. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1295. continue;
  1296. varbuf_end_str(&removalwhy);
  1297. notice(_("may have trouble removing %s, as it provides %s ..."),
  1298. pkg_name(fixbyrm, pnaw_nonambig),
  1299. providecheck->list->ed->name);
  1300. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1301. goto break_from_both_loops_at_once;
  1302. }
  1303. }
  1304. break_from_both_loops_at_once:;
  1305. }
  1306. }
  1307. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1308. pdep= &flagdeppossi;
  1309. }
  1310. if (!pdep && (fixbyrm->eflag & eflag_reinstreq)) {
  1311. if (fc_removereinstreq) {
  1312. notice(_("package %s requires reinstallation, but will "
  1313. "remove anyway as you requested"),
  1314. pkg_name(fixbyrm, pnaw_nonambig));
  1315. } else {
  1316. notice(_("package %s requires reinstallation, will not remove"),
  1317. pkg_name(fixbyrm, pnaw_nonambig));
  1318. pdep= &flagdeppossi;
  1319. }
  1320. }
  1321. if (!pdep) {
  1322. /* This conflict is OK - we'll remove the conflictor. */
  1323. enqueue_conflictor(pkg, fixbyrm);
  1324. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1325. notice(_("yes, will remove %s in favour of %s"),
  1326. pkg_name(fixbyrm, pnaw_nonambig),
  1327. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1328. return;
  1329. }
  1330. /* Put it back. */
  1331. fixbyrm->clientdata->istobe = itb_normal;
  1332. }
  1333. }
  1334. varbuf_end_str(&conflictwhy);
  1335. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1336. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
  1337. if (!force_conflicts(dep->list))
  1338. ohshit(_("conflicting packages - not installing %.250s"),
  1339. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1340. warning(_("ignoring conflict, may proceed anyway!"));
  1341. varbuf_destroy(&conflictwhy);
  1342. return;
  1343. }
  1344. void cu_cidir(int argc, void **argv) {
  1345. char *cidir= (char*)argv[0];
  1346. char *cidirrest= (char*)argv[1];
  1347. cidirrest[-1] = '\0';
  1348. ensure_pathname_nonexisting(cidir);
  1349. }
  1350. void cu_fileslist(int argc, void **argv) {
  1351. destroyobstack();
  1352. }
  1353. int
  1354. archivefiles(const char *const *argv)
  1355. {
  1356. const char *volatile thisarg;
  1357. const char *const *volatile argp;
  1358. jmp_buf ejbuf;
  1359. trigproc_install_hooks();
  1360. modstatdb_open(f_noact ? msdbrw_readonly :
  1361. (cipaction->arg_int == act_avail ? msdbrw_readonly :
  1362. fc_nonroot ? msdbrw_write :
  1363. msdbrw_needsuperuser) |
  1364. msdbrw_available_write);
  1365. checkpath();
  1366. pkg_infodb_upgrade();
  1367. log_message("startup archives %s", cipaction->olong);
  1368. if (f_recursive) {
  1369. int pi[2], nfiles, c, i, r;
  1370. pid_t pid;
  1371. FILE *pf;
  1372. static struct varbuf findoutput;
  1373. const char **arglist;
  1374. char *p;
  1375. if (!*argv)
  1376. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1377. m_pipe(pi);
  1378. pid = subproc_fork();
  1379. if (pid == 0) {
  1380. struct command cmd;
  1381. const char *const *ap;
  1382. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1383. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1384. command_add_args(&cmd, FIND, "-L", NULL);
  1385. for (ap = argv; *ap; ap++) {
  1386. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1387. char *a;
  1388. m_asprintf(&a, "./%s", *ap);
  1389. command_add_arg(&cmd, a);
  1390. } else {
  1391. command_add_arg(&cmd, (const char *)*ap);
  1392. }
  1393. }
  1394. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1395. command_exec(&cmd);
  1396. }
  1397. close(pi[1]);
  1398. nfiles= 0;
  1399. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1400. varbuf_reset(&findoutput);
  1401. while ((c= fgetc(pf)) != EOF) {
  1402. varbuf_add_char(&findoutput, c);
  1403. if (!c) nfiles++;
  1404. }
  1405. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1406. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1407. r = subproc_wait_check(pid, "find", PROCNOERR);
  1408. if (r != 0)
  1409. ohshit(_("find for --recursive returned unhandled error %i"),r);
  1410. if (!nfiles)
  1411. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1412. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1413. p = findoutput.buf;
  1414. for (i = 0; i < nfiles; i++) {
  1415. arglist[i] = p;
  1416. while (*p++ != '\0') ;
  1417. }
  1418. arglist[i] = NULL;
  1419. argp= arglist;
  1420. } else {
  1421. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1422. cipaction->olong);
  1423. argp= argv;
  1424. }
  1425. currenttime = time(NULL);
  1426. /* Initialize fname variables contents. */
  1427. varbuf_reset(&fnamevb);
  1428. varbuf_reset(&fnametmpvb);
  1429. varbuf_reset(&fnamenewvb);
  1430. varbuf_add_str(&fnamevb, instdir);
  1431. varbuf_add_char(&fnamevb, '/');
  1432. varbuf_add_str(&fnametmpvb, instdir);
  1433. varbuf_add_char(&fnametmpvb, '/');
  1434. varbuf_add_str(&fnamenewvb, instdir);
  1435. varbuf_add_char(&fnamenewvb, '/');
  1436. fnameidlu= fnamevb.used;
  1437. ensure_diversions();
  1438. ensure_statoverrides();
  1439. while ((thisarg = *argp++) != NULL) {
  1440. if (setjmp(ejbuf)) {
  1441. pop_error_context(ehflag_bombout);
  1442. if (abort_processing)
  1443. break;
  1444. continue;
  1445. }
  1446. push_error_context_jump(&ejbuf, print_error_perpackage, thisarg);
  1447. dpkg_selabel_load();
  1448. process_archive(thisarg);
  1449. onerr_abort++;
  1450. m_output(stdout, _("<standard output>"));
  1451. m_output(stderr, _("<standard error>"));
  1452. onerr_abort--;
  1453. pop_error_context(ehflag_normaltidy);
  1454. }
  1455. dpkg_selabel_close();
  1456. switch (cipaction->arg_int) {
  1457. case act_install:
  1458. case act_configure:
  1459. case act_triggers:
  1460. case act_remove:
  1461. case act_purge:
  1462. process_queue();
  1463. case act_unpack:
  1464. case act_avail:
  1465. break;
  1466. default:
  1467. internerr("unknown action '%d'", cipaction->arg_int);
  1468. }
  1469. trigproc_run_deferred();
  1470. modstatdb_shutdown();
  1471. return 0;
  1472. }
  1473. /**
  1474. * Decide whether we want to install a new version of the package.
  1475. *
  1476. * @param pkg The package with the version we might want to install
  1477. *
  1478. * @retval true If the package should be skipped.
  1479. * @retval false If the package should be installed.
  1480. */
  1481. bool
  1482. wanttoinstall(struct pkginfo *pkg)
  1483. {
  1484. int r;
  1485. if (pkg->want != want_install && pkg->want != want_hold) {
  1486. if (f_alsoselect) {
  1487. printf(_("Selecting previously unselected package %s.\n"),
  1488. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1489. return true;
  1490. } else {
  1491. printf(_("Skipping unselected package %s.\n"),
  1492. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1493. return false;
  1494. }
  1495. }
  1496. if (pkg->eflag & eflag_reinstreq)
  1497. return true;
  1498. if (pkg->status < stat_unpacked)
  1499. return true;
  1500. r = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
  1501. if (r > 0) {
  1502. return true;
  1503. } else if (r == 0) {
  1504. /* Same version fully installed. */
  1505. if (f_skipsame) {
  1506. notice(_("version %.250s of %.250s already installed, skipping"),
  1507. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1508. pkg_name(pkg, pnaw_nonambig));
  1509. return false;
  1510. } else {
  1511. return true;
  1512. }
  1513. } else {
  1514. if (fc_downgrade) {
  1515. warning(_("downgrading %.250s from %.250s to %.250s"),
  1516. pkg_name(pkg, pnaw_nonambig),
  1517. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1518. versiondescribe(&pkg->available.version, vdew_nonambig));
  1519. return true;
  1520. } else {
  1521. notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
  1522. pkg_name(pkg, pnaw_nonambig),
  1523. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1524. versiondescribe(&pkg->available.version, vdew_nonambig));
  1525. return false;
  1526. }
  1527. }
  1528. }
  1529. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1530. struct filenamenode *namenode) {
  1531. struct fileinlist *newconff;
  1532. newconff= m_malloc(sizeof(struct fileinlist));
  1533. newconff->next = NULL;
  1534. newconff->namenode= namenode;
  1535. **newconffileslastp_io= newconff;
  1536. *newconffileslastp_io= &newconff->next;
  1537. return newconff;
  1538. }