opkg_upgrade.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* opkg_upgrade.c - the opkg package management system
  2. Carl D. Worth
  3. Copyright (C) 2001 University of Southern California
  4. Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2, or (at
  8. your option) any later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "opkg_install.h"
  17. #include "opkg_upgrade.h"
  18. #include "opkg_message.h"
  19. int opkg_upgrade_pkg(pkg_t * old)
  20. {
  21. pkg_t *new;
  22. int cmp;
  23. char *old_version, *new_version;
  24. if (old->state_flag & SF_HOLD) {
  25. opkg_msg(NOTICE, "Not upgrading package %s which is marked "
  26. "hold (flags=%#x).\n", old->name, old->state_flag);
  27. return 0;
  28. }
  29. new = pkg_hash_fetch_best_installation_candidate_by_name(old->name);
  30. if (new == NULL) {
  31. old_version = pkg_version_str_alloc(old);
  32. opkg_msg(NOTICE, "Assuming locally installed package %s (%s) "
  33. "is up to date.\n", old->name, old_version);
  34. free(old_version);
  35. return 0;
  36. }
  37. old_version = pkg_version_str_alloc(old);
  38. new_version = pkg_version_str_alloc(new);
  39. cmp = pkg_compare_versions(old, new);
  40. opkg_msg(DEBUG, "Comparing visible versions of pkg %s:"
  41. "\n\t%s is installed "
  42. "\n\t%s is available "
  43. "\n\t%d was comparison result\n",
  44. old->name, old_version, new_version, cmp);
  45. if (cmp == 0) {
  46. opkg_msg(INFO,
  47. "Package %s (%s) installed in %s is up to date.\n",
  48. old->name, old_version, old->dest->name);
  49. free(old_version);
  50. free(new_version);
  51. return 0;
  52. } else if (cmp > 0) {
  53. opkg_msg(NOTICE,
  54. "Not downgrading package %s on %s from %s to %s.\n",
  55. old->name, old->dest->name, old_version, new_version);
  56. free(old_version);
  57. free(new_version);
  58. return 0;
  59. } else if (cmp < 0) {
  60. new->dest = old->dest;
  61. old->state_want = SW_DEINSTALL;
  62. }
  63. free(old_version);
  64. free(new_version);
  65. new->state_flag |= SF_USER;
  66. return opkg_install_pkg(new, 1);
  67. }
  68. static void
  69. pkg_hash_check_installed_pkg_helper(const char *pkg_name, void *entry,
  70. void *data)
  71. {
  72. struct active_list *item, *head = (struct active_list *)data;
  73. abstract_pkg_t *ab_pkg = (abstract_pkg_t *) entry;
  74. pkg_vec_t *pkg_vec = ab_pkg->pkgs;
  75. int j;
  76. if (!pkg_vec)
  77. return;
  78. for (j = 0; j < pkg_vec->len; j++) {
  79. pkg_t *pkg = pkg_vec->pkgs[j];
  80. if (pkg->state_status == SS_INSTALLED
  81. || pkg->state_status == SS_UNPACKED) {
  82. printf("alloc item for pkg=%p\n", pkg);
  83. item = active_list_head_new();
  84. item->pkg = pkg;
  85. active_list_add(head, item);
  86. }
  87. }
  88. }
  89. struct active_list *prepare_upgrade_list(void)
  90. {
  91. struct active_list *head = active_list_head_new();
  92. struct active_list *all = active_list_head_new();
  93. struct active_list *node = NULL;
  94. /* ensure all data is valid */
  95. pkg_info_preinstall_check();
  96. hash_table_foreach(&conf->pkg_hash, pkg_hash_check_installed_pkg_helper,
  97. all);
  98. for (node = active_list_next(all, all); node;
  99. node = active_list_next(all, node)) {
  100. pkg_t *old, *new;
  101. int cmp;
  102. old = node->pkg;
  103. new =
  104. pkg_hash_fetch_best_installation_candidate_by_name(old->
  105. name);
  106. if (new == NULL)
  107. continue;
  108. cmp = pkg_compare_versions(old, new);
  109. if (cmp < 0) {
  110. node = active_list_move_node(all, head, node);
  111. }
  112. }
  113. active_list_head_delete(all);
  114. return head;
  115. }