archives.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /*
  2. * dpkg - main program for package management
  3. * archives.c - actions that process archive files, mainly unpack
  4. *
  5. * Copyright © 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright © 2000 Wichert Akkerman <wakkerma@debian.org>
  7. * Copyright © 2007-2015 Guillem Jover <guillem@debian.org>
  8. * Copyright © 2011 Linaro Limited
  9. * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
  10. *
  11. * This is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. #include <config.h>
  25. #include <compat.h>
  26. #include <sys/types.h>
  27. #include <sys/time.h>
  28. #include <sys/stat.h>
  29. #include <assert.h>
  30. #include <errno.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include <fcntl.h>
  34. #include <unistd.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <obstack.h>
  39. #define obstack_chunk_alloc m_malloc
  40. #define obstack_chunk_free free
  41. #include <dpkg/i18n.h>
  42. #include <dpkg/dpkg.h>
  43. #include <dpkg/dpkg-db.h>
  44. #include <dpkg/pkg.h>
  45. #include <dpkg/path.h>
  46. #include <dpkg/fdio.h>
  47. #include <dpkg/buffer.h>
  48. #include <dpkg/subproc.h>
  49. #include <dpkg/command.h>
  50. #include <dpkg/file.h>
  51. #include <dpkg/tarfn.h>
  52. #include <dpkg/options.h>
  53. #include <dpkg/triglib.h>
  54. #include "filesdb.h"
  55. #include "main.h"
  56. #include "archives.h"
  57. #include "filters.h"
  58. #include "infodb.h"
  59. static inline void
  60. fd_writeback_init(int fd)
  61. {
  62. /* Ignore the return code as it should be considered equivalent to an
  63. * asynchronous hint for the kernel, we are doing an fsync() later on
  64. * anyway. */
  65. #if defined(SYNC_FILE_RANGE_WRITE)
  66. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WRITE);
  67. #elif defined(HAVE_POSIX_FADVISE)
  68. posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
  69. #endif
  70. }
  71. static struct obstack tar_obs;
  72. static bool tarobs_init = false;
  73. /**
  74. * Ensure the obstack is properly initialized.
  75. */
  76. static void ensureobstackinit(void) {
  77. if (!tarobs_init) {
  78. obstack_init(&tar_obs);
  79. tarobs_init = true;
  80. }
  81. }
  82. /**
  83. * Destroy the obstack.
  84. */
  85. static void destroyobstack(void) {
  86. if (tarobs_init) {
  87. obstack_free(&tar_obs, NULL);
  88. tarobs_init = false;
  89. }
  90. }
  91. /**
  92. * Check if a file or directory will save a package from disappearance.
  93. *
  94. * A package can only be saved by a file or directory which is part
  95. * only of itself - it must be neither part of the new package being
  96. * installed nor part of any 3rd package (this is important so that
  97. * shared directories don't stop packages from disappearing).
  98. */
  99. bool
  100. filesavespackage(struct fileinlist *file,
  101. struct pkginfo *pkgtobesaved,
  102. struct pkginfo *pkgbeinginstalled)
  103. {
  104. struct filepackages_iterator *iter;
  105. struct pkgset *divpkgset;
  106. struct pkginfo *thirdpkg;
  107. debug(dbg_eachfiledetail, "filesavespackage file '%s' package %s",
  108. file->namenode->name, pkg_name(pkgtobesaved, pnaw_always));
  109. /* If the file is a contended one and it's overridden by either
  110. * the package we're considering disappearing or the package
  111. * we're installing then they're not actually the same file, so
  112. * we can't disappear the package - it is saved by this file. */
  113. if (file->namenode->divert && file->namenode->divert->useinstead) {
  114. divpkgset = file->namenode->divert->pkgset;
  115. if (divpkgset == pkgtobesaved->set || divpkgset == pkgbeinginstalled->set) {
  116. debug(dbg_eachfiledetail,"filesavespackage ... diverted -- save!");
  117. return true;
  118. }
  119. }
  120. /* Is the file in the package being installed? If so then it can't save. */
  121. if (file->namenode->flags & fnnf_new_inarchive) {
  122. debug(dbg_eachfiledetail,"filesavespackage ... in new archive -- no save");
  123. return false;
  124. }
  125. /* Look for a 3rd package which can take over the file (in case
  126. * it's a directory which is shared by many packages. */
  127. iter = filepackages_iter_new(file->namenode);
  128. while ((thirdpkg = filepackages_iter_next(iter))) {
  129. debug(dbg_eachfiledetail, "filesavespackage ... also in %s",
  130. pkg_name(thirdpkg, pnaw_always));
  131. /* Is this not the package being installed or the one being
  132. * checked for disappearance? */
  133. if (thirdpkg == pkgbeinginstalled || thirdpkg == pkgtobesaved)
  134. continue;
  135. /* A Multi-Arch: same package can share files and their presence in a
  136. * third package of the same set is not a sign that we can get rid of
  137. * it. */
  138. if (pkgtobesaved->installed.multiarch == PKG_MULTIARCH_SAME &&
  139. thirdpkg->set == pkgtobesaved->set)
  140. continue;
  141. /* If !fileslistvalid then we've already disappeared this one, so
  142. * we shouldn't try to make it take over this shared directory. */
  143. debug(dbg_eachfiledetail,"filesavespackage ... is 3rd package");
  144. if (!thirdpkg->clientdata->fileslistvalid) {
  145. debug(dbg_eachfiledetail, "process_archive ... already disappeared!");
  146. continue;
  147. }
  148. /* We've found a package that can take this file. */
  149. debug(dbg_eachfiledetail, "filesavespackage ... taken -- no save");
  150. filepackages_iter_free(iter);
  151. return false;
  152. }
  153. filepackages_iter_free(iter);
  154. debug(dbg_eachfiledetail, "filesavespackage ... not taken -- save !");
  155. return true;
  156. }
  157. static void
  158. md5hash_prev_conffile(struct pkginfo *pkg, char *oldhash, const char *oldname,
  159. struct filenamenode *namenode)
  160. {
  161. struct pkginfo *otherpkg;
  162. struct conffile *conff;
  163. debug(dbg_conffdetail, "tarobject looking for shared conffile %s",
  164. namenode->name);
  165. for (otherpkg = &pkg->set->pkg; otherpkg; otherpkg = otherpkg->arch_next) {
  166. if (otherpkg == pkg)
  167. continue;
  168. /* The hash in the Conffiles is only meaningful if the package
  169. * configuration has been at least tried. */
  170. if (otherpkg->status < PKG_STAT_UNPACKED)
  171. continue;
  172. /* If we are reinstalling, even if the other package is only unpacked,
  173. * we can always make use of the Conffiles hash value from an initial
  174. * installation, if that happened at all. */
  175. if (otherpkg->status == PKG_STAT_UNPACKED &&
  176. dpkg_version_compare(&otherpkg->installed.version,
  177. &otherpkg->configversion) != 0)
  178. continue;
  179. for (conff = otherpkg->installed.conffiles; conff; conff = conff->next) {
  180. if (conff->obsolete)
  181. continue;
  182. if (strcmp(conff->name, namenode->name) == 0)
  183. break;
  184. }
  185. if (conff) {
  186. strcpy(oldhash, conff->hash);
  187. debug(dbg_conffdetail,
  188. "tarobject found shared conffile, from pkg %s (%s); hash=%s",
  189. pkg_name(otherpkg, pnaw_always),
  190. pkg_status_name(otherpkg), oldhash);
  191. break;
  192. }
  193. }
  194. /* If no package was found with a valid Conffiles field, we make the
  195. * risky assumption that the hash of the current .dpkg-new file is
  196. * the one of the previously unpacked package. */
  197. if (otherpkg == NULL) {
  198. md5hash(pkg, oldhash, oldname);
  199. debug(dbg_conffdetail,
  200. "tarobject found shared conffile, from disk; hash=%s", oldhash);
  201. }
  202. }
  203. void cu_pathname(int argc, void **argv) {
  204. path_remove_tree((char*)(argv[0]));
  205. }
  206. int tarfileread(void *ud, char *buf, int len) {
  207. struct tarcontext *tc= (struct tarcontext*)ud;
  208. int r;
  209. r = fd_read(tc->backendpipe, buf, len);
  210. if (r < 0)
  211. ohshite(_("error reading from dpkg-deb pipe"));
  212. return r;
  213. }
  214. static void
  215. tarobject_skip_padding(struct tarcontext *tc, struct tar_entry *te)
  216. {
  217. struct dpkg_error err;
  218. size_t r;
  219. r = te->size % TARBLKSZ;
  220. if (r == 0)
  221. return;
  222. if (fd_skip(tc->backendpipe, TARBLKSZ - r, &err) < 0)
  223. ohshit(_("cannot skip padding for file '%.255s': %s"), te->name, err.str);
  224. }
  225. static void
  226. tarobject_skip_entry(struct tarcontext *tc, struct tar_entry *ti)
  227. {
  228. /* We need to advance the tar file to the next object, so read the
  229. * file data and set it to oblivion. */
  230. if (ti->type == TAR_FILETYPE_FILE) {
  231. struct dpkg_error err;
  232. char fnamebuf[256];
  233. if (fd_skip(tc->backendpipe, ti->size, &err) < 0)
  234. ohshit(_("cannot skip file '%.255s' (replaced or excluded?) from pipe: %s"),
  235. path_quote_filename(fnamebuf, ti->name, 256), err.str);
  236. tarobject_skip_padding(tc, ti);
  237. }
  238. }
  239. int fnameidlu;
  240. struct varbuf fnamevb;
  241. struct varbuf fnametmpvb;
  242. struct varbuf fnamenewvb;
  243. struct pkg_deconf_list *deconfigure = NULL;
  244. static time_t currenttime;
  245. static int
  246. does_replace(struct pkginfo *new_pkg, struct pkgbin *new_pkgbin,
  247. struct pkginfo *old_pkg, struct pkgbin *old_pkgbin)
  248. {
  249. struct dependency *dep;
  250. debug(dbg_depcon,"does_replace new=%s old=%s (%s)",
  251. pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
  252. pkgbin_name(old_pkg, old_pkgbin, pnaw_always),
  253. versiondescribe(&old_pkgbin->version, vdew_always));
  254. for (dep = new_pkgbin->depends; dep; dep = dep->next) {
  255. if (dep->type != dep_replaces || dep->list->ed != old_pkg->set)
  256. continue;
  257. debug(dbg_depcondetail,"does_replace ... found old, version %s",
  258. versiondescribe(&dep->list->version,vdew_always));
  259. if (!versionsatisfied(old_pkgbin, dep->list))
  260. continue;
  261. /* The test below can only trigger if dep_replaces start having
  262. * arch qualifiers different from “any”. */
  263. if (!archsatisfied(old_pkgbin, dep->list))
  264. continue;
  265. debug(dbg_depcon,"does_replace ... yes");
  266. return true;
  267. }
  268. debug(dbg_depcon,"does_replace ... no");
  269. return false;
  270. }
  271. static void
  272. tarobject_extract(struct tarcontext *tc, struct tar_entry *te,
  273. const char *path, struct file_stat *st,
  274. struct filenamenode *namenode)
  275. {
  276. static struct varbuf hardlinkfn;
  277. static int fd;
  278. struct dpkg_error err;
  279. struct filenamenode *linknode;
  280. char fnamebuf[256];
  281. char fnamenewbuf[256];
  282. char *newhash;
  283. switch (te->type) {
  284. case TAR_FILETYPE_FILE:
  285. /* We create the file with mode 0 to make sure nobody can do anything with
  286. * it until we apply the proper mode, which might be a statoverride. */
  287. fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0);
  288. if (fd < 0)
  289. ohshite(_("unable to create '%.255s' (while processing '%.255s')"),
  290. path, te->name);
  291. push_cleanup(cu_closefd, ehflag_bombout, NULL, 0, 1, &fd);
  292. debug(dbg_eachfiledetail, "tarobject file open size=%jd",
  293. (intmax_t)te->size);
  294. /* We try to tell the filesystem how much disk space we are going to
  295. * need to let it reduce fragmentation and possibly improve performance,
  296. * as we do know the size beforehand. */
  297. fd_allocate_size(fd, 0, te->size);
  298. newhash = nfmalloc(MD5HASHLEN + 1);
  299. if (fd_fd_copy_and_md5(tc->backendpipe, fd, newhash, te->size, &err) < 0)
  300. ohshit(_("cannot copy extracted data for '%.255s' to '%.255s': %s"),
  301. path_quote_filename(fnamebuf, te->name, 256),
  302. path_quote_filename(fnamenewbuf, fnamenewvb.buf, 256), err.str);
  303. namenode->newhash = newhash;
  304. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  305. tarobject_skip_padding(tc, te);
  306. fd_writeback_init(fd);
  307. if (namenode->statoverride)
  308. debug(dbg_eachfile, "tarobject ... stat override, uid=%d, gid=%d, mode=%04o",
  309. namenode->statoverride->uid,
  310. namenode->statoverride->gid,
  311. namenode->statoverride->mode);
  312. if (fchown(fd, st->uid, st->gid))
  313. ohshite(_("error setting ownership of '%.255s'"), te->name);
  314. if (fchmod(fd, st->mode & ~S_IFMT))
  315. ohshite(_("error setting permissions of '%.255s'"), te->name);
  316. /* Postpone the fsync, to try to avoid massive I/O degradation. */
  317. if (!fc_unsafe_io)
  318. namenode->flags |= fnnf_deferred_fsync;
  319. pop_cleanup(ehflag_normaltidy); /* fd = open(path) */
  320. if (close(fd))
  321. ohshite(_("error closing/writing '%.255s'"), te->name);
  322. break;
  323. case TAR_FILETYPE_FIFO:
  324. if (mkfifo(path, 0))
  325. ohshite(_("error creating pipe '%.255s'"), te->name);
  326. debug(dbg_eachfiledetail, "tarobject fifo");
  327. break;
  328. case TAR_FILETYPE_CHARDEV:
  329. if (mknod(path, S_IFCHR, te->dev))
  330. ohshite(_("error creating device '%.255s'"), te->name);
  331. debug(dbg_eachfiledetail, "tarobject chardev");
  332. break;
  333. case TAR_FILETYPE_BLOCKDEV:
  334. if (mknod(path, S_IFBLK, te->dev))
  335. ohshite(_("error creating device '%.255s'"), te->name);
  336. debug(dbg_eachfiledetail, "tarobject blockdev");
  337. break;
  338. case TAR_FILETYPE_HARDLINK:
  339. varbuf_reset(&hardlinkfn);
  340. varbuf_add_str(&hardlinkfn, instdir);
  341. linknode = findnamenode(te->linkname, 0);
  342. varbuf_add_str(&hardlinkfn,
  343. namenodetouse(linknode, tc->pkg, &tc->pkg->available)->name);
  344. if (linknode->flags & (fnnf_deferred_rename | fnnf_new_conff))
  345. varbuf_add_str(&hardlinkfn, DPKGNEWEXT);
  346. varbuf_end_str(&hardlinkfn);
  347. if (link(hardlinkfn.buf, path))
  348. ohshite(_("error creating hard link '%.255s'"), te->name);
  349. namenode->newhash = linknode->newhash;
  350. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  351. break;
  352. case TAR_FILETYPE_SYMLINK:
  353. /* We've already checked for an existing directory. */
  354. if (symlink(te->linkname, path))
  355. ohshite(_("error creating symbolic link '%.255s'"), te->name);
  356. debug(dbg_eachfiledetail, "tarobject symlink creating");
  357. break;
  358. case TAR_FILETYPE_DIR:
  359. /* We've already checked for an existing directory. */
  360. if (mkdir(path, 0))
  361. ohshite(_("error creating directory '%.255s'"), te->name);
  362. debug(dbg_eachfiledetail, "tarobject directory creating");
  363. break;
  364. default:
  365. internerr("unknown tar type '%d', but already checked", te->type);
  366. }
  367. }
  368. static void
  369. tarobject_hash(struct tarcontext *tc, struct tar_entry *te,
  370. struct filenamenode *namenode)
  371. {
  372. if (te->type == TAR_FILETYPE_FILE) {
  373. struct dpkg_error err;
  374. char fnamebuf[256];
  375. char *newhash;
  376. newhash = nfmalloc(MD5HASHLEN + 1);
  377. if (fd_md5(tc->backendpipe, newhash, te->size, &err) < 0)
  378. ohshit(_("cannot compute MD5 hash for tar file '%.255s': %s"),
  379. path_quote_filename(fnamebuf, te->name, 256), err.str);
  380. tarobject_skip_padding(tc, te);
  381. namenode->newhash = newhash;
  382. debug(dbg_eachfiledetail, "tarobject file hash=%s", namenode->newhash);
  383. } else if (te->type == TAR_FILETYPE_HARDLINK) {
  384. struct filenamenode *linknode;
  385. linknode = findnamenode(te->linkname, 0);
  386. namenode->newhash = linknode->newhash;
  387. debug(dbg_eachfiledetail, "tarobject hardlink hash=%s", namenode->newhash);
  388. }
  389. }
  390. static void
  391. tarobject_set_mtime(struct tar_entry *te, const char *path)
  392. {
  393. struct timeval tv[2];
  394. tv[0].tv_sec = currenttime;
  395. tv[0].tv_usec = 0;
  396. tv[1].tv_sec = te->mtime;
  397. tv[1].tv_usec = 0;
  398. if (te->type == TAR_FILETYPE_SYMLINK) {
  399. #ifdef HAVE_LUTIMES
  400. if (lutimes(path, tv) && errno != ENOSYS)
  401. ohshite(_("error setting timestamps of '%.255s'"), path);
  402. #endif
  403. } else {
  404. if (utimes(path, tv))
  405. ohshite(_("error setting timestamps of '%.255s'"), path);
  406. }
  407. }
  408. static void
  409. tarobject_set_perms(struct tar_entry *te, const char *path, struct file_stat *st)
  410. {
  411. if (te->type == TAR_FILETYPE_FILE)
  412. return; /* Already handled using the file descriptor. */
  413. if (te->type == TAR_FILETYPE_SYMLINK) {
  414. if (lchown(path, st->uid, st->gid))
  415. ohshite(_("error setting ownership of symlink '%.255s'"), path);
  416. } else {
  417. if (chown(path, st->uid, st->gid))
  418. ohshite(_("error setting ownership of '%.255s'"), path);
  419. if (chmod(path, st->mode & ~S_IFMT))
  420. ohshite(_("error setting permissions of '%.255s'"), path);
  421. }
  422. }
  423. static void
  424. tarobject_set_se_context(const char *matchpath, const char *path, mode_t mode)
  425. {
  426. dpkg_selabel_set_context(matchpath, path, mode);
  427. }
  428. static void
  429. tarobject_matches(struct tarcontext *tc,
  430. const char *fn_old, struct stat *stab, char *oldhash,
  431. const char *fn_new, struct tar_entry *te,
  432. struct filenamenode *namenode)
  433. {
  434. char *linkname;
  435. ssize_t linksize;
  436. debug(dbg_eachfiledetail, "tarobject matches on-disk object?");
  437. switch (te->type) {
  438. case TAR_FILETYPE_DIR:
  439. /* Nothing to check for a new directory. */
  440. return;
  441. case TAR_FILETYPE_SYMLINK:
  442. /* Symlinks to existing dirs have already been dealt with, only
  443. * reamin real symlinks where we can compare the target. */
  444. if (!S_ISLNK(stab->st_mode))
  445. break;
  446. linkname = m_malloc(stab->st_size + 1);
  447. linksize = readlink(fn_old, linkname, stab->st_size + 1);
  448. if (linksize < 0)
  449. ohshite(_("unable to read link '%.255s'"), fn_old);
  450. else if (linksize != stab->st_size)
  451. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  452. fn_old, stab->st_size, linksize);
  453. linkname[linksize] = '\0';
  454. if (strcmp(linkname, te->linkname) == 0) {
  455. free(linkname);
  456. return;
  457. } else {
  458. free(linkname);
  459. }
  460. break;
  461. case TAR_FILETYPE_CHARDEV:
  462. if (S_ISCHR(stab->st_mode) && stab->st_rdev == te->dev)
  463. return;
  464. break;
  465. case TAR_FILETYPE_BLOCKDEV:
  466. if (S_ISBLK(stab->st_mode) && stab->st_rdev == te->dev)
  467. return;
  468. break;
  469. case TAR_FILETYPE_FIFO:
  470. if (S_ISFIFO(stab->st_mode))
  471. return;
  472. break;
  473. case TAR_FILETYPE_HARDLINK:
  474. /* Fall through. */
  475. case TAR_FILETYPE_FILE:
  476. /* Only check metadata for non-conffiles. */
  477. if (!(namenode->flags & fnnf_new_conff) &&
  478. !(S_ISREG(stab->st_mode) && te->size == stab->st_size))
  479. break;
  480. if (strcmp(oldhash, namenode->newhash) == 0)
  481. return;
  482. break;
  483. default:
  484. internerr("unknown tar type '%d', but already checked", te->type);
  485. }
  486. forcibleerr(fc_overwrite,
  487. _("trying to overwrite shared '%.250s', which is different "
  488. "from other instances of package %.250s"),
  489. namenode->name, pkg_name(tc->pkg, pnaw_nonambig));
  490. }
  491. void setupfnamevbs(const char *filename) {
  492. varbuf_trunc(&fnamevb, fnameidlu);
  493. varbuf_add_str(&fnamevb, filename);
  494. varbuf_end_str(&fnamevb);
  495. varbuf_trunc(&fnametmpvb, fnameidlu);
  496. varbuf_add_str(&fnametmpvb, filename);
  497. varbuf_add_str(&fnametmpvb, DPKGTEMPEXT);
  498. varbuf_end_str(&fnametmpvb);
  499. varbuf_trunc(&fnamenewvb, fnameidlu);
  500. varbuf_add_str(&fnamenewvb, filename);
  501. varbuf_add_str(&fnamenewvb, DPKGNEWEXT);
  502. varbuf_end_str(&fnamenewvb);
  503. debug(dbg_eachfiledetail, "setupvnamevbs main='%s' tmp='%s' new='%s'",
  504. fnamevb.buf, fnametmpvb.buf, fnamenewvb.buf);
  505. }
  506. struct fileinlist *addfiletolist(struct tarcontext *tc,
  507. struct filenamenode *namenode) {
  508. struct fileinlist *nifd;
  509. nifd= obstack_alloc(&tar_obs, sizeof(struct fileinlist));
  510. nifd->namenode= namenode;
  511. nifd->next = NULL;
  512. *tc->newfilesp = nifd;
  513. tc->newfilesp = &nifd->next;
  514. return nifd;
  515. }
  516. static void
  517. remove_file_from_list(struct tarcontext *tc, struct tar_entry *ti,
  518. struct fileinlist **oldnifd,
  519. struct fileinlist *nifd)
  520. {
  521. obstack_free(&tar_obs, nifd);
  522. tc->newfilesp = oldnifd;
  523. *oldnifd = NULL;
  524. }
  525. static bool
  526. linktosameexistingdir(const struct tar_entry *ti, const char *fname,
  527. struct varbuf *symlinkfn)
  528. {
  529. struct stat oldstab, newstab;
  530. int statr;
  531. const char *lastslash;
  532. statr= stat(fname, &oldstab);
  533. if (statr) {
  534. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  535. ohshite(_("failed to stat (dereference) existing symlink '%.250s'"),
  536. fname);
  537. return false;
  538. }
  539. if (!S_ISDIR(oldstab.st_mode))
  540. return false;
  541. /* But is it to the same dir? */
  542. varbuf_reset(symlinkfn);
  543. if (ti->linkname[0] == '/') {
  544. varbuf_add_str(symlinkfn, instdir);
  545. } else {
  546. lastslash= strrchr(fname, '/');
  547. assert(lastslash);
  548. varbuf_add_buf(symlinkfn, fname, (lastslash - fname) + 1);
  549. }
  550. varbuf_add_str(symlinkfn, ti->linkname);
  551. varbuf_end_str(symlinkfn);
  552. statr= stat(symlinkfn->buf, &newstab);
  553. if (statr) {
  554. if (!(errno == ENOENT || errno == ELOOP || errno == ENOTDIR))
  555. ohshite(_("failed to stat (dereference) proposed new symlink target"
  556. " '%.250s' for symlink '%.250s'"), symlinkfn->buf, fname);
  557. return false;
  558. }
  559. if (!S_ISDIR(newstab.st_mode))
  560. return false;
  561. if (newstab.st_dev != oldstab.st_dev ||
  562. newstab.st_ino != oldstab.st_ino)
  563. return false;
  564. return true;
  565. }
  566. int
  567. tarobject(void *ctx, struct tar_entry *ti)
  568. {
  569. static struct varbuf conffderefn, symlinkfn;
  570. const char *usename;
  571. struct filenamenode *usenode;
  572. struct conffile *conff;
  573. struct tarcontext *tc = ctx;
  574. bool existingdir, keepexisting;
  575. bool refcounting;
  576. char oldhash[MD5HASHLEN + 1];
  577. int statr;
  578. ssize_t r;
  579. struct stat stab, stabtmp;
  580. struct file_stat nodestat;
  581. struct fileinlist *nifd, **oldnifd;
  582. struct pkgset *divpkgset;
  583. struct pkginfo *otherpkg;
  584. ensureobstackinit();
  585. tar_entry_update_from_system(ti);
  586. /* Perform some sanity checks on the tar entry. */
  587. if (strchr(ti->name, '\n'))
  588. ohshit(_("newline not allowed in archive object name '%.255s'"), ti->name);
  589. /* Append to list of files.
  590. * The trailing ‘/’ put on the end of names in tarfiles has already
  591. * been stripped by tar_extractor(). */
  592. oldnifd= tc->newfilesp;
  593. nifd= addfiletolist(tc, findnamenode(ti->name, 0));
  594. nifd->namenode->flags |= fnnf_new_inarchive;
  595. debug(dbg_eachfile,
  596. "tarobject ti->name='%s' mode=%lo owner=%u:%u type=%d(%c)"
  597. " ti->linkname='%s' namenode='%s' flags=%o instead='%s'",
  598. ti->name, (long)ti->stat.mode,
  599. (unsigned)ti->stat.uid, (unsigned)ti->stat.gid,
  600. ti->type,
  601. ti->type >= '0' && ti->type <= '6' ? "-hlcbdp"[ti->type - '0'] : '?',
  602. ti->linkname,
  603. nifd->namenode->name, nifd->namenode->flags,
  604. nifd->namenode->divert && nifd->namenode->divert->useinstead
  605. ? nifd->namenode->divert->useinstead->name : "<none>");
  606. if (nifd->namenode->divert && nifd->namenode->divert->camefrom) {
  607. divpkgset = nifd->namenode->divert->pkgset;
  608. if (divpkgset) {
  609. forcibleerr(fc_overwritediverted,
  610. _("trying to overwrite '%.250s', which is the "
  611. "diverted version of '%.250s' (package: %.100s)"),
  612. nifd->namenode->name, nifd->namenode->divert->camefrom->name,
  613. divpkgset->name);
  614. } else {
  615. forcibleerr(fc_overwritediverted,
  616. _("trying to overwrite '%.250s', which is the "
  617. "diverted version of '%.250s'"),
  618. nifd->namenode->name, nifd->namenode->divert->camefrom->name);
  619. }
  620. }
  621. if (nifd->namenode->statoverride) {
  622. nodestat = *nifd->namenode->statoverride;
  623. nodestat.mode |= ti->stat.mode & S_IFMT;
  624. } else {
  625. nodestat = ti->stat;
  626. }
  627. usenode = namenodetouse(nifd->namenode, tc->pkg, &tc->pkg->available);
  628. usename = usenode->name;
  629. trig_file_activate(usenode, tc->pkg);
  630. if (nifd->namenode->flags & fnnf_new_conff) {
  631. /* If it's a conffile we have to extract it next to the installed
  632. * version (i.e. we do the usual link-following). */
  633. if (conffderef(tc->pkg, &conffderefn, usename))
  634. usename= conffderefn.buf;
  635. debug(dbg_conff, "tarobject fnnf_new_conff deref='%s'", usename);
  636. }
  637. setupfnamevbs(usename);
  638. statr= lstat(fnamevb.buf,&stab);
  639. if (statr) {
  640. /* The lstat failed. */
  641. if (errno != ENOENT && errno != ENOTDIR)
  642. ohshite(_("unable to stat '%.255s' (which I was about to install)"),
  643. ti->name);
  644. /* OK, so it doesn't exist.
  645. * However, it's possible that we were in the middle of some other
  646. * backup/restore operation and were rudely interrupted.
  647. * So, we see if we have .dpkg-tmp, and if so we restore it. */
  648. if (rename(fnametmpvb.buf,fnamevb.buf)) {
  649. if (errno != ENOENT && errno != ENOTDIR)
  650. ohshite(_("unable to clean up mess surrounding '%.255s' before "
  651. "installing another version"), ti->name);
  652. debug(dbg_eachfiledetail,"tarobject nonexistent");
  653. } else {
  654. debug(dbg_eachfiledetail,"tarobject restored tmp to main");
  655. statr= lstat(fnamevb.buf,&stab);
  656. if (statr)
  657. ohshite(_("unable to stat restored '%.255s' before installing"
  658. " another version"), ti->name);
  659. }
  660. } else {
  661. debug(dbg_eachfiledetail,"tarobject already exists");
  662. }
  663. /* Check to see if it's a directory or link to one and we don't need to
  664. * do anything. This has to be done now so that we don't die due to
  665. * a file overwriting conflict. */
  666. existingdir = false;
  667. switch (ti->type) {
  668. case TAR_FILETYPE_SYMLINK:
  669. /* If it's already an existing directory, do nothing. */
  670. if (!statr && S_ISDIR(stab.st_mode)) {
  671. debug(dbg_eachfiledetail, "tarobject symlink exists as directory");
  672. existingdir = true;
  673. } else if (!statr && S_ISLNK(stab.st_mode)) {
  674. if (linktosameexistingdir(ti, fnamevb.buf, &symlinkfn))
  675. existingdir = true;
  676. }
  677. break;
  678. case TAR_FILETYPE_DIR:
  679. /* If it's already an existing directory, do nothing. */
  680. if (!stat(fnamevb.buf,&stabtmp) && S_ISDIR(stabtmp.st_mode)) {
  681. debug(dbg_eachfiledetail, "tarobject directory exists");
  682. existingdir = true;
  683. }
  684. break;
  685. case TAR_FILETYPE_FILE:
  686. case TAR_FILETYPE_CHARDEV:
  687. case TAR_FILETYPE_BLOCKDEV:
  688. case TAR_FILETYPE_FIFO:
  689. case TAR_FILETYPE_HARDLINK:
  690. break;
  691. default:
  692. ohshit(_("archive contained object '%.255s' of unknown type 0x%x"),
  693. ti->name, ti->type);
  694. }
  695. keepexisting = false;
  696. refcounting = false;
  697. if (!existingdir) {
  698. struct filepackages_iterator *iter;
  699. iter = filepackages_iter_new(nifd->namenode);
  700. while ((otherpkg = filepackages_iter_next(iter))) {
  701. if (otherpkg == tc->pkg)
  702. continue;
  703. debug(dbg_eachfile, "tarobject ... found in %s",
  704. pkg_name(otherpkg, pnaw_always));
  705. /* A pkgset can share files between its instances. Overwriting
  706. * is allowed when they are not getting in sync, otherwise the
  707. * file content must match the installed file. */
  708. if (otherpkg->set == tc->pkg->set &&
  709. otherpkg->installed.multiarch == PKG_MULTIARCH_SAME &&
  710. tc->pkg->available.multiarch == PKG_MULTIARCH_SAME) {
  711. if (statr == 0 && tc->pkgset_getting_in_sync)
  712. refcounting = true;
  713. debug(dbg_eachfiledetail, "tarobject ... shared with %s %s (syncing=%d)",
  714. pkg_name(otherpkg, pnaw_always),
  715. versiondescribe(&otherpkg->installed.version, vdew_nonambig),
  716. tc->pkgset_getting_in_sync);
  717. continue;
  718. }
  719. if (nifd->namenode->divert && nifd->namenode->divert->useinstead) {
  720. /* Right, so we may be diverting this file. This makes the conflict
  721. * OK iff one of us is the diverting package (we don't need to
  722. * check for both being the diverting package, obviously). */
  723. divpkgset = nifd->namenode->divert->pkgset;
  724. debug(dbg_eachfile, "tarobject ... diverted, divpkgset=%s",
  725. divpkgset ? divpkgset->name : "<none>");
  726. if (otherpkg->set == divpkgset || tc->pkg->set == divpkgset)
  727. continue;
  728. }
  729. /* If the new object is a directory and the previous object does
  730. * not exist assume it's also a directory and skip further checks.
  731. * XXX: Ideally with more information about the installed files we
  732. * could perform more clever checks. */
  733. if (statr != 0 && ti->type == TAR_FILETYPE_DIR) {
  734. debug(dbg_eachfile, "tarobject ... assuming shared directory");
  735. continue;
  736. }
  737. /* Nope? Hmm, file conflict, perhaps. Check Replaces. */
  738. switch (otherpkg->clientdata->replacingfilesandsaid) {
  739. case 2:
  740. keepexisting = true;
  741. case 1:
  742. continue;
  743. }
  744. /* Is the package with the conflicting file in the “config files only”
  745. * state? If so it must be a config file and we can silenty take it
  746. * over. */
  747. if (otherpkg->status == PKG_STAT_CONFIGFILES)
  748. continue;
  749. /* Perhaps we're removing a conflicting package? */
  750. if (otherpkg->clientdata->istobe == PKG_ISTOBE_REMOVE)
  751. continue;
  752. /* Is the file an obsolete conffile in the other package
  753. * and a conffile in the new package? */
  754. if ((nifd->namenode->flags & fnnf_new_conff) &&
  755. !statr && S_ISREG(stab.st_mode)) {
  756. for (conff = otherpkg->installed.conffiles;
  757. conff;
  758. conff = conff->next) {
  759. if (!conff->obsolete)
  760. continue;
  761. if (stat(conff->name, &stabtmp)) {
  762. if (errno == ENOENT || errno == ENOTDIR || errno == ELOOP)
  763. continue;
  764. else
  765. ohshite(_("cannot stat file '%s'"), conff->name);
  766. }
  767. if (stabtmp.st_dev == stab.st_dev &&
  768. stabtmp.st_ino == stab.st_ino)
  769. break;
  770. }
  771. if (conff) {
  772. debug(dbg_eachfiledetail, "tarobject other's obsolete conffile");
  773. /* process_archive() will have copied its hash already. */
  774. continue;
  775. }
  776. }
  777. if (does_replace(tc->pkg, &tc->pkg->available,
  778. otherpkg, &otherpkg->installed)) {
  779. printf(_("Replacing files in old package %s (%s) ...\n"),
  780. pkg_name(otherpkg, pnaw_nonambig),
  781. versiondescribe(&otherpkg->installed.version, vdew_nonambig));
  782. otherpkg->clientdata->replacingfilesandsaid = 1;
  783. } else if (does_replace(otherpkg, &otherpkg->installed,
  784. tc->pkg, &tc->pkg->available)) {
  785. printf(_("Replaced by files in installed package %s (%s) ...\n"),
  786. pkg_name(otherpkg, pnaw_nonambig),
  787. versiondescribe(&otherpkg->installed.version, vdew_nonambig));
  788. otherpkg->clientdata->replacingfilesandsaid = 2;
  789. nifd->namenode->flags &= ~fnnf_new_inarchive;
  790. keepexisting = true;
  791. } else {
  792. /* At this point we are replacing something without a Replaces. */
  793. if (!statr && S_ISDIR(stab.st_mode)) {
  794. forcibleerr(fc_overwritedir,
  795. _("trying to overwrite directory '%.250s' "
  796. "in package %.250s %.250s with nondirectory"),
  797. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  798. versiondescribe(&otherpkg->installed.version,
  799. vdew_nonambig));
  800. } else {
  801. forcibleerr(fc_overwrite,
  802. _("trying to overwrite '%.250s', "
  803. "which is also in package %.250s %.250s"),
  804. nifd->namenode->name, pkg_name(otherpkg, pnaw_nonambig),
  805. versiondescribe(&otherpkg->installed.version,
  806. vdew_nonambig));
  807. }
  808. }
  809. }
  810. filepackages_iter_free(iter);
  811. }
  812. if (keepexisting) {
  813. if (nifd->namenode->flags & fnnf_new_conff)
  814. nifd->namenode->flags |= fnnf_obs_conff;
  815. remove_file_from_list(tc, ti, oldnifd, nifd);
  816. tarobject_skip_entry(tc, ti);
  817. return 0;
  818. }
  819. if (filter_should_skip(ti)) {
  820. nifd->namenode->flags &= ~fnnf_new_inarchive;
  821. nifd->namenode->flags |= fnnf_filtered;
  822. tarobject_skip_entry(tc, ti);
  823. return 0;
  824. }
  825. if (existingdir)
  826. return 0;
  827. /* Compute the hash of the previous object, before we might replace it
  828. * with the new version on forced overwrites. */
  829. if (refcounting) {
  830. debug(dbg_eachfiledetail, "tarobject hashing on-disk file '%s', refcounting",
  831. fnamevb.buf);
  832. if (nifd->namenode->flags & fnnf_new_conff) {
  833. md5hash_prev_conffile(tc->pkg, oldhash, fnamenewvb.buf, nifd->namenode);
  834. } else if (S_ISREG(stab.st_mode)) {
  835. md5hash(tc->pkg, oldhash, fnamevb.buf);
  836. } else {
  837. strcpy(oldhash, EMPTYHASHFLAG);
  838. }
  839. }
  840. if (refcounting && !fc_overwrite) {
  841. /* If we are not forced to overwrite the path and are refcounting,
  842. * just compute the hash w/o extracting the object. */
  843. tarobject_hash(tc, ti, nifd->namenode);
  844. } else {
  845. /* Now, at this stage we want to make sure neither of .dpkg-new and
  846. * .dpkg-tmp are hanging around. */
  847. path_remove_tree(fnamenewvb.buf);
  848. path_remove_tree(fnametmpvb.buf);
  849. /* Now we start to do things that we need to be able to undo
  850. * if something goes wrong. Watch out for the CLEANUP comments to
  851. * keep an eye on what's installed on the disk at each point. */
  852. push_cleanup(cu_installnew, ~ehflag_normaltidy, NULL, 0, 1, nifd->namenode);
  853. /*
  854. * CLEANUP: Now we either have the old file on the disk, or not, in
  855. * its original filename.
  856. */
  857. /* Extract whatever it is as .dpkg-new ... */
  858. tarobject_extract(tc, ti, fnamenewvb.buf, &nodestat, nifd->namenode);
  859. }
  860. /* For shared files, check now if the object matches. */
  861. if (refcounting)
  862. tarobject_matches(tc, fnamevb.buf, &stab, oldhash,
  863. fnamenewvb.buf, ti, nifd->namenode);
  864. /* If we didn't extract anything, there's nothing else to do. */
  865. if (refcounting && !fc_overwrite)
  866. return 0;
  867. tarobject_set_perms(ti, fnamenewvb.buf, &nodestat);
  868. tarobject_set_mtime(ti, fnamenewvb.buf);
  869. tarobject_set_se_context(fnamevb.buf, fnamenewvb.buf, nodestat.mode);
  870. /*
  871. * CLEANUP: Now we have extracted the new object in .dpkg-new (or,
  872. * if the file already exists as a directory and we were trying to
  873. * extract a directory or symlink, we returned earlier, so we don't
  874. * need to worry about that here).
  875. *
  876. * The old file is still in the original filename,
  877. */
  878. /* First, check to see if it's a conffile. If so we don't install
  879. * it now - we leave it in .dpkg-new for --configure to take care of. */
  880. if (nifd->namenode->flags & fnnf_new_conff) {
  881. debug(dbg_conffdetail,"tarobject conffile extracted");
  882. nifd->namenode->flags |= fnnf_elide_other_lists;
  883. return 0;
  884. }
  885. /* Now we move the old file out of the way, the backup file will
  886. * be deleted later. */
  887. if (statr) {
  888. /* Don't try to back it up if it didn't exist. */
  889. debug(dbg_eachfiledetail,"tarobject new - no backup");
  890. } else {
  891. if (ti->type == TAR_FILETYPE_DIR || S_ISDIR(stab.st_mode)) {
  892. /* One of the two is a directory - can't do atomic install. */
  893. debug(dbg_eachfiledetail,"tarobject directory, nonatomic");
  894. nifd->namenode->flags |= fnnf_no_atomic_overwrite;
  895. if (rename(fnamevb.buf,fnametmpvb.buf))
  896. ohshite(_("unable to move aside '%.255s' to install new version"),
  897. ti->name);
  898. } else if (S_ISLNK(stab.st_mode)) {
  899. /* We can't make a symlink with two hardlinks, so we'll have to
  900. * copy it. (Pretend that making a copy of a symlink is the same
  901. * as linking to it.) */
  902. varbuf_reset(&symlinkfn);
  903. varbuf_grow(&symlinkfn, stab.st_size + 1);
  904. r = readlink(fnamevb.buf, symlinkfn.buf, symlinkfn.size);
  905. if (r < 0)
  906. ohshite(_("unable to read link '%.255s'"), ti->name);
  907. else if (r != stab.st_size)
  908. ohshit(_("symbolic link '%.250s' size has changed from %jd to %zd"),
  909. fnamevb.buf, stab.st_size, r);
  910. varbuf_trunc(&symlinkfn, r);
  911. varbuf_end_str(&symlinkfn);
  912. if (symlink(symlinkfn.buf,fnametmpvb.buf))
  913. ohshite(_("unable to make backup symlink for '%.255s'"), ti->name);
  914. if (lchown(fnametmpvb.buf,stab.st_uid,stab.st_gid))
  915. ohshite(_("unable to chown backup symlink for '%.255s'"), ti->name);
  916. tarobject_set_se_context(fnamevb.buf, fnametmpvb.buf, stab.st_mode);
  917. } else {
  918. debug(dbg_eachfiledetail, "tarobject nondirectory, 'link' backup");
  919. if (link(fnamevb.buf,fnametmpvb.buf))
  920. ohshite(_("unable to make backup link of '%.255s' before installing new version"),
  921. ti->name);
  922. }
  923. }
  924. /*
  925. * CLEANUP: Now the old file is in .dpkg-tmp, and the new file is still
  926. * in .dpkg-new.
  927. */
  928. if (ti->type == TAR_FILETYPE_FILE || ti->type == TAR_FILETYPE_HARDLINK ||
  929. ti->type == TAR_FILETYPE_SYMLINK) {
  930. nifd->namenode->flags |= fnnf_deferred_rename;
  931. debug(dbg_eachfiledetail, "tarobject done and installation deferred");
  932. } else {
  933. if (rename(fnamenewvb.buf, fnamevb.buf))
  934. ohshite(_("unable to install new version of '%.255s'"), ti->name);
  935. /*
  936. * CLEANUP: Now the new file is in the destination file, and the
  937. * old file is in .dpkg-tmp to be cleaned up later. We now need
  938. * to take a different attitude to cleanup, because we need to
  939. * remove the new file.
  940. */
  941. nifd->namenode->flags |= fnnf_placed_on_disk;
  942. nifd->namenode->flags |= fnnf_elide_other_lists;
  943. debug(dbg_eachfiledetail, "tarobject done and installed");
  944. }
  945. return 0;
  946. }
  947. #if defined(SYNC_FILE_RANGE_WAIT_BEFORE)
  948. static void
  949. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  950. {
  951. struct fileinlist *cfile;
  952. for (cfile = files; cfile; cfile = cfile->next) {
  953. struct filenamenode *usenode;
  954. int fd;
  955. if (!(cfile->namenode->flags & fnnf_deferred_fsync))
  956. continue;
  957. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  958. setupfnamevbs(usenode->name);
  959. fd = open(fnamenewvb.buf, O_WRONLY);
  960. if (fd < 0)
  961. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  962. /* Ignore the return code as it should be considered equivalent to an
  963. * asynchronous hint for the kernel, we are doing an fsync() later on
  964. * anyway. */
  965. sync_file_range(fd, 0, 0, SYNC_FILE_RANGE_WAIT_BEFORE);
  966. if (close(fd))
  967. ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
  968. }
  969. }
  970. #else
  971. static void
  972. tar_writeback_barrier(struct fileinlist *files, struct pkginfo *pkg)
  973. {
  974. }
  975. #endif
  976. void
  977. tar_deferred_extract(struct fileinlist *files, struct pkginfo *pkg)
  978. {
  979. struct fileinlist *cfile;
  980. struct filenamenode *usenode;
  981. tar_writeback_barrier(files, pkg);
  982. for (cfile = files; cfile; cfile = cfile->next) {
  983. debug(dbg_eachfile, "deferred extract of '%.255s'", cfile->namenode->name);
  984. if (!(cfile->namenode->flags & fnnf_deferred_rename))
  985. continue;
  986. usenode = namenodetouse(cfile->namenode, pkg, &pkg->available);
  987. setupfnamevbs(usenode->name);
  988. if (cfile->namenode->flags & fnnf_deferred_fsync) {
  989. int fd;
  990. debug(dbg_eachfiledetail, "deferred extract needs fsync");
  991. fd = open(fnamenewvb.buf, O_WRONLY);
  992. if (fd < 0)
  993. ohshite(_("unable to open '%.255s'"), fnamenewvb.buf);
  994. if (fsync(fd))
  995. ohshite(_("unable to sync file '%.255s'"), fnamenewvb.buf);
  996. if (close(fd))
  997. ohshite(_("error closing/writing '%.255s'"), fnamenewvb.buf);
  998. cfile->namenode->flags &= ~fnnf_deferred_fsync;
  999. }
  1000. debug(dbg_eachfiledetail, "deferred extract needs rename");
  1001. if (rename(fnamenewvb.buf, fnamevb.buf))
  1002. ohshite(_("unable to install new version of '%.255s'"),
  1003. cfile->namenode->name);
  1004. cfile->namenode->flags &= ~fnnf_deferred_rename;
  1005. /*
  1006. * CLEANUP: Now the new file is in the destination file, and the
  1007. * old file is in .dpkg-tmp to be cleaned up later. We now need
  1008. * to take a different attitude to cleanup, because we need to
  1009. * remove the new file.
  1010. */
  1011. cfile->namenode->flags |= fnnf_placed_on_disk;
  1012. cfile->namenode->flags |= fnnf_elide_other_lists;
  1013. debug(dbg_eachfiledetail, "deferred extract done and installed");
  1014. }
  1015. }
  1016. void
  1017. enqueue_deconfigure(struct pkginfo *pkg, struct pkginfo *pkg_removal)
  1018. {
  1019. struct pkg_deconf_list *newdeconf;
  1020. ensure_package_clientdata(pkg);
  1021. pkg->clientdata->istobe = PKG_ISTOBE_DECONFIGURE;
  1022. newdeconf = m_malloc(sizeof(struct pkg_deconf_list));
  1023. newdeconf->next = deconfigure;
  1024. newdeconf->pkg = pkg;
  1025. newdeconf->pkg_removal = pkg_removal;
  1026. deconfigure = newdeconf;
  1027. }
  1028. void
  1029. clear_deconfigure_queue(void)
  1030. {
  1031. struct pkg_deconf_list *deconf, *deconf_next;
  1032. for (deconf = deconfigure; deconf; deconf = deconf_next) {
  1033. deconf_next = deconf->next;
  1034. free(deconf);
  1035. }
  1036. deconfigure = NULL;
  1037. }
  1038. /**
  1039. * Try if we can deconfigure the package and queue it if so.
  1040. *
  1041. * Also checks whether the pdep is forced, first, according to force_p.
  1042. * force_p may be NULL in which case nothing is considered forced.
  1043. *
  1044. * Action is a string describing the action which causes the
  1045. * deconfiguration:
  1046. *
  1047. * "removal of <package>" (due to Conflicts+Depends; removal != NULL)
  1048. * "installation of <package>" (due to Breaks; removal == NULL)
  1049. *
  1050. * @retval 0 Not possible (why is printed).
  1051. * @retval 1 Deconfiguration queued ok (no message printed).
  1052. * @retval 2 Forced (no deconfiguration needed, why is printed).
  1053. */
  1054. static int
  1055. try_deconfigure_can(bool (*force_p)(struct deppossi *), struct pkginfo *pkg,
  1056. struct deppossi *pdep, const char *action,
  1057. struct pkginfo *removal, const char *why)
  1058. {
  1059. if (force_p && force_p(pdep)) {
  1060. warning(_("ignoring dependency problem with %s:\n%s"), action, why);
  1061. return 2;
  1062. } else if (f_autodeconf) {
  1063. if (pkg->installed.essential) {
  1064. if (fc_removeessential) {
  1065. warning(_("considering deconfiguration of essential\n"
  1066. " package %s, to enable %s"),
  1067. pkg_name(pkg, pnaw_nonambig), action);
  1068. } else {
  1069. notice(_("no, %s is essential, will not deconfigure\n"
  1070. " it in order to enable %s"),
  1071. pkg_name(pkg, pnaw_nonambig), action);
  1072. return 0;
  1073. }
  1074. }
  1075. enqueue_deconfigure(pkg, removal);
  1076. return 1;
  1077. } else {
  1078. notice(_("no, cannot proceed with %s (--auto-deconfigure will help):\n%s"),
  1079. action, why);
  1080. return 0;
  1081. }
  1082. }
  1083. static int try_remove_can(struct deppossi *pdep,
  1084. struct pkginfo *fixbyrm,
  1085. const char *why) {
  1086. char action[512];
  1087. sprintf(action, _("removal of %.250s"), pkg_name(fixbyrm, pnaw_nonambig));
  1088. return try_deconfigure_can(force_depends, pdep->up->up, pdep,
  1089. action, fixbyrm, why);
  1090. }
  1091. void check_breaks(struct dependency *dep, struct pkginfo *pkg,
  1092. const char *pfilename) {
  1093. struct pkginfo *fixbydeconf;
  1094. struct varbuf why = VARBUF_INIT;
  1095. int ok;
  1096. fixbydeconf = NULL;
  1097. if (depisok(dep, &why, &fixbydeconf, NULL, false)) {
  1098. varbuf_destroy(&why);
  1099. return;
  1100. }
  1101. varbuf_end_str(&why);
  1102. if (fixbydeconf && f_autodeconf) {
  1103. char action[512];
  1104. ensure_package_clientdata(fixbydeconf);
  1105. assert(fixbydeconf->clientdata->istobe == PKG_ISTOBE_NORMAL);
  1106. sprintf(action, _("installation of %.250s"),
  1107. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1108. notice(_("considering deconfiguration of %s, which would be broken by %s ..."),
  1109. pkg_name(fixbydeconf, pnaw_nonambig), action);
  1110. ok= try_deconfigure_can(force_breaks, fixbydeconf, dep->list,
  1111. action, NULL, why.buf);
  1112. if (ok == 1) {
  1113. notice(_("yes, will deconfigure %s (broken by %s)"),
  1114. pkg_name(fixbydeconf, pnaw_nonambig),
  1115. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1116. }
  1117. } else {
  1118. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1119. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), why.buf);
  1120. ok= 0;
  1121. }
  1122. varbuf_destroy(&why);
  1123. if (ok > 0) return;
  1124. if (force_breaks(dep->list)) {
  1125. warning(_("ignoring breakage, may proceed anyway!"));
  1126. return;
  1127. }
  1128. if (fixbydeconf && !f_autodeconf) {
  1129. ohshit(_("installing %.250s would break %.250s, and\n"
  1130. " deconfiguration is not permitted (--auto-deconfigure might help)"),
  1131. pkgbin_name(pkg, &pkg->available, pnaw_nonambig),
  1132. pkg_name(fixbydeconf, pnaw_nonambig));
  1133. } else {
  1134. ohshit(_("installing %.250s would break existing software"),
  1135. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1136. }
  1137. }
  1138. void check_conflict(struct dependency *dep, struct pkginfo *pkg,
  1139. const char *pfilename) {
  1140. struct pkginfo *fixbyrm;
  1141. struct deppossi *pdep, flagdeppossi;
  1142. struct varbuf conflictwhy = VARBUF_INIT, removalwhy = VARBUF_INIT;
  1143. struct dependency *providecheck;
  1144. fixbyrm = NULL;
  1145. if (depisok(dep, &conflictwhy, &fixbyrm, NULL, false)) {
  1146. varbuf_destroy(&conflictwhy);
  1147. varbuf_destroy(&removalwhy);
  1148. return;
  1149. }
  1150. if (fixbyrm) {
  1151. ensure_package_clientdata(fixbyrm);
  1152. if (fixbyrm->clientdata->istobe == PKG_ISTOBE_INSTALLNEW) {
  1153. fixbyrm= dep->up;
  1154. ensure_package_clientdata(fixbyrm);
  1155. }
  1156. if (((pkg->available.essential && fixbyrm->installed.essential) ||
  1157. (((fixbyrm->want != PKG_WANT_INSTALL &&
  1158. fixbyrm->want != PKG_WANT_HOLD) ||
  1159. does_replace(pkg, &pkg->available, fixbyrm, &fixbyrm->installed)) &&
  1160. (!fixbyrm->installed.essential || fc_removeessential)))) {
  1161. assert(fixbyrm->clientdata->istobe == PKG_ISTOBE_NORMAL ||
  1162. fixbyrm->clientdata->istobe == PKG_ISTOBE_DECONFIGURE);
  1163. fixbyrm->clientdata->istobe = PKG_ISTOBE_REMOVE;
  1164. notice(_("considering removing %s in favour of %s ..."),
  1165. pkg_name(fixbyrm, pnaw_nonambig),
  1166. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1167. if (!(fixbyrm->status == PKG_STAT_INSTALLED ||
  1168. fixbyrm->status == PKG_STAT_TRIGGERSPENDING ||
  1169. fixbyrm->status == PKG_STAT_TRIGGERSAWAITED)) {
  1170. notice(_("%s is not properly installed; ignoring any dependencies on it"),
  1171. pkg_name(fixbyrm, pnaw_nonambig));
  1172. pdep = NULL;
  1173. } else {
  1174. for (pdep = fixbyrm->set->depended.installed;
  1175. pdep;
  1176. pdep = pdep->rev_next) {
  1177. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1178. continue;
  1179. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1180. continue;
  1181. varbuf_end_str(&removalwhy);
  1182. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1183. break;
  1184. }
  1185. if (!pdep) {
  1186. /* If we haven't found a reason not to yet, let's look some more. */
  1187. for (providecheck= fixbyrm->installed.depends;
  1188. providecheck;
  1189. providecheck= providecheck->next) {
  1190. if (providecheck->type != dep_provides) continue;
  1191. for (pdep = providecheck->list->ed->depended.installed;
  1192. pdep;
  1193. pdep = pdep->rev_next) {
  1194. if (pdep->up->type != dep_depends && pdep->up->type != dep_predepends)
  1195. continue;
  1196. if (depisok(pdep->up, &removalwhy, NULL, NULL, false))
  1197. continue;
  1198. varbuf_end_str(&removalwhy);
  1199. notice(_("may have trouble removing %s, as it provides %s ..."),
  1200. pkg_name(fixbyrm, pnaw_nonambig),
  1201. providecheck->list->ed->name);
  1202. if (!try_remove_can(pdep,fixbyrm,removalwhy.buf))
  1203. goto break_from_both_loops_at_once;
  1204. }
  1205. }
  1206. break_from_both_loops_at_once:;
  1207. }
  1208. }
  1209. if (!pdep && skip_due_to_hold(fixbyrm)) {
  1210. pdep= &flagdeppossi;
  1211. }
  1212. if (!pdep && (fixbyrm->eflag & PKG_EFLAG_REINSTREQ)) {
  1213. if (fc_removereinstreq) {
  1214. notice(_("package %s requires reinstallation, but will "
  1215. "remove anyway as you requested"),
  1216. pkg_name(fixbyrm, pnaw_nonambig));
  1217. } else {
  1218. notice(_("package %s requires reinstallation, will not remove"),
  1219. pkg_name(fixbyrm, pnaw_nonambig));
  1220. pdep= &flagdeppossi;
  1221. }
  1222. }
  1223. if (!pdep) {
  1224. /* This conflict is OK - we'll remove the conflictor. */
  1225. enqueue_conflictor(fixbyrm);
  1226. varbuf_destroy(&conflictwhy); varbuf_destroy(&removalwhy);
  1227. notice(_("yes, will remove %s in favour of %s"),
  1228. pkg_name(fixbyrm, pnaw_nonambig),
  1229. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1230. return;
  1231. }
  1232. /* Put it back. */
  1233. fixbyrm->clientdata->istobe = PKG_ISTOBE_NORMAL;
  1234. }
  1235. }
  1236. varbuf_end_str(&conflictwhy);
  1237. notice(_("regarding %s containing %s:\n%s"), pfilename,
  1238. pkgbin_name(pkg, &pkg->available, pnaw_nonambig), conflictwhy.buf);
  1239. if (!force_conflicts(dep->list))
  1240. ohshit(_("conflicting packages - not installing %.250s"),
  1241. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1242. warning(_("ignoring conflict, may proceed anyway!"));
  1243. varbuf_destroy(&conflictwhy);
  1244. return;
  1245. }
  1246. void cu_cidir(int argc, void **argv) {
  1247. char *cidir= (char*)argv[0];
  1248. char *cidirrest= (char*)argv[1];
  1249. cidirrest[-1] = '\0';
  1250. path_remove_tree(cidir);
  1251. }
  1252. void cu_fileslist(int argc, void **argv) {
  1253. destroyobstack();
  1254. }
  1255. int
  1256. archivefiles(const char *const *argv)
  1257. {
  1258. const char *volatile thisarg;
  1259. const char *const *volatile argp;
  1260. jmp_buf ejbuf;
  1261. enum modstatdb_rw msdbflags;
  1262. trigproc_install_hooks();
  1263. if (f_noact)
  1264. msdbflags = msdbrw_readonly;
  1265. else if (cipaction->arg_int == act_avail)
  1266. msdbflags = msdbrw_readonly | msdbrw_available_write;
  1267. else if (fc_nonroot)
  1268. msdbflags = msdbrw_write;
  1269. else
  1270. msdbflags = msdbrw_needsuperuser;
  1271. modstatdb_open(msdbflags);
  1272. checkpath();
  1273. pkg_infodb_upgrade();
  1274. log_message("startup archives %s", cipaction->olong);
  1275. if (f_recursive) {
  1276. int pi[2], nfiles, c, i, rc;
  1277. pid_t pid;
  1278. FILE *pf;
  1279. static struct varbuf findoutput;
  1280. const char **arglist;
  1281. char *p;
  1282. if (!*argv)
  1283. badusage(_("--%s --recursive needs at least one path argument"),cipaction->olong);
  1284. m_pipe(pi);
  1285. pid = subproc_fork();
  1286. if (pid == 0) {
  1287. struct command cmd;
  1288. const char *const *ap;
  1289. m_dup2(pi[1],1); close(pi[0]); close(pi[1]);
  1290. command_init(&cmd, FIND, _("find for dpkg --recursive"));
  1291. command_add_args(&cmd, FIND, "-L", NULL);
  1292. for (ap = argv; *ap; ap++) {
  1293. if (strchr(FIND_EXPRSTARTCHARS,(*ap)[0])) {
  1294. char *a;
  1295. m_asprintf(&a, "./%s", *ap);
  1296. command_add_arg(&cmd, a);
  1297. } else {
  1298. command_add_arg(&cmd, (const char *)*ap);
  1299. }
  1300. }
  1301. command_add_args(&cmd, "-name", "*.deb", "-type", "f", "-print0", NULL);
  1302. command_exec(&cmd);
  1303. }
  1304. close(pi[1]);
  1305. nfiles= 0;
  1306. pf= fdopen(pi[0],"r"); if (!pf) ohshite(_("failed to fdopen find's pipe"));
  1307. varbuf_reset(&findoutput);
  1308. while ((c= fgetc(pf)) != EOF) {
  1309. varbuf_add_char(&findoutput, c);
  1310. if (!c) nfiles++;
  1311. }
  1312. if (ferror(pf)) ohshite(_("error reading find's pipe"));
  1313. if (fclose(pf)) ohshite(_("error closing find's pipe"));
  1314. rc = subproc_reap(pid, "find", SUBPROC_RETERROR);
  1315. if (rc != 0)
  1316. ohshit(_("find for --recursive returned unhandled error %i"), rc);
  1317. if (!nfiles)
  1318. ohshit(_("searched, but found no packages (files matching *.deb)"));
  1319. arglist= m_malloc(sizeof(char*)*(nfiles+1));
  1320. p = findoutput.buf;
  1321. for (i = 0; i < nfiles; i++) {
  1322. arglist[i] = p;
  1323. while (*p++ != '\0') ;
  1324. }
  1325. arglist[i] = NULL;
  1326. argp= arglist;
  1327. } else {
  1328. if (!*argv) badusage(_("--%s needs at least one package archive file argument"),
  1329. cipaction->olong);
  1330. argp= argv;
  1331. }
  1332. currenttime = time(NULL);
  1333. /* Initialize fname variables contents. */
  1334. varbuf_reset(&fnamevb);
  1335. varbuf_reset(&fnametmpvb);
  1336. varbuf_reset(&fnamenewvb);
  1337. varbuf_add_str(&fnamevb, instdir);
  1338. varbuf_add_str(&fnametmpvb, instdir);
  1339. varbuf_add_str(&fnamenewvb, instdir);
  1340. fnameidlu= fnamevb.used;
  1341. ensure_diversions();
  1342. ensure_statoverrides(STATDB_PARSE_NORMAL);
  1343. while ((thisarg = *argp++) != NULL) {
  1344. if (setjmp(ejbuf)) {
  1345. pop_error_context(ehflag_bombout);
  1346. if (abort_processing)
  1347. break;
  1348. continue;
  1349. }
  1350. push_error_context_jump(&ejbuf, print_error_perarchive, thisarg);
  1351. dpkg_selabel_load();
  1352. process_archive(thisarg);
  1353. onerr_abort++;
  1354. m_output(stdout, _("<standard output>"));
  1355. m_output(stderr, _("<standard error>"));
  1356. onerr_abort--;
  1357. pop_error_context(ehflag_normaltidy);
  1358. }
  1359. dpkg_selabel_close();
  1360. switch (cipaction->arg_int) {
  1361. case act_install:
  1362. case act_configure:
  1363. case act_triggers:
  1364. case act_remove:
  1365. case act_purge:
  1366. process_queue();
  1367. case act_unpack:
  1368. case act_avail:
  1369. break;
  1370. default:
  1371. internerr("unknown action '%d'", cipaction->arg_int);
  1372. }
  1373. trigproc_run_deferred();
  1374. modstatdb_shutdown();
  1375. return 0;
  1376. }
  1377. /**
  1378. * Decide whether we want to install a new version of the package.
  1379. *
  1380. * @param pkg The package with the version we might want to install
  1381. *
  1382. * @retval true If the package should be skipped.
  1383. * @retval false If the package should be installed.
  1384. */
  1385. bool
  1386. wanttoinstall(struct pkginfo *pkg)
  1387. {
  1388. int rc;
  1389. if (pkg->want != PKG_WANT_INSTALL && pkg->want != PKG_WANT_HOLD) {
  1390. if (f_alsoselect) {
  1391. printf(_("Selecting previously unselected package %s.\n"),
  1392. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1393. return true;
  1394. } else {
  1395. printf(_("Skipping unselected package %s.\n"),
  1396. pkgbin_name(pkg, &pkg->available, pnaw_nonambig));
  1397. return false;
  1398. }
  1399. }
  1400. if (pkg->eflag & PKG_EFLAG_REINSTREQ)
  1401. return true;
  1402. if (pkg->status < PKG_STAT_UNPACKED)
  1403. return true;
  1404. rc = dpkg_version_compare(&pkg->available.version, &pkg->installed.version);
  1405. if (rc > 0) {
  1406. return true;
  1407. } else if (rc == 0) {
  1408. /* Same version fully installed. */
  1409. if (f_skipsame) {
  1410. notice(_("version %.250s of %.250s already installed, skipping"),
  1411. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1412. pkg_name(pkg, pnaw_nonambig));
  1413. return false;
  1414. } else {
  1415. return true;
  1416. }
  1417. } else {
  1418. if (fc_downgrade) {
  1419. warning(_("downgrading %.250s from %.250s to %.250s"),
  1420. pkg_name(pkg, pnaw_nonambig),
  1421. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1422. versiondescribe(&pkg->available.version, vdew_nonambig));
  1423. return true;
  1424. } else {
  1425. notice(_("will not downgrade %.250s from %.250s to %.250s, skipping"),
  1426. pkg_name(pkg, pnaw_nonambig),
  1427. versiondescribe(&pkg->installed.version, vdew_nonambig),
  1428. versiondescribe(&pkg->available.version, vdew_nonambig));
  1429. return false;
  1430. }
  1431. }
  1432. }
  1433. struct fileinlist *
  1434. filenamenode_queue_push(struct filenamenode_queue *queue,
  1435. struct filenamenode *namenode)
  1436. {
  1437. struct fileinlist *node;
  1438. node = m_malloc(sizeof(*node));
  1439. node->next = NULL;
  1440. node->namenode = namenode;
  1441. *queue->tail = node;
  1442. queue->tail = &node->next;
  1443. return node;
  1444. }