archives.c 28 KB

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