archives.c 32 KB

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