archives.c 34 KB

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