archives.c 45 KB

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