Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini dpkg implementation for busybox. |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 3 | * This is not meant as a replacement for dpkg |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 4 | * |
Glenn L McGrath | 3563654 | 2001-10-03 03:10:35 +0000 | [diff] [blame] | 5 | * Written By Glenn McGrath with the help of others |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 6 | * Copyright (C) 2001 by Glenn McGrath |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 7 | * |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 8 | * Started life as a busybox implementation of udpkg |
| 9 | * |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 11 | */ |
Glenn L McGrath | 649968c | 2001-02-10 14:26:48 +0000 | [diff] [blame] | 12 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 13 | /* |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 14 | * Known difference between busybox dpkg and the official dpkg that i don't |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 15 | * consider important, its worth keeping a note of differences anyway, just to |
| 16 | * make it easier to maintain. |
| 17 | * - The first value for the Confflile: field isnt placed on a new line. |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 18 | * - When installing a package the Status: field is placed at the end of the |
| 19 | * section, rather than just after the Package: field. |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 20 | * |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 21 | * Bugs that need to be fixed |
| 22 | * - (unknown, please let me know when you find any) |
| 23 | * |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 24 | */ |
| 25 | |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 26 | #include <fcntl.h> |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 27 | #include <getopt.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 31 | #include "unarchive.h" |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
| 33 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 34 | /* NOTE: If you vary HASH_PRIME sizes be aware, |
| 35 | * 1) Tweaking these will have a big effect on how much memory this program uses. |
| 36 | * 2) For computational efficiency these hash tables should be at least 20% |
| 37 | * larger than the maximum number of elements stored in it. |
| 38 | * 3) All _HASH_PRIME's must be a prime number or chaos is assured, if your looking |
| 39 | * for a prime, try http://www.utm.edu/research/primes/lists/small/10000.txt |
| 40 | * 4) If you go bigger than 15 bits you may get into trouble (untested) as its |
| 41 | * sometimes cast to an unsigned int, if you go to 16 bit you will overlap |
| 42 | * int's and chaos is assured, 16381 is the max prime for 14 bit field |
| 43 | */ |
| 44 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 45 | /* NAME_HASH_PRIME, Stores package names and versions, |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 46 | * I estimate it should be at least 50% bigger than PACKAGE_HASH_PRIME, |
| 47 | * as there a lot of duplicate version numbers */ |
| 48 | #define NAME_HASH_PRIME 16381 |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 49 | static char *name_hashtable[NAME_HASH_PRIME + 1]; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 50 | |
| 51 | /* PACKAGE_HASH_PRIME, Maximum number of unique packages, |
| 52 | * It must not be smaller than STATUS_HASH_PRIME, |
| 53 | * Currently only packages from status_hashtable are stored in here, but in |
| 54 | * future this may be used to store packages not only from a status file, |
| 55 | * but an available_hashtable, and even multiple packages files. |
| 56 | * Package can be stored more than once if they have different versions. |
| 57 | * e.g. The same package may have different versions in the status file |
| 58 | * and available file */ |
| 59 | #define PACKAGE_HASH_PRIME 10007 |
| 60 | typedef struct edge_s { |
| 61 | unsigned int operator:3; |
| 62 | unsigned int type:4; |
| 63 | unsigned int name:14; |
| 64 | unsigned int version:14; |
| 65 | } edge_t; |
| 66 | |
| 67 | typedef struct common_node_s { |
| 68 | unsigned int name:14; |
| 69 | unsigned int version:14; |
| 70 | unsigned int num_of_edges:14; |
| 71 | edge_t **edge; |
| 72 | } common_node_t; |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 73 | static common_node_t *package_hashtable[PACKAGE_HASH_PRIME + 1]; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 74 | |
| 75 | /* Currently it doesnt store packages that have state-status of not-installed |
| 76 | * So it only really has to be the size of the maximum number of packages |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 77 | * likely to be installed at any one time, so there is a bit of leeway here */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 78 | #define STATUS_HASH_PRIME 8191 |
| 79 | typedef struct status_node_s { |
| 80 | unsigned int package:14; /* has to fit PACKAGE_HASH_PRIME */ |
| 81 | unsigned int status:14; /* has to fit STATUS_HASH_PRIME */ |
| 82 | } status_node_t; |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 83 | static status_node_t *status_hashtable[STATUS_HASH_PRIME + 1]; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 84 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 85 | /* Even numbers are for 'extras', like ored dependencies or null */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 86 | enum edge_type_e { |
| 87 | EDGE_NULL = 0, |
| 88 | EDGE_PRE_DEPENDS = 1, |
| 89 | EDGE_OR_PRE_DEPENDS = 2, |
| 90 | EDGE_DEPENDS = 3, |
| 91 | EDGE_OR_DEPENDS = 4, |
| 92 | EDGE_REPLACES = 5, |
| 93 | EDGE_PROVIDES = 7, |
| 94 | EDGE_CONFLICTS = 9, |
| 95 | EDGE_SUGGESTS = 11, |
| 96 | EDGE_RECOMMENDS = 13, |
| 97 | EDGE_ENHANCES = 15 |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 98 | }; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 99 | enum operator_e { |
| 100 | VER_NULL = 0, |
| 101 | VER_EQUAL = 1, |
| 102 | VER_LESS = 2, |
| 103 | VER_LESS_EQUAL = 3, |
| 104 | VER_MORE = 4, |
| 105 | VER_MORE_EQUAL = 5, |
| 106 | VER_ANY = 6 |
| 107 | }; |
| 108 | |
| 109 | enum dpkg_opt_e { |
| 110 | dpkg_opt_purge = 1, |
| 111 | dpkg_opt_remove = 2, |
| 112 | dpkg_opt_unpack = 4, |
| 113 | dpkg_opt_configure = 8, |
| 114 | dpkg_opt_install = 16, |
| 115 | dpkg_opt_package_name = 32, |
| 116 | dpkg_opt_filename = 64, |
| 117 | dpkg_opt_list_installed = 128, |
| 118 | dpkg_opt_force_ignore_depends = 256 |
| 119 | }; |
| 120 | |
| 121 | typedef struct deb_file_s { |
| 122 | char *control_file; |
| 123 | char *filename; |
| 124 | unsigned int package:14; |
| 125 | } deb_file_t; |
| 126 | |
| 127 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 128 | static void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 129 | { |
| 130 | unsigned long int hash_num = key[0]; |
| 131 | int len = strlen(key); |
| 132 | int i; |
| 133 | |
| 134 | /* Maybe i should have uses a "proper" hashing algorithm here instead |
| 135 | * of making one up myself, seems to be working ok though. */ |
| 136 | for(i = 1; i < len; i++) { |
| 137 | /* shifts the ascii based value and adds it to previous value |
| 138 | * shift amount is mod 24 because long int is 32 bit and data |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 139 | * to be shifted is 8, don't want to shift data to where it has |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 140 | * no effect*/ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 141 | hash_num += ((key[i] + key[i-1]) << ((key[i] * i) % 24)); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 142 | } |
| 143 | *start = (unsigned int) hash_num % hash_prime; |
| 144 | *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1)); |
| 145 | } |
| 146 | |
| 147 | /* this adds the key to the hash table */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 148 | static int search_name_hashtable(const char *key) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 149 | { |
| 150 | unsigned int probe_address = 0; |
| 151 | unsigned int probe_decrement = 0; |
| 152 | // char *temp; |
| 153 | |
| 154 | make_hash(key, &probe_address, &probe_decrement, NAME_HASH_PRIME); |
| 155 | while(name_hashtable[probe_address] != NULL) { |
| 156 | if (strcmp(name_hashtable[probe_address], key) == 0) { |
| 157 | return(probe_address); |
| 158 | } else { |
| 159 | probe_address -= probe_decrement; |
| 160 | if ((int)probe_address < 0) { |
| 161 | probe_address += NAME_HASH_PRIME; |
| 162 | } |
| 163 | } |
| 164 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | name_hashtable[probe_address] = bb_xstrdup(key); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 166 | return(probe_address); |
| 167 | } |
| 168 | |
| 169 | /* this DOESNT add the key to the hashtable |
| 170 | * TODO make it consistent with search_name_hashtable |
| 171 | */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 172 | static unsigned int search_status_hashtable(const char *key) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 173 | { |
| 174 | unsigned int probe_address = 0; |
| 175 | unsigned int probe_decrement = 0; |
| 176 | |
| 177 | make_hash(key, &probe_address, &probe_decrement, STATUS_HASH_PRIME); |
| 178 | while(status_hashtable[probe_address] != NULL) { |
| 179 | if (strcmp(key, name_hashtable[package_hashtable[status_hashtable[probe_address]->package]->name]) == 0) { |
| 180 | break; |
| 181 | } else { |
| 182 | probe_address -= probe_decrement; |
| 183 | if ((int)probe_address < 0) { |
| 184 | probe_address += STATUS_HASH_PRIME; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | return(probe_address); |
| 189 | } |
| 190 | |
| 191 | /* Need to rethink version comparison, maybe the official dpkg has something i can use ? */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 192 | static int version_compare_part(const char *version1, const char *version2) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 193 | { |
| 194 | int upstream_len1 = 0; |
| 195 | int upstream_len2 = 0; |
| 196 | char *name1_char; |
| 197 | char *name2_char; |
| 198 | int len1 = 0; |
| 199 | int len2 = 0; |
| 200 | int tmp_int; |
| 201 | int ver_num1; |
| 202 | int ver_num2; |
| 203 | int ret; |
| 204 | |
| 205 | if (version1 == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 206 | version1 = bb_xstrdup(""); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 207 | } |
| 208 | if (version2 == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 209 | version2 = bb_xstrdup(""); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 210 | } |
| 211 | upstream_len1 = strlen(version1); |
| 212 | upstream_len2 = strlen(version2); |
| 213 | |
| 214 | while ((len1 < upstream_len1) || (len2 < upstream_len2)) { |
| 215 | /* Compare non-digit section */ |
| 216 | tmp_int = strcspn(&version1[len1], "0123456789"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 217 | name1_char = bb_xstrndup(&version1[len1], tmp_int); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 218 | len1 += tmp_int; |
| 219 | tmp_int = strcspn(&version2[len2], "0123456789"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 220 | name2_char = bb_xstrndup(&version2[len2], tmp_int); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 221 | len2 += tmp_int; |
| 222 | tmp_int = strcmp(name1_char, name2_char); |
| 223 | free(name1_char); |
| 224 | free(name2_char); |
| 225 | if (tmp_int != 0) { |
| 226 | ret = tmp_int; |
| 227 | goto cleanup_version_compare_part; |
| 228 | } |
| 229 | |
| 230 | /* Compare digits */ |
| 231 | tmp_int = strspn(&version1[len1], "0123456789"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 232 | name1_char = bb_xstrndup(&version1[len1], tmp_int); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 233 | len1 += tmp_int; |
| 234 | tmp_int = strspn(&version2[len2], "0123456789"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 235 | name2_char = bb_xstrndup(&version2[len2], tmp_int); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 236 | len2 += tmp_int; |
| 237 | ver_num1 = atoi(name1_char); |
| 238 | ver_num2 = atoi(name2_char); |
| 239 | free(name1_char); |
| 240 | free(name2_char); |
| 241 | if (ver_num1 < ver_num2) { |
| 242 | ret = -1; |
| 243 | goto cleanup_version_compare_part; |
| 244 | } |
| 245 | else if (ver_num1 > ver_num2) { |
| 246 | ret = 1; |
| 247 | goto cleanup_version_compare_part; |
| 248 | } |
| 249 | } |
| 250 | ret = 0; |
| 251 | cleanup_version_compare_part: |
| 252 | return(ret); |
| 253 | } |
| 254 | |
| 255 | /* if ver1 < ver2 return -1, |
| 256 | * if ver1 = ver2 return 0, |
| 257 | * if ver1 > ver2 return 1, |
| 258 | */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 259 | static int version_compare(const unsigned int ver1, const unsigned int ver2) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 260 | { |
| 261 | char *ch_ver1 = name_hashtable[ver1]; |
| 262 | char *ch_ver2 = name_hashtable[ver2]; |
| 263 | |
| 264 | char epoch1, epoch2; |
| 265 | char *deb_ver1, *deb_ver2; |
| 266 | char *ver1_ptr, *ver2_ptr; |
| 267 | char *upstream_ver1; |
| 268 | char *upstream_ver2; |
| 269 | int result; |
| 270 | |
| 271 | /* Compare epoch */ |
| 272 | if (ch_ver1[1] == ':') { |
| 273 | epoch1 = ch_ver1[0]; |
| 274 | ver1_ptr = strchr(ch_ver1, ':') + 1; |
| 275 | } else { |
| 276 | epoch1 = '0'; |
| 277 | ver1_ptr = ch_ver1; |
| 278 | } |
| 279 | if (ch_ver2[1] == ':') { |
| 280 | epoch2 = ch_ver2[0]; |
| 281 | ver2_ptr = strchr(ch_ver2, ':') + 1; |
| 282 | } else { |
| 283 | epoch2 = '0'; |
| 284 | ver2_ptr = ch_ver2; |
| 285 | } |
| 286 | if (epoch1 < epoch2) { |
| 287 | return(-1); |
| 288 | } |
| 289 | else if (epoch1 > epoch2) { |
| 290 | return(1); |
| 291 | } |
| 292 | |
| 293 | /* Compare upstream version */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 294 | upstream_ver1 = bb_xstrdup(ver1_ptr); |
| 295 | upstream_ver2 = bb_xstrdup(ver2_ptr); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 296 | |
| 297 | /* Chop off debian version, and store for later use */ |
| 298 | deb_ver1 = strrchr(upstream_ver1, '-'); |
| 299 | deb_ver2 = strrchr(upstream_ver2, '-'); |
| 300 | if (deb_ver1) { |
| 301 | deb_ver1[0] = '\0'; |
| 302 | deb_ver1++; |
| 303 | } |
| 304 | if (deb_ver2) { |
| 305 | deb_ver2[0] = '\0'; |
| 306 | deb_ver2++; |
| 307 | } |
| 308 | result = version_compare_part(upstream_ver1, upstream_ver2); |
| 309 | |
| 310 | free(upstream_ver1); |
| 311 | free(upstream_ver2); |
| 312 | |
| 313 | if (result != 0) { |
| 314 | return(result); |
| 315 | } |
| 316 | |
| 317 | /* Compare debian versions */ |
| 318 | return(version_compare_part(deb_ver1, deb_ver2)); |
| 319 | } |
| 320 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 321 | static int test_version(const unsigned int version1, const unsigned int version2, const unsigned int operator) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 322 | { |
| 323 | const int version_result = version_compare(version1, version2); |
| 324 | switch(operator) { |
| 325 | case (VER_ANY): |
| 326 | return(TRUE); |
| 327 | case (VER_EQUAL): |
| 328 | if (version_result == 0) { |
| 329 | return(TRUE); |
| 330 | } |
| 331 | break; |
| 332 | case (VER_LESS): |
| 333 | if (version_result < 0) { |
| 334 | return(TRUE); |
| 335 | } |
| 336 | break; |
| 337 | case (VER_LESS_EQUAL): |
| 338 | if (version_result <= 0) { |
| 339 | return(TRUE); |
| 340 | } |
| 341 | break; |
| 342 | case (VER_MORE): |
| 343 | if (version_result > 0) { |
| 344 | return(TRUE); |
| 345 | } |
| 346 | break; |
| 347 | case (VER_MORE_EQUAL): |
| 348 | if (version_result >= 0) { |
| 349 | return(TRUE); |
| 350 | } |
| 351 | break; |
| 352 | } |
| 353 | return(FALSE); |
| 354 | } |
| 355 | |
| 356 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 357 | static int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 358 | { |
| 359 | unsigned int probe_address = 0; |
| 360 | unsigned int probe_decrement = 0; |
| 361 | |
| 362 | make_hash(name_hashtable[name], &probe_address, &probe_decrement, PACKAGE_HASH_PRIME); |
| 363 | while(package_hashtable[probe_address] != NULL) { |
| 364 | if (package_hashtable[probe_address]->name == name) { |
| 365 | if (operator == VER_ANY) { |
| 366 | return(probe_address); |
| 367 | } |
| 368 | if (test_version(package_hashtable[probe_address]->version, version, operator)) { |
| 369 | return(probe_address); |
| 370 | } |
| 371 | } |
| 372 | probe_address -= probe_decrement; |
| 373 | if ((int)probe_address < 0) { |
| 374 | probe_address += PACKAGE_HASH_PRIME; |
| 375 | } |
| 376 | } |
| 377 | return(probe_address); |
| 378 | } |
| 379 | |
| 380 | /* |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 381 | * This function searches through the entire package_hashtable looking |
| 382 | * for a package which provides "needle". It returns the index into |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 383 | * the package_hashtable for the providing package. |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 384 | * |
| 385 | * needle is the index into name_hashtable of the package we are |
| 386 | * looking for. |
| 387 | * |
| 388 | * start_at is the index in the package_hashtable to start looking |
| 389 | * at. If start_at is -1 then start at the beginning. This is to allow |
| 390 | * for repeated searches since more than one package might provide |
| 391 | * needle. |
| 392 | * |
| 393 | * FIXME: I don't think this is very efficient, but I thought I'd keep |
| 394 | * it simple for now until it proves to be a problem. |
| 395 | */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 396 | static int search_for_provides(int needle, int start_at) { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 397 | int i, j; |
| 398 | common_node_t *p; |
| 399 | for (i = start_at + 1; i < PACKAGE_HASH_PRIME; i++) { |
| 400 | p = package_hashtable[i]; |
| 401 | if ( p == NULL ) continue; |
| 402 | for(j = 0; j < p->num_of_edges; j++) |
| 403 | if ( p->edge[j]->type == EDGE_PROVIDES && p->edge[j]->name == needle ) |
| 404 | return i; |
| 405 | } |
| 406 | return -1; |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Add an edge to a node |
| 411 | */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 412 | static void add_edge_to_node(common_node_t *node, edge_t *edge) |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 413 | { |
| 414 | node->num_of_edges++; |
| 415 | node->edge = xrealloc(node->edge, sizeof(edge_t) * (node->num_of_edges + 1)); |
| 416 | node->edge[node->num_of_edges - 1] = edge; |
| 417 | } |
| 418 | |
| 419 | /* |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 420 | * Create one new node and one new edge for every dependency. |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 421 | * |
| 422 | * Dependencies which contain multiple alternatives are represented as |
| 423 | * an EDGE_OR_PRE_DEPENDS or EDGE_OR_DEPENDS node, followed by a |
| 424 | * number of EDGE_PRE_DEPENDS or EDGE_DEPENDS nodes. The name field of |
| 425 | * the OR edge contains the full dependency string while the version |
| 426 | * field contains the number of EDGE nodes which follow as part of |
| 427 | * this alternative. |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 428 | */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 429 | static void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 430 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 431 | char *line = bb_xstrdup(whole_line); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 432 | char *line2; |
| 433 | char *line_ptr1 = NULL; |
| 434 | char *line_ptr2 = NULL; |
| 435 | char *field; |
| 436 | char *field2; |
| 437 | char *version; |
| 438 | edge_t *edge; |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 439 | edge_t *or_edge; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 440 | int offset_ch; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 441 | |
| 442 | field = strtok_r(line, ",", &line_ptr1); |
| 443 | do { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 444 | /* skip leading spaces */ |
| 445 | field += strspn(field, " "); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 446 | line2 = bb_xstrdup(field); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 447 | field2 = strtok_r(line2, "|", &line_ptr2); |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 448 | if ( (edge_type == EDGE_DEPENDS || edge_type == EDGE_PRE_DEPENDS) && |
| 449 | (strcmp(field, field2) != 0)) { |
| 450 | or_edge = (edge_t *)xmalloc(sizeof(edge_t)); |
| 451 | or_edge->type = edge_type + 1; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 452 | } else { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 453 | or_edge = NULL; |
| 454 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 455 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 456 | if ( or_edge ) { |
| 457 | or_edge->name = search_name_hashtable(field); |
| 458 | or_edge->version = 0; // tracks the number of altenatives |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 459 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 460 | add_edge_to_node(parent_node, or_edge); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | do { |
| 464 | edge = (edge_t *) xmalloc(sizeof(edge_t)); |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 465 | edge->type = edge_type; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 466 | |
| 467 | /* Skip any extra leading spaces */ |
| 468 | field2 += strspn(field2, " "); |
| 469 | |
| 470 | /* Get dependency version info */ |
| 471 | version = strchr(field2, '('); |
| 472 | if (version == NULL) { |
| 473 | edge->operator = VER_ANY; |
| 474 | /* Get the versions hash number, adding it if the number isnt already in there */ |
| 475 | edge->version = search_name_hashtable("ANY"); |
| 476 | } else { |
| 477 | /* Skip leading ' ' or '(' */ |
| 478 | version += strspn(field2, " "); |
| 479 | version += strspn(version, "("); |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 480 | /* Calculate length of any operator characters */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 481 | offset_ch = strspn(version, "<=>"); |
| 482 | /* Determine operator */ |
| 483 | if (offset_ch > 0) { |
| 484 | if (strncmp(version, "=", offset_ch) == 0) { |
| 485 | edge->operator = VER_EQUAL; |
| 486 | } |
| 487 | else if (strncmp(version, "<<", offset_ch) == 0) { |
| 488 | edge->operator = VER_LESS; |
| 489 | } |
| 490 | else if (strncmp(version, "<=", offset_ch) == 0) { |
| 491 | edge->operator = VER_LESS_EQUAL; |
| 492 | } |
| 493 | else if (strncmp(version, ">>", offset_ch) == 0) { |
| 494 | edge->operator = VER_MORE; |
| 495 | } |
| 496 | else if (strncmp(version, ">=", offset_ch) == 0) { |
| 497 | edge->operator = VER_MORE_EQUAL; |
| 498 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 499 | bb_error_msg_and_die("Illegal operator\n"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | /* skip to start of version numbers */ |
| 503 | version += offset_ch; |
| 504 | version += strspn(version, " "); |
| 505 | |
| 506 | /* Truncate version at trailing ' ' or ')' */ |
| 507 | version[strcspn(version, " )")] = '\0'; |
| 508 | /* Get the versions hash number, adding it if the number isnt already in there */ |
| 509 | edge->version = search_name_hashtable(version); |
| 510 | } |
| 511 | |
| 512 | /* Get the dependency name */ |
| 513 | field2[strcspn(field2, " (")] = '\0'; |
| 514 | edge->name = search_name_hashtable(field2); |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 515 | |
| 516 | if ( or_edge ) |
| 517 | or_edge->version++; |
| 518 | |
| 519 | add_edge_to_node(parent_node, edge); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 520 | } while ((field2 = strtok_r(NULL, "|", &line_ptr2)) != NULL); |
| 521 | free(line2); |
| 522 | } while ((field = strtok_r(NULL, ",", &line_ptr1)) != NULL); |
| 523 | free(line); |
| 524 | |
| 525 | return; |
| 526 | } |
| 527 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 528 | static void free_package(common_node_t *node) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 529 | { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 530 | unsigned short i; |
| 531 | if (node) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 532 | for (i = 0; i < node->num_of_edges; i++) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 533 | free(node->edge[i]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 534 | } |
Rob Landley | e7c43b6 | 2006-03-01 16:39:45 +0000 | [diff] [blame] | 535 | free(node->edge); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 536 | free(node); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 540 | static unsigned int fill_package_struct(char *control_buffer) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 541 | { |
Rob Landley | 0a7c8ef | 2006-02-22 17:01:00 +0000 | [diff] [blame] | 542 | static const char *const field_names[] = { "Package", "Version", |
| 543 | "Pre-Depends", "Depends","Replaces", "Provides", |
| 544 | "Conflicts", "Suggests", "Recommends", "Enhances", 0 |
| 545 | }; |
| 546 | |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 547 | common_node_t *new_node = (common_node_t *) xzalloc(sizeof(common_node_t)); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 548 | char *field_name; |
| 549 | char *field_value; |
| 550 | int field_start = 0; |
| 551 | int num = -1; |
| 552 | int buffer_length = strlen(control_buffer); |
| 553 | |
| 554 | new_node->version = search_name_hashtable("unknown"); |
| 555 | while (field_start < buffer_length) { |
Glenn L McGrath | 62d2882 | 2002-11-06 23:35:28 +0000 | [diff] [blame] | 556 | unsigned short field_num; |
| 557 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 558 | field_start += read_package_field(&control_buffer[field_start], |
| 559 | &field_name, &field_value); |
| 560 | |
| 561 | if (field_name == NULL) { |
Glenn L McGrath | 62d2882 | 2002-11-06 23:35:28 +0000 | [diff] [blame] | 562 | goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement ! */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 565 | field_num = compare_string_array(field_names, field_name); |
Glenn L McGrath | 62d2882 | 2002-11-06 23:35:28 +0000 | [diff] [blame] | 566 | switch(field_num) { |
| 567 | case 0: /* Package */ |
| 568 | new_node->name = search_name_hashtable(field_value); |
| 569 | break; |
| 570 | case 1: /* Version */ |
| 571 | new_node->version = search_name_hashtable(field_value); |
| 572 | break; |
| 573 | case 2: /* Pre-Depends */ |
| 574 | add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS); |
| 575 | break; |
| 576 | case 3: /* Depends */ |
| 577 | add_split_dependencies(new_node, field_value, EDGE_DEPENDS); |
| 578 | break; |
| 579 | case 4: /* Replaces */ |
| 580 | add_split_dependencies(new_node, field_value, EDGE_REPLACES); |
| 581 | break; |
| 582 | case 5: /* Provides */ |
| 583 | add_split_dependencies(new_node, field_value, EDGE_PROVIDES); |
| 584 | break; |
| 585 | case 6: /* Conflicts */ |
| 586 | add_split_dependencies(new_node, field_value, EDGE_CONFLICTS); |
| 587 | break; |
| 588 | case 7: /* Suggests */ |
| 589 | add_split_dependencies(new_node, field_value, EDGE_SUGGESTS); |
| 590 | break; |
| 591 | case 8: /* Recommends */ |
| 592 | add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS); |
| 593 | break; |
| 594 | case 9: /* Enhances */ |
| 595 | add_split_dependencies(new_node, field_value, EDGE_ENHANCES); |
| 596 | break; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 597 | } |
| 598 | fill_package_struct_cleanup: |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 599 | free(field_name); |
| 600 | free(field_value); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | if (new_node->version == search_name_hashtable("unknown")) { |
| 604 | free_package(new_node); |
| 605 | return(-1); |
| 606 | } |
| 607 | num = search_package_hashtable(new_node->name, new_node->version, VER_EQUAL); |
| 608 | if (package_hashtable[num] == NULL) { |
| 609 | package_hashtable[num] = new_node; |
| 610 | } else { |
| 611 | free_package(new_node); |
| 612 | } |
| 613 | return(num); |
| 614 | } |
| 615 | |
| 616 | /* if num = 1, it returns the want status, 2 returns flag, 3 returns status */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 617 | static unsigned int get_status(const unsigned int status_node, const int num) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 618 | { |
| 619 | char *status_string = name_hashtable[status_hashtable[status_node]->status]; |
| 620 | char *state_sub_string; |
| 621 | unsigned int state_sub_num; |
| 622 | int len; |
| 623 | int i; |
| 624 | |
| 625 | /* set tmp_string to point to the start of the word number */ |
| 626 | for (i = 1; i < num; i++) { |
| 627 | /* skip past a word */ |
| 628 | status_string += strcspn(status_string, " "); |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 629 | /* skip past the separating spaces */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 630 | status_string += strspn(status_string, " "); |
| 631 | } |
| 632 | len = strcspn(status_string, " \n\0"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 633 | state_sub_string = bb_xstrndup(status_string, len); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 634 | state_sub_num = search_name_hashtable(state_sub_string); |
| 635 | free(state_sub_string); |
| 636 | return(state_sub_num); |
| 637 | } |
| 638 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 639 | static void set_status(const unsigned int status_node_num, const char *new_value, const int position) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 640 | { |
| 641 | const unsigned int new_value_len = strlen(new_value); |
| 642 | const unsigned int new_value_num = search_name_hashtable(new_value); |
| 643 | unsigned int want = get_status(status_node_num, 1); |
| 644 | unsigned int flag = get_status(status_node_num, 2); |
| 645 | unsigned int status = get_status(status_node_num, 3); |
| 646 | int want_len = strlen(name_hashtable[want]); |
| 647 | int flag_len = strlen(name_hashtable[flag]); |
| 648 | int status_len = strlen(name_hashtable[status]); |
| 649 | char *new_status; |
| 650 | |
| 651 | switch (position) { |
| 652 | case (1): |
| 653 | want = new_value_num; |
| 654 | want_len = new_value_len; |
| 655 | break; |
| 656 | case (2): |
| 657 | flag = new_value_num; |
| 658 | flag_len = new_value_len; |
| 659 | break; |
| 660 | case (3): |
| 661 | status = new_value_num; |
| 662 | status_len = new_value_len; |
| 663 | break; |
| 664 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 665 | bb_error_msg_and_die("DEBUG ONLY: this shouldnt happen"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 666 | } |
| 667 | |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 668 | new_status = bb_xasprintf("%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 669 | status_hashtable[status_node_num]->status = search_name_hashtable(new_status); |
| 670 | free(new_status); |
| 671 | return; |
| 672 | } |
| 673 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 674 | static const char *describe_status(int status_num) { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 675 | int status_want, status_state ; |
| 676 | if ( status_hashtable[status_num] == NULL || status_hashtable[status_num]->status == 0 ) |
| 677 | return "is not installed or flagged to be installed\n"; |
| 678 | |
| 679 | status_want = get_status(status_num, 1); |
| 680 | status_state = get_status(status_num, 3); |
| 681 | |
| 682 | if ( status_state == search_name_hashtable("installed") ) { |
| 683 | if ( status_want == search_name_hashtable("install") ) |
| 684 | return "is installed"; |
| 685 | if ( status_want == search_name_hashtable("deinstall") ) |
| 686 | return "is marked to be removed"; |
| 687 | if ( status_want == search_name_hashtable("purge") ) |
| 688 | return "is marked to be purged"; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 689 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 690 | if ( status_want == search_name_hashtable("unknown") ) |
| 691 | return "is in an indeterminate state"; |
| 692 | if ( status_want == search_name_hashtable("install") ) |
| 693 | return "is marked to be installed"; |
| 694 | |
| 695 | return "is not installed or flagged to be installed"; |
| 696 | } |
| 697 | |
| 698 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 699 | static void index_status_file(const char *filename) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 700 | { |
| 701 | FILE *status_file; |
| 702 | char *control_buffer; |
| 703 | char *status_line; |
| 704 | status_node_t *status_node = NULL; |
| 705 | unsigned int status_num; |
| 706 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 707 | status_file = bb_xfopen(filename, "r"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 708 | while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) { |
| 709 | const unsigned int package_num = fill_package_struct(control_buffer); |
| 710 | if (package_num != -1) { |
| 711 | status_node = xmalloc(sizeof(status_node_t)); |
| 712 | /* fill_package_struct doesnt handle the status field */ |
| 713 | status_line = strstr(control_buffer, "Status:"); |
| 714 | if (status_line != NULL) { |
| 715 | status_line += 7; |
| 716 | status_line += strspn(status_line, " \n\t"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 717 | status_line = bb_xstrndup(status_line, strcspn(status_line, "\n\0")); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 718 | status_node->status = search_name_hashtable(status_line); |
| 719 | free(status_line); |
| 720 | } |
| 721 | status_node->package = package_num; |
| 722 | status_num = search_status_hashtable(name_hashtable[package_hashtable[status_node->package]->name]); |
| 723 | status_hashtable[status_num] = status_node; |
| 724 | } |
| 725 | free(control_buffer); |
| 726 | } |
| 727 | fclose(status_file); |
| 728 | return; |
| 729 | } |
| 730 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 731 | static void write_buffer_no_status(FILE *new_status_file, const char *control_buffer) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 732 | { |
| 733 | char *name; |
| 734 | char *value; |
| 735 | int start = 0; |
| 736 | while (1) { |
| 737 | start += read_package_field(&control_buffer[start], &name, &value); |
| 738 | if (name == NULL) { |
| 739 | break; |
| 740 | } |
| 741 | if (strcmp(name, "Status") != 0) { |
| 742 | fprintf(new_status_file, "%s: %s\n", name, value); |
| 743 | } |
| 744 | } |
| 745 | return; |
| 746 | } |
| 747 | |
| 748 | /* This could do with a cleanup */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 749 | static void write_status_file(deb_file_t **deb_file) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 750 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 751 | FILE *old_status_file = bb_xfopen("/var/lib/dpkg/status", "r"); |
| 752 | FILE *new_status_file = bb_xfopen("/var/lib/dpkg/status.udeb", "w"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 753 | char *package_name; |
| 754 | char *status_from_file; |
| 755 | char *control_buffer = NULL; |
| 756 | char *tmp_string; |
| 757 | int status_num; |
| 758 | int field_start = 0; |
| 759 | int write_flag; |
| 760 | int i = 0; |
| 761 | |
| 762 | /* Update previously known packages */ |
| 763 | while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) { |
| 764 | if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) { |
| 765 | continue; |
| 766 | } |
| 767 | |
| 768 | tmp_string += 8; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 769 | tmp_string += strspn(tmp_string, " \n\t"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 770 | package_name = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n\0")); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 771 | write_flag = FALSE; |
| 772 | tmp_string = strstr(control_buffer, "Status:"); |
| 773 | if (tmp_string != NULL) { |
| 774 | /* Seperate the status value from the control buffer */ |
| 775 | tmp_string += 7; |
| 776 | tmp_string += strspn(tmp_string, " \n\t"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 777 | status_from_file = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n")); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 778 | } else { |
| 779 | status_from_file = NULL; |
| 780 | } |
| 781 | |
| 782 | /* Find this package in the status hashtable */ |
| 783 | status_num = search_status_hashtable(package_name); |
| 784 | if (status_hashtable[status_num] != NULL) { |
| 785 | const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status]; |
| 786 | if (strcmp(status_from_file, status_from_hashtable) != 0) { |
| 787 | /* New status isnt exactly the same as old status */ |
| 788 | const int state_status = get_status(status_num, 3); |
| 789 | if ((strcmp("installed", name_hashtable[state_status]) == 0) || |
| 790 | (strcmp("unpacked", name_hashtable[state_status]) == 0)) { |
| 791 | /* We need to add the control file from the package */ |
| 792 | i = 0; |
| 793 | while(deb_file[i] != NULL) { |
| 794 | if (strcmp(package_name, name_hashtable[package_hashtable[deb_file[i]->package]->name]) == 0) { |
| 795 | /* Write a status file entry with a modified status */ |
| 796 | /* remove trailing \n's */ |
| 797 | write_buffer_no_status(new_status_file, deb_file[i]->control_file); |
| 798 | set_status(status_num, "ok", 2); |
| 799 | fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); |
| 800 | write_flag = TRUE; |
| 801 | break; |
| 802 | } |
| 803 | i++; |
| 804 | } |
| 805 | /* This is temperary, debugging only */ |
| 806 | if (deb_file[i] == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 807 | bb_error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 808 | } |
| 809 | } |
| 810 | else if (strcmp("not-installed", name_hashtable[state_status]) == 0) { |
| 811 | /* Only write the Package, Status, Priority and Section lines */ |
| 812 | fprintf(new_status_file, "Package: %s\n", package_name); |
| 813 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); |
| 814 | |
| 815 | while (1) { |
| 816 | char *field_name; |
| 817 | char *field_value; |
| 818 | field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value); |
| 819 | if (field_name == NULL) { |
| 820 | break; |
| 821 | } |
| 822 | if ((strcmp(field_name, "Priority") == 0) || |
| 823 | (strcmp(field_name, "Section") == 0)) { |
| 824 | fprintf(new_status_file, "%s: %s\n", field_name, field_value); |
| 825 | } |
| 826 | } |
| 827 | write_flag = TRUE; |
| 828 | fputs("\n", new_status_file); |
| 829 | } |
| 830 | else if (strcmp("config-files", name_hashtable[state_status]) == 0) { |
| 831 | /* only change the status line */ |
| 832 | while (1) { |
| 833 | char *field_name; |
| 834 | char *field_value; |
| 835 | field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value); |
| 836 | if (field_name == NULL) { |
| 837 | break; |
| 838 | } |
| 839 | /* Setup start point for next field */ |
| 840 | if (strcmp(field_name, "Status") == 0) { |
| 841 | fprintf(new_status_file, "Status: %s\n", status_from_hashtable); |
| 842 | } else { |
| 843 | fprintf(new_status_file, "%s: %s\n", field_name, field_value); |
| 844 | } |
| 845 | } |
| 846 | write_flag = TRUE; |
| 847 | fputs("\n", new_status_file); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | /* If the package from the status file wasnt handle above, do it now*/ |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 852 | if (! write_flag) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 853 | fprintf(new_status_file, "%s\n\n", control_buffer); |
| 854 | } |
| 855 | |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 856 | free(status_from_file); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 857 | free(package_name); |
| 858 | free(control_buffer); |
| 859 | } |
| 860 | |
| 861 | /* Write any new packages */ |
| 862 | for(i = 0; deb_file[i] != NULL; i++) { |
| 863 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[i]->package]->name]); |
| 864 | if (strcmp("reinstreq", name_hashtable[get_status(status_num, 2)]) == 0) { |
| 865 | write_buffer_no_status(new_status_file, deb_file[i]->control_file); |
| 866 | set_status(status_num, "ok", 2); |
| 867 | fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]); |
| 868 | } |
| 869 | } |
| 870 | fclose(old_status_file); |
| 871 | fclose(new_status_file); |
| 872 | |
| 873 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 874 | /* Create a separate backfile to dpkg */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 875 | if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 876 | struct stat stat_buf; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 877 | if (stat("/var/lib/dpkg/status", &stat_buf) == 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 878 | bb_error_msg_and_die("Couldnt create backup status file"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 879 | } |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 880 | /* Its ok if renaming the status file fails because status |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 881 | * file doesnt exist, maybe we are starting from scratch */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 882 | bb_error_msg("No status file found, creating new one"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 886 | bb_error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 890 | /* This function returns TRUE if the given package can satisfy a |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 891 | * dependency of type depend_type. |
| 892 | * |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 893 | * A pre-depends is satisfied only if a package is already installed, |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 894 | * which a regular depends can be satisfied by a package which we want |
| 895 | * to install. |
| 896 | */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 897 | static int package_satisfies_dependency(int package, int depend_type) |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 898 | { |
| 899 | int status_num = search_status_hashtable(name_hashtable[package_hashtable[package]->name]); |
| 900 | |
| 901 | /* status could be unknown if package is a pure virtual |
| 902 | * provides which cannot satisfy any dependency by itself. |
| 903 | */ |
| 904 | if ( status_hashtable[status_num] == NULL ) |
| 905 | return 0; |
| 906 | |
| 907 | switch (depend_type) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 908 | case EDGE_PRE_DEPENDS: return get_status(status_num, 3) == search_name_hashtable("installed"); |
| 909 | case EDGE_DEPENDS: return get_status(status_num, 1) == search_name_hashtable("install"); |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 910 | } |
| 911 | return 0; |
| 912 | } |
| 913 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 914 | static int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 915 | { |
| 916 | int *conflicts = NULL; |
| 917 | int conflicts_num = 0; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 918 | int i = deb_start; |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 919 | int j; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 920 | |
| 921 | /* Check for conflicts |
| 922 | * TODO: TEST if conflicts with other packages to be installed |
| 923 | * |
| 924 | * Add install packages and the packages they provide |
| 925 | * to the list of files to check conflicts for |
| 926 | */ |
| 927 | |
| 928 | /* Create array of package numbers to check against |
| 929 | * installed package for conflicts*/ |
| 930 | while (deb_file[i] != NULL) { |
| 931 | const unsigned int package_num = deb_file[i]->package; |
| 932 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); |
| 933 | conflicts[conflicts_num] = package_num; |
| 934 | conflicts_num++; |
| 935 | /* add provides to conflicts list */ |
| 936 | for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) { |
| 937 | if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) { |
| 938 | const int conflicts_package_num = search_package_hashtable( |
| 939 | package_hashtable[package_num]->edge[j]->name, |
| 940 | package_hashtable[package_num]->edge[j]->version, |
| 941 | package_hashtable[package_num]->edge[j]->operator); |
| 942 | if (package_hashtable[conflicts_package_num] == NULL) { |
| 943 | /* create a new package */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 944 | common_node_t *new_node = (common_node_t *) xzalloc(sizeof(common_node_t)); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 945 | new_node->name = package_hashtable[package_num]->edge[j]->name; |
| 946 | new_node->version = package_hashtable[package_num]->edge[j]->version; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 947 | package_hashtable[conflicts_package_num] = new_node; |
| 948 | } |
| 949 | conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1)); |
| 950 | conflicts[conflicts_num] = conflicts_package_num; |
| 951 | conflicts_num++; |
| 952 | } |
| 953 | } |
| 954 | i++; |
| 955 | } |
| 956 | |
| 957 | /* Check conflicts */ |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 958 | i = 0; |
| 959 | while (deb_file[i] != NULL) { |
| 960 | const common_node_t *package_node = package_hashtable[deb_file[i]->package]; |
| 961 | int status_num = 0; |
| 962 | status_num = search_status_hashtable(name_hashtable[package_node->name]); |
| 963 | |
| 964 | if (get_status(status_num, 3) == search_name_hashtable("installed")) { |
| 965 | i++; |
| 966 | continue; |
| 967 | } |
| 968 | |
| 969 | for (j = 0; j < package_node->num_of_edges; j++) { |
| 970 | const edge_t *package_edge = package_node->edge[j]; |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 971 | |
| 972 | if (package_edge->type == EDGE_CONFLICTS) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 973 | const unsigned int package_num = |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 974 | search_package_hashtable(package_edge->name, |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 975 | package_edge->version, |
| 976 | package_edge->operator); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 977 | int result = 0; |
| 978 | if (package_hashtable[package_num] != NULL) { |
| 979 | status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 980 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 981 | if (get_status(status_num, 1) == search_name_hashtable("install")) { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 982 | result = test_version(package_hashtable[deb_file[i]->package]->version, |
| 983 | package_edge->version, package_edge->operator); |
| 984 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 985 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 986 | |
| 987 | if (result) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 988 | bb_error_msg_and_die("Package %s conflicts with %s", |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 989 | name_hashtable[package_node->name], |
| 990 | name_hashtable[package_edge->name]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 991 | } |
| 992 | } |
| 993 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 994 | i++; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 995 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 996 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 997 | |
| 998 | /* Check dependendcies */ |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 999 | for (i = 0; i < PACKAGE_HASH_PRIME; i++) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1000 | int status_num = 0; |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1001 | int number_of_alternatives = 0; |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1002 | const edge_t * root_of_alternatives = NULL; |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1003 | const common_node_t *package_node = package_hashtable[i]; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1004 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1005 | /* If the package node does not exist then this |
| 1006 | * package is a virtual one. In which case there are |
| 1007 | * no dependencies to check. |
| 1008 | */ |
| 1009 | if ( package_node == NULL ) continue; |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1010 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1011 | status_num = search_status_hashtable(name_hashtable[package_node->name]); |
| 1012 | |
| 1013 | /* If there is no status then this package is a |
| 1014 | * virtual one provided by something else. In which |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1015 | * case there are no dependencies to check. |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1016 | */ |
| 1017 | if ( status_hashtable[status_num] == NULL ) continue; |
| 1018 | |
| 1019 | /* If we don't want this package installed then we may |
| 1020 | * as well ignore it's dependencies. |
| 1021 | */ |
| 1022 | if (get_status(status_num, 1) != search_name_hashtable("install")) { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1023 | continue; |
| 1024 | } |
| 1025 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1026 | #if 0 |
| 1027 | /* This might be needed so we don't complain about |
| 1028 | * things which are broken but unrelated to the |
| 1029 | * packages that are currently being installed |
| 1030 | */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1031 | if (state_status == search_name_hashtable("installed")) |
| 1032 | continue; |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1033 | #endif |
| 1034 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1035 | /* This code is tested only for EDGE_DEPENDS, since I |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1036 | * have no suitable pre-depends available. There is no |
| 1037 | * reason that it shouldn't work though :-) |
| 1038 | */ |
| 1039 | for (j = 0; j < package_node->num_of_edges; j++) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1040 | const edge_t *package_edge = package_node->edge[j]; |
Glenn L McGrath | 1dbbd2f | 2001-12-05 04:10:14 +0000 | [diff] [blame] | 1041 | unsigned int package_num; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1042 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1043 | if ( package_edge->type == EDGE_OR_PRE_DEPENDS || |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1044 | package_edge->type == EDGE_OR_DEPENDS ) { /* start an EDGE_OR_ list */ |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1045 | number_of_alternatives = package_edge->version; |
| 1046 | root_of_alternatives = package_edge; |
| 1047 | continue; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1048 | } else if ( number_of_alternatives == 0 ) { /* not in the middle of an EDGE_OR_ list */ |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1049 | number_of_alternatives = 1; |
| 1050 | root_of_alternatives = NULL; |
| 1051 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1052 | |
Glenn L McGrath | 1dbbd2f | 2001-12-05 04:10:14 +0000 | [diff] [blame] | 1053 | package_num = search_package_hashtable(package_edge->name, package_edge->version, package_edge->operator); |
Glenn L McGrath | 1dbbd2f | 2001-12-05 04:10:14 +0000 | [diff] [blame] | 1054 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1055 | if (package_edge->type == EDGE_PRE_DEPENDS || |
| 1056 | package_edge->type == EDGE_DEPENDS ) { |
| 1057 | int result=1; |
| 1058 | status_num = 0; |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1059 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1060 | /* If we are inside an alternative then check |
| 1061 | * this edge is the right type. |
| 1062 | * |
| 1063 | * EDGE_DEPENDS == OR_DEPENDS -1 |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1064 | * EDGE_PRE_DEPENDS == OR_PRE_DEPENDS -1 |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1065 | */ |
| 1066 | if ( root_of_alternatives && package_edge->type != root_of_alternatives->type - 1) |
| 1067 | bb_error_msg_and_die("Fatal error. Package dependencies corrupt: %d != %d - 1 \n", |
| 1068 | package_edge->type, root_of_alternatives->type); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1069 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1070 | if (package_hashtable[package_num] != NULL) |
| 1071 | result = !package_satisfies_dependency(package_num, package_edge->type); |
| 1072 | |
| 1073 | if (result) { /* check for other package which provide what we are looking for */ |
| 1074 | int provider = -1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1075 | |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1076 | while ( (provider = search_for_provides(package_edge->name, provider) ) > -1 ) { |
| 1077 | if ( package_hashtable[provider] == NULL ) { |
| 1078 | printf("Have a provider but no package information for it\n"); |
| 1079 | continue; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1080 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1081 | result = !package_satisfies_dependency(provider, package_edge->type); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1082 | |
| 1083 | if ( result == 0 ) |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1084 | break; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1085 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1086 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1087 | |
| 1088 | /* It must be already installed, or to be installed */ |
| 1089 | number_of_alternatives--; |
| 1090 | if (result && number_of_alternatives == 0) { |
| 1091 | if ( root_of_alternatives ) |
| 1092 | bb_error_msg_and_die( |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1093 | "Package %s %sdepends on %s, " |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1094 | "which cannot be satisfied", |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1095 | name_hashtable[package_node->name], |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1096 | package_edge->type == EDGE_PRE_DEPENDS ? "pre-" : "", |
| 1097 | name_hashtable[root_of_alternatives->name]); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1098 | else |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1099 | bb_error_msg_and_die( |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1100 | "Package %s %sdepends on %s, which %s\n", |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1101 | name_hashtable[package_node->name], |
| 1102 | package_edge->type == EDGE_PRE_DEPENDS ? "pre-" : "", |
| 1103 | name_hashtable[package_edge->name], |
| 1104 | describe_status(status_num)); |
| 1105 | } else if ( result == 0 && number_of_alternatives ) { |
| 1106 | /* we've found a package which |
| 1107 | * satisfies the dependency, |
| 1108 | * so skip over the rest of |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1109 | * the alternatives. |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1110 | */ |
| 1111 | j += number_of_alternatives; |
| 1112 | number_of_alternatives = 0; |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1113 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1116 | } |
| 1117 | free(conflicts); |
| 1118 | return(TRUE); |
| 1119 | } |
| 1120 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1121 | static char **create_list(const char *filename) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1122 | { |
| 1123 | FILE *list_stream; |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1124 | char **file_list = NULL; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1125 | char *line = NULL; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1126 | int count = 0; |
| 1127 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1128 | /* don't use [xw]fopen here, handle error ourself */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1129 | list_stream = fopen(filename, "r"); |
| 1130 | if (list_stream == NULL) { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1131 | return(NULL); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1132 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1133 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1134 | while ((line = bb_get_chomped_line_from_file(list_stream)) != NULL) { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1135 | file_list = xrealloc(file_list, sizeof(char *) * (count + 2)); |
Glenn L McGrath | b323162 | 2002-12-11 03:10:13 +0000 | [diff] [blame] | 1136 | file_list[count] = line; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1137 | count++; |
| 1138 | } |
| 1139 | fclose(list_stream); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1140 | |
| 1141 | if (count == 0) { |
| 1142 | return(NULL); |
| 1143 | } else { |
| 1144 | file_list[count] = NULL; |
| 1145 | return(file_list); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /* maybe i should try and hook this into remove_file.c somehow */ |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1150 | static int remove_file_array(char **remove_names, char **exclude_names) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1151 | { |
| 1152 | struct stat path_stat; |
| 1153 | int match_flag; |
| 1154 | int remove_flag = FALSE; |
| 1155 | int i,j; |
| 1156 | |
| 1157 | if (remove_names == NULL) { |
| 1158 | return(FALSE); |
| 1159 | } |
| 1160 | for (i = 0; remove_names[i] != NULL; i++) { |
| 1161 | match_flag = FALSE; |
| 1162 | if (exclude_names != NULL) { |
| 1163 | for (j = 0; exclude_names[j] != 0; j++) { |
| 1164 | if (strcmp(remove_names[i], exclude_names[j]) == 0) { |
| 1165 | match_flag = TRUE; |
| 1166 | break; |
| 1167 | } |
| 1168 | } |
| 1169 | } |
| 1170 | if (!match_flag) { |
| 1171 | if (lstat(remove_names[i], &path_stat) < 0) { |
| 1172 | continue; |
| 1173 | } |
| 1174 | if (S_ISDIR(path_stat.st_mode)) { |
| 1175 | if (rmdir(remove_names[i]) != -1) { |
| 1176 | remove_flag = TRUE; |
| 1177 | } |
| 1178 | } else { |
| 1179 | if (unlink(remove_names[i]) != -1) { |
| 1180 | remove_flag = TRUE; |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | } |
| 1185 | return(remove_flag); |
| 1186 | } |
| 1187 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1188 | static int run_package_script(const char *package_name, const char *script_type) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1189 | { |
| 1190 | struct stat path_stat; |
| 1191 | char *script_path; |
| 1192 | int result; |
| 1193 | |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1194 | script_path = bb_xasprintf("/var/lib/dpkg/info/%s.%s", package_name, script_type); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1195 | |
| 1196 | /* If the file doesnt exist is isnt a fatal */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1197 | result = lstat(script_path, &path_stat) < 0 ? EXIT_SUCCESS : system(script_path); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1198 | free(script_path); |
| 1199 | return(result); |
| 1200 | } |
| 1201 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1202 | static const char *all_control_files[] = {"preinst", "postinst", "prerm", "postrm", |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1203 | "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL }; |
| 1204 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1205 | static char **all_control_list(const char *package_name) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1206 | { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1207 | unsigned short i = 0; |
| 1208 | char **remove_files; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1211 | remove_files = xzalloc(sizeof(all_control_files)); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1212 | while (all_control_files[i]) { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1213 | remove_files[i] = bb_xasprintf("/var/lib/dpkg/info/%s.%s", package_name, all_control_files[i]); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1214 | i++; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1215 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1216 | |
| 1217 | return(remove_files); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1220 | static void free_array(char **array) |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1221 | { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1222 | |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1223 | if (array) { |
| 1224 | unsigned short i = 0; |
| 1225 | while (array[i]) { |
| 1226 | free(array[i]); |
| 1227 | i++; |
| 1228 | } |
| 1229 | free(array); |
| 1230 | } |
| 1231 | } |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1232 | |
| 1233 | /* This function lists information on the installed packages. It loops through |
| 1234 | * the status_hashtable to retrieve the info. This results in smaller code than |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1235 | * scanning the status file. The resulting list, however, is unsorted. |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1236 | */ |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 1237 | static void list_packages(void) |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1238 | { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1239 | int i; |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1240 | |
| 1241 | printf(" Name Version\n"); |
| 1242 | printf("+++-==============-==============\n"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1243 | |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1244 | /* go through status hash, dereference package hash and finally strings */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1245 | for (i=0; i<STATUS_HASH_PRIME+1; i++) { |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1246 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1247 | if (status_hashtable[i]) { |
| 1248 | const char *stat_str; /* status string */ |
| 1249 | const char *name_str; /* package name */ |
| 1250 | const char *vers_str; /* version */ |
| 1251 | char s1, s2; /* status abbreviations */ |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1252 | int spccnt; /* space count */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1253 | int j; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1254 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1255 | stat_str = name_hashtable[status_hashtable[i]->status]; |
| 1256 | name_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->name]; |
| 1257 | vers_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->version]; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1258 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1259 | /* get abbreviation for status field 1 */ |
| 1260 | s1 = stat_str[0] == 'i' ? 'i' : 'r'; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1261 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1262 | /* get abbreviation for status field 2 */ |
| 1263 | for (j=0, spccnt=0; stat_str[j] && spccnt<2; j++) { |
| 1264 | if (stat_str[j] == ' ') spccnt++; |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1265 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1266 | s2 = stat_str[j]; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1267 | |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1268 | /* print out the line formatted like Debian dpkg */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1269 | printf("%c%c %-14s %s\n", s1, s2, name_str, vers_str); |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1273 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1274 | static void remove_package(const unsigned int package_num, int noisy) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1275 | { |
| 1276 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1277 | const char *package_version = name_hashtable[package_hashtable[package_num]->version]; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1278 | const unsigned int status_num = search_status_hashtable(package_name); |
| 1279 | const int package_name_length = strlen(package_name); |
| 1280 | char **remove_files; |
| 1281 | char **exclude_files; |
| 1282 | char list_name[package_name_length + 25]; |
| 1283 | char conffile_name[package_name_length + 30]; |
| 1284 | int return_value; |
| 1285 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1286 | if ( noisy ) |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1287 | printf("Removing %s (%s) ...\n", package_name, package_version); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1288 | |
| 1289 | /* run prerm script */ |
| 1290 | return_value = run_package_script(package_name, "prerm"); |
| 1291 | if (return_value == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1292 | bb_error_msg_and_die("script failed, prerm failure"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1295 | /* Create a list of files to remove, and a separate list of those to keep */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1296 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 1297 | remove_files = create_list(list_name); |
| 1298 | |
| 1299 | sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name); |
| 1300 | exclude_files = create_list(conffile_name); |
| 1301 | |
| 1302 | /* Some directories cant be removed straight away, so do multiple passes */ |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 1303 | while (remove_file_array(remove_files, exclude_files)); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1304 | free_array(exclude_files); |
| 1305 | free_array(remove_files); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1306 | |
| 1307 | /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1308 | exclude_files = xzalloc(sizeof(char*) * 3); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1309 | exclude_files[0] = bb_xstrdup(conffile_name); |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1310 | exclude_files[1] = bb_xasprintf("/var/lib/dpkg/info/%s.postrm", package_name); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1311 | |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1312 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
| 1313 | remove_files = all_control_list(package_name); |
| 1314 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1315 | remove_file_array(remove_files, exclude_files); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1316 | free_array(remove_files); |
| 1317 | free_array(exclude_files); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1318 | |
| 1319 | /* rename <package>.conffile to <package>.list */ |
| 1320 | rename(conffile_name, list_name); |
| 1321 | |
| 1322 | /* Change package status */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1323 | set_status(status_num, "config-files", 3); |
| 1324 | } |
| 1325 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1326 | static void purge_package(const unsigned int package_num) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1327 | { |
| 1328 | const char *package_name = name_hashtable[package_hashtable[package_num]->name]; |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1329 | const char *package_version = name_hashtable[package_hashtable[package_num]->version]; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1330 | const unsigned int status_num = search_status_hashtable(package_name); |
| 1331 | char **remove_files; |
| 1332 | char **exclude_files; |
| 1333 | char list_name[strlen(package_name) + 25]; |
| 1334 | |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1335 | printf("Purging %s (%s) ...\n", package_name, package_version); |
| 1336 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1337 | /* run prerm script */ |
| 1338 | if (run_package_script(package_name, "prerm") != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1339 | bb_error_msg_and_die("script failed, prerm failure"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1340 | } |
| 1341 | |
| 1342 | /* Create a list of files to remove */ |
| 1343 | sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name); |
| 1344 | remove_files = create_list(list_name); |
| 1345 | |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1346 | exclude_files = xzalloc(sizeof(char*)); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1347 | |
| 1348 | /* Some directories cant be removed straight away, so do multiple passes */ |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 1349 | while (remove_file_array(remove_files, exclude_files)); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1350 | free_array(remove_files); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1351 | |
| 1352 | /* Create a list of all /var/lib/dpkg/info/<package> files */ |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1353 | remove_files = all_control_list(package_name); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1354 | remove_file_array(remove_files, exclude_files); |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1355 | free_array(remove_files); |
| 1356 | free(exclude_files); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1357 | |
| 1358 | /* run postrm script */ |
| 1359 | if (run_package_script(package_name, "postrm") == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1360 | bb_error_msg_and_die("postrm fialure.. set status to what?"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
| 1363 | /* Change package status */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1364 | set_status(status_num, "not-installed", 3); |
| 1365 | } |
| 1366 | |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1367 | static archive_handle_t *init_archive_deb_ar(const char *filename) |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1368 | { |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1369 | archive_handle_t *ar_handle; |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1370 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1371 | /* Setup an ar archive handle that refers to the gzip sub archive */ |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1372 | ar_handle = init_handle(); |
| 1373 | ar_handle->filter = filter_accept_list_reassign; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1374 | ar_handle->src_fd = bb_xopen(filename, O_RDONLY); |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1375 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1376 | return(ar_handle); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1377 | } |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1378 | |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1379 | static void init_archive_deb_control(archive_handle_t *ar_handle) |
| 1380 | { |
| 1381 | archive_handle_t *tar_handle; |
| 1382 | |
| 1383 | /* Setup the tar archive handle */ |
| 1384 | tar_handle = init_handle(); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1385 | tar_handle->src_fd = ar_handle->src_fd; |
| 1386 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1387 | /* We don't care about data.tar.* or debian-binary, just control.tar.* */ |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1388 | #ifdef CONFIG_FEATURE_DEB_TAR_GZ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1389 | llist_add_to(&(ar_handle->accept), "control.tar.gz"); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1390 | #endif |
| 1391 | #ifdef CONFIG_FEATURE_DEB_TAR_BZ2 |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1392 | llist_add_to(&(ar_handle->accept), "control.tar.bz2"); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1393 | #endif |
| 1394 | |
| 1395 | /* Assign the tar handle as a subarchive of the ar handle */ |
| 1396 | ar_handle->sub_archive = tar_handle; |
| 1397 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1398 | return; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | static void init_archive_deb_data(archive_handle_t *ar_handle) |
| 1402 | { |
| 1403 | archive_handle_t *tar_handle; |
| 1404 | |
| 1405 | /* Setup the tar archive handle */ |
| 1406 | tar_handle = init_handle(); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1407 | tar_handle->src_fd = ar_handle->src_fd; |
| 1408 | |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1409 | /* We don't care about control.tar.* or debian-binary, just data.tar.* */ |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1410 | #ifdef CONFIG_FEATURE_DEB_TAR_GZ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1411 | llist_add_to(&(ar_handle->accept), "data.tar.gz"); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1412 | #endif |
| 1413 | #ifdef CONFIG_FEATURE_DEB_TAR_BZ2 |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1414 | llist_add_to(&(ar_handle->accept), "data.tar.bz2"); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1415 | #endif |
| 1416 | |
| 1417 | /* Assign the tar handle as a subarchive of the ar handle */ |
| 1418 | ar_handle->sub_archive = tar_handle; |
| 1419 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1420 | return; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
Eric Andersen | 1393a39 | 2003-09-15 08:06:15 +0000 | [diff] [blame] | 1423 | static char *deb_extract_control_file_to_buffer(archive_handle_t *ar_handle, llist_t *myaccept) |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1424 | { |
| 1425 | ar_handle->sub_archive->action_data = data_extract_to_buffer; |
Eric Andersen | 1393a39 | 2003-09-15 08:06:15 +0000 | [diff] [blame] | 1426 | ar_handle->sub_archive->accept = myaccept; |
Paul Fox | 37dd624 | 2005-07-22 13:17:41 +0000 | [diff] [blame] | 1427 | ar_handle->sub_archive->filter = filter_accept_list; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1428 | |
| 1429 | unpack_ar_archive(ar_handle); |
| 1430 | close(ar_handle->src_fd); |
| 1431 | |
| 1432 | return(ar_handle->sub_archive->buffer); |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1433 | } |
| 1434 | |
Glenn L McGrath | 6ab32eb | 2002-11-03 11:57:10 +0000 | [diff] [blame] | 1435 | static void data_extract_all_prefix(archive_handle_t *archive_handle) |
| 1436 | { |
| 1437 | char *name_ptr = archive_handle->file_header->name; |
| 1438 | |
| 1439 | name_ptr += strspn(name_ptr, "./"); |
| 1440 | if (name_ptr[0] != '\0') { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1441 | archive_handle->file_header->name = bb_xasprintf("%s%s", archive_handle->buffer, name_ptr); |
Glenn L McGrath | 6ab32eb | 2002-11-03 11:57:10 +0000 | [diff] [blame] | 1442 | data_extract_all(archive_handle); |
| 1443 | } |
| 1444 | return; |
| 1445 | } |
| 1446 | |
| 1447 | static void unpack_package(deb_file_t *deb_file) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1448 | { |
| 1449 | const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name]; |
| 1450 | const unsigned int status_num = search_status_hashtable(package_name); |
| 1451 | const unsigned int status_package_num = status_hashtable[status_num]->package; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1452 | char *info_prefix; |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1453 | archive_handle_t *archive_handle; |
| 1454 | FILE *out_stream; |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1455 | llist_t *accept_list = NULL; |
| 1456 | int i = 0; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* If existing version, remove it first */ |
| 1459 | if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) { |
| 1460 | /* Package is already installed, remove old version first */ |
| 1461 | printf("Preparing to replace %s %s (using %s) ...\n", package_name, |
| 1462 | name_hashtable[package_hashtable[status_package_num]->version], |
| 1463 | deb_file->filename); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1464 | remove_package(status_package_num, 0); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1465 | } else { |
| 1466 | printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename); |
| 1467 | } |
| 1468 | |
| 1469 | /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */ |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1470 | info_prefix = bb_xasprintf("/var/lib/dpkg/info/%s.", package_name); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1471 | archive_handle = init_archive_deb_ar(deb_file->filename); |
| 1472 | init_archive_deb_control(archive_handle); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1473 | |
| 1474 | while(all_control_files[i]) { |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1475 | char *c = bb_xasprintf("./%s", all_control_files[i]); |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1476 | llist_add_to(&accept_list, c); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1477 | i++; |
| 1478 | } |
| 1479 | archive_handle->sub_archive->accept = accept_list; |
| 1480 | archive_handle->sub_archive->filter = filter_accept_list; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1481 | archive_handle->sub_archive->action_data = data_extract_all_prefix; |
| 1482 | archive_handle->sub_archive->buffer = info_prefix; |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1483 | archive_handle->sub_archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1484 | unpack_ar_archive(archive_handle); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Run the preinst prior to extracting */ |
| 1487 | if (run_package_script(package_name, "preinst") != 0) { |
| 1488 | /* when preinst returns exit code != 0 then quit installation process */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1489 | bb_error_msg_and_die("subprocess pre-installation script returned error."); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1490 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1491 | |
| 1492 | /* Extract data.tar.gz to the root directory */ |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1493 | archive_handle = init_archive_deb_ar(deb_file->filename); |
| 1494 | init_archive_deb_data(archive_handle); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1495 | archive_handle->sub_archive->action_data = data_extract_all_prefix; |
| 1496 | archive_handle->sub_archive->buffer = "/"; |
| 1497 | archive_handle->sub_archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1498 | unpack_ar_archive(archive_handle); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1499 | |
| 1500 | /* Create the list file */ |
| 1501 | strcat(info_prefix, "list"); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1502 | out_stream = bb_xfopen(info_prefix, "w"); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1503 | while (archive_handle->sub_archive->passed) { |
| 1504 | /* the leading . has been stripped by data_extract_all_prefix already */ |
| 1505 | fputs(archive_handle->sub_archive->passed->data, out_stream); |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1506 | fputc('\n', out_stream); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1507 | archive_handle->sub_archive->passed = archive_handle->sub_archive->passed->link; |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1508 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1509 | fclose(out_stream); |
| 1510 | |
| 1511 | /* change status */ |
| 1512 | set_status(status_num, "install", 1); |
| 1513 | set_status(status_num, "unpacked", 3); |
| 1514 | |
| 1515 | free(info_prefix); |
| 1516 | } |
| 1517 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 1518 | static void configure_package(deb_file_t *deb_file) |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1519 | { |
| 1520 | const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name]; |
| 1521 | const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version]; |
| 1522 | const int status_num = search_status_hashtable(package_name); |
| 1523 | |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1524 | printf("Setting up %s (%s) ...\n", package_name, package_version); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1525 | |
| 1526 | /* Run the postinst script */ |
| 1527 | if (run_package_script(package_name, "postinst") != 0) { |
| 1528 | /* TODO: handle failure gracefully */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1529 | bb_error_msg_and_die("postrm failure.. set status to what?"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1530 | } |
| 1531 | /* Change status to reflect success */ |
| 1532 | set_status(status_num, "install", 1); |
| 1533 | set_status(status_num, "installed", 3); |
| 1534 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1535 | |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1536 | int dpkg_main(int argc, char **argv) |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1537 | { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1538 | deb_file_t **deb_file = NULL; |
| 1539 | status_node_t *status_node; |
Matt Kraai | efd7f03 | 2001-11-19 21:07:15 +0000 | [diff] [blame] | 1540 | int opt; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1541 | int package_num; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1542 | int dpkg_opt = 0; |
| 1543 | int deb_count = 0; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1544 | int state_status; |
| 1545 | int status_num; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1546 | int i; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1547 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1548 | while ((opt = getopt(argc, argv, "CF:ilPru")) != -1) { |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1549 | switch (opt) { |
Glenn L McGrath | 778041f | 2001-07-18 05:17:39 +0000 | [diff] [blame] | 1550 | case 'C': // equivalent to --configure in official dpkg |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1551 | dpkg_opt |= dpkg_opt_configure; |
| 1552 | dpkg_opt |= dpkg_opt_package_name; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | case 'F': // equivalent to --force in official dpkg |
| 1555 | if (strcmp(optarg, "depends") == 0) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1556 | dpkg_opt |= dpkg_opt_force_ignore_depends; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1557 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1558 | break; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1559 | case 'i': |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1560 | dpkg_opt |= dpkg_opt_install; |
| 1561 | dpkg_opt |= dpkg_opt_filename; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1562 | break; |
| 1563 | case 'l': |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1564 | dpkg_opt |= dpkg_opt_list_installed; |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1565 | break; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1566 | case 'P': |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1567 | dpkg_opt |= dpkg_opt_purge; |
| 1568 | dpkg_opt |= dpkg_opt_package_name; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1569 | break; |
| 1570 | case 'r': |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1571 | dpkg_opt |= dpkg_opt_remove; |
| 1572 | dpkg_opt |= dpkg_opt_package_name; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1573 | break; |
| 1574 | case 'u': /* Equivalent to --unpack in official dpkg */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1575 | dpkg_opt |= dpkg_opt_unpack; |
| 1576 | dpkg_opt |= dpkg_opt_filename; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1577 | break; |
| 1578 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1579 | bb_show_usage(); |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1580 | } |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1581 | } |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1582 | /* check for non-otion argument if expected */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1583 | if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1584 | bb_show_usage(); |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1585 | } |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1586 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1587 | /* puts("(Reading database ... xxxxx files and directories installed.)"); */ |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1588 | index_status_file("/var/lib/dpkg/status"); |
| 1589 | |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1590 | /* if the list action was given print the installed packages and exit */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1591 | if (dpkg_opt & dpkg_opt_list_installed) { |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1592 | list_packages(); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1593 | return(EXIT_SUCCESS); |
Glenn L McGrath | e738661 | 2001-09-21 04:30:51 +0000 | [diff] [blame] | 1594 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1595 | |
Glenn L McGrath | 0d2fb76 | 2001-10-25 14:26:05 +0000 | [diff] [blame] | 1596 | /* Read arguments and store relevant info in structs */ |
| 1597 | while (optind < argc) { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1598 | /* deb_count = nb_elem - 1 and we need nb_elem + 1 to allocate terminal node [NULL pointer] */ |
| 1599 | deb_file = xrealloc(deb_file, sizeof(deb_file_t *) * (deb_count + 2)); |
Rob Landley | 1ec5b29 | 2006-05-29 07:42:02 +0000 | [diff] [blame^] | 1600 | deb_file[deb_count] = (deb_file_t *) xzalloc(sizeof(deb_file_t)); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1601 | if (dpkg_opt & dpkg_opt_filename) { |
Glenn L McGrath | 61b7904 | 2002-10-19 10:40:55 +0000 | [diff] [blame] | 1602 | archive_handle_t *archive_handle; |
Glenn L McGrath | 66125c8 | 2002-12-08 00:54:33 +0000 | [diff] [blame] | 1603 | llist_t *control_list = NULL; |
Glenn L McGrath | d8d1191 | 2002-11-05 13:56:04 +0000 | [diff] [blame] | 1604 | |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1605 | /* Extract the control file */ |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1606 | llist_add_to(&control_list, "./control"); |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1607 | archive_handle = init_archive_deb_ar(argv[optind]); |
| 1608 | init_archive_deb_control(archive_handle); |
| 1609 | deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1610 | if (deb_file[deb_count]->control_file == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1611 | bb_error_msg_and_die("Couldnt extract control file"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1612 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1613 | deb_file[deb_count]->filename = bb_xstrdup(argv[optind]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1614 | package_num = fill_package_struct(deb_file[deb_count]->control_file); |
Glenn L McGrath | 0d2fb76 | 2001-10-25 14:26:05 +0000 | [diff] [blame] | 1615 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1616 | if (package_num == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1617 | bb_error_msg("Invalid control file in %s", argv[optind]); |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1618 | optind++; |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1619 | continue; |
| 1620 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1621 | deb_file[deb_count]->package = (unsigned int) package_num; |
Glenn L McGrath | 747381c | 2002-11-06 22:54:41 +0000 | [diff] [blame] | 1622 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1623 | /* Add the package to the status hashtable */ |
| 1624 | if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1625 | /* Try and find a currently installed version of this package */ |
| 1626 | status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); |
| 1627 | /* If no previous entry was found initialise a new entry */ |
| 1628 | if ((status_hashtable[status_num] == NULL) || |
| 1629 | (status_hashtable[status_num]->status == 0)) { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1630 | status_node = (status_node_t *) xmalloc(sizeof(status_node_t)); |
| 1631 | status_node->package = deb_file[deb_count]->package; |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1632 | /* reinstreq isnt changed to "ok" until the package control info |
| 1633 | * is written to the status file*/ |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1634 | status_node->status = search_name_hashtable("install reinstreq not-installed"); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1635 | status_hashtable[status_num] = status_node; |
| 1636 | } else { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1637 | set_status(status_num, "install", 1); |
| 1638 | set_status(status_num, "reinstreq", 2); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1642 | else if (dpkg_opt & dpkg_opt_package_name) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1643 | deb_file[deb_count]->package = search_package_hashtable( |
| 1644 | search_name_hashtable(argv[optind]), |
| 1645 | search_name_hashtable("ANY"), VER_ANY); |
| 1646 | if (package_hashtable[deb_file[deb_count]->package] == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1647 | bb_error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1648 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1649 | package_num = deb_file[deb_count]->package; |
| 1650 | status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]); |
| 1651 | state_status = get_status(status_num, 3); |
Glenn L McGrath | 0d2fb76 | 2001-10-25 14:26:05 +0000 | [diff] [blame] | 1652 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1653 | /* check package status is "installed" */ |
| 1654 | if (dpkg_opt & dpkg_opt_remove) { |
| 1655 | if ((strcmp(name_hashtable[state_status], "not-installed") == 0) || |
| 1656 | (strcmp(name_hashtable[state_status], "config-files") == 0)) { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1657 | bb_error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[package_num]->name]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1658 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1659 | set_status(status_num, "deinstall", 1); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1660 | } |
| 1661 | else if (dpkg_opt & dpkg_opt_purge) { |
| 1662 | /* if package status is "conf-files" then its ok */ |
| 1663 | if (strcmp(name_hashtable[state_status], "not-installed") == 0) { |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1664 | bb_error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[package_num]->name]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1665 | } |
Glenn L McGrath | b8c3a54 | 2003-11-28 22:38:14 +0000 | [diff] [blame] | 1666 | set_status(status_num, "purge", 1); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1667 | } |
| 1668 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1669 | deb_count++; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1670 | optind++; |
| 1671 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1672 | deb_file[deb_count] = NULL; |
Glenn L McGrath | 0e757a2 | 2001-04-08 05:27:18 +0000 | [diff] [blame] | 1673 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1674 | /* Check that the deb file arguments are installable */ |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1675 | if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) { |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1676 | if (!check_deps(deb_file, 0, deb_count)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1677 | bb_error_msg_and_die("Dependency check failed"); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1678 | } |
Glenn L McGrath | 0d2fb76 | 2001-10-25 14:26:05 +0000 | [diff] [blame] | 1679 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1680 | |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1681 | /* TODO: install or remove packages in the correct dependency order */ |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1682 | for (i = 0; i < deb_count; i++) { |
| 1683 | /* Remove or purge packages */ |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1684 | if (dpkg_opt & dpkg_opt_remove) { |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1685 | remove_package(deb_file[i]->package, 1); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1686 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1687 | else if (dpkg_opt & dpkg_opt_purge) { |
| 1688 | purge_package(deb_file[i]->package); |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1689 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1690 | else if (dpkg_opt & dpkg_opt_unpack) { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1691 | unpack_package(deb_file[i]); |
| 1692 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1693 | else if (dpkg_opt & dpkg_opt_install) { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1694 | unpack_package(deb_file[i]); |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1695 | /* package is configured in second pass below */ |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1696 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1697 | else if (dpkg_opt & dpkg_opt_configure) { |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1698 | configure_package(deb_file[i]); |
| 1699 | } |
| 1700 | } |
Glenn L McGrath | fea4b44 | 2003-11-26 21:53:37 +0000 | [diff] [blame] | 1701 | /* configure installed packages */ |
| 1702 | if (dpkg_opt & dpkg_opt_install) { |
| 1703 | for (i = 0; i < deb_count; i++) |
| 1704 | configure_package(deb_file[i]); |
| 1705 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 1706 | |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1707 | write_status_file(deb_file); |
| 1708 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1709 | for (i = 0; i < deb_count; i++) { |
| 1710 | free(deb_file[i]->control_file); |
| 1711 | free(deb_file[i]->filename); |
| 1712 | free(deb_file[i]); |
Glenn L McGrath | c900575 | 2001-02-10 02:05:24 +0000 | [diff] [blame] | 1713 | } |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1714 | |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1715 | free(deb_file); |
| 1716 | |
| 1717 | for (i = 0; i < NAME_HASH_PRIME; i++) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1718 | free(name_hashtable[i]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
| 1721 | for (i = 0; i < PACKAGE_HASH_PRIME; i++) { |
Glenn L McGrath | a94a06a | 2002-05-29 13:45:34 +0000 | [diff] [blame] | 1722 | if (package_hashtable[i] != NULL) { |
| 1723 | free_package(package_hashtable[i]); |
| 1724 | } |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
| 1727 | for (i = 0; i < STATUS_HASH_PRIME; i++) { |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1728 | free(status_hashtable[i]); |
Glenn L McGrath | ef0eab5 | 2001-10-25 14:49:48 +0000 | [diff] [blame] | 1729 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1730 | |
Glenn L McGrath | 95bfe63 | 2001-09-29 03:34:38 +0000 | [diff] [blame] | 1731 | return(EXIT_SUCCESS); |
Eric Andersen | 67991cf | 2001-02-14 21:23:06 +0000 | [diff] [blame] | 1732 | } |
Glenn L McGrath | ccd65c9 | 2001-07-13 18:35:24 +0000 | [diff] [blame] | 1733 | |