2 * Mini dpkg implementation for busybox.
3 * This is not meant as a replacement for dpkg
5 * Written By Glenn McGrath with the help of others
6 * Copyright (C) 2001 by Glenn McGrath
8 * Started life as a busybox implementation of udpkg
10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 * Known difference between busybox dpkg and the official dpkg that i don't
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.
18 * - When installing a package the Status: field is placed at the end of the
19 * section, rather than just after the Package: field.
21 * Bugs that need to be fixed
22 * - (unknown, please let me know when you find any)
31 #include "unarchive.h"
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
45 /* NAME_HASH_PRIME, Stores package names and versions,
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
49 static char *name_hashtable[NAME_HASH_PRIME + 1];
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;
64 unsigned int version:14;
67 typedef struct common_node_s {
69 unsigned int version:14;
70 unsigned int num_of_edges:14;
73 static common_node_t *package_hashtable[PACKAGE_HASH_PRIME + 1];
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
77 * likely to be installed at any one time, so there is a bit of leeway here */
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 */
83 static status_node_t *status_hashtable[STATUS_HASH_PRIME + 1];
85 /* Even numbers are for 'extras', like ored dependencies or null */
89 EDGE_OR_PRE_DEPENDS = 2,
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
121 typedef struct deb_file_s {
124 unsigned int package:14;
128 static void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime)
130 unsigned long int hash_num = key[0];
131 int len = strlen(key);
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
139 * to be shifted is 8, don't want to shift data to where it has
141 hash_num += ((key[i] + key[i-1]) << ((key[i] * i) % 24));
143 *start = (unsigned int) hash_num % hash_prime;
144 *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1));
147 /* this adds the key to the hash table */
148 static int search_name_hashtable(const char *key)
150 unsigned int probe_address = 0;
151 unsigned int probe_decrement = 0;
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);
159 probe_address -= probe_decrement;
160 if ((int)probe_address < 0) {
161 probe_address += NAME_HASH_PRIME;
165 name_hashtable[probe_address] = bb_xstrdup(key);
166 return(probe_address);
169 /* this DOESNT add the key to the hashtable
170 * TODO make it consistent with search_name_hashtable
172 static unsigned int search_status_hashtable(const char *key)
174 unsigned int probe_address = 0;
175 unsigned int probe_decrement = 0;
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) {
182 probe_address -= probe_decrement;
183 if ((int)probe_address < 0) {
184 probe_address += STATUS_HASH_PRIME;
188 return(probe_address);
191 /* Need to rethink version comparison, maybe the official dpkg has something i can use ? */
192 static int version_compare_part(const char *version1, const char *version2)
194 int upstream_len1 = 0;
195 int upstream_len2 = 0;
205 if (version1 == NULL) {
206 version1 = bb_xstrdup("");
208 if (version2 == NULL) {
209 version2 = bb_xstrdup("");
211 upstream_len1 = strlen(version1);
212 upstream_len2 = strlen(version2);
214 while ((len1 < upstream_len1) || (len2 < upstream_len2)) {
215 /* Compare non-digit section */
216 tmp_int = strcspn(&version1[len1], "0123456789");
217 name1_char = bb_xstrndup(&version1[len1], tmp_int);
219 tmp_int = strcspn(&version2[len2], "0123456789");
220 name2_char = bb_xstrndup(&version2[len2], tmp_int);
222 tmp_int = strcmp(name1_char, name2_char);
227 goto cleanup_version_compare_part;
231 tmp_int = strspn(&version1[len1], "0123456789");
232 name1_char = bb_xstrndup(&version1[len1], tmp_int);
234 tmp_int = strspn(&version2[len2], "0123456789");
235 name2_char = bb_xstrndup(&version2[len2], tmp_int);
237 ver_num1 = atoi(name1_char);
238 ver_num2 = atoi(name2_char);
241 if (ver_num1 < ver_num2) {
243 goto cleanup_version_compare_part;
245 else if (ver_num1 > ver_num2) {
247 goto cleanup_version_compare_part;
251 cleanup_version_compare_part:
255 /* if ver1 < ver2 return -1,
256 * if ver1 = ver2 return 0,
257 * if ver1 > ver2 return 1,
259 static int version_compare(const unsigned int ver1, const unsigned int ver2)
261 char *ch_ver1 = name_hashtable[ver1];
262 char *ch_ver2 = name_hashtable[ver2];
265 char *deb_ver1, *deb_ver2;
266 char *ver1_ptr, *ver2_ptr;
272 if (ch_ver1[1] == ':') {
274 ver1_ptr = strchr(ch_ver1, ':') + 1;
279 if (ch_ver2[1] == ':') {
281 ver2_ptr = strchr(ch_ver2, ':') + 1;
286 if (epoch1 < epoch2) {
289 else if (epoch1 > epoch2) {
293 /* Compare upstream version */
294 upstream_ver1 = bb_xstrdup(ver1_ptr);
295 upstream_ver2 = bb_xstrdup(ver2_ptr);
297 /* Chop off debian version, and store for later use */
298 deb_ver1 = strrchr(upstream_ver1, '-');
299 deb_ver2 = strrchr(upstream_ver2, '-');
308 result = version_compare_part(upstream_ver1, upstream_ver2);
317 /* Compare debian versions */
318 return(version_compare_part(deb_ver1, deb_ver2));
321 static int test_version(const unsigned int version1, const unsigned int version2, const unsigned int operator)
323 const int version_result = version_compare(version1, version2);
328 if (version_result == 0) {
333 if (version_result < 0) {
337 case (VER_LESS_EQUAL):
338 if (version_result <= 0) {
343 if (version_result > 0) {
347 case (VER_MORE_EQUAL):
348 if (version_result >= 0) {
357 static int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator)
359 unsigned int probe_address = 0;
360 unsigned int probe_decrement = 0;
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);
368 if (test_version(package_hashtable[probe_address]->version, version, operator)) {
369 return(probe_address);
372 probe_address -= probe_decrement;
373 if ((int)probe_address < 0) {
374 probe_address += PACKAGE_HASH_PRIME;
377 return(probe_address);
381 * This function searches through the entire package_hashtable looking
382 * for a package which provides "needle". It returns the index into
383 * the package_hashtable for the providing package.
385 * needle is the index into name_hashtable of the package we are
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
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.
396 static int search_for_provides(int needle, int start_at) {
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 )
410 * Add an edge to a node
412 static void add_edge_to_node(common_node_t *node, edge_t *edge)
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;
420 * Create one new node and one new edge for every dependency.
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
429 static void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type)
431 char *line = bb_xstrdup(whole_line);
433 char *line_ptr1 = NULL;
434 char *line_ptr2 = NULL;
442 field = strtok_r(line, ",", &line_ptr1);
444 /* skip leading spaces */
445 field += strspn(field, " ");
446 line2 = bb_xstrdup(field);
447 field2 = strtok_r(line2, "|", &line_ptr2);
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;
457 or_edge->name = search_name_hashtable(field);
458 or_edge->version = 0; // tracks the number of altenatives
460 add_edge_to_node(parent_node, or_edge);
464 edge = (edge_t *) xmalloc(sizeof(edge_t));
465 edge->type = edge_type;
467 /* Skip any extra leading spaces */
468 field2 += strspn(field2, " ");
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");
477 /* Skip leading ' ' or '(' */
478 version += strspn(field2, " ");
479 version += strspn(version, "(");
480 /* Calculate length of any operator characters */
481 offset_ch = strspn(version, "<=>");
482 /* Determine operator */
484 if (strncmp(version, "=", offset_ch) == 0) {
485 edge->operator = VER_EQUAL;
487 else if (strncmp(version, "<<", offset_ch) == 0) {
488 edge->operator = VER_LESS;
490 else if (strncmp(version, "<=", offset_ch) == 0) {
491 edge->operator = VER_LESS_EQUAL;
493 else if (strncmp(version, ">>", offset_ch) == 0) {
494 edge->operator = VER_MORE;
496 else if (strncmp(version, ">=", offset_ch) == 0) {
497 edge->operator = VER_MORE_EQUAL;
499 bb_error_msg_and_die("Illegal operator\n");
502 /* skip to start of version numbers */
503 version += offset_ch;
504 version += strspn(version, " ");
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);
512 /* Get the dependency name */
513 field2[strcspn(field2, " (")] = '\0';
514 edge->name = search_name_hashtable(field2);
519 add_edge_to_node(parent_node, edge);
520 } while ((field2 = strtok_r(NULL, "|", &line_ptr2)) != NULL);
522 } while ((field = strtok_r(NULL, ",", &line_ptr1)) != NULL);
528 static void free_package(common_node_t *node)
532 for (i = 0; i < node->num_of_edges; i++) {
540 static unsigned int fill_package_struct(char *control_buffer)
542 static const char *const field_names[] = { "Package", "Version",
543 "Pre-Depends", "Depends","Replaces", "Provides",
544 "Conflicts", "Suggests", "Recommends", "Enhances", 0
547 common_node_t *new_node = (common_node_t *) xzalloc(sizeof(common_node_t));
552 int buffer_length = strlen(control_buffer);
554 new_node->version = search_name_hashtable("unknown");
555 while (field_start < buffer_length) {
556 unsigned short field_num;
558 field_start += read_package_field(&control_buffer[field_start],
559 &field_name, &field_value);
561 if (field_name == NULL) {
562 goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement ! */
565 field_num = compare_string_array(field_names, field_name);
567 case 0: /* Package */
568 new_node->name = search_name_hashtable(field_value);
570 case 1: /* Version */
571 new_node->version = search_name_hashtable(field_value);
573 case 2: /* Pre-Depends */
574 add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS);
576 case 3: /* Depends */
577 add_split_dependencies(new_node, field_value, EDGE_DEPENDS);
579 case 4: /* Replaces */
580 add_split_dependencies(new_node, field_value, EDGE_REPLACES);
582 case 5: /* Provides */
583 add_split_dependencies(new_node, field_value, EDGE_PROVIDES);
585 case 6: /* Conflicts */
586 add_split_dependencies(new_node, field_value, EDGE_CONFLICTS);
588 case 7: /* Suggests */
589 add_split_dependencies(new_node, field_value, EDGE_SUGGESTS);
591 case 8: /* Recommends */
592 add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS);
594 case 9: /* Enhances */
595 add_split_dependencies(new_node, field_value, EDGE_ENHANCES);
598 fill_package_struct_cleanup:
603 if (new_node->version == search_name_hashtable("unknown")) {
604 free_package(new_node);
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;
611 free_package(new_node);
616 /* if num = 1, it returns the want status, 2 returns flag, 3 returns status */
617 static unsigned int get_status(const unsigned int status_node, const int num)
619 char *status_string = name_hashtable[status_hashtable[status_node]->status];
620 char *state_sub_string;
621 unsigned int state_sub_num;
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, " ");
629 /* skip past the separating spaces */
630 status_string += strspn(status_string, " ");
632 len = strcspn(status_string, " \n\0");
633 state_sub_string = bb_xstrndup(status_string, len);
634 state_sub_num = search_name_hashtable(state_sub_string);
635 free(state_sub_string);
636 return(state_sub_num);
639 static void set_status(const unsigned int status_node_num, const char *new_value, const int position)
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]);
653 want = new_value_num;
654 want_len = new_value_len;
657 flag = new_value_num;
658 flag_len = new_value_len;
661 status = new_value_num;
662 status_len = new_value_len;
665 bb_error_msg_and_die("DEBUG ONLY: this shouldnt happen");
668 new_status = bb_xasprintf("%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]);
669 status_hashtable[status_node_num]->status = search_name_hashtable(new_status);
674 static const char *describe_status(int status_num) {
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";
679 status_want = get_status(status_num, 1);
680 status_state = get_status(status_num, 3);
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";
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";
695 return "is not installed or flagged to be installed";
699 static void index_status_file(const char *filename)
702 char *control_buffer;
704 status_node_t *status_node = NULL;
705 unsigned int status_num;
707 status_file = bb_xfopen(filename, "r");
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) {
716 status_line += strspn(status_line, " \n\t");
717 status_line = bb_xstrndup(status_line, strcspn(status_line, "\n\0"));
718 status_node->status = search_name_hashtable(status_line);
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;
725 free(control_buffer);
731 static void write_buffer_no_status(FILE *new_status_file, const char *control_buffer)
737 start += read_package_field(&control_buffer[start], &name, &value);
741 if (strcmp(name, "Status") != 0) {
742 fprintf(new_status_file, "%s: %s\n", name, value);
748 /* This could do with a cleanup */
749 static void write_status_file(deb_file_t **deb_file)
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");
754 char *status_from_file;
755 char *control_buffer = NULL;
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) {
769 tmp_string += strspn(tmp_string, " \n\t");
770 package_name = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
772 tmp_string = strstr(control_buffer, "Status:");
773 if (tmp_string != NULL) {
774 /* Seperate the status value from the control buffer */
776 tmp_string += strspn(tmp_string, " \n\t");
777 status_from_file = bb_xstrndup(tmp_string, strcspn(tmp_string, "\n"));
779 status_from_file = NULL;
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 */
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]);
805 /* This is temperary, debugging only */
806 if (deb_file[i] == NULL) {
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);
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);
818 field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
819 if (field_name == NULL) {
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);
828 fputs("\n", new_status_file);
830 else if (strcmp("config-files", name_hashtable[state_status]) == 0) {
831 /* only change the status line */
835 field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
836 if (field_name == NULL) {
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);
843 fprintf(new_status_file, "%s: %s\n", field_name, field_value);
847 fputs("\n", new_status_file);
851 /* If the package from the status file wasnt handle above, do it now*/
853 fprintf(new_status_file, "%s\n\n", control_buffer);
856 free(status_from_file);
858 free(control_buffer);
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]);
870 fclose(old_status_file);
871 fclose(new_status_file);
874 /* Create a separate backfile to dpkg */
875 if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) {
876 struct stat stat_buf;
877 xstat("/var/lib/dpkg/status", &stat_buf);
878 /* Its ok if renaming the status file fails because status
879 * file doesnt exist, maybe we are starting from scratch */
880 bb_error_msg("No status file found, creating new one");
883 if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) {
884 bb_error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file");
888 /* This function returns TRUE if the given package can satisfy a
889 * dependency of type depend_type.
891 * A pre-depends is satisfied only if a package is already installed,
892 * which a regular depends can be satisfied by a package which we want
895 static int package_satisfies_dependency(int package, int depend_type)
897 int status_num = search_status_hashtable(name_hashtable[package_hashtable[package]->name]);
899 /* status could be unknown if package is a pure virtual
900 * provides which cannot satisfy any dependency by itself.
902 if ( status_hashtable[status_num] == NULL )
905 switch (depend_type) {
906 case EDGE_PRE_DEPENDS: return get_status(status_num, 3) == search_name_hashtable("installed");
907 case EDGE_DEPENDS: return get_status(status_num, 1) == search_name_hashtable("install");
912 static int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count)
914 int *conflicts = NULL;
915 int conflicts_num = 0;
919 /* Check for conflicts
920 * TODO: TEST if conflicts with other packages to be installed
922 * Add install packages and the packages they provide
923 * to the list of files to check conflicts for
926 /* Create array of package numbers to check against
927 * installed package for conflicts*/
928 while (deb_file[i] != NULL) {
929 const unsigned int package_num = deb_file[i]->package;
930 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1));
931 conflicts[conflicts_num] = package_num;
933 /* add provides to conflicts list */
934 for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) {
935 if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) {
936 const int conflicts_package_num = search_package_hashtable(
937 package_hashtable[package_num]->edge[j]->name,
938 package_hashtable[package_num]->edge[j]->version,
939 package_hashtable[package_num]->edge[j]->operator);
940 if (package_hashtable[conflicts_package_num] == NULL) {
941 /* create a new package */
942 common_node_t *new_node = (common_node_t *) xzalloc(sizeof(common_node_t));
943 new_node->name = package_hashtable[package_num]->edge[j]->name;
944 new_node->version = package_hashtable[package_num]->edge[j]->version;
945 package_hashtable[conflicts_package_num] = new_node;
947 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1));
948 conflicts[conflicts_num] = conflicts_package_num;
955 /* Check conflicts */
957 while (deb_file[i] != NULL) {
958 const common_node_t *package_node = package_hashtable[deb_file[i]->package];
960 status_num = search_status_hashtable(name_hashtable[package_node->name]);
962 if (get_status(status_num, 3) == search_name_hashtable("installed")) {
967 for (j = 0; j < package_node->num_of_edges; j++) {
968 const edge_t *package_edge = package_node->edge[j];
970 if (package_edge->type == EDGE_CONFLICTS) {
971 const unsigned int package_num =
972 search_package_hashtable(package_edge->name,
973 package_edge->version,
974 package_edge->operator);
976 if (package_hashtable[package_num] != NULL) {
977 status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
979 if (get_status(status_num, 1) == search_name_hashtable("install")) {
980 result = test_version(package_hashtable[deb_file[i]->package]->version,
981 package_edge->version, package_edge->operator);
986 bb_error_msg_and_die("Package %s conflicts with %s",
987 name_hashtable[package_node->name],
988 name_hashtable[package_edge->name]);
996 /* Check dependendcies */
997 for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
999 int number_of_alternatives = 0;
1000 const edge_t * root_of_alternatives = NULL;
1001 const common_node_t *package_node = package_hashtable[i];
1003 /* If the package node does not exist then this
1004 * package is a virtual one. In which case there are
1005 * no dependencies to check.
1007 if ( package_node == NULL ) continue;
1009 status_num = search_status_hashtable(name_hashtable[package_node->name]);
1011 /* If there is no status then this package is a
1012 * virtual one provided by something else. In which
1013 * case there are no dependencies to check.
1015 if ( status_hashtable[status_num] == NULL ) continue;
1017 /* If we don't want this package installed then we may
1018 * as well ignore it's dependencies.
1020 if (get_status(status_num, 1) != search_name_hashtable("install")) {
1024 /* This code is tested only for EDGE_DEPENDS, since I
1025 * have no suitable pre-depends available. There is no
1026 * reason that it shouldn't work though :-)
1028 for (j = 0; j < package_node->num_of_edges; j++) {
1029 const edge_t *package_edge = package_node->edge[j];
1030 unsigned int package_num;
1032 if ( package_edge->type == EDGE_OR_PRE_DEPENDS ||
1033 package_edge->type == EDGE_OR_DEPENDS ) { /* start an EDGE_OR_ list */
1034 number_of_alternatives = package_edge->version;
1035 root_of_alternatives = package_edge;
1037 } else if ( number_of_alternatives == 0 ) { /* not in the middle of an EDGE_OR_ list */
1038 number_of_alternatives = 1;
1039 root_of_alternatives = NULL;
1042 package_num = search_package_hashtable(package_edge->name, package_edge->version, package_edge->operator);
1044 if (package_edge->type == EDGE_PRE_DEPENDS ||
1045 package_edge->type == EDGE_DEPENDS ) {
1049 /* If we are inside an alternative then check
1050 * this edge is the right type.
1052 * EDGE_DEPENDS == OR_DEPENDS -1
1053 * EDGE_PRE_DEPENDS == OR_PRE_DEPENDS -1
1055 if ( root_of_alternatives && package_edge->type != root_of_alternatives->type - 1)
1056 bb_error_msg_and_die("Fatal error. Package dependencies corrupt: %d != %d - 1 \n",
1057 package_edge->type, root_of_alternatives->type);
1059 if (package_hashtable[package_num] != NULL)
1060 result = !package_satisfies_dependency(package_num, package_edge->type);
1062 if (result) { /* check for other package which provide what we are looking for */
1065 while ( (provider = search_for_provides(package_edge->name, provider) ) > -1 ) {
1066 if ( package_hashtable[provider] == NULL ) {
1067 printf("Have a provider but no package information for it\n");
1070 result = !package_satisfies_dependency(provider, package_edge->type);
1077 /* It must be already installed, or to be installed */
1078 number_of_alternatives--;
1079 if (result && number_of_alternatives == 0) {
1080 if ( root_of_alternatives )
1081 bb_error_msg_and_die(
1082 "Package %s %sdepends on %s, "
1083 "which cannot be satisfied",
1084 name_hashtable[package_node->name],
1085 package_edge->type == EDGE_PRE_DEPENDS ? "pre-" : "",
1086 name_hashtable[root_of_alternatives->name]);
1088 bb_error_msg_and_die(
1089 "Package %s %sdepends on %s, which %s\n",
1090 name_hashtable[package_node->name],
1091 package_edge->type == EDGE_PRE_DEPENDS ? "pre-" : "",
1092 name_hashtable[package_edge->name],
1093 describe_status(status_num));
1094 } else if ( result == 0 && number_of_alternatives ) {
1095 /* we've found a package which
1096 * satisfies the dependency,
1097 * so skip over the rest of
1100 j += number_of_alternatives;
1101 number_of_alternatives = 0;
1110 static char **create_list(const char *filename)
1113 char **file_list = NULL;
1117 /* don't use [xw]fopen here, handle error ourself */
1118 list_stream = fopen(filename, "r");
1119 if (list_stream == NULL) {
1123 while ((line = bb_get_chomped_line_from_file(list_stream)) != NULL) {
1124 file_list = xrealloc(file_list, sizeof(char *) * (count + 2));
1125 file_list[count] = line;
1128 fclose(list_stream);
1133 file_list[count] = NULL;
1138 /* maybe i should try and hook this into remove_file.c somehow */
1139 static int remove_file_array(char **remove_names, char **exclude_names)
1141 struct stat path_stat;
1143 int remove_flag = FALSE;
1146 if (remove_names == NULL) {
1149 for (i = 0; remove_names[i] != NULL; i++) {
1151 if (exclude_names != NULL) {
1152 for (j = 0; exclude_names[j] != 0; j++) {
1153 if (strcmp(remove_names[i], exclude_names[j]) == 0) {
1160 if (lstat(remove_names[i], &path_stat) < 0) {
1163 if (S_ISDIR(path_stat.st_mode)) {
1164 if (rmdir(remove_names[i]) != -1) {
1168 if (unlink(remove_names[i]) != -1) {
1174 return(remove_flag);
1177 static int run_package_script(const char *package_name, const char *script_type)
1179 struct stat path_stat;
1183 script_path = bb_xasprintf("/var/lib/dpkg/info/%s.%s", package_name, script_type);
1185 /* If the file doesnt exist is isnt a fatal */
1186 result = lstat(script_path, &path_stat) < 0 ? EXIT_SUCCESS : system(script_path);
1191 static const char *all_control_files[] = {"preinst", "postinst", "prerm", "postrm",
1192 "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL };
1194 static char **all_control_list(const char *package_name)
1196 unsigned short i = 0;
1197 char **remove_files;
1199 /* Create a list of all /var/lib/dpkg/info/<package> files */
1200 remove_files = xzalloc(sizeof(all_control_files));
1201 while (all_control_files[i]) {
1202 remove_files[i] = bb_xasprintf("/var/lib/dpkg/info/%s.%s", package_name, all_control_files[i]);
1206 return(remove_files);
1209 static void free_array(char **array)
1213 unsigned short i = 0;
1222 /* This function lists information on the installed packages. It loops through
1223 * the status_hashtable to retrieve the info. This results in smaller code than
1224 * scanning the status file. The resulting list, however, is unsorted.
1226 static void list_packages(void)
1230 printf(" Name Version\n");
1231 printf("+++-==============-==============\n");
1233 /* go through status hash, dereference package hash and finally strings */
1234 for (i=0; i<STATUS_HASH_PRIME+1; i++) {
1236 if (status_hashtable[i]) {
1237 const char *stat_str; /* status string */
1238 const char *name_str; /* package name */
1239 const char *vers_str; /* version */
1240 char s1, s2; /* status abbreviations */
1241 int spccnt; /* space count */
1244 stat_str = name_hashtable[status_hashtable[i]->status];
1245 name_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->name];
1246 vers_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->version];
1248 /* get abbreviation for status field 1 */
1249 s1 = stat_str[0] == 'i' ? 'i' : 'r';
1251 /* get abbreviation for status field 2 */
1252 for (j=0, spccnt=0; stat_str[j] && spccnt<2; j++) {
1253 if (stat_str[j] == ' ') spccnt++;
1257 /* print out the line formatted like Debian dpkg */
1258 printf("%c%c %-14s %s\n", s1, s2, name_str, vers_str);
1263 static void remove_package(const unsigned int package_num, int noisy)
1265 const char *package_name = name_hashtable[package_hashtable[package_num]->name];
1266 const char *package_version = name_hashtable[package_hashtable[package_num]->version];
1267 const unsigned int status_num = search_status_hashtable(package_name);
1268 const int package_name_length = strlen(package_name);
1269 char **remove_files;
1270 char **exclude_files;
1271 char list_name[package_name_length + 25];
1272 char conffile_name[package_name_length + 30];
1276 printf("Removing %s (%s) ...\n", package_name, package_version);
1278 /* run prerm script */
1279 return_value = run_package_script(package_name, "prerm");
1280 if (return_value == -1) {
1281 bb_error_msg_and_die("script failed, prerm failure");
1284 /* Create a list of files to remove, and a separate list of those to keep */
1285 sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name);
1286 remove_files = create_list(list_name);
1288 sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name);
1289 exclude_files = create_list(conffile_name);
1291 /* Some directories cant be removed straight away, so do multiple passes */
1292 while (remove_file_array(remove_files, exclude_files));
1293 free_array(exclude_files);
1294 free_array(remove_files);
1296 /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */
1297 exclude_files = xzalloc(sizeof(char*) * 3);
1298 exclude_files[0] = bb_xstrdup(conffile_name);
1299 exclude_files[1] = bb_xasprintf("/var/lib/dpkg/info/%s.postrm", package_name);
1301 /* Create a list of all /var/lib/dpkg/info/<package> files */
1302 remove_files = all_control_list(package_name);
1304 remove_file_array(remove_files, exclude_files);
1305 free_array(remove_files);
1306 free_array(exclude_files);
1308 /* rename <package>.conffile to <package>.list */
1309 rename(conffile_name, list_name);
1311 /* Change package status */
1312 set_status(status_num, "config-files", 3);
1315 static void purge_package(const unsigned int package_num)
1317 const char *package_name = name_hashtable[package_hashtable[package_num]->name];
1318 const char *package_version = name_hashtable[package_hashtable[package_num]->version];
1319 const unsigned int status_num = search_status_hashtable(package_name);
1320 char **remove_files;
1321 char **exclude_files;
1322 char list_name[strlen(package_name) + 25];
1324 printf("Purging %s (%s) ...\n", package_name, package_version);
1326 /* run prerm script */
1327 if (run_package_script(package_name, "prerm") != 0) {
1328 bb_error_msg_and_die("script failed, prerm failure");
1331 /* Create a list of files to remove */
1332 sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name);
1333 remove_files = create_list(list_name);
1335 exclude_files = xzalloc(sizeof(char*));
1337 /* Some directories cant be removed straight away, so do multiple passes */
1338 while (remove_file_array(remove_files, exclude_files));
1339 free_array(remove_files);
1341 /* Create a list of all /var/lib/dpkg/info/<package> files */
1342 remove_files = all_control_list(package_name);
1343 remove_file_array(remove_files, exclude_files);
1344 free_array(remove_files);
1345 free(exclude_files);
1347 /* run postrm script */
1348 if (run_package_script(package_name, "postrm") == -1) {
1349 bb_error_msg_and_die("postrm fialure.. set status to what?");
1352 /* Change package status */
1353 set_status(status_num, "not-installed", 3);
1356 static archive_handle_t *init_archive_deb_ar(const char *filename)
1358 archive_handle_t *ar_handle;
1360 /* Setup an ar archive handle that refers to the gzip sub archive */
1361 ar_handle = init_handle();
1362 ar_handle->filter = filter_accept_list_reassign;
1363 ar_handle->src_fd = bb_xopen(filename, O_RDONLY);
1368 static void init_archive_deb_control(archive_handle_t *ar_handle)
1370 archive_handle_t *tar_handle;
1372 /* Setup the tar archive handle */
1373 tar_handle = init_handle();
1374 tar_handle->src_fd = ar_handle->src_fd;
1376 /* We don't care about data.tar.* or debian-binary, just control.tar.* */
1377 #ifdef CONFIG_FEATURE_DEB_TAR_GZ
1378 llist_add_to(&(ar_handle->accept), "control.tar.gz");
1380 #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
1381 llist_add_to(&(ar_handle->accept), "control.tar.bz2");
1384 /* Assign the tar handle as a subarchive of the ar handle */
1385 ar_handle->sub_archive = tar_handle;
1390 static void init_archive_deb_data(archive_handle_t *ar_handle)
1392 archive_handle_t *tar_handle;
1394 /* Setup the tar archive handle */
1395 tar_handle = init_handle();
1396 tar_handle->src_fd = ar_handle->src_fd;
1398 /* We don't care about control.tar.* or debian-binary, just data.tar.* */
1399 #ifdef CONFIG_FEATURE_DEB_TAR_GZ
1400 llist_add_to(&(ar_handle->accept), "data.tar.gz");
1402 #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
1403 llist_add_to(&(ar_handle->accept), "data.tar.bz2");
1406 /* Assign the tar handle as a subarchive of the ar handle */
1407 ar_handle->sub_archive = tar_handle;
1412 static char *deb_extract_control_file_to_buffer(archive_handle_t *ar_handle, llist_t *myaccept)
1414 ar_handle->sub_archive->action_data = data_extract_to_buffer;
1415 ar_handle->sub_archive->accept = myaccept;
1416 ar_handle->sub_archive->filter = filter_accept_list;
1418 unpack_ar_archive(ar_handle);
1419 close(ar_handle->src_fd);
1421 return(ar_handle->sub_archive->buffer);
1424 static void data_extract_all_prefix(archive_handle_t *archive_handle)
1426 char *name_ptr = archive_handle->file_header->name;
1428 name_ptr += strspn(name_ptr, "./");
1429 if (name_ptr[0] != '\0') {
1430 archive_handle->file_header->name = bb_xasprintf("%s%s", archive_handle->buffer, name_ptr);
1431 data_extract_all(archive_handle);
1436 static void unpack_package(deb_file_t *deb_file)
1438 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
1439 const unsigned int status_num = search_status_hashtable(package_name);
1440 const unsigned int status_package_num = status_hashtable[status_num]->package;
1442 archive_handle_t *archive_handle;
1444 llist_t *accept_list = NULL;
1447 /* If existing version, remove it first */
1448 if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) {
1449 /* Package is already installed, remove old version first */
1450 printf("Preparing to replace %s %s (using %s) ...\n", package_name,
1451 name_hashtable[package_hashtable[status_package_num]->version],
1452 deb_file->filename);
1453 remove_package(status_package_num, 0);
1455 printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename);
1458 /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */
1459 info_prefix = bb_xasprintf("/var/lib/dpkg/info/%s.", package_name);
1460 archive_handle = init_archive_deb_ar(deb_file->filename);
1461 init_archive_deb_control(archive_handle);
1463 while(all_control_files[i]) {
1464 char *c = bb_xasprintf("./%s", all_control_files[i]);
1465 llist_add_to(&accept_list, c);
1468 archive_handle->sub_archive->accept = accept_list;
1469 archive_handle->sub_archive->filter = filter_accept_list;
1470 archive_handle->sub_archive->action_data = data_extract_all_prefix;
1471 archive_handle->sub_archive->buffer = info_prefix;
1472 archive_handle->sub_archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
1473 unpack_ar_archive(archive_handle);
1475 /* Run the preinst prior to extracting */
1476 if (run_package_script(package_name, "preinst") != 0) {
1477 /* when preinst returns exit code != 0 then quit installation process */
1478 bb_error_msg_and_die("subprocess pre-installation script returned error.");
1481 /* Extract data.tar.gz to the root directory */
1482 archive_handle = init_archive_deb_ar(deb_file->filename);
1483 init_archive_deb_data(archive_handle);
1484 archive_handle->sub_archive->action_data = data_extract_all_prefix;
1485 archive_handle->sub_archive->buffer = "/";
1486 archive_handle->sub_archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
1487 unpack_ar_archive(archive_handle);
1489 /* Create the list file */
1490 strcat(info_prefix, "list");
1491 out_stream = bb_xfopen(info_prefix, "w");
1492 while (archive_handle->sub_archive->passed) {
1493 /* the leading . has been stripped by data_extract_all_prefix already */
1494 fputs(archive_handle->sub_archive->passed->data, out_stream);
1495 fputc('\n', out_stream);
1496 archive_handle->sub_archive->passed = archive_handle->sub_archive->passed->link;
1501 set_status(status_num, "install", 1);
1502 set_status(status_num, "unpacked", 3);
1507 static void configure_package(deb_file_t *deb_file)
1509 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
1510 const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version];
1511 const int status_num = search_status_hashtable(package_name);
1513 printf("Setting up %s (%s) ...\n", package_name, package_version);
1515 /* Run the postinst script */
1516 if (run_package_script(package_name, "postinst") != 0) {
1517 /* TODO: handle failure gracefully */
1518 bb_error_msg_and_die("postrm failure.. set status to what?");
1520 /* Change status to reflect success */
1521 set_status(status_num, "install", 1);
1522 set_status(status_num, "installed", 3);
1525 int dpkg_main(int argc, char **argv)
1527 deb_file_t **deb_file = NULL;
1528 status_node_t *status_node;
1537 while ((opt = getopt(argc, argv, "CF:ilPru")) != -1) {
1539 case 'C': // equivalent to --configure in official dpkg
1540 dpkg_opt |= dpkg_opt_configure;
1541 dpkg_opt |= dpkg_opt_package_name;
1543 case 'F': // equivalent to --force in official dpkg
1544 if (strcmp(optarg, "depends") == 0) {
1545 dpkg_opt |= dpkg_opt_force_ignore_depends;
1549 dpkg_opt |= dpkg_opt_install;
1550 dpkg_opt |= dpkg_opt_filename;
1553 dpkg_opt |= dpkg_opt_list_installed;
1556 dpkg_opt |= dpkg_opt_purge;
1557 dpkg_opt |= dpkg_opt_package_name;
1560 dpkg_opt |= dpkg_opt_remove;
1561 dpkg_opt |= dpkg_opt_package_name;
1563 case 'u': /* Equivalent to --unpack in official dpkg */
1564 dpkg_opt |= dpkg_opt_unpack;
1565 dpkg_opt |= dpkg_opt_filename;
1571 /* check for non-otion argument if expected */
1572 if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) {
1576 /* puts("(Reading database ... xxxxx files and directories installed.)"); */
1577 index_status_file("/var/lib/dpkg/status");
1579 /* if the list action was given print the installed packages and exit */
1580 if (dpkg_opt & dpkg_opt_list_installed) {
1582 return(EXIT_SUCCESS);
1585 /* Read arguments and store relevant info in structs */
1586 while (optind < argc) {
1587 /* deb_count = nb_elem - 1 and we need nb_elem + 1 to allocate terminal node [NULL pointer] */
1588 deb_file = xrealloc(deb_file, sizeof(deb_file_t *) * (deb_count + 2));
1589 deb_file[deb_count] = (deb_file_t *) xzalloc(sizeof(deb_file_t));
1590 if (dpkg_opt & dpkg_opt_filename) {
1591 archive_handle_t *archive_handle;
1592 llist_t *control_list = NULL;
1594 /* Extract the control file */
1595 llist_add_to(&control_list, "./control");
1596 archive_handle = init_archive_deb_ar(argv[optind]);
1597 init_archive_deb_control(archive_handle);
1598 deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list);
1599 if (deb_file[deb_count]->control_file == NULL) {
1600 bb_error_msg_and_die("Couldnt extract control file");
1602 deb_file[deb_count]->filename = bb_xstrdup(argv[optind]);
1603 package_num = fill_package_struct(deb_file[deb_count]->control_file);
1605 if (package_num == -1) {
1606 bb_error_msg("Invalid control file in %s", argv[optind]);
1610 deb_file[deb_count]->package = (unsigned int) package_num;
1612 /* Add the package to the status hashtable */
1613 if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) {
1614 /* Try and find a currently installed version of this package */
1615 status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1616 /* If no previous entry was found initialise a new entry */
1617 if ((status_hashtable[status_num] == NULL) ||
1618 (status_hashtable[status_num]->status == 0)) {
1619 status_node = (status_node_t *) xmalloc(sizeof(status_node_t));
1620 status_node->package = deb_file[deb_count]->package;
1621 /* reinstreq isnt changed to "ok" until the package control info
1622 * is written to the status file*/
1623 status_node->status = search_name_hashtable("install reinstreq not-installed");
1624 status_hashtable[status_num] = status_node;
1626 set_status(status_num, "install", 1);
1627 set_status(status_num, "reinstreq", 2);
1631 else if (dpkg_opt & dpkg_opt_package_name) {
1632 deb_file[deb_count]->package = search_package_hashtable(
1633 search_name_hashtable(argv[optind]),
1634 search_name_hashtable("ANY"), VER_ANY);
1635 if (package_hashtable[deb_file[deb_count]->package] == NULL) {
1636 bb_error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]);
1638 package_num = deb_file[deb_count]->package;
1639 status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
1640 state_status = get_status(status_num, 3);
1642 /* check package status is "installed" */
1643 if (dpkg_opt & dpkg_opt_remove) {
1644 if ((strcmp(name_hashtable[state_status], "not-installed") == 0) ||
1645 (strcmp(name_hashtable[state_status], "config-files") == 0)) {
1646 bb_error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[package_num]->name]);
1648 set_status(status_num, "deinstall", 1);
1650 else if (dpkg_opt & dpkg_opt_purge) {
1651 /* if package status is "conf-files" then its ok */
1652 if (strcmp(name_hashtable[state_status], "not-installed") == 0) {
1653 bb_error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[package_num]->name]);
1655 set_status(status_num, "purge", 1);
1661 deb_file[deb_count] = NULL;
1663 /* Check that the deb file arguments are installable */
1664 if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) {
1665 if (!check_deps(deb_file, 0, deb_count)) {
1666 bb_error_msg_and_die("Dependency check failed");
1670 /* TODO: install or remove packages in the correct dependency order */
1671 for (i = 0; i < deb_count; i++) {
1672 /* Remove or purge packages */
1673 if (dpkg_opt & dpkg_opt_remove) {
1674 remove_package(deb_file[i]->package, 1);
1676 else if (dpkg_opt & dpkg_opt_purge) {
1677 purge_package(deb_file[i]->package);
1679 else if (dpkg_opt & dpkg_opt_unpack) {
1680 unpack_package(deb_file[i]);
1682 else if (dpkg_opt & dpkg_opt_install) {
1683 unpack_package(deb_file[i]);
1684 /* package is configured in second pass below */
1686 else if (dpkg_opt & dpkg_opt_configure) {
1687 configure_package(deb_file[i]);
1690 /* configure installed packages */
1691 if (dpkg_opt & dpkg_opt_install) {
1692 for (i = 0; i < deb_count; i++)
1693 configure_package(deb_file[i]);
1696 write_status_file(deb_file);
1698 for (i = 0; i < deb_count; i++) {
1699 free(deb_file[i]->control_file);
1700 free(deb_file[i]->filename);
1706 for (i = 0; i < NAME_HASH_PRIME; i++) {
1707 free(name_hashtable[i]);
1710 for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
1711 if (package_hashtable[i] != NULL) {
1712 free_package(package_hashtable[i]);
1716 for (i = 0; i < STATUS_HASH_PRIME; i++) {
1717 free(status_hashtable[i]);
1720 return(EXIT_SUCCESS);