archives.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /*
  2. * dpkg - main program for package management
  3. * archives.c - actions that process archive files, mainly unpack
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <iwj10@cus.cam.ac.uk>
  6. * Copyright (C) 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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <errno.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include <utime.h>
  28. #include <assert.h>
  29. #include <time.h>
  30. #include <ctype.h>
  31. #include <fcntl.h>
  32. #include <sys/stat.h>
  33. #include <sys/types.h>
  34. #include <obstack.h>
  35. #define obstack_chunk_alloc m_malloc
  36. #define obstack_chunk_free free
  37. #include <config.h>
  38. #include <dpkg.h>
  39. #include <dpkg-db.h>
  40. #include <tarfn.h>
  41. #include <myopt.h>
  42. #include "filesdb.h"
  43. #include "main.h"
  44. #include "archives.h"
  45. /* We shouldn't need anymore than 10 conflictors */
  46. struct pkginfo *conflictor[20];
  47. int cflict_index = 0;
  48. int filesavespackage(struct fileinlist *file, struct pkginfo *pkgtobesaved,
  49. struct pkginfo *pkgbeinginstalled) {
  50. struct pkginfo *divpkg, *thirdpkg;
  51. struct filepackages *packageslump;
  52. int i;
  53. debug(dbg_eachfiledetail,"filesavespackage file `%s' package %s",
  54. file->namenode->name,pkgtobesaved->name);
  55. /* A package can only be saved by a file or directory which is part
  56. * only of itself - it must be neither part of the new package being
  57. * installed nor part of any 3rd package (this is important so that
  58. * shared directories don't stop packages from disappearing).
  59. */
  60. /* Is the file in the package being installed ? If so then it can't save.
  61. */
  62. if (file->namenode->flags & fnnf_new_inarchive) {
  63. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  64. return 0;
  65. }
  66. /* If the file is a contended one and it's overridden by either
  67. * the package we're considering disappearing or the package
  68. * we're installing then they're not actually the same file, so
  69. * we can't disappear the package - it is saved by this file.
  70. */
  71. if (file->namenode->divert && file->namenode->divert->useinstead) {
  72. divpkg= file->namenode->divert->pkg;
  73. if (divpkg == pkgtobesaved || divpkg == pkgbeinginstalled) {
  74. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  75. return 1;
  76. }
  77. }
  78. /* Look for a 3rd package which can take over the file (in case
  79. * it's a directory which is shared by many packages.
  80. */
  81. for (packageslump= file->namenode->packages;
  82. packageslump;
  83. packageslump= packageslump->more) {
  84. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  85. thirdpkg= packageslump->pkgs[i];
  86. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  87. thirdpkg->name);
  88. /* Is this not the package being installed or the one being
  89. * checked for disappearance ?
  90. */
  91. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved) continue;
  92. /* If !fileslistvalid then we've already disappeared this one, so
  93. * we shouldn't try to make it take over this shared directory.
  94. */
  95. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  96. if (!thirdpkg->clientdata->fileslistvalid) {
  97. debug(dbg_eachfiledetail,
  98. _("process_archive ... already disappeared !"));
  99. continue;
  100. }
  101. /* We've found a package that can take this file. */
  102. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  103. return 0;
  104. }
  105. }
  106. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  107. return 1;
  108. }
  109. void cu_pathname(int argc, void **argv) {
  110. ensure_pathname_nonexisting((char*)(argv[0]));
  111. }
  112. int tarfileread(void *ud, char *buf, int len) {
  113. struct tarcontext *tc= (struct tarcontext*)ud;
  114. int r;
  115. if ((r= read(tc->backendpipe,buf,len)) == -1)
  116. ohshite(_("error reading from dpkg-deb pipe"));
  117. return r;
  118. }
  119. int fnameidlu;
  120. struct varbuf fnamevb;
  121. struct varbuf fnametmpvb;
  122. struct varbuf fnamenewvb;
  123. struct packageinlist *deconfigure= 0;
  124. static time_t currenttime;
  125. static int does_replace(struct pkginfo *newpigp,
  126. struct pkginfoperfile *newpifp,
  127. struct pkginfo *oldpigp) {
  128. struct dependency *dep;
  129. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",newpigp->name,
  130. oldpigp->name,versiondescribe(&oldpigp->installed.version,
  131. vdew_always));
  132. for (dep= newpifp->depends; dep; dep= dep->next) {
  133. if (dep->type != dep_replaces || dep->list->ed != oldpigp) continue;
  134. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  135. versiondescribe(&dep->list->version,vdew_always));
  136. if (!versionsatisfied(&oldpigp->installed,dep->list)) continue;
  137. debug(dbg_depcon,"does_replace ... yes");
  138. return 1;
  139. }
  140. debug(dbg_depcon,"does_replace ... no");
  141. return 0;
  142. }
  143. static void newtarobject_utime(const char *path, struct TarInfo *ti) {
  144. struct utimbuf utb;
  145. utb.actime= currenttime;
  146. utb.modtime= ti->ModTime;
  147. if (utime(path,&utb))
  148. ohshite(_("error setting timestamps of `%.255s'"),ti->Name);
  149. }
  150. static void newtarobject_allmodes(const char *path, struct TarInfo *ti, struct filestatoverride* statoverride) {
  151. if (chown(path,
  152. statoverride ? statoverride->uid : ti->UserID,
  153. statoverride ? statoverride->gid : ti->GroupID))
  154. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  155. if (chmod(path,(statoverride ? statoverride->mode : ti->Mode) & ~S_IFMT))
  156. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  157. newtarobject_utime(path,ti);
  158. }
  159. void setupfnamevbs(const char *filename) {
  160. fnamevb.used= fnameidlu;
  161. varbufaddstr(&fnamevb,filename);
  162. varbufaddc(&fnamevb,0);
  163. fnametmpvb.used= fnameidlu;
  164. varbufaddstr(&fnametmpvb,filename);
  165. varbufaddstr(&fnametmpvb,DPKGTEMPEXT);
  166. varbufaddc(&fnametmpvb,0);
  167. fnamenewvb.used= fnameidlu;
  168. varbufaddstr(&fnamenewvb,filename);
  169. varbufaddstr(&fnamenewvb,DPKGNEWEXT);
  170. varbufaddc(&fnamenewvb,0);
  171. debug(dbg_eachfiledetail, "setupvnamevbs main=`%s' tmp=`%s' new=`%s'",
  172. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  173. }
  174. int unlinkorrmdir(const char *filename) {
  175. /* Returns 0 on success or -1 on failure, just like unlink & rmdir */
  176. int r, e;
  177. if (!rmdir(filename)) {
  178. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' rmdir OK",filename);
  179. return 0;
  180. }
  181. if (errno != ENOTDIR) {
  182. e= errno;
  183. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' rmdir %s",filename,strerror(e));
  184. errno= e; return -1;
  185. }
  186. r= unlink(filename); e= errno;
  187. debug(dbg_eachfiledetail,"unlinkorrmdir `%s' unlink %s",
  188. filename, r ? strerror(e) : "OK");
  189. errno= e; return r;
  190. }
  191. static struct obstack tar_obs;
  192. static int tarobs_init = 0;
  193. int tarobject(struct TarInfo *ti) {
  194. static struct varbuf conffderefn, hardlinkfn, symlinkfn;
  195. const char *usename;
  196. struct tarcontext *tc= (struct tarcontext*)ti->UserData;
  197. int statr, fd, r, i, existingdirectory;
  198. struct stat stab, stabd;
  199. char databuf[TARBLKSZ];
  200. struct fileinlist *nifd;
  201. struct pkginfo *divpkg, *otherpkg;
  202. struct filepackages *packageslump;
  203. mode_t am;
  204. if (!tarobs_init) {
  205. obstack_init(&tar_obs);
  206. tarobs_init = 1;
  207. }
  208. /* Append to list of files.
  209. * The trailing / put on the end of names in tarfiles has already
  210. * been stripped by TarExtractor (lib/tarfn.c).
  211. */
  212. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  213. nifd->namenode= findnamenode(ti->Name, 0);
  214. nifd->next= 0; *tc->newfilesp= nifd; tc->newfilesp= &nifd->next;
  215. nifd->namenode->flags |= fnnf_new_inarchive;
  216. debug(dbg_eachfile,
  217. "tarobject ti->Name=`%s' Mode=%lo owner=%u.%u Type=%d(%c)"
  218. " ti->LinkName=`%s' namenode=`%s' flags=%o instead=`%s'",
  219. ti->Name, (long)ti->Mode, (unsigned)ti->UserID, (unsigned)ti->GroupID, ti->Type,
  220. ti->Type == '\0' ? '_' :
  221. ti->Type >= '0' && ti->Type <= '6' ? "-hlcbdp"[ti->Type - '0'] : '?',
  222. ti->LinkName,
  223. nifd->namenode->name, nifd->namenode->flags,
  224. nifd->namenode->divert && nifd->namenode->divert->useinstead
  225. ? nifd->namenode->divert->useinstead->name : "<none>");
  226. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  227. divpkg= nifd->namenode->divert->pkg;
  228. forcibleerr(fc_overwritediverted,
  229. _("trying to overwrite `%.250s', which is the "
  230. "diverted version of `%.250s'%.10s%.100s%.10s"),
  231. nifd->namenode->name,
  232. nifd->namenode->divert->camefrom->name,
  233. divpkg ? _(" (package: ") : "",
  234. divpkg ? divpkg->name : "",
  235. divpkg ? ")" : "");
  236. }
  237. usename= namenodetouse(nifd->namenode,tc->pkg)->name + 1; /* Skip the leading `/' */
  238. if (nifd->namenode->flags & fnnf_new_conff) {
  239. /* If it's a conffile we have to extract it next to the installed
  240. * version (ie, we do the usual link-following).
  241. */
  242. if (conffderef(tc->pkg, &conffderefn, usename))
  243. usename= conffderefn.buf;
  244. debug(dbg_conff,"tarobject fnnf_new_conff deref=`%s'",usename);
  245. }
  246. setupfnamevbs(usename);
  247. statr= lstat(fnamevb.buf,&stab);
  248. if (statr) {
  249. /* The lstat failed. */
  250. if (errno != ENOENT && errno != ENOTDIR)
  251. ohshite(_("unable to stat `%.255s' (which I was about to install)"),ti->Name);
  252. /* OK, so it doesn't exist.
  253. * However, it's possible that we were in the middle of some other
  254. * backup/restore operation and were rudely interrupted.
  255. * So, we see if we have .dpkg-tmp, and if so we restore it.
  256. */
  257. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  258. if (errno != ENOENT && errno != ENOTDIR)
  259. ohshite(_("unable to clean up mess surrounding `%.255s' before "
  260. "installing another version"),ti->Name);
  261. debug(dbg_eachfiledetail,"tarobject nonexistent");
  262. } else {
  263. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  264. statr= lstat(fnamevb.buf,&stab);
  265. if (statr) ohshite(_("unable to stat restored `%.255s' before installing"
  266. " another version"), ti->Name);
  267. }
  268. } else {
  269. debug(dbg_eachfiledetail,"tarobject already exists");
  270. }
  271. /* Check to see if it's a directory or link to one and we don't need to
  272. * do anything. This has to be done now so that we don't die due to
  273. * a file overwriting conflict.
  274. */
  275. existingdirectory= 0;
  276. switch (ti->Type) {
  277. case SymbolicLink:
  278. /* If it's already an existing directory, do nothing. */
  279. if (!statr && S_ISDIR(stab.st_mode)) {
  280. debug(dbg_eachfiledetail,"tarobject SymbolicLink exists as directory");
  281. existingdirectory= 1;
  282. }
  283. break;
  284. case Directory:
  285. /* If it's already an existing directory, do nothing. */
  286. if (!stat(fnamevb.buf,&stabd) && S_ISDIR(stabd.st_mode)) {
  287. debug(dbg_eachfiledetail,"tarobject Directory exists");
  288. existingdirectory= 1;
  289. }
  290. break;
  291. case NormalFile0: case NormalFile1:
  292. case CharacterDevice: case BlockDevice: case FIFO:
  293. case HardLink:
  294. break;
  295. default:
  296. ohshit(_("archive contained object `%.255s' of unknown type 0x%x"),ti->Name,ti->Type);
  297. }
  298. if (!existingdirectory) {
  299. for (packageslump= nifd->namenode->packages;
  300. packageslump;
  301. packageslump= packageslump->more) {
  302. for (i=0; i < PERFILEPACKAGESLUMP && packageslump->pkgs[i]; i++) {
  303. otherpkg= packageslump->pkgs[i];
  304. if (otherpkg == tc->pkg) continue;
  305. debug(dbg_eachfile, "tarobject ... found in %s",otherpkg->name);
  306. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  307. /* Right, so we may be diverting this file. This makes the conflict
  308. * OK iff one of us is the diverting package (we don't need to
  309. * check for both being the diverting package, obviously).
  310. */
  311. divpkg= nifd->namenode->divert->pkg;
  312. debug(dbg_eachfile, "tarobject ... diverted, divpkg=%s",
  313. divpkg ? divpkg->name : "<none>");
  314. if (otherpkg == divpkg || tc->pkg == divpkg) continue;
  315. }
  316. /* Nope ? Hmm, file conflict, perhaps. Check Replaces. */
  317. if (otherpkg->clientdata->replacingfilesandsaid) continue;
  318. /* Is the package with the conflicting file in the `config files
  319. * only' state ? If so it must be a config file and we can
  320. * silenty take it over.
  321. */
  322. if (otherpkg->status == stat_configfiles) continue;
  323. /* Perhaps we're removing a conflicting package ? */
  324. if (otherpkg->clientdata->istobe == itb_remove) continue;
  325. if (does_replace(tc->pkg,&tc->pkg->available,otherpkg)) {
  326. printf(_("Replacing files in old package %s ...\n"),otherpkg->name);
  327. otherpkg->clientdata->replacingfilesandsaid= 1;
  328. } else {
  329. if (S_ISDIR(stab.st_mode)) {
  330. forcibleerr(fc_overwritedir, _("trying to overwrite directory `%.250s' "
  331. "in package %.250s with nondirectory"),
  332. nifd->namenode->name,otherpkg->name);
  333. } else {
  334. /* WTA: At this point we are replacing something without a Replaces.
  335. * if the new object is a directory and the previous object does not
  336. * exist assume it's also a directory and don't complain
  337. */
  338. if (! (statr && ti->Type==Directory))
  339. forcibleerr(fc_overwrite,
  340. _("trying to overwrite `%.250s', which is also in package %.250s"),
  341. nifd->namenode->name,otherpkg->name);
  342. }
  343. }
  344. }
  345. }
  346. }
  347. /* Now, at this stage we want to make sure neither of .dpkg-new and .dpkg-tmp
  348. * are hanging around.
  349. */
  350. ensure_pathname_nonexisting(fnamenewvb.buf);
  351. ensure_pathname_nonexisting(fnametmpvb.buf);
  352. if (existingdirectory) return 0;
  353. /* Now we start to do things that we need to be able to undo
  354. * if something goes wrong.
  355. */
  356. push_cleanup(cu_installnew,~ehflag_normaltidy, 0,0, 1,(void*)nifd);
  357. /* Extract whatever it is as .dpkg-new ... */
  358. switch (ti->Type) {
  359. case NormalFile0: case NormalFile1:
  360. /* We create the file with mode 0 to make sure nobody can do anything with
  361. * it until we apply the proper mode, which might be a statoverride.
  362. */
  363. fd= open(fnamenewvb.buf, (O_CREAT|O_EXCL|O_WRONLY), 0);
  364. if (fd < 0) ohshite(_("unable to create `%.255s'"),ti->Name);
  365. push_cleanup(cu_closefd,ehflag_bombout, 0,0, 1,(void*)fd);
  366. debug(dbg_eachfiledetail,"tarobject NormalFile[01] open size=%lu",
  367. (unsigned long)ti->Size);
  368. fd_fd_copy(tc->backendpipe, fd, ti->Size, _("backend dpkg-deb during `%.255s'"),ti->Name);
  369. r= ti->Size % TARBLKSZ;
  370. if (r > 0) r= read(tc->backendpipe,databuf,TARBLKSZ - r);
  371. if (nifd->namenode->statoverride)
  372. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  373. nifd->namenode->statoverride->uid,
  374. nifd->namenode->statoverride->gid,
  375. nifd->namenode->statoverride->mode);
  376. if (fchown(fd,
  377. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  378. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  379. ohshite(_("error setting ownership of `%.255s'"),ti->Name);
  380. am=(nifd->namenode->statoverride ? nifd->namenode->statoverride->mode : ti->Mode) & ~S_IFMT;
  381. if (fchmod(fd,am))
  382. ohshite(_("error setting permissions of `%.255s'"),ti->Name);
  383. pop_cleanup(ehflag_normaltidy); /* fd= open(fnamenewvb.buf) */
  384. if (close(fd))
  385. ohshite(_("error closing/writing `%.255s'"),ti->Name);
  386. newtarobject_utime(fnamenewvb.buf,ti);
  387. break;
  388. case FIFO:
  389. if (mkfifo(fnamenewvb.buf,0))
  390. ohshite(_("error creating pipe `%.255s'"),ti->Name);
  391. debug(dbg_eachfiledetail,"tarobject FIFO");
  392. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  393. break;
  394. case CharacterDevice: case BlockDevice:
  395. if (mknod(fnamenewvb.buf,0,ti->Device))
  396. ohshite(_("error creating device `%.255s'"),ti->Name);
  397. debug(dbg_eachfiledetail,"tarobject CharacterDevice|BlockDevice");
  398. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  399. break;
  400. case HardLink:
  401. varbufreset(&hardlinkfn);
  402. varbufaddstr(&hardlinkfn,instdir); varbufaddc(&hardlinkfn,'/');
  403. varbufaddstr(&hardlinkfn,ti->LinkName); varbufaddc(&hardlinkfn,0);
  404. if (link(hardlinkfn.buf,fnamenewvb.buf))
  405. ohshite(_("error creating hard link `%.255s'"),ti->Name);
  406. debug(dbg_eachfiledetail,"tarobject HardLink");
  407. newtarobject_allmodes(fnamenewvb.buf,ti, nifd->namenode->statoverride);
  408. break;
  409. case SymbolicLink:
  410. /* We've already cheched for an existing directory. */
  411. if (symlink(ti->LinkName,fnamenewvb.buf))
  412. ohshite(_("error creating symbolic link `%.255s'"),ti->Name);
  413. debug(dbg_eachfiledetail,"tarobject SymbolicLink creating");
  414. #ifdef HAVE_LCHOWN
  415. if (lchown(fnamenewvb.buf,
  416. #else
  417. if (chown(fnamenewvb.buf,
  418. #endif
  419. nifd->namenode->statoverride ? nifd->namenode->statoverride->uid : ti->UserID,
  420. nifd->namenode->statoverride ? nifd->namenode->statoverride->gid : ti->GroupID))
  421. ohshite(_("error setting ownership of symlink `%.255s'"),ti->Name);
  422. break;
  423. case Directory:
  424. /* We've already checked for an existing directory. */
  425. if (mkdir(fnamenewvb.buf,0))
  426. ohshite(_("error creating directory `%.255s'"),ti->Name);
  427. debug(dbg_eachfiledetail,"tarobject Directory creating");
  428. newtarobject_allmodes(fnamenewvb.buf,ti,nifd->namenode->statoverride);
  429. break;
  430. default:
  431. internerr("bad tar type, but already checked");
  432. }
  433. /*
  434. * Now we have extracted the new object in .dpkg-new (or, if the
  435. * file already exists as a directory and we were trying to extract
  436. * a directory or symlink, we returned earlier, so we don't need
  437. * to worry about that here).
  438. */
  439. /* First, check to see if it's a conffile. If so we don't install
  440. * it now - we leave it in .dpkg-new for --configure to take care of
  441. */
  442. if (nifd->namenode->flags & fnnf_new_conff) {
  443. debug(dbg_conffdetail,"tarobject conffile extracted");
  444. nifd->namenode->flags |= fnnf_elide_other_lists;
  445. return 0;
  446. }
  447. /* Now we install it. If we can do an atomic overwrite we do so.
  448. * If not we move aside the old file and then install the new.
  449. * The backup file will be deleted later.
  450. */
  451. if (statr) { /* Don't try to back it up if it didn't exist. */
  452. debug(dbg_eachfiledetail,"tarobject new - no backup");
  453. } else {
  454. if (ti->Type == Directory || S_ISDIR(stab.st_mode)) {
  455. /* One of the two is a directory - can't do atomic install. */
  456. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  457. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  458. if (rename(fnamevb.buf,fnametmpvb.buf))
  459. ohshite(_("unable to move aside `%.255s' to install new version"),ti->Name);
  460. } else if (S_ISLNK(stab.st_mode)) {
  461. /* We can't make a symlink with two hardlinks, so we'll have to copy it.
  462. * (Pretend that making a copy of a symlink is the same as linking to it.)
  463. */
  464. varbufreset(&symlinkfn);
  465. do {
  466. varbufextend(&symlinkfn);
  467. r= readlink(fnamevb.buf,symlinkfn.buf,symlinkfn.size);
  468. if (r<0) ohshite(_("unable to read link `%.255s'"),ti->Name);
  469. } while (r == symlinkfn.size);
  470. symlinkfn.used= r; varbufaddc(&symlinkfn,0);
  471. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  472. ohshite(_("unable to make backup symlink for `%.255s'"),ti->Name);
  473. #ifdef HAVE_LCHOWN
  474. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  475. #else
  476. if (chown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  477. #endif
  478. ohshite(_("unable to chown backup symlink for `%.255s'"),ti->Name);
  479. } else {
  480. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  481. if (link(fnamevb.buf,fnametmpvb.buf))
  482. ohshite(_("unable to make backup link of `%.255s' before installing new version"),
  483. ti->Name);
  484. }
  485. }
  486. if (rename(fnamenewvb.buf,fnamevb.buf))
  487. ohshite(_("unable to install new version of `%.255s'"),ti->Name);
  488. nifd->namenode->flags |= fnnf_elide_other_lists;
  489. debug(dbg_eachfiledetail,"tarobject done and installed");
  490. return 0;
  491. }
  492. static int try_remove_can(struct deppossi *pdep,
  493. struct pkginfo *fixbyrm,
  494. const char *why) {
  495. struct packageinlist *newdeconf;
  496. if (force_depends(pdep)) {
  497. fprintf(stderr, _("dpkg: warning - "
  498. "ignoring dependency problem with removal of %s:\n%s"),
  499. fixbyrm->name, why);
  500. return 1;
  501. } else if (f_autodeconf) {
  502. if (pdep->up->up->installed.essential) {
  503. if (fc_removeessential) {
  504. fprintf(stderr, _("dpkg: warning - considering deconfiguration of essential\n"
  505. " package %s, to enable removal of %s.\n"),
  506. pdep->up->up->name,fixbyrm->name);
  507. } else {
  508. fprintf(stderr, _("dpkg: no, %s is essential, will not deconfigure\n"
  509. " it in order to enable removal of %s.\n"),
  510. pdep->up->up->name,fixbyrm->name);
  511. return 0;
  512. }
  513. }
  514. pdep->up->up->clientdata->istobe= itb_deconfigure;
  515. newdeconf= obstack_alloc(&tar_obs, sizeof(struct packageinlist));
  516. newdeconf->next= deconfigure;
  517. newdeconf->pkg= pdep->up->up;
  518. deconfigure= newdeconf;
  519. return 1;
  520. } else {
  521. fprintf(stderr, _("dpkg: no, cannot remove %s (--auto-deconfigure will help):\n%s"),
  522. fixbyrm->name, why);
  523. return 0;
  524. }
  525. }
  526. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  527. const char *pfilename) {
  528. struct pkginfo *fixbyrm;
  529. struct deppossi *pdep, flagdeppossi;
  530. struct varbuf conflictwhy, removalwhy;
  531. struct dependency *providecheck;
  532. varbufinit(&conflictwhy);
  533. varbufinit(&removalwhy);
  534. fixbyrm= 0;
  535. if (depisok(dep, &conflictwhy, &fixbyrm, 0)) {
  536. varbuffree(&conflictwhy);
  537. varbuffree(&removalwhy);
  538. return;
  539. }
  540. if (fixbyrm) {
  541. ensure_package_clientdata(fixbyrm);
  542. if (fixbyrm->clientdata->istobe == itb_installnew) {
  543. fixbyrm= dep->up;
  544. ensure_package_clientdata(fixbyrm);
  545. }
  546. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  547. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  548. does_replace(pkg,&pkg->available,fixbyrm)) &&
  549. (!fixbyrm->installed.essential || fc_removeessential)))) {
  550. assert(fixbyrm->clientdata->istobe == itb_normal);
  551. fixbyrm->clientdata->istobe= itb_remove;
  552. fprintf(stderr, _("dpkg: considering removing %s in favour of %s ...\n"),
  553. fixbyrm->name, pkg->name);
  554. if (fixbyrm->status != stat_installed) {
  555. fprintf(stderr,
  556. _("%s is not properly installed - ignoring any dependencies on it.\n"),
  557. fixbyrm->name);
  558. pdep= 0;
  559. } else {
  560. for (pdep= fixbyrm->installed.depended;
  561. pdep;
  562. pdep= pdep->nextrev) {
  563. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  564. continue;
  565. if (depisok(pdep->up, &removalwhy, 0,0)) continue;
  566. varbufaddc(&removalwhy,0);
  567. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  568. break;
  569. }
  570. if (!pdep) {
  571. /* If we haven't found a reason not to yet, let's look some more. */
  572. for (providecheck= fixbyrm->installed.depends;
  573. providecheck;
  574. providecheck= providecheck->next) {
  575. if (providecheck->type != dep_provides) continue;
  576. for (pdep= providecheck->list->ed->installed.depended;
  577. pdep;
  578. pdep= pdep->nextrev) {
  579. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  580. continue;
  581. if (depisok(pdep->up, &removalwhy, 0,0)) continue;
  582. varbufaddc(&removalwhy,0);
  583. fprintf(stderr, _("dpkg"
  584. ": may have trouble removing %s, as it provides %s ...\n"),
  585. fixbyrm->name, providecheck->list->ed->name);
  586. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  587. goto break_from_both_loops_at_once;
  588. }
  589. }
  590. break_from_both_loops_at_once:;
  591. }
  592. }
  593. if (!pdep && skip_due_to_hold(fixbyrm)) {
  594. pdep= &flagdeppossi;
  595. }
  596. if (!pdep && (fixbyrm->eflag & eflagf_reinstreq)) {
  597. if (fc_removereinstreq) {
  598. fprintf(stderr, _("dpkg: package %s requires reinstallation, but will"
  599. " remove anyway as you request.\n"), fixbyrm->name);
  600. } else {
  601. fprintf(stderr, _("dpkg: package %s requires reinstallation, "
  602. "will not remove.\n"), fixbyrm->name);
  603. pdep= &flagdeppossi;
  604. }
  605. }
  606. if (!pdep) {
  607. /* if this gets triggered, it means a package has > 10 conflicts/replaces
  608. * pairs, which is the package's fault
  609. */
  610. assert(cflict_index < sizeof(conflictor));
  611. /* This conflict is OK - we'll remove the conflictor. */
  612. conflictor[cflict_index++]= fixbyrm;
  613. varbuffree(&conflictwhy); varbuffree(&removalwhy);
  614. fprintf(stderr, _("dpkg: yes, will remove %s in favour of %s.\n"),
  615. fixbyrm->name, pkg->name);
  616. return;
  617. }
  618. fixbyrm->clientdata->istobe= itb_normal; /* put it back */
  619. }
  620. }
  621. varbufaddc(&conflictwhy,0);
  622. fprintf(stderr, _("dpkg: regarding %s containing %s:\n%s"),
  623. pfilename, pkg->name, conflictwhy.buf);
  624. if (!force_conflicts(dep->list))
  625. ohshit(_("conflicting packages - not installing %.250s"),pkg->name);
  626. fprintf(stderr, _("dpkg: warning - ignoring conflict, may proceed anyway !\n"));
  627. varbuffree(&conflictwhy);
  628. return;
  629. }
  630. void cu_cidir(int argc, void **argv) {
  631. char *cidir= (char*)argv[0];
  632. char *cidirrest= (char*)argv[1];
  633. cidirrest[-1]= 0;
  634. ensure_pathname_nonexisting(cidir);
  635. }
  636. void cu_fileslist(int argc, void **argv) {
  637. if (tarobs_init) {
  638. obstack_free(&tar_obs, 0);
  639. tarobs_init = 0;
  640. }
  641. }
  642. void archivefiles(const char *const *argv) {
  643. const char *volatile thisarg;
  644. const char *const *volatile argp;
  645. jmp_buf ejbuf;
  646. int pi[2], fc, nfiles, c, i;
  647. FILE *pf;
  648. static struct varbuf findoutput;
  649. const char **arglist;
  650. char *p;
  651. modstatdb_init(admindir,
  652. f_noact ? msdbrw_readonly
  653. : cipaction->arg == act_avail ? msdbrw_write
  654. : fc_nonroot ? msdbrw_write
  655. : msdbrw_needsuperuser);
  656. checkpath();
  657. if (f_recursive) {
  658. if (!*argv)
  659. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  660. m_pipe(pi);
  661. if (!(fc= m_fork())) {
  662. const char *const *ap;
  663. int i;
  664. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  665. for (i=0, ap=argv; *ap; ap++, i++);
  666. arglist= m_malloc(sizeof(char*)*(i+15));
  667. arglist[0]= FIND;
  668. for (i=1, ap=argv; *ap; ap++, i++) {
  669. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  670. char *a;
  671. a= m_malloc(strlen(*ap)+10);
  672. strcpy(a,"./");
  673. strcat(a,*ap);
  674. arglist[i]= a;
  675. } else {
  676. arglist[i]= *ap;
  677. }
  678. }
  679. arglist[i++]= "-follow"; /* When editing these, make sure that */
  680. arglist[i++]= "-name"; /* arglist is mallocd big enough, above. */
  681. arglist[i++]= ARCHIVE_FILENAME_PATTERN;
  682. arglist[i++]= "-type";
  683. arglist[i++]= "f";
  684. arglist[i++]= "-print0";
  685. arglist[i++]= 0;
  686. execvp(FIND, (char* const*)arglist);
  687. ohshite(_("failed to exec find for --recursive"));
  688. }
  689. close(pi[1]);
  690. nfiles= 0;
  691. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  692. varbufreset(&findoutput);
  693. while ((c= fgetc(pf)) != EOF) {
  694. varbufaddc(&findoutput,c);
  695. if (!c) nfiles++;
  696. }
  697. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  698. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  699. waitsubproc(fc,"find",0,0);
  700. if (!nfiles)
  701. ohshit(_("searched, but found no packages (files matching *.deb)"));
  702. varbufaddc(&findoutput,0);
  703. varbufaddc(&findoutput,0);
  704. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  705. p= findoutput.buf; i=0;
  706. while (*p) {
  707. arglist[i++]= p;
  708. while ((c= *p++) != 0);
  709. }
  710. arglist[i]= 0;
  711. argp= arglist;
  712. } else {
  713. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  714. cipaction->olong);
  715. argp= argv;
  716. }
  717. currenttime= time(0);
  718. varbufinit(&fnamevb);
  719. varbufinit(&fnametmpvb);
  720. varbufinit(&fnamenewvb);
  721. varbufaddstr(&fnamevb,instdir); varbufaddc(&fnamevb,'/');
  722. varbufaddstr(&fnametmpvb,instdir); varbufaddc(&fnametmpvb,'/');
  723. varbufaddstr(&fnamenewvb,instdir); varbufaddc(&fnamenewvb,'/');
  724. fnameidlu= fnamevb.used;
  725. ensure_diversions();
  726. ensure_statoverrides();
  727. while ((thisarg= *argp++) != 0) {
  728. if (setjmp(ejbuf)) {
  729. error_unwind(ehflag_bombout);
  730. if (onerr_abort > 0) break;
  731. continue;
  732. }
  733. push_error_handler(&ejbuf,print_error_perpackage,thisarg);
  734. process_archive(thisarg);
  735. onerr_abort++;
  736. if (ferror(stdout)) werr("stdout");
  737. if (ferror(stderr)) werr("stderr");
  738. onerr_abort--;
  739. set_error_display(0,0);
  740. error_unwind(ehflag_normaltidy);
  741. }
  742. switch (cipaction->arg) {
  743. case act_install:
  744. case act_configure:
  745. case act_remove:
  746. case act_purge:
  747. process_queue();
  748. case act_unpack:
  749. case act_avail:
  750. break;
  751. default:
  752. internerr("unknown action");
  753. }
  754. modstatdb_shutdown();
  755. }
  756. int wanttoinstall(struct pkginfo *pkg, const struct versionrevision *ver, int saywhy) {
  757. /* Decide whether we want to install a new version of the package.
  758. * ver is the version we might want to install. If saywhy is 1 then
  759. * if we skip the package we say what we are doing (and, if we are
  760. * selecting a previously deselected package, say so and actually do
  761. * the select). want_install returns 0 if the package should be
  762. * skipped and 1 if it should be installed.
  763. *
  764. * ver may be 0, in which case saywhy must be 0 and want_install may
  765. * also return -1 to mean it doesn't know because it would depend on
  766. * the version number.
  767. */
  768. enum versiondisplayepochwhen needepochs;
  769. int r;
  770. if (pkg->want != want_install) {
  771. if (f_alsoselect) {
  772. if (saywhy) {
  773. printf(_("Selecting previously deselected package %s.\n"),pkg->name);
  774. pkg->want= want_install;
  775. }
  776. return 1;
  777. } else {
  778. if (saywhy) printf(_("Skipping deselected package %s.\n"),pkg->name);
  779. return 0;
  780. }
  781. }
  782. if (pkg->status != stat_installed) return 1;
  783. if (!ver) return -1;
  784. r= versioncompare(ver,&pkg->installed.version);
  785. if (r > 0) {
  786. return 1;
  787. } else if (r == 0) {
  788. if (f_skipsame && /* same version fully installed ? */
  789. pkg->status == stat_installed && !(pkg->eflag &= eflagf_reinstreq)) {
  790. if (saywhy) fprintf(stderr, _("Version %.250s of %.250s already installed, "
  791. "skipping.\n"),
  792. versiondescribe(&pkg->installed.version,vdew_never),
  793. pkg->name);
  794. return 0;
  795. } else {
  796. return 1;
  797. }
  798. } else {
  799. needepochs= epochsdiffer(&pkg->available.version,&pkg->installed.version) ?
  800. vdew_always : vdew_never;
  801. if (fc_downgrade) {
  802. if (saywhy) fprintf(stderr, _("%s - warning: downgrading %.250s "
  803. "from %.250s to %.250s.\n"), DPKG, pkg->name,
  804. versiondescribe(&pkg->installed.version,needepochs),
  805. versiondescribe(&pkg->available.version,needepochs));
  806. return 1;
  807. } else {
  808. if (saywhy) fprintf(stderr, _("Will not downgrade %.250s from version %.250s "
  809. "to %.250s, skipping.\n"), pkg->name,
  810. versiondescribe(&pkg->installed.version,needepochs),
  811. versiondescribe(&pkg->available.version,needepochs));
  812. return 0;
  813. }
  814. }
  815. }
  816. /* vi: ts=8 sw=2
  817. */