2 * Mini dpkg implementation for busybox.
3 * This is not meant as a replacemnt 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 * 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.
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.
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.
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
36 * Bugs that need to be fixed
37 * - (unknown, please let me know when you find any)
46 #include "unarchive.h"
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
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];
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;
79 unsigned int version:14;
82 typedef struct common_node_s {
84 unsigned int version:14;
85 unsigned int num_of_edges:14;
88 common_node_t *package_hashtable[PACKAGE_HASH_PRIME + 1];
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 */
98 status_node_t *status_hashtable[STATUS_HASH_PRIME + 1];
100 /* Even numbers are for 'extras', like ored dependecies or null */
103 EDGE_PRE_DEPENDS = 1,
104 EDGE_OR_PRE_DEPENDS = 2,
111 EDGE_RECOMMENDS = 13,
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
136 typedef struct deb_file_s {
139 unsigned int package:14;
143 void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime)
145 unsigned long int hash_num = key[0];
146 int len = strlen(key);
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
156 hash_num += ((key[i] + key[i-1]) << ((key[i] * i) % 24));
158 *start = (unsigned int) hash_num % hash_prime;
159 *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1));
162 /* this adds the key to the hash table */
163 int search_name_hashtable(const char *key)
165 unsigned int probe_address = 0;
166 unsigned int probe_decrement = 0;
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);
174 probe_address -= probe_decrement;
175 if ((int)probe_address < 0) {
176 probe_address += NAME_HASH_PRIME;
180 name_hashtable[probe_address] = xstrdup(key);
181 return(probe_address);
184 /* this DOESNT add the key to the hashtable
185 * TODO make it consistent with search_name_hashtable
187 unsigned int search_status_hashtable(const char *key)
189 unsigned int probe_address = 0;
190 unsigned int probe_decrement = 0;
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) {
197 probe_address -= probe_decrement;
198 if ((int)probe_address < 0) {
199 probe_address += STATUS_HASH_PRIME;
203 return(probe_address);
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)
209 int upstream_len1 = 0;
210 int upstream_len2 = 0;
220 if (version1 == NULL) {
221 version1 = xstrdup("");
223 if (version2 == NULL) {
224 version2 = xstrdup("");
226 upstream_len1 = strlen(version1);
227 upstream_len2 = strlen(version2);
229 while ((len1 < upstream_len1) || (len2 < upstream_len2)) {
230 /* Compare non-digit section */
231 tmp_int = strcspn(&version1[len1], "0123456789");
232 name1_char = xstrndup(&version1[len1], tmp_int);
234 tmp_int = strcspn(&version2[len2], "0123456789");
235 name2_char = xstrndup(&version2[len2], tmp_int);
237 tmp_int = strcmp(name1_char, name2_char);
242 goto cleanup_version_compare_part;
246 tmp_int = strspn(&version1[len1], "0123456789");
247 name1_char = xstrndup(&version1[len1], tmp_int);
249 tmp_int = strspn(&version2[len2], "0123456789");
250 name2_char = xstrndup(&version2[len2], tmp_int);
252 ver_num1 = atoi(name1_char);
253 ver_num2 = atoi(name2_char);
256 if (ver_num1 < ver_num2) {
258 goto cleanup_version_compare_part;
260 else if (ver_num1 > ver_num2) {
262 goto cleanup_version_compare_part;
266 cleanup_version_compare_part:
270 /* if ver1 < ver2 return -1,
271 * if ver1 = ver2 return 0,
272 * if ver1 > ver2 return 1,
274 int version_compare(const unsigned int ver1, const unsigned int ver2)
276 char *ch_ver1 = name_hashtable[ver1];
277 char *ch_ver2 = name_hashtable[ver2];
280 char *deb_ver1, *deb_ver2;
281 char *ver1_ptr, *ver2_ptr;
287 if (ch_ver1[1] == ':') {
289 ver1_ptr = strchr(ch_ver1, ':') + 1;
294 if (ch_ver2[1] == ':') {
296 ver2_ptr = strchr(ch_ver2, ':') + 1;
301 if (epoch1 < epoch2) {
304 else if (epoch1 > epoch2) {
308 /* Compare upstream version */
309 upstream_ver1 = xstrdup(ver1_ptr);
310 upstream_ver2 = xstrdup(ver2_ptr);
312 /* Chop off debian version, and store for later use */
313 deb_ver1 = strrchr(upstream_ver1, '-');
314 deb_ver2 = strrchr(upstream_ver2, '-');
323 result = version_compare_part(upstream_ver1, upstream_ver2);
332 /* Compare debian versions */
333 return(version_compare_part(deb_ver1, deb_ver2));
336 int test_version(const unsigned int version1, const unsigned int version2, const unsigned int operator)
338 const int version_result = version_compare(version1, version2);
343 if (version_result == 0) {
348 if (version_result < 0) {
352 case (VER_LESS_EQUAL):
353 if (version_result <= 0) {
358 if (version_result > 0) {
362 case (VER_MORE_EQUAL):
363 if (version_result >= 0) {
372 int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator)
374 unsigned int probe_address = 0;
375 unsigned int probe_decrement = 0;
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);
383 if (test_version(package_hashtable[probe_address]->version, version, operator)) {
384 return(probe_address);
387 probe_address -= probe_decrement;
388 if ((int)probe_address < 0) {
389 probe_address += PACKAGE_HASH_PRIME;
392 return(probe_address);
396 * Create one new node and one new edge for every dependency.
398 void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type)
400 char *line = xstrdup(whole_line);
402 char *line_ptr1 = NULL;
403 char *line_ptr2 = NULL;
411 field = strtok_r(line, ",", &line_ptr1);
413 line2 = xstrdup(field);
414 field2 = strtok_r(line2, "|", &line_ptr2);
415 if ((edge_type == EDGE_DEPENDS) && (strcmp(field, field2) != 0)) {
416 type = EDGE_OR_DEPENDS;
418 else if ((edge_type == EDGE_PRE_DEPENDS) && (strcmp(field, field2) != 0)) {
419 type = EDGE_OR_PRE_DEPENDS;
425 edge = (edge_t *) xmalloc(sizeof(edge_t));
428 /* Skip any extra leading spaces */
429 field2 += strspn(field2, " ");
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");
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 */
445 if (strncmp(version, "=", offset_ch) == 0) {
446 edge->operator = VER_EQUAL;
448 else if (strncmp(version, "<<", offset_ch) == 0) {
449 edge->operator = VER_LESS;
451 else if (strncmp(version, "<=", offset_ch) == 0) {
452 edge->operator = VER_LESS_EQUAL;
454 else if (strncmp(version, ">>", offset_ch) == 0) {
455 edge->operator = VER_MORE;
457 else if (strncmp(version, ">=", offset_ch) == 0) {
458 edge->operator = VER_MORE_EQUAL;
460 error_msg_and_die("Illegal operator\n");
463 /* skip to start of version numbers */
464 version += offset_ch;
465 version += strspn(version, " ");
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);
473 /* Get the dependency name */
474 field2[strcspn(field2, " (")] = '\0';
475 edge->name = search_name_hashtable(field2);
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);
483 } while ((field = strtok_r(NULL, ",", &line_ptr1)) != NULL);
489 void free_package(common_node_t *node)
493 for (i = 0; i < node->num_of_edges; i++) {
501 /* returns the array number of the string */
502 static unsigned short compare_string_array(const char *string_array[], const char *key)
506 for (i = 0; string_array[i] != 0; i++) {
507 if (strcmp(string_array[i], key) == 0) {
514 unsigned int fill_package_struct(char *control_buffer)
516 common_node_t *new_node = (common_node_t *) xcalloc(1, sizeof(common_node_t));
517 const char *field_names[] = { "Package", "Version", "Pre-Depends", "Depends",
518 "Replaces", "Provides", "Conflicts", "Suggests", "Recommends", "Enhances", 0};
523 int buffer_length = strlen(control_buffer);
525 new_node->version = search_name_hashtable("unknown");
526 while (field_start < buffer_length) {
527 unsigned short field_num;
529 field_start += read_package_field(&control_buffer[field_start],
530 &field_name, &field_value);
532 if (field_name == NULL) {
533 goto fill_package_struct_cleanup; /* Oh no, the dreaded goto statement ! */
536 field_num = compare_string_array(field_names, field_name);
538 case 0: /* Package */
539 new_node->name = search_name_hashtable(field_value);
541 case 1: /* Version */
542 new_node->version = search_name_hashtable(field_value);
544 case 2: /* Pre-Depends */
545 add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS);
547 case 3: /* Depends */
548 add_split_dependencies(new_node, field_value, EDGE_DEPENDS);
550 case 4: /* Replaces */
551 add_split_dependencies(new_node, field_value, EDGE_REPLACES);
553 case 5: /* Provides */
554 add_split_dependencies(new_node, field_value, EDGE_PROVIDES);
556 case 6: /* Conflicts */
557 add_split_dependencies(new_node, field_value, EDGE_CONFLICTS);
559 case 7: /* Suggests */
560 add_split_dependencies(new_node, field_value, EDGE_SUGGESTS);
562 case 8: /* Recommends */
563 add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS);
565 case 9: /* Enhances */
566 add_split_dependencies(new_node, field_value, EDGE_ENHANCES);
569 fill_package_struct_cleanup:
574 if (new_node->version == search_name_hashtable("unknown")) {
575 free_package(new_node);
578 num = search_package_hashtable(new_node->name, new_node->version, VER_EQUAL);
579 if (package_hashtable[num] == NULL) {
580 package_hashtable[num] = new_node;
582 free_package(new_node);
587 /* if num = 1, it returns the want status, 2 returns flag, 3 returns status */
588 unsigned int get_status(const unsigned int status_node, const int num)
590 char *status_string = name_hashtable[status_hashtable[status_node]->status];
591 char *state_sub_string;
592 unsigned int state_sub_num;
596 /* set tmp_string to point to the start of the word number */
597 for (i = 1; i < num; i++) {
598 /* skip past a word */
599 status_string += strcspn(status_string, " ");
600 /* skip past the seperating spaces */
601 status_string += strspn(status_string, " ");
603 len = strcspn(status_string, " \n\0");
604 state_sub_string = xstrndup(status_string, len);
605 state_sub_num = search_name_hashtable(state_sub_string);
606 free(state_sub_string);
607 return(state_sub_num);
610 void set_status(const unsigned int status_node_num, const char *new_value, const int position)
612 const unsigned int new_value_len = strlen(new_value);
613 const unsigned int new_value_num = search_name_hashtable(new_value);
614 unsigned int want = get_status(status_node_num, 1);
615 unsigned int flag = get_status(status_node_num, 2);
616 unsigned int status = get_status(status_node_num, 3);
617 int want_len = strlen(name_hashtable[want]);
618 int flag_len = strlen(name_hashtable[flag]);
619 int status_len = strlen(name_hashtable[status]);
624 want = new_value_num;
625 want_len = new_value_len;
628 flag = new_value_num;
629 flag_len = new_value_len;
632 status = new_value_num;
633 status_len = new_value_len;
636 error_msg_and_die("DEBUG ONLY: this shouldnt happen");
639 new_status = (char *) xmalloc(want_len + flag_len + status_len + 3);
640 sprintf(new_status, "%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]);
641 status_hashtable[status_node_num]->status = search_name_hashtable(new_status);
646 void index_status_file(const char *filename)
649 char *control_buffer;
651 status_node_t *status_node = NULL;
652 unsigned int status_num;
654 status_file = xfopen(filename, "r");
655 while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) {
656 const unsigned int package_num = fill_package_struct(control_buffer);
657 if (package_num != -1) {
658 status_node = xmalloc(sizeof(status_node_t));
659 /* fill_package_struct doesnt handle the status field */
660 status_line = strstr(control_buffer, "Status:");
661 if (status_line != NULL) {
663 status_line += strspn(status_line, " \n\t");
664 status_line = xstrndup(status_line, strcspn(status_line, "\n\0"));
665 status_node->status = search_name_hashtable(status_line);
668 status_node->package = package_num;
669 status_num = search_status_hashtable(name_hashtable[package_hashtable[status_node->package]->name]);
670 status_hashtable[status_num] = status_node;
672 free(control_buffer);
679 char *get_depends_field(common_node_t *package, const int depends_type)
681 char *depends = NULL;
682 char *old_sep = (char *)xcalloc(1, 3);
683 char *new_sep = (char *)xcalloc(1, 3);
689 for (i = 0; i < package->num_of_edges; i++) {
690 if ((package->edge[i]->type == EDGE_OR_PRE_DEPENDS) ||
691 (package->edge[i]->type == EDGE_OR_DEPENDS)) {
694 if ((package->edge[i]->type == depends_type) ||
695 (package->edge[i]->type == depends_type + 1)) {
696 /* Check if its the first time through */
698 depends_size = 8 + strlen(name_hashtable[package->edge[i]->name])
699 + strlen(name_hashtable[package->edge[i]->version]);
700 line_size += depends_size;
701 depends = (char *) xrealloc(depends, line_size + 1);
703 /* Check to see if this dependency is the type we are looking for
704 * +1 to check for 'extra' types, e.g. ored dependecies */
705 strcpy(old_sep, new_sep);
706 if (package->edge[i]->type == depends_type) {
707 strcpy(new_sep, ", ");
709 else if (package->edge[i]->type == depends_type + 1) {
710 strcpy(new_sep, "| ");
713 if (depends_size == line_size) {
716 if ((strcmp(old_sep, "| ") == 0) && (strcmp(new_sep, "| ") == 0)) {
717 strcat(depends, " | ");
719 strcat(depends, ", ");
723 strcat(depends, name_hashtable[package->edge[i]->name]);
724 if (strcmp(name_hashtable[package->edge[i]->version], "NULL") != 0) {
725 if (package->edge[i]->operator == VER_EQUAL) {
726 strcat(depends, " (= ");
728 else if (package->edge[i]->operator == VER_LESS) {
729 strcat(depends, " (<< ");
731 else if (package->edge[i]->operator == VER_LESS_EQUAL) {
732 strcat(depends, " (<= ");
734 else if (package->edge[i]->operator == VER_MORE) {
735 strcat(depends, " (>> ");
737 else if (package->edge[i]->operator == VER_MORE_EQUAL) {
738 strcat(depends, " (>= ");
740 strcat(depends, " (");
742 strcat(depends, name_hashtable[package->edge[i]->version]);
743 strcat(depends, ")");
750 void write_buffer_no_status(FILE *new_status_file, const char *control_buffer)
756 start += read_package_field(&control_buffer[start], &name, &value);
760 if (strcmp(name, "Status") != 0) {
761 fprintf(new_status_file, "%s: %s\n", name, value);
767 /* This could do with a cleanup */
768 void write_status_file(deb_file_t **deb_file)
770 FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r");
771 FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w");
773 char *status_from_file;
774 char *control_buffer = NULL;
781 /* Update previously known packages */
782 while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) {
783 if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) {
788 tmp_string += strspn(tmp_string, " \n\t");
789 package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
791 tmp_string = strstr(control_buffer, "Status:");
792 if (tmp_string != NULL) {
793 /* Seperate the status value from the control buffer */
795 tmp_string += strspn(tmp_string, " \n\t");
796 status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n"));
798 status_from_file = NULL;
801 /* Find this package in the status hashtable */
802 status_num = search_status_hashtable(package_name);
803 if (status_hashtable[status_num] != NULL) {
804 const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status];
805 if (strcmp(status_from_file, status_from_hashtable) != 0) {
806 /* New status isnt exactly the same as old status */
807 const int state_status = get_status(status_num, 3);
808 if ((strcmp("installed", name_hashtable[state_status]) == 0) ||
809 (strcmp("unpacked", name_hashtable[state_status]) == 0)) {
810 /* We need to add the control file from the package */
812 while(deb_file[i] != NULL) {
813 if (strcmp(package_name, name_hashtable[package_hashtable[deb_file[i]->package]->name]) == 0) {
814 /* Write a status file entry with a modified status */
815 /* remove trailing \n's */
816 write_buffer_no_status(new_status_file, deb_file[i]->control_file);
817 set_status(status_num, "ok", 2);
818 fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]);
824 /* This is temperary, debugging only */
825 if (deb_file[i] == NULL) {
826 error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name);
829 else if (strcmp("not-installed", name_hashtable[state_status]) == 0) {
830 /* Only write the Package, Status, Priority and Section lines */
831 fprintf(new_status_file, "Package: %s\n", package_name);
832 fprintf(new_status_file, "Status: %s\n", status_from_hashtable);
837 field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
838 if (field_name == NULL) {
841 if ((strcmp(field_name, "Priority") == 0) ||
842 (strcmp(field_name, "Section") == 0)) {
843 fprintf(new_status_file, "%s: %s\n", field_name, field_value);
847 fputs("\n", new_status_file);
849 else if (strcmp("config-files", name_hashtable[state_status]) == 0) {
850 /* only change the status line */
854 field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
855 if (field_name == NULL) {
858 /* Setup start point for next field */
859 if (strcmp(field_name, "Status") == 0) {
860 fprintf(new_status_file, "Status: %s\n", status_from_hashtable);
862 fprintf(new_status_file, "%s: %s\n", field_name, field_value);
866 fputs("\n", new_status_file);
870 /* If the package from the status file wasnt handle above, do it now*/
872 fprintf(new_status_file, "%s\n\n", control_buffer);
875 free(status_from_file);
877 free(control_buffer);
880 /* Write any new packages */
881 for(i = 0; deb_file[i] != NULL; i++) {
882 status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[i]->package]->name]);
883 if (strcmp("reinstreq", name_hashtable[get_status(status_num, 2)]) == 0) {
884 write_buffer_no_status(new_status_file, deb_file[i]->control_file);
885 set_status(status_num, "ok", 2);
886 fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]);
889 fclose(old_status_file);
890 fclose(new_status_file);
893 /* Create a seperate backfile to dpkg */
894 if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) {
895 struct stat stat_buf;
896 if (stat("/var/lib/dpkg/status", &stat_buf) == 0) {
897 error_msg_and_die("Couldnt create backup status file");
899 /* Its ok if renaming the status file fails becasue status
900 * file doesnt exist, maybe we are starting from scratch */
901 error_msg("No status file found, creating new one");
904 if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) {
905 error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file");
909 int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count)
911 int *conflicts = NULL;
912 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 *) xmalloc(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 new_node->num_of_edges = 0;
946 new_node->edge = NULL;
947 package_hashtable[conflicts_package_num] = new_node;
949 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1));
950 conflicts[conflicts_num] = conflicts_package_num;
957 /* Check conflicts */
959 while (deb_file[i] != NULL) {
960 const common_node_t *package_node = package_hashtable[deb_file[i]->package];
962 status_num = search_status_hashtable(name_hashtable[package_node->name]);
964 if (get_status(status_num, 3) == search_name_hashtable("installed")) {
969 for (j = 0; j < package_node->num_of_edges; j++) {
970 const edge_t *package_edge = package_node->edge[j];
971 const unsigned int package_num =
972 search_package_hashtable(package_edge->name,
973 package_edge->version, package_edge->operator);
975 if (package_edge->type == EDGE_CONFLICTS) {
977 if (package_hashtable[package_num] != NULL) {
978 status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
979 state_status = get_status(status_num, 3);
980 state_flag = get_status(status_num, 1);
982 result = (state_status == search_name_hashtable("installed")) ||
983 (state_flag == search_name_hashtable("want-install"));
986 result = test_version(package_hashtable[deb_file[i]->package]->version,
987 package_edge->version, package_edge->operator);
992 error_msg_and_die("Package %s conflicts with %s",
993 name_hashtable[package_node->name],
994 name_hashtable[package_edge->name]);
1002 /* Check dependendcies */
1004 while (deb_file[i] != NULL) {
1005 const common_node_t *package_node = package_hashtable[deb_file[i]->package];
1008 status_num = search_status_hashtable(name_hashtable[package_node->name]);
1009 state_status = get_status(status_num, 3);
1010 state_want = get_status(status_num, 1);
1012 if (state_status == search_name_hashtable("installed")) {
1017 for (j = 0; j < package_hashtable[deb_file[i]->package]->num_of_edges; j++) {
1018 const edge_t *package_edge = package_node->edge[j];
1019 unsigned int package_num;
1021 package_num = search_package_hashtable(package_edge->name, package_edge->version, package_edge->operator);
1023 switch (package_edge->type) {
1024 case(EDGE_PRE_DEPENDS):
1025 case(EDGE_OR_PRE_DEPENDS): {
1027 /* It must be already installed */
1028 /* NOTE: This is untested, nothing apropriate in my status file */
1029 if (package_hashtable[package_num] != NULL) {
1030 status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
1031 state_status = get_status(status_num, 3);
1032 state_want = get_status(status_num, 1);
1033 result = (state_status != search_name_hashtable("installed"));
1037 error_msg_and_die("Package %s pre-depends on %s, but it is not installed",
1038 name_hashtable[package_node->name],
1039 name_hashtable[package_edge->name]);
1044 case(EDGE_OR_DEPENDS): {
1046 if (package_hashtable[package_num] != NULL) {
1047 status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
1048 state_status = get_status(status_num, 3);
1049 state_want = get_status(status_num, 1);
1050 result=(state_status != search_name_hashtable("installed")) && (state_want != search_name_hashtable("want-install"));
1052 /* It must be already installed, or to be installed */
1054 error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed",
1055 name_hashtable[package_node->name],
1056 name_hashtable[package_edge->name]);
1068 char **create_list(const char *filename)
1071 char **file_list = NULL;
1076 /* dont use [xw]fopen here, handle error ourself */
1077 list_stream = fopen(filename, "r");
1078 if (list_stream == NULL) {
1082 while (getline(&line, &length, list_stream) != -1) {
1083 file_list = xrealloc(file_list, sizeof(char *) * (count + 2));
1085 file_list[count] = xstrdup(line);
1088 fclose(list_stream);
1094 file_list[count] = NULL;
1099 /* maybe i should try and hook this into remove_file.c somehow */
1100 int remove_file_array(char **remove_names, char **exclude_names)
1102 struct stat path_stat;
1104 int remove_flag = FALSE;
1107 if (remove_names == NULL) {
1110 for (i = 0; remove_names[i] != NULL; i++) {
1112 if (exclude_names != NULL) {
1113 for (j = 0; exclude_names[j] != 0; j++) {
1114 if (strcmp(remove_names[i], exclude_names[j]) == 0) {
1121 if (lstat(remove_names[i], &path_stat) < 0) {
1124 if (S_ISDIR(path_stat.st_mode)) {
1125 if (rmdir(remove_names[i]) != -1) {
1129 if (unlink(remove_names[i]) != -1) {
1135 return(remove_flag);
1138 int run_package_script(const char *package_name, const char *script_type)
1140 struct stat path_stat;
1144 script_path = xmalloc(strlen(package_name) + strlen(script_type) + 21);
1145 sprintf(script_path, "/var/lib/dpkg/info/%s.%s", package_name, script_type);
1147 /* If the file doesnt exist is isnt a fatal */
1148 if (lstat(script_path, &path_stat) < 0) {
1149 result = EXIT_SUCCESS;
1151 result = system(script_path);
1157 char **all_control_list(const char *package_name)
1159 const char *extensions[11] = {"preinst", "postinst", "prerm", "postrm",
1160 "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL };
1161 unsigned short i = 0;
1162 char **remove_files;
1164 /* Create a list of all /var/lib/dpkg/info/<package> files */
1165 remove_files = malloc(sizeof(char *) * 11);
1166 while (extensions[i]) {
1167 remove_files[i] = xmalloc(strlen(package_name) + strlen(extensions[i]) + 21);
1168 sprintf(remove_files[i], "/var/lib/dpkg/info/%s.%s", package_name, extensions[i]);
1171 remove_files[10] = NULL;
1173 return(remove_files);
1176 void free_array(char **array)
1180 unsigned short i = 0;
1189 /* This function lists information on the installed packages. It loops through
1190 * the status_hashtable to retrieve the info. This results in smaller code than
1191 * scanning the status file. The resulting list, however, is unsorted.
1193 void list_packages(void)
1197 printf(" Name Version\n");
1198 printf("+++-==============-==============\n");
1200 /* go through status hash, dereference package hash and finally strings */
1201 for (i=0; i<STATUS_HASH_PRIME+1; i++) {
1203 if (status_hashtable[i]) {
1204 const char *stat_str; /* status string */
1205 const char *name_str; /* package name */
1206 const char *vers_str; /* version */
1207 char s1, s2; /* status abbreviations */
1208 int spccnt; /* space count */
1211 stat_str = name_hashtable[status_hashtable[i]->status];
1212 name_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->name];
1213 vers_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->version];
1215 /* get abbreviation for status field 1 */
1216 s1 = stat_str[0] == 'i' ? 'i' : 'r';
1218 /* get abbreviation for status field 2 */
1219 for (j=0, spccnt=0; stat_str[j] && spccnt<2; j++) {
1220 if (stat_str[j] == ' ') spccnt++;
1224 /* print out the line formatted like Debian dpkg */
1225 printf("%c%c %-14s %s\n", s1, s2, name_str, vers_str);
1230 void remove_package(const unsigned int package_num)
1232 const char *package_name = name_hashtable[package_hashtable[package_num]->name];
1233 const unsigned int status_num = search_status_hashtable(package_name);
1234 const int package_name_length = strlen(package_name);
1235 char **remove_files;
1236 char **exclude_files;
1237 char list_name[package_name_length + 25];
1238 char conffile_name[package_name_length + 30];
1241 printf("Removing %s ...\n", package_name);
1243 /* run prerm script */
1244 return_value = run_package_script(package_name, "prerm");
1245 if (return_value == -1) {
1246 error_msg_and_die("script failed, prerm failure");
1249 /* Create a list of files to remove, and a seperate list of those to keep */
1250 sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name);
1251 remove_files = create_list(list_name);
1253 sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name);
1254 exclude_files = create_list(conffile_name);
1256 /* Some directories cant be removed straight away, so do multiple passes */
1257 while (remove_file_array(remove_files, exclude_files));
1258 free_array(exclude_files);
1259 free_array(remove_files);
1261 /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */
1262 exclude_files = xmalloc(sizeof(char*) * 3);
1263 exclude_files[0] = xstrdup(conffile_name);
1264 exclude_files[1] = xmalloc(package_name_length + 27);
1265 sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name);
1266 exclude_files[2] = NULL;
1268 /* Create a list of all /var/lib/dpkg/info/<package> files */
1269 remove_files = all_control_list(package_name);
1271 remove_file_array(remove_files, exclude_files);
1272 free_array(remove_files);
1273 free_array(exclude_files);
1275 /* rename <package>.conffile to <package>.list */
1276 rename(conffile_name, list_name);
1278 /* Change package status */
1279 set_status(status_num, "deinstall", 1);
1280 set_status(status_num, "config-files", 3);
1283 void purge_package(const unsigned int package_num)
1285 const char *package_name = name_hashtable[package_hashtable[package_num]->name];
1286 const unsigned int status_num = search_status_hashtable(package_name);
1287 char **remove_files;
1288 char **exclude_files;
1289 char list_name[strlen(package_name) + 25];
1291 /* run prerm script */
1292 if (run_package_script(package_name, "prerm") != 0) {
1293 error_msg_and_die("script failed, prerm failure");
1296 /* Create a list of files to remove */
1297 sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name);
1298 remove_files = create_list(list_name);
1300 exclude_files = xmalloc(sizeof(char*));
1301 exclude_files[0] = NULL;
1303 /* Some directories cant be removed straight away, so do multiple passes */
1304 while (remove_file_array(remove_files, exclude_files));
1305 free_array(remove_files);
1307 /* Create a list of all /var/lib/dpkg/info/<package> files */
1308 remove_files = all_control_list(package_name);
1309 remove_file_array(remove_files, exclude_files);
1310 free_array(remove_files);
1311 free(exclude_files);
1313 /* run postrm script */
1314 if (run_package_script(package_name, "postrm") == -1) {
1315 error_msg_and_die("postrm fialure.. set status to what?");
1318 /* Change package status */
1319 set_status(status_num, "purge", 1);
1320 set_status(status_num, "not-installed", 3);
1323 static archive_handle_t *init_archive_deb_ar(const char *filename)
1325 archive_handle_t *ar_handle;
1327 /* Setup an ar archive handle that refers to the gzip sub archive */
1328 ar_handle = init_handle();
1329 ar_handle->filter = filter_accept_list_reassign;
1330 ar_handle->src_fd = xopen(filename, O_RDONLY);
1335 static void init_archive_deb_control(archive_handle_t *ar_handle)
1337 archive_handle_t *tar_handle;
1339 /* Setup the tar archive handle */
1340 tar_handle = init_handle();
1341 tar_handle->filter = filter_accept_list;
1342 tar_handle->src_fd = ar_handle->src_fd;
1344 /* We dont care about data.tar.* or debian-binary, just control.tar.* */
1345 #ifdef CONFIG_FEATURE_DEB_TAR_GZ
1346 ar_handle->accept = add_to_list(NULL, "control.tar.gz");
1348 #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
1349 ar_handle->accept = add_to_list(ar_handle->accept, "control.tar.bz2");
1352 /* Assign the tar handle as a subarchive of the ar handle */
1353 ar_handle->sub_archive = tar_handle;
1358 static void init_archive_deb_data(archive_handle_t *ar_handle)
1360 archive_handle_t *tar_handle;
1362 /* Setup the tar archive handle */
1363 tar_handle = init_handle();
1364 tar_handle->filter = filter_accept_all;
1365 tar_handle->src_fd = ar_handle->src_fd;
1367 /* We dont care about data.tar.* or debian-binary, just control.tar.* */
1368 #ifdef CONFIG_FEATURE_DEB_TAR_GZ
1369 tar_handle->accept = add_to_list(NULL, "data.tar.gz");
1371 #ifdef CONFIG_FEATURE_DEB_TAR_BZ2
1372 tar_handle->accept = add_to_list(ar_handle->accept, "data.tar.bz2");
1375 /* Assign the tar handle as a subarchive of the ar handle */
1376 ar_handle->sub_archive = tar_handle;
1381 static char *deb_extract_control_file_to_buffer(archive_handle_t *ar_handle, const llist_t *accept)
1383 ar_handle->sub_archive->action_data = data_extract_to_buffer;
1384 ar_handle->sub_archive->accept = accept;
1386 unpack_ar_archive(ar_handle);
1387 close(ar_handle->src_fd);
1389 return(ar_handle->sub_archive->buffer);
1392 static void data_extract_all_prefix(archive_handle_t *archive_handle)
1394 char *name_ptr = archive_handle->file_header->name;
1396 name_ptr += strspn(name_ptr, "./");
1397 if (name_ptr[0] != '\0') {
1398 archive_handle->file_header->name = xmalloc(strlen(archive_handle->buffer) + 2 + strlen(name_ptr));
1399 strcpy(archive_handle->file_header->name, archive_handle->buffer);
1400 strcat(archive_handle->file_header->name, name_ptr);
1401 data_extract_all(archive_handle);
1406 static void unpack_package(deb_file_t *deb_file)
1408 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
1409 const unsigned int status_num = search_status_hashtable(package_name);
1410 const unsigned int status_package_num = status_hashtable[status_num]->package;
1412 archive_handle_t *archive_handle;
1415 /* If existing version, remove it first */
1416 if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) {
1417 /* Package is already installed, remove old version first */
1418 printf("Preparing to replace %s %s (using %s) ...\n", package_name,
1419 name_hashtable[package_hashtable[status_package_num]->version],
1420 deb_file->filename);
1421 remove_package(status_package_num);
1423 printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename);
1426 /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */
1427 info_prefix = (char *) xmalloc(strlen(package_name) + 20 + 4 + 2);
1428 sprintf(info_prefix, "/var/lib/dpkg/info/%s.", package_name);
1429 archive_handle = init_archive_deb_ar(deb_file->filename);
1430 init_archive_deb_control(archive_handle);
1431 archive_handle->sub_archive->action_data = data_extract_all_prefix;
1432 archive_handle->sub_archive->buffer = info_prefix;
1433 unpack_ar_archive(archive_handle);
1435 /* Run the preinst prior to extracting */
1436 if (run_package_script(package_name, "preinst") != 0) {
1437 /* when preinst returns exit code != 0 then quit installation process */
1438 error_msg_and_die("subprocess pre-installation script returned error.");
1441 /* Extract data.tar.gz to the root directory */
1442 archive_handle = init_archive_deb_ar(deb_file->filename);
1443 init_archive_deb_data(archive_handle);
1444 unpack_ar_archive(archive_handle);
1446 /* Create the list file */
1447 strcat(info_prefix, "list");
1448 out_stream = xfopen(info_prefix, "w");
1449 while (archive_handle->passed) {
1450 /* blindly skip over the leading '.' */
1451 fputs(archive_handle->passed->data + 1, out_stream);
1452 fputc('\n', out_stream);
1453 archive_handle->passed = archive_handle->passed->link;
1458 set_status(status_num, "install", 1);
1459 set_status(status_num, "unpacked", 3);
1464 void configure_package(deb_file_t *deb_file)
1466 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
1467 const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version];
1468 const int status_num = search_status_hashtable(package_name);
1470 printf("Setting up %s (%s)\n", package_name, package_version);
1472 /* Run the postinst script */
1473 if (run_package_script(package_name, "postinst") != 0) {
1474 /* TODO: handle failure gracefully */
1475 error_msg_and_die("postrm failure.. set status to what?");
1477 /* Change status to reflect success */
1478 set_status(status_num, "install", 1);
1479 set_status(status_num, "installed", 3);
1482 int dpkg_main(int argc, char **argv)
1484 deb_file_t **deb_file = NULL;
1485 status_node_t *status_node;
1494 while ((opt = getopt(argc, argv, "CF:ilPru")) != -1) {
1496 case 'C': // equivalent to --configure in official dpkg
1497 dpkg_opt |= dpkg_opt_configure;
1498 dpkg_opt |= dpkg_opt_package_name;
1500 case 'F': // equivalent to --force in official dpkg
1501 if (strcmp(optarg, "depends") == 0) {
1502 dpkg_opt |= dpkg_opt_force_ignore_depends;
1505 dpkg_opt |= dpkg_opt_install;
1506 dpkg_opt |= dpkg_opt_filename;
1509 dpkg_opt |= dpkg_opt_list_installed;
1512 dpkg_opt |= dpkg_opt_purge;
1513 dpkg_opt |= dpkg_opt_package_name;
1516 dpkg_opt |= dpkg_opt_remove;
1517 dpkg_opt |= dpkg_opt_package_name;
1519 case 'u': /* Equivalent to --unpack in official dpkg */
1520 dpkg_opt |= dpkg_opt_unpack;
1521 dpkg_opt |= dpkg_opt_filename;
1527 /* check for non-otion argument if expected */
1528 if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) {
1532 /* puts("(Reading database ... xxxxx files and directories installed.)"); */
1533 index_status_file("/var/lib/dpkg/status");
1535 /* if the list action was given print the installed packages and exit */
1536 if (dpkg_opt & dpkg_opt_list_installed) {
1538 return(EXIT_SUCCESS);
1541 /* Read arguments and store relevant info in structs */
1542 while (optind < argc) {
1543 /* deb_count = nb_elem - 1 and we need nb_elem + 1 to allocate terminal node [NULL pointer] */
1544 deb_file = xrealloc(deb_file, sizeof(deb_file_t *) * (deb_count + 2));
1545 deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t));
1546 if (dpkg_opt & dpkg_opt_filename) {
1547 archive_handle_t *archive_handle;
1548 const llist_t *control_list = NULL;
1550 /* Extract the control file */
1551 control_list = add_to_list(NULL, "./control");
1552 archive_handle = init_archive_deb_ar(argv[optind]);
1553 init_archive_deb_control(archive_handle);
1554 deb_file[deb_count]->control_file = deb_extract_control_file_to_buffer(archive_handle, control_list);
1555 if (deb_file[deb_count]->control_file == NULL) {
1556 error_msg_and_die("Couldnt extract control file");
1558 deb_file[deb_count]->filename = xstrdup(argv[optind]);
1559 package_num = fill_package_struct(deb_file[deb_count]->control_file);
1561 if (package_num == -1) {
1562 error_msg("Invalid control file in %s", argv[optind]);
1565 deb_file[deb_count]->package = (unsigned int) package_num;
1567 /* Add the package to the status hashtable */
1568 if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) {
1569 status_node = (status_node_t *) xmalloc(sizeof(status_node_t));
1570 status_node->package = deb_file[deb_count]->package;
1571 /* Try and find a currently installed version of this package */
1572 status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1573 /* If no previous entry was found initialise a new entry */
1574 if ((status_hashtable[status_num] == NULL) ||
1575 (status_hashtable[status_num]->status == 0)) {
1576 /* reinstreq isnt changed to "ok" until the package control info
1577 * is written to the status file*/
1578 status_node->status = search_name_hashtable("want-install reinstreq not-installed");
1579 status_hashtable[status_num] = status_node;
1581 status_hashtable[status_num]->status = search_name_hashtable("want-install reinstreq not-installed");
1585 else if (dpkg_opt & dpkg_opt_package_name) {
1586 deb_file[deb_count]->filename = NULL;
1587 deb_file[deb_count]->control_file = NULL;
1588 deb_file[deb_count]->package = search_package_hashtable(
1589 search_name_hashtable(argv[optind]),
1590 search_name_hashtable("ANY"), VER_ANY);
1591 if (package_hashtable[deb_file[deb_count]->package] == NULL) {
1592 error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]);
1594 state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3);
1596 /* check package status is "installed" */
1597 if (dpkg_opt & dpkg_opt_remove) {
1598 if ((strcmp(name_hashtable[state_status], "not-installed") == 0) ||
1599 (strcmp(name_hashtable[state_status], "config-files") == 0)) {
1600 error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1603 else if (dpkg_opt & dpkg_opt_purge) {
1604 /* if package status is "conf-files" then its ok */
1605 if (strcmp(name_hashtable[state_status], "not-installed") == 0) {
1606 error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1613 deb_file[deb_count] = NULL;
1615 /* Check that the deb file arguments are installable */
1616 /* TODO: check dependencies before removing */
1617 if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) {
1618 if (!check_deps(deb_file, 0, deb_count)) {
1619 error_msg_and_die("Dependency check failed");
1623 for (i = 0; i < deb_count; i++) {
1624 /* Remove or purge packages */
1625 if (dpkg_opt & dpkg_opt_remove) {
1626 remove_package(deb_file[i]->package);
1628 else if (dpkg_opt & dpkg_opt_purge) {
1629 purge_package(deb_file[i]->package);
1631 else if (dpkg_opt & dpkg_opt_unpack) {
1632 unpack_package(deb_file[i]);
1634 else if (dpkg_opt & dpkg_opt_install) {
1635 unpack_package(deb_file[i]);
1636 configure_package(deb_file[i]);
1638 else if (dpkg_opt & dpkg_opt_configure) {
1639 configure_package(deb_file[i]);
1643 write_status_file(deb_file);
1645 for (i = 0; i < deb_count; i++) {
1646 free(deb_file[i]->control_file);
1647 free(deb_file[i]->filename);
1653 for (i = 0; i < NAME_HASH_PRIME; i++) {
1654 free(name_hashtable[i]);
1657 for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
1658 if (package_hashtable[i] != NULL) {
1659 free_package(package_hashtable[i]);
1663 for (i = 0; i < STATUS_HASH_PRIME; i++) {
1664 free(status_hashtable[i]);
1667 return(EXIT_SUCCESS);