opkg_install.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. /* opkg_install.c - the opkg package management system
  2. Carl D. Worth
  3. Copyright (C) 2001 University of Southern California
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License as
  6. published by the Free Software Foundation; either version 2, or (at
  7. your option) any later version.
  8. This program is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. */
  13. #include <stdio.h>
  14. #include <time.h>
  15. #include <signal.h>
  16. #include <unistd.h>
  17. #include <sys/stat.h>
  18. #include "pkg.h"
  19. #include "pkg_hash.h"
  20. #include "pkg_extract.h"
  21. #include "opkg_install.h"
  22. #include "opkg_configure.h"
  23. #include "opkg_download.h"
  24. #include "opkg_remove.h"
  25. #include "opkg_utils.h"
  26. #include "opkg_message.h"
  27. #include "opkg_cmd.h"
  28. #include "opkg_defines.h"
  29. #include "sprintf_alloc.h"
  30. #include "file_util.h"
  31. #include "xsystem.h"
  32. #include "libbb/libbb.h"
  33. static int satisfy_dependencies_for(pkg_t * pkg)
  34. {
  35. int i, err;
  36. pkg_vec_t *depends = pkg_vec_alloc();
  37. pkg_t *dep;
  38. char **tmp, **unresolved = NULL, *prev = NULL;
  39. int ndepends;
  40. ndepends = pkg_hash_fetch_unsatisfied_dependencies(pkg, depends,
  41. &unresolved, 0);
  42. if (unresolved) {
  43. opkg_msg(ERROR,
  44. "Cannot satisfy the following dependencies for %s:\n",
  45. pkg->name);
  46. tmp = unresolved;
  47. while (*unresolved) {
  48. if (!prev || strcmp(*unresolved, prev))
  49. opkg_message(ERROR, "\t%s\n", *unresolved);
  50. prev = *unresolved;
  51. unresolved++;
  52. }
  53. unresolved = tmp;
  54. while (*unresolved) {
  55. free(*unresolved);
  56. unresolved++;
  57. }
  58. free(tmp);
  59. if (!conf->force_depends) {
  60. opkg_msg(INFO,
  61. "This could mean that your package list is out of date or that the packages\n"
  62. "mentioned above do not yet exist (try 'opkg update'). To proceed in spite\n"
  63. "of this problem try again with the '-force-depends' option.\n");
  64. pkg_vec_free(depends);
  65. return -1;
  66. }
  67. }
  68. if (ndepends <= 0) {
  69. pkg_vec_free(depends);
  70. return 0;
  71. }
  72. /* Mark packages as to-be-installed */
  73. for (i = 0; i < depends->len; i++) {
  74. /* Dependencies should be installed the same place as pkg */
  75. if (depends->pkgs[i]->dest == NULL) {
  76. depends->pkgs[i]->dest = pkg->dest;
  77. }
  78. depends->pkgs[i]->state_want = SW_INSTALL;
  79. }
  80. for (i = 0; i < depends->len; i++) {
  81. dep = depends->pkgs[i];
  82. /* The package was uninstalled when we started, but another
  83. dep earlier in this loop may have depended on it and pulled
  84. it in, so check first. */
  85. if ((dep->state_status != SS_INSTALLED)
  86. && (dep->state_status != SS_UNPACKED)) {
  87. opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
  88. err = opkg_install_pkg(dep, 0);
  89. /* mark this package as having been automatically installed to
  90. * satisfy a dependancy */
  91. dep->auto_installed = 1;
  92. if (err) {
  93. pkg_vec_free(depends);
  94. return err;
  95. }
  96. }
  97. }
  98. pkg_vec_free(depends);
  99. return 0;
  100. }
  101. static int check_conflicts_for(pkg_t * pkg)
  102. {
  103. int i;
  104. pkg_vec_t *conflicts = NULL;
  105. message_level_t level;
  106. if (conf->force_depends) {
  107. level = NOTICE;
  108. } else {
  109. level = ERROR;
  110. }
  111. if (!conf->force_depends)
  112. conflicts = pkg_hash_fetch_conflicts(pkg);
  113. if (conflicts) {
  114. opkg_msg(level, "The following packages conflict with %s:\n",
  115. pkg->name);
  116. i = 0;
  117. while (i < conflicts->len)
  118. opkg_msg(level, "\t%s", conflicts->pkgs[i++]->name);
  119. opkg_message(level, "\n");
  120. pkg_vec_free(conflicts);
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static int update_file_ownership(pkg_t * new_pkg, pkg_t * old_pkg)
  126. {
  127. str_list_t *new_list, *old_list;
  128. str_list_elt_t *iter, *niter;
  129. new_list = pkg_get_installed_files(new_pkg);
  130. if (new_list == NULL)
  131. return -1;
  132. for (iter = str_list_first(new_list), niter =
  133. str_list_next(new_list, iter); iter;
  134. iter = niter, niter = str_list_next(new_list, niter)) {
  135. char *new_file = (char *)iter->data;
  136. pkg_t *owner = file_hash_get_file_owner(new_file);
  137. pkg_t *obs = hash_table_get(&conf->obs_file_hash, new_file);
  138. opkg_msg(DEBUG2,
  139. "%s: new_pkg=%s wants file %s, from owner=%s\n",
  140. __func__, new_pkg->name, new_file,
  141. owner ? owner->name : "<NULL>");
  142. if (!owner || (owner == old_pkg) || obs)
  143. file_hash_set_file_owner(new_file, new_pkg);
  144. }
  145. if (old_pkg) {
  146. old_list = pkg_get_installed_files(old_pkg);
  147. if (old_list == NULL) {
  148. pkg_free_installed_files(new_pkg);
  149. return -1;
  150. }
  151. for (iter = str_list_first(old_list), niter =
  152. str_list_next(old_list, iter); iter;
  153. iter = niter, niter = str_list_next(old_list, niter)) {
  154. char *old_file = (char *)iter->data;
  155. pkg_t *owner = file_hash_get_file_owner(old_file);
  156. if (!owner || (owner == old_pkg)) {
  157. /* obsolete */
  158. hash_table_insert(&conf->obs_file_hash,
  159. old_file, old_pkg);
  160. }
  161. }
  162. pkg_free_installed_files(old_pkg);
  163. }
  164. pkg_free_installed_files(new_pkg);
  165. return 0;
  166. }
  167. static int verify_pkg_installable(pkg_t * pkg)
  168. {
  169. unsigned long kbs_available, pkg_size_kbs;
  170. unsigned long installed_size;
  171. char *root_dir = NULL;
  172. struct stat s;
  173. installed_size = (unsigned long) pkg_get_int(pkg, PKG_INSTALLED_SIZE);
  174. if (conf->force_space || installed_size == 0)
  175. return 0;
  176. if (pkg->dest) {
  177. if (!strcmp(pkg->dest->name, "root") && conf->overlay_root
  178. && !stat(conf->overlay_root, &s) && (s.st_mode & S_IFDIR))
  179. root_dir = conf->overlay_root;
  180. else
  181. root_dir = pkg->dest->root_dir;
  182. }
  183. if (!root_dir)
  184. root_dir = conf->default_dest->root_dir;
  185. kbs_available = get_available_kbytes(root_dir);
  186. pkg_size_kbs = (installed_size + 1023) / 1024;
  187. if (pkg_size_kbs >= kbs_available) {
  188. opkg_msg(ERROR, "Only have %ldkb available on filesystem %s, "
  189. "pkg %s needs %ld\n",
  190. kbs_available, root_dir, pkg->name, pkg_size_kbs);
  191. return -1;
  192. }
  193. return 0;
  194. }
  195. static int unpack_pkg_control_files(pkg_t * pkg)
  196. {
  197. int err;
  198. char *conffiles_file_name;
  199. char *root_dir;
  200. char *tmp_unpack_dir;
  201. FILE *conffiles_file;
  202. conffile_list_t *cl;
  203. sprintf_alloc(&tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir,
  204. pkg->name);
  205. tmp_unpack_dir = mkdtemp(tmp_unpack_dir);
  206. if (tmp_unpack_dir == NULL) {
  207. opkg_perror(ERROR, "Failed to create temporary directory");
  208. return -1;
  209. }
  210. pkg_set_string(pkg, PKG_TMP_UNPACK_DIR, tmp_unpack_dir);
  211. err = pkg_extract_control_files_to_dir(pkg, tmp_unpack_dir);
  212. if (err) {
  213. return err;
  214. }
  215. /* XXX: CLEANUP: There might be a cleaner place to read in the
  216. conffiles. Seems like I should be able to get everything to go
  217. through pkg_init_from_file. If so, maybe it would make sense to
  218. move all of unpack_pkg_control_files to that function. */
  219. /* Don't need to re-read conffiles if we already have it */
  220. cl = pkg_get_ptr(pkg, PKG_CONFFILES);
  221. if (cl && !nv_pair_list_empty(cl)) {
  222. return 0;
  223. }
  224. sprintf_alloc(&conffiles_file_name, "%s/conffiles",
  225. tmp_unpack_dir);
  226. if (!file_exists(conffiles_file_name)) {
  227. free(conffiles_file_name);
  228. return 0;
  229. }
  230. conffiles_file = fopen(conffiles_file_name, "r");
  231. if (conffiles_file == NULL) {
  232. opkg_perror(ERROR, "Failed to open %s", conffiles_file_name);
  233. free(conffiles_file_name);
  234. return -1;
  235. }
  236. free(conffiles_file_name);
  237. cl = xcalloc(1, sizeof(*cl));
  238. conffile_list_init(cl);
  239. while (1) {
  240. char *cf_name;
  241. char *cf_name_in_dest;
  242. int i;
  243. cf_name = file_read_line_alloc(conffiles_file);
  244. if (cf_name == NULL) {
  245. break;
  246. }
  247. if (cf_name[0] == '\0') {
  248. continue;
  249. }
  250. for (i = strlen(cf_name) - 1;
  251. (i >= 0) && (cf_name[i] == ' ' || cf_name[i] == '\t');
  252. i--) {
  253. cf_name[i] = '\0';
  254. }
  255. /* Prepend dest->root_dir to conffile name.
  256. Take pains to avoid multiple slashes. */
  257. root_dir = pkg->dest->root_dir;
  258. if (conf->offline_root)
  259. /* skip the offline_root prefix */
  260. root_dir =
  261. pkg->dest->root_dir + strlen(conf->offline_root);
  262. sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
  263. cf_name[0] == '/' ? (cf_name + 1) : cf_name);
  264. /* Can't get an md5sum now, (file isn't extracted yet).
  265. We'll wait until resolve_conffiles */
  266. conffile_list_append(cl, cf_name_in_dest, NULL);
  267. free(cf_name);
  268. free(cf_name_in_dest);
  269. }
  270. pkg_set_ptr(pkg, PKG_CONFFILES, cl);
  271. fclose(conffiles_file);
  272. return 0;
  273. }
  274. /*
  275. * Remove packages which were auto_installed due to a dependency by old_pkg,
  276. * which are no longer a dependency in the new (upgraded) pkg.
  277. */
  278. static int pkg_remove_orphan_dependent(pkg_t * pkg, pkg_t * old_pkg)
  279. {
  280. int j, l, found, r, err = 0;
  281. int n_deps;
  282. pkg_t *p;
  283. struct compound_depend *cd0, *cd1;
  284. abstract_pkg_t **dependents;
  285. for (cd0 = pkg_get_ptr(old_pkg, PKG_DEPENDS); cd0 && cd0->type; cd0++) {
  286. if (cd0->type != DEPEND)
  287. continue;
  288. for (j = 0; j < cd0->possibility_count; j++) {
  289. found = 0;
  290. for (cd1 = pkg_get_ptr(pkg, PKG_DEPENDS); cd1 && cd1->type; cd1++) {
  291. if (cd1->type != DEPEND)
  292. continue;
  293. for (l = 0; l < cd1->possibility_count; l++) {
  294. if (cd0->possibilities[j]
  295. == cd1->possibilities[l]) {
  296. found = 1;
  297. break;
  298. }
  299. }
  300. if (found)
  301. break;
  302. }
  303. if (found)
  304. continue;
  305. /*
  306. * old_pkg has a dependency that pkg does not.
  307. */
  308. p = pkg_hash_fetch_installed_by_name(cd0->
  309. possibilities[j]->
  310. pkg->name);
  311. if (!p)
  312. continue;
  313. if (!p->auto_installed)
  314. continue;
  315. n_deps = pkg_has_installed_dependents(p, &dependents);
  316. n_deps--; /* don't count old_pkg */
  317. if (n_deps == 0) {
  318. opkg_msg(NOTICE, "%s was autoinstalled and is "
  319. "now orphaned, removing.\n", p->name);
  320. /* p has one installed dependency (old_pkg),
  321. * which we need to ignore during removal. */
  322. p->state_flag |= SF_REPLACE;
  323. r = opkg_remove_pkg(p, 0);
  324. if (!err)
  325. err = r;
  326. } else
  327. opkg_msg(INFO, "%s was autoinstalled and is "
  328. "still required by %d "
  329. "installed packages.\n",
  330. p->name, n_deps);
  331. }
  332. }
  333. return err;
  334. }
  335. /* returns number of installed replacees */
  336. static int
  337. pkg_get_installed_replacees(pkg_t * pkg, pkg_vec_t * installed_replacees)
  338. {
  339. abstract_pkg_t **replaces = pkg_get_ptr(pkg, PKG_REPLACES);
  340. int j;
  341. while (replaces && *replaces) {
  342. abstract_pkg_t *ab_pkg = *replaces++;
  343. pkg_vec_t *pkg_vec = ab_pkg->pkgs;
  344. if (pkg_vec) {
  345. for (j = 0; j < pkg_vec->len; j++) {
  346. pkg_t *replacee = pkg_vec->pkgs[j];
  347. if (!pkg_conflicts(pkg, replacee))
  348. continue;
  349. if (replacee->state_status == SS_INSTALLED) {
  350. pkg_vec_insert(installed_replacees,
  351. replacee);
  352. }
  353. }
  354. }
  355. }
  356. return installed_replacees->len;
  357. }
  358. static int pkg_remove_installed_replacees(pkg_vec_t * replacees)
  359. {
  360. int i;
  361. int replaces_count = replacees->len;
  362. for (i = 0; i < replaces_count; i++) {
  363. pkg_t *replacee = replacees->pkgs[i];
  364. int err;
  365. replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
  366. err = opkg_remove_pkg(replacee, 0);
  367. if (err)
  368. return err;
  369. }
  370. return 0;
  371. }
  372. /* to unwind the removal: make sure they are installed */
  373. static int pkg_remove_installed_replacees_unwind(pkg_vec_t * replacees)
  374. {
  375. int i, err;
  376. int replaces_count = replacees->len;
  377. for (i = 0; i < replaces_count; i++) {
  378. pkg_t *replacee = replacees->pkgs[i];
  379. if (replacee->state_status != SS_INSTALLED) {
  380. opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
  381. err = opkg_install_pkg(replacee, 0);
  382. if (err)
  383. return err;
  384. }
  385. }
  386. return 0;
  387. }
  388. /* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
  389. static int
  390. opkg_install_check_downgrade(pkg_t * pkg, pkg_t * old_pkg, int message)
  391. {
  392. if (old_pkg) {
  393. char message_out[15];
  394. char *old_version = pkg_version_str_alloc(old_pkg);
  395. char *new_version = pkg_version_str_alloc(pkg);
  396. int cmp = pkg_compare_versions(old_pkg, pkg);
  397. int rc = 0;
  398. memset(message_out, '\x0', 15);
  399. strncpy(message_out, "Upgrading ", strlen("Upgrading "));
  400. if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */
  401. cmp = -1; /* then we force opkg to downgrade */
  402. strncpy(message_out, "Downgrading ", strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
  403. /* reinstall, and some check could fail asking the "force-reinstall" option */
  404. }
  405. if (cmp > 0) {
  406. if (!conf->download_only)
  407. opkg_msg(NOTICE,
  408. "Not downgrading package %s on %s from %s to %s.\n",
  409. old_pkg->name, old_pkg->dest->name,
  410. old_version, new_version);
  411. rc = 1;
  412. } else if (cmp < 0) {
  413. if (!conf->download_only)
  414. opkg_msg(NOTICE,
  415. "%s%s on %s from %s to %s...\n",
  416. message_out, pkg->name,
  417. old_pkg->dest->name, old_version,
  418. new_version);
  419. pkg->dest = old_pkg->dest;
  420. rc = 0;
  421. } else { /* cmp == 0 */
  422. if (!conf->download_only)
  423. opkg_msg(NOTICE,
  424. "%s (%s) already install on %s.\n",
  425. pkg->name, new_version,
  426. old_pkg->dest->name);
  427. rc = 1;
  428. }
  429. free(old_version);
  430. free(new_version);
  431. return rc;
  432. } else {
  433. char message_out[15];
  434. memset(message_out, '\x0', 15);
  435. if (message)
  436. strncpy(message_out, "Upgrading ",
  437. strlen("Upgrading "));
  438. else
  439. strncpy(message_out, "Installing ",
  440. strlen("Installing "));
  441. char *version = pkg_version_str_alloc(pkg);
  442. if (!conf->download_only)
  443. opkg_msg(NOTICE, "%s%s (%s) to %s...\n", message_out,
  444. pkg->name, version, pkg->dest->name);
  445. free(version);
  446. }
  447. return 0;
  448. }
  449. static int prerm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg)
  450. {
  451. /* DPKG_INCOMPATIBILITY:
  452. dpkg does some things here that we don't do yet. Do we care?
  453. 1. If a version of the package is already installed, call
  454. old-prerm upgrade new-version
  455. 2. If the script runs but exits with a non-zero exit status
  456. new-prerm failed-upgrade old-version
  457. Error unwind, for both the above cases:
  458. old-postinst abort-upgrade new-version
  459. */
  460. int err;
  461. char *script_args;
  462. char *new_version;
  463. if (!old_pkg || !pkg)
  464. return 0;
  465. new_version = pkg_version_str_alloc(pkg);
  466. sprintf_alloc(&script_args, "upgrade %s", new_version);
  467. free(new_version);
  468. err = pkg_run_script(old_pkg, "prerm", script_args);
  469. free(script_args);
  470. if (err != 0) {
  471. opkg_msg(ERROR, "prerm script for package \"%s\" failed\n",
  472. old_pkg->name);
  473. return -1;
  474. }
  475. return 0;
  476. }
  477. static int prerm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg)
  478. {
  479. /* DPKG_INCOMPATIBILITY:
  480. dpkg does some things here that we don't do yet. Do we care?
  481. (See prerm_upgrade_old_package for details)
  482. */
  483. return 0;
  484. }
  485. static int prerm_deconfigure_conflictors(pkg_t * pkg, pkg_vec_t * conflictors)
  486. {
  487. /* DPKG_INCOMPATIBILITY:
  488. dpkg does some things here that we don't do yet. Do we care?
  489. 2. If a 'conflicting' package is being removed at the same time:
  490. 1. If any packages depended on that conflicting package and
  491. --auto-deconfigure is specified, call, for each such package:
  492. deconfigured's-prerm deconfigure \
  493. in-favour package-being-installed version \
  494. removing conflicting-package version
  495. Error unwind:
  496. deconfigured's-postinst abort-deconfigure \
  497. in-favour package-being-installed-but-failed version \
  498. removing conflicting-package version
  499. The deconfigured packages are marked as requiring
  500. configuration, so that if --install is used they will be
  501. configured again if possible.
  502. 2. To prepare for removal of the conflicting package, call:
  503. conflictor's-prerm remove in-favour package new-version
  504. Error unwind:
  505. conflictor's-postinst abort-remove in-favour package new-version
  506. */
  507. return 0;
  508. }
  509. static int
  510. prerm_deconfigure_conflictors_unwind(pkg_t * pkg, pkg_vec_t * conflictors)
  511. {
  512. /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
  513. do yet. Do we care? (See prerm_deconfigure_conflictors for
  514. details) */
  515. return 0;
  516. }
  517. static int preinst_configure(pkg_t * pkg, pkg_t * old_pkg)
  518. {
  519. int err;
  520. char *preinst_args;
  521. if (old_pkg) {
  522. char *old_version = pkg_version_str_alloc(old_pkg);
  523. sprintf_alloc(&preinst_args, "upgrade %s", old_version);
  524. free(old_version);
  525. } else if (pkg->state_status == SS_CONFIG_FILES) {
  526. char *pkg_version = pkg_version_str_alloc(pkg);
  527. sprintf_alloc(&preinst_args, "install %s", pkg_version);
  528. free(pkg_version);
  529. } else {
  530. preinst_args = xstrdup("install");
  531. }
  532. err = pkg_run_script(pkg, "preinst", preinst_args);
  533. if (err) {
  534. opkg_msg(ERROR, "Aborting installation of %s.\n", pkg->name);
  535. return -1;
  536. }
  537. free(preinst_args);
  538. return 0;
  539. }
  540. static int preinst_configure_unwind(pkg_t * pkg, pkg_t * old_pkg)
  541. {
  542. /* DPKG_INCOMPATIBILITY:
  543. dpkg does the following error unwind, should we?
  544. pkg->postrm abort-upgrade old-version
  545. OR pkg->postrm abort-install old-version
  546. OR pkg->postrm abort-install
  547. */
  548. return 0;
  549. }
  550. static char *backup_filename_alloc(const char *file_name)
  551. {
  552. char *backup;
  553. sprintf_alloc(&backup, "%s%s", file_name, OPKG_BACKUP_SUFFIX);
  554. return backup;
  555. }
  556. static int backup_make_backup(const char *file_name)
  557. {
  558. int err;
  559. char *backup;
  560. backup = backup_filename_alloc(file_name);
  561. err = file_copy(file_name, backup);
  562. if (err) {
  563. opkg_msg(ERROR, "Failed to copy %s to %s\n", file_name, backup);
  564. }
  565. free(backup);
  566. return err;
  567. }
  568. static int backup_exists_for(const char *file_name)
  569. {
  570. int ret;
  571. char *backup;
  572. backup = backup_filename_alloc(file_name);
  573. ret = file_exists(backup);
  574. free(backup);
  575. return ret;
  576. }
  577. static int backup_remove(const char *file_name)
  578. {
  579. char *backup;
  580. backup = backup_filename_alloc(file_name);
  581. unlink(backup);
  582. free(backup);
  583. return 0;
  584. }
  585. static int backup_modified_conffiles(pkg_t * pkg, pkg_t * old_pkg)
  586. {
  587. int err;
  588. conffile_list_elt_t *iter;
  589. conffile_t *cf;
  590. conffile_list_t *cl;
  591. if (conf->noaction)
  592. return 0;
  593. /* Backup all modified conffiles */
  594. if (old_pkg) {
  595. cl = pkg_get_ptr(old_pkg, PKG_CONFFILES);
  596. for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
  597. iter = nv_pair_list_next(cl, iter)) {
  598. char *cf_name;
  599. cf = iter->data;
  600. cf_name = root_filename_alloc(cf->name);
  601. /* Don't worry if the conffile is just plain gone */
  602. if (file_exists(cf_name)
  603. && conffile_has_been_modified(cf)) {
  604. err = backup_make_backup(cf_name);
  605. if (err) {
  606. return err;
  607. }
  608. }
  609. free(cf_name);
  610. }
  611. }
  612. /* Backup all conffiles that were not conffiles in old_pkg */
  613. cl = pkg_get_ptr(pkg, PKG_CONFFILES);
  614. for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
  615. iter = nv_pair_list_next(cl, iter)) {
  616. char *cf_name;
  617. cf = (conffile_t *) iter->data;
  618. cf_name = root_filename_alloc(cf->name);
  619. /* Ignore if this was a conffile in old_pkg as well */
  620. if (pkg_get_conffile(old_pkg, cf->name)) {
  621. continue;
  622. }
  623. if (file_exists(cf_name) && (!backup_exists_for(cf_name))) {
  624. err = backup_make_backup(cf_name);
  625. if (err) {
  626. return err;
  627. }
  628. }
  629. free(cf_name);
  630. }
  631. return 0;
  632. }
  633. static int backup_modified_conffiles_unwind(pkg_t * pkg, pkg_t * old_pkg)
  634. {
  635. conffile_list_t *cl;
  636. conffile_list_elt_t *iter;
  637. if (old_pkg) {
  638. cl = pkg_get_ptr(old_pkg, PKG_CONFFILES);
  639. for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
  640. iter = nv_pair_list_next(cl, iter)) {
  641. backup_remove(((nv_pair_t *) iter->data)->name);
  642. }
  643. }
  644. cl = pkg_get_ptr(pkg, PKG_CONFFILES);
  645. for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
  646. iter = nv_pair_list_next(cl, iter)) {
  647. backup_remove(((nv_pair_t *) iter->data)->name);
  648. }
  649. return 0;
  650. }
  651. static int check_data_file_clashes(pkg_t * pkg, pkg_t * old_pkg)
  652. {
  653. /* DPKG_INCOMPATIBILITY:
  654. opkg takes a slightly different approach than dpkg at this
  655. point. dpkg installs each file in the new package while
  656. creating a backup for any file that is replaced, (so that it
  657. can unwind if necessary). To avoid complexity and redundant
  658. storage, opkg doesn't do any installation until later, (at the
  659. point at which dpkg removes the backups.
  660. But, we do have to check for data file clashes, since after
  661. installing a package with a file clash, removing either of the
  662. packages involved in the clash has the potential to break the
  663. other package.
  664. */
  665. str_list_t *files_list;
  666. str_list_elt_t *iter, *niter;
  667. char *filename;
  668. int clashes = 0;
  669. files_list = pkg_get_installed_files(pkg);
  670. if (files_list == NULL)
  671. return -1;
  672. for (iter = str_list_first(files_list), niter =
  673. str_list_next(files_list, iter); iter;
  674. iter = niter, niter = str_list_next(files_list, iter)) {
  675. filename = (char *)iter->data;
  676. if (file_exists(filename) && (!file_is_dir(filename))) {
  677. pkg_t *owner;
  678. pkg_t *obs;
  679. if (backup_exists_for(filename)) {
  680. continue;
  681. }
  682. /* Pre-existing files are OK if force-overwrite was asserted. */
  683. if (conf->force_overwrite) {
  684. /* but we need to change who owns this file */
  685. file_hash_set_file_owner(filename, pkg);
  686. continue;
  687. }
  688. owner = file_hash_get_file_owner(filename);
  689. /* Pre-existing files are OK if owned by the pkg being upgraded. */
  690. if (owner && old_pkg) {
  691. if (strcmp(owner->name, old_pkg->name) == 0) {
  692. continue;
  693. }
  694. }
  695. /* Pre-existing files are OK if owned by a package replaced by new pkg. */
  696. if (owner) {
  697. opkg_msg(DEBUG2,
  698. "Checking replaces for %s in package %s\n",
  699. filename, owner->name);
  700. if (pkg_replaces(pkg, owner)) {
  701. continue;
  702. }
  703. /* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
  704. then it's ok to overwrite. */
  705. if (strcmp(owner->name, pkg->name) == 0) {
  706. opkg_msg(INFO,
  707. "Replacing pre-existing file %s"
  708. " owned by package %s\n",
  709. filename, owner->name);
  710. continue;
  711. }
  712. }
  713. /* Pre-existing files are OK if they are obsolete */
  714. obs = hash_table_get(&conf->obs_file_hash, filename);
  715. if (obs) {
  716. opkg_msg(INFO,
  717. "Pre-exiting file %s is obsolete."
  718. " obs_pkg=%s\n", filename, obs->name);
  719. continue;
  720. }
  721. /* We have found a clash. */
  722. opkg_msg(ERROR, "Package %s wants to install file %s\n"
  723. "\tBut that file is already provided by package ",
  724. pkg->name, filename);
  725. if (owner) {
  726. opkg_message(ERROR, "%s\n", owner->name);
  727. } else {
  728. opkg_message(ERROR, "<no package>\n"
  729. "Please move this file out of the way and try again.\n");
  730. }
  731. clashes++;
  732. }
  733. }
  734. pkg_free_installed_files(pkg);
  735. return clashes;
  736. }
  737. /*
  738. * XXX: This function sucks, as does the below comment.
  739. */
  740. static int check_data_file_clashes_change(pkg_t * pkg, pkg_t * old_pkg)
  741. {
  742. /* Basically that's the worst hack I could do to be able to change ownership of
  743. file list, but, being that we have no way to unwind the mods, due to structure
  744. of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
  745. What we do here is change the ownership of file in hash if a replace ( or similar events
  746. happens )
  747. Only the action that are needed to change name should be considered.
  748. @@@ To change after 1.0 release.
  749. */
  750. str_list_t *files_list;
  751. str_list_elt_t *iter, *niter;
  752. files_list = pkg_get_installed_files(pkg);
  753. if (files_list == NULL)
  754. return -1;
  755. for (iter = str_list_first(files_list), niter =
  756. str_list_next(files_list, iter); iter;
  757. iter = niter, niter = str_list_next(files_list, niter)) {
  758. char *filename = (char *)iter->data;
  759. if (file_exists(filename) && (!file_is_dir(filename))) {
  760. pkg_t *owner;
  761. owner = file_hash_get_file_owner(filename);
  762. if (conf->force_overwrite) {
  763. /* but we need to change who owns this file */
  764. file_hash_set_file_owner(filename, pkg);
  765. continue;
  766. }
  767. /* Pre-existing files are OK if owned by a package replaced by new pkg. */
  768. if (owner) {
  769. if (pkg_replaces(pkg, owner)) {
  770. /* It's now time to change the owner of that file.
  771. It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
  772. opkg_msg(INFO,
  773. "Replacing pre-existing file %s "
  774. "owned by package %s\n",
  775. filename, owner->name);
  776. file_hash_set_file_owner(filename, pkg);
  777. continue;
  778. }
  779. }
  780. }
  781. }
  782. pkg_free_installed_files(pkg);
  783. return 0;
  784. }
  785. static int check_data_file_clashes_unwind(pkg_t * pkg, pkg_t * old_pkg)
  786. {
  787. /* Nothing to do since check_data_file_clashes doesn't change state */
  788. return 0;
  789. }
  790. static int postrm_upgrade_old_pkg(pkg_t * pkg, pkg_t * old_pkg)
  791. {
  792. /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
  793. 1. If the package is being upgraded, call
  794. old-postrm upgrade new-version
  795. 2. If this fails, attempt:
  796. new-postrm failed-upgrade old-version
  797. Error unwind, for both cases:
  798. old-preinst abort-upgrade new-version */
  799. int err;
  800. char *script_args;
  801. char *new_version;
  802. if (!old_pkg || !pkg)
  803. return 0;
  804. new_version = pkg_version_str_alloc(pkg);
  805. sprintf_alloc(&script_args, "upgrade %s", new_version);
  806. free(new_version);
  807. err = pkg_run_script(old_pkg, "postrm", script_args);
  808. free(script_args);
  809. if (err != 0) {
  810. opkg_msg(ERROR, "postrm script for package \"%s\" failed\n",
  811. old_pkg->name);
  812. return -1;
  813. }
  814. return 0;
  815. }
  816. static int postrm_upgrade_old_pkg_unwind(pkg_t * pkg, pkg_t * old_pkg)
  817. {
  818. /* DPKG_INCOMPATIBILITY:
  819. dpkg does some things here that we don't do yet. Do we care?
  820. (See postrm_upgrade_old_pkg for details)
  821. */
  822. return 0;
  823. }
  824. static int remove_obsolesced_files(pkg_t * pkg, pkg_t * old_pkg)
  825. {
  826. int err = 0;
  827. str_list_t *old_files;
  828. str_list_elt_t *of;
  829. str_list_t *new_files;
  830. str_list_elt_t *nf;
  831. hash_table_t new_files_table;
  832. old_files = pkg_get_installed_files(old_pkg);
  833. if (old_files == NULL)
  834. return -1;
  835. new_files = pkg_get_installed_files(pkg);
  836. if (new_files == NULL) {
  837. pkg_free_installed_files(old_pkg);
  838. return -1;
  839. }
  840. new_files_table.entries = NULL;
  841. hash_table_init("new_files", &new_files_table, 20);
  842. for (nf = str_list_first(new_files); nf;
  843. nf = str_list_next(new_files, nf)) {
  844. if (nf && nf->data)
  845. hash_table_insert(&new_files_table, nf->data, nf->data);
  846. }
  847. for (of = str_list_first(old_files); of;
  848. of = str_list_next(old_files, of)) {
  849. pkg_t *owner;
  850. char *old, *new;
  851. old = (char *)of->data;
  852. new = (char *)hash_table_get(&new_files_table, old);
  853. if (new)
  854. continue;
  855. if (file_is_dir(old)) {
  856. continue;
  857. }
  858. owner = file_hash_get_file_owner(old);
  859. if (owner != old_pkg) {
  860. /* in case obsolete file no longer belongs to old_pkg */
  861. continue;
  862. }
  863. /* old file is obsolete */
  864. opkg_msg(NOTICE, "Removing obsolete file %s.\n", old);
  865. if (!conf->noaction) {
  866. err = unlink(old);
  867. if (err) {
  868. opkg_perror(ERROR, "unlinking %s failed", old);
  869. }
  870. }
  871. }
  872. hash_table_deinit(&new_files_table);
  873. pkg_free_installed_files(old_pkg);
  874. pkg_free_installed_files(pkg);
  875. return err;
  876. }
  877. static int install_maintainer_scripts(pkg_t * pkg, pkg_t * old_pkg)
  878. {
  879. int ret;
  880. char *prefix;
  881. sprintf_alloc(&prefix, "%s.", pkg->name);
  882. ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
  883. pkg->dest->info_dir,
  884. prefix);
  885. free(prefix);
  886. return ret;
  887. }
  888. static int remove_disappeared(pkg_t * pkg)
  889. {
  890. /* DPKG_INCOMPATIBILITY:
  891. This is a fairly sophisticated dpkg operation. Shall we
  892. skip it? */
  893. /* Any packages all of whose files have been overwritten during the
  894. installation, and which aren't required for dependencies, are
  895. considered to have been removed. For each such package
  896. 1. disappearer's-postrm disappear overwriter overwriter-version
  897. 2. The package's maintainer scripts are removed
  898. 3. It is noted in the status database as being in a sane state,
  899. namely not installed (any conffiles it may have are ignored,
  900. rather than being removed by dpkg). Note that disappearing
  901. packages do not have their prerm called, because dpkg doesn't
  902. know in advance that the package is going to vanish.
  903. */
  904. return 0;
  905. }
  906. static int install_data_files(pkg_t * pkg)
  907. {
  908. int err;
  909. /* opkg takes a slightly different approach to data file backups
  910. than dpkg. Rather than removing backups at this point, we
  911. actually do the data file installation now. See comments in
  912. check_data_file_clashes() for more details. */
  913. opkg_msg(INFO, "Extracting data files to %s.\n", pkg->dest->root_dir);
  914. err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
  915. if (err) {
  916. return err;
  917. }
  918. opkg_msg(DEBUG, "Calling pkg_write_filelist.\n");
  919. err = pkg_write_filelist(pkg);
  920. if (err)
  921. return err;
  922. /* XXX: FEATURE: opkg should identify any files which existed
  923. before installation and which were overwritten, (see
  924. check_data_file_clashes()). What it must do is remove any such
  925. files from the filelist of the old package which provided the
  926. file. Otherwise, if the old package were removed at some point
  927. it would break the new package. Removing the new package will
  928. also break the old one, but this cannot be helped since the old
  929. package's file has already been deleted. This is the importance
  930. of check_data_file_clashes(), and only allowing opkg to install
  931. a clashing package with a user force. */
  932. return 0;
  933. }
  934. static int resolve_conffiles(pkg_t * pkg)
  935. {
  936. conffile_list_elt_t *iter;
  937. conffile_list_t *cl;
  938. conffile_t *cf;
  939. char *cf_backup;
  940. char *chksum;
  941. if (conf->noaction)
  942. return 0;
  943. cl = pkg_get_ptr(pkg, PKG_CONFFILES);
  944. for (iter = cl ? nv_pair_list_first(cl) : NULL; iter;
  945. iter = nv_pair_list_next(cl, iter)) {
  946. char *root_filename;
  947. cf = (conffile_t *) iter->data;
  948. root_filename = root_filename_alloc(cf->name);
  949. /* Might need to initialize the md5sum for each conffile */
  950. if (cf->value == NULL) {
  951. cf->value = file_sha256sum_alloc(root_filename);
  952. }
  953. if (!file_exists(root_filename)) {
  954. free(root_filename);
  955. continue;
  956. }
  957. cf_backup = backup_filename_alloc(root_filename);
  958. if (file_exists(cf_backup)) {
  959. /* Let's compute md5 to test if files are changed */
  960. if (cf->value && strlen(cf->value) > 33) {
  961. chksum = file_sha256sum_alloc(cf_backup);
  962. } else {
  963. chksum = file_md5sum_alloc(cf_backup);
  964. }
  965. if (chksum && cf->value
  966. && strcmp(cf->value, chksum) != 0) {
  967. if (conf->force_maintainer) {
  968. opkg_msg(NOTICE,
  969. "Conffile %s using maintainer's setting.\n",
  970. cf_backup);
  971. } else {
  972. char *new_conffile;
  973. sprintf_alloc(&new_conffile, "%s-opkg",
  974. root_filename);
  975. opkg_msg(ERROR,
  976. "Existing conffile %s "
  977. "is different from the conffile in the new package."
  978. " The new conffile will be placed at %s.\n",
  979. root_filename, new_conffile);
  980. rename(root_filename, new_conffile);
  981. rename(cf_backup, root_filename);
  982. free(new_conffile);
  983. }
  984. }
  985. unlink(cf_backup);
  986. if (chksum)
  987. free(chksum);
  988. }
  989. free(cf_backup);
  990. free(root_filename);
  991. }
  992. return 0;
  993. }
  994. int opkg_install_by_name(const char *pkg_name)
  995. {
  996. int cmp;
  997. pkg_t *old, *new;
  998. char *old_version, *new_version;
  999. old = pkg_hash_fetch_installed_by_name(pkg_name);
  1000. if (old)
  1001. opkg_msg(DEBUG2, "Old versions from pkg_hash_fetch %s.\n",
  1002. pkg_get_string(old, PKG_VERSION));
  1003. new = pkg_hash_fetch_best_installation_candidate_by_name(pkg_name);
  1004. if (new == NULL) {
  1005. opkg_msg(NOTICE, "Unknown package '%s'.\n", pkg_name);
  1006. return -1;
  1007. }
  1008. opkg_msg(DEBUG2, "Versions from pkg_hash_fetch:");
  1009. if (old)
  1010. opkg_message(DEBUG2, " old %s ", pkg_get_string(old, PKG_VERSION));
  1011. opkg_message(DEBUG2, " new %s\n", pkg_get_string(new, PKG_VERSION));
  1012. new->state_flag |= SF_USER;
  1013. if (old) {
  1014. old_version = pkg_version_str_alloc(old);
  1015. new_version = pkg_version_str_alloc(new);
  1016. cmp = pkg_compare_versions(old, new);
  1017. if ((conf->force_downgrade == 1) && (cmp > 0)) { /* We've been asked to allow downgrade and version is precedent */
  1018. opkg_msg(DEBUG, "Forcing downgrade\n");
  1019. cmp = -1; /* then we force opkg to downgrade */
  1020. /* We need to use a value < 0 because in the 0 case we are asking to */
  1021. /* reinstall, and some check could fail asking the "force-reinstall" option */
  1022. }
  1023. opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
  1024. "\n\t%s is installed "
  1025. "\n\t%s is available "
  1026. "\n\t%d was comparison result\n",
  1027. pkg_name, old_version, new_version, cmp);
  1028. if (cmp == 0) {
  1029. opkg_msg(NOTICE,
  1030. "Package %s (%s) installed in %s is up to date.\n",
  1031. old->name, old_version, old->dest->name);
  1032. free(old_version);
  1033. free(new_version);
  1034. return 0;
  1035. } else if (cmp > 0) {
  1036. opkg_msg(NOTICE,
  1037. "Not downgrading package %s on %s from %s to %s.\n",
  1038. old->name, old->dest->name, old_version,
  1039. new_version);
  1040. free(old_version);
  1041. free(new_version);
  1042. return 0;
  1043. } else if (cmp < 0) {
  1044. new->dest = old->dest;
  1045. old->state_want = SW_DEINSTALL;
  1046. }
  1047. free(old_version);
  1048. free(new_version);
  1049. }
  1050. opkg_msg(DEBUG2, "Calling opkg_install_pkg.\n");
  1051. return opkg_install_pkg(new, 0);
  1052. }
  1053. /**
  1054. * @brief Really install a pkg_t
  1055. */
  1056. int opkg_install_pkg(pkg_t * pkg, int from_upgrade)
  1057. {
  1058. int err = 0;
  1059. int message = 0;
  1060. pkg_t *old_pkg = NULL;
  1061. pkg_vec_t *replacees;
  1062. abstract_pkg_t *ab_pkg = NULL;
  1063. int old_state_flag;
  1064. sigset_t newset, oldset;
  1065. const char *local_filename;
  1066. time_t now;
  1067. if (from_upgrade)
  1068. message = 1; /* Coming from an upgrade, and should change the output message */
  1069. opkg_msg(DEBUG2, "Calling pkg_arch_supported.\n");
  1070. if (!pkg_arch_supported(pkg)) {
  1071. opkg_msg(ERROR,
  1072. "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
  1073. pkg_get_architecture(pkg), pkg->name);
  1074. return -1;
  1075. }
  1076. if (pkg->state_status == SS_INSTALLED && conf->nodeps == 0) {
  1077. err = satisfy_dependencies_for(pkg);
  1078. if (err)
  1079. return -1;
  1080. opkg_msg(NOTICE, "Package %s is already installed on %s.\n",
  1081. pkg->name, pkg->dest->name);
  1082. return 0;
  1083. }
  1084. if (pkg->dest == NULL) {
  1085. pkg->dest = conf->default_dest;
  1086. }
  1087. old_pkg = pkg_hash_fetch_installed_by_name(pkg->name);
  1088. err = opkg_install_check_downgrade(pkg, old_pkg, message);
  1089. if (err)
  1090. return -1;
  1091. pkg->state_want = SW_INSTALL;
  1092. if (old_pkg) {
  1093. old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependencies */
  1094. }
  1095. err = check_conflicts_for(pkg);
  1096. if (err)
  1097. return -1;
  1098. /* this setup is to remove the upgrade scenario in the end when
  1099. installing pkg A, A deps B & B deps on A. So both B and A are
  1100. installed. Then A's installation is started resulting in an
  1101. uncecessary upgrade */
  1102. if (pkg->state_status == SS_INSTALLED)
  1103. return 0;
  1104. err = verify_pkg_installable(pkg);
  1105. if (err)
  1106. return -1;
  1107. local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
  1108. if (local_filename == NULL) {
  1109. if (!conf->cache && conf->download_only) {
  1110. char cwd[4096];
  1111. if (getcwd(cwd, sizeof(cwd)) != NULL)
  1112. err = opkg_download_pkg(pkg, cwd);
  1113. else
  1114. return -1;
  1115. } else {
  1116. err = opkg_download_pkg(pkg, conf->tmp_dir);
  1117. }
  1118. if (err) {
  1119. opkg_msg(ERROR, "Failed to download %s. "
  1120. "Perhaps you need to run 'opkg update'?\n",
  1121. pkg->name);
  1122. return -1;
  1123. }
  1124. local_filename = pkg_get_string(pkg, PKG_LOCAL_FILENAME);
  1125. }
  1126. /* check that the repository is valid */
  1127. #if defined(HAVE_USIGN)
  1128. char *list_file_name, *sig_file_name, *lists_dir;
  1129. /* check to ensure the package has come from a repository */
  1130. if (conf->check_signature && pkg->src) {
  1131. sprintf_alloc(&lists_dir, "%s", (conf->restrict_to_default_dest)
  1132. ? conf->default_dest->lists_dir
  1133. : conf->lists_dir);
  1134. sprintf_alloc(&list_file_name, "%s/%s", lists_dir,
  1135. pkg->src->name);
  1136. sprintf_alloc(&sig_file_name, "%s/%s.sig", lists_dir,
  1137. pkg->src->name);
  1138. if (file_exists(sig_file_name)) {
  1139. if (opkg_verify_file(list_file_name, sig_file_name)) {
  1140. opkg_msg(ERROR,
  1141. "Failed to verify the signature of %s.\n",
  1142. list_file_name);
  1143. if (!conf->force_signature)
  1144. return -1;
  1145. }
  1146. } else {
  1147. opkg_msg(ERROR, "Signature file is missing for %s. "
  1148. "Perhaps you need to run 'opkg update'?\n",
  1149. pkg->name);
  1150. if (!conf->force_signature)
  1151. return -1;
  1152. }
  1153. free(lists_dir);
  1154. free(list_file_name);
  1155. free(sig_file_name);
  1156. }
  1157. #endif
  1158. /* Check integrity (size, checksums) */
  1159. err = opkg_verify_integrity(pkg, local_filename);
  1160. if (err && !conf->force_checksum) {
  1161. opkg_msg(ERROR, "Checksum or size mismatch for package %s. "
  1162. "Either the opkg or the package index are corrupt. "
  1163. "Try 'opkg update'.\n", pkg->name);
  1164. return -1;
  1165. }
  1166. if (err && conf->force_checksum) {
  1167. opkg_msg(NOTICE, "Ignored %s checksum or size mismatch.\n",
  1168. pkg->name);
  1169. }
  1170. if (conf->download_only) {
  1171. if (conf->nodeps == 0) {
  1172. err = satisfy_dependencies_for(pkg);
  1173. if (err)
  1174. return -1;
  1175. }
  1176. return 0;
  1177. }
  1178. if (!pkg_get_string(pkg, PKG_TMP_UNPACK_DIR)) {
  1179. if (unpack_pkg_control_files(pkg) == -1) {
  1180. opkg_msg(ERROR,
  1181. "Failed to unpack control files from %s.\n",
  1182. local_filename);
  1183. return -1;
  1184. }
  1185. }
  1186. err = update_file_ownership(pkg, old_pkg);
  1187. if (err)
  1188. return -1;
  1189. if (conf->nodeps == 0) {
  1190. err = satisfy_dependencies_for(pkg);
  1191. if (err)
  1192. return -1;
  1193. if (pkg->state_status == SS_UNPACKED)
  1194. /* Circular dependency has installed it for us. */
  1195. return 0;
  1196. }
  1197. replacees = pkg_vec_alloc();
  1198. pkg_get_installed_replacees(pkg, replacees);
  1199. /* this next section we do with SIGINT blocked to prevent inconsistency between opkg database and filesystem */
  1200. sigemptyset(&newset);
  1201. sigaddset(&newset, SIGINT);
  1202. sigprocmask(SIG_BLOCK, &newset, &oldset);
  1203. opkg_state_changed++;
  1204. pkg->state_flag |= SF_FILELIST_CHANGED;
  1205. if (old_pkg) {
  1206. pkg_remove_orphan_dependent(pkg, old_pkg);
  1207. old_pkg->is_upgrade = 1;
  1208. pkg->is_upgrade = 1;
  1209. }
  1210. /* XXX: BUG: we really should treat replacement more like an upgrade
  1211. * Instead, we're going to remove the replacees
  1212. */
  1213. err = pkg_remove_installed_replacees(replacees);
  1214. if (err)
  1215. goto UNWIND_REMOVE_INSTALLED_REPLACEES;
  1216. err = prerm_upgrade_old_pkg(pkg, old_pkg);
  1217. if (err)
  1218. goto UNWIND_PRERM_UPGRADE_OLD_PKG;
  1219. err = prerm_deconfigure_conflictors(pkg, replacees);
  1220. if (err)
  1221. goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
  1222. err = preinst_configure(pkg, old_pkg);
  1223. if (err)
  1224. goto UNWIND_PREINST_CONFIGURE;
  1225. err = backup_modified_conffiles(pkg, old_pkg);
  1226. if (err)
  1227. goto UNWIND_BACKUP_MODIFIED_CONFFILES;
  1228. err = check_data_file_clashes(pkg, old_pkg);
  1229. if (err)
  1230. goto UNWIND_CHECK_DATA_FILE_CLASHES;
  1231. err = postrm_upgrade_old_pkg(pkg, old_pkg);
  1232. if (err)
  1233. goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
  1234. if (conf->noaction)
  1235. return 0;
  1236. /* point of no return: no unwinding after this */
  1237. if (old_pkg) {
  1238. old_pkg->state_want = SW_DEINSTALL;
  1239. if (old_pkg->state_flag & SF_NOPRUNE) {
  1240. opkg_msg(INFO, "Not removing obsolesced files because "
  1241. "package %s marked noprune.\n", old_pkg->name);
  1242. } else {
  1243. opkg_msg(INFO, "Removing obsolesced files for %s\n",
  1244. old_pkg->name);
  1245. if (remove_obsolesced_files(pkg, old_pkg)) {
  1246. opkg_msg(ERROR, "Failed to determine "
  1247. "obsolete files from previously "
  1248. "installed %s\n", old_pkg->name);
  1249. }
  1250. }
  1251. /* removing files from old package, to avoid ghost files */
  1252. remove_data_files_and_list(old_pkg);
  1253. remove_maintainer_scripts(old_pkg);
  1254. }
  1255. opkg_msg(INFO, "%s maintainer scripts.\n",
  1256. (pkg->is_upgrade) ? ("Upgrading") : ("Installing"));
  1257. if (install_maintainer_scripts(pkg, old_pkg)) {
  1258. opkg_msg(ERROR, "Failed to extract maintainer scripts for %s."
  1259. " Package debris may remain!\n", pkg->name);
  1260. goto pkg_is_hosed;
  1261. }
  1262. /* the following just returns 0 */
  1263. remove_disappeared(pkg);
  1264. opkg_msg(INFO, "Installing data files for %s.\n", pkg->name);
  1265. if (install_data_files(pkg)) {
  1266. opkg_msg(ERROR, "Failed to extract data files for %s. "
  1267. "Package debris may remain!\n", pkg->name);
  1268. goto pkg_is_hosed;
  1269. }
  1270. err = check_data_file_clashes_change(pkg, old_pkg);
  1271. if (err) {
  1272. opkg_msg(ERROR, "check_data_file_clashes_change() failed for "
  1273. "for files belonging to %s.\n", pkg->name);
  1274. }
  1275. opkg_msg(INFO, "Resolving conf files for %s\n", pkg->name);
  1276. resolve_conffiles(pkg);
  1277. pkg->state_status = SS_UNPACKED;
  1278. old_state_flag = pkg->state_flag;
  1279. pkg->state_flag &= ~SF_PREFER;
  1280. opkg_msg(DEBUG, "pkg=%s old_state_flag=%x state_flag=%x\n",
  1281. pkg->name, old_state_flag, pkg->state_flag);
  1282. if (old_pkg)
  1283. old_pkg->state_status = SS_NOT_INSTALLED;
  1284. now = time(NULL);
  1285. pkg_set_int(pkg, PKG_INSTALLED_TIME, now);
  1286. ab_pkg = pkg->parent;
  1287. if (ab_pkg)
  1288. ab_pkg->state_status = pkg->state_status;
  1289. sigprocmask(SIG_UNBLOCK, &newset, &oldset);
  1290. pkg_vec_free(replacees);
  1291. return 0;
  1292. UNWIND_POSTRM_UPGRADE_OLD_PKG:
  1293. postrm_upgrade_old_pkg_unwind(pkg, old_pkg);
  1294. UNWIND_CHECK_DATA_FILE_CLASHES:
  1295. check_data_file_clashes_unwind(pkg, old_pkg);
  1296. UNWIND_BACKUP_MODIFIED_CONFFILES:
  1297. backup_modified_conffiles_unwind(pkg, old_pkg);
  1298. UNWIND_PREINST_CONFIGURE:
  1299. preinst_configure_unwind(pkg, old_pkg);
  1300. UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
  1301. prerm_deconfigure_conflictors_unwind(pkg, replacees);
  1302. UNWIND_PRERM_UPGRADE_OLD_PKG:
  1303. prerm_upgrade_old_pkg_unwind(pkg, old_pkg);
  1304. UNWIND_REMOVE_INSTALLED_REPLACEES:
  1305. pkg_remove_installed_replacees_unwind(replacees);
  1306. pkg_is_hosed:
  1307. sigprocmask(SIG_UNBLOCK, &newset, &oldset);
  1308. pkg_vec_free(replacees);
  1309. return -1;
  1310. }