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