archives.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  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-2011 Guillem Jover <guillem@debian.org>
  8. *
  9. * This is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26. #include <sys/stat.h>
  27. #include <assert.h>
  28. #include <errno.h>
  29. #include <ctype.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <fcntl.h>
  33. #include <unistd.h>
  34. #include <stdint.h>
  35. #include <stdlib.h>
  36. #include <stdio.h>
  37. #include <obstack.h>
  38. #define obstack_chunk_alloc m_malloc
  39. #define obstack_chunk_free free
  40. #include <dpkg/i18n.h>
  41. #include <dpkg/dpkg.h>
  42. #include <dpkg/dpkg-db.h>
  43. #include <dpkg/path.h>
  44. #include <dpkg/fdio.h>
  45. #include <dpkg/buffer.h>
  46. #include <dpkg/subproc.h>
  47. #include <dpkg/command.h>
  48. #include <dpkg/file.h>
  49. #include <dpkg/tarfn.h>
  50. #include <dpkg/options.h>
  51. #include <dpkg/triglib.h>
  52. #ifdef WITH_SELINUX
  53. #include <selinux/selinux.h>
  54. #endif
  55. #include "filesdb.h"
  56. #include "main.h"
  57. #include "archives.h"
  58. #include "filters.h"
  59. static inline void
  60. fd_writeback_init(int fd)
  61. {
  62. /* Ignore the return code as it should be considered equivalent to an
  63. * asynchronous hint for the kernel, we are doing an fsync() later on
  64. * anyway. */
  65. #if defined(SYNC_FILE_RANGE_WRITE)
  66. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
  67. #elif defined(HAVE_POSIX_FADVISE)
  68. posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
  69. #endif
  70. }
  71. static struct obstack tar_obs;
  72. static bool tarobs_init = false;
  73. /**
  74. * Ensure the obstack is properly initialized.
  75. */
  76. static void ensureobstackinit(void) {
  77. if (!tarobs_init) {
  78. obstack_init(&tar_obs);
  79. tarobs_init = true;
  80. }
  81. }
  82. /**
  83. * Destroy the obstack.
  84. */
  85. static void destroyobstack(void) {
  86. if (tarobs_init) {
  87. obstack_free(&tar_obs, NULL);
  88. tarobs_init = false;
  89. }
  90. }
  91. /**
  92. * Check if a file or directory will save a package from disappearance.
  93. *
  94. * A package can only be saved by a file or directory which is part
  95. * only of itself - it must be neither part of the new package being
  96. * installed nor part of any 3rd package (this is important so that
  97. * shared directories don't stop packages from disappearing).
  98. */
  99. bool
  100. filesavespackage(struct fileinlist *file,
  101. struct pkginfo *pkgtobesaved,
  102. struct pkginfo *pkgbeinginstalled)
  103. {
  104. struct filepackages_iterator *iter;
  105. struct pkginfo *divpkg, *thirdpkg;
  106. debug(dbg_eachfiledetail,"filesavespackage file `%s' package %s",
  107. file->namenode->name,pkgtobesaved->name);
  108. /* If the file is a contended one and it's overridden by either
  109. * the package we're considering disappearing or the package
  110. * we're installing then they're not actually the same file, so
  111. * we can't disappear the package - it is saved by this file. */
  112. if (file->namenode->divert && file->namenode->divert->useinstead) {
  113. divpkg= file->namenode->divert->pkg;
  114. if (divpkg == pkgtobesaved || divpkg == pkgbeinginstalled) {
  115. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  116. return true;
  117. }
  118. }
  119. /* Is the file in the package being installed? If so then it can't save. */
  120. if (file->namenode->flags & fnnf_new_inarchive) {
  121. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  122. return false;
  123. }
  124. /* Look for a 3rd package which can take over the file (in case
  125. * it's a directory which is shared by many packages. */
  126. iter = filepackages_iter_new(file->namenode);
  127. while ((thirdpkg = filepackages_iter_next(iter))) {
  128. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  129. thirdpkg->name);
  130. /* Is this not the package being installed or the one being
  131. * checked for disappearance? */
  132. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
  133. continue;
  134. /* If !fileslistvalid then we've already disappeared this one, so
  135. * we shouldn't try to make it take over this shared directory. */
  136. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  137. if (!thirdpkg->clientdata->fileslistvalid) {
  138. debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
  139. continue;
  140. }
  141. /* We've found a package that can take this file. */
  142. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  143. return false;
  144. }
  145. filepackages_iter_free(iter);
  146. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  147. return true;
  148. }
  149. void cu_pathname(int argc, void **argv) {
  150. ensure_pathname_nonexisting((char*)(argv[0]));
  151. }
  152. int tarfileread(void *ud, char *buf, int len) {
  153. struct tarcontext *tc= (struct tarcontext*)ud;
  154. int r;
  155. r = fd_read(tc->backendpipe, buf, len);
  156. if (r < 0)
  157. ohshite(_("error reading from dpkg-deb pipe"));
  158. return r;
  159. }
  160. static void
  161. tarfile_skip_one_forward(struct tarcontext *tc, struct tar_entry *ti)
  162. {
  163. size_t r;
  164. char databuf[TARBLKSZ];
  165. /* We need to advance the tar file to the next object, so read the
  166. * file data and set it to oblivion. */
  167. if (ti->type == tar_filetype_file) {
  168. char fnamebuf[256];
  169. fd_skip(tc->backendpipe, ti->size,
  170. _("skipped unpacking file '%.255s' (replaced or excluded?)"),
  171. path_quote_filename(fnamebuf, ti->name, 256));
  172. r = ti->size % TARBLKSZ;
  173. if (r > 0)
  174. if (fd_read(tc->backendpipe, databuf, TARBLKSZ - r) < 0)
  175. ohshite(_("error reading from dpkg-deb pipe"));
  176. }
  177. }
  178. int fnameidlu;
  179. struct varbuf fnamevb;
  180. struct varbuf fnametmpvb;
  181. struct varbuf fnamenewvb;
  182. struct pkg_deconf_list *deconfigure = NULL;
  183. static time_t currenttime;
  184. static int
  185. does_replace(struct pkginfo *newpigp, struct pkgbin *newpifp,
  186. struct pkginfo *oldpigp, struct pkgbin *oldpifp)
  187. {
  188. struct dependency *dep;
  189. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",newpigp->name,
  190. oldpigp->name, versiondescribe(&oldpifp->version, vdew_always));
  191. for (dep= newpifp->depends; dep; dep= dep->next) {
  192. if (dep->type != dep_replaces || dep->list->ed != oldpigp) continue;
  193. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  194. versiondescribe(&dep->list->version,vdew_always));
  195. if (!versionsatisfied(oldpifp, dep->list))
  196. continue;
  197. debug(dbg_depcon,"does_replace ... yes");
  198. return true;
  199. }
  200. debug(dbg_depcon,"does_replace ... no");
  201. return false;
  202. }
  203. static void
  204. tarobject_set_mtime(struct tar_entry *te, const char *path)
  205. {
  206. struct timeval tv[2];
  207. tv[0].tv_sec = currenttime;
  208. tv[0].tv_usec = 0;
  209. tv[1].tv_sec = te->mtime;
  210. tv[1].tv_usec = 0;
  211. if (te->type == tar_filetype_symlink) {
  212. #ifdef HAVE_LUTIMES
  213. if (lutimes(path, tv) && errno != ENOSYS)
  214. ohshite(_("error setting timestamps of `%.255s'"), path);
  215. #endif
  216. } else {
  217. if (utimes(path, tv))
  218. ohshite(_("error setting timestamps of `%.255s'"), path);
  219. }
  220. }
  221. static void
  222. tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
  223. {
  224. if (te->type == tar_filetype_file)
  225. return; /* Already handled using the file descriptor. */
  226. if (te->type == tar_filetype_symlink) {
  227. if (lchown(path, st->uid, st->gid))
  228. ohshite(_("error setting ownership of symlink `%.255s'"), path);
  229. } else {
  230. if (chown(path, st->uid, st->gid))
  231. ohshite(_("error setting ownership of `%.255s'"), path);
  232. if (chmod(path, st->mode & ~S_IFMT))
  233. ohshite(_("error setting permissions of `%.255s'"), path);
  234. }
  235. }
  236. static void
  237. tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
  238. {
  239. #ifdef WITH_SELINUX
  240. static int selinux_enabled = -1;
  241. security_context_t scontext = NULL;
  242. int ret;
  243. /* If there's no file type, just give up. */
  244. if ((mode & S_IFMT) == 0)
  245. return;
  246. /* Set selinux_enabled if it is not already set (singleton). */
  247. if (selinux_enabled < 0)
  248. selinux_enabled = (is_selinux_enabled() > 0);
  249. /* If SE Linux is not enabled just do nothing. */
  250. if (!selinux_enabled)
  251. return;
  252. /* XXX: Well, we could use set_matchpathcon_printf() to redirect the
  253. * errors from the following bit, but that seems too much effort. */
  254. /* Do nothing if we can't figure out what the context is, or if it has
  255. * no context; in which case the default context shall be applied. */
  256. ret = matchpathcon(matchpath, mode & S_IFMT, &scontext);
  257. if (ret == -1 || (ret == 0 && scontext == NULL))
  258. return;
  259. if (strcmp(scontext, "<<none>>") != 0) {
  260. if (lsetfilecon(path, scontext) < 0)
  261. /* XXX: This might need to be fatal instead!? */
  262. perror("Error setting security context for next file object:");
  263. }
  264. freecon(scontext);
  265. #endif /* WITH_SELINUX */
  266. }
  267. void setupfnamevbs(const char *filename) {
  268. varbuf_trunc(&fnamevb, fnameidlu);
  269. varbuf_add_str(&fnamevb, filename);
  270. varbuf_end_str(&fnamevb);
  271. varbuf_trunc(&fnametmpvb, fnameidlu);
  272. varbuf_add_str(&fnametmpvb, filename);
  273. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  274. varbuf_end_str(&fnametmpvb);
  275. varbuf_trunc(&fnamenewvb, fnameidlu);
  276. varbuf_add_str(&fnamenewvb, filename);
  277. varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
  278. varbuf_end_str(&fnamenewvb);
  279. debug(dbg_eachfiledetail, "setupvnamevbs main=`%s' tmp=`%s' new=`%s'",
  280. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  281. }
  282. /**
  283. * Securely remove a pathname.
  284. *
  285. * This is a secure version of remove(3) using secure_unlink() instead of
  286. * unlink(2).
  287. *
  288. * @retval 0 On success.
  289. * @retval -1 On failure, just like unlink(2) & rmdir(2).
  290. */
  291. int
  292. secure_remove(const char *filename)
  293. {
  294. int r, e;
  295. if (!rmdir(filename)) {
  296. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir OK", filename);
  297. return 0;
  298. }
  299. if (errno != ENOTDIR) {
  300. e= errno;
  301. debug(dbg_eachfiledetail, "secure_remove '%s' rmdir %s", filename,
  302. strerror(e));
  303. errno= e; return -1;
  304. }
  305. r = secure_unlink(filename);
  306. e = errno;
  307. debug(dbg_eachfiledetail, "secure_remove '%s' unlink %s",
  308. filename, r ? strerror(e) : "OK");
  309. errno= e; return r;
  310. }
  311. struct fileinlist *addfiletolist(struct tarcontext *tc,
  312. struct filenamenode *namenode) {
  313. struct fileinlist *nifd;
  314. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  315. nifd->namenode= namenode;
  316. nifd->next = NULL;
  317. *tc->newfilesp = nifd;
  318. tc->newfilesp = &nifd->next;
  319. return nifd;
  320. }
  321. static void
  322. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  323. struct fileinlist **oldnifd,
  324. struct fileinlist *nifd)
  325. {
  326. obstack_free(&tar_obs, nifd);
  327. tc->newfilesp = oldnifd;
  328. *oldnifd = NULL;
  329. }
  330. static bool
  331. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  332. struct varbuf *symlinkfn)
  333. {
  334. struct stat oldstab, newstab;
  335. int statr;
  336. const char *lastslash;
  337. statr= stat(fname, &oldstab);
  338. if (statr) {
  339. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  340. ohshite(_("failed to stat (dereference) existing symlink `%.250s'"),
  341. fname);
  342. return false;
  343. }
  344. if (!S_ISDIR(oldstab.st_mode))
  345. return false;
  346. /* But is it to the same dir? */
  347. varbuf_reset(symlinkfn);
  348. if (ti->linkname[0] == '/') {
  349. varbuf_add_str(symlinkfn, instdir);
  350. } else {
  351. lastslash= strrchr(fname, '/');
  352. assert(lastslash);
  353. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  354. }
  355. varbuf_add_str(symlinkfn, ti->linkname);
  356. varbuf_end_str(symlinkfn);
  357. statr= stat(symlinkfn->buf, &newstab);
  358. if (statr) {
  359. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  360. ohshite(_("failed to stat (dereference) proposed new symlink target"
  361. " `%.250s' for symlink `%.250s'"), symlinkfn->buf, fname);
  362. return false;
  363. }
  364. if (!S_ISDIR(newstab.st_mode))
  365. return false;
  366. if (newstab.st_dev != oldstab.st_dev ||
  367. newstab.st_ino != oldstab.st_ino)
  368. return false;
  369. return true;
  370. }
  371. int
  372. tarobject(void *ctx, struct tar_entry *ti)
  373. {
  374. static struct varbuf conffderefn, hardlinkfn, symlinkfn;
  375. static int fd;
  376. const char *usename;
  377. struct filenamenode *usenode;
  378. struct filenamenode *linknode;
  379. struct conffile *conff;
  380. struct tarcontext *tc = ctx;
  381. bool existingdir, keepexisting;
  382. int statr;
  383. ssize_t r;
  384. struct stat stab, stabtmp;
  385. char databuf[TARBLKSZ];
  386. struct file_stat *st;
  387. struct fileinlist *nifd, **oldnifd;
  388. struct pkginfo *divpkg, *otherpkg;
  389. ensureobstackinit();
  390. /* Append to list of files.
  391. * The trailing ‘/’ put on the end of names in tarfiles has already
  392. * been stripped by tar_extractor(). */
  393. oldnifd= tc->newfilesp;
  394. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  395. nifd->namenode->flags |= fnnf_new_inarchive;
  396. debug(dbg_eachfile,
  397. "tarobject ti->name='%s' mode=%lo owner=%u.%u type=%d(%c)"
  398. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  399. ti->name, (long)ti->stat.mode,
  400. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  401. ti->type,
  402. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  403. ti->linkname,
  404. nifd->namenode->name, nifd->namenode->flags,
  405. nifd->namenode->divert && nifd->namenode->divert->useinstead
  406. ? nifd->namenode->divert->useinstead->name : "<none>");
  407. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  408. divpkg= nifd->namenode->divert->pkg;
  409. if (divpkg) {
  410. forcibleerr(fc_overwritediverted,
  411. _("trying to overwrite `%.250s', which is the "
  412. "diverted version of `%.250s' (package: %.100s)"),
  413. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  414. divpkg->name);
  415. } else {
  416. forcibleerr(fc_overwritediverted,
  417. _("trying to overwrite `%.250s', which is the "
  418. "diverted version of `%.250s'"),
  419. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  420. }
  421. }
  422. if (nifd->namenode->statoverride)
  423. st = nifd->namenode->statoverride;
  424. else
  425. st = &ti->stat;
  426. usenode = namenodetouse(nifd->namenode, tc->pkg);
  427. usename = usenode->name + 1; /* Skip the leading '/'. */
  428. trig_file_activate(usenode, tc->pkg);
  429. if (nifd->namenode->flags & fnnf_new_conff) {
  430. /* If it's a conffile we have to extract it next to the installed
  431. * version (i.e. we do the usual link-following). */
  432. if (conffderef(tc->pkg, &conffderefn, usename))
  433. usename= conffderefn.buf;
  434. debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename);
  435. }
  436. setupfnamevbs(usename);
  437. statr= lstat(fnamevb.buf,&stab);
  438. if (statr) {
  439. /* The lstat failed. */
  440. if (errno != ENOENT && errno != ENOTDIR)
  441. ohshite(_("unable to stat `%.255s' (which I was about to install)"),
  442. ti->name);
  443. /* OK, so it doesn't exist.
  444. * However, it's possible that we were in the middle of some other
  445. * backup/restore operation and were rudely interrupted.
  446. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  447. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  448. if (errno != ENOENT && errno != ENOTDIR)
  449. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  450. "installing another version"), ti->name);
  451. debug(dbg_eachfiledetail,"tarobject nonexistent");
  452. } else {
  453. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  454. statr= lstat(fnamevb.buf,&stab);
  455. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  456. " another version"), ti->name);
  457. }
  458. } else {
  459. debug(dbg_eachfiledetail,"tarobject already exists");
  460. }
  461. /* Check to see if it's a directory or link to one and we don't need to
  462. * do anything. This has to be done now so that we don't die due to
  463. * a file overwriting conflict. */
  464. existingdir = false;
  465. switch (ti->type) {
  466. case tar_filetype_symlink:
  467. /* If it's already an existing directory, do nothing. */
  468. if (!statr && S_ISDIR(stab.st_mode)) {
  469. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  470. existingdir = true;
  471. } else if (!statr && S_ISLNK(stab.st_mode)) {
  472. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  473. existingdir = true;
  474. }
  475. break;
  476. case tar_filetype_dir:
  477. /* If it's already an existing directory, do nothing. */
  478. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  479. debug(dbg_eachfiledetail, "tarobject directory exists");
  480. existingdir = true;
  481. }
  482. break;
  483. case tar_filetype_file:
  484. case tar_filetype_chardev:
  485. case tar_filetype_blockdev:
  486. case tar_filetype_fifo:
  487. case tar_filetype_hardlink:
  488. break;
  489. default:
  490. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),
  491. ti->name, ti->type);
  492. }
  493. keepexisting = false;
  494. if (!existingdir) {
  495. struct filepackages_iterator *iter;
  496. iter = filepackages_iter_new(nifd->namenode);
  497. while ((otherpkg = filepackages_iter_next(iter))) {
  498. if (otherpkg == tc->pkg)
  499. continue;
  500. debug(dbg_eachfile, "tarobject ... found in %s", otherpkg->name);
  501. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  502. /* Right, so we may be diverting this file. This makes the conflict
  503. * OK iff one of us is the diverting package (we don't need to
  504. * check for both being the diverting package, obviously). */
  505. divpkg = nifd->namenode->divert->pkg;
  506. debug(dbg_eachfile, "tarobject ... diverted, divpkg=%s",
  507. divpkg ? divpkg->name : "<none>");
  508. if (otherpkg == divpkg || tc->pkg == divpkg)
  509. continue;
  510. }
  511. /* If the new object is a directory and the previous object does
  512. * not exist assume it's also a directory and skip further checks.
  513. * XXX: Ideally with more information about the installed files we
  514. * could perform more clever checks. */
  515. if (statr != 0 && ti->type == tar_filetype_dir) {
  516. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  517. continue;
  518. }
  519. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  520. switch (otherpkg->clientdata->replacingfilesandsaid) {
  521. case 2:
  522. keepexisting = true;
  523. case 1:
  524. continue;
  525. }
  526. /* Is the package with the conflicting file in the “config files only”
  527. * state? If so it must be a config file and we can silenty take it
  528. * over. */
  529. if (otherpkg->status == stat_configfiles)
  530. continue;
  531. /* Perhaps we're removing a conflicting package? */
  532. if (otherpkg->clientdata->istobe == itb_remove)
  533. continue;
  534. /* Is the file an obsolete conffile in the other package
  535. * and a conffile in the new package? */
  536. if ((nifd->namenode->flags & fnnf_new_conff) &&
  537. !statr && S_ISREG(stab.st_mode)) {
  538. for (conff = otherpkg->installed.conffiles;
  539. conff;
  540. conff = conff->next) {
  541. if (!conff->obsolete)
  542. continue;
  543. if (stat(conff->name, &stabtmp)) {
  544. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  545. continue;
  546. else
  547. ohshite(_("cannot stat file '%s'"), conff->name);
  548. }
  549. if (stabtmp.st_dev == stab.st_dev &&
  550. stabtmp.st_ino == stab.st_ino)
  551. break;
  552. }
  553. if (conff) {
  554. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  555. /* process_archive() will have copied its hash already. */
  556. continue;
  557. }
  558. }
  559. if (does_replace(tc->pkg, &tc->pkg->available,
  560. otherpkg, &otherpkg->installed)) {
  561. printf(_("Replacing files in old package %s ...\n"),otherpkg->name);
  562. otherpkg->clientdata->replacingfilesandsaid = 1;
  563. } else if (does_replace(otherpkg, &otherpkg->installed,
  564. tc->pkg, &tc->pkg->available)) {
  565. printf(_("Replaced by files in installed package %s ...\n"),
  566. otherpkg->name);
  567. otherpkg->clientdata->replacingfilesandsaid = 2;
  568. nifd->namenode->flags &= ~fnnf_new_inarchive;
  569. keepexisting = true;
  570. } else {
  571. /* At this point we are replacing something without a Replaces. */
  572. if (!statr && S_ISDIR(stab.st_mode)) {
  573. forcibleerr(fc_overwritedir,
  574. _("trying to overwrite directory '%.250s' "
  575. "in package %.250s %.250s with nondirectory"),
  576. nifd->namenode->name, otherpkg->name,
  577. versiondescribe(&otherpkg->installed.version,
  578. vdew_nonambig));
  579. } else {
  580. forcibleerr(fc_overwrite,
  581. _("trying to overwrite '%.250s', "
  582. "which is also in package %.250s %.250s"),
  583. nifd->namenode->name, otherpkg->name,
  584. versiondescribe(&otherpkg->installed.version,
  585. vdew_nonambig));
  586. }
  587. }
  588. }
  589. filepackages_iter_free(iter);
  590. }
  591. if (keepexisting) {
  592. remove_file_from_list(tc, ti, oldnifd, nifd);
  593. tarfile_skip_one_forward(tc, ti);
  594. return 0;
  595. }
  596. if (filter_should_skip(ti)) {
  597. nifd->namenode->flags &= ~fnnf_new_inarchive;
  598. nifd->namenode->flags |= fnnf_filtered;
  599. tarfile_skip_one_forward(tc, ti);
  600. return 0;
  601. }
  602. if (existingdir)
  603. return 0;
  604. /* Now, at this stage we want to make sure neither of .dpkg-new and
  605. * .dpkg-tmp are hanging around. */
  606. ensure_pathname_nonexisting(fnamenewvb.buf);
  607. ensure_pathname_nonexisting(fnametmpvb.buf);
  608. /* Now we start to do things that we need to be able to undo
  609. * if something goes wrong. Watch out for the CLEANUP comments to
  610. * keep an eye on what's installed on the disk at each point. */
  611. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, (void *)nifd);
  612. /*
  613. * CLEANUP: Now we either have the old file on the disk, or not, in
  614. * its original filename.
  615. */
  616. /* Extract whatever it is as .dpkg-new ... */
  617. switch (ti->type) {
  618. case tar_filetype_file:
  619. /* We create the file with mode 0 to make sure nobody can do anything with
  620. * it until we apply the proper mode, which might be a statoverride. */
  621. fd= open(fnamenewvb.buf, (O_CREAT|O_EXCL|O_WRONLY), 0);
  622. if (fd < 0)
  623. ohshite(_("unable to create `%.255s' (while processing `%.255s')"),
  624. fnamenewvb.buf, ti->name);
  625. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  626. debug(dbg_eachfiledetail, "tarobject file open size=%jd",
  627. (intmax_t)ti->size);
  628. { char fnamebuf[256];
  629. fd_fd_copy(tc->backendpipe, fd, ti->size,
  630. _("backend dpkg-deb during `%.255s'"),
  631. path_quote_filename(fnamebuf, ti->name, 256));
  632. }
  633. r = ti->size % TARBLKSZ;
  634. if (r > 0)
  635. if (fd_read(tc->backendpipe, databuf, TARBLKSZ - r) < 0)
  636. ohshite(_("error reading from dpkg-deb pipe"));
  637. fd_writeback_init(fd);
  638. if (nifd->namenode->statoverride)
  639. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  640. nifd->namenode->statoverride->uid,
  641. nifd->namenode->statoverride->gid,
  642. nifd->namenode->statoverride->mode);
  643. if (fchown(fd, st->uid, st->gid))
  644. ohshite(_("error setting ownership of `%.255s'"), ti->name);
  645. if (fchmod(fd, st->mode & ~S_IFMT))
  646. ohshite(_("error setting permissions of `%.255s'"), ti->name);
  647. /* Postpone the fsync, to try to avoid massive I/O degradation. */
  648. if (!fc_unsafe_io)
  649. nifd->namenode->flags |= fnnf_deferred_fsync;
  650. pop_cleanup(ehflag_normaltidy); /* fd = open(fnamenewvb.buf) */
  651. if (close(fd))
  652. ohshite(_("error closing/writing `%.255s'"), ti->name);
  653. break;
  654. case tar_filetype_fifo:
  655. if (mkfifo(fnamenewvb.buf,0))
  656. ohshite(_("error creating pipe `%.255s'"), ti->name);
  657. debug(dbg_eachfiledetail, "tarobject fifo");
  658. break;
  659. case tar_filetype_chardev:
  660. if (mknod(fnamenewvb.buf, S_IFCHR, ti->dev))
  661. ohshite(_("error creating device `%.255s'"), ti->name);
  662. debug(dbg_eachfiledetail, "tarobject chardev");
  663. break;
  664. case tar_filetype_blockdev:
  665. if (mknod(fnamenewvb.buf, S_IFBLK, ti->dev))
  666. ohshite(_("error creating device `%.255s'"), ti->name);
  667. debug(dbg_eachfiledetail, "tarobject blockdev");
  668. break;
  669. case tar_filetype_hardlink:
  670. varbuf_reset(&hardlinkfn);
  671. varbuf_add_str(&hardlinkfn, instdir);
  672. varbuf_add_char(&hardlinkfn, '/');
  673. linknode = findnamenode(ti->linkname, 0);
  674. varbuf_add_str(&hardlinkfn, namenodetouse(linknode, tc->pkg)->name);
  675. if (linknode->flags & (fnnf_deferred_rename|fnnf_new_conff))
  676. varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
  677. varbuf_end_str(&hardlinkfn);
  678. if (link(hardlinkfn.buf,fnamenewvb.buf))
  679. ohshite(_("error creating hard link `%.255s'"), ti->name);
  680. debug(dbg_eachfiledetail, "tarobject hardlink");
  681. break;
  682. case tar_filetype_symlink:
  683. /* We've already checked for an existing directory. */
  684. if (symlink(ti->linkname, fnamenewvb.buf))
  685. ohshite(_("error creating symbolic link `%.255s'"), ti->name);
  686. debug(dbg_eachfiledetail, "tarobject symlink creating");
  687. break;
  688. case tar_filetype_dir:
  689. /* We've already checked for an existing directory. */
  690. if (mkdir(fnamenewvb.buf,0))
  691. ohshite(_("error creating directory `%.255s'"), ti->name);
  692. debug(dbg_eachfiledetail, "tarobject directory creating");
  693. break;
  694. default:
  695. internerr("unknown tar type '%d', but already checked", ti->type);
  696. }
  697. tarobject_set_perms(ti, fnamenewvb.buf, st);
  698. tarobject_set_mtime(ti, fnamenewvb.buf);
  699. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, st->mode);
  700. /*
  701. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  702. * if the file already exists as a directory and we were trying to
  703. * extract a directory or symlink, we returned earlier, so we don't
  704. * need to worry about that here).
  705. *
  706. * The old file is still in the original filename,
  707. */
  708. /* First, check to see if it's a conffile. If so we don't install
  709. * it now - we leave it in .dpkg-new for --configure to take care of. */
  710. if (nifd->namenode->flags & fnnf_new_conff) {
  711. debug(dbg_conffdetail,"tarobject conffile extracted");
  712. nifd->namenode->flags |= fnnf_elide_other_lists;
  713. return 0;
  714. }
  715. /* Now we move the old file out of the way, the backup file will
  716. * be deleted later. */
  717. if (statr) {
  718. /* Don't try to back it up if it didn't exist. */
  719. debug(dbg_eachfiledetail,"tarobject new - no backup");
  720. } else {
  721. if (ti->type == tar_filetype_dir || S_ISDIR(stab.st_mode)) {
  722. /* One of the two is a directory - can't do atomic install. */
  723. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  724. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  725. if (rename(fnamevb.buf,fnametmpvb.buf))
  726. ohshite(_("unable to move aside `%.255s' to install new version"),
  727. ti->name);
  728. } else if (S_ISLNK(stab.st_mode)) {
  729. /* We can't make a symlink with two hardlinks, so we'll have to
  730. * copy it. (Pretend that making a copy of a symlink is the same
  731. * as linking to it.) */
  732. varbuf_reset(&symlinkfn);
  733. varbuf_grow(&symlinkfn, stab.st_size + 1);
  734. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  735. if (r < 0)
  736. ohshite(_("unable to read link `%.255s'"), ti->name);
  737. else if (r != stab.st_size)
  738. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  739. fnamevb.buf, stab.st_size, r);
  740. varbuf_trunc(&symlinkfn, r);
  741. varbuf_end_str(&symlinkfn);
  742. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  743. ohshite(_("unable to make backup symlink for `%.255s'"), ti->name);
  744. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  745. ohshite(_("unable to chown backup symlink for `%.255s'"), ti->name);
  746. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  747. } else {
  748. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  749. if (link(fnamevb.buf,fnametmpvb.buf))
  750. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  751. ti->name);
  752. }
  753. }
  754. /*
  755. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  756. * in .dpkg-new.
  757. */
  758. if (ti->type == tar_filetype_file || ti->type == tar_filetype_hardlink ||
  759. ti->type == tar_filetype_symlink) {
  760. nifd->namenode->flags |= fnnf_deferred_rename;
  761. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  762. } else {
  763. if (rename(fnamenewvb.buf, fnamevb.buf))
  764. ohshite(_("unable to install new version of `%.255s'"), ti->name);
  765. /*
  766. * CLEANUP: Now the new file is in the destination file, and the
  767. * old file is in .dpkg-tmp to be cleaned up later. We now need
  768. * to take a different attitude to cleanup, because we need to
  769. * remove the new file.
  770. */
  771. nifd->namenode->flags |= fnnf_placed_on_disk;
  772. nifd->namenode->flags |= fnnf_elide_other_lists;
  773. debug(dbg_eachfiledetail, "tarobject done and installed");
  774. }
  775. return 0;
  776. }
  777. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  778. static void
  779. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  780. {
  781. struct fileinlist *cfile;
  782. for (cfile = files; cfile; cfile = cfile->next) {
  783. struct filenamenode *usenode;
  784. const char *usename;
  785. int fd;
  786. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  787. continue;
  788. usenode = namenodetouse(cfile->namenode, pkg);
  789. usename = usenode->name + 1; /* Skip the leading '/'. */
  790. setupfnamevbs(usename);
  791. fd = open(fnamenewvb.buf, O_WRONLY);
  792. if (fd < 0)
  793. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  794. /* Ignore the return code as it should be considered equivalent to an
  795. * asynchronous hint for the kernel, we are doing an fsync() later on
  796. * anyway. */
  797. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  798. if (close(fd))
  799. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  800. }
  801. }
  802. #else
  803. static void
  804. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  805. {
  806. }
  807. #endif
  808. void
  809. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  810. {
  811. struct fileinlist *cfile;
  812. struct filenamenode *usenode;
  813. const char *usename;
  814. tar_writeback_barrier(files, pkg);
  815. for (cfile = files; cfile; cfile = cfile->next) {
  816. debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
  817. if (!(cfile->namenode->flags & fnnf_deferred_rename))
  818. continue;
  819. usenode = namenodetouse(cfile->namenode, pkg);
  820. usename = usenode->name + 1; /* Skip the leading '/'. */
  821. setupfnamevbs(usename);
  822. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  823. int fd;
  824. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  825. fd = open(fnamenewvb.buf, O_WRONLY);
  826. if (fd < 0)
  827. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  828. if (fsync(fd))
  829. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  830. if (close(fd))
  831. ohshite(_("error closing/writing `%.255s'"), fnamenewvb.buf);
  832. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  833. }
  834. debug(dbg_eachfiledetail, "deferred extract needs rename");
  835. if (rename(fnamenewvb.buf, fnamevb.buf))
  836. ohshite(_("unable to install new version of `%.255s'"),
  837. cfile->namenode->name);
  838. cfile->namenode->flags &= ~fnnf_deferred_rename;
  839. /*
  840. * CLEANUP: Now the new file is in the destination file, and the
  841. * old file is in .dpkg-tmp to be cleaned up later. We now need
  842. * to take a different attitude to cleanup, because we need to
  843. * remove the new file.
  844. */
  845. cfile->namenode->flags |= fnnf_placed_on_disk;
  846. cfile->namenode->flags |= fnnf_elide_other_lists;
  847. debug(dbg_eachfiledetail, "deferred extract done and installed");
  848. }
  849. }
  850. /**
  851. * Try if we can deconfigure the package and queue it if so.
  852. *
  853. * Also checks whether the pdep is forced, first, according to force_p.
  854. * force_p may be NULL in which case nothing is considered forced.
  855. *
  856. * Action is a string describing the action which causes the
  857. * deconfiguration:
  858. *
  859. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  860. * "installation of <package>" (due to Breaks; removal == NULL)
  861. *
  862. * @retval 0 Not possible (why is printed).
  863. * @retval 1 Deconfiguration queued ok (no message printed).
  864. * @retval 2 Forced (no deconfiguration needed, why is printed).
  865. */
  866. static int
  867. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  868. struct deppossi *pdep, const char *action,
  869. struct pkginfo *removal, const char *why)
  870. {
  871. struct pkg_deconf_list *newdeconf;
  872. if (force_p && force_p(pdep)) {
  873. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  874. return 2;
  875. } else if (f_autodeconf) {
  876. if (pkg->installed.essential) {
  877. if (fc_removeessential) {
  878. warning(_("considering deconfiguration of essential\n"
  879. " package %s, to enable %s."), pkg->name, action);
  880. } else {
  881. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  882. " it in order to enable %s.\n"),
  883. pkg->name, action);
  884. return 0;
  885. }
  886. }
  887. pkg->clientdata->istobe= itb_deconfigure;
  888. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  889. newdeconf->next= deconfigure;
  890. newdeconf->pkg= pkg;
  891. newdeconf->pkg_removal = removal;
  892. deconfigure= newdeconf;
  893. return 1;
  894. } else {
  895. fprintf(stderr, _("dpkg: no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  896. action, why);
  897. return 0;
  898. }
  899. }
  900. static int try_remove_can(struct deppossi *pdep,
  901. struct pkginfo *fixbyrm,
  902. const char *why) {
  903. char action[512];
  904. sprintf(action, _("removal of %.250s"), fixbyrm->name);
  905. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  906. action, fixbyrm, why);
  907. }
  908. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  909. const char *pfilename) {
  910. struct pkginfo *fixbydeconf;
  911. struct varbuf why = VARBUF_INIT;
  912. int ok;
  913. fixbydeconf = NULL;
  914. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  915. varbuf_destroy(&why);
  916. return;
  917. }
  918. varbuf_end_str(&why);
  919. if (fixbydeconf && f_autodeconf) {
  920. char action[512];
  921. ensure_package_clientdata(fixbydeconf);
  922. assert(fixbydeconf->clientdata->istobe == itb_normal);
  923. sprintf(action, _("installation of %.250s"), pkg->name);
  924. fprintf(stderr, _("dpkg: considering deconfiguration of %s,"
  925. " which would be broken by %s ...\n"),
  926. fixbydeconf->name, action);
  927. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  928. action, NULL, why.buf);
  929. if (ok == 1) {
  930. fprintf(stderr, _("dpkg: yes, will deconfigure %s (broken by %s).\n"),
  931. fixbydeconf->name, pkg->name);
  932. }
  933. } else {
  934. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  935. pfilename, pkg->name, why.buf);
  936. ok= 0;
  937. }
  938. varbuf_destroy(&why);
  939. if (ok > 0) return;
  940. if (force_breaks(dep->list)) {
  941. warning(_("ignoring breakage, may proceed anyway!"));
  942. return;
  943. }
  944. if (fixbydeconf && !f_autodeconf) {
  945. ohshit(_("installing %.250s would break %.250s, and\n"
  946. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  947. pkg->name, fixbydeconf->name);
  948. } else {
  949. ohshit(_("installing %.250s would break existing software"),
  950. pkg->name);
  951. }
  952. }
  953. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  954. const char *pfilename) {
  955. struct pkginfo *fixbyrm;
  956. struct deppossi *pdep, flagdeppossi;
  957. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  958. struct dependency *providecheck;
  959. fixbyrm = NULL;
  960. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  961. varbuf_destroy(&conflictwhy);
  962. varbuf_destroy(&removalwhy);
  963. return;
  964. }
  965. if (fixbyrm) {
  966. ensure_package_clientdata(fixbyrm);
  967. if (fixbyrm->clientdata->istobe == itb_installnew) {
  968. fixbyrm= dep->up;
  969. ensure_package_clientdata(fixbyrm);
  970. }
  971. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  972. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  973. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  974. (!fixbyrm->installed.essential || fc_removeessential)))) {
  975. assert(fixbyrm->clientdata->istobe == itb_normal || fixbyrm->clientdata->istobe == itb_deconfigure);
  976. fixbyrm->clientdata->istobe= itb_remove;
  977. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  978. fixbyrm->name, pkg->name);
  979. if (!(fixbyrm->status == stat_installed ||
  980. fixbyrm->status == stat_triggerspending ||
  981. fixbyrm->status == stat_triggersawaited)) {
  982. fprintf(stderr,
  983. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  984. fixbyrm->name);
  985. pdep = NULL;
  986. } else {
  987. for (pdep= fixbyrm->installed.depended;
  988. pdep;
  989. pdep = pdep->rev_next) {
  990. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  991. continue;
  992. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  993. continue;
  994. varbuf_end_str(&removalwhy);
  995. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  996. break;
  997. }
  998. if (!pdep) {
  999. /* If we haven't found a reason not to yet, let's look some more. */
  1000. for (providecheck= fixbyrm->installed.depends;
  1001. providecheck;
  1002. providecheck= providecheck->next) {
  1003. if (providecheck->type != dep_provides) continue;
  1004. for (pdep= providecheck->list->ed->installed.depended;
  1005. pdep;
  1006. pdep = pdep->rev_next) {
  1007. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1008. continue;
  1009. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1010. continue;
  1011. varbuf_end_str(&removalwhy);
  1012. fprintf(stderr, _("dpkg"
  1013. ": may have trouble removing %s, as it provides %s ...\n"),
  1014. fixbyrm->name, providecheck->list->ed->name);
  1015. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1016. goto break_from_both_loops_at_once;
  1017. }
  1018. }
  1019. break_from_both_loops_at_once:;
  1020. }
  1021. }
  1022. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1023. pdep= &flagdeppossi;
  1024. }
  1025. if (!pdep && (fixbyrm->eflag & eflag_reinstreq)) {
  1026. if (fc_removereinstreq) {
  1027. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  1028. " remove anyway as you requested.\n"), fixbyrm->name);
  1029. } else {
  1030. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  1031. "will not remove.\n"), fixbyrm->name);
  1032. pdep= &flagdeppossi;
  1033. }
  1034. }
  1035. if (!pdep) {
  1036. /* This conflict is OK - we'll remove the conflictor. */
  1037. push_conflictor(pkg, fixbyrm);
  1038. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1039. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  1040. fixbyrm->name, pkg->name);
  1041. return;
  1042. }
  1043. /* Put it back. */
  1044. fixbyrm->clientdata->istobe = itb_normal;
  1045. }
  1046. }
  1047. varbuf_end_str(&conflictwhy);
  1048. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  1049. pfilename, pkg->name, conflictwhy.buf);
  1050. if (!force_conflicts(dep->list))
  1051. ohshit(_("conflicting packages - not installing %.250s"),pkg->name);
  1052. warning(_("ignoring conflict, may proceed anyway!"));
  1053. varbuf_destroy(&conflictwhy);
  1054. return;
  1055. }
  1056. void cu_cidir(int argc, void **argv) {
  1057. char *cidir= (char*)argv[0];
  1058. char *cidirrest= (char*)argv[1];
  1059. cidirrest[-1] = '\0';
  1060. ensure_pathname_nonexisting(cidir);
  1061. }
  1062. void cu_fileslist(int argc, void **argv) {
  1063. destroyobstack();
  1064. }
  1065. int
  1066. archivefiles(const char *const *argv)
  1067. {
  1068. const char *volatile thisarg;
  1069. const char *const *volatile argp;
  1070. jmp_buf ejbuf;
  1071. trigproc_install_hooks();
  1072. modstatdb_open(f_noact ? msdbrw_readonly :
  1073. (cipaction->arg_int == act_avail ? msdbrw_readonly :
  1074. fc_nonroot ? msdbrw_write :
  1075. msdbrw_needsuperuser) |
  1076. msdbrw_available_write);
  1077. checkpath();
  1078. log_message("startup archives %s", cipaction->olong);
  1079. if (f_recursive) {
  1080. int pi[2], nfiles, c, i, r;
  1081. pid_t pid;
  1082. FILE *pf;
  1083. static struct varbuf findoutput;
  1084. const char **arglist;
  1085. char *p;
  1086. if (!*argv)
  1087. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1088. m_pipe(pi);
  1089. pid = subproc_fork();
  1090. if (pid == 0) {
  1091. struct command cmd;
  1092. const char *const *ap;
  1093. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1094. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1095. command_add_args(&cmd, FIND, "-L", NULL);
  1096. for (ap = argv; *ap; ap++) {
  1097. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1098. char *a;
  1099. m_asprintf(&a, "./%s", *ap);
  1100. command_add_arg(&cmd, a);
  1101. } else {
  1102. command_add_arg(&cmd, (const char *)*ap);
  1103. }
  1104. }
  1105. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1106. command_exec(&cmd);
  1107. }
  1108. close(pi[1]);
  1109. nfiles= 0;
  1110. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1111. varbuf_reset(&findoutput);
  1112. while ((c= fgetc(pf)) != EOF) {
  1113. varbuf_add_char(&findoutput, c);
  1114. if (!c) nfiles++;
  1115. }
  1116. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1117. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1118. r = subproc_wait_check(pid, "find", PROCNOERR);
  1119. if (r != 0)
  1120. ohshit(_("find for --recursive returned unhandled error %i"),r);
  1121. if (!nfiles)
  1122. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1123. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1124. p = findoutput.buf;
  1125. for (i = 0; i < nfiles; i++) {
  1126. arglist[i] = p;
  1127. while (*p++ != '\0') ;
  1128. }
  1129. arglist[i] = NULL;
  1130. argp= arglist;
  1131. } else {
  1132. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1133. cipaction->olong);
  1134. argp= argv;
  1135. }
  1136. currenttime = time(NULL);
  1137. /* Initialize fname variables contents. */
  1138. varbuf_reset(&fnamevb);
  1139. varbuf_reset(&fnametmpvb);
  1140. varbuf_reset(&fnamenewvb);
  1141. varbuf_add_str(&fnamevb, instdir);
  1142. varbuf_add_char(&fnamevb, '/');
  1143. varbuf_add_str(&fnametmpvb, instdir);
  1144. varbuf_add_char(&fnametmpvb, '/');
  1145. varbuf_add_str(&fnamenewvb, instdir);
  1146. varbuf_add_char(&fnamenewvb, '/');
  1147. fnameidlu= fnamevb.used;
  1148. ensure_diversions();
  1149. ensure_statoverrides();
  1150. while ((thisarg = *argp++) != NULL) {
  1151. if (setjmp(ejbuf)) {
  1152. pop_error_context(ehflag_bombout);
  1153. if (abort_processing)
  1154. break;
  1155. continue;
  1156. }
  1157. push_error_context_jump(&ejbuf, print_error_perpackage, thisarg);
  1158. process_archive(thisarg);
  1159. onerr_abort++;
  1160. m_output(stdout, _("<standard output>"));
  1161. m_output(stderr, _("<standard error>"));
  1162. onerr_abort--;
  1163. pop_error_context(ehflag_normaltidy);
  1164. }
  1165. switch (cipaction->arg_int) {
  1166. case act_install:
  1167. case act_configure:
  1168. case act_triggers:
  1169. case act_remove:
  1170. case act_purge:
  1171. process_queue();
  1172. case act_unpack:
  1173. case act_avail:
  1174. break;
  1175. default:
  1176. internerr("unknown action '%d'", cipaction->arg_int);
  1177. }
  1178. trigproc_run_deferred();
  1179. modstatdb_shutdown();
  1180. return 0;
  1181. }
  1182. /**
  1183. * Decide whether we want to install a new version of the package.
  1184. *
  1185. * @param pkg The package with the version we might want to install
  1186. *
  1187. * @retval true If the package should be skipped.
  1188. * @retval false If the package should be installed.
  1189. */
  1190. bool
  1191. wanttoinstall(struct pkginfo *pkg)
  1192. {
  1193. int r;
  1194. if (pkg->want != want_install && pkg->want != want_hold) {
  1195. if (f_alsoselect) {
  1196. printf(_("Selecting previously unselected package %s.\n"), pkg->name);
  1197. pkg->want = want_install;
  1198. return true;
  1199. } else {
  1200. printf(_("Skipping unselected package %s.\n"), pkg->name);
  1201. return false;
  1202. }
  1203. }
  1204. if (pkg->eflag & eflag_reinstreq)
  1205. return true;
  1206. if (pkg->status < stat_unpacked)
  1207. return true;
  1208. r = versioncompare(&pkg->available.version, &pkg->installed.version);
  1209. if (r > 0) {
  1210. return true;
  1211. } else if (r == 0) {
  1212. /* Same version fully installed. */
  1213. if (f_skipsame) {
  1214. fprintf(stderr, _("Version %.250s of %.250s already installed, "
  1215. "skipping.\n"),
  1216. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1217. pkg->name);
  1218. return false;
  1219. } else {
  1220. return true;
  1221. }
  1222. } else {
  1223. if (fc_downgrade) {
  1224. warning(_("downgrading %.250s from %.250s to %.250s."), pkg->name,
  1225. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1226. versiondescribe(&pkg->available.version, vdew_nonambig));
  1227. return true;
  1228. } else {
  1229. fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  1230. "to %.250s, skipping.\n"), pkg->name,
  1231. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1232. versiondescribe(&pkg->available.version, vdew_nonambig));
  1233. return false;
  1234. }
  1235. }
  1236. }
  1237. struct fileinlist *newconff_append(struct fileinlist ***newconffileslastp_io,
  1238. struct filenamenode *namenode) {
  1239. struct fileinlist *newconff;
  1240. newconff= m_malloc(sizeof(struct fileinlist));
  1241. newconff->next = NULL;
  1242. newconff->namenode= namenode;
  1243. **newconffileslastp_io= newconff;
  1244. *newconffileslastp_io= &newconff->next;
  1245. return newconff;
  1246. }
  1247. /* vi: ts=8 sw=2
  1248. */