aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2001-10-25 14:26:05 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2001-10-25 14:26:05 +0000
commit0d2fb76c11be8d9902bca7a62d4d66f1df314fa0 (patch)
tree9dee29939c91e2e4c45efa7e4f4098fca423947e
parent95ebf618b78814ef250bf40737816784cdaef74d (diff)
downloadbusybox-w32-0d2fb76c11be8d9902bca7a62d4d66f1df314fa0.tar.gz
busybox-w32-0d2fb76c11be8d9902bca7a62d4d66f1df314fa0.tar.bz2
busybox-w32-0d2fb76c11be8d9902bca7a62d4d66f1df314fa0.zip
Modify applets to use libunarchive
-rw-r--r--archival/config.in6
-rw-r--r--archival/dpkg.c1553
-rw-r--r--include/libbb.h44
-rw-r--r--libbb/Makefile2
-rw-r--r--networking/Makefile2
-rw-r--r--networking/config.in1
6 files changed, 242 insertions, 1366 deletions
diff --git a/archival/config.in b/archival/config.in
index 099810cc5..183d9d3a1 100644
--- a/archival/config.in
+++ b/archival/config.in
@@ -10,6 +10,12 @@ bool 'ar' CONFIG_AR
10bool 'bunzip2' CONFIG_BUNZIP2 10bool 'bunzip2' CONFIG_BUNZIP2
11bool 'cpio' CONFIG_CPIO 11bool 'cpio' CONFIG_CPIO
12bool 'dpkg' CONFIG_DPKG 12bool 'dpkg' CONFIG_DPKG
13if [ "$CONFIG_DPKG" = "y" ] ; then
14 bool ' list support' CONFIG_FEATURE_DPKG_LIST
15 if [ "$CONFIG_FEATURE_DPKG_LIST" = "y" ] ; then
16 bool ' short descriptions' CONFIG_FEATURE_DPKG_LIST_DESCRIPTIONS
17 fi
18fi
13bool 'dpkg_deb' CONFIG_DPKG_DEB 19bool 'dpkg_deb' CONFIG_DPKG_DEB
14bool 'gunzip' CONFIG_GUNZIP 20bool 'gunzip' CONFIG_GUNZIP
15bool 'gzip' CONFIG_GZIP 21bool 'gzip' CONFIG_GZIP
diff --git a/archival/dpkg.c b/archival/dpkg.c
index bf0dcf3c3..501cd605c 100644
--- a/archival/dpkg.c
+++ b/archival/dpkg.c
@@ -33,1476 +33,301 @@
33 * - Packages with previously unknown status are inserted at the begining of 33 * - Packages with previously unknown status are inserted at the begining of
34 * the status file 34 * the status file
35 * 35 *
36 * Bugs that need to be fixed 36 * Work to be done
37 * - (unknown, please let me know when you find any) 37 * - (bugs, unknown, please let me know when you find them)
38 * 38 * - dependency checking is incomplete
39 * - provides, replaces, conflicts fields arent considered when installing
39 */ 40 */
40 41
41#include <getopt.h> 42#include <getopt.h>
42#include <stdlib.h> 43#include <stdlib.h>
43#include <string.h> 44#include <string.h>
44#include <unistd.h> 45#include <unistd.h>
46#include "../libbb/deb_functs.h"
47#include "libbb.h"
45#include "busybox.h" 48#include "busybox.h"
46 49
47/* NOTE: If you vary HASH_PRIME sizes be aware,
48 * 1) Tweaking these will have a big effect on how much memory this program uses.
49 * 2) For computational efficiency these hash tables should be at least 20%
50 * larger than the maximum number of elements stored in it.
51 * 3) All _HASH_PRIME's must be a prime number or chaos is assured, if your looking
52 * for a prime, try http://www.utm.edu/research/primes/lists/small/10000.txt
53 * 4) If you go bigger than 15 bits you may get into trouble (untested) as its
54 * sometimes cast to an unsigned int, if you go to 16 bit you will overlap
55 * int's and chaos is assured, 16381 is the max prime for 14 bit field
56 */
57
58/* NAME_HASH_PRIME, Stores package names and versions,
59 * I estimate it should be at least 50% bigger than PACKAGE_HASH_PRIME,
60 * as there a lot of duplicate version numbers */
61#define NAME_HASH_PRIME 16381
62char *name_hashtable[NAME_HASH_PRIME + 1];
63
64/* PACKAGE_HASH_PRIME, Maximum number of unique packages,
65 * It must not be smaller than STATUS_HASH_PRIME,
66 * Currently only packages from status_hashtable are stored in here, but in
67 * future this may be used to store packages not only from a status file,
68 * but an available_hashtable, and even multiple packages files.
69 * Package can be stored more than once if they have different versions.
70 * e.g. The same package may have different versions in the status file
71 * and available file */
72#define PACKAGE_HASH_PRIME 10007
73typedef struct edge_s {
74 unsigned int operator:3;
75 unsigned int type:4;
76 unsigned int name:14;
77 unsigned int version:14;
78} edge_t;
79
80typedef struct common_node_s {
81 unsigned int name:14;
82 unsigned int version:14;
83 unsigned int num_of_edges:14;
84 edge_t **edge;
85} common_node_t;
86common_node_t *package_hashtable[PACKAGE_HASH_PRIME + 1];
87
88/* Currently it doesnt store packages that have state-status of not-installed
89 * So it only really has to be the size of the maximum number of packages
90 * likely to be installed at any one time, so there is a bit of leaway here */
91#define STATUS_HASH_PRIME 8191
92typedef struct status_node_s {
93 unsigned int package:14; /* has to fit PACKAGE_HASH_PRIME */
94 unsigned int status:14; /* has to fit STATUS_HASH_PRIME */
95} status_node_t;
96status_node_t *status_hashtable[STATUS_HASH_PRIME + 1];
97
98/* Even numbers are for 'extras', like ored dependecies or null */
99enum edge_type_e {
100 EDGE_NULL = 0,
101 EDGE_PRE_DEPENDS = 1,
102 EDGE_OR_PRE_DEPENDS = 2,
103 EDGE_DEPENDS = 3,
104 EDGE_OR_DEPENDS = 4,
105 EDGE_REPLACES = 5,
106 EDGE_PROVIDES = 7,
107 EDGE_CONFLICTS = 9,
108 EDGE_SUGGESTS = 11,
109 EDGE_RECOMMENDS = 13,
110 EDGE_ENHANCES = 15
111};
112enum operator_e {
113 VER_NULL = 0,
114 VER_EQUAL = 1,
115 VER_LESS = 2,
116 VER_LESS_EQUAL = 3,
117 VER_MORE = 4,
118 VER_MORE_EQUAL = 5,
119 VER_ANY = 6
120};
121
122enum dpkg_opt_e { 50enum dpkg_opt_e {
123 dpkg_opt_purge = 1, 51 DPKG_OPT_PURGE = 1,
124 dpkg_opt_remove = 2, 52 DPKG_OPT_REMOVE = 2,
125 dpkg_opt_unpack = 4, 53 DPKG_OPT_UNPACK = 4,
126 dpkg_opt_configure = 8, 54 DPKG_OPT_CONFIGURE = 8,
127 dpkg_opt_install = 16, 55 DPKG_OPT_INSTALL = 16,
128 dpkg_opt_package_name = 32, 56 DPKG_OPT_PACKAGENAME = 32,
129 dpkg_opt_filename = 64, 57 DPKG_OPT_FILENAME = 64,
130 dpkg_opt_list_installed = 128, 58 DPKG_OPT_LIST_INSTALLED = 128,
131 dpkg_opt_force_ignore_depends = 256 59 DPKG_OPT_FORCE_IGNORE_DEPENDS = 256,
132}; 60};
61extern unsigned short char_name_hash_prime;
62extern unsigned short char_ver_hash_prime;
63extern unsigned short char_rev_hash_prime;
64extern unsigned short char_filename_hash_prime;
65extern unsigned short char_source_hash_prime;
66extern unsigned short edge_hash_prime;
67extern unsigned short node_hash_prime;
133 68
134typedef struct deb_file_s { 69/* This function lists information on the installed packages. It loops through
135 char *control_file; 70 * the status_hashtable to retrieve the info. This results in smaller code than
136 char *filename; 71 * scanning the status file. The resulting list, however, is unsorted.
137 unsigned int package:14;
138} deb_file_t;
139
140
141void make_hash(const char *key, unsigned int *start, unsigned int *decrement, const int hash_prime)
142{
143 unsigned long int hash_num = key[0];
144 int len = strlen(key);
145 int i;
146
147 /* Maybe i should have uses a "proper" hashing algorithm here instead
148 * of making one up myself, seems to be working ok though. */
149 for(i = 1; i < len; i++) {
150 /* shifts the ascii based value and adds it to previous value
151 * shift amount is mod 24 because long int is 32 bit and data
152 * to be shifted is 8, dont want to shift data to where it has
153 * no effect*/
154 hash_num += ((key[i] + key[i-1]) << ((key[i] * i) % 24));
155 }
156 *start = (unsigned int) hash_num % hash_prime;
157 *decrement = (unsigned int) 1 + (hash_num % (hash_prime - 1));
158}
159
160/* this adds the key to the hash table */
161int search_name_hashtable(const char *key)
162{
163 unsigned int probe_address = 0;
164 unsigned int probe_decrement = 0;
165// char *temp;
166
167 make_hash(key, &probe_address, &probe_decrement, NAME_HASH_PRIME);
168 while(name_hashtable[probe_address] != NULL) {
169 if (strcmp(name_hashtable[probe_address], key) == 0) {
170 return(probe_address);
171 } else {
172 probe_address -= probe_decrement;
173 if ((int)probe_address < 0) {
174 probe_address += NAME_HASH_PRIME;
175 }
176 }
177 }
178 name_hashtable[probe_address] = xstrdup(key);
179 return(probe_address);
180}
181
182/* this DOESNT add the key to the hashtable
183 * TODO make it consistent with search_name_hashtable
184 */
185unsigned int search_status_hashtable(const char *key)
186{
187 unsigned int probe_address = 0;
188 unsigned int probe_decrement = 0;
189
190 make_hash(key, &probe_address, &probe_decrement, STATUS_HASH_PRIME);
191 while(status_hashtable[probe_address] != NULL) {
192 if (strcmp(key, name_hashtable[package_hashtable[status_hashtable[probe_address]->package]->name]) == 0) {
193 break;
194 } else {
195 probe_address -= probe_decrement;
196 if ((int)probe_address < 0) {
197 probe_address += STATUS_HASH_PRIME;
198 }
199 }
200 }
201 return(probe_address);
202}
203
204/* Need to rethink version comparison, maybe the official dpkg has something i can use ? */
205int version_compare_part(const char *version1, const char *version2)
206{
207 int upstream_len1 = 0;
208 int upstream_len2 = 0;
209 char *name1_char;
210 char *name2_char;
211 int len1 = 0;
212 int len2 = 0;
213 int tmp_int;
214 int ver_num1;
215 int ver_num2;
216 int ret;
217
218 if (version1 == NULL) {
219 version1 = xstrdup("");
220 }
221 if (version2 == NULL) {
222 version2 = xstrdup("");
223 }
224 upstream_len1 = strlen(version1);
225 upstream_len2 = strlen(version2);
226
227 while ((len1 < upstream_len1) || (len2 < upstream_len2)) {
228 /* Compare non-digit section */
229 tmp_int = strcspn(&version1[len1], "0123456789");
230 name1_char = xstrndup(&version1[len1], tmp_int);
231 len1 += tmp_int;
232 tmp_int = strcspn(&version2[len2], "0123456789");
233 name2_char = xstrndup(&version2[len2], tmp_int);
234 len2 += tmp_int;
235 tmp_int = strcmp(name1_char, name2_char);
236 free(name1_char);
237 free(name2_char);
238 if (tmp_int != 0) {
239 ret = tmp_int;
240 goto cleanup_version_compare_part;
241 }
242
243 /* Compare digits */
244 tmp_int = strspn(&version1[len1], "0123456789");
245 name1_char = xstrndup(&version1[len1], tmp_int);
246 len1 += tmp_int;
247 tmp_int = strspn(&version2[len2], "0123456789");
248 name2_char = xstrndup(&version2[len2], tmp_int);
249 len2 += tmp_int;
250 ver_num1 = atoi(name1_char);
251 ver_num2 = atoi(name2_char);
252 free(name1_char);
253 free(name2_char);
254 if (ver_num1 < ver_num2) {
255 ret = -1;
256 goto cleanup_version_compare_part;
257 }
258 else if (ver_num1 > ver_num2) {
259 ret = 1;
260 goto cleanup_version_compare_part;
261 }
262 }
263 ret = 0;
264cleanup_version_compare_part:
265 return(ret);
266}
267
268/* if ver1 < ver2 return -1,
269 * if ver1 = ver2 return 0,
270 * if ver1 > ver2 return 1,
271 */
272int version_compare(const unsigned int ver1, const unsigned int ver2)
273{
274 char *ch_ver1 = name_hashtable[ver1];
275 char *ch_ver2 = name_hashtable[ver2];
276
277 char epoch1, epoch2;
278 char *deb_ver1, *deb_ver2;
279 char *ver1_ptr, *ver2_ptr;
280 char *upstream_ver1;
281 char *upstream_ver2;
282 int result;
283
284 /* Compare epoch */
285 if (ch_ver1[1] == ':') {
286 epoch1 = ch_ver1[0];
287 ver1_ptr = strchr(ch_ver1, ':') + 1;
288 } else {
289 epoch1 = '0';
290 ver1_ptr = ch_ver1;
291 }
292 if (ch_ver2[1] == ':') {
293 epoch2 = ch_ver2[0];
294 ver2_ptr = strchr(ch_ver2, ':') + 1;
295 } else {
296 epoch2 = '0';
297 ver2_ptr = ch_ver2;
298 }
299 if (epoch1 < epoch2) {
300 return(-1);
301 }
302 else if (epoch1 > epoch2) {
303 return(1);
304 }
305
306 /* Compare upstream version */
307 upstream_ver1 = xstrdup(ver1_ptr);
308 upstream_ver2 = xstrdup(ver2_ptr);
309
310 /* Chop off debian version, and store for later use */
311 deb_ver1 = strrchr(upstream_ver1, '-');
312 deb_ver2 = strrchr(upstream_ver2, '-');
313 if (deb_ver1) {
314 deb_ver1[0] = '\0';
315 deb_ver1++;
316 }
317 if (deb_ver2) {
318 deb_ver2[0] = '\0';
319 deb_ver2++;
320 }
321 result = version_compare_part(upstream_ver1, upstream_ver2);
322
323 free(upstream_ver1);
324 free(upstream_ver2);
325
326 if (result != 0) {
327 return(result);
328 }
329
330 /* Compare debian versions */
331 return(version_compare_part(deb_ver1, deb_ver2));
332}
333
334int test_version(const unsigned int version1, const unsigned int version2, const unsigned int operator)
335{
336 const int version_result = version_compare(version1, version2);
337 switch(operator) {
338 case (VER_ANY):
339 return(TRUE);
340 case (VER_EQUAL):
341 if (version_result == 0) {
342 return(TRUE);
343 }
344 break;
345 case (VER_LESS):
346 if (version_result < 0) {
347 return(TRUE);
348 }
349 break;
350 case (VER_LESS_EQUAL):
351 if (version_result <= 0) {
352 return(TRUE);
353 }
354 break;
355 case (VER_MORE):
356 if (version_result > 0) {
357 return(TRUE);
358 }
359 break;
360 case (VER_MORE_EQUAL):
361 if (version_result >= 0) {
362 return(TRUE);
363 }
364 break;
365 }
366 return(FALSE);
367}
368
369
370int search_package_hashtable(const unsigned int name, const unsigned int version, const unsigned int operator)
371{
372 unsigned int probe_address = 0;
373 unsigned int probe_decrement = 0;
374
375 make_hash(name_hashtable[name], &probe_address, &probe_decrement, PACKAGE_HASH_PRIME);
376 while(package_hashtable[probe_address] != NULL) {
377 if (package_hashtable[probe_address]->name == name) {
378 if (operator == VER_ANY) {
379 return(probe_address);
380 }
381 if (test_version(package_hashtable[probe_address]->version, version, operator)) {
382 return(probe_address);
383 }
384 }
385 probe_address -= probe_decrement;
386 if ((int)probe_address < 0) {
387 probe_address += PACKAGE_HASH_PRIME;
388 }
389 }
390 return(probe_address);
391}
392
393/*
394 * Create one new node and one new edge for every dependency.
395 */ 72 */
396void add_split_dependencies(common_node_t *parent_node, const char *whole_line, unsigned int edge_type) 73/* TODO: check the character flags ive assigned match real dpkg */
74#ifdef BB_FEATURE_DPKG_LIST
75extern void list_packages(void)
397{ 76{
398 char *line = xstrdup(whole_line); 77 int i;
399 char *line2;
400 char *line_ptr1 = NULL;
401 char *line_ptr2 = NULL;
402 char *field;
403 char *field2;
404 char *version;
405 edge_t *edge;
406 int offset_ch;
407 int type;
408
409 field = strtok_r(line, ",", &line_ptr1);
410 do {
411 line2 = xstrdup(field);
412 field2 = strtok_r(line2, "|", &line_ptr2);
413 if ((edge_type == EDGE_DEPENDS) && (strcmp(field, field2) != 0)) {
414 type = EDGE_OR_DEPENDS;
415 }
416 else if ((edge_type == EDGE_PRE_DEPENDS) && (strcmp(field, field2) != 0)) {
417 type = EDGE_OR_PRE_DEPENDS;
418 } else {
419 type = edge_type;
420 }
421
422 do {
423 edge = (edge_t *) xmalloc(sizeof(edge_t));
424 edge->type = type;
425
426 /* Skip any extra leading spaces */
427 field2 += strspn(field2, " ");
428
429 /* Get dependency version info */
430 version = strchr(field2, '(');
431 if (version == NULL) {
432 edge->operator = VER_ANY;
433 /* Get the versions hash number, adding it if the number isnt already in there */
434 edge->version = search_name_hashtable("ANY");
435 } else {
436 /* Skip leading ' ' or '(' */
437 version += strspn(field2, " ");
438 version += strspn(version, "(");
439 /* Calculate length of any operator charactors */
440 offset_ch = strspn(version, "<=>");
441 /* Determine operator */
442 if (offset_ch > 0) {
443 if (strncmp(version, "=", offset_ch) == 0) {
444 edge->operator = VER_EQUAL;
445 }
446 else if (strncmp(version, "<<", offset_ch) == 0) {
447 edge->operator = VER_LESS;
448 }
449 else if (strncmp(version, "<=", offset_ch) == 0) {
450 edge->operator = VER_LESS_EQUAL;
451 }
452 else if (strncmp(version, ">>", offset_ch) == 0) {
453 edge->operator = VER_MORE;
454 }
455 else if (strncmp(version, ">=", offset_ch) == 0) {
456 edge->operator = VER_MORE_EQUAL;
457 } else {
458 error_msg_and_die("Illegal operator\n");
459 }
460 }
461 /* skip to start of version numbers */
462 version += offset_ch;
463 version += strspn(version, " ");
464
465 /* Truncate version at trailing ' ' or ')' */
466 version[strcspn(version, " )")] = '\0';
467 /* Get the versions hash number, adding it if the number isnt already in there */
468 edge->version = search_name_hashtable(version);
469 }
470 78
471 /* Get the dependency name */ 79 printf(" Name Version\n");
472 field2[strcspn(field2, " (")] = '\0'; 80 printf("+++-==============-==============\n");
473 edge->name = search_name_hashtable(field2);
474 81
475 /* link the new edge to the current node */ 82 /* go through status hash, dereference package hash and finally strings */
476 parent_node->num_of_edges++; 83 for (i = 0; i < node_hash_prime; i++) {
477 parent_node->edge = xrealloc(parent_node->edge, sizeof(edge_t) * (parent_node->num_of_edges + 1)); 84 node_t *node = get_node_ht(i);
478 parent_node->edge[parent_node->num_of_edges - 1] = edge;
479 } while ((field2 = strtok_r(NULL, "|", &line_ptr2)) != NULL);
480 free(line2);
481 } while ((field = strtok_r(NULL, ",", &line_ptr1)) != NULL);
482 free(line);
483
484 return;
485}
486
487void free_package(common_node_t *node)
488{
489 int i;
490 if (node != NULL) {
491 for (i = 0; i < node->num_of_edges; i++) {
492 if (node->edge[i] != NULL) {
493 free(node->edge[i]);
494 }
495 }
496 if (node->edge != NULL) {
497 free(node->edge);
498 }
499 if (node != NULL) { 85 if (node != NULL) {
500 free(node); 86 char *name_str = get_name_ht(node->name);
501 } 87 char *ver_rev;
502 } 88 char want_flag;
503} 89 char status_flag;
504
505unsigned int fill_package_struct(char *control_buffer)
506{
507 common_node_t *new_node = (common_node_t *) xcalloc(1, sizeof(common_node_t));
508
509 char *field_name;
510 char *field_value;
511 int field_start = 0;
512 int num = -1;
513 int buffer_length = strlen(control_buffer);
514
515 new_node->version = search_name_hashtable("unknown");
516 while (field_start < buffer_length) {
517 field_start += read_package_field(&control_buffer[field_start],
518 &field_name, &field_value);
519
520 if (field_name == NULL) {
521 goto fill_package_struct_cleanup; // Oh no, the dreaded goto statement !!
522 }
523
524 if (strcmp(field_name, "Package") == 0) {
525 new_node->name = search_name_hashtable(field_value);
526 }
527 else if (strcmp(field_name, "Version") == 0) {
528 new_node->version = search_name_hashtable(field_value);
529 }
530 else if (strcmp(field_name, "Pre-Depends") == 0) {
531 add_split_dependencies(new_node, field_value, EDGE_PRE_DEPENDS);
532 }
533 else if (strcmp(field_name, "Depends") == 0) {
534 add_split_dependencies(new_node, field_value, EDGE_DEPENDS);
535 }
536 else if (strcmp(field_name, "Replaces") == 0) {
537 add_split_dependencies(new_node, field_value, EDGE_REPLACES);
538 }
539 else if (strcmp(field_name, "Provides") == 0) {
540 add_split_dependencies(new_node, field_value, EDGE_PROVIDES);
541 }
542 else if (strcmp(field_name, "Conflicts") == 0) {
543 add_split_dependencies(new_node, field_value, EDGE_CONFLICTS);
544 }
545 else if (strcmp(field_name, "Suggests") == 0) {
546 add_split_dependencies(new_node, field_value, EDGE_SUGGESTS);
547 }
548 else if (strcmp(field_name, "Recommends") == 0) {
549 add_split_dependencies(new_node, field_value, EDGE_RECOMMENDS);
550 }
551 else if (strcmp(field_name, "Enhances") == 0) {
552 add_split_dependencies(new_node, field_value, EDGE_ENHANCES);
553 }
554fill_package_struct_cleanup:
555 if (field_name) {
556 free(field_name);
557 }
558 if (field_value) {
559 free(field_value);
560 }
561 }
562
563 if (new_node->version == search_name_hashtable("unknown")) {
564 free_package(new_node);
565 return(-1);
566 }
567 num = search_package_hashtable(new_node->name, new_node->version, VER_EQUAL);
568 if (package_hashtable[num] == NULL) {
569 package_hashtable[num] = new_node;
570 } else {
571 free_package(new_node);
572 }
573 return(num);
574}
575
576/* if num = 1, it returns the want status, 2 returns flag, 3 returns status */
577unsigned int get_status(const unsigned int status_node, const int num)
578{
579 char *status_string = name_hashtable[status_hashtable[status_node]->status];
580 char *state_sub_string;
581 unsigned int state_sub_num;
582 int len;
583 int i;
584
585 /* set tmp_string to point to the start of the word number */
586 for (i = 1; i < num; i++) {
587 /* skip past a word */
588 status_string += strcspn(status_string, " ");
589 /* skip past the seperating spaces */
590 status_string += strspn(status_string, " ");
591 }
592 len = strcspn(status_string, " \n\0");
593 state_sub_string = xstrndup(status_string, len);
594 state_sub_num = search_name_hashtable(state_sub_string);
595 free(state_sub_string);
596 return(state_sub_num);
597}
598
599void set_status(const unsigned int status_node_num, const char *new_value, const int position)
600{
601 const unsigned int new_value_len = strlen(new_value);
602 const unsigned int new_value_num = search_name_hashtable(new_value);
603 unsigned int want = get_status(status_node_num, 1);
604 unsigned int flag = get_status(status_node_num, 2);
605 unsigned int status = get_status(status_node_num, 3);
606 int want_len = strlen(name_hashtable[want]);
607 int flag_len = strlen(name_hashtable[flag]);
608 int status_len = strlen(name_hashtable[status]);
609 char *new_status;
610
611 switch (position) {
612 case (1):
613 want = new_value_num;
614 want_len = new_value_len;
615 break;
616 case (2):
617 flag = new_value_num;
618 flag_len = new_value_len;
619 break;
620 case (3):
621 status = new_value_num;
622 status_len = new_value_len;
623 break;
624 default:
625 error_msg_and_die("DEBUG ONLY: this shouldnt happen");
626 }
627
628 new_status = (char *) xmalloc(want_len + flag_len + status_len + 3);
629 sprintf(new_status, "%s %s %s", name_hashtable[want], name_hashtable[flag], name_hashtable[status]);
630 status_hashtable[status_node_num]->status = search_name_hashtable(new_status);
631 free(new_status);
632 return;
633}
634
635void index_status_file(const char *filename)
636{
637 FILE *status_file;
638 char *control_buffer;
639 char *status_line;
640 status_node_t *status_node = NULL;
641 unsigned int status_num;
642
643 status_file = xfopen(filename, "r");
644 while ((control_buffer = fgets_str(status_file, "\n\n")) != NULL) {
645 const unsigned int package_num = fill_package_struct(control_buffer);
646 if (package_num != -1) {
647 status_node = xmalloc(sizeof(status_node_t));
648 /* fill_package_struct doesnt handle the status field */
649 status_line = strstr(control_buffer, "Status:");
650 if (status_line != NULL) {
651 status_line += 7;
652 status_line += strspn(status_line, " \n\t");
653 status_line = xstrndup(status_line, strcspn(status_line, "\n\0"));
654 status_node->status = search_name_hashtable(status_line);
655 free(status_line);
656 }
657 status_node->package = package_num;
658 status_num = search_status_hashtable(name_hashtable[package_hashtable[status_node->package]->name]);
659 status_hashtable[status_num] = status_node;
660 }
661 free(control_buffer);
662 }
663 fclose(status_file);
664 return;
665}
666
667
668char *get_depends_field(common_node_t *package, const int depends_type)
669{
670 char *depends = NULL;
671 char *old_sep = (char *)xcalloc(1, 3);
672 char *new_sep = (char *)xcalloc(1, 3);
673 int line_size = 0;
674 int depends_size;
675
676 int i;
677
678 for (i = 0; i < package->num_of_edges; i++) {
679 if ((package->edge[i]->type == EDGE_OR_PRE_DEPENDS) ||
680 (package->edge[i]->type == EDGE_OR_DEPENDS)) {
681 }
682
683 if ((package->edge[i]->type == depends_type) ||
684 (package->edge[i]->type == depends_type + 1)) {
685 /* Check if its the first time through */
686
687 depends_size = 8 + strlen(name_hashtable[package->edge[i]->name])
688 + strlen(name_hashtable[package->edge[i]->version]);
689 line_size += depends_size;
690 depends = (char *) xrealloc(depends, line_size + 1);
691
692 /* Check to see if this dependency is the type we are looking for
693 * +1 to check for 'extra' types, e.g. ored dependecies */
694 strcpy(old_sep, new_sep);
695 if (package->edge[i]->type == depends_type) {
696 strcpy(new_sep, ", ");
697 }
698 else if (package->edge[i]->type == depends_type + 1) {
699 strcpy(new_sep, "| ");
700 }
701
702 if (depends_size == line_size) {
703 strcpy(depends, "");
704 } else {
705 if ((strcmp(old_sep, "| ") == 0) && (strcmp(new_sep, "| ") == 0)) {
706 strcat(depends, " | ");
707 } else {
708 strcat(depends, ", ");
709 }
710 }
711
712 strcat(depends, name_hashtable[package->edge[i]->name]);
713 if (strcmp(name_hashtable[package->edge[i]->version], "NULL") != 0) {
714 if (package->edge[i]->operator == VER_EQUAL) {
715 strcat(depends, " (= ");
716 }
717 else if (package->edge[i]->operator == VER_LESS) {
718 strcat(depends, " (<< ");
719 }
720 else if (package->edge[i]->operator == VER_LESS_EQUAL) {
721 strcat(depends, " (<= ");
722 }
723 else if (package->edge[i]->operator == VER_MORE) {
724 strcat(depends, " (>> ");
725 }
726 else if (package->edge[i]->operator == VER_MORE_EQUAL) {
727 strcat(depends, " (>= ");
728 } else {
729 strcat(depends, " (");
730 }
731 strcat(depends, name_hashtable[package->edge[i]->version]);
732 strcat(depends, ")");
733 }
734 }
735 }
736 return(depends);
737}
738 90
739void write_buffer_no_status(FILE *new_status_file, const char *control_buffer) 91 ver_rev = version_revision(node->version, node->revision);
740{ 92 if (strlen(ver_rev) > 14) {
741 char *name; 93 ver_rev[14] = '\0';
742 char *value;
743 int start = 0;
744 while (1) {
745 start += read_package_field(&control_buffer[start], &name, &value);
746 if (name == NULL) {
747 break;
748 }
749 if (strcmp(name, "Status") != 0) {
750 fprintf(new_status_file, "%s: %s\n", name, value);
751 }
752 }
753 return;
754}
755
756/* This could do with a cleanup */
757void write_status_file(deb_file_t **deb_file)
758{
759 FILE *old_status_file = xfopen("/var/lib/dpkg/status", "r");
760 FILE *new_status_file = xfopen("/var/lib/dpkg/status.udeb", "w");
761 char *package_name;
762 char *status_from_file;
763 char *control_buffer = NULL;
764 char *tmp_string;
765 int status_num;
766 int field_start = 0;
767 int write_flag;
768 int i = 0;
769
770 /* Update previously known packages */
771 while ((control_buffer = fgets_str(old_status_file, "\n\n")) != NULL) {
772 if ((tmp_string = strstr(control_buffer, "Package:")) == NULL) {
773 continue;
774 }
775
776 tmp_string += 8;
777 tmp_string += strspn(tmp_string, " \n\t");
778 package_name = xstrndup(tmp_string, strcspn(tmp_string, "\n\0"));
779 write_flag = FALSE;
780 tmp_string = strstr(control_buffer, "Status:");
781 if (tmp_string != NULL) {
782 /* Seperate the status value from the control buffer */
783 tmp_string += 7;
784 tmp_string += strspn(tmp_string, " \n\t");
785 status_from_file = xstrndup(tmp_string, strcspn(tmp_string, "\n"));
786 } else {
787 status_from_file = NULL;
788 }
789
790 /* Find this package in the status hashtable */
791 status_num = search_status_hashtable(package_name);
792 if (status_hashtable[status_num] != NULL) {
793 const char *status_from_hashtable = name_hashtable[status_hashtable[status_num]->status];
794 if (strcmp(status_from_file, status_from_hashtable) != 0) {
795 /* New status isnt exactly the same as old status */
796 const int state_status = get_status(status_num, 3);
797 if ((strcmp("installed", name_hashtable[state_status]) == 0) ||
798 (strcmp("unpacked", name_hashtable[state_status]) == 0)) {
799 /* We need to add the control file from the package */
800 i = 0;
801 while(deb_file[i] != NULL) {
802 if (strcmp(package_name, name_hashtable[package_hashtable[deb_file[i]->package]->name]) == 0) {
803 /* Write a status file entry with a modified status */
804 /* remove trailing \n's */
805 write_buffer_no_status(new_status_file, deb_file[i]->control_file);
806 set_status(status_num, "ok", 2);
807 fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]);
808 write_flag = TRUE;
809 break;
810 }
811 i++;
812 }
813 /* This is temperary, debugging only */
814 if (deb_file[i] == NULL) {
815 error_msg_and_die("ALERT: Couldnt find a control file, your status file may be broken, status may be incorrect for %s", package_name);
816 }
817 }
818 else if (strcmp("not-installed", name_hashtable[state_status]) == 0) {
819 /* Only write the Package, Status, Priority and Section lines */
820 fprintf(new_status_file, "Package: %s\n", package_name);
821 fprintf(new_status_file, "Status: %s\n", status_from_hashtable);
822
823 while (1) {
824 char *field_name;
825 char *field_value;
826 field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
827 if (field_name == NULL) {
828 break;
829 }
830 if ((strcmp(field_name, "Priority") == 0) ||
831 (strcmp(field_name, "Section") == 0)) {
832 fprintf(new_status_file, "%s: %s\n", field_name, field_value);
833 }
834 }
835 write_flag = TRUE;
836 fputs("\n", new_status_file);
837 }
838 else if (strcmp("config-files", name_hashtable[state_status]) == 0) {
839 /* only change the status line */
840 while (1) {
841 char *field_name;
842 char *field_value;
843 field_start += read_package_field(&control_buffer[field_start], &field_name, &field_value);
844 if (field_name == NULL) {
845 break;
846 }
847 /* Setup start point for next field */
848 if (strcmp(field_name, "Status") == 0) {
849 fprintf(new_status_file, "Status: %s\n", status_from_hashtable);
850 } else {
851 fprintf(new_status_file, "%s: %s\n", field_name, field_value);
852 }
853 }
854 write_flag = TRUE;
855 fputs("\n", new_status_file);
856 }
857 }
858 }
859 /* If the package from the status file wasnt handle above, do it now*/
860 if (write_flag == FALSE) {
861 fprintf(new_status_file, "%s\n\n", control_buffer);
862 }
863
864 if (status_from_file != NULL) {
865 free(status_from_file);
866 }
867 free(package_name);
868 free(control_buffer);
869 }
870
871 /* Write any new packages */
872 for(i = 0; deb_file[i] != NULL; i++) {
873 status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[i]->package]->name]);
874 if (strcmp("reinstreq", name_hashtable[get_status(status_num, 2)]) == 0) {
875 write_buffer_no_status(new_status_file, deb_file[i]->control_file);
876 set_status(status_num, "ok", 2);
877 fprintf(new_status_file, "Status: %s\n\n", name_hashtable[status_hashtable[status_num]->status]);
878 }
879 }
880 fclose(old_status_file);
881 fclose(new_status_file);
882
883
884 /* Create a seperate backfile to dpkg */
885 if (rename("/var/lib/dpkg/status", "/var/lib/dpkg/status.udeb.bak") == -1) {
886 struct stat stat_buf;
887 if (stat("/var/lib/dpkg/status", &stat_buf) == 0) {
888 error_msg_and_die("Couldnt create backup status file");
889 }
890 /* Its ok if renaming the status file fails becasue status
891 * file doesnt exist, maybe we are starting from scratch */
892 error_msg("No status file found, creating new one");
893 }
894
895 if (rename("/var/lib/dpkg/status.udeb", "/var/lib/dpkg/status") == -1) {
896 error_msg_and_die("DANGER: Couldnt create status file, you need to manually repair your status file");
897 }
898}
899
900int check_deps(deb_file_t **deb_file, int deb_start, int dep_max_count)
901{
902 int *conflicts = NULL;
903 int conflicts_num = 0;
904 int state_status;
905 int state_flag;
906 int state_want;
907 unsigned int status_package_num;
908 int i = deb_start;
909 int j, k;
910
911 /* Check for conflicts
912 * TODO: TEST if conflicts with other packages to be installed
913 *
914 * Add install packages and the packages they provide
915 * to the list of files to check conflicts for
916 */
917
918 /* Create array of package numbers to check against
919 * installed package for conflicts*/
920 while (deb_file[i] != NULL) {
921 const unsigned int package_num = deb_file[i]->package;
922 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1));
923 conflicts[conflicts_num] = package_num;
924 conflicts_num++;
925 /* add provides to conflicts list */
926 for (j = 0; j < package_hashtable[package_num]->num_of_edges; j++) {
927 if (package_hashtable[package_num]->edge[j]->type == EDGE_PROVIDES) {
928 const int conflicts_package_num = search_package_hashtable(
929 package_hashtable[package_num]->edge[j]->name,
930 package_hashtable[package_num]->edge[j]->version,
931 package_hashtable[package_num]->edge[j]->operator);
932 if (package_hashtable[conflicts_package_num] == NULL) {
933 /* create a new package */
934 common_node_t *new_node = (common_node_t *) xmalloc(sizeof(common_node_t));
935 new_node->name = package_hashtable[package_num]->edge[j]->name;
936 new_node->version = package_hashtable[package_num]->edge[j]->version;
937 new_node->num_of_edges = 0;
938 new_node->edge = NULL;
939 package_hashtable[conflicts_package_num] = new_node;
940 }
941 conflicts = xrealloc(conflicts, sizeof(int) * (conflicts_num + 1));
942 conflicts[conflicts_num] = conflicts_package_num;
943 conflicts_num++;
944 }
945 }
946 i++;
947 }
948
949 /* Check conflicts */
950 for (i = 0; i < conflicts_num; i++) {
951 /* Check for conflicts */
952 for (j = 0; j < STATUS_HASH_PRIME; j++) {
953 if (status_hashtable[j] == NULL) {
954 continue;
955 }
956 state_flag = get_status(j, 2);
957 state_status = get_status(j, 3);
958 if ((state_status != search_name_hashtable("installed"))
959 && (state_flag != search_name_hashtable("want-install"))) {
960 continue;
961 }
962 status_package_num = status_hashtable[j]->package;
963 for (k = 0; k < package_hashtable[status_package_num]->num_of_edges; k++) {
964 const edge_t *package_edge = package_hashtable[status_package_num]->edge[k];
965 if (package_edge->type != EDGE_CONFLICTS) {
966 continue;
967 }
968 if (package_edge->name != package_hashtable[conflicts[i]]->name) {
969 continue;
970 }
971 /* There is a conflict against the package name
972 * check if version conflict as well */
973 if (test_version(package_hashtable[deb_file[i]->package]->version,
974 package_edge->version, package_edge->operator)) {
975 error_msg_and_die("Package %s conflict with %s",
976 name_hashtable[package_hashtable[deb_file[i]->package]->name],
977 name_hashtable[package_hashtable[status_package_num]->name]);
978 }
979 } 94 }
980 }
981 }
982
983 /* Check dependendcies */
984 i = 0;
985 while (deb_file[i] != NULL) {
986 const common_node_t *package_node = package_hashtable[deb_file[i]->package];
987 int status_num = 0;
988 95
989 for (j = 0; j < package_hashtable[deb_file[i]->package]->num_of_edges; j++) { 96 switch (node->state_want) {
990 const edge_t *package_edge = package_node->edge[j]; 97 case(STATE_WANT_HOLD):
991 const unsigned int package_num = search_package_hashtable(package_edge->name, 98 want_flag = 'h';
992 package_edge->version, package_edge->operator);
993
994 status_num = search_status_hashtable(name_hashtable[package_hashtable[package_num]->name]);
995 state_status = get_status(status_num, 3);
996 state_want = get_status(status_num, 1);
997 switch (package_edge->type) {
998 case(EDGE_PRE_DEPENDS):
999 case(EDGE_OR_PRE_DEPENDS):
1000 /* It must be already installed */
1001 /* NOTE: This is untested, nothing apropriate in my status file */
1002 if ((package_hashtable[package_num] == NULL) || (state_status != search_name_hashtable("installed"))) {
1003 error_msg_and_die("Package %s pre-depends on %s, but it is not installed",
1004 name_hashtable[package_node->name],
1005 name_hashtable[package_edge->name]);
1006 }
1007 break; 99 break;
1008 case(EDGE_DEPENDS): 100 case(STATE_WANT_INSTALL):
1009 case(EDGE_OR_DEPENDS): 101 want_flag = 'i';
1010 /* It must be already installed, or to be installed */
1011 if ((package_hashtable[package_num] == NULL) ||
1012 ((state_status != search_name_hashtable("installed")) &&
1013 (state_want != search_name_hashtable("want_install")))) {
1014 error_msg_and_die("Package %s depends on %s, but it is not installed, or flaged to be installed",
1015 name_hashtable[package_node->name],
1016 name_hashtable[package_edge->name]);
1017 }
1018 break; 102 break;
1019 } 103 case(STATE_WANT_DEINSTALL):
1020 } 104 want_flag = 'r';
1021 i++;
1022 }
1023 free(conflicts);
1024 return(TRUE);
1025}
1026
1027char **create_list(const char *filename)
1028{
1029 FILE *list_stream;
1030 char **file_list = xmalloc(sizeof(char *));
1031 char *line = NULL;
1032 char *last_char;
1033 int length = 0;
1034 int count = 0;
1035
1036 /* dont use [xw]fopen here, handle error ourself */
1037 list_stream = fopen(filename, "r");
1038 if (list_stream == NULL) {
1039 *file_list = NULL;
1040 return(file_list);
1041 }
1042 while (getline(&line, &length, list_stream) != -1) {
1043 file_list = xrealloc(file_list, sizeof(char *) * (length + 1));
1044 last_char = last_char_is(line, '\n');
1045 if (last_char) {
1046 *last_char = '\0';
1047 }
1048 file_list[count] = xstrdup(line);
1049 count++;
1050 }
1051 fclose(list_stream);
1052 free(line);
1053
1054 if (count == 0) {
1055 return(NULL);
1056 } else {
1057 file_list[count] = NULL;
1058 return(file_list);
1059 }
1060}
1061
1062/* maybe i should try and hook this into remove_file.c somehow */
1063int remove_file_array(char **remove_names, char **exclude_names)
1064{
1065 struct stat path_stat;
1066 int match_flag;
1067 int remove_flag = FALSE;
1068 int i,j;
1069
1070 if (remove_names == NULL) {
1071 return(FALSE);
1072 }
1073 for (i = 0; remove_names[i] != NULL; i++) {
1074 match_flag = FALSE;
1075 if (exclude_names != NULL) {
1076 for (j = 0; exclude_names[j] != 0; j++) {
1077 if (strcmp(remove_names[i], exclude_names[j]) == 0) {
1078 match_flag = TRUE;
1079 break; 105 break;
1080 } 106 case(STATE_WANT_PURGE):
1081 } 107 want_flag = 'p';
1082 } 108 break;
1083 if (!match_flag) { 109 default:
1084 if (lstat(remove_names[i], &path_stat) < 0) { 110 want_flag = '?';
1085 continue;
1086 } 111 }
1087 if (S_ISDIR(path_stat.st_mode)) { 112 switch(node->state_status) {
1088 if (rmdir(remove_names[i]) != -1) { 113 case(STATE_STATUS_NOTINSTALLED):
1089 remove_flag = TRUE; 114 status_flag = 'n';
1090 } 115 break;
1091 } else { 116 case(STATE_STATUS_UNPACKED):
1092 if (unlink(remove_names[i]) != -1) { 117 status_flag = 'u';
1093 remove_flag = TRUE; 118 break;
1094 } 119 case(STATE_STATUS_HALFCONFIGURED):
120 status_flag = 'h';
121 break;
122 case(STATE_STATUS_INSTALLED):
123 status_flag = 'i';
124 break;
125 case(STATE_STATUS_HALFINSTALLED):
126 status_flag = 'h';
127 break;
128 case(STATE_STATUS_CONFIGFILES):
129 status_flag = 'c';
130 break;
131 default:
132 status_flag = '?';
1095 } 133 }
1096 }
1097 }
1098 return(remove_flag);
1099}
1100 134
1101int run_package_script(const char *package_name, const char *script_type)
1102{
1103 struct stat path_stat;
1104 char *script_path;
1105 int result;
1106
1107 script_path = xmalloc(strlen(package_name) + strlen(script_type) + 21);
1108 sprintf(script_path, "/var/lib/dpkg/info/%s.%s", package_name, script_type);
1109
1110 /* If the file doesnt exist is isnt a fatal */
1111 if (lstat(script_path, &path_stat) < 0) {
1112 result = EXIT_SUCCESS;
1113 } else {
1114 result = system(script_path);
1115 }
1116 free(script_path);
1117 return(result);
1118}
1119
1120void all_control_list(char **remove_files, const char *package_name)
1121{
1122 const char *all_extensions[11] = {"preinst", "postinst", "prerm", "postrm",
1123 "list", "md5sums", "shlibs", "conffiles", "config", "templates", NULL };
1124 int i;
1125
1126 /* Create a list of all /var/lib/dpkg/info/<package> files */
1127 for(i = 0; i < 10; i++) {
1128 remove_files[i] = xmalloc(strlen(package_name) + strlen(all_extensions[i]) + 21);
1129 sprintf(remove_files[i], "/var/lib/dpkg/info/%s.%s", package_name, all_extensions[i]);
1130 }
1131 remove_files[10] = NULL;
1132}
1133
1134
1135/* This function lists information on the installed packages. It loops through
1136 * the status_hashtable to retrieve the info. This results in smaller code than
1137 * scanning the status file. The resulting list, however, is unsorted.
1138 */
1139void list_packages(void)
1140{
1141 int i;
1142
1143 printf(" Name Version\n");
1144 printf("+++-==============-==============\n");
1145
1146 /* go through status hash, dereference package hash and finally strings */
1147 for (i=0; i<STATUS_HASH_PRIME+1; i++) {
1148
1149 if (status_hashtable[i]) {
1150 const char *stat_str; /* status string */
1151 const char *name_str; /* package name */
1152 const char *vers_str; /* version */
1153 char s1, s2; /* status abbreviations */
1154 int spccnt; /* space count */
1155 int j;
1156
1157 stat_str = name_hashtable[status_hashtable[i]->status];
1158 name_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->name];
1159 vers_str = name_hashtable[package_hashtable[status_hashtable[i]->package]->version];
1160
1161 /* get abbreviation for status field 1 */
1162 s1 = stat_str[0] == 'i' ? 'i' : 'r';
1163
1164 /* get abbreviation for status field 2 */
1165 for (j=0, spccnt=0; stat_str[j] && spccnt<2; j++) {
1166 if (stat_str[j] == ' ') spccnt++;
1167 }
1168 s2 = stat_str[j];
1169
1170 /* print out the line formatted like Debian dpkg */ 135 /* print out the line formatted like Debian dpkg */
1171 printf("%c%c %-14s %s\n", s1, s2, name_str, vers_str); 136 if ((status_flag != '?') && (want_flag != '?')) {
137#ifdef BB_FEATURE_DPKG_LIST_SHORT_DESCRIPTIONS
138 printf("%c%c %-14s %-14s %s\n", want_flag, status_flag, name_str, ver_rev, node->description_short);
139#else
140 printf("%c%c %-14s %-14s\n", want_flag, status_flag, name_str, ver_rev);
141#endif
142 }
143 free(ver_rev);
1172 } 144 }
1173 } 145 }
1174} 146}
1175 147#endif
1176void remove_package(const unsigned int package_num)
1177{
1178 const char *package_name = name_hashtable[package_hashtable[package_num]->name];
1179 const unsigned int status_num = search_status_hashtable(package_name);
1180 const int package_name_length = strlen(package_name);
1181 char **remove_files;
1182 char **exclude_files;
1183 char list_name[package_name_length + 25];
1184 char conffile_name[package_name_length + 30];
1185 int return_value;
1186
1187 printf("Removing %s ...\n", package_name);
1188
1189 /* run prerm script */
1190 return_value = run_package_script(package_name, "prerm");
1191 if (return_value == -1) {
1192 error_msg_and_die("script failed, prerm failure");
1193 }
1194
1195 /* Create a list of files to remove, and a seperate list of those to keep */
1196 sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name);
1197 remove_files = create_list(list_name);
1198
1199 sprintf(conffile_name, "/var/lib/dpkg/info/%s.conffiles", package_name);
1200 exclude_files = create_list(conffile_name);
1201
1202 /* Some directories cant be removed straight away, so do multiple passes */
1203 while (remove_file_array(remove_files, exclude_files) == TRUE);
1204
1205 /* Create a list of all /var/lib/dpkg/info/<package> files */
1206 remove_files = xmalloc(11);
1207 all_control_list(remove_files, package_name);
1208
1209 /* Create a list of files in /var/lib/dpkg/info/<package>.* to keep */
1210 exclude_files = xmalloc(sizeof(char*) * 3);
1211 exclude_files[0] = xstrdup(conffile_name);
1212 exclude_files[1] = xmalloc(package_name_length + 27);
1213 sprintf(exclude_files[1], "/var/lib/dpkg/info/%s.postrm", package_name);
1214 exclude_files[2] = NULL;
1215
1216 remove_file_array(remove_files, exclude_files);
1217
1218 /* rename <package>.conffile to <package>.list */
1219 rename(conffile_name, list_name);
1220
1221 /* Change package status */
1222 set_status(status_num, "deinstall", 1);
1223 set_status(status_num, "config-files", 3);
1224}
1225
1226void purge_package(const unsigned int package_num)
1227{
1228 const char *package_name = name_hashtable[package_hashtable[package_num]->name];
1229 const unsigned int status_num = search_status_hashtable(package_name);
1230 char **remove_files;
1231 char **exclude_files;
1232 char list_name[strlen(package_name) + 25];
1233
1234 /* run prerm script */
1235 if (run_package_script(package_name, "prerm") != 0) {
1236 error_msg_and_die("script failed, prerm failure");
1237 }
1238
1239 /* Create a list of files to remove */
1240 sprintf(list_name, "/var/lib/dpkg/info/%s.list", package_name);
1241 remove_files = create_list(list_name);
1242
1243 exclude_files = xmalloc(1);
1244 exclude_files[0] = NULL;
1245
1246 /* Some directories cant be removed straight away, so do multiple passes */
1247 while (remove_file_array(remove_files, exclude_files) == TRUE);
1248
1249 /* Create a list of all /var/lib/dpkg/info/<package> files */
1250 remove_files = xmalloc(11);
1251 all_control_list(remove_files, package_name);
1252 remove_file_array(remove_files, exclude_files);
1253
1254 /* run postrm script */
1255 if (run_package_script(package_name, "postrm") == -1) {
1256 error_msg_and_die("postrm fialure.. set status to what?");
1257 }
1258
1259 /* Change package status */
1260 set_status(status_num, "purge", 1);
1261 set_status(status_num, "not-installed", 3);
1262}
1263
1264void unpack_package(deb_file_t *deb_file)
1265{
1266 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
1267 const unsigned int status_num = search_status_hashtable(package_name);
1268 const unsigned int status_package_num = status_hashtable[status_num]->package;
1269
1270 FILE *out_stream;
1271 char *info_prefix;
1272
1273 /* If existing version, remove it first */
1274 if (strcmp(name_hashtable[get_status(status_num, 3)], "installed") == 0) {
1275 /* Package is already installed, remove old version first */
1276 printf("Preparing to replace %s %s (using %s) ...\n", package_name,
1277 name_hashtable[package_hashtable[status_package_num]->version],
1278 deb_file->filename);
1279 remove_package(status_package_num);
1280 } else {
1281 printf("Unpacking %s (from %s) ...\n", package_name, deb_file->filename);
1282 }
1283
1284 /* Extract control.tar.gz to /var/lib/dpkg/info/<package>.filename */
1285 info_prefix = (char *) xmalloc(sizeof(package_name) + 20 + 4 + 1);
1286 sprintf(info_prefix, "/var/lib/dpkg/info/%s.", package_name);
1287 deb_extract(deb_file->filename, stdout, (extract_quiet | extract_control_tar_gz | extract_all_to_fs | extract_unconditional), info_prefix, NULL);
1288
1289 /* Run the preinst prior to extracting */
1290 if (run_package_script(package_name, "preinst") != 0) {
1291 /* when preinst returns exit code != 0 then quit installation process */
1292 error_msg_and_die("subprocess pre-installation script returned error.");
1293 }
1294
1295 /* Extract data.tar.gz to the root directory */
1296 deb_extract(deb_file->filename, stdout, (extract_quiet | extract_data_tar_gz | extract_all_to_fs | extract_unconditional), "/", NULL);
1297
1298 /* Create the list file */
1299 strcat(info_prefix, "list");
1300 out_stream = xfopen(info_prefix, "w");
1301 deb_extract(deb_file->filename, out_stream, (extract_quiet | extract_data_tar_gz | extract_list), "/", NULL);
1302 fclose(out_stream);
1303
1304 /* change status */
1305 set_status(status_num, "install", 1);
1306 set_status(status_num, "unpacked", 3);
1307
1308 free(info_prefix);
1309}
1310
1311void configure_package(deb_file_t *deb_file)
1312{
1313 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
1314 const char *package_version = name_hashtable[package_hashtable[deb_file->package]->version];
1315 const int status_num = search_status_hashtable(package_name);
1316
1317 printf("Setting up %s (%s)\n", package_name, package_version);
1318
1319 /* Run the postinst script */
1320 if (run_package_script(package_name, "postinst") != 0) {
1321 /* TODO: handle failure gracefully */
1322 error_msg_and_die("postrm failure.. set status to what?");
1323 }
1324 /* Change status to reflect success */
1325 set_status(status_num, "install", 1);
1326 set_status(status_num, "installed", 3);
1327}
1328 148
1329extern int dpkg_main(int argc, char **argv) 149extern int dpkg_main(int argc, char **argv)
1330{ 150{
1331 deb_file_t **deb_file = NULL; 151 deb_file_t **deb_file = xcalloc(1, sizeof(deb_file_t *));
1332 status_node_t *status_node;
1333 char opt = 0; 152 char opt = 0;
1334 int package_num;
1335 int dpkg_opt = 0; 153 int dpkg_opt = 0;
1336 int deb_count = 0; 154 int deb_count = 0;
1337 int state_status;
1338 int status_num;
1339 int i; 155 int i;
1340 156 unsigned short state_default_new_want = STATE_WANT_INSTALL;
1341 while ((opt = getopt(argc, argv, "CF:ilPru")) != -1) { 157 unsigned short state_default_new_flag = STATE_FLAG_REINSTREQ;
158 unsigned short state_default_new_status = STATE_STATUS_NOTINSTALLED;
159
160 while ((opt = getopt(argc, argv,
161#ifdef BB_FEATURE_DPKG_LIST
162 "CF:ilPru")) != -1) {
163#else
164 "CF:iPru")) != -1) {
165#endif
1342 switch (opt) { 166 switch (opt) {
1343 case 'C': // equivalent to --configure in official dpkg 167 case 'C': // equivalent to --configure in official dpkg
1344 dpkg_opt |= dpkg_opt_configure; 168 dpkg_opt |= DPKG_OPT_CONFIGURE | DPKG_OPT_PACKAGENAME;
1345 dpkg_opt |= dpkg_opt_package_name;
1346 break; 169 break;
1347 case 'F': // equivalent to --force in official dpkg 170 case 'F': // equivalent to --force in official dpkg
1348 if (strcmp(optarg, "depends") == 0) { 171 if (strcmp(optarg, "depends") == 0) {
1349 dpkg_opt |= dpkg_opt_force_ignore_depends; 172 dpkg_opt |= DPKG_OPT_FORCE_IGNORE_DEPENDS;
1350 } 173 }
1351 case 'i': 174 case 'i':
1352 dpkg_opt |= dpkg_opt_install; 175 dpkg_opt |= DPKG_OPT_INSTALL | DPKG_OPT_FILENAME;
1353 dpkg_opt |= dpkg_opt_filename;
1354 break; 176 break;
177#ifdef BB_FEATURE_DPKG_LIST
1355 case 'l': 178 case 'l':
1356 dpkg_opt |= dpkg_opt_list_installed; 179 dpkg_opt |= DPKG_OPT_LIST_INSTALLED;
1357 break; 180 break;
181#endif
1358 case 'P': 182 case 'P':
1359 dpkg_opt |= dpkg_opt_purge; 183 dpkg_opt |= DPKG_OPT_PURGE | DPKG_OPT_PACKAGENAME;
1360 dpkg_opt |= dpkg_opt_package_name; 184 state_default_new_want = STATE_WANT_PURGE;
1361 break; 185 break;
1362 case 'r': 186 case 'r':
1363 dpkg_opt |= dpkg_opt_remove; 187 dpkg_opt |= DPKG_OPT_REMOVE | DPKG_OPT_PACKAGENAME;
1364 dpkg_opt |= dpkg_opt_package_name; 188 state_default_new_want = STATE_WANT_DEINSTALL;
1365 break; 189 break;
1366 case 'u': /* Equivalent to --unpack in official dpkg */ 190 case 'u': /* Equivalent to --unpack in official dpkg */
1367 dpkg_opt |= dpkg_opt_unpack; 191 dpkg_opt |= DPKG_OPT_UNPACK | DPKG_OPT_FILENAME;
1368 dpkg_opt |= dpkg_opt_filename;
1369 break; 192 break;
1370 default: 193 default:
1371 show_usage(); 194 show_usage();
1372 } 195 }
1373 } 196 }
197
1374 /* check for non-otion argument if expected */ 198 /* check for non-otion argument if expected */
1375 if ((dpkg_opt == 0) || ((argc == optind) && !(dpkg_opt && dpkg_opt_list_installed))) { 199 if ((dpkg_opt == 0) || ((dpkg_opt & DPKG_OPT_FILENAME) && (dpkg_opt & DPKG_OPT_PACKAGENAME))) {
1376 show_usage(); 200 show_usage();
1377 } 201 }
1378 202
1379/* puts("(Reading database ... xxxxx files and directories installed.)"); */ 203 deb_initialise_hashtables(1);
1380 index_status_file("/var/lib/dpkg/status"); 204 index_status_file("/var/lib/dpkg/status");
1381 205
206#ifdef BB_FEATURE_DPKG_LIST
1382 /* if the list action was given print the installed packages and exit */ 207 /* if the list action was given print the installed packages and exit */
1383 if (dpkg_opt & dpkg_opt_list_installed) { 208 if (dpkg_opt & DPKG_OPT_LIST_INSTALLED) {
1384 list_packages(); 209 list_packages();
1385 return(EXIT_SUCCESS);
1386 } 210 }
1387 211#endif
212
1388 /* Read arguments and store relevant info in structs */ 213 /* Read arguments and store relevant info in structs */
1389 deb_file = xmalloc(sizeof(deb_file_t));
1390 while (optind < argc) { 214 while (optind < argc) {
1391 deb_file[deb_count] = (deb_file_t *) xmalloc(sizeof(deb_file_t)); 215 const char *arg_name = argv[optind];
1392 if (dpkg_opt & dpkg_opt_filename) { 216 unsigned int node_num;
1393 deb_file[deb_count]->filename = xstrdup(argv[optind]); 217 deb_file_t *tmp;
1394 deb_file[deb_count]->control_file = deb_extract(argv[optind], stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control"); 218 node_t *node;
1395 if (deb_file[deb_count]->control_file == NULL) { 219
1396 error_msg_and_die("Couldnt extract control file"); 220 tmp = (deb_file_t *) xcalloc(sizeof(deb_file_t), 1);
1397 } 221
1398 package_num = fill_package_struct(deb_file[deb_count]->control_file); 222 if (dpkg_opt & DPKG_OPT_FILENAME) {
1399 223 tmp->filename = xstrdup(arg_name);
1400 if (package_num == -1) { 224 /* Extract the control file */
1401 error_msg("Invalid control file in %s", argv[optind]); 225 tmp->control_file = deb_extract(arg_name, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control");
226 if (tmp->control_file == NULL) {
227 error_msg("Couldnt extract control file from, ignoring package %s", arg_name);
228 optind++;
229 free(tmp->filename);
230 free(tmp);
1402 continue; 231 continue;
1403 } 232 }
1404 deb_file[deb_count]->package = (unsigned int) package_num; 233 node = parse_package_metadata(tmp->control_file);
1405 /* Add the package to the status hashtable */ 234
1406 if ((dpkg_opt & dpkg_opt_unpack) || (dpkg_opt & dpkg_opt_install)) { 235 if (node == NULL) {
1407 status_node = (status_node_t *) xmalloc(sizeof(status_node_t)); 236 error_msg_and_die("node was null\n");
1408 status_node->package = deb_file[deb_count]->package; 237 } else {
238 node_num = search_node_ht(node, CMP_ANY);
239 node->state_want = state_default_new_want;
240 node->state_flag = state_default_new_flag;
241 node->state_status = state_default_new_status;
1409 242
1410 /* Try and find a currently installed version of this package */ 243 if (get_node_ht(node_num) == NULL) {
1411 status_num = search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); 244 printf("Selecting previously deselected package %s\n", get_name_ht(node->name));
1412 /* If no previous entry was found initialise a new entry */
1413 if ((status_hashtable[status_num] == NULL) ||
1414 (status_hashtable[status_num]->status == 0)) {
1415 /* reinstreq isnt changed to "ok" until the package control info
1416 * is written to the status file*/
1417 status_node->status = search_name_hashtable("install reinstreq not-installed");
1418 status_hashtable[status_num] = status_node;
1419 } else {
1420 status_hashtable[status_num]->status = search_name_hashtable("install reinstreq installed");
1421 } 245 }
246 add_node(node, node_num);
1422 } 247 }
1423 } 248 } else {
1424 else if (dpkg_opt & dpkg_opt_package_name) { 249 /* It must be a package name */
1425 deb_file[deb_count]->filename = NULL; 250 node = initialise_node(arg_name);
1426 deb_file[deb_count]->control_file = NULL; 251 node_num = search_node_ht(node, CMP_ANY);
1427 deb_file[deb_count]->package = search_package_hashtable( 252 free_node(node);
1428 search_name_hashtable(argv[optind]), 253
1429 search_name_hashtable("ANY"), VER_ANY); 254 node = get_node_ht(node_num);
1430 if (package_hashtable[deb_file[deb_count]->package] == NULL) { 255 if (node == NULL) {
1431 error_msg_and_die("Package %s is uninstalled or unknown\n", argv[optind]); 256 error_msg_and_die("Package %s is uninstalled or unknown\n", arg_name);
1432 } 257 }
1433 state_status = get_status(search_status_hashtable(name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]), 3); 258
259 node->state_want = state_default_new_want;
1434 260
1435 /* check package status is "installed" */ 261 /* check package status is "installed" */
1436 if (dpkg_opt & dpkg_opt_remove) { 262 if (dpkg_opt & DPKG_OPT_REMOVE) {
1437 if ((strcmp(name_hashtable[state_status], "not-installed") == 0) || 263 if ((node->state_status == STATE_STATUS_NOTINSTALLED) ||
1438 (strcmp(name_hashtable[state_status], "config-files") == 0)) { 264 (node->state_status == STATE_STATUS_CONFIGFILES)) {
1439 error_msg_and_die("%s is already removed.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]); 265 error_msg_and_die("%s is already removed.", arg_name);
1440 } 266 }
1441 } 267 }
1442 else if (dpkg_opt & dpkg_opt_purge) { 268 else if (dpkg_opt & DPKG_OPT_PURGE) {
1443 /* if package status is "conf-files" then its ok */ 269 if (node->state_status == STATE_STATUS_NOTINSTALLED) {
1444 if (strcmp(name_hashtable[state_status], "not-installed") == 0) { 270 error_msg_and_die("%s is already purged.", arg_name);
1445 error_msg_and_die("%s is already purged.", name_hashtable[package_hashtable[deb_file[deb_count]->package]->name]);
1446 } 271 }
1447 } 272 }
1448 } 273 }
274
275 tmp->node = node_num;
276 deb_file = realloc(deb_file, sizeof(deb_file_t) * (deb_count + 1));
277 deb_file[deb_count] = tmp;
278
1449 deb_count++; 279 deb_count++;
280 deb_file[deb_count] = NULL;
281
1450 optind++; 282 optind++;
1451 } 283 }
1452 deb_file[deb_count] = NULL;
1453 284
285#if 0
1454 /* Check that the deb file arguments are installable */ 286 /* Check that the deb file arguments are installable */
1455 /* TODO: check dependencies before removing */ 287 /* TODO: check dependencies before removing */
1456 if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) { 288 if ((dpkg_opt & dpkg_opt_force_ignore_depends) != dpkg_opt_force_ignore_depends) {
1457 if (!check_deps(deb_file, 0, deb_count)) { 289 /* */
1458 error_msg_and_die("Dependency check failed"); 290 for (i = 0; i < deb_count; i++) {
291 /* add filename to list */
1459 } 292 }
1460 }
1461 293
294 /* Sort packages into groups and add dependencies */
295 find_deps(package_name);
296
297 /* Check all packages in groups and sub groups are installed */
298 }
299#endif
1462 for (i = 0; i < deb_count; i++) { 300 for (i = 0; i < deb_count; i++) {
1463 /* Remove or purge packages */ 301 /* Remove or purge packages */
1464 if (dpkg_opt & dpkg_opt_remove) { 302 if (dpkg_opt & DPKG_OPT_REMOVE) {
1465 remove_package(deb_file[i]->package); 303 remove_package(deb_file[i]->node);
1466 } 304 }
1467 else if (dpkg_opt & dpkg_opt_purge) { 305 else if (dpkg_opt & DPKG_OPT_PURGE) {
1468 purge_package(deb_file[i]->package); 306 purge_package(deb_file[i]->node);
1469 } 307 }
1470 else if (dpkg_opt & dpkg_opt_unpack) { 308 else if (dpkg_opt & DPKG_OPT_UNPACK) {
1471 unpack_package(deb_file[i]); 309 unpack_package(deb_file[i]);
1472 } 310 }
1473 else if (dpkg_opt & dpkg_opt_install) { 311 else if (dpkg_opt & DPKG_OPT_INSTALL) {
1474 unpack_package(deb_file[i]); 312 unpack_package(deb_file[i]);
1475 configure_package(deb_file[i]); 313 configure_package(deb_file[i]);
1476 } 314 }
1477 else if (dpkg_opt & dpkg_opt_configure) { 315 else if (dpkg_opt & DPKG_OPT_CONFIGURE) {
1478 configure_package(deb_file[i]); 316 configure_package(deb_file[i]);
1479 } 317 }
1480 } 318 }
1481 319
1482 write_status_file(deb_file); 320 write_status_file(deb_file);
1483 321
1484 for (i = 0; i < deb_count; i++) { 322 if (deb_file != NULL) {
1485 free(deb_file[i]->control_file); 323 for (i = 0; i < deb_count; i++) {
1486 free(deb_file[i]->filename); 324 free(deb_file[i]->control_file);
1487 free(deb_file[i]); 325 free(deb_file[i]->filename);
1488 } 326 free(deb_file[i]);
1489 free(deb_file);
1490
1491 for (i = 0; i < NAME_HASH_PRIME; i++) {
1492 if (name_hashtable[i] != NULL) {
1493 free(name_hashtable[i]);
1494 }
1495 }
1496
1497 for (i = 0; i < PACKAGE_HASH_PRIME; i++) {
1498 free_package(package_hashtable[i]);
1499 }
1500
1501 for (i = 0; i < STATUS_HASH_PRIME; i++) {
1502 if (status_hashtable[i] != NULL) {
1503 free(status_hashtable[i]);
1504 } 327 }
328 free(deb_file);
1505 } 329 }
330 free_hashtables();
1506 331
1507 return(EXIT_SUCCESS); 332 return(EXIT_SUCCESS);
1508} 333}
diff --git a/include/libbb.h b/include/libbb.h
index 8b84077d8..90b1c25c6 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -318,5 +318,49 @@ extern const char * const can_not_create_raw_socket;
318/* The following devices are the same on devfs and non-devfs systems. */ 318/* The following devices are the same on devfs and non-devfs systems. */
319#define CURRENT_TTY "/dev/tty" 319#define CURRENT_TTY "/dev/tty"
320#define CONSOLE_DEV "/dev/console" 320#define CONSOLE_DEV "/dev/console"
321typedef struct deb_file_s {
322 char *filename;
323 char *control_file;
324 unsigned short node:14;
325} deb_file_t;
326
327typedef struct node_s {
328 /* This are always used by dpkg */
329 unsigned short name:14;
330 unsigned short epoch:4;
331 unsigned short version:12;
332 unsigned short revision:8;
333 unsigned short essential:1;
334 unsigned short state_want:3;
335 unsigned short state_flag:2;
336 unsigned short state_status:3;
337 unsigned short pool_flag:2;
338 unsigned short source:11;
339 unsigned short filename:11;
340 unsigned short num_of_edges:7;
341 unsigned short *edge;
342#ifdef BB_FEATURE_DPKG_LIST_SHORT_DESCRIPTIONS
343 char *description_short; /* This is used by dpkg -l */
344#endif
345} node_t;
346
347/* All these are in deb_functs.c */
348extern void deb_initialise_hashtables(short type);
349extern void write_status_file(deb_file_t **deb_file);
350extern void configure_package(deb_file_t *deb_file);
351extern void unpack_package(deb_file_t *deb_file);
352extern void remove_package(const unsigned int node_num);
353extern void index_status_file(const char *filename);
354extern void find_deps(const char *package_name);
355extern void purge_package(const unsigned int node_num);
356extern node_t *parse_package_metadata(char *control_buffer);
357extern unsigned short search_node_ht(node_t *search_node, unsigned short operator);
358extern void add_node(node_t *node, unsigned short node_num);
359extern node_t *initialise_node(const char *package_name);
360extern char *get_name_ht(unsigned short name_num);
361extern void free_hashtables(void);
362extern node_t *get_node_ht(unsigned short node_num);
363extern char *version_revision(unsigned short version_num, unsigned short revision_num);
364extern void free_node(node_t *node);
321 365
322#endif /* __LIBCONFIG_H__ */ 366#endif /* __LIBCONFIG_H__ */
diff --git a/libbb/Makefile b/libbb/Makefile
index 60c3dda97..0aeaaf79d 100644
--- a/libbb/Makefile
+++ b/libbb/Makefile
@@ -38,7 +38,7 @@ obj-n :=
38obj- := 38obj- :=
39 39
40obj-y += ask_confirmation.o chomp.o concat_path_file.o copy_file.o \ 40obj-y += ask_confirmation.o chomp.o concat_path_file.o copy_file.o \
41 copy_file_chunk.o libc5.o device_open.o error_msg.o \ 41 copy_file_chunk.o deb_functs.o libc5.o device_open.o error_msg.o \
42 error_msg_and_die.o fgets_str.o find_mount_point.o find_pid_by_name.o \ 42 error_msg_and_die.o fgets_str.o find_mount_point.o find_pid_by_name.o \
43 find_root_device.o full_read.o full_write.o get_console.o \ 43 find_root_device.o full_read.o full_write.o get_console.o \
44 get_last_path_component.o get_line_from_file.o gz_open.o human_readable.o \ 44 get_last_path_component.o get_line_from_file.o gz_open.o human_readable.o \
diff --git a/networking/Makefile b/networking/Makefile
index 4dd0cdb56..bc110eefc 100644
--- a/networking/Makefile
+++ b/networking/Makefile
@@ -24,7 +24,7 @@ obj-y :=
24obj-n := 24obj-n :=
25obj- := 25obj- :=
26 26
27 27obj-$(CONFIG_APT_GET) += apt_get.o
28obj-$(CONFIG_HOSTNAME) += hostname.o 28obj-$(CONFIG_HOSTNAME) += hostname.o
29obj-$(CONFIG_IFCONFIG) += ifconfig.o 29obj-$(CONFIG_IFCONFIG) += ifconfig.o
30obj-$(CONFIG_NC) += nc.o 30obj-$(CONFIG_NC) += nc.o
diff --git a/networking/config.in b/networking/config.in
index 577d925c3..95cda42d8 100644
--- a/networking/config.in
+++ b/networking/config.in
@@ -6,6 +6,7 @@
6mainmenu_option next_comment 6mainmenu_option next_comment
7comment 'Networking Utilities' 7comment 'Networking Utilities'
8 8
9bool 'apt_get' CONFIG_APT_GET
9bool 'hostname' CONFIG_HOSTNAME 10bool 'hostname' CONFIG_HOSTNAME
10bool 'ifconfig' CONFIG_IFCONFIG 11bool 'ifconfig' CONFIG_IFCONFIG
11bool 'nc' CONFIG_NC 12bool 'nc' CONFIG_NC