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