archives.c 53 KB

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