archives.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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: case FIFO:
  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. if (S_ISDIR(stab.st_mode)) {
  320. forcibleerr(fc_overwritedir, "trying to overwrite directory `%.250s' "
  321. "in package %.250s with nondirectory",
  322. nifd->namenode->name,otherpkg->name);
  323. } else {
  324. forcibleerr(fc_overwrite,
  325. "trying to overwrite `%.250s', which is also in package %.250s",
  326. nifd->namenode->name,otherpkg->name);
  327. }
  328. }
  329. }
  330. }
  331. }
  332. /* Now, at this stage we want to make sure neither of .dpkg-new and .dpkg-tmp
  333. * are hanging around.
  334. */
  335. ensure_pathname_nonexisting(fnamenewvb.buf);
  336. ensure_pathname_nonexisting(fnametmpvb.buf);
  337. if (existingdirectory) return 0;
  338. /* Now we start to do things that we need to be able to undo
  339. * if something goes wrong.
  340. */
  341. push_cleanup(cu_installnew,~ehflag_normaltidy, 0,0, 1,(void*)nifd);
  342. /* Extract whatever it is as .dpkg-new ... */
  343. switch (ti->Type) {
  344. case NormalFile0: case NormalFile1:
  345. fd= open(fnamenewvb.buf, O_CREAT|O_EXCL|O_WRONLY,
  346. ti->Mode & (S_IRUSR|S_IRGRP|S_IROTH));
  347. if (fd < 0) ohshite("unable to create `%.255s'",ti->Name);
  348. thefile= fdopen(fd,"w");
  349. if (!thefile) { close(fd); ohshite("unable to fdopen for `%.255s'",ti->Name); }
  350. push_cleanup(cu_closefile,ehflag_bombout, 0,0, 1,(void*)thefile);
  351. debug(dbg_eachfiledetail,"tarobject NormalFile[01] open size=%lu",
  352. (unsigned long)ti->Size);
  353. for (sz= ti->Size; sz > 0; sz -= wsz) {
  354. wsz= sz > TARBLKSZ ? TARBLKSZ : sz;
  355. r= fread(databuf,1,TARBLKSZ,tc->backendpipe);
  356. if (r<TARBLKSZ) {
  357. if (ferror(tc->backendpipe)) {
  358. ohshite("error reading " BACKEND " during `%.255s'",ti->Name);
  359. } else {
  360. errno= 0;
  361. return -1;
  362. }
  363. }
  364. if (fwrite(databuf,1,wsz,thefile) != wsz)
  365. ohshite("error writing to `%.255s'",ti->Name);
  366. }
  367. if (fchown(fd,ti->UserID,ti->GroupID))
  368. ohshite("error setting ownership of `%.255s'",ti->Name);
  369. if (fchmod(fd,ti->Mode & ~S_IFMT))
  370. ohshite("error setting permissions of `%.255s'",ti->Name);
  371. pop_cleanup(ehflag_normaltidy); /* thefile= fdopen(fd) */
  372. if (fclose(thefile))
  373. ohshite("error closing/writing `%.255s'",ti->Name);
  374. newtarobject_utime(fnamenewvb.buf,ti);
  375. break;
  376. case FIFO:
  377. if (mkfifo(fnamenewvb.buf,ti->Mode & S_IFMT))
  378. ohshite("error creating pipe `%.255s'",ti->Name);
  379. debug(dbg_eachfiledetail,"tarobject FIFO");
  380. newtarobject_allmodes(fnamenewvb.buf,ti);
  381. break;
  382. case CharacterDevice: case BlockDevice:
  383. if (mknod(fnamenewvb.buf,ti->Mode & S_IFMT,ti->Device))
  384. ohshite("error creating device `%.255s'",ti->Name);
  385. debug(dbg_eachfiledetail,"tarobject CharacterDevice|BlockDevice");
  386. newtarobject_allmodes(fnamenewvb.buf,ti);
  387. break;
  388. case HardLink:
  389. varbufreset(&hardlinkfn);
  390. varbufaddstr(&hardlinkfn,instdir); varbufaddc(&hardlinkfn,'/');
  391. varbufaddstr(&hardlinkfn,ti->LinkName); varbufaddc(&hardlinkfn,0);
  392. if (link(hardlinkfn.buf,fnamenewvb.buf))
  393. ohshite("error creating hard link `%.255s'",ti->Name);
  394. debug(dbg_eachfiledetail,"tarobject HardLink");
  395. newtarobject_allmodes(fnamenewvb.buf,ti);
  396. break;
  397. case SymbolicLink:
  398. /* We've already cheched for an existing directory. */
  399. if (symlink(ti->LinkName,fnamenewvb.buf))
  400. ohshite("error creating symbolic link `%.255s'",ti->Name);
  401. debug(dbg_eachfiledetail,"tarobject SymbolicLink creating");
  402. if (chown(fnamenewvb.buf,ti->UserID,ti->GroupID))
  403. ohshite("error setting ownership of symlink `%.255s'",ti->Name);
  404. break;
  405. case Directory:
  406. /* We've already checked for an existing directory. */
  407. if (mkdir(fnamenewvb.buf,
  408. ti->Mode & (S_IRUSR|S_IRGRP|S_IROTH | S_IXUSR|S_IXGRP|S_IXOTH)))
  409. ohshite("error creating directory `%.255s'",ti->Name);
  410. debug(dbg_eachfiledetail,"tarobject Directory creating");
  411. newtarobject_allmodes(fnamenewvb.buf,ti);
  412. break;
  413. default:
  414. internerr("bad tar type, but already checked");
  415. }
  416. /*
  417. * Now we have extracted the new object in .dpkg-new (or, if the
  418. * file already exists as a directory and we were trying to extract
  419. * a directory or symlink, we returned earlier, so we don't need
  420. * to worry about that here).
  421. */
  422. /* First, check to see if it's a conffile. If so we don't install
  423. * it now - we leave it in .dpkg-new for --configure to take care of
  424. */
  425. if (nifd->namenode->flags & fnnf_new_conff) {
  426. debug(dbg_conffdetail,"tarobject conffile extracted");
  427. nifd->namenode->flags |= fnnf_elide_other_lists;
  428. return 0;
  429. }
  430. /* Now we install it. If we can do an atomic overwrite we do so.
  431. * If not we move aside the old file and then install the new.
  432. * The backup file will be deleted later.
  433. */
  434. if (statr) { /* Don't try to back it up if it didn't exist. */
  435. debug(dbg_eachfiledetail,"tarobject new - no backup");
  436. } else {
  437. if (ti->Type == Directory || S_ISDIR(stab.st_mode)) {
  438. /* One of the two is a directory - can't do atomic install. */
  439. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  440. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  441. if (rename(fnamevb.buf,fnametmpvb.buf))
  442. ohshite("unable to move aside `%.255s' to install new version",ti->Name);
  443. } else if (S_ISLNK(stab.st_mode)) {
  444. /* We can't make a symlink with two hardlinks, so we'll have to copy it.
  445. * (Pretend that making a copy of a symlink is the same as linking to it.)
  446. */
  447. varbufreset(&symlinkfn);
  448. do {
  449. varbufextend(&symlinkfn);
  450. r= readlink(fnamevb.buf,symlinkfn.buf,symlinkfn.size);
  451. if (r<0) ohshite("unable to read link `%.255s'",ti->Name);
  452. } while (r == symlinkfn.size);
  453. symlinkfn.used= r; varbufaddc(&symlinkfn,0);
  454. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  455. ohshite("unable to make backup symlink for `%.255s'",ti->Name);
  456. if (chown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  457. ohshite("unable to chown backup symlink for `%.255s'",ti->Name);
  458. } else {
  459. debug(dbg_eachfiledetail,"tarobject nondirectory, `link' backup");
  460. if (link(fnamevb.buf,fnametmpvb.buf))
  461. ohshite("unable to make backup link of `%.255s' before installing new version",
  462. ti->Name);
  463. }
  464. }
  465. if (rename(fnamenewvb.buf,fnamevb.buf))
  466. ohshite("unable to install new version of `%.255s'",ti->Name);
  467. nifd->namenode->flags |= fnnf_elide_other_lists;
  468. debug(dbg_eachfiledetail,"tarobject done and installed");
  469. return 0;
  470. }
  471. static int try_remove_can(struct deppossi *pdep,
  472. struct pkginfo *fixbyrm,
  473. const char *why) {
  474. struct packageinlist *newdeconf;
  475. if (force_depends(pdep)) {
  476. fprintf(stderr, DPKG ": warning - "
  477. "ignoring dependency problem with removal of %s:\n%s",
  478. fixbyrm->name, why);
  479. return 1;
  480. } else if (f_autodeconf) {
  481. if (pdep->up->up->installed.essential) {
  482. if (fc_removeessential) {
  483. fprintf(stderr, DPKG ": warning - considering deconfiguration of essential\n"
  484. " package %s, to enable removal of %s.\n",
  485. pdep->up->up->name,fixbyrm->name);
  486. } else {
  487. fprintf(stderr, DPKG ": no, %s is essential, will not deconfigure\n"
  488. " it in order to enable removal of %s.\n",
  489. pdep->up->up->name,fixbyrm->name);
  490. return 0;
  491. }
  492. }
  493. pdep->up->up->clientdata->istobe= itb_deconfigure;
  494. newdeconf= m_malloc(sizeof(struct packageinlist));
  495. newdeconf->next= deconfigure;
  496. newdeconf->pkg= pdep->up->up;
  497. deconfigure= newdeconf;
  498. return 1;
  499. } else {
  500. fprintf(stderr, DPKG ": no, cannot remove %s (--auto-deconfigure will help):\n%s",
  501. fixbyrm->name, why);
  502. return 0;
  503. }
  504. }
  505. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  506. const char *pfilename, struct pkginfo **conflictorp) {
  507. struct pkginfo *fixbyrm;
  508. struct deppossi *pdep, flagdeppossi;
  509. struct varbuf conflictwhy, removalwhy;
  510. struct dependency *providecheck;
  511. varbufinit(&conflictwhy);
  512. varbufinit(&removalwhy);
  513. fixbyrm= 0;
  514. if (depisok(dep, &conflictwhy, *conflictorp ? 0 : &fixbyrm, 0)) {
  515. varbuffree(&conflictwhy);
  516. varbuffree(&removalwhy);
  517. return;
  518. }
  519. if (fixbyrm) {
  520. ensure_package_clientdata(fixbyrm);
  521. if (fixbyrm->clientdata->istobe == itb_installnew) {
  522. fixbyrm= dep->up;
  523. ensure_package_clientdata(fixbyrm);
  524. }
  525. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  526. (((fixbyrm->want != want_install && fixbyrm->want != want_hold) ||
  527. does_replace(pkg,&pkg->available,fixbyrm)) &&
  528. (!fixbyrm->installed.essential || fc_removeessential)))) {
  529. assert(fixbyrm->clientdata->istobe == itb_normal);
  530. fixbyrm->clientdata->istobe= itb_remove;
  531. fprintf(stderr, DPKG ": considering removing %s in favour of %s ...\n",
  532. fixbyrm->name, pkg->name);
  533. if (fixbyrm->status != stat_installed) {
  534. fprintf(stderr,
  535. "%s is not properly installed - ignoring any dependencies on it.\n",
  536. fixbyrm->name);
  537. pdep= 0;
  538. } else {
  539. for (pdep= fixbyrm->installed.depended;
  540. pdep;
  541. pdep= pdep->nextrev) {
  542. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  543. continue;
  544. if (depisok(pdep->up, &removalwhy, 0,0)) continue;
  545. varbufaddc(&removalwhy,0);
  546. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  547. break;
  548. }
  549. if (!pdep) {
  550. /* If we haven't found a reason not to yet, let's look some more. */
  551. for (providecheck= fixbyrm->installed.depends;
  552. providecheck;
  553. providecheck= providecheck->next) {
  554. if (providecheck->type != dep_provides) continue;
  555. for (pdep= providecheck->list->ed->installed.depended;
  556. pdep;
  557. pdep= pdep->nextrev) {
  558. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  559. continue;
  560. if (depisok(pdep->up, &removalwhy, 0,0)) continue;
  561. varbufaddc(&removalwhy,0);
  562. fprintf(stderr, DPKG
  563. ": may have trouble removing %s, as it provides %s ...\n",
  564. fixbyrm->name, providecheck->list->ed->name);
  565. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  566. goto break_from_both_loops_at_once;
  567. }
  568. }
  569. break_from_both_loops_at_once:;
  570. }
  571. }
  572. if (!pdep && skip_due_to_hold(fixbyrm)) {
  573. pdep= &flagdeppossi;
  574. }
  575. if (!pdep && (fixbyrm->eflag & eflagf_reinstreq)) {
  576. if (fc_removereinstreq) {
  577. fprintf(stderr, DPKG ": package %s requires reinstallation, but will"
  578. " remove anyway as you request.\n", fixbyrm->name);
  579. } else {
  580. fprintf(stderr, DPKG ": package %s requires reinstallation, "
  581. "will not remove.\n", fixbyrm->name);
  582. pdep= &flagdeppossi;
  583. }
  584. }
  585. if (!pdep) {
  586. /* This conflict is OK - we'll remove the conflictor. */
  587. *conflictorp= fixbyrm;
  588. varbuffree(&conflictwhy); varbuffree(&removalwhy);
  589. fprintf(stderr, DPKG ": yes, will remove %s in favour of %s.\n",
  590. fixbyrm->name, pkg->name);
  591. return;
  592. }
  593. fixbyrm->clientdata->istobe= itb_normal; /* put it back */
  594. }
  595. }
  596. varbufaddc(&conflictwhy,0);
  597. fprintf(stderr, DPKG ": regarding %s containing %s:\n%s",
  598. pfilename, pkg->name, conflictwhy.buf);
  599. if (!force_conflicts(dep->list))
  600. ohshit("conflicting packages - not installing %.250s",pkg->name);
  601. fprintf(stderr, DPKG ": warning - ignoring conflict, may proceed anyway !\n");
  602. varbuffree(&conflictwhy);
  603. return;
  604. }
  605. void cu_cidir(int argc, void **argv) {
  606. char *cidir= (char*)argv[0];
  607. char *cidirrest= (char*)argv[1];
  608. cidirrest[-1]= 0;
  609. ensure_pathname_nonexisting(cidir);
  610. }
  611. void cu_fileslist(int argc, void **argv) {
  612. struct fileinlist **headp= (struct fileinlist**)argv[0];
  613. struct fileinlist *current, *next;
  614. for (current= *headp; current; current= next) {
  615. next= current->next;
  616. free(current);
  617. }
  618. }
  619. void archivefiles(const char *const *argv) {
  620. const char *volatile thisarg;
  621. const char *const *volatile argp;
  622. jmp_buf ejbuf;
  623. int pi[2], fc, nfiles, c, i;
  624. FILE *pf;
  625. static struct varbuf findoutput;
  626. const char **arglist;
  627. char *p;
  628. checkpath();
  629. if (f_recursive) {
  630. if (!*argv)
  631. badusage("--%s --recursive needs at least one path argument",cipaction->olong);
  632. m_pipe(pi);
  633. if (!(fc= m_fork())) {
  634. const char *const *ap;
  635. int i;
  636. m_dup2(pi[1],1); close(pi[0]);
  637. for (i=0, ap=argv; *ap; ap++, i++);
  638. arglist= m_malloc(sizeof(char*)*(i+15));
  639. arglist[0]= FIND;
  640. for (i=1, ap=argv; *ap; ap++, i++) {
  641. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  642. char *a;
  643. a= m_malloc(strlen(*ap)+10);
  644. strcpy(a,"./");
  645. strcat(a,*ap);
  646. arglist[i]= a;
  647. } else {
  648. arglist[i]= *ap;
  649. }
  650. }
  651. arglist[i++]= "-follow"; /* When editing these, make sure that */
  652. arglist[i++]= "-name"; /* arglist is mallocd big enough, above. */
  653. arglist[i++]= ARCHIVE_FILENAME_PATTERN;
  654. arglist[i++]= "-type";
  655. arglist[i++]= "f";
  656. arglist[i++]= "-print0";
  657. arglist[i++]= 0;
  658. execvp(FIND, (char* const*)arglist);
  659. ohshite("failed to exec " FIND " for --recursive");
  660. }
  661. nfiles= 0;
  662. pf= fdopen(pi[0],"r"); if (!pf) ohshite("failed to fdopen find's pipe");
  663. close(pi[1]);
  664. varbufreset(&findoutput);
  665. while ((c= fgetc(pf)) != EOF) {
  666. varbufaddc(&findoutput,c);
  667. if (!c) nfiles++;
  668. }
  669. if (ferror(pf)) ohshite("error reading find's pipe");
  670. if (fclose(pf)) ohshite("error closing find's pipe");
  671. waitsubproc(fc,"find",0);
  672. if (!nfiles) ohshit("searched, but found no packages (files matching "
  673. ARCHIVE_FILENAME_PATTERN ")");
  674. varbufaddc(&findoutput,0);
  675. varbufaddc(&findoutput,0);
  676. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  677. p= findoutput.buf; i=0;
  678. while (*p) {
  679. arglist[i++]= p;
  680. while ((c= *p++) != 0);
  681. }
  682. arglist[i]= 0;
  683. argp= arglist;
  684. } else {
  685. if (!*argv) badusage("--%s needs at least one package archive file argument",
  686. cipaction->olong);
  687. argp= argv;
  688. }
  689. modstatdb_init(admindir,
  690. f_noact ? msdbrw_readonly
  691. : cipaction->arg == act_avail ? msdbrw_write
  692. : fc_nonroot ? msdbrw_write
  693. : msdbrw_needsuperuser);
  694. currenttime= time(0);
  695. varbufaddstr(&fnamevb,instdir); varbufaddc(&fnamevb,'/');
  696. varbufaddstr(&fnametmpvb,instdir); varbufaddc(&fnametmpvb,'/');
  697. varbufaddstr(&fnamenewvb,instdir); varbufaddc(&fnamenewvb,'/');
  698. fnameidlu= fnamevb.used;
  699. ensure_diversions();
  700. while ((thisarg= *argp++) != 0) {
  701. if (setjmp(ejbuf)) {
  702. error_unwind(ehflag_bombout);
  703. if (onerr_abort > 0) break;
  704. continue;
  705. }
  706. push_error_handler(&ejbuf,print_error_perpackage,thisarg);
  707. process_archive(thisarg);
  708. onerr_abort++;
  709. if (ferror(stdout)) werr("stdout");
  710. if (ferror(stderr)) werr("stderr");
  711. onerr_abort--;
  712. set_error_display(0,0);
  713. error_unwind(ehflag_normaltidy);
  714. }
  715. switch (cipaction->arg) {
  716. case act_install:
  717. case act_configure:
  718. case act_remove:
  719. case act_purge:
  720. process_queue();
  721. case act_unpack:
  722. case act_avail:
  723. break;
  724. default:
  725. internerr("unknown action");
  726. }
  727. modstatdb_shutdown();
  728. }