Add macosx fix from #1407
[openwrt.git] / package / busybox / patches / 911-ipkg.patch
1 diff -urN busybox.old/archival/Config.in busybox.dev/archival/Config.in
2 --- busybox.old/archival/Config.in      2007-01-19 22:23:02.000000000 +0100
3 +++ busybox.dev/archival/Config.in      2007-01-22 13:41:03.000000000 +0100
4 @@ -121,6 +121,14 @@
5           gzip is used to compress files.
6           It's probably the most widely used UNIX compression program.
7  
8 +config IPKG
9 +       bool "ipkg"
10 +       default n
11 +       select MD5SUM
12 +       select WGET
13 +       help
14 +         ipkg is the itsy package management system.
15 +
16  config RPM2CPIO
17         bool "rpm2cpio"
18         default n
19 diff -urN busybox.old/archival/dpkg.c busybox.dev/archival/dpkg.c
20 --- busybox.old/archival/dpkg.c 2007-01-19 22:23:02.000000000 +0100
21 +++ busybox.dev/archival/dpkg.c 2007-01-22 13:41:03.000000000 +0100
22 @@ -1463,6 +1463,10 @@
23         return ar_handle->sub_archive->buffer;
24  }
25  
26 +/*
27 +
28 +// moved to data_extract_all.c
29 +
30  static void data_extract_all_prefix(archive_handle_t *archive_handle)
31  {
32         char *name_ptr = archive_handle->file_header->name;
33 @@ -1475,6 +1479,8 @@
34         return;
35  }
36  
37 +*/
38 +
39  static void unpack_package(deb_file_t *deb_file)
40  {
41         const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
42 diff -urN busybox.old/archival/ipkg.c busybox.dev/archival/ipkg.c
43 --- busybox.old/archival/ipkg.c 1970-01-01 01:00:00.000000000 +0100
44 +++ busybox.dev/archival/ipkg.c 2007-01-22 13:41:03.000000000 +0100
45 @@ -0,0 +1,26 @@
46 +/* ipkg.c - the itsy package management system
47 +
48 +   Florina Boor
49 +
50 +   Copyright (C) 2003 kernel concepts
51 +
52 +   This program is free software; you can redistribute it and/or
53 +   modify it under the terms of the GNU General Public License as
54 +   published by the Free Software Foundation; either version 2, or (at
55 +   your option) any later version.
56 +
57 +   This program is distributed in the hope that it will be useful, but
58 +   WITHOUT ANY WARRANTY; without even the implied warranty of
59 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
60 +   General Public License for more details.
61 +   
62 +   ipkg command line frontend using libipkg
63 +   
64 +*/
65 +
66 +#include "libipkg/libipkg.h"
67 +
68 +int ipkg_main(int argc, char **argv)
69 +{
70 +       return ipkg_op(argc, argv);
71 +}
72 diff -urN busybox.old/archival/Kbuild busybox.dev/archival/Kbuild
73 --- busybox.old/archival/Kbuild 2007-01-19 22:23:02.000000000 +0100
74 +++ busybox.dev/archival/Kbuild 2007-01-22 13:41:03.000000000 +0100
75 @@ -15,6 +15,7 @@
76  lib-$(CONFIG_DPKG_DEB)         += dpkg_deb.o
77  lib-$(CONFIG_GUNZIP)           += gunzip.o
78  lib-$(CONFIG_GZIP)             += gzip.o
79 +lib-$(CONFIG_IPKG)             += ipkg.o
80  lib-$(CONFIG_RPM2CPIO)         += rpm2cpio.o
81  lib-$(CONFIG_RPM)              += rpm.o
82  lib-$(CONFIG_TAR)              += tar.o
83 diff -urN busybox.old/archival/libipkg/args.c busybox.dev/archival/libipkg/args.c
84 --- busybox.old/archival/libipkg/args.c 1970-01-01 01:00:00.000000000 +0100
85 +++ busybox.dev/archival/libipkg/args.c 2007-01-22 13:41:03.000000000 +0100
86 @@ -0,0 +1,242 @@
87 +/* args.c - parse command-line args
88
89 +  Carl D. Worth
90 +
91 +  Copyright 2001 University of Southern California
92
93 +  This program is free software; you can redistribute it and/or modify
94 +  it under the terms of the GNU General Public License as published by
95 +  the Free Software Foundation; either version 2, or (at your option)
96 +  any later version.
97
98 +  This program is distributed in the hope that it will be useful,
99 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
100 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
101 +  GNU General Public License for more details.
102 + */
103 +
104 +#include <getopt.h>
105 +#include <stdlib.h>
106 +#include <string.h>
107 +#include <unistd.h>
108 +
109 +#include "ipkg.h"
110 +#include "ipkg_message.h"
111 +
112 +#include "args.h"
113 +#include "sprintf_alloc.h"
114 +
115 +#include "libbb.h"
116 +
117 +
118 +static void print_version(void);
119 +
120 +enum long_args_opt
121 +{
122 +     ARGS_OPT_FORCE_DEFAULTS = 129,
123 +     ARGS_OPT_FORCE_DEPENDS,
124 +     ARGS_OPT_FORCE_OVERWRITE,
125 +     ARGS_OPT_FORCE_DOWNGRADE,
126 +     ARGS_OPT_FORCE_REINSTALL,
127 +     ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
128 +     ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
129 +     ARGS_OPT_FORCE_SPACE,
130 +     ARGS_OPT_NOACTION,
131 +     ARGS_OPT_NODEPS,
132 +     ARGS_OPT_VERBOSE_WGET,
133 +     ARGS_OPT_VERBOSITY,
134 +     ARGS_OPT_MULTIPLE_PROVIDERS
135 +};
136 +
137 +int args_init(args_t *args)
138 +{
139 +     char *conf_file_dir;
140 +
141 +     memset(args, 0, sizeof(args_t));
142 +
143 +     args->dest = ARGS_DEFAULT_DEST;
144 +
145 +     conf_file_dir = getenv("IPKG_CONF_DIR");
146 +     if (conf_file_dir == NULL || conf_file_dir[0] == '\0') {
147 +         conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
148 +     }
149 +     sprintf_alloc(&args->conf_file, "%s/%s", conf_file_dir,
150 +                  ARGS_DEFAULT_CONF_FILE_NAME);
151 +
152 +     args->force_defaults = ARGS_DEFAULT_FORCE_DEFAULTS;
153 +     args->force_depends = ARGS_DEFAULT_FORCE_DEPENDS;
154 +     args->force_overwrite = ARGS_DEFAULT_FORCE_OVERWRITE;
155 +     args->force_downgrade = ARGS_DEFAULT_FORCE_DOWNGRADE;
156 +     args->force_reinstall = ARGS_DEFAULT_FORCE_REINSTALL;
157 +     args->force_removal_of_dependent_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES;
158 +     args->force_removal_of_essential_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES;
159 +     args->noaction = ARGS_DEFAULT_NOACTION;
160 +     args->nodeps = ARGS_DEFAULT_NODEPS;
161 +     args->verbose_wget = ARGS_DEFAULT_VERBOSE_WGET;
162 +     args->verbosity = ARGS_DEFAULT_VERBOSITY;
163 +     args->offline_root = ARGS_DEFAULT_OFFLINE_ROOT;
164 +     args->offline_root_pre_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD;
165 +     args->offline_root_post_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD;
166 +     args->multiple_providers = 0;
167 +     args->nocheckfordirorfile = 0;
168 +     args->noreadfeedsfile = 0;
169 +
170 +     return 1;
171 +}
172 +
173 +void args_deinit(args_t *args)
174 +{
175 +     free(args->conf_file);
176 +     args->conf_file = NULL;
177 +}
178 +
179 +int args_parse(args_t *args, int argc, char *argv[])
180 +{
181 +     int c;
182 +     int option_index = 0;
183 +     int parse_err = 0;
184 +     static struct option long_options[] = {
185 +         {"query-all", 0, 0, 'A'},
186 +         {"conf-file", 1, 0, 'f'},
187 +         {"conf", 1, 0, 'f'},
188 +         {"dest", 1, 0, 'd'},
189 +         {"force-defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
190 +         {"force_defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
191 +         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
192 +         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
193 +         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
194 +         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
195 +         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
196 +         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
197 +         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
198 +         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
199 +         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
200 +         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
201 +         {"recursive", 0, 0,
202 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
203 +         {"force-removal-of-dependent-packages", 0, 0,
204 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
205 +         {"force_removal_of_dependent_packages", 0, 0,
206 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
207 +         {"force-removal-of-essential-packages", 0, 0,
208 +          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
209 +         {"force_removal_of_essential_packages", 0, 0,
210 +          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
211 +         {"multiple-providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
212 +         {"multiple_providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
213 +         {"noaction", 0, 0, ARGS_OPT_NOACTION},
214 +         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
215 +         {"offline", 1, 0, 'o'},
216 +         {"offline-root", 1, 0, 'o'},
217 +         {"test", 0, 0, ARGS_OPT_NOACTION},
218 +         {"tmp-dir", 1, 0, 't'},
219 +         {"verbose-wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
220 +         {"verbose_wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
221 +         {"verbosity", 2, 0, 'V'},
222 +         {"version", 0, 0, 'v'},
223 +         {0, 0, 0, 0}
224 +     };
225 +
226 +     while (1) {
227 +         c = getopt_long_only(argc, argv, "Ad:f:no:t:vV:", long_options, &option_index);
228 +         if (c == -1)
229 +              break;
230 +
231 +         switch (c) {
232 +         case 'A':
233 +              args->query_all = 1;
234 +              break;
235 +         case 'd':
236 +              args->dest = optarg;
237 +              break;
238 +         case 'f':
239 +              free(args->conf_file);
240 +              args->conf_file = strdup(optarg);
241 +              break;
242 +         case 'o':
243 +              args->offline_root = optarg;
244 +              break;
245 +         case 'n':
246 +              args->noaction = 1;
247 +              break;
248 +         case 't':
249 +              args->tmp_dir = strdup(optarg);
250 +              break;
251 +         case 'v':
252 +              print_version();
253 +              exit(0);
254 +         case 'V':
255 +         case ARGS_OPT_VERBOSITY:
256 +              if (optarg)
257 +                   args->verbosity = atoi(optarg);
258 +              else
259 +                   args->verbosity += 1;
260 +              break;
261 +         case ARGS_OPT_FORCE_DEFAULTS:
262 +              args->force_defaults = 1;
263 +              break;
264 +         case ARGS_OPT_FORCE_DEPENDS:
265 +              args->force_depends = 1;
266 +              break;
267 +         case ARGS_OPT_FORCE_OVERWRITE:
268 +              args->force_overwrite = 1;
269 +              break;
270 +         case ARGS_OPT_FORCE_DOWNGRADE:
271 +              args->force_downgrade = 1;
272 +              break;
273 +         case ARGS_OPT_FORCE_REINSTALL:
274 +              args->force_reinstall = 1;
275 +              break;
276 +         case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
277 +              args->force_removal_of_essential_packages = 1;
278 +              break;
279 +         case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
280 +              args->force_removal_of_dependent_packages = 1;
281 +              break;
282 +         case ARGS_OPT_FORCE_SPACE:
283 +              args->force_space = 1;
284 +              break;
285 +         case ARGS_OPT_VERBOSE_WGET:
286 +              args->verbose_wget = 1;
287 +              break;
288 +         case ARGS_OPT_MULTIPLE_PROVIDERS:
289 +              args->multiple_providers = 1;
290 +              break;
291 +         case ARGS_OPT_NODEPS:
292 +              args->nodeps = 1;
293 +              break;
294 +         case ARGS_OPT_NOACTION:
295 +              args->noaction = 1;
296 +              break;
297 +         case ':':
298 +              parse_err++;
299 +              break;
300 +         case '?':
301 +              parse_err++;
302 +              break;
303 +         default:
304 +              bb_error_msg("Confusion: getopt_long returned %d\n", c);
305 +         }
306 +     }
307 +    
308 +     if (parse_err) {
309 +         return -parse_err;
310 +     } else {
311 +         return optind;
312 +     }
313 +}
314 +
315 +void args_usage(char *complaint)
316 +{
317 +     if (complaint) {
318 +          bb_error_msg("%s\n", complaint);
319 +     }
320 +     print_version();
321 +     bb_show_usage();
322 +     exit(1);
323 +}
324 +
325 +static void print_version(void)
326 +{
327 +       bb_error_msg("version %s\n", IPKG_VERSION);
328 +}
329 diff -urN busybox.old/archival/libipkg/args.h busybox.dev/archival/libipkg/args.h
330 --- busybox.old/archival/libipkg/args.h 1970-01-01 01:00:00.000000000 +0100
331 +++ busybox.dev/archival/libipkg/args.h 2007-01-22 13:41:03.000000000 +0100
332 @@ -0,0 +1,72 @@
333 +/* args.h - parse command-line args
334 +
335 +  Carl D. Worth
336 +
337 +  Copyright 2001 University of Southern California
338
339 +  This program is free software; you can redistribute it and/or modify
340 +  it under the terms of the GNU General Public License as published by
341 +  the Free Software Foundation; either version 2, or (at your option)
342 +  any later version.
343
344 +  This program is distributed in the hope that it will be useful,
345 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
346 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
347 +  GNU General Public License for more details.
348 +*/
349 +
350 +#ifndef ARGS_H
351 +#define ARGS_H
352 +
353 +struct args
354 +{
355 +    char *conf_file;
356 +    char *dest;
357 +    char *tmp_dir;
358 +    int force_defaults;
359 +    int force_depends;
360 +    int force_overwrite;
361 +    int force_downgrade;
362 +    int force_reinstall;
363 +    int force_removal_of_essential_packages;
364 +    int force_removal_of_dependent_packages;
365 +    int force_space;
366 +    int noaction;
367 +    int nodeps;
368 +    int multiple_providers;
369 +    int query_all;
370 +    int verbose_wget;
371 +    int verbosity;
372 +    int nocheckfordirorfile;
373 +    int noreadfeedsfile;
374 +    char *offline_root;
375 +    char *offline_root_pre_script_cmd;
376 +    char *offline_root_post_script_cmd;
377 +};
378 +typedef struct args args_t;
379 +
380 +#define ARGS_DEFAULT_CONF_FILE_DIR "/etc"
381 +#define ARGS_DEFAULT_CONF_FILE_NAME "ipkg.conf"
382 +#define ARGS_DEFAULT_DEST NULL
383 +#define ARGS_DEFAULT_FORCE_DEFAULTS 0
384 +#define ARGS_DEFAULT_FORCE_DEPENDS 0
385 +#define ARGS_DEFAULT_FORCE_OVERWRITE 0 
386 +#define ARGS_DEFAULT_FORCE_DOWNGRADE 0 
387 +#define ARGS_DEFAULT_FORCE_REINSTALL 0
388 +#define ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES 0
389 +#define ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES 0
390 +#define ARGS_DEFAULT_FORCE_SPACE 0
391 +#define ARGS_DEFAULT_OFFLINE_ROOT NULL
392 +#define ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD NULL
393 +#define ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD NULL
394 +#define ARGS_DEFAULT_NOACTION 0
395 +#define ARGS_DEFAULT_NODEPS 0
396 +#define ARGS_DEFAULT_VERBOSE_WGET 0
397 +#define ARGS_DEFAULT_VERBOSITY 1
398 +
399 +int args_init(args_t *args);
400 +void args_deinit(args_t *args);
401 +int args_parse(args_t *args, int argc, char *argv[]);
402 +void args_usage(char *complaint);
403 +
404 +#endif
405 diff -urN busybox.old/archival/libipkg/conffile.c busybox.dev/archival/libipkg/conffile.c
406 --- busybox.old/archival/libipkg/conffile.c     1970-01-01 01:00:00.000000000 +0100
407 +++ busybox.dev/archival/libipkg/conffile.c     2007-01-22 13:41:03.000000000 +0100
408 @@ -0,0 +1,64 @@
409 +/* conffile.c - the itsy package management system
410 +
411 +   Carl D. Worth
412 +
413 +   Copyright (C) 2001 University of Southern California
414 +
415 +   This program is free software; you can redistribute it and/or
416 +   modify it under the terms of the GNU General Public License as
417 +   published by the Free Software Foundation; either version 2, or (at
418 +   your option) any later version.
419 +
420 +   This program is distributed in the hope that it will be useful, but
421 +   WITHOUT ANY WARRANTY; without even the implied warranty of
422 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
423 +   General Public License for more details.
424 +*/
425 +
426 +#include <string.h>
427 +#include <stdlib.h>
428 +
429 +#include "ipkg.h"
430 +#include "ipkg_message.h"
431 +
432 +#include "conffile.h"
433 +#include "file_util.h"
434 +#include "sprintf_alloc.h"
435 +
436 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum)
437 +{
438 +    return nv_pair_init(conffile, file_name, md5sum);
439 +}
440 +
441 +void conffile_deinit(conffile_t *conffile)
442 +{
443 +    nv_pair_deinit(conffile);
444 +}
445 +
446 +int conffile_has_been_modified(ipkg_conf_t *conf, conffile_t *conffile)
447 +{
448 +    char *md5sum;
449 +    char *filename = conffile->name;
450 +    char *root_filename;
451 +    int ret;
452 +
453 +    if (conffile->value == NULL) {
454 +        ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s has no md5sum\n", __FUNCTION__, conffile->name);
455 +        return 1;
456 +    }
457 +
458 +    root_filename = root_filename_alloc(conf, filename);
459 +
460 +    md5sum = file_md5sum_alloc(root_filename);
461 +
462 +    ret = strcmp(md5sum, conffile->value);
463 +    if (ret) {
464 +      ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s: \t\nold md5=%s \t\nnew md5=%s\n", __FUNCTION__,
465 +              conffile->name, md5sum, conffile->value);
466 +    }
467 +
468 +    free(root_filename);
469 +    free(md5sum);
470 +
471 +    return ret;
472 +}
473 diff -urN busybox.old/archival/libipkg/conffile.h busybox.dev/archival/libipkg/conffile.h
474 --- busybox.old/archival/libipkg/conffile.h     1970-01-01 01:00:00.000000000 +0100
475 +++ busybox.dev/archival/libipkg/conffile.h     2007-01-22 13:41:03.000000000 +0100
476 @@ -0,0 +1,30 @@
477 +/* conffile.h - the itsy package management system
478 +
479 +   Carl D. Worth
480 +
481 +   Copyright (C) 2001 University of Southern California
482 +
483 +   This program is free software; you can redistribute it and/or
484 +   modify it under the terms of the GNU General Public License as
485 +   published by the Free Software Foundation; either version 2, or (at
486 +   your option) any later version.
487 +
488 +   This program is distributed in the hope that it will be useful, but
489 +   WITHOUT ANY WARRANTY; without even the implied warranty of
490 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
491 +   General Public License for more details.
492 +*/
493 +
494 +#ifndef CONFFILE_H
495 +#define CONFFILE_H
496 +
497 +#include "nv_pair.h"
498 +
499 +typedef struct nv_pair conffile_t;
500 +
501 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum);
502 +void conffile_deinit(conffile_t *conffile);
503 +int conffile_has_been_modified(struct ipkg_conf *conf, conffile_t *conffile);
504 +
505 +#endif
506 +
507 diff -urN busybox.old/archival/libipkg/conffile_list.c busybox.dev/archival/libipkg/conffile_list.c
508 --- busybox.old/archival/libipkg/conffile_list.c        1970-01-01 01:00:00.000000000 +0100
509 +++ busybox.dev/archival/libipkg/conffile_list.c        2007-01-22 13:41:03.000000000 +0100
510 @@ -0,0 +1,47 @@
511 +/* conffile_list.c - the itsy package management system
512 +
513 +   Carl D. Worth
514 +
515 +   Copyright (C) 2001 University of Southern California
516 +
517 +   This program is free software; you can redistribute it and/or
518 +   modify it under the terms of the GNU General Public License as
519 +   published by the Free Software Foundation; either version 2, or (at
520 +   your option) any later version.
521 +
522 +   This program is distributed in the hope that it will be useful, but
523 +   WITHOUT ANY WARRANTY; without even the implied warranty of
524 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
525 +   General Public License for more details.
526 +*/
527 +
528 +#include "ipkg.h"
529 +
530 +#include "conffile_list.h"
531 +
532 +int conffile_list_init(conffile_list_t *list)
533 +{
534 +    return nv_pair_list_init(list);
535 +}
536 +
537 +void conffile_list_deinit(conffile_list_t *list)
538 +{
539 +    nv_pair_list_deinit(list);
540 +}
541 +
542 +conffile_t *conffile_list_append(conffile_list_t *list, const char *file_name,
543 +                        const char *md5sum)
544 +{
545 +    return nv_pair_list_append(list, file_name, md5sum);
546 +}
547 +
548 +int conffile_list_push(conffile_list_t *list, conffile_t *data)
549 +{
550 +    return nv_pair_list_push(list, data);
551 +}
552 +
553 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list)
554 +{
555 +    return nv_pair_list_pop(list);
556 +}
557 +
558 diff -urN busybox.old/archival/libipkg/conffile_list.h busybox.dev/archival/libipkg/conffile_list.h
559 --- busybox.old/archival/libipkg/conffile_list.h        1970-01-01 01:00:00.000000000 +0100
560 +++ busybox.dev/archival/libipkg/conffile_list.h        2007-01-22 13:41:03.000000000 +0100
561 @@ -0,0 +1,36 @@
562 +/* conffile_list.h - the itsy package management system
563 +
564 +   Carl D. Worth
565 +
566 +   Copyright (C) 2001 University of Southern California
567 +
568 +   This program is free software; you can redistribute it and/or
569 +   modify it under the terms of the GNU General Public License as
570 +   published by the Free Software Foundation; either version 2, or (at
571 +   your option) any later version.
572 +
573 +   This program is distributed in the hope that it will be useful, but
574 +   WITHOUT ANY WARRANTY; without even the implied warranty of
575 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
576 +   General Public License for more details.
577 +*/
578 +
579 +#ifndef CONFFILE_LIST_H
580 +#define CONFFILE_LIST_H
581 +
582 +#include "conffile.h"
583 +#include "nv_pair_list.h"
584 +
585 +typedef struct nv_pair_list_elt conffile_list_elt_t;
586 +typedef struct nv_pair_list conffile_list_t;
587 +
588 +int conffile_list_init(conffile_list_t *list);
589 +void conffile_list_deinit(conffile_list_t *list);
590 +
591 +conffile_t *conffile_list_append(conffile_list_t *list, const char *name,
592 +                              const char *root_dir);
593 +int conffile_list_push(conffile_list_t *list, conffile_t *data);
594 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list);
595 +
596 +#endif
597 +
598 diff -urN busybox.old/archival/libipkg/file_util.c busybox.dev/archival/libipkg/file_util.c
599 --- busybox.old/archival/libipkg/file_util.c    1970-01-01 01:00:00.000000000 +0100
600 +++ busybox.dev/archival/libipkg/file_util.c    2007-01-22 14:00:52.000000000 +0100
601 @@ -0,0 +1,132 @@
602 +/* file_util.c - convenience routines for common stat operations
603 +
604 +   Carl D. Worth
605 +
606 +   Copyright (C) 2001 University of Southern California
607 +
608 +   This program is free software; you can redistribute it and/or
609 +   modify it under the terms of the GNU General Public License as
610 +   published by the Free Software Foundation; either version 2, or (at
611 +   your option) any later version.
612 +
613 +   This program is distributed in the hope that it will be useful, but
614 +   WITHOUT ANY WARRANTY; without even the implied warranty of
615 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
616 +   General Public License for more details.
617 +*/
618 +
619 +#include "ipkg.h"
620 +#include <sys/types.h>
621 +#include <sys/stat.h>
622 +
623 +#include "sprintf_alloc.h"
624 +#include "file_util.h"
625 +#include "libbb.h"
626 +#undef strlen
627 +
628 +int file_exists(const char *file_name)
629 +{
630 +    int err;
631 +    struct stat stat_buf;
632 +
633 +    err = stat(file_name, &stat_buf);
634 +    if (err == 0) {
635 +       return 1;
636 +    } else {
637 +       return 0;
638 +    }
639 +}
640 +
641 +int file_is_dir(const char *file_name)
642 +{
643 +    int err;
644 +    struct stat stat_buf;
645 +
646 +    err = stat(file_name, &stat_buf);
647 +    if (err) {
648 +       return 0;
649 +    }
650 +
651 +    return S_ISDIR(stat_buf.st_mode);
652 +}
653 +
654 +/* read a single line from a file, stopping at a newline or EOF.
655 +   If a newline is read, it will appear in the resulting string.
656 +   Return value is a malloc'ed char * which should be freed at
657 +   some point by the caller.
658 +
659 +   Return value is NULL if the file is at EOF when called.
660 +*/
661 +#define FILE_READ_LINE_BUF_SIZE 1024
662 +char *file_read_line_alloc(FILE *file)
663 +{
664 +    char buf[FILE_READ_LINE_BUF_SIZE];
665 +    int buf_len;
666 +    char *line = NULL;
667 +    int line_size = 0;
668 +
669 +    memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
670 +    while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
671 +       buf_len = strlen(buf);
672 +       if (line) {
673 +           line_size += buf_len;
674 +           line = realloc(line, line_size);
675 +           if (line == NULL) {
676 +               fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
677 +               break;
678 +           }
679 +           strcat(line, buf);
680 +       } else {
681 +           line_size = buf_len + 1;
682 +           line = strdup(buf);
683 +       }
684 +       if (buf[buf_len - 1] == '\n') {
685 +           break;
686 +       }
687 +    }
688 +
689 +    return line;
690 +}
691 +
692 +int file_move(const char *src, const char *dest)
693 +{
694 +    int err;
695 +
696 +    err = rename(src, dest);
697 +
698 +    if (err && errno == EXDEV) {
699 +       err = file_copy(src, dest);
700 +       unlink(src);
701 +    } else if (err) {
702 +       fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
703 +               __FUNCTION__, src, dest, strerror(errno));
704 +    }
705 +
706 +    return err;
707 +}
708 +
709 +/* I put these here to keep libbb dependencies from creeping all over
710 +   the ipkg code */
711 +int file_copy(const char *src, const char *dest)
712 +{
713 +    int err;
714 +
715 +    err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
716 +    if (err) {
717 +       fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
718 +               __FUNCTION__, src, dest);
719 +    }
720 +
721 +    return err;
722 +}
723 +
724 +int file_mkdir_hier(const char *path, long mode)
725 +{
726 +    return bb_make_directory((char *)path, mode, FILEUTILS_RECUR);
727 +}
728 +
729 +char *file_md5sum_alloc(const char *file_name)
730 +{
731 +       return hash_file(file_name, HASH_MD5);
732 +}
733 +
734 diff -urN busybox.old/archival/libipkg/file_util.h busybox.dev/archival/libipkg/file_util.h
735 --- busybox.old/archival/libipkg/file_util.h    1970-01-01 01:00:00.000000000 +0100
736 +++ busybox.dev/archival/libipkg/file_util.h    2007-01-22 13:41:03.000000000 +0100
737 @@ -0,0 +1,29 @@
738 +/* file_util.h - convenience routines for common file operations
739 +
740 +   Carl D. Worth
741 +
742 +   Copyright (C) 2001 University of Southern California
743 +
744 +   This program is free software; you can redistribute it and/or
745 +   modify it under the terms of the GNU General Public License as
746 +   published by the Free Software Foundation; either version 2, or (at
747 +   your option) any later version.
748 +
749 +   This program is distributed in the hope that it will be useful, but
750 +   WITHOUT ANY WARRANTY; without even the implied warranty of
751 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
752 +   General Public License for more details.
753 +*/
754 +
755 +#ifndef FILE_UTIL_H
756 +#define FILE_UTIL_H
757 +
758 +int file_exists(const char *file_name);
759 +int file_is_dir(const char *file_name);
760 +char *file_read_line_alloc(FILE *file);
761 +int file_move(const char *src, const char *dest);
762 +int file_copy(const char *src, const char *dest);
763 +int file_mkdir_hier(const char *path, long mode);
764 +char *file_md5sum_alloc(const char *file_name);
765 +
766 +#endif
767 diff -urN busybox.old/archival/libipkg/hash_table.c busybox.dev/archival/libipkg/hash_table.c
768 --- busybox.old/archival/libipkg/hash_table.c   1970-01-01 01:00:00.000000000 +0100
769 +++ busybox.dev/archival/libipkg/hash_table.c   2007-01-22 13:41:03.000000000 +0100
770 @@ -0,0 +1,155 @@
771 +/* hash.c - hash tables for ipkg
772 +
773 +   Steven M. Ayer, Jamey Hicks
774 +   
775 +   Copyright (C) 2002 Compaq Computer Corporation
776 +
777 +   This program is free software; you can redistribute it and/or
778 +   modify it under the terms of the GNU General Public License as
779 +   published by the Free Software Foundation; either version 2, or (at
780 +   your option) any later version.
781 +
782 +   This program is distributed in the hope that it will be useful, but
783 +   WITHOUT ANY WARRANTY; without even the implied warranty of
784 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
785 +   General Public License for more details.
786 +*/
787 +
788 +#include <errno.h>
789 +#include <stdio.h>
790 +#include <stdlib.h>
791 +#include <string.h>
792 +#include "hash_table.h"
793 +#include "ipkg_message.h"
794 +
795 +
796 +static int hash_index(hash_table_t *hash, const char *pkg_name);
797 +static int rotating(const char *key, int len, int prime);
798 +
799 +static int hash_index(hash_table_t *hash, const char *pkg_name)
800 +{
801 +    return rotating(pkg_name, strlen(pkg_name), hash->n_entries);
802 +}
803 +  
804 +static int rotating(const char *key, int len, int prime)
805 +{
806 +    unsigned int hash, i;
807 +    for (hash=len, i=0; i<len; ++i)
808 +       hash = (hash<<4)^(hash>>28)^key[i];
809 +    return (hash % prime);
810 +}
811 +
812 +
813 +/*
814 + * this is an open table keyed by strings
815 + */
816 +int hash_table_init(const char *name, hash_table_t *hash, int len)
817 +{
818 +    static int primes_table[] = {
819 +       379, 761, 983, 1423, 2711, 3361, 3931, 4679, 5519, 6701, 9587,
820 +       19471, 23143, 33961, 46499, 49727, 99529, 0
821 +    };
822 +    int *picker;
823 +
824 +    if (hash->entries != NULL) {
825 +       /* we have been here already */
826 +       return 0;
827 +    }
828 +
829 +    hash->name = name;
830 +    hash->entries = NULL;
831 +    hash->n_entries = 0;
832 +    hash->hash_entry_key = NULL;
833 +
834 +    picker = primes_table;
835 +    while(*picker && (*picker++ < len));
836 +    if(!*picker)
837 +       fprintf(stderr, "%s: primes table might not be big enough (! << %d)\n", __FUNCTION__, len);
838 +    --picker;
839 +
840 +    hash->n_entries = *picker;
841 +    hash->entries = (hash_entry_t *)calloc(hash->n_entries, sizeof(hash_entry_t));
842 +    if (hash->entries == NULL) {
843 +       fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__);
844 +       return ENOMEM;
845 +    }
846 +    return 0;
847 +}
848 +
849 +void hash_table_deinit(hash_table_t *hash)
850 +{
851 +    free(hash->entries);
852 +    hash->entries = NULL;
853 +    hash->n_entries = 0;
854 +}
855 +
856 +void *hash_table_get(hash_table_t *hash, const char *key)
857 +{
858 +  int ndx= hash_index(hash, key);
859 +  hash_entry_t *hash_entry = hash->entries + ndx;
860 +  while (hash_entry) 
861 +  {
862 +    if (hash_entry->key) 
863 +    {
864 +      if (strcmp(key, hash_entry->key) == 0) {
865 +         // ipkg_message(NULL, IPKG_DEBUG, "Function: %s. Key found for '%s' \n", __FUNCTION__, key);
866 +        return hash_entry->data;
867 +      }
868 +    }
869 +    hash_entry = hash_entry->next;
870 +  }
871 +  return NULL;
872 +}
873 +
874 +int hash_table_insert(hash_table_t *hash, const char *key, void *value)
875 +{
876 +     int ndx= hash_index(hash, key);
877 +     hash_entry_t *hash_entry = hash->entries + ndx;
878 +     if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Inserting in hash for '%s' \n", __FUNCTION__, key);
879 +     if (hash_entry->key) {
880 +         if (strcmp(hash_entry->key, key) == 0) {
881 +              /* alread in table, update the value */
882 +               if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash for '%s' \n", __FUNCTION__, key);
883 +              hash_entry->data = value;
884 +              return 0;
885 +         } else {
886 +              /* 
887 +               * if this is a collision, we have to go to the end of the ll,
888 +               * then add a new entry
889 +               * before we can hook up the value
890 +               */
891 +               if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash by collision for '%s' \n", __FUNCTION__, key);
892 +              while (hash_entry->next)
893 +                   hash_entry = hash_entry->next;
894 +              hash_entry->next = (hash_entry_t *)malloc(sizeof(hash_entry_t));
895 +              if (!hash_entry->next) {
896 +                   return -ENOMEM;
897 +              }
898 +              hash_entry = hash_entry->next;
899 +              hash_entry->next = NULL;
900 +         }
901 +     }
902 +     hash->n_elements++;
903 +     hash_entry->key = strdup(key);
904 +     hash_entry->data = value;
905 +
906 +     return 0;
907 +}
908 +
909 +
910 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data)
911 +{ 
912 +    int i;
913 +    if (!hash || !f)
914 +       return;
915 +
916 +    for (i = 0; i < hash->n_entries; i++) {
917 +       hash_entry_t *hash_entry = (hash->entries + i);
918 +       do {
919 +           if(hash_entry->key) {
920 +               f(hash_entry->key, hash_entry->data, data);
921 +           }
922 +       } while((hash_entry = hash_entry->next));
923 +    }
924 +}
925 +
926 diff -urN busybox.old/archival/libipkg/hash_table.h busybox.dev/archival/libipkg/hash_table.h
927 --- busybox.old/archival/libipkg/hash_table.h   1970-01-01 01:00:00.000000000 +0100
928 +++ busybox.dev/archival/libipkg/hash_table.h   2007-01-22 13:41:03.000000000 +0100
929 @@ -0,0 +1,44 @@
930 +/* hash.h - hash tables for ipkg
931 +
932 +   Steven M. Ayer, Jamey Hicks
933 +   
934 +   Copyright (C) 2002 Compaq Computer Corporation
935 +
936 +   This program is free software; you can redistribute it and/or
937 +   modify it under the terms of the GNU General Public License as
938 +   published by the Free Software Foundation; either version 2, or (at
939 +   your option) any later version.
940 +
941 +   This program is distributed in the hope that it will be useful, but
942 +   WITHOUT ANY WARRANTY; without even the implied warranty of
943 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
944 +   General Public License for more details.
945 +*/
946 +
947 +#ifndef _HASH_TABLE_H_
948 +#define _HASH_TABLE_H_
949 +
950 +typedef struct hash_entry hash_entry_t;
951 +typedef struct hash_table hash_table_t;
952 +
953 +struct hash_entry {
954 +  const char * key;
955 +  void * data;
956 +  struct hash_entry * next;
957 +};
958 +
959 +struct hash_table {
960 +  const char *name; 
961 +  hash_entry_t * entries;  
962 +  int n_entries; /* number of buckets */
963 +  int n_elements;
964 +  const char * (*hash_entry_key)(void * data);
965 +};
966 +
967 +int hash_table_init(const char *name, hash_table_t *hash, int len);
968 +void hash_table_deinit(hash_table_t *hash);
969 +void *hash_table_get(hash_table_t *hash, const char *key);
970 +int hash_table_insert(hash_table_t *hash, const char *key, void *value);
971 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data);
972 +
973 +#endif /* _HASH_TABLE_H_ */
974 diff -urN busybox.old/archival/libipkg/ipkg_cmd.c busybox.dev/archival/libipkg/ipkg_cmd.c
975 --- busybox.old/archival/libipkg/ipkg_cmd.c     1970-01-01 01:00:00.000000000 +0100
976 +++ busybox.dev/archival/libipkg/ipkg_cmd.c     2007-01-22 13:47:47.000000000 +0100
977 @@ -0,0 +1,1431 @@
978 +/* ipkg_cmd.c - the itsy package management system
979 +
980 +   Carl D. Worth
981 +
982 +   Copyright (C) 2001 University of Southern California
983 +
984 +   This program is free software; you can redistribute it and/or
985 +   modify it under the terms of the GNU General Public License as
986 +   published by the Free Software Foundation; either version 2, or (at
987 +   your option) any later version.
988 +
989 +   This program is distributed in the hope that it will be useful, but
990 +   WITHOUT ANY WARRANTY; without even the implied warranty of
991 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
992 +   General Public License for more details.
993 +*/
994 +
995 +#include <string.h>
996 +
997 +#include "ipkg.h"
998 +#include <libgen.h>
999 +#include <glob.h>
1000 +#include <errno.h>
1001 +#include <stdlib.h>
1002 +#include <unistd.h>
1003 +#include <signal.h>
1004 +#include <stdio.h>
1005 +#include <dirent.h>
1006 +
1007 +#include "ipkg_conf.h"
1008 +#include "ipkg_cmd.h"
1009 +#include "ipkg_message.h"
1010 +#include "pkg.h"
1011 +#include "pkg_dest.h"
1012 +#include "pkg_parse.h"
1013 +#include "sprintf_alloc.h"
1014 +#include "pkg.h"
1015 +#include "file_util.h"
1016 +#include "str_util.h"
1017 +#include "libbb.h"
1018 +#include "unarchive.h"
1019 +
1020 +#include <fnmatch.h>
1021 +
1022 +
1023 +#include "ipkg_download.h"
1024 +#include "ipkg_install.h"
1025 +#include "ipkg_upgrade.h"
1026 +#include "ipkg_remove.h"
1027 +#include "ipkg_configure.h"
1028 +#include "ipkg_message.h"
1029 +
1030 +#ifdef IPKG_LIB
1031 +#include "libipkg.h"
1032 +static void *p_userdata = NULL;
1033 +#endif
1034 +
1035 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv);
1036 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv);
1037 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv);
1038 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv);
1039 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv);
1040 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv);
1041 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv);
1042 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv);
1043 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv);
1044 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv);
1045 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv);
1046 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv);
1047 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv);
1048 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv);
1049 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1050 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1051 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv);
1052 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv);
1053 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1054 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv);
1055 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv);
1056 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv);
1057 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv);
1058 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv);
1059 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv);
1060 +
1061 +/* XXX: CLEANUP: The usage strings should be incorporated into this
1062 +   array for easier maintenance */
1063 +static ipkg_cmd_t cmds[] = {
1064 +     {"update", 0, (ipkg_cmd_fun_t)ipkg_update_cmd}, 
1065 +     {"upgrade", 0, (ipkg_cmd_fun_t)ipkg_upgrade_cmd},
1066 +     {"list", 0, (ipkg_cmd_fun_t)ipkg_list_cmd},
1067 +     {"list_installed", 0, (ipkg_cmd_fun_t)ipkg_list_installed_cmd},
1068 +     {"info", 0, (ipkg_cmd_fun_t)ipkg_info_cmd},
1069 +     {"flag", 1, (ipkg_cmd_fun_t)ipkg_flag_cmd},
1070 +     {"status", 0, (ipkg_cmd_fun_t)ipkg_status_cmd},
1071 +     {"install_pending", 0, (ipkg_cmd_fun_t)ipkg_install_pending_cmd},
1072 +     {"install", 1, (ipkg_cmd_fun_t)ipkg_install_cmd},
1073 +     {"remove", 1, (ipkg_cmd_fun_t)ipkg_remove_cmd},
1074 +     {"purge", 1, (ipkg_cmd_fun_t)ipkg_purge_cmd},
1075 +     {"configure", 0, (ipkg_cmd_fun_t)ipkg_configure_cmd},
1076 +     {"files", 1, (ipkg_cmd_fun_t)ipkg_files_cmd},
1077 +     {"search", 1, (ipkg_cmd_fun_t)ipkg_search_cmd},
1078 +     {"download", 1, (ipkg_cmd_fun_t)ipkg_download_cmd},
1079 +     {"compare_versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1080 +     {"compare-versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1081 +     {"print-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1082 +     {"print_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1083 +     {"print-installation-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1084 +     {"print_installation_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1085 +     {"depends", 1, (ipkg_cmd_fun_t)ipkg_depends_cmd},
1086 +     {"whatdepends", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_cmd},
1087 +     {"whatdependsrec", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_recursively_cmd},
1088 +     {"whatrecommends", 1, (ipkg_cmd_fun_t)ipkg_whatrecommends_cmd},
1089 +     {"whatsuggests", 1, (ipkg_cmd_fun_t)ipkg_whatsuggests_cmd},
1090 +     {"whatprovides", 1, (ipkg_cmd_fun_t)ipkg_whatprovides_cmd},
1091 +     {"whatreplaces", 1, (ipkg_cmd_fun_t)ipkg_whatreplaces_cmd},
1092 +     {"whatconflicts", 1, (ipkg_cmd_fun_t)ipkg_whatconflicts_cmd},
1093 +};
1094 +
1095 +int ipkg_state_changed;
1096 +static void write_status_files_if_changed(ipkg_conf_t *conf)
1097 +{
1098 +     if (ipkg_state_changed && !conf->noaction) {
1099 +         ipkg_message(conf, IPKG_INFO,
1100 +                      "  writing status file\n");
1101 +         ipkg_conf_write_status_files(conf);
1102 +         pkg_write_changed_filelists(conf);
1103 +     } else { 
1104 +         ipkg_message(conf, IPKG_NOTICE, "Nothing to be done\n");
1105 +     }
1106 +}
1107 +
1108 +
1109 +static int num_cmds = sizeof(cmds) / sizeof(ipkg_cmd_t);
1110 +
1111 +ipkg_cmd_t *ipkg_cmd_find(const char *name)
1112 +{
1113 +     int i;
1114 +     ipkg_cmd_t *cmd;
1115 +
1116 +     for (i=0; i < num_cmds; i++) {
1117 +         cmd = &cmds[i];
1118 +         if (strcmp(name, cmd->name) == 0) {
1119 +              return cmd;
1120 +         }
1121 +     }
1122 +
1123 +     return NULL;
1124 +}
1125 +
1126 +#ifdef IPKG_LIB
1127 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv, void *userdata)
1128 +{
1129 +       int result;
1130 +       p_userdata = userdata;
1131 +      
1132 +
1133 +       result = (cmd->fun)(conf, argc, argv);
1134 +        if ( result == 0 ) {
1135 +           ipkg_message(conf, IPKG_NOTICE, "Done.\n");
1136 +        } else {
1137 +           ipkg_message(conf, IPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
1138 +
1139 +        }
1140 +        if ( error_list ) {
1141 +           reverse_error_list(&error_list);
1142 +
1143 +           ipkg_message(conf, IPKG_NOTICE, "Collected errors:\n");
1144 +           /* Here we print the errors collected and free the list */
1145 +           while (error_list != NULL) {
1146 +                 ipkg_message(conf, IPKG_NOTICE, "%s",error_list->errmsg);
1147 +                 error_list = error_list->next;
1148 +
1149 +           }
1150 +           free_error_list(&error_list);
1151 +
1152 +        }
1153 +   
1154 +       p_userdata = NULL;
1155 +       return result;
1156 +}
1157 +#else
1158 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv)
1159 +{
1160 +     return (cmd->fun)(conf, argc, argv);
1161 +}
1162 +#endif
1163 +
1164 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv)
1165 +{
1166 +     int err;
1167 +     int failures;
1168 +     char *lists_dir;
1169 +     pkg_src_list_elt_t *iter;
1170 +     pkg_src_t *src;
1171 +
1172
1173 +    sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
1174
1175 +    if (! file_is_dir(lists_dir)) {
1176 +         if (file_exists(lists_dir)) {
1177 +              ipkg_message(conf, IPKG_ERROR,
1178 +                           "%s: ERROR: %s exists, but is not a directory\n",
1179 +                           __FUNCTION__, lists_dir);
1180 +              free(lists_dir);
1181 +              return EINVAL;
1182 +         }
1183 +         err = file_mkdir_hier(lists_dir, 0755);
1184 +         if (err) {
1185 +              ipkg_message(conf, IPKG_ERROR,
1186 +                           "%s: ERROR: failed to make directory %s: %s\n",
1187 +                           __FUNCTION__, lists_dir, strerror(errno));
1188 +              free(lists_dir);
1189 +              return EINVAL;
1190 +         }     
1191 +     } 
1192 +
1193 +     failures = 0;
1194 +     for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
1195 +         char *url, *list_file_name;
1196 +
1197 +         src = iter->data;
1198 +
1199 +         if (src->extra_data)  /* debian style? */
1200 +             sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, 
1201 +                           src->gzip ? "Packages.gz" : "Packages");
1202 +         else
1203 +             sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
1204 +
1205 +         sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
1206 +         if (src->gzip) {
1207 +             char *tmp;
1208 +             char *tmp_file_name;
1209 +             FILE *in, *out;
1210 +
1211 +             tmp = strdup ("/tmp/ipkg.XXXXXX");
1212 +
1213 +             if (mkdtemp (tmp) == NULL) {
1214 +                 perror ("mkdtemp");
1215 +                 failures++;
1216 +                 continue;
1217 +             }
1218 +             
1219 +             sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
1220 +             err = ipkg_download(conf, url, tmp_file_name);
1221 +             if (err == 0) {
1222 +                  ipkg_message (conf, IPKG_NOTICE, "Inflating %s\n", url);
1223 +                  in = fopen (tmp_file_name, "r");
1224 +                  out = fopen (list_file_name, "w");
1225 +                  if (in && out) {
1226 +                       inflate_unzip_result res;
1227 +                       inflate_unzip (&res, 0x8000, fileno(in), fileno(out));
1228 +                  } else
1229 +                       err = 1;
1230 +                  if (in)
1231 +                       fclose (in);
1232 +                  if (out)
1233 +                       fclose (out);
1234 +                  unlink (tmp_file_name);
1235 +                  rmdir (tmp);
1236 +                  free (tmp);
1237 +             }
1238 +         } else
1239 +             err = ipkg_download(conf, url, list_file_name);
1240 +         if (err) {
1241 +              failures++;
1242 +         } else {
1243 +              ipkg_message(conf, IPKG_NOTICE,
1244 +                           "Updated list of available packages in %s\n",
1245 +                           list_file_name);
1246 +         }
1247 +         free(url);
1248 +         free(list_file_name);
1249 +     }
1250 +     free(lists_dir);
1251 +
1252 +#ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
1253 +#warning here
1254 +     /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
1255 +      * this is a hack to work around poor bookkeeping in old ipkg upgrade code 
1256 +      * -Jamey 3/1/03
1257 +      */
1258 +     {
1259 +         int i;
1260 +         int changed = 0;
1261 +         pkg_vec_t *available = pkg_vec_alloc();
1262 +         pkg_hash_fetch_available(&conf->pkg_hash, available);
1263 +         ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
1264 +         for (i = 0; i < available->len; i++) {
1265 +              pkg_t *pkg = available->pkgs[i];
1266 +              if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
1267 +                   ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
1268 +                   pkg->state_want = SW_UNKNOWN;
1269 +                   changed = 1;
1270 +              }
1271 +         }
1272 +         pkg_vec_free(available);
1273 +         if (changed) {
1274 +              write_status_files_if_changed(conf);
1275 +         }
1276 +     }
1277 +#endif
1278 +
1279 +     return failures;
1280 +}
1281 +
1282 +
1283 +/* scan the args passed and cache the local filenames of the packages */
1284 +int ipkg_multiple_files_scan(ipkg_conf_t *conf, int argc, char **argv)
1285 +{
1286 +     int i;
1287 +     int err;
1288 +    
1289 +     /* 
1290 +      * First scan through package names/urls
1291 +      * For any urls, download the packages and install in database.
1292 +      * For any files, install package info in database.
1293 +      */
1294 +     for (i = 0; i < argc; i ++) {
1295 +         char *filename = argv [i];
1296 +         //char *tmp = basename (tmp);
1297 +         //int tmplen = strlen (tmp);
1298 +
1299 +         //if (strcmp (tmp + (tmplen - strlen (IPKG_PKG_EXTENSION)), IPKG_PKG_EXTENSION) != 0)
1300 +         //     continue;
1301 +         //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
1302 +         //     continue;
1303 +       
1304 +          ipkg_message(conf, IPKG_DEBUG2, "Debug mfs: %s  \n",filename );
1305 +
1306 +         err = ipkg_prepare_url_for_install(conf, filename, &argv[i]);
1307 +         if (err)
1308 +           return err;
1309 +     }
1310 +     return 0;
1311 +}
1312 +
1313 +struct ipkg_intercept
1314 +{
1315 +    char *oldpath;
1316 +    char *statedir;
1317 +};
1318 +
1319 +typedef struct ipkg_intercept *ipkg_intercept_t;
1320 +
1321 +ipkg_intercept_t ipkg_prep_intercepts(ipkg_conf_t *conf)
1322 +{
1323 +    ipkg_intercept_t ctx;
1324 +    char *newpath;
1325 +    int gen;
1326 +
1327 +    ctx = malloc (sizeof (*ctx));
1328 +    ctx->oldpath = strdup (getenv ("PATH"));
1329 +
1330 +    sprintf_alloc (&newpath, "%s/ipkg/intercept:%s", IPKGLIBDIR, ctx->oldpath);
1331 +    setenv ("PATH", newpath, 1);
1332 +    free (newpath);
1333 +    
1334 +    gen = 0;
1335 + retry:
1336 +    sprintf_alloc (&ctx->statedir, "/tmp/ipkg-intercept-%d-%d", getpid (), gen);
1337 +    if (mkdir (ctx->statedir, 0770) < 0) {
1338 +       if (errno == EEXIST) {
1339 +           free (ctx->statedir);
1340 +           gen++;
1341 +           goto retry;
1342 +       }
1343 +       perror (ctx->statedir);
1344 +       return NULL;
1345 +    }
1346 +    setenv ("IPKG_INTERCEPT_DIR", ctx->statedir, 1);
1347 +    return ctx;
1348 +}
1349 +
1350 +int ipkg_finalize_intercepts(ipkg_intercept_t ctx)
1351 +{
1352 +    char *cmd;
1353 +    DIR *dir;
1354 +    int err = 0;
1355 +
1356 +    setenv ("PATH", ctx->oldpath, 1);
1357 +    free (ctx->oldpath);
1358 +
1359 +    dir = opendir (ctx->statedir);
1360 +    if (dir) {
1361 +       struct dirent *de;
1362 +       while (de = readdir (dir), de != NULL) {
1363 +           char *path;
1364 +           
1365 +           if (de->d_name[0] == '.')
1366 +               continue;
1367 +           
1368 +           sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
1369 +           if (access (path, X_OK) == 0) {
1370 +               if (system (path)) {
1371 +                   err = errno;
1372 +                   perror (de->d_name);
1373 +               }
1374 +           }
1375 +           free (path);
1376 +       }
1377 +    } else
1378 +       perror (ctx->statedir);
1379 +       
1380 +    sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
1381 +    system (cmd);
1382 +    free (cmd);
1383 +
1384 +    free (ctx->statedir);
1385 +    free (ctx);
1386 +
1387 +    return err;
1388 +}
1389 +
1390 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name)
1391 +{
1392 +     pkg_vec_t *all;
1393 +     int i;
1394 +     pkg_t *pkg;
1395 +     ipkg_intercept_t ic;
1396 +     int r, err = 0;
1397 +
1398 +     ipkg_message(conf, IPKG_INFO,
1399 +                 "Configuring unpacked packages\n");
1400 +     fflush( stdout );
1401 +
1402 +     all = pkg_vec_alloc();
1403 +     pkg_hash_fetch_available(&conf->pkg_hash, all);
1404 +
1405 +     ic = ipkg_prep_intercepts (conf);
1406 +    
1407 +     for(i = 0; i < all->len; i++) {
1408 +         pkg = all->pkgs[i];
1409 +
1410 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1411 +              continue;
1412 +
1413 +         if (pkg->state_status == SS_UNPACKED) {
1414 +              ipkg_message(conf, IPKG_NOTICE,
1415 +                           "Configuring %s\n", pkg->name);
1416 +              fflush( stdout );
1417 +              r = ipkg_configure(conf, pkg);
1418 +              if (r == 0) {
1419 +                   pkg->state_status = SS_INSTALLED;
1420 +                   pkg->parent->state_status = SS_INSTALLED;
1421 +                   pkg->state_flag &= ~SF_PREFER;
1422 +              } else {
1423 +                   if (!err)
1424 +                       err = r;
1425 +              }
1426 +         }
1427 +     }
1428 +
1429 +     r = ipkg_finalize_intercepts (ic);
1430 +     if (r && !err)
1431 +        err = r;
1432 +
1433 +     pkg_vec_free(all);
1434 +     return err;
1435 +}
1436 +
1437 +static void sigint_handler(int sig)
1438 +{
1439 +     signal(sig, SIG_DFL);
1440 +     ipkg_message(NULL, IPKG_NOTICE,
1441 +                 "ipkg: interrupted. writing out status database\n");
1442 +     write_status_files_if_changed(global_conf);
1443 +     exit(128 + sig);
1444 +}
1445 +
1446 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv)
1447 +{
1448 +     int i;
1449 +     char *arg;
1450 +     int err=0;
1451 +
1452 +     global_conf = conf;
1453 +     signal(SIGINT, sigint_handler);
1454 +
1455 +     /*
1456 +      * Now scan through package names and install
1457 +      */
1458 +     for (i=0; i < argc; i++) {
1459 +         arg = argv[i];
1460 +
1461 +          ipkg_message(conf, IPKG_DEBUG2, "Debug install_cmd: %s  \n",arg );
1462 +          err = ipkg_prepare_url_for_install(conf, arg, &argv[i]);
1463 +          if (err != EINVAL && err != 0)
1464 +              return err;
1465 +     }
1466 +     pkg_info_preinstall_check(conf);
1467 +
1468 +     for (i=0; i < argc; i++) {
1469 +         arg = argv[i];
1470 +         if (conf->multiple_providers)
1471 +              err = ipkg_install_multi_by_name(conf, arg);
1472 +         else{
1473 +              err = ipkg_install_by_name(conf, arg);
1474 +          }
1475 +         if (err == IPKG_PKG_HAS_NO_CANDIDATE) {
1476 +              ipkg_message(conf, IPKG_ERROR,
1477 +                           "Cannot find package %s.\n"
1478 +                           "Check the spelling or perhaps run 'ipkg update'\n",
1479 +                           arg);
1480 +         }
1481 +     }
1482 +
1483 +     /* recheck to verify that all dependences are satisfied */
1484 +     if (0) ipkg_satisfy_all_dependences(conf);
1485 +
1486 +     ipkg_configure_packages(conf, NULL);
1487 +
1488 +     write_status_files_if_changed(conf);
1489 +
1490 +     return err;
1491 +}
1492 +
1493 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv)
1494 +{
1495 +     int i;
1496 +     pkg_t *pkg;
1497 +     int err;
1498 +
1499 +     global_conf = conf;
1500 +     signal(SIGINT, sigint_handler);
1501 +
1502 +     if (argc) {
1503 +         for (i=0; i < argc; i++) {
1504 +              char *arg = argv[i];
1505 +
1506 +               err = ipkg_prepare_url_for_install(conf, arg, &arg);
1507 +               if (err != EINVAL && err != 0)
1508 +                   return err;
1509 +         }
1510 +         pkg_info_preinstall_check(conf);
1511 +
1512 +         for (i=0; i < argc; i++) {
1513 +              char *arg = argv[i];
1514 +              if (conf->restrict_to_default_dest) {
1515 +                   pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1516 +                                                               argv[i],
1517 +                                                               conf->default_dest);
1518 +                   if (pkg == NULL) {
1519 +                        ipkg_message(conf, IPKG_NOTICE,
1520 +                                     "Package %s not installed in %s\n",
1521 +                                     argv[i], conf->default_dest->name);
1522 +                        continue;
1523 +                   }
1524 +              } else {
1525 +                   pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1526 +                                                          argv[i]);
1527 +              }
1528 +              if (pkg)
1529 +                   ipkg_upgrade_pkg(conf, pkg);
1530 +              else {
1531 +                   ipkg_install_by_name(conf, arg);
1532 +               }
1533 +         }
1534 +     } else {
1535 +         pkg_vec_t *installed = pkg_vec_alloc();
1536 +
1537 +         pkg_info_preinstall_check(conf);
1538 +
1539 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1540 +         for (i = 0; i < installed->len; i++) {
1541 +              pkg = installed->pkgs[i];
1542 +              ipkg_upgrade_pkg(conf, pkg);
1543 +         }
1544 +         pkg_vec_free(installed);
1545 +     }
1546 +
1547 +     /* recheck to verify that all dependences are satisfied */
1548 +     if (0) ipkg_satisfy_all_dependences(conf);
1549 +
1550 +     ipkg_configure_packages(conf, NULL);
1551 +
1552 +     write_status_files_if_changed(conf);
1553 +
1554 +     return 0;
1555 +}
1556 +
1557 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv)
1558 +{
1559 +     int i, err;
1560 +     char *arg;
1561 +     pkg_t *pkg;
1562 +
1563 +     pkg_info_preinstall_check(conf);
1564 +     for (i = 0; i < argc; i++) {
1565 +         arg = argv[i];
1566 +
1567 +         pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
1568 +         if (pkg == NULL) {
1569 +              ipkg_message(conf, IPKG_ERROR,
1570 +                           "Cannot find package %s.\n"
1571 +                           "Check the spelling or perhaps run 'ipkg update'\n",
1572 +                           arg);
1573 +              continue;
1574 +         }
1575 +
1576 +         err = ipkg_download_pkg(conf, pkg, ".");
1577 +
1578 +         if (err) {
1579 +              ipkg_message(conf, IPKG_ERROR,
1580 +                           "Failed to download %s\n", pkg->name);
1581 +         } else {
1582 +              ipkg_message(conf, IPKG_NOTICE,
1583 +                           "Downloaded %s as %s\n",
1584 +                           pkg->name, pkg->local_filename);
1585 +         }
1586 +     }
1587 +
1588 +     return 0;
1589 +}
1590 +
1591 +
1592 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv)
1593 +{
1594 +     int i ;
1595 +     pkg_vec_t *available;
1596 +     pkg_t *pkg;
1597 +     char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1598 +     char *newline;
1599 +     char *pkg_name = NULL;
1600 +     char *version_str;
1601 +
1602 +     if (argc > 0) {
1603 +         pkg_name = argv[0];
1604 +     }
1605 +     available = pkg_vec_alloc();
1606 +     pkg_hash_fetch_available(&conf->pkg_hash, available);
1607 +     for (i=0; i < available->len; i++) {
1608 +         pkg = available->pkgs[i];
1609 +         /* if we have package name or pattern and pkg does not match, then skip it */
1610 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1611 +              continue;
1612 +         if (pkg->description) {
1613 +              strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1614 +         } else {
1615 +              desc_short[0] = '\0';
1616 +         }
1617 +         desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1618 +         newline = strchr(desc_short, '\n');
1619 +         if (newline) {
1620 +              *newline = '\0';
1621 +         }
1622 +#ifndef IPKG_LIB
1623 +         printf("%s - %s\n", pkg->name, desc_short);
1624 +#else
1625 +         if (ipkg_cb_list) {
1626 +               version_str = pkg_version_str_alloc(pkg);
1627 +               ipkg_cb_list(pkg->name,desc_short,
1628 +                                            version_str,
1629 +                                        pkg->state_status,
1630 +                                        p_userdata);
1631 +               free(version_str);
1632 +         }
1633 +#endif
1634 +     }
1635 +     pkg_vec_free(available);
1636 +
1637 +     return 0;
1638 +}
1639 +
1640 +
1641 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv)
1642 +{
1643 +     int i ;
1644 +     pkg_vec_t *available;
1645 +     pkg_t *pkg;
1646 +     char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1647 +     char *newline;
1648 +     char *pkg_name = NULL;
1649 +     char *version_str;
1650 +
1651 +     if (argc > 0) {
1652 +         pkg_name = argv[0];
1653 +     }
1654 +     available = pkg_vec_alloc();
1655 +     pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1656 +     for (i=0; i < available->len; i++) {
1657 +         pkg = available->pkgs[i];
1658 +         /* if we have package name or pattern and pkg does not match, then skip it */
1659 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1660 +              continue;
1661 +         if (pkg->description) {
1662 +              strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1663 +         } else {
1664 +              desc_short[0] = '\0';
1665 +         }
1666 +         desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1667 +         newline = strchr(desc_short, '\n');
1668 +         if (newline) {
1669 +              *newline = '\0';
1670 +         }
1671 +#ifndef IPKG_LIB
1672 +         printf("%s - %s\n", pkg->name, desc_short);
1673 +#else
1674 +         if (ipkg_cb_list) {
1675 +               version_str = pkg_version_str_alloc(pkg);
1676 +               ipkg_cb_list(pkg->name,desc_short,
1677 +                                            version_str,
1678 +                                        pkg->state_status,
1679 +                                        p_userdata);
1680 +               free(version_str);
1681 +         }
1682 +#endif
1683 +     }
1684 +
1685 +     return 0;
1686 +}
1687 +
1688 +static int ipkg_info_status_cmd(ipkg_conf_t *conf, int argc, char **argv, int installed_only)
1689 +{
1690 +     int i;
1691 +     pkg_vec_t *available;
1692 +     pkg_t *pkg;
1693 +     char *pkg_name = NULL;
1694 +     char **pkg_fields = NULL;
1695 +     int n_fields = 0;
1696 +     char *buff ; // = (char *)malloc(1);
1697 +
1698 +     if (argc > 0) {
1699 +         pkg_name = argv[0];
1700 +     }
1701 +     if (argc > 1) {
1702 +         pkg_fields = &argv[1];
1703 +         n_fields = argc - 1;
1704 +     }
1705 +
1706 +     available = pkg_vec_alloc();
1707 +     if (installed_only)
1708 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1709 +     else
1710 +         pkg_hash_fetch_available(&conf->pkg_hash, available);
1711 +     for (i=0; i < available->len; i++) {
1712 +         pkg = available->pkgs[i];
1713 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1714 +              continue;
1715 +         }
1716 +#ifndef IPKG_LIB
1717 +         if (n_fields) {
1718 +              for (j = 0; j < n_fields; j++)
1719 +                   pkg_print_field(pkg, stdout, pkg_fields[j]);
1720 +         } else {
1721 +              pkg_print_info(pkg, stdout);
1722 +         }
1723 +#else
1724 +
1725 +         buff = pkg_formatted_info(pkg);
1726 +          if ( buff ) {
1727 +              if (ipkg_cb_status) ipkg_cb_status(pkg->name,
1728 +                                                 pkg->state_status,
1729 +                                                 buff,
1730 +                                                 p_userdata);
1731 +/* 
1732 +   We should not forget that actually the pointer is allocated. 
1733 +   We need to free it :)  ( Thanks florian for seeing the error )
1734 +*/
1735 +               free(buff);
1736 +          }
1737 +#endif
1738 +         if (conf->verbosity > 1) {
1739 +              conffile_list_elt_t *iter;
1740 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
1741 +                   conffile_t *cf = iter->data;
1742 +                   int modified = conffile_has_been_modified(conf, cf);
1743 +                   ipkg_message(conf, IPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
1744 +                                cf->name, cf->value, modified);
1745 +              }
1746 +         }
1747 +     }
1748 +#ifndef IPKG_LIB
1749 +     if (buff)
1750 +         free(buff);
1751 +#endif
1752 +     pkg_vec_free(available);
1753 +
1754 +     return 0;
1755 +}
1756 +
1757 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv)
1758 +{
1759 +     return ipkg_info_status_cmd(conf, argc, argv, 0);
1760 +}
1761 +
1762 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv)
1763 +{
1764 +     return ipkg_info_status_cmd(conf, argc, argv, 1);
1765 +}
1766 +
1767 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv)
1768 +{
1769 +     
1770 +     int err;
1771 +     if (argc > 0) {
1772 +         char *pkg_name = NULL;
1773 +
1774 +         pkg_name = argv[0];
1775 +
1776 +         err = ipkg_configure_packages (conf, pkg_name);
1777 +     
1778 +     } else {
1779 +         err = ipkg_configure_packages (conf, NULL);
1780 +     }
1781 +
1782 +     write_status_files_if_changed(conf);
1783 +
1784 +     return err;
1785 +}
1786 +
1787 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv)
1788 +{
1789 +     int i, err;
1790 +     char *globpattern;
1791 +     glob_t globbuf;
1792 +    
1793 +     sprintf_alloc(&globpattern, "%s/*" IPKG_PKG_EXTENSION, conf->pending_dir);
1794 +     err = glob(globpattern, 0, NULL, &globbuf);
1795 +     free(globpattern);
1796 +     if (err) {
1797 +         return 0;
1798 +     }
1799 +
1800 +     ipkg_message(conf, IPKG_NOTICE,
1801 +                 "The following packages in %s will now be installed.\n",
1802 +                 conf->pending_dir);
1803 +     for (i = 0; i < globbuf.gl_pathc; i++) {
1804 +         ipkg_message(conf, IPKG_NOTICE,
1805 +                      "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
1806 +     }
1807 +     ipkg_message(conf, IPKG_NOTICE, "\n");
1808 +     for (i = 0; i < globbuf.gl_pathc; i++) {
1809 +         err = ipkg_install_from_file(conf, globbuf.gl_pathv[i]);
1810 +         if (err == 0) {
1811 +              err = unlink(globbuf.gl_pathv[i]);
1812 +              if (err) {
1813 +                   ipkg_message(conf, IPKG_ERROR,
1814 +                                "%s: ERROR: failed to unlink %s: %s\n",
1815 +                                __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
1816 +                   return err;
1817 +              }
1818 +         }
1819 +     }
1820 +     globfree(&globbuf);
1821 +
1822 +     return err;
1823 +}
1824 +
1825 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv)
1826 +{
1827 +     int i,a,done;
1828 +     pkg_t *pkg;
1829 +     pkg_t *pkg_to_remove;
1830 +     pkg_vec_t *available;
1831 +     char *pkg_name = NULL;
1832 +     global_conf = conf;
1833 +     signal(SIGINT, sigint_handler);
1834 +
1835 +// ENH: Add the "no pkg removed" just in case.
1836 +
1837 +    done = 0;
1838 +
1839 +     available = pkg_vec_alloc();
1840 +     pkg_info_preinstall_check(conf);
1841 +     if ( argc > 0 ) {
1842 +        pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1843 +        for (i=0; i < argc; i++) {
1844 +           pkg_name = malloc(strlen(argv[i])+2);
1845 +           strcpy(pkg_name,argv[i]);
1846 +           for (a=0; a < available->len; a++) {
1847 +               pkg = available->pkgs[a];
1848 +              if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1849 +                  continue;
1850 +               }
1851 +               if (conf->restrict_to_default_dest) {
1852 +                   pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1853 +                                                               pkg->name,
1854 +                                                               conf->default_dest);
1855 +               } else {
1856 +                   pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
1857 +               }
1858 +        
1859 +               if (pkg == NULL) {
1860 +                   ipkg_message(conf, IPKG_ERROR, "Package %s is not installed.\n", pkg->name);
1861 +                   continue;
1862 +               }
1863 +               if (pkg->state_status == SS_NOT_INSTALLED) {    // Added the control, so every already removed package could be skipped
1864 +                   ipkg_message(conf, IPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
1865 +                    continue;
1866 +               }
1867 +               ipkg_remove_pkg(conf, pkg_to_remove,0);
1868 +               done = 1;
1869 +           }
1870 +           free (pkg_name);
1871 +        }
1872 +        pkg_vec_free(available);
1873 +     } else {
1874 +         pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1875 +         int flagged_pkg_count = 0;
1876 +         int removed;
1877 +
1878 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
1879 +
1880 +         for (i = 0; i < installed_pkgs->len; i++) {
1881 +              pkg = installed_pkgs->pkgs[i];
1882 +              if (pkg->state_flag & SF_USER) {
1883 +                   flagged_pkg_count++;
1884 +              } else {
1885 +                   if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
1886 +                        ipkg_message(conf, IPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
1887 +              }
1888 +         }
1889 +         if (!flagged_pkg_count) {
1890 +              ipkg_message(conf, IPKG_NOTICE, "No packages flagged as installed by user, \n"
1891 +                           "so refusing to uninstall unflagged non-leaf packages\n");
1892 +              return 0;
1893 +         }
1894 +
1895 +         /* find packages not flagged SF_USER (i.e., installed to
1896 +          * satisfy a dependence) and not having any dependents, and
1897 +          * remove them */
1898 +         do {
1899 +              removed = 0;
1900 +              for (i = 0; i < installed_pkgs->len; i++) {
1901 +                   pkg = installed_pkgs->pkgs[i];
1902 +                   if (!(pkg->state_flag & SF_USER)
1903 +                       && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
1904 +                        removed++;
1905 +                        ipkg_message(conf, IPKG_NOTICE, "Removing non-user leaf package %s\n");
1906 +                        ipkg_remove_pkg(conf, pkg,0);
1907 +                         done = 1;
1908 +                   }
1909 +              }
1910 +         } while (removed);
1911 +         pkg_vec_free(installed_pkgs);
1912 +     }
1913 +
1914 +     if ( done == 0 ) 
1915 +        ipkg_message(conf, IPKG_NOTICE, "No packages removed.\n");
1916 +
1917 +     write_status_files_if_changed(conf);
1918 +     return 0;
1919 +}
1920 +
1921 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv)
1922 +{
1923 +     int i;
1924 +     pkg_t *pkg;
1925 +
1926 +     global_conf = conf;
1927 +     signal(SIGINT, sigint_handler);
1928 +
1929 +     pkg_info_preinstall_check(conf);
1930 +
1931 +     for (i=0; i < argc; i++) {
1932 +         if (conf->restrict_to_default_dest) {
1933 +              pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1934 +                                                          argv[i],
1935 +                                                          conf->default_dest);
1936 +         } else {
1937 +              pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1938 +         }
1939 +
1940 +         if (pkg == NULL) {
1941 +              ipkg_message(conf, IPKG_ERROR,
1942 +                           "Package %s is not installed.\n", argv[i]);
1943 +              continue;
1944 +         }
1945 +         ipkg_purge_pkg(conf, pkg);
1946 +     }
1947 +
1948 +     write_status_files_if_changed(conf);
1949 +     return 0;
1950 +}
1951 +
1952 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv)
1953 +{
1954 +     int i;
1955 +     pkg_t *pkg;
1956 +     char *flags = argv[0];
1957 +    
1958 +     global_conf = conf;
1959 +     signal(SIGINT, sigint_handler);
1960 +
1961 +     for (i=1; i < argc; i++) {
1962 +         if (conf->restrict_to_default_dest) {
1963 +              pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1964 +                                                          argv[i],
1965 +                                                          conf->default_dest);
1966 +         } else {
1967 +              pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1968 +         }
1969 +
1970 +         if (pkg == NULL) {
1971 +              ipkg_message(conf, IPKG_ERROR,
1972 +                           "Package %s is not installed.\n", argv[i]);
1973 +              continue;
1974 +         }
1975 +          if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1976 +              ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1977 +             pkg->state_flag = pkg_state_flag_from_str(flags);
1978 +          }
1979 +/* pb_ asked this feature 03292004 */
1980 +/* Actually I will use only this two, but this is an open for various status */
1981 +          if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1982 +             pkg->state_status = pkg_state_status_from_str(flags);
1983 +          }
1984 +         ipkg_state_changed++;
1985 +         ipkg_message(conf, IPKG_NOTICE,
1986 +                      "Setting flags for package %s to %s\n",
1987 +                      pkg->name, flags);
1988 +     }
1989 +
1990 +     write_status_files_if_changed(conf);
1991 +     return 0;
1992 +}
1993 +
1994 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv)
1995 +{
1996 +     pkg_t *pkg;
1997 +     str_list_t *installed_files;
1998 +     str_list_elt_t *iter;
1999 +     char *pkg_version;
2000 +     size_t buff_len = 8192;
2001 +     size_t used_len;
2002 +     char *buff ;
2003 +
2004 +     buff = (char *)malloc(buff_len);
2005 +     if ( buff == NULL ) {
2006 +        fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
2007 +        return ENOMEM;
2008 +     }
2009
2010 +     if (argc < 1) {
2011 +         return EINVAL;
2012 +     }
2013 +
2014 +     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
2015 +                                           argv[0]);
2016 +     if (pkg == NULL) {
2017 +         ipkg_message(conf, IPKG_ERROR,
2018 +                      "Package %s not installed.\n", argv[0]);
2019 +         return 0;
2020 +     }
2021 +
2022 +     installed_files = pkg_get_installed_files(pkg);
2023 +     pkg_version = pkg_version_str_alloc(pkg);
2024 +
2025 +#ifndef IPKG_LIB
2026 +     printf("Package %s (%s) is installed on %s and has the following files:\n",
2027 +           pkg->name, pkg_version, pkg->dest->name);
2028 +     for (iter = installed_files->head; iter; iter = iter->next) {
2029 +         puts(iter->data);
2030 +     }
2031 +#else
2032 +     if (buff) {
2033 +     try_again:
2034 +         used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
2035 +                             pkg->name, pkg_version, pkg->dest->name) + 1;
2036 +         if (used_len > buff_len) {
2037 +              buff_len *= 2;
2038 +              buff = realloc (buff, buff_len);
2039 +              goto try_again;
2040 +         }
2041 +         for (iter = installed_files->head; iter; iter = iter->next) {
2042 +              used_len += strlen (iter->data) + 1;
2043 +              while (buff_len <= used_len) {
2044 +                   buff_len *= 2;
2045 +                   buff = realloc (buff, buff_len);
2046 +              }
2047 +              strncat(buff, iter->data, buff_len);
2048 +              strncat(buff, "\n", buff_len);
2049 +         } 
2050 +         if (ipkg_cb_list) ipkg_cb_list(pkg->name,
2051 +                                        buff,
2052 +                                        pkg_version_str_alloc(pkg),
2053 +                                        pkg->state_status,
2054 +                                        p_userdata);
2055 +         free(buff);
2056 +     }
2057 +#endif
2058 +
2059 +     free(pkg_version);
2060 +     pkg_free_installed_files(pkg);
2061 +
2062 +     return 0;
2063 +}
2064 +
2065 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2066 +{
2067 +
2068 +     if (argc > 0) {
2069 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2070 +         const char *rel_str = "depends on";
2071 +         int i;
2072 +     
2073 +         pkg_info_preinstall_check(conf);
2074 +
2075 +         if (conf->query_all)
2076 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2077 +         else
2078 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2079 +         for (i = 0; i < argc; i++) {
2080 +              const char *target = argv[i];
2081 +              int j;
2082 +
2083 +              ipkg_message(conf, IPKG_ERROR, "target=%s\n", target);
2084 +
2085 +              for (j = 0; j < available_pkgs->len; j++) {
2086 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2087 +                   if (fnmatch(target, pkg->name, 0) == 0) {
2088 +                        int k;
2089 +                        int count = pkg->depends_count + pkg->pre_depends_count;
2090 +                        ipkg_message(conf, IPKG_ERROR, "What %s (arch=%s) %s\n",
2091 +                                     target, pkg->architecture, rel_str);
2092 +                        for (k = 0; k < count; k++) {
2093 +                             compound_depend_t *cdepend = &pkg->depends[k];
2094 +                             int l;
2095 +                             for (l = 0; l < cdepend->possibility_count; l++) {
2096 +                                  depend_t *possibility = cdepend->possibilities[l];
2097 +                                  ipkg_message(conf, IPKG_ERROR, "    %s", possibility->pkg->name);
2098 +                                  if (conf->verbosity > 0) {
2099 +                                       // char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 
2100 +                                       ipkg_message(conf, IPKG_NOTICE, " %s", possibility->version);
2101 +                                       if (possibility->version) {
2102 +                                            char *typestr = NULL;
2103 +                                            switch (possibility->constraint) {
2104 +                                            case NONE: typestr = "none"; break;
2105 +                                            case EARLIER: typestr = "<"; break;
2106 +                                            case EARLIER_EQUAL: typestr = "<="; break;
2107 +                                            case EQUAL: typestr = "="; break;
2108 +                                            case LATER_EQUAL: typestr = ">="; break;
2109 +                                            case LATER: typestr = ">"; break;
2110 +                                            }
2111 +                                            ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2112 +                                       }
2113 +                                       // free(ver);
2114 +                                  }
2115 +                                  ipkg_message(conf, IPKG_ERROR, "\n");
2116 +                             }
2117 +                        }
2118 +                   }
2119 +              }
2120 +         }
2121 +         pkg_vec_free(available_pkgs);
2122 +     }
2123 +     return 0;
2124 +}
2125 +
2126 +enum what_field_type {
2127 +  WHATDEPENDS,
2128 +  WHATCONFLICTS,
2129 +  WHATPROVIDES,
2130 +  WHATREPLACES,
2131 +  WHATRECOMMENDS,
2132 +  WHATSUGGESTS
2133 +};
2134 +
2135 +static int ipkg_what_depends_conflicts_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
2136 +{
2137 +
2138 +     if (argc > 0) {
2139 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2140 +         const char *rel_str = NULL;
2141 +         int i;
2142 +         int changed;
2143 +
2144 +         switch (what_field_type) {
2145 +         case WHATDEPENDS: rel_str = "depends on"; break;
2146 +         case WHATCONFLICTS: rel_str = "conflicts with"; break;
2147 +         case WHATSUGGESTS: rel_str = "suggests"; break;
2148 +         case WHATRECOMMENDS: rel_str = "recommends"; break;
2149 +         case WHATPROVIDES: rel_str = "provides"; break;
2150 +         case WHATREPLACES: rel_str = "replaces"; break;
2151 +         }
2152 +     
2153 +         if (conf->query_all)
2154 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2155 +         else
2156 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2157 +
2158 +         /* mark the root set */
2159 +         pkg_vec_clear_marks(available_pkgs);
2160 +         ipkg_message(conf, IPKG_NOTICE, "Root set:\n");
2161 +         for (i = 0; i < argc; i++) {
2162 +              const char *dependee_pattern = argv[i];
2163 +              pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
2164 +         }
2165 +         for (i = 0; i < available_pkgs->len; i++) {
2166 +              pkg_t *pkg = available_pkgs->pkgs[i];
2167 +              if (pkg->state_flag & SF_MARKED) {
2168 +                   /* mark the parent (abstract) package */
2169 +                   pkg_mark_provides(pkg);
2170 +                   ipkg_message(conf, IPKG_NOTICE, "  %s\n", pkg->name);
2171 +              }
2172 +         }
2173 +
2174 +         ipkg_message(conf, IPKG_NOTICE, "What %s root set\n", rel_str);
2175 +         do {
2176 +              int j;
2177 +              changed = 0;
2178 +
2179 +              for (j = 0; j < available_pkgs->len; j++) {
2180 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2181 +                   int k;
2182 +                   int count = ((what_field_type == WHATCONFLICTS)
2183 +                                ? pkg->conflicts_count
2184 +                                : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
2185 +                   /* skip this package if it is already marked */
2186 +                   if (pkg->parent->state_flag & SF_MARKED) {
2187 +                        continue;
2188 +                   }
2189 +                   for (k = 0; k < count; k++) {
2190 +                        compound_depend_t *cdepend = 
2191 +                             (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
2192 +                        int l;
2193 +                        for (l = 0; l < cdepend->possibility_count; l++) {
2194 +                             depend_t *possibility = cdepend->possibilities[l];
2195 +                             if (possibility->pkg->state_flag & SF_MARKED) {
2196 +                                  /* mark the depending package so we won't visit it again */
2197 +                                  pkg->state_flag |= SF_MARKED;
2198 +                                  pkg_mark_provides(pkg);
2199 +                                  changed++;
2200 +
2201 +                                  ipkg_message(conf, IPKG_NOTICE, "    %s", pkg->name);
2202 +                                  if (conf->verbosity > 0) {
2203 +                                       char *ver = pkg_version_str_alloc(pkg); 
2204 +                                       ipkg_message(conf, IPKG_NOTICE, " %s", ver);
2205 +                                       ipkg_message(conf, IPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
2206 +                                       if (possibility->version) {
2207 +                                            char *typestr = NULL;
2208 +                                            switch (possibility->constraint) {
2209 +                                            case NONE: typestr = "none"; break;
2210 +                                            case EARLIER: typestr = "<"; break;
2211 +                                            case EARLIER_EQUAL: typestr = "<="; break;
2212 +                                            case EQUAL: typestr = "="; break;
2213 +                                            case LATER_EQUAL: typestr = ">="; break;
2214 +                                            case LATER: typestr = ">"; break;
2215 +                                            }
2216 +                                            ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2217 +                                       }
2218 +                                       free(ver);
2219 +                                       if (!pkg_dependence_satisfiable(conf, possibility))
2220 +                                            ipkg_message(conf, IPKG_NOTICE, " unsatisfiable");
2221 +                                  }
2222 +                                  ipkg_message(conf, IPKG_NOTICE, "\n");
2223 +                                  goto next_package;
2224 +                             }
2225 +                        }
2226 +                   }
2227 +              next_package:
2228 +                   ;
2229 +              }
2230 +         } while (changed && recursive);
2231 +         pkg_vec_free(available_pkgs);
2232 +     }
2233 +
2234 +     return 0;
2235 +}
2236 +
2237 +int pkg_mark_provides(pkg_t *pkg)
2238 +{
2239 +     int provides_count = pkg->provides_count;
2240 +     abstract_pkg_t **provides = pkg->provides;
2241 +     int i;
2242 +     pkg->parent->state_flag |= SF_MARKED;
2243 +     for (i = 0; i < provides_count; i++) {
2244 +         provides[i]->state_flag |= SF_MARKED;
2245 +     }
2246 +     return 0;
2247 +}
2248 +
2249 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv)
2250 +{
2251 +     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
2252 +}
2253 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2254 +{
2255 +     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
2256 +}
2257 +
2258 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv)
2259 +{
2260 +     return ipkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
2261 +}
2262 +
2263 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2264 +{
2265 +     return ipkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
2266 +}
2267 +
2268 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv)
2269 +{
2270 +     return ipkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
2271 +}
2272 +
2273 +static int ipkg_what_provides_replaces_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
2274 +{
2275 +
2276 +     if (argc > 0) {
2277 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2278 +         const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
2279 +         int i;
2280 +     
2281 +         pkg_info_preinstall_check(conf);
2282 +
2283 +         if (conf->query_all)
2284 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2285 +         else
2286 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2287 +         for (i = 0; i < argc; i++) {
2288 +              const char *target = argv[i];
2289 +              int j;
2290 +
2291 +              ipkg_message(conf, IPKG_ERROR, "What %s %s\n",
2292 +                           rel_str, target);
2293 +              for (j = 0; j < available_pkgs->len; j++) {
2294 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2295 +                   int k;
2296 +                   int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
2297 +                   for (k = 0; k < count; k++) {
2298 +                        abstract_pkg_t *apkg = 
2299 +                             ((what_field_type == WHATPROVIDES) 
2300 +                              ? pkg->provides[k]
2301 +                              : pkg->replaces[k]);
2302 +                        if (fnmatch(target, apkg->name, 0) == 0) {
2303 +                             ipkg_message(conf, IPKG_ERROR, "    %s", pkg->name);
2304 +                             if (strcmp(target, apkg->name) != 0)
2305 +                                  ipkg_message(conf, IPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
2306 +                             ipkg_message(conf, IPKG_ERROR, "\n");
2307 +                        }
2308 +                   }
2309 +              }
2310 +         }
2311 +         pkg_vec_free(available_pkgs);
2312 +     }
2313 +     return 0;
2314 +}
2315 +
2316 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv)
2317 +{
2318 +     return ipkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
2319 +}
2320 +
2321 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv)
2322 +{
2323 +     return ipkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
2324 +}
2325 +
2326 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv)
2327 +{
2328 +     int i;
2329 +
2330 +     pkg_vec_t *installed;
2331 +     pkg_t *pkg;
2332 +     str_list_t *installed_files;
2333 +     str_list_elt_t *iter;
2334 +     char *installed_file;
2335 +
2336 +     if (argc < 1) {
2337 +         return EINVAL;
2338 +     }
2339
2340 +     installed = pkg_vec_alloc();
2341 +     pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
2342 +
2343 +     for (i=0; i < installed->len; i++) {
2344 +         pkg = installed->pkgs[i];
2345 +
2346 +         installed_files = pkg_get_installed_files(pkg);
2347 +
2348 +         for (iter = installed_files->head; iter; iter = iter->next) {
2349 +              installed_file = iter->data;
2350 +              if (fnmatch(argv[0], installed_file, 0)==0)  {
2351 +#ifndef IPKG_LIB
2352 +                   printf("%s: %s\n", pkg->name, installed_file);
2353 +#else
2354 +                       if (ipkg_cb_list) ipkg_cb_list(pkg->name, 
2355 +                                                      installed_file, 
2356 +                                                      pkg_version_str_alloc(pkg), 
2357 +                                                      pkg->state_status, p_userdata);
2358 +#endif                    
2359 +              }                
2360 +         }
2361 +
2362 +         pkg_free_installed_files(pkg);
2363 +     }
2364 +
2365 +     /* XXX: CLEANUP: It's not obvious from the name of
2366 +       pkg_hash_fetch_all_installed that we need to call
2367 +       pkg_vec_free to avoid a memory leak. */
2368 +     pkg_vec_free(installed);
2369 +
2370 +     return 0;
2371 +}
2372 +
2373 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv)
2374 +{
2375 +     if (argc == 3) {
2376 +         /* this is a bit gross */
2377 +         struct pkg p1, p2;
2378 +         parseVersion(&p1, argv[0]); 
2379 +         parseVersion(&p2, argv[2]); 
2380 +         return pkg_version_satisfied(&p1, &p2, argv[1]);
2381 +     } else {
2382 +         ipkg_message(conf, IPKG_ERROR,
2383 +                      "ipkg compare_versions <v1> <op> <v2>\n"
2384 +                      "<op> is one of <= >= << >> =\n");
2385 +         return -1;
2386 +     }
2387 +}
2388 +
2389 +#ifndef HOST_CPU_STR
2390 +#define HOST_CPU_STR__(X) #X
2391 +#define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
2392 +#define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
2393 +#endif
2394 +
2395 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv)
2396 +{
2397 +     nv_pair_list_elt_t *l;
2398 +
2399 +     l = conf->arch_list.head;
2400 +     while (l) {
2401 +         nv_pair_t *nv = l->data;
2402 +         printf("arch %s %s\n", nv->name, nv->value);
2403 +         l = l->next;
2404 +     }
2405 +     return 0;
2406 +}
2407 +
2408 +
2409 diff -urN busybox.old/archival/libipkg/ipkg_cmd.h busybox.dev/archival/libipkg/ipkg_cmd.h
2410 --- busybox.old/archival/libipkg/ipkg_cmd.h     1970-01-01 01:00:00.000000000 +0100
2411 +++ busybox.dev/archival/libipkg/ipkg_cmd.h     2007-01-22 13:41:03.000000000 +0100
2412 @@ -0,0 +1,46 @@
2413 +/* ipkg_cmd.h - the itsy package management system
2414 +
2415 +   Carl D. Worth
2416 +
2417 +   Copyright (C) 2001 University of Southern California
2418 +
2419 +   This program is free software; you can redistribute it and/or
2420 +   modify it under the terms of the GNU General Public License as
2421 +   published by the Free Software Foundation; either version 2, or (at
2422 +   your option) any later version.
2423 +
2424 +   This program is distributed in the hope that it will be useful, but
2425 +   WITHOUT ANY WARRANTY; without even the implied warranty of
2426 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2427 +   General Public License for more details.
2428 +*/
2429 +
2430 +#ifndef IPKG_CMD_H
2431 +#define IPKG_CMD_H
2432 +
2433 +typedef int (*ipkg_cmd_fun_t)(ipkg_conf_t *conf, int argc, const char **argv);
2434 +
2435 +struct ipkg_cmd
2436 +{
2437 +    char *name;
2438 +    int requires_args;
2439 +    ipkg_cmd_fun_t fun;
2440 +};
2441 +typedef struct ipkg_cmd ipkg_cmd_t;
2442 +
2443 +ipkg_cmd_t *ipkg_cmd_find(const char *name);
2444 +#ifdef IPKG_LIB
2445 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, 
2446 +                  const char **argv, void *userdata);
2447 +#else
2448 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv);
2449 +#endif
2450 +int ipkg_multiple_files_scan (ipkg_conf_t *conf, int argc, char *argv[]);
2451 +/* install any packges with state_want == SW_INSTALL */
2452 +int ipkg_install_wanted_packages(ipkg_conf_t *conf);
2453 +/* ensure that all dependences are satisfied */
2454 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name);
2455 +
2456 +int pkg_mark_provides(pkg_t *pkg);
2457 +
2458 +#endif
2459 diff -urN busybox.old/archival/libipkg/ipkg_conf.c busybox.dev/archival/libipkg/ipkg_conf.c
2460 --- busybox.old/archival/libipkg/ipkg_conf.c    1970-01-01 01:00:00.000000000 +0100
2461 +++ busybox.dev/archival/libipkg/ipkg_conf.c    2007-01-22 13:41:06.000000000 +0100
2462 @@ -0,0 +1,711 @@
2463 +/* ipkg_conf.c - the itsy package management system
2464 +
2465 +   Carl D. Worth
2466 +
2467 +   Copyright (C) 2001 University of Southern California
2468 +
2469 +   This program is free software; you can redistribute it and/or
2470 +   modify it under the terms of the GNU General Public License as
2471 +   published by the Free Software Foundation; either version 2, or (at
2472 +   your option) any later version.
2473 +
2474 +   This program is distributed in the hope that it will be useful, but
2475 +   WITHOUT ANY WARRANTY; without even the implied warranty of
2476 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2477 +   General Public License for more details.
2478 +*/
2479 +
2480 +#include <glob.h>
2481 +
2482 +#include "ipkg.h"
2483 +#include "ipkg_conf.h"
2484 +
2485 +#include "xregex.h"
2486 +#include "sprintf_alloc.h"
2487 +#include "ipkg_conf.h"
2488 +#include "ipkg_message.h"
2489 +#include "file_util.h"
2490 +#include "str_util.h"
2491 +#include "xsystem.h"
2492 +
2493 +
2494 +ipkg_conf_t *global_conf;
2495 +
2496 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2497 +                               pkg_src_list_t *pkg_src_list,
2498 +                               nv_pair_list_t *tmp_dest_nv_pair_list,
2499 +                               char **tmp_lists_dir);
2500 +static int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options);
2501 +static int ipkg_conf_set_option(const ipkg_option_t *options,
2502 +                               const char *name, const char *value);
2503 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2504 +                                     const char *default_dest_name);
2505 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf,
2506 +                                    pkg_src_list_t *nv_pair_list);
2507 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf,
2508 +                                     nv_pair_list_t *nv_pair_list, char * lists_dir);
2509 +
2510 +int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options)
2511 +{
2512 +     ipkg_option_t tmp[] = {
2513 +         { "force_defaults", IPKG_OPT_TYPE_BOOL, &conf->force_defaults },
2514 +         { "force_depends", IPKG_OPT_TYPE_BOOL, &conf->force_depends },
2515 +         { "force_overwrite", IPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
2516 +         { "force_downgrade", IPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
2517 +         { "force_reinstall", IPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
2518 +         { "force_space", IPKG_OPT_TYPE_BOOL, &conf->force_space },
2519 +         { "ftp_proxy", IPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
2520 +         { "http_proxy", IPKG_OPT_TYPE_STRING, &conf->http_proxy },
2521 +         { "multiple_providers", IPKG_OPT_TYPE_BOOL, &conf->multiple_providers },
2522 +         { "no_proxy", IPKG_OPT_TYPE_STRING, &conf->no_proxy },
2523 +         { "test", IPKG_OPT_TYPE_INT, &conf->noaction },
2524 +         { "noaction", IPKG_OPT_TYPE_INT, &conf->noaction },
2525 +         { "nodeps", IPKG_OPT_TYPE_BOOL, &conf->nodeps },
2526 +         { "offline_root", IPKG_OPT_TYPE_STRING, &conf->offline_root },
2527 +         { "offline_root_post_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
2528 +         { "offline_root_pre_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
2529 +         { "proxy_passwd", IPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
2530 +         { "proxy_user", IPKG_OPT_TYPE_STRING, &conf->proxy_user },
2531 +         { "query-all", IPKG_OPT_TYPE_BOOL, &conf->query_all },
2532 +         { "verbose-wget", IPKG_OPT_TYPE_BOOL, &conf->verbose_wget },
2533 +         { "verbosity", IPKG_OPT_TYPE_BOOL, &conf->verbosity },
2534 +         { NULL }
2535 +     };
2536 +
2537 +     *options = (ipkg_option_t *)malloc(sizeof(tmp));
2538 +     if ( options == NULL ){
2539 +        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
2540 +        return -1;
2541 +     }
2542 +
2543 +     memcpy(*options, tmp, sizeof(tmp));
2544 +     return 0;
2545 +};
2546 +
2547 +static void ipkg_conf_override_string(char **conf_str, char *arg_str) 
2548 +{
2549 +     if (arg_str) {
2550 +         if (*conf_str) {
2551 +              free(*conf_str);
2552 +         }
2553 +         *conf_str = strdup(arg_str);
2554 +     }
2555 +}
2556 +
2557 +static void ipkg_conf_free_string(char **conf_str)
2558 +{
2559 +     if (*conf_str) {
2560 +         free(*conf_str);
2561 +         *conf_str = NULL;
2562 +     }
2563 +}
2564 +
2565 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args)
2566 +{
2567 +     int err;
2568 +     char *tmp_dir_base;
2569 +     nv_pair_list_t tmp_dest_nv_pair_list;
2570 +     char * lists_dir =NULL;
2571 +     glob_t globbuf;
2572 +     char *etc_ipkg_conf_pattern = "/etc/ipkg/*.conf";
2573 +     char *pending_dir  =NULL;
2574 +
2575 +     memset(conf, 0, sizeof(ipkg_conf_t));
2576 +
2577 +     pkg_src_list_init(&conf->pkg_src_list);
2578 +
2579 +     nv_pair_list_init(&tmp_dest_nv_pair_list);
2580 +     pkg_dest_list_init(&conf->pkg_dest_list);
2581 +
2582 +     nv_pair_list_init(&conf->arch_list);
2583 +
2584 +     conf->restrict_to_default_dest = 0;
2585 +     conf->default_dest = NULL;
2586 +
2587 +
2588 +     if (args->tmp_dir)
2589 +         tmp_dir_base = args->tmp_dir;
2590 +     else 
2591 +         tmp_dir_base = getenv("TMPDIR");
2592 +     sprintf_alloc(&conf->tmp_dir, "%s/%s",
2593 +                  tmp_dir_base ? tmp_dir_base : IPKG_CONF_DEFAULT_TMP_DIR_BASE,
2594 +                  IPKG_CONF_TMP_DIR_SUFFIX);
2595 +     conf->tmp_dir = mkdtemp(conf->tmp_dir);
2596 +     if (conf->tmp_dir == NULL) {
2597 +         fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
2598 +                 __FUNCTION__, conf->tmp_dir, strerror(errno));
2599 +         return errno;
2600 +     }
2601 +
2602 +     conf->force_depends = 0;
2603 +     conf->force_defaults = 0;
2604 +     conf->force_overwrite = 0;
2605 +     conf->force_downgrade = 0;
2606 +     conf->force_reinstall = 0;
2607 +     conf->force_space = 0;
2608 +     conf->force_removal_of_essential_packages = 0;
2609 +     conf->force_removal_of_dependent_packages = 0;
2610 +     conf->nodeps = 0;
2611 +     conf->verbose_wget = 0;
2612 +     conf->offline_root = NULL;
2613 +     conf->offline_root_pre_script_cmd = NULL;
2614 +     conf->offline_root_post_script_cmd = NULL;
2615 +     conf->multiple_providers = 0;
2616 +     conf->verbosity = 1;
2617 +     conf->noaction = 0;
2618 +
2619 +     conf->http_proxy = NULL;
2620 +     conf->ftp_proxy = NULL;
2621 +     conf->no_proxy = NULL;
2622 +     conf->proxy_user = NULL;
2623 +     conf->proxy_passwd = NULL;
2624 +
2625 +     pkg_hash_init("pkg-hash", &conf->pkg_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2626 +     hash_table_init("file-hash", &conf->file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2627 +     hash_table_init("obs-file-hash", &conf->obs_file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2628 +     lists_dir=(char *)malloc(1);
2629 +     lists_dir[0]='\0';
2630 +     if (args->conf_file) {
2631 +         struct stat stat_buf;
2632 +         err = stat(args->conf_file, &stat_buf);
2633 +         if (err == 0)
2634 +              if (ipkg_conf_parse_file(conf, args->conf_file,
2635 +                                   &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2636 +                   /* Memory leakage from ipkg_conf_parse-file */
2637 +                   return -1;
2638 +               }
2639 +                   
2640 +     }
2641 +
2642 +     /* if (!lists_dir ){*/
2643 +     if (strlen(lists_dir)<=1 ){
2644 +        lists_dir = realloc(lists_dir,strlen(IPKG_CONF_LISTS_DIR)+2);
2645 +        sprintf (lists_dir,"%s",IPKG_CONF_LISTS_DIR);
2646 +     }
2647 +
2648 +     if (args->offline_root) {
2649 +            char *tmp = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
2650 +            sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
2651 +            free(lists_dir);
2652 +            lists_dir = tmp;
2653 +     }
2654 +
2655 +     pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
2656 +     snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
2657 +
2658 +     conf->lists_dir = strdup(lists_dir);
2659 +     conf->pending_dir = strdup(pending_dir);
2660 +
2661 +     if (args->offline_root) 
2662 +         sprintf_alloc(&etc_ipkg_conf_pattern, "%s/etc/ipkg/*.conf", args->offline_root);
2663 +     memset(&globbuf, 0, sizeof(globbuf));
2664 +     err = glob(etc_ipkg_conf_pattern, 0, NULL, &globbuf);
2665 +     if (!err) {
2666 +         int i;
2667 +         for (i = 0; i < globbuf.gl_pathc; i++) {
2668 +              if (globbuf.gl_pathv[i]) 
2669 +                   if ( ipkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
2670 +                                        &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2671 +                        /* Memory leakage from ipkg_conf_parse-file */
2672 +                        return -1;
2673 +                   }
2674 +         }
2675 +     }
2676 +     globfree(&globbuf);
2677 +
2678 +     /* if no architectures were defined, then default all, noarch, and host architecture */
2679 +     if (nv_pair_list_empty(&conf->arch_list)) {
2680 +         nv_pair_list_append(&conf->arch_list, "all", "1");
2681 +         nv_pair_list_append(&conf->arch_list, "noarch", "1");
2682 +         nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
2683 +     }
2684 +
2685 +     /* Even if there is no conf file, we'll need at least one dest. */
2686 +     if (tmp_dest_nv_pair_list.head == NULL) {
2687 +         nv_pair_list_append(&tmp_dest_nv_pair_list,
2688 +                             IPKG_CONF_DEFAULT_DEST_NAME,
2689 +                             IPKG_CONF_DEFAULT_DEST_ROOT_DIR);
2690 +     }
2691 +
2692 +     /* After parsing the file, set options from command-line, (so that
2693 +       command-line arguments take precedence) */
2694 +     /* XXX: CLEANUP: The interaction between args.c and ipkg_conf.c
2695 +       really needs to be cleaned up. There is so much duplication
2696 +       right now it is ridiculous. Maybe ipkg_conf_t should just save
2697 +       a pointer to args_t (which could then not be freed), rather
2698 +       than duplicating every field here? */
2699 +     if (args->force_depends) {
2700 +         conf->force_depends = 1;
2701 +     }
2702 +     if (args->force_defaults) {
2703 +         conf->force_defaults = 1;
2704 +     }
2705 +     if (args->force_overwrite) {
2706 +         conf->force_overwrite = 1;
2707 +     }
2708 +     if (args->force_downgrade) {
2709 +         conf->force_downgrade = 1;
2710 +     }
2711 +     if (args->force_reinstall) {
2712 +         conf->force_reinstall = 1;
2713 +     }
2714 +     if (args->force_removal_of_dependent_packages) {
2715 +         conf->force_removal_of_dependent_packages = 1;
2716 +     }
2717 +     if (args->force_removal_of_essential_packages) {
2718 +         conf->force_removal_of_essential_packages = 1;
2719 +     }
2720 +     if (args->nodeps) {
2721 +         conf->nodeps = 1;
2722 +     }
2723 +     if (args->noaction) {
2724 +         conf->noaction = 1;
2725 +     }
2726 +     if (args->query_all) {
2727 +         conf->query_all = 1;
2728 +     }
2729 +     if (args->verbose_wget) {
2730 +         conf->verbose_wget = 1;
2731 +     }
2732 +     if (args->multiple_providers) {
2733 +         conf->multiple_providers = 1;
2734 +     }
2735 +     if (args->verbosity != conf->verbosity) {
2736 +         conf->verbosity = args->verbosity;
2737 +     } 
2738 +
2739 +     ipkg_conf_override_string(&conf->offline_root, 
2740 +                              args->offline_root);
2741 +     ipkg_conf_override_string(&conf->offline_root_pre_script_cmd, 
2742 +                              args->offline_root_pre_script_cmd);
2743 +     ipkg_conf_override_string(&conf->offline_root_post_script_cmd, 
2744 +                              args->offline_root_post_script_cmd);
2745 +
2746 +/* Pigi: added a flag to disable the checking of structures if the command does not need to 
2747 +         read anything from there.
2748 +*/
2749 +     if ( !(args->nocheckfordirorfile)){
2750 +        /* need to run load the source list before dest list -Jamey */
2751 +        if ( !(args->noreadfeedsfile))
2752 +           set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
2753 +   
2754 +        /* Now that we have resolved conf->offline_root, we can commit to
2755 +          the directory names for the dests and load in all the package
2756 +          lists. */
2757 +        set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
2758 +   
2759 +        if (args->dest) {
2760 +            err = ipkg_conf_set_default_dest(conf, args->dest);
2761 +            if (err) {
2762 +                 return err;
2763 +            }
2764 +        }
2765 +     }
2766 +     nv_pair_list_deinit(&tmp_dest_nv_pair_list);
2767 +     free(lists_dir);
2768 +     free(pending_dir);
2769 +
2770 +     return 0;
2771 +}
2772 +
2773 +void ipkg_conf_deinit(ipkg_conf_t *conf)
2774 +{
2775 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
2776 +#error
2777 +     fprintf(stderr, "%s: Not cleaning up %s since ipkg compiled "
2778 +            "with IPKG_DEBUG_NO_TMP_CLEANUP\n",
2779 +            __FUNCTION__, conf->tmp_dir);
2780 +#else
2781 +     int err;
2782 +
2783 +     err = rmdir(conf->tmp_dir);
2784 +     if (err) {
2785 +         if (errno == ENOTEMPTY) {
2786 +              char *cmd;
2787 +              sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
2788 +              err = xsystem(cmd);
2789 +              free(cmd);
2790 +         }
2791 +         if (err)
2792 +              fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
2793 +     }
2794 +#endif /* IPKG_DEBUG_NO_TMP_CLEANUP */
2795 +
2796 +     free(conf->tmp_dir); /*XXX*/
2797 +
2798 +     pkg_src_list_deinit(&conf->pkg_src_list);
2799 +     pkg_dest_list_deinit(&conf->pkg_dest_list);
2800 +     nv_pair_list_deinit(&conf->arch_list);
2801 +     if (&conf->pkg_hash)
2802 +                   pkg_hash_deinit(&conf->pkg_hash);
2803 +     if (&conf->file_hash)
2804 +                   hash_table_deinit(&conf->file_hash);
2805 +     if (&conf->obs_file_hash)
2806 +                   hash_table_deinit(&conf->obs_file_hash);
2807 +
2808 +     ipkg_conf_free_string(&conf->offline_root);
2809 +     ipkg_conf_free_string(&conf->offline_root_pre_script_cmd);
2810 +     ipkg_conf_free_string(&conf->offline_root_post_script_cmd);
2811 +
2812 +     if (conf->verbosity > 1) { 
2813 +         int i;
2814 +         hash_table_t *hashes[] = {
2815 +              &conf->pkg_hash,
2816 +              &conf->file_hash,
2817 +              &conf->obs_file_hash };
2818 +         for (i = 0; i < 3; i++) {
2819 +              hash_table_t *hash = hashes[i];
2820 +              int c = 0;
2821 +              int n_conflicts = 0;
2822 +              int j;
2823 +              for (j = 0; j < hash->n_entries; j++) {
2824 +                   int len = 0;
2825 +                   hash_entry_t *e = &hash->entries[j];
2826 +                   if (e->next)
2827 +                        n_conflicts++;
2828 +                   while (e && e->key) {
2829 +                        len++;
2830 +                        e = e->next;
2831 +                   }
2832 +                   if (len > c) 
2833 +                        c = len;
2834 +              }
2835 +              ipkg_message(conf, IPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 
2836 +                           hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
2837 +              hash_table_deinit(hash);
2838 +         }
2839 +     }
2840 +}
2841 +
2842 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2843 +                                     const char *default_dest_name)
2844 +{
2845 +     pkg_dest_list_elt_t *iter;
2846 +     pkg_dest_t *dest;
2847 +
2848 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
2849 +         dest = iter->data;
2850 +         if (strcmp(dest->name, default_dest_name) == 0) {
2851 +              conf->default_dest = dest;
2852 +              conf->restrict_to_default_dest = 1;
2853 +              return 0;
2854 +         }
2855 +     }
2856 +
2857 +     fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
2858 +
2859 +     return 1;
2860 +}
2861 +
2862 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
2863 +{
2864 +     pkg_src_list_elt_t *iter;
2865 +     pkg_src_t *src;
2866 +     char *list_file;
2867 +
2868 +     for (iter = pkg_src_list->head; iter; iter = iter->next) {
2869 +          src = iter->data;
2870 +         if (src == NULL) {
2871 +              continue;
2872 +         }
2873 +
2874 +         sprintf_alloc(&list_file, "%s/%s", 
2875 +                         conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
2876 +                         src->name);
2877 +
2878 +         if (file_exists(list_file)) {
2879 +              pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
2880 +         }
2881 +         free(list_file);
2882 +     }
2883 +
2884 +     return 0;
2885 +}
2886 +
2887 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
2888 +{
2889 +     nv_pair_list_elt_t *iter;
2890 +     nv_pair_t *nv_pair;
2891 +     pkg_dest_t *dest;
2892 +     char *root_dir;
2893 +
2894 +     for (iter = nv_pair_list->head; iter; iter = iter->next) {
2895 +         nv_pair = iter->data;
2896 +
2897 +         if (conf->offline_root) {
2898 +              sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
2899 +         } else {
2900 +              root_dir = strdup(nv_pair->value);
2901 +         }
2902 +         dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
2903 +         free(root_dir);
2904 +         if (dest == NULL) {
2905 +              continue;
2906 +         }
2907 +         if (conf->default_dest == NULL) {
2908 +              conf->default_dest = dest;
2909 +         }
2910 +         if (file_exists(dest->status_file_name)) {
2911 +              pkg_hash_add_from_file(conf, dest->status_file_name,
2912 +                                     NULL, dest, 1);
2913 +         }
2914 +     }
2915 +
2916 +     return 0;
2917 +}
2918 +
2919 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2920 +                               pkg_src_list_t *pkg_src_list,
2921 +                               nv_pair_list_t *tmp_dest_nv_pair_list,
2922 +                               char **lists_dir)
2923 +{
2924 +     ipkg_option_t * options;
2925 +     FILE *file = fopen(filename, "r");
2926 +     regex_t valid_line_re, comment_re;
2927 +#define regmatch_size 12
2928 +     regmatch_t regmatch[regmatch_size];
2929 +
2930 +     if (ipkg_init_options_array(conf, &options)<0)
2931 +        return ENOMEM;
2932 +
2933 +     if (file == NULL) {
2934 +         fprintf(stderr, "%s: failed to open %s: %s\n",
2935 +                 __FUNCTION__, filename, strerror(errno));
2936 +         free(options);
2937 +         return errno;
2938 +     }
2939 +     ipkg_message(conf, IPKG_NOTICE, "loading conf file %s\n", filename);
2940 +
2941 +     xregcomp(&comment_re, 
2942 +                   "^[[:space:]]*(#.*|[[:space:]]*)$",
2943 +                   REG_EXTENDED);
2944 +     xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
2945 +
2946 +     while(1) {
2947 +         int line_num = 0;
2948 +         char *line;
2949 +         char *type, *name, *value, *extra;
2950 +
2951 +         line = file_read_line_alloc(file);
2952 +         line_num++;
2953 +         if (line == NULL) {
2954 +              break;
2955 +         }
2956 +
2957 +         str_chomp(line);
2958 +
2959 +         if (regexec(&comment_re, line, 0, 0, 0) == 0) {
2960 +              goto NEXT_LINE;
2961 +         }
2962 +
2963 +         if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
2964 +              str_chomp(line);
2965 +              fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
2966 +                      filename, line_num, line);
2967 +              goto NEXT_LINE;
2968 +         }
2969 +
2970 +         /* This has to be so ugly to deal with optional quotation marks */
2971 +         if (regmatch[2].rm_so > 0) {
2972 +              type = strndup(line + regmatch[2].rm_so,
2973 +                             regmatch[2].rm_eo - regmatch[2].rm_so);
2974 +         } else {
2975 +              type = strndup(line + regmatch[3].rm_so,
2976 +                             regmatch[3].rm_eo - regmatch[3].rm_so);
2977 +         }
2978 +         if (regmatch[5].rm_so > 0) {
2979 +              name = strndup(line + regmatch[5].rm_so,
2980 +                             regmatch[5].rm_eo - regmatch[5].rm_so);
2981 +         } else {
2982 +              name = strndup(line + regmatch[6].rm_so,
2983 +                             regmatch[6].rm_eo - regmatch[6].rm_so);
2984 +         }
2985 +         if (regmatch[8].rm_so > 0) {
2986 +              value = strndup(line + regmatch[8].rm_so,
2987 +                              regmatch[8].rm_eo - regmatch[8].rm_so);
2988 +         } else {
2989 +              value = strndup(line + regmatch[9].rm_so,
2990 +                              regmatch[9].rm_eo - regmatch[9].rm_so);
2991 +         }
2992 +         extra = NULL;
2993 +         if (regmatch[11].rm_so > 0) {
2994 +              extra = strndup (line + regmatch[11].rm_so,
2995 +                               regmatch[11].rm_eo - regmatch[11].rm_so);
2996 +         }
2997 +
2998 +         /* We use the tmp_dest_nv_pair_list below instead of
2999 +            conf->pkg_dest_list because we might encounter an
3000 +            offline_root option later and that would invalidate the
3001 +            directories we would have computed in
3002 +            pkg_dest_list_init. (We do a similar thing with
3003 +            tmp_src_nv_pair_list for sake of symmetry.) */
3004 +         if (strcmp(type, "option") == 0) {
3005 +              ipkg_conf_set_option(options, name, value);
3006 +         } else if (strcmp(type, "src") == 0) {
3007 +              if (!nv_pair_list_find((nv_pair_list_t *)pkg_src_list, name)) {
3008 +                   pkg_src_list_append (pkg_src_list, name, value, extra, 0);
3009 +              } else {
3010 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
3011 +                                name, value);
3012 +              }
3013 +         } else if (strcmp(type, "src/gz") == 0) {
3014 +              if (!nv_pair_list_find((nv_pair_list_t *)pkg_src_list, name)) {
3015 +                   pkg_src_list_append (pkg_src_list, name, value, extra, 1);
3016 +              } else {
3017 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
3018 +                                name, value);
3019 +              }
3020 +         } else if (strcmp(type, "dest") == 0) {
3021 +              nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
3022 +         } else if (strcmp(type, "lists_dir") == 0) {
3023 +              *lists_dir = realloc(*lists_dir,strlen(value)+1);
3024 +               if (*lists_dir == NULL) {
3025 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: Not enough memory\n");
3026 +                   free(options);
3027 +                   return EINVAL;
3028 +               }
3029 +               sprintf (*lists_dir,"%s",value);
3030 +         } else if (strcmp(type, "arch") == 0) {
3031 +              ipkg_message(conf, IPKG_INFO, "supported arch %s priority (%s)\n", name, value);
3032 +              if (!value) {
3033 +                   ipkg_message(conf, IPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
3034 +                   value = strdup("10");
3035 +              }
3036 +              nv_pair_list_append(&conf->arch_list, strdup(name), strdup(value));
3037 +         } else {
3038 +              fprintf(stderr, "WARNING: Ignoring unknown configuration "
3039 +                      "parameter: %s %s %s\n", type, name, value);
3040 +              free(options);
3041 +              return EINVAL;
3042 +         }
3043 +
3044 +         free(type);
3045 +         free(name);
3046 +         free(value);
3047 +         if (extra)
3048 +              free (extra);
3049 +
3050 +     NEXT_LINE:
3051 +         free(line);
3052 +     }
3053 +
3054 +     free(options);
3055 +     regfree(&comment_re);
3056 +     regfree(&valid_line_re);
3057 +     fclose(file);
3058 +
3059 +     return 0;
3060 +}
3061 +
3062 +static int ipkg_conf_set_option(const ipkg_option_t *options,
3063 +                               const char *name, const char *value)
3064 +{
3065 +     int i = 0;
3066 +     while (options[i].name) {
3067 +         if (strcmp(options[i].name, name) == 0) {
3068 +              switch (options[i].type) {
3069 +              case IPKG_OPT_TYPE_BOOL:
3070 +                   *((int *)options[i].value) = 1;
3071 +                   return 0;
3072 +              case IPKG_OPT_TYPE_INT:
3073 +                   if (value) {
3074 +                        *((int *)options[i].value) = atoi(value);
3075 +                        return 0;
3076 +                   } else {
3077 +                        printf("%s: Option %s need an argument\n",
3078 +                               __FUNCTION__, name);
3079 +                        return EINVAL;
3080 +                   }               
3081 +              case IPKG_OPT_TYPE_STRING:
3082 +                   if (value) {
3083 +                        *((char **)options[i].value) = strdup(value);
3084 +                        return 0;
3085 +                   } else {
3086 +                        printf("%s: Option %s need an argument\n",
3087 +                               __FUNCTION__, name);
3088 +                        return EINVAL;
3089 +                   }
3090 +              }
3091 +         }
3092 +         i++;
3093 +     }
3094 +    
3095 +     fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
3096 +            __FUNCTION__, name, value);
3097 +     return EINVAL;
3098 +}
3099 +
3100 +int ipkg_conf_write_status_files(ipkg_conf_t *conf)
3101 +{
3102 +     pkg_dest_list_elt_t *iter;
3103 +     pkg_dest_t *dest;
3104 +     pkg_vec_t *all;
3105 +     pkg_t *pkg;
3106 +     register int i;
3107 +     int err;
3108 +
3109 +     if (conf->noaction)
3110 +         return 0;
3111 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3112 +         dest = iter->data;
3113 +         dest->status_file = fopen(dest->status_file_tmp_name, "w");
3114 +         if (dest->status_file == NULL) {
3115 +              fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
3116 +                      __FUNCTION__, dest->status_file_name, strerror(errno));
3117 +         }
3118 +     }
3119 +
3120 +     all = pkg_vec_alloc();
3121 +     pkg_hash_fetch_available(&conf->pkg_hash, all);
3122 +
3123 +     for(i = 0; i < all->len; i++) {
3124 +         pkg = all->pkgs[i];
3125 +         /* We don't need most uninstalled packages in the status file */
3126 +         if (pkg->state_status == SS_NOT_INSTALLED
3127 +             && (pkg->state_want == SW_UNKNOWN
3128 +                 || pkg->state_want == SW_DEINSTALL
3129 +                 || pkg->state_want == SW_PURGE)) {
3130 +              continue;
3131 +         }
3132 +         if (!pkg) {
3133 +           fprintf(stderr, "Null package\n");
3134 +         }
3135 +         if (pkg->dest == NULL) {
3136 +              fprintf(stderr, "%s: ERROR: Can't write status for "
3137 +                      "package %s since it has a NULL dest\n",
3138 +                      __FUNCTION__, pkg->name);
3139 +              continue;
3140 +         }
3141 +         if (pkg->dest->status_file) {
3142 +              pkg_print_status(pkg, pkg->dest->status_file);
3143 +         }
3144 +     }
3145 +
3146 +     pkg_vec_free(all);
3147 +
3148 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3149 +         dest = iter->data;
3150 +         if (dest->status_file) {
3151 +              err = ferror(dest->status_file);
3152 +              fclose(dest->status_file);
3153 +              dest->status_file = NULL;
3154 +              if (!err) {
3155 +                   file_move(dest->status_file_tmp_name, dest->status_file_name);
3156 +              } else {
3157 +                   fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
3158 +                           "retaining old %s\n", __FUNCTION__, 
3159 +                           dest->status_file_tmp_name, dest->status_file_name);
3160 +              }
3161 +         }
3162 +     }
3163 +
3164 +     return 0;
3165 +}
3166 +
3167 +
3168 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename)
3169 +{
3170 +     char *root_filename;
3171 +     sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
3172 +     return root_filename;
3173 +}
3174 diff -urN busybox.old/archival/libipkg/ipkg_conf.h busybox.dev/archival/libipkg/ipkg_conf.h
3175 --- busybox.old/archival/libipkg/ipkg_conf.h    1970-01-01 01:00:00.000000000 +0100
3176 +++ busybox.dev/archival/libipkg/ipkg_conf.h    2007-01-22 13:41:03.000000000 +0100
3177 @@ -0,0 +1,107 @@
3178 +/* ipkg_conf.h - the itsy package management system
3179 +
3180 +   Carl D. Worth
3181 +
3182 +   Copyright (C) 2001 University of Southern California
3183 +
3184 +   This program is free software; you can redistribute it and/or
3185 +   modify it under the terms of the GNU General Public License as
3186 +   published by the Free Software Foundation; either version 2, or (at
3187 +   your option) any later version.
3188 +
3189 +   This program is distributed in the hope that it will be useful, but
3190 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3191 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3192 +   General Public License for more details.
3193 +*/
3194 +
3195 +#ifndef IPKG_CONF_H
3196 +#define IPKG_CONF_H
3197 +
3198 +typedef struct ipkg_conf ipkg_conf_t;
3199 +
3200 +#include "hash_table.h"
3201 +#include "ipkg.h"
3202 +#include "args.h"
3203 +#include "pkg.h"
3204 +#include "pkg_hash.h"
3205 +#include "pkg_src_list.h"
3206 +#include "pkg_dest_list.h"
3207 +#include "nv_pair_list.h"
3208 +
3209 +#define IPKG_CONF_DEFAULT_TMP_DIR_BASE "/tmp"
3210 +#define IPKG_CONF_TMP_DIR_SUFFIX "ipkg-XXXXXX"
3211 +#define IPKG_CONF_LISTS_DIR  IPKG_STATE_DIR_PREFIX "/lists"
3212 +#define IPKG_CONF_PENDING_DIR IPKG_STATE_DIR_PREFIX "/pending"
3213 +
3214 +/* In case the config file defines no dest */
3215 +#define IPKG_CONF_DEFAULT_DEST_NAME "root"
3216 +#define IPKG_CONF_DEFAULT_DEST_ROOT_DIR "/"
3217 +
3218 +#define IPKG_CONF_DEFAULT_HASH_LEN 1024
3219 +
3220 +struct ipkg_conf
3221 +{
3222 +     pkg_src_list_t pkg_src_list;
3223 +     pkg_dest_list_t pkg_dest_list;
3224 +     nv_pair_list_t arch_list;
3225 +
3226 +     int restrict_to_default_dest;
3227 +     pkg_dest_t *default_dest;
3228 +
3229 +     char *tmp_dir;
3230 +     const char *lists_dir;
3231 +     const char *pending_dir;
3232 +
3233 +     /* options */
3234 +     int force_depends;
3235 +     int force_defaults;
3236 +     int force_overwrite;
3237 +     int force_downgrade;
3238 +     int force_reinstall;
3239 +     int force_space;
3240 +     int force_removal_of_dependent_packages;
3241 +     int force_removal_of_essential_packages;
3242 +     int nodeps; /* do not follow dependences */
3243 +     int verbose_wget;
3244 +     int multiple_providers;
3245 +     char *offline_root;
3246 +     char *offline_root_pre_script_cmd;
3247 +     char *offline_root_post_script_cmd;
3248 +     int query_all;
3249 +     int verbosity;
3250 +     int noaction;
3251 +
3252 +     /* proxy options */
3253 +     char *http_proxy;
3254 +     char *ftp_proxy;
3255 +     char *no_proxy;
3256 +     char *proxy_user;
3257 +     char *proxy_passwd;
3258 +
3259 +     hash_table_t pkg_hash;
3260 +     hash_table_t file_hash;
3261 +     hash_table_t obs_file_hash;
3262 +};
3263 +
3264 +enum ipkg_option_type {
3265 +     IPKG_OPT_TYPE_BOOL,
3266 +     IPKG_OPT_TYPE_INT,
3267 +     IPKG_OPT_TYPE_STRING
3268 +};
3269 +typedef enum ipkg_option_type ipkg_option_type_t;
3270 +
3271 +typedef struct ipkg_option ipkg_option_t;
3272 +struct ipkg_option {
3273 +     const char *name;
3274 +     const ipkg_option_type_t type;
3275 +     const void *value;
3276 +};
3277 +
3278 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args);
3279 +void ipkg_conf_deinit(ipkg_conf_t *conf);
3280 +
3281 +int ipkg_conf_write_status_files(ipkg_conf_t *conf);
3282 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename);
3283 +
3284 +#endif
3285 diff -urN busybox.old/archival/libipkg/ipkg_configure.c busybox.dev/archival/libipkg/ipkg_configure.c
3286 --- busybox.old/archival/libipkg/ipkg_configure.c       1970-01-01 01:00:00.000000000 +0100
3287 +++ busybox.dev/archival/libipkg/ipkg_configure.c       2007-01-22 13:41:03.000000000 +0100
3288 @@ -0,0 +1,40 @@
3289 +/* ipkg_configure.c - the itsy package management system
3290 +
3291 +   Carl D. Worth
3292 +
3293 +   Copyright (C) 2001 University of Southern California
3294 +
3295 +   This program is free software; you can redistribute it and/or
3296 +   modify it under the terms of the GNU General Public License as
3297 +   published by the Free Software Foundation; either version 2, or (at
3298 +   your option) any later version.
3299 +
3300 +   This program is distributed in the hope that it will be useful, but
3301 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3302 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3303 +   General Public License for more details.
3304 +*/
3305 +
3306 +#include "ipkg.h"
3307 +
3308 +#include "ipkg_configure.h"
3309 +
3310 +int ipkg_configure(ipkg_conf_t *conf, pkg_t *pkg)
3311 +{
3312 +    int err;
3313 +
3314 +    /* DPKG_INCOMPATIBILITY:
3315 +       dpkg actually does some conffile handling here, rather than at the
3316 +       end of ipkg_install(). Do we care? */
3317 +    /* DPKG_INCOMPATIBILITY:
3318 +       dpkg actually includes a version number to this script call */
3319 +    err = pkg_run_script(conf, pkg, "postinst", "configure");
3320 +    if (err) {
3321 +       printf("ERROR: %s.postinst returned %d\n", pkg->name, err);
3322 +       return err;
3323 +    }
3324 +
3325 +    ipkg_state_changed++;
3326 +    return 0;
3327 +}
3328 +
3329 diff -urN busybox.old/archival/libipkg/ipkg_configure.h busybox.dev/archival/libipkg/ipkg_configure.h
3330 --- busybox.old/archival/libipkg/ipkg_configure.h       1970-01-01 01:00:00.000000000 +0100
3331 +++ busybox.dev/archival/libipkg/ipkg_configure.h       2007-01-22 13:41:03.000000000 +0100
3332 @@ -0,0 +1,25 @@
3333 +/* ipkg_configure.h - the itsy package management system
3334 +
3335 +   Carl D. Worth
3336 +
3337 +   Copyright (C) 2001 University of Southern California
3338 +
3339 +   This program is free software; you can redistribute it and/or
3340 +   modify it under the terms of the GNU General Public License as
3341 +   published by the Free Software Foundation; either version 2, or (at
3342 +   your option) any later version.
3343 +
3344 +   This program is distributed in the hope that it will be useful, but
3345 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3346 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3347 +   General Public License for more details.
3348 +*/
3349 +
3350 +#ifndef IPKG_CONFIGURE_H
3351 +#define IPKG_CONFIGURE_H
3352 +
3353 +#include "ipkg_conf.h"
3354 +
3355 +int ipkg_configure(ipkg_conf_t *ipkg_conf, pkg_t *pkg);
3356 +
3357 +#endif
3358 diff -urN busybox.old/archival/libipkg/ipkg_download.c busybox.dev/archival/libipkg/ipkg_download.c
3359 --- busybox.old/archival/libipkg/ipkg_download.c        1970-01-01 01:00:00.000000000 +0100
3360 +++ busybox.dev/archival/libipkg/ipkg_download.c        2007-01-22 13:41:06.000000000 +0100
3361 @@ -0,0 +1,195 @@
3362 +/* ipkg_download.c - the itsy package management system
3363 +
3364 +   Carl D. Worth
3365 +
3366 +   Copyright (C) 2001 University of Southern California
3367 +
3368 +   This program is free software; you can redistribute it and/or
3369 +   modify it under the terms of the GNU General Public License as
3370 +   published by the Free Software Foundation; either version 2, or (at
3371 +   your option) any later version.
3372 +
3373 +   This program is distributed in the hope that it will be useful, but
3374 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3375 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3376 +   General Public License for more details.
3377 +*/
3378 +
3379 +#include "ipkg.h"
3380 +#include "ipkg_download.h"
3381 +#include "ipkg_message.h"
3382 +
3383 +#include "sprintf_alloc.h"
3384 +#include "xsystem.h"
3385 +#include "file_util.h"
3386 +#include "str_util.h"
3387 +
3388 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
3389 +{
3390 +    int err = 0;
3391 +
3392 +    char *src_basec = strdup(src);
3393 +    char *src_base = basename(src_basec);
3394 +    char *tmp_file_location;
3395 +    char *cmd;
3396 +
3397 +    ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
3398 +       
3399 +    fflush(stdout);
3400 +    
3401 +    if (str_starts_with(src, "file:")) {
3402 +       int ret;
3403 +       const char *file_src = src + 5;
3404 +       ipkg_message(conf,IPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
3405 +       ret = file_copy(src + 5, dest_file_name);
3406 +       ipkg_message(conf,IPKG_INFO,"Done.\n");
3407 +       return ret;
3408 +    }
3409 +
3410 +    sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
3411 +    err = unlink(tmp_file_location);
3412 +    if (err && errno != ENOENT) {
3413 +       ipkg_message(conf,IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
3414 +               __FUNCTION__, tmp_file_location, strerror(errno));
3415 +       free(tmp_file_location);
3416 +       return errno;
3417 +    }
3418 +
3419 +    if (conf->http_proxy) {
3420 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
3421 +       setenv("http_proxy", conf->http_proxy, 1);
3422 +    }
3423 +    if (conf->ftp_proxy) {
3424 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
3425 +       setenv("ftp_proxy", conf->ftp_proxy, 1);
3426 +    }
3427 +    if (conf->no_proxy) {
3428 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
3429 +       setenv("no_proxy", conf->no_proxy, 1);
3430 +    }
3431 +
3432 +    /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */ 
3433 +    sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
3434 +                 (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
3435 +                 conf->proxy_user ? "--proxy-user=" : "",
3436 +                 conf->proxy_user ? conf->proxy_user : "",
3437 +                 conf->proxy_passwd ? "--proxy-passwd=" : "",
3438 +                 conf->proxy_passwd ? conf->proxy_passwd : "",
3439 +                 conf->verbose_wget ? "" : "-q",
3440 +                 conf->tmp_dir,
3441 +                 src);
3442 +    err = xsystem(cmd);
3443 +    if (err) {
3444 +       if (err != -1) {
3445 +           ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
3446 +                   __FUNCTION__, err, cmd);
3447 +       } 
3448 +       unlink(tmp_file_location);
3449 +       free(tmp_file_location);
3450 +       free(src_basec);
3451 +       free(cmd);
3452 +       return EINVAL;
3453 +    }
3454 +    free(cmd);
3455 +
3456 +    err = file_move(tmp_file_location, dest_file_name);
3457 +
3458 +    free(tmp_file_location);
3459 +    free(src_basec);
3460 +
3461 +    if (err) {
3462 +       return err;
3463 +    }
3464 +
3465 +    return 0;
3466 +}
3467 +
3468 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
3469 +{
3470 +    int err;
3471 +    char *url;
3472 +
3473 +    if (pkg->src == NULL) {
3474 +       ipkg_message(conf,IPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
3475 +               pkg->name, pkg->parent->name);
3476 +       return -1;
3477 +    }
3478 +
3479 +    sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
3480 +
3481 +    /* XXX: BUG: The pkg->filename might be something like
3482 +       "../../foo.ipk". While this is correct, and exactly what we
3483 +       want to use to construct url above, here we actually need to
3484 +       use just the filename part, without any directory. */
3485 +    sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
3486 +
3487 +    err = ipkg_download(conf, url, pkg->local_filename);
3488 +    free(url);
3489 +
3490 +    return err;
3491 +}
3492 +
3493 +/*
3494 + * Downloads file from url, installs in package database, return package name. 
3495 + */
3496 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
3497 +{
3498 +     int err = 0;
3499 +     pkg_t *pkg;
3500 +     pkg = pkg_new();
3501 +     if (pkg == NULL)
3502 +         return ENOMEM;
3503 +
3504 +     if (str_starts_with(url, "http://")
3505 +        || str_starts_with(url, "ftp://")) {
3506 +         char *tmp_file;
3507 +         char *file_basec = strdup(url);
3508 +         char *file_base = basename(file_basec);
3509 +
3510 +         sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
3511 +         err = ipkg_download(conf, url, tmp_file);
3512 +         if (err)
3513 +              return err;
3514 +
3515 +         err = pkg_init_from_file(pkg, tmp_file);
3516 +         if (err)
3517 +              return err;
3518 +         pkg->local_filename = strdup(tmp_file);
3519 +
3520 +         free(tmp_file);
3521 +         free(file_basec);
3522 +
3523 +     } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
3524 +               || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
3525 +
3526 +         err = pkg_init_from_file(pkg, url);
3527 +         if (err)
3528 +              return err;
3529 +         pkg->local_filename = strdup(url);
3530 +         ipkg_message(conf, IPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
3531 +          pkg->provided_by_hand = 1;
3532 +
3533 +     } else {
3534 +       pkg_deinit(pkg);
3535 +       free(pkg);
3536 +       return 0;
3537 +     }
3538 +
3539 +     if (!pkg->architecture) {
3540 +         ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3541 +         return -EINVAL;
3542 +     }
3543 +
3544 +     pkg->dest = conf->default_dest;
3545 +     pkg->state_want = SW_INSTALL;
3546 +     pkg->state_flag |= SF_PREFER;
3547 +     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
3548 +     if ( pkg == NULL ){
3549 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
3550 +        return 0;
3551 +     }
3552 +     if (namep) {
3553 +         *namep = strdup(pkg->name);
3554 +     }
3555 +     return 0;
3556 +}
3557 diff -urN busybox.old/archival/libipkg/ipkg_download.h busybox.dev/archival/libipkg/ipkg_download.h
3558 --- busybox.old/archival/libipkg/ipkg_download.h        1970-01-01 01:00:00.000000000 +0100
3559 +++ busybox.dev/archival/libipkg/ipkg_download.h        2007-01-22 13:41:03.000000000 +0100
3560 @@ -0,0 +1,30 @@
3561 +/* ipkg_download.h - the itsy package management system
3562 +
3563 +   Carl D. Worth
3564 +
3565 +   Copyright (C) 2001 University of Southern California
3566 +
3567 +   This program is free software; you can redistribute it and/or
3568 +   modify it under the terms of the GNU General Public License as
3569 +   published by the Free Software Foundation; either version 2, or (at
3570 +   your option) any later version.
3571 +
3572 +   This program is distributed in the hope that it will be useful, but
3573 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3574 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3575 +   General Public License for more details.
3576 +*/
3577 +
3578 +#ifndef IPKG_DOWNLOAD_H
3579 +#define IPKG_DOWNLOAD_H
3580 +
3581 +#include "ipkg_conf.h"
3582 +
3583 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name);
3584 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir);
3585 +/*
3586 + * Downloads file from url, installs in package database, return package name. 
3587 + */
3588 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep);
3589 +
3590 +#endif
3591 diff -urN busybox.old/archival/libipkg/ipkg.h busybox.dev/archival/libipkg/ipkg.h
3592 --- busybox.old/archival/libipkg/ipkg.h 1970-01-01 01:00:00.000000000 +0100
3593 +++ busybox.dev/archival/libipkg/ipkg.h 2007-01-22 13:41:03.000000000 +0100
3594 @@ -0,0 +1,74 @@
3595 +/* ipkg.h - the itsy package management system
3596 +
3597 +   Carl D. Worth
3598 +
3599 +   Copyright (C) 2001 University of Southern California
3600 +
3601 +   This program is free software; you can redistribute it and/or
3602 +   modify it under the terms of the GNU General Public License as
3603 +   published by the Free Software Foundation; either version 2, or (at
3604 +   your option) any later version.
3605 +
3606 +   This program is distributed in the hope that it will be useful, but
3607 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3608 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3609 +   General Public License for more details.
3610 +*/
3611 +
3612 +#ifndef IPKG_H
3613 +#define IPKG_H
3614 +
3615 +/*
3616 +#ifdef HAVE_CONFIG_H
3617 +#include "config.h"
3618 +#endif
3619 +*/
3620 +
3621 +#if 0
3622 +#define IPKG_DEBUG_NO_TMP_CLEANUP
3623 +#endif
3624 +
3625 +#include "ipkg_includes.h"
3626 +#include "ipkg_conf.h"
3627 +#include "ipkg_message.h"
3628 +
3629 +#define IPKG_PKG_EXTENSION ".ipk"
3630 +#define DPKG_PKG_EXTENSION ".deb"
3631 +
3632 +#define IPKG_LEGAL_PKG_NAME_CHARS "abcdefghijklmnopqrstuvwxyz0123456789.+-"
3633 +#define IPKG_PKG_VERSION_SEP_CHAR '_'
3634 +
3635 +#define IPKG_STATE_DIR_PREFIX IPKGLIBDIR"/ipkg"
3636 +#define IPKG_LISTS_DIR_SUFFIX "lists"
3637 +#define IPKG_INFO_DIR_SUFFIX "info"
3638 +#define IPKG_STATUS_FILE_SUFFIX "status"
3639 +
3640 +#define IPKG_BACKUP_SUFFIX "-ipkg.backup"
3641 +
3642 +#define IPKG_LIST_DESCRIPTION_LENGTH 128
3643 +
3644 +#define IPKG_VERSION "0.99.162"
3645 +
3646 +
3647 +enum ipkg_error {
3648 +    IPKG_SUCCESS = 0,
3649 +    IPKG_PKG_DEPS_UNSATISFIED,
3650 +    IPKG_PKG_IS_ESSENTIAL,
3651 +    IPKG_PKG_HAS_DEPENDENTS,
3652 +    IPKG_PKG_HAS_NO_CANDIDATE
3653 +};
3654 +typedef enum ipkg_error ipkg_error_t;
3655 +
3656 +extern int ipkg_state_changed;
3657 +
3658 +
3659 +struct errlist {
3660 +    char * errmsg;
3661 +    struct errlist * next;
3662 +} ;
3663 +
3664 +extern struct errlist* error_list;
3665 +
3666 +extern ipkg_conf_t *global_conf;
3667 +
3668 +#endif
3669 diff -urN busybox.old/archival/libipkg/ipkg_includes.h busybox.dev/archival/libipkg/ipkg_includes.h
3670 --- busybox.old/archival/libipkg/ipkg_includes.h        1970-01-01 01:00:00.000000000 +0100
3671 +++ busybox.dev/archival/libipkg/ipkg_includes.h        2007-01-22 13:41:03.000000000 +0100
3672 @@ -0,0 +1,79 @@
3673 +#ifndef IPKG_INCLUDES_H
3674 +#define IPKG_INCLUDES_H
3675 +
3676 +/* Define to 1 if you have the <memory.h> header file. */
3677 +#define HAVE_MEMORY_H 1
3678 +
3679 +/* Define to 1 if you have the <regex.h> header file. */
3680 +#define HAVE_REGEX_H 1
3681 +
3682 +/* Define to 1 if you have the <stdlib.h> header file. */
3683 +#define HAVE_STDLIB_H 1
3684 +
3685 +/* Define to 1 if you have the <strings.h> header file. */
3686 +#define HAVE_STRINGS_H 1
3687 +
3688 +/* Define to 1 if you have the <string.h> header file. */
3689 +#define HAVE_STRING_H 1
3690 +
3691 +/* Define to 1 if you have the <sys/stat.h> header file. */
3692 +#define HAVE_SYS_STAT_H 1
3693 +
3694 +/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
3695 +#define HAVE_SYS_WAIT_H 1
3696 +
3697 +/* Define to 1 if you have the <unistd.h> header file. */
3698 +#define HAVE_UNISTD_H 1
3699 +
3700 +/* Define to 1 if you have the ANSI C header files. */
3701 +#define STDC_HEADERS 1
3702 +
3703 +
3704 +#include <stdio.h>
3705 +
3706 +#if STDC_HEADERS
3707 +# include <stdlib.h>
3708 +# include <stdarg.h>
3709 +# include <stddef.h>
3710 +# include <ctype.h>
3711 +# include <errno.h>
3712 +#else
3713 +# if HAVE_STDLIB_H
3714 +#  include <stdlib.h>
3715 +# endif
3716 +#endif
3717 +
3718 +#if HAVE_REGEX_H
3719 +# include <regex.h>
3720 +#endif
3721 +
3722 +#if HAVE_STRING_H
3723 +# if !STDC_HEADERS && HAVE_MEMORY_H
3724 +#  include <memory.h>
3725 +# endif
3726 +/* XXX: What's the right way to pick up GNU's strndup declaration? */
3727 +# if __GNUC__
3728 +#   define __USE_GNU 1
3729 +# endif
3730 +# include <string.h>
3731 +# undef __USE_GNU
3732 +#endif
3733 +
3734 +#if HAVE_STRINGS_H
3735 +# include <strings.h>
3736 +#endif
3737 +
3738 +#if HAVE_SYS_STAT_H
3739 +# include <sys/stat.h>
3740 +#endif
3741 +
3742 +#if HAVE_SYS_WAIT_H
3743 +# include <sys/wait.h>
3744 +#endif
3745 +
3746 +#if HAVE_UNISTD_H
3747 +# include <sys/types.h>
3748 +# include <unistd.h>
3749 +#endif
3750 +
3751 +#endif /* IPKG_INCLUDES_H */
3752 diff -urN busybox.old/archival/libipkg/ipkg_install.c busybox.dev/archival/libipkg/ipkg_install.c
3753 --- busybox.old/archival/libipkg/ipkg_install.c 1970-01-01 01:00:00.000000000 +0100
3754 +++ busybox.dev/archival/libipkg/ipkg_install.c 2007-01-22 13:41:03.000000000 +0100
3755 @@ -0,0 +1,1942 @@
3756 +/* ipkg_install.c - the itsy package management system
3757 +
3758 +   Carl D. Worth
3759 +
3760 +   Copyright (C) 2001 University of Southern California
3761 +
3762 +   This program is free software; you can redistribute it and/or
3763 +   modify it under the terms of the GNU General Public License as
3764 +   published by the Free Software Foundation; either version 2, or (at
3765 +   your option) any later version.
3766 +
3767 +   This program is distributed in the hope that it will be useful, but
3768 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3769 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3770 +   General Public License for more details.
3771 +*/
3772 +
3773 +#include "ipkg.h"
3774 +#include <errno.h>
3775 +#include <dirent.h>
3776 +#include <glob.h>
3777 +#include <time.h>
3778 +#include <signal.h>
3779 +typedef void (*sighandler_t)(int);
3780 +
3781 +#include "pkg.h"
3782 +#include "pkg_hash.h"
3783 +#include "pkg_extract.h"
3784 +
3785 +#include "ipkg_install.h"
3786 +#include "ipkg_configure.h"
3787 +#include "ipkg_download.h"
3788 +#include "ipkg_remove.h"
3789 +
3790 +#include "ipkg_utils.h"
3791 +#include "ipkg_message.h"
3792 +
3793 +#include "sprintf_alloc.h"
3794 +#include "file_util.h"
3795 +#include "str_util.h"
3796 +#include "xsystem.h"
3797 +#include "user.h"
3798 +
3799 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
3800 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg);
3801 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg);
3802 +
3803 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3804 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3805 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3806 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3807 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3808 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3809 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3810 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3811 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3812 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3813 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3814 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3815 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3816 +
3817 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3818 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3819 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg);
3820 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg);
3821 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg);
3822 +
3823 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg);
3824 +
3825 +static int user_prefers_old_conffile(const char *file, const char *backup);
3826 +
3827 +static char *backup_filename_alloc(const char *file_name);
3828 +static int backup_make_backup(ipkg_conf_t *conf, const char *file_name);
3829 +static int backup_exists_for(const char *file_name);
3830 +static int backup_remove(const char *file_name);
3831 +
3832 +
3833 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename)
3834 +{
3835 +     int err, cmp;
3836 +     pkg_t *pkg, *old;
3837 +     char *old_version, *new_version;
3838 +
3839 +     pkg = pkg_new();
3840 +     if (pkg == NULL) {
3841 +         return ENOMEM;
3842 +     }
3843 +
3844 +     err = pkg_init_from_file(pkg, filename);
3845 +     if (err) {
3846 +         return err;
3847 +     }
3848 +
3849 +     if (!pkg->architecture) {
3850 +         ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3851 +         return -EINVAL;
3852 +     }
3853 +
3854 +     /* XXX: CLEANUP: hash_insert_pkg has a nasty side effect of possibly
3855 +       freeing the pkg that we pass in. It might be nice to clean this up
3856 +       if possible.  */
3857 +     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
3858 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
3859 +
3860 +     pkg->local_filename = strdup(filename);
3861 +
3862 +     if (old) {
3863 +         old_version = pkg_version_str_alloc(old);
3864 +         new_version = pkg_version_str_alloc(pkg);
3865 +
3866 +         cmp = pkg_compare_versions(old, pkg);
3867 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
3868 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
3869 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
3870 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
3871 +          } 
3872 +         if (cmp > 0) {
3873 +                ipkg_message(conf, IPKG_NOTICE,
3874 +                             "Not downgrading package %s on %s from %s to %s.\n",
3875 +                             old->name, old->dest->name, old_version, new_version);
3876 +                pkg->state_want = SW_DEINSTALL;
3877 +                pkg->state_flag |= SF_OBSOLETE;
3878 +                free(old_version);
3879 +                free(new_version);
3880 +                return 0;
3881 +         } else {
3882 +              free(old_version);
3883 +              free(new_version);
3884 +         }
3885 +     }
3886 +
3887 +     ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
3888 +     return ipkg_install_pkg(conf, pkg,0);
3889 +}
3890 +
3891 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name)
3892 +{
3893 +     int cmp;
3894 +     pkg_t *old, *new;
3895 +     char *old_version, *new_version;
3896 +
3897 +     ipkg_message(conf, IPKG_DEBUG2, " Getting old  from pkg_hash_fetch \n" );
3898 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
3899 +     if ( old ) 
3900 +        ipkg_message(conf, IPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n",  old->version );
3901 +    
3902 +     ipkg_message(conf, IPKG_DEBUG2, " Getting new  from pkg_hash_fetch \n" );
3903 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
3904 +     if ( new ) 
3905 +        ipkg_message(conf, IPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n",  new->version );
3906 +
3907 +/* Pigi Basically here is broken the version stuff.
3908 +   What's happening is that nothing provide the version to differents 
3909 +   functions, so the returned struct is always the latest.
3910 +   That's why the install by name don't work.
3911 +*/
3912 +     ipkg_message(conf, IPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
3913 +
3914 +     if ( old ) 
3915 +        ipkg_message(conf, IPKG_DEBUG2, " old %s ", old->version );
3916 +     if ( new ) 
3917 +        ipkg_message(conf, IPKG_DEBUG2, " new %s ", new->version );
3918 +     ipkg_message(conf, IPKG_DEBUG2, " \n");
3919 +
3920 +     if (new == NULL) {
3921 +         return IPKG_PKG_HAS_NO_CANDIDATE;
3922 +     }
3923 +
3924 +     new->state_flag |= SF_USER;
3925 +     if (old) {
3926 +         old_version = pkg_version_str_alloc(old);
3927 +         new_version = pkg_version_str_alloc(new);
3928 +
3929 +         cmp = pkg_compare_versions(old, new);
3930 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
3931 +            ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade \n");
3932 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
3933 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
3934 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
3935 +          } 
3936 +         ipkg_message(conf, IPKG_DEBUG, 
3937 +                      "Comparing visible versions of pkg %s:"
3938 +                      "\n\t%s is installed "
3939 +                      "\n\t%s is available "
3940 +                      "\n\t%d was comparison result\n",
3941 +                      pkg_name, old_version, new_version, cmp);
3942 +         if (cmp == 0 && !conf->force_reinstall) {
3943 +              ipkg_message(conf, IPKG_NOTICE,
3944 +                           "Package %s (%s) installed in %s is up to date.\n",
3945 +                           old->name, old_version, old->dest->name);
3946 +              free(old_version);
3947 +              free(new_version);
3948 +              return 0;
3949 +         } else if (cmp > 0) {
3950 +              ipkg_message(conf, IPKG_NOTICE,
3951 +                           "Not downgrading package %s on %s from %s to %s.\n",
3952 +                           old->name, old->dest->name, old_version, new_version);
3953 +              free(old_version);
3954 +              free(new_version);
3955 +              return 0;
3956 +         } else if (cmp < 0) {
3957 +              new->dest = old->dest;
3958 +              old->state_want = SW_DEINSTALL;    /* Here probably the problem for bug 1277 */
3959 +         }
3960 +     }
3961 +
3962 +     /* XXX: CLEANUP: The error code of ipkg_install_by_name is really
3963 +       supposed to be an ipkg_error_t, but ipkg_install_pkg could
3964 +       return any kind of integer, (might be errno from a syscall,
3965 +       etc.). This is a real mess and will need to be cleaned up if
3966 +       anyone ever wants to make a nice libipkg. */
3967 +
3968 +     ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
3969 +     return ipkg_install_pkg(conf, new,0);
3970 +}
3971 +
3972 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name)
3973 +{
3974 +     abstract_pkg_vec_t *providers = pkg_hash_fetch_all_installation_candidates (&conf->pkg_hash, pkg_name);
3975 +     int i;
3976 +     ipkg_error_t err;
3977 +     abstract_pkg_t *ppkg ;
3978 +
3979 +     if (providers == NULL)
3980 +         return IPKG_PKG_HAS_NO_CANDIDATE;
3981 +
3982 +     for (i = 0; i < providers->len; i++) {
3983 +         ppkg = abstract_pkg_vec_get(providers, i);
3984 +          ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_by_name %d \n",__FUNCTION__, i);
3985 +         err = ipkg_install_by_name(conf, ppkg->name);
3986 +         if (err)
3987 +              return err;
3988 +/* XXX Maybe ppkg should be freed ? */
3989 +     }
3990 +     return 0;
3991 +}
3992 +
3993 +/*
3994 + * Walk dependence graph starting with pkg, collect packages to be
3995 + * installed into pkgs_needed, in dependence order.
3996 + */
3997 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *pkgs_needed)
3998 +{
3999 +     int i, err;
4000 +     pkg_vec_t *depends = pkg_vec_alloc();
4001 +     char **unresolved = NULL;
4002 +     int ndepends;
4003 +
4004 +     ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
4005 +                                                       pkg, depends, 
4006 +                                                       &unresolved);
4007 +
4008 +     if (unresolved) {
4009 +         ipkg_message(conf, IPKG_ERROR,
4010 +                      "%s: Cannot satisfy the following dependencies for %s:\n\t",
4011 +                      conf->force_depends ? "Warning" : "ERROR", pkg->name);
4012 +         while (*unresolved) {
4013 +              ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4014 +              unresolved++;
4015 +         }
4016 +         ipkg_message(conf, IPKG_ERROR, "\n");
4017 +         if (! conf->force_depends) {
4018 +              ipkg_message(conf, IPKG_INFO,
4019 +                           "This could mean that your package list is out of date or that the packages\n"
4020 +                           "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4021 +                           "of this problem try again with the '-force-depends' option.\n");
4022 +              pkg_vec_free(depends);
4023 +              return IPKG_PKG_DEPS_UNSATISFIED;
4024 +         }
4025 +     }
4026 +
4027 +     if (ndepends <= 0) {
4028 +         pkg_vec_free(depends);
4029 +         return 0;
4030 +     }
4031 +
4032 +     for (i = 0; i < depends->len; i++) {
4033 +         pkg_t *dep = depends->pkgs[i];
4034 +         /* The package was uninstalled when we started, but another
4035 +            dep earlier in this loop may have depended on it and pulled
4036 +            it in, so check first. */
4037 +         if ((dep->state_status != SS_INSTALLED)
4038 +             && (dep->state_status != SS_UNPACKED)
4039 +             && (dep->state_want != SW_INSTALL)) {
4040 +
4041 +              /* Mark packages as to-be-installed */
4042 +              dep->state_want = SW_INSTALL;
4043 +
4044 +              /* Dependencies should be installed the same place as pkg */
4045 +              if (dep->dest == NULL) {
4046 +                   dep->dest = pkg->dest;
4047 +              }
4048 +
4049 +              err = pkg_mark_dependencies_for_installation(conf, dep, pkgs_needed);
4050 +              if (err) {
4051 +                   pkg_vec_free(depends);
4052 +                   return err;
4053 +              }
4054 +         }
4055 +     }
4056 +     if (pkgs_needed)
4057 +         pkg_vec_insert(pkgs_needed, pkg);
4058 +
4059 +     pkg_vec_free(depends);
4060 +
4061 +     return 0;
4062 +}
4063 +
4064 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed)
4065 +{
4066 +     int cmp;
4067 +     pkg_t *old, *new;
4068 +     char *old_version, *new_version;
4069 +
4070 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
4071 +    
4072 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
4073 +     if (new == NULL) {
4074 +         return IPKG_PKG_HAS_NO_CANDIDATE;
4075 +     }
4076 +     if (old) {
4077 +         old_version = pkg_version_str_alloc(old);
4078 +         new_version = pkg_version_str_alloc(new);
4079 +
4080 +         cmp = pkg_compare_versions(old, new);
4081 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
4082 +           ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade ");
4083 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
4084 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
4085 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
4086 +          } 
4087 +         ipkg_message(conf, IPKG_DEBUG, 
4088 +                      "comparing visible versions of pkg %s:"
4089 +                      "\n\t%s is installed "
4090 +                      "\n\t%s is available "
4091 +                      "\n\t%d was comparison result\n",
4092 +                      pkg_name, old_version, new_version, cmp);
4093 +         if (cmp == 0 && !conf->force_reinstall) {
4094 +              ipkg_message(conf, IPKG_NOTICE,
4095 +                           "Package %s (%s) installed in %s is up to date.\n",
4096 +                           old->name, old_version, old->dest->name);
4097 +              free(old_version);
4098 +              free(new_version);
4099 +              return 0;
4100 +         } else if (cmp > 0) {
4101 +              ipkg_message(conf, IPKG_NOTICE,
4102 +                           "Not downgrading package %s on %s from %s to %s.\n",
4103 +                           old->name, old->dest->name, old_version, new_version);
4104 +              free(old_version);
4105 +              free(new_version);
4106 +              return 0;
4107 +         } else if (cmp < 0) {
4108 +              new->dest = old->dest;
4109 +              old->state_want = SW_DEINSTALL;
4110 +              old->state_flag |= SF_OBSOLETE;
4111 +         }
4112 +     }
4113 +     return pkg_mark_dependencies_for_installation(conf, new, pkgs_needed);
4114 +}
4115 +
4116 +\f
4117 +
4118 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg)
4119 +{
4120 +     int i, err;
4121 +     pkg_vec_t *depends = pkg_vec_alloc();
4122 +     pkg_t *dep;
4123 +     char **unresolved = NULL;
4124 +     int ndepends;
4125 +
4126 +     ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
4127 +                                                       pkg, depends, 
4128 +                                                       &unresolved);
4129 +
4130 +     if (unresolved) {
4131 +         ipkg_message(conf, IPKG_ERROR,
4132 +                      "%s: Cannot satisfy the following dependencies for %s:\n\t",
4133 +                      conf->force_depends ? "Warning" : "ERROR", pkg->name);
4134 +         while (*unresolved) {
4135 +              ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4136 +              unresolved++;
4137 +         }
4138 +         ipkg_message(conf, IPKG_ERROR, "\n");
4139 +         if (! conf->force_depends) {
4140 +              ipkg_message(conf, IPKG_INFO,
4141 +                           "This could mean that your package list is out of date or that the packages\n"
4142 +                           "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4143 +                           "of this problem try again with the '-force-depends' option.\n");
4144 +              pkg_vec_free(depends);
4145 +              return IPKG_PKG_DEPS_UNSATISFIED;
4146 +         }
4147 +     }
4148 +
4149 +     if (ndepends <= 0) {
4150 +         return 0;
4151 +     }
4152 +
4153 +     /* Mark packages as to-be-installed */
4154 +     for (i=0; i < depends->len; i++) {
4155 +         /* Dependencies should be installed the same place as pkg */
4156 +         if (depends->pkgs[i]->dest == NULL) {
4157 +              depends->pkgs[i]->dest = pkg->dest;
4158 +         }
4159 +         depends->pkgs[i]->state_want = SW_INSTALL;
4160 +     }
4161 +
4162 +     for (i = 0; i < depends->len; i++) {
4163 +         dep = depends->pkgs[i];
4164 +         /* The package was uninstalled when we started, but another
4165 +            dep earlier in this loop may have depended on it and pulled
4166 +            it in, so check first. */
4167 +         if ((dep->state_status != SS_INSTALLED)
4168 +             && (dep->state_status != SS_UNPACKED)) {
4169 +               ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4170 +              err = ipkg_install_pkg(conf, dep,0);
4171 +              if (err) {
4172 +                   pkg_vec_free(depends);
4173 +                   return err;
4174 +              }
4175 +         }
4176 +     }
4177 +
4178 +     pkg_vec_free(depends);
4179 +
4180 +     return 0;
4181 +}
4182 +
4183 +
4184 +/* check all packages have their dependences satisfied, e.g., in case an upgraded package split */ 
4185 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf)
4186 +{
4187 +     if (conf->nodeps == 0) {
4188 +         int i;
4189 +         pkg_vec_t *installed = pkg_vec_alloc();
4190 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
4191 +         for (i = 0; i < installed->len; i++) {
4192 +              pkg_t *pkg = installed->pkgs[i];
4193 +              satisfy_dependencies_for(conf, pkg);
4194 +         }
4195 +         pkg_vec_free(installed);
4196 +     }
4197 +     return 0;
4198 +}
4199 +
4200 +\f
4201 +
4202 +static int check_conflicts_for(ipkg_conf_t *conf, pkg_t *pkg)
4203 +{
4204 +     int i;
4205 +     pkg_vec_t *conflicts = NULL;
4206 +     int level;
4207 +     const char *prefix;
4208 +     if (conf->force_depends) {
4209 +         level = IPKG_NOTICE;
4210 +         prefix = "Warning";
4211 +     } else {
4212 +         level = IPKG_ERROR;
4213 +         prefix = "ERROR";
4214 +     }
4215 +
4216 +     if (!conf->force_depends)
4217 +         conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
4218 +
4219 +     if (conflicts) {
4220 +         ipkg_message(conf, level,
4221 +                      "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
4222 +         i = 0;
4223 +         while (i < conflicts->len)
4224 +              ipkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
4225 +         ipkg_message(conf, level, "\n");
4226 +         pkg_vec_free(conflicts);
4227 +         return IPKG_PKG_DEPS_UNSATISFIED;
4228 +     }
4229 +     return 0;
4230 +}
4231 +
4232 +static int update_file_ownership(ipkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
4233 +{
4234 +     str_list_t *new_list = pkg_get_installed_files(new_pkg);
4235 +     str_list_elt_t *iter;
4236 +
4237 +     for (iter = new_list->head; iter; iter = iter->next) {
4238 +         char *new_file = iter->data;
4239 +         pkg_t *owner = file_hash_get_file_owner(conf, new_file);
4240 +         if (!new_file)
4241 +              ipkg_message(conf, IPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
4242 +         if (!owner || (owner == old_pkg))
4243 +              file_hash_set_file_owner(conf, new_file, new_pkg);
4244 +     }
4245 +     if (old_pkg) {
4246 +         str_list_t *old_list = pkg_get_installed_files(old_pkg);
4247 +         for (iter = old_list->head; iter; iter = iter->next) {
4248 +              char *old_file = iter->data;
4249 +              pkg_t *owner = file_hash_get_file_owner(conf, old_file);
4250 +              if (owner == old_pkg) {
4251 +                   /* obsolete */
4252 +                   hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
4253 +              }
4254 +         }
4255 +     }
4256 +     return 0;
4257 +}
4258 +
4259 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg)
4260 +{
4261 +    /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
4262 +
4263 +    /* sma 6.20.02:  yup; here's the first bit */
4264 +    /* 
4265 +     * XXX: BUG easy for cworth
4266 +     * 1) please point the call below to the correct current root destination
4267 +     * 2) we need to resolve how to check the required space for a pending pkg, 
4268 +     *    my diddling with the .ipk file size below isn't going to cut it.
4269 +     * 3) return a proper error code instead of 1
4270 +     */
4271 +     int comp_size, blocks_available;
4272 +    
4273 +     if (!conf->force_space && pkg->installed_size != NULL) {
4274 +         blocks_available = get_available_blocks(conf->default_dest->root_dir);
4275 +
4276 +         comp_size = strtoul(pkg->installed_size, NULL, 0);
4277 +         /* round up a blocks count without doing fancy-but-slow casting jazz */ 
4278 +         comp_size = (int)((comp_size + 1023) / 1024);
4279 +
4280 +         if (comp_size >= blocks_available) {
4281 +              ipkg_message(conf, IPKG_ERROR,
4282 +                           "Only have %d available blocks on filesystem %s, pkg %s needs %d\n", 
4283 +                           blocks_available, conf->default_dest->root_dir, pkg->name, comp_size);
4284 +              return ENOSPC;
4285 +         }
4286 +     }
4287 +     return 0;
4288 +}
4289 +
4290 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg)
4291 +{
4292 +     int err;
4293 +     char *conffiles_file_name;
4294 +     char *root_dir;
4295 +     FILE *conffiles_file;
4296 +
4297 +     sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
4298 +
4299 +     pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
4300 +     if (pkg->tmp_unpack_dir == NULL) {
4301 +         ipkg_message(conf, IPKG_ERROR,
4302 +                      "%s: Failed to create temporary directory '%s': %s\n",
4303 +                      __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
4304 +         return errno;
4305 +     }
4306 +
4307 +     err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
4308 +     if (err) {
4309 +         return err;
4310 +     }
4311 +
4312 +     /* XXX: CLEANUP: There might be a cleaner place to read in the
4313 +       conffiles. Seems like I should be able to get everything to go
4314 +       through pkg_init_from_file. If so, maybe it would make sense to
4315 +       move all of unpack_pkg_control_files to that function. */
4316 +
4317 +     /* Don't need to re-read conffiles if we already have it */
4318 +     if (pkg->conffiles.head) {
4319 +         return 0;
4320 +     }
4321 +
4322 +     sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
4323 +     if (! file_exists(conffiles_file_name)) {
4324 +         free(conffiles_file_name);
4325 +         return 0;
4326 +     }
4327 +    
4328 +     conffiles_file = fopen(conffiles_file_name, "r");
4329 +     if (conffiles_file == NULL) {
4330 +         fprintf(stderr, "%s: failed to open %s: %s\n",
4331 +                 __FUNCTION__, conffiles_file_name, strerror(errno));
4332 +         free(conffiles_file_name);
4333 +         return errno;
4334 +     }
4335 +     free(conffiles_file_name);
4336 +
4337 +     while (1) {
4338 +         char *cf_name;
4339 +         char *cf_name_in_dest;
4340 +
4341 +         cf_name = file_read_line_alloc(conffiles_file);
4342 +         if (cf_name == NULL) {
4343 +              break;
4344 +         }
4345 +         str_chomp(cf_name);
4346 +         if (cf_name[0] == '\0') {
4347 +              continue;
4348 +         }
4349 +
4350 +         /* Prepend dest->root_dir to conffile name.
4351 +            Take pains to avoid multiple slashes. */
4352 +         root_dir = pkg->dest->root_dir;
4353 +         if (conf->offline_root)
4354 +              /* skip the offline_root prefix */
4355 +              root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
4356 +         sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
4357 +                       cf_name[0] == '/' ? (cf_name + 1) : cf_name);
4358 +
4359 +         /* Can't get an md5sum now, (file isn't extracted yet).
4360 +            We'll wait until resolve_conffiles */
4361 +         conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
4362 +
4363 +         free(cf_name);
4364 +         free(cf_name_in_dest);
4365 +     }
4366 +
4367 +     fclose(conffiles_file);
4368 +
4369 +     return 0;
4370 +}
4371 +
4372 +/* returns number of installed replacees */
4373 +int pkg_get_installed_replacees(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
4374 +{
4375 +     abstract_pkg_t **replaces = pkg->replaces;
4376 +     int replaces_count = pkg->replaces_count;
4377 +     int i, j;
4378 +     for (i = 0; i < replaces_count; i++) {
4379 +         abstract_pkg_t *ab_pkg = replaces[i];
4380 +         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
4381 +         if (pkg_vec) {
4382 +              for (j = 0; j < pkg_vec->len; j++) {
4383 +                   pkg_t *replacee = pkg_vec->pkgs[j];
4384 +                   if (!pkg_conflicts(pkg, replacee))
4385 +                        continue;
4386 +                   if (replacee->state_status == SS_INSTALLED) {
4387 +                        pkg_vec_insert(installed_replacees, replacee);
4388 +                   }
4389 +              }
4390 +         }
4391 +     }
4392 +     return installed_replacees->len;
4393 +}
4394 +
4395 +int pkg_remove_installed_replacees(ipkg_conf_t *conf, pkg_vec_t *replacees)
4396 +{
4397 +     int i;
4398 +     int replaces_count = replacees->len;
4399 +     for (i = 0; i < replaces_count; i++) {
4400 +         pkg_t *replacee = replacees->pkgs[i];
4401 +         int err;
4402 +         replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
4403 +         err = ipkg_remove_pkg(conf, replacee,0);
4404 +         if (err)
4405 +              return err;
4406 +     }
4407 +     return 0;
4408 +}
4409 +
4410 +/* to unwind the removal: make sure they are installed */
4411 +int pkg_remove_installed_replacees_unwind(ipkg_conf_t *conf, pkg_vec_t *replacees)
4412 +{
4413 +     int i, err;
4414 +     int replaces_count = replacees->len;
4415 +     for (i = 0; i < replaces_count; i++) {
4416 +         pkg_t *replacee = replacees->pkgs[i];
4417 +         if (replacee->state_status != SS_INSTALLED) {
4418 +               ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4419 +              err = ipkg_install_pkg(conf, replacee,0);
4420 +              if (err)
4421 +                   return err;
4422 +         }
4423 +     }
4424 +     return 0;
4425 +}
4426 +
4427 +int caught_sigint = 0;
4428 +static void ipkg_install_pkg_sigint_handler(int sig)
4429 +{
4430 +     caught_sigint = sig;
4431 +}
4432 +
4433 +/* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
4434 +static int ipkg_install_check_downgrade(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
4435 +{        
4436 +     if (old_pkg) {
4437 +          char message_out[15];
4438 +         char *old_version = pkg_version_str_alloc(old_pkg);
4439 +         char *new_version = pkg_version_str_alloc(pkg);
4440 +         int cmp = pkg_compare_versions(old_pkg, pkg);
4441 +         int rc = 0;
4442 +
4443 +          memset(message_out,'\x0',15);
4444 +          strncpy (message_out,"Upgrading ",strlen("Upgrading ")); 
4445 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
4446 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
4447 +             strncpy (message_out,"Downgrading ",strlen("Downgrading "));         /* We need to use a value < 0 because in the 0 case we are asking to */
4448 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
4449 +          } 
4450 +
4451 +         if (cmp > 0) {
4452 +              ipkg_message(conf, IPKG_NOTICE,
4453 +                           "Not downgrading package %s on %s from %s to %s.\n",
4454 +                           old_pkg->name, old_pkg->dest->name, old_version, new_version);
4455 +              rc = 1;
4456 +         } else if (cmp < 0) {
4457 +              ipkg_message(conf, IPKG_NOTICE,
4458 +                           "%s%s on %s from %s to %s...\n",
4459 +                           message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
4460 +              pkg->dest = old_pkg->dest;
4461 +              rc = 0;
4462 +         } else /* cmp == 0 */ {
4463 +              if (conf->force_reinstall) {
4464 +                   ipkg_message(conf, IPKG_NOTICE,
4465 +                                "Reinstalling %s (%s) on %s...\n",
4466 +                                pkg->name, new_version, old_pkg->dest->name);
4467 +                   pkg->dest = old_pkg->dest;
4468 +                   rc = 0;
4469 +              } else {
4470 +                   ipkg_message(conf, IPKG_NOTICE,
4471 +                                "Not installing %s (%s) on %s -- already installed.\n",
4472 +                                pkg->name, new_version, old_pkg->dest->name);
4473 +                   rc = 1;
4474 +              }
4475 +         } 
4476 +         free(old_version);
4477 +         free(new_version);
4478 +         return rc;
4479 +     } else {
4480 +      char message_out[15], *version ;
4481 +      memset(message_out,'\x0',15);
4482 +      if ( message ) 
4483 +          strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
4484 +      else 
4485 +          strncpy( message_out,"Installing ",strlen("Installing ") );
4486 +         version = pkg_version_str_alloc(pkg);
4487 +      
4488 +         ipkg_message(conf, IPKG_NOTICE,
4489 +                      "%s%s (%s) to %s...\n", message_out,
4490 +                      pkg->name, version, pkg->dest->name);
4491 +         free(version);
4492 +         return 0;
4493 +     }
4494 +}
4495 +
4496 +/* and now the meat... */
4497 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
4498 +{
4499 +     int err = 0;
4500 +     int message = 0;
4501 +     pkg_t *old_pkg = NULL;
4502 +     pkg_vec_t *replacees;
4503 +     abstract_pkg_t *ab_pkg = NULL;
4504 +     int old_state_flag;
4505 +     char* file_md5;
4506 +
4507 +    
4508 +     if ( from_upgrade ) 
4509 +        message = 1;            /* Coming from an upgrade, and should change the output message */
4510 +
4511 +     if (!pkg) {
4512 +         ipkg_message(conf, IPKG_ERROR,
4513 +                      "INTERNAL ERROR: null pkg passed to ipkg_install_pkg\n");
4514 +         return -EINVAL;
4515 +     }
4516 +
4517 +     ipkg_message(conf, IPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
4518 +
4519 +     if (!pkg_arch_supported(conf, pkg)) {
4520 +         ipkg_message(conf, IPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
4521 +                      pkg->architecture, pkg->name);
4522 +         return -EINVAL;
4523 +     }
4524 +     if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
4525 +         err = satisfy_dependencies_for(conf, pkg);
4526 +         if (err) { return err; }
4527 +
4528 +         ipkg_message(conf, IPKG_NOTICE,
4529 +                      "Package %s is already installed in %s.\n", 
4530 +                      pkg->name, pkg->dest->name);
4531 +         return 0;
4532 +     }
4533 +
4534 +     if (pkg->dest == NULL) {
4535 +         pkg->dest = conf->default_dest;
4536 +     }
4537 +
4538 +     old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
4539 +
4540 +     err = ipkg_install_check_downgrade(conf, pkg, old_pkg, message);
4541 +     if (err) { return err; }
4542 +
4543 +     pkg->state_want = SW_INSTALL;
4544 +     if (old_pkg){                          
4545 +         old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
4546 +     }
4547 +
4548 +
4549 +     /* Abhaya: conflicts check */
4550 +     err = check_conflicts_for(conf, pkg);
4551 +     if (err) { return err; }
4552 +    
4553 +     /* this setup is to remove the upgrade scenario in the end when
4554 +       installing pkg A, A deps B & B deps on A. So both B and A are
4555 +       installed. Then A's installation is started resulting in an
4556 +       uncecessary upgrade */ 
4557 +     if (pkg->state_status == SS_INSTALLED
4558 +        && conf->force_reinstall == 0) return 0;
4559 +    
4560 +     err = verify_pkg_installable(conf, pkg);
4561 +     if (err) { return err; }
4562 +
4563 +     if (pkg->local_filename == NULL) {
4564 +         err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
4565 +         if (err) {
4566 +              ipkg_message(conf, IPKG_ERROR,
4567 +                           "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
4568 +                           pkg->name);
4569 +              return err;
4570 +         }
4571 +     }
4572 +
4573 +/* Check for md5 values */
4574 +     if (pkg->md5sum)
4575 +     {
4576 +         file_md5 = file_md5sum_alloc(pkg->local_filename);
4577 +         if (strcmp(file_md5, pkg->md5sum))
4578 +         {
4579 +              ipkg_message(conf, IPKG_ERROR,
4580 +                           "Package %s md5sum mismatch. Either the ipkg or the package index are corrupt. Try 'ipkg update'.\n",
4581 +                           pkg->name);
4582 +              free(file_md5);
4583 +              return err;
4584 +         }
4585 +         free(file_md5);
4586 +     }
4587 +
4588 +     if (pkg->tmp_unpack_dir == NULL) {
4589 +         unpack_pkg_control_files(conf, pkg);
4590 +     }
4591 +
4592 +     /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
4593 +/* Pigi: check if it will pass from here when replacing. It seems to fail */
4594 +/* That's rather strange that files don't change owner. Investigate !!!!!!*/
4595 +     err = update_file_ownership(conf, pkg, old_pkg);
4596 +     if (err) { return err; }
4597 +
4598 +     if (conf->nodeps == 0) {
4599 +         err = satisfy_dependencies_for(conf, pkg);
4600 +         if (err) { return err; }
4601 +     }
4602 +
4603 +     replacees = pkg_vec_alloc();
4604 +     pkg_get_installed_replacees(conf, pkg, replacees);
4605 +
4606 +     /* this next section we do with SIGINT blocked to prevent inconsistency between ipkg database and filesystem */
4607 +     {
4608 +         sigset_t newset, oldset;
4609 +         sighandler_t old_handler = NULL;
4610 +         int use_signal = 0;
4611 +         caught_sigint = 0;
4612 +         if (use_signal) {
4613 +              old_handler = signal(SIGINT, ipkg_install_pkg_sigint_handler);
4614 +         } else {
4615 +              sigemptyset(&newset);
4616 +              sigaddset(&newset, SIGINT);
4617 +              sigprocmask(SIG_BLOCK, &newset, &oldset);
4618 +         }
4619 +
4620 +         ipkg_state_changed++;
4621 +         pkg->state_flag |= SF_FILELIST_CHANGED;
4622 +
4623 +         /* XXX: BUG: we really should treat replacement more like an upgrade
4624 +          *      Instead, we're going to remove the replacees 
4625 +          */
4626 +         err = pkg_remove_installed_replacees(conf, replacees);
4627 +         if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
4628 +
4629 +         err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
4630 +         if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
4631 +
4632 +         err = prerm_deconfigure_conflictors(conf, pkg, replacees);
4633 +         if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
4634 +
4635 +         err = preinst_configure(conf, pkg, old_pkg);
4636 +         if (err) goto UNWIND_PREINST_CONFIGURE;
4637 +
4638 +         err = backup_modified_conffiles(conf, pkg, old_pkg);
4639 +         if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
4640 +
4641 +         err = check_data_file_clashes(conf, pkg, old_pkg);
4642 +         if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
4643 +
4644 +         err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
4645 +         if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
4646 +
4647 +         if (conf->noaction) return 0;
4648 +
4649 +         /* point of no return: no unwinding after this */
4650 +         if (old_pkg && !conf->force_reinstall) {
4651 +              old_pkg->state_want = SW_DEINSTALL;
4652 +
4653 +              if (old_pkg->state_flag & SF_NOPRUNE) {
4654 +                   ipkg_message(conf, IPKG_INFO,
4655 +                                "  not removing obsolesced files because package marked noprune\n");
4656 +              } else {
4657 +                   ipkg_message(conf, IPKG_INFO,
4658 +                                "  removing obsolesced files\n");
4659 +                   remove_obsolesced_files(conf, pkg, old_pkg);
4660 +              }
4661 +               /* removing files from old package, to avoid ghost files */ 
4662 +               remove_data_files_and_list(conf, old_pkg);
4663 +/* Pigi : It should be better to remove also maintainer and postrem scripts here, just in case*/
4664 +               remove_maintainer_scripts_except_postrm(conf, old_pkg);
4665 +               remove_postrm(conf, old_pkg);
4666 +/* Pigi */
4667 +
4668 +         }
4669 +
4670 +
4671 +         ipkg_message(conf, IPKG_INFO,
4672 +                      "  installing maintainer scripts\n");
4673 +         install_maintainer_scripts(conf, pkg, old_pkg);
4674 +
4675 +         /* the following just returns 0 */
4676 +         remove_disappeared(conf, pkg);
4677 +
4678 +         ipkg_message(conf, IPKG_INFO,
4679 +                      "  installing data files\n");
4680 +         install_data_files(conf, pkg);
4681 +
4682 +/* read comments from function for detail but I will execute this here as all other tests are ok.*/
4683 +         err = check_data_file_clashes_change(conf, pkg, old_pkg);
4684 +
4685 +         ipkg_message(conf, IPKG_INFO,
4686 +                      "  resolving conf files\n");
4687 +         resolve_conffiles(conf, pkg);
4688 +
4689 +         pkg->state_status = SS_UNPACKED;
4690 +         old_state_flag = pkg->state_flag;
4691 +         pkg->state_flag &= ~SF_PREFER;
4692 +         ipkg_message(conf, IPKG_DEBUG, "   pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
4693 +
4694 +         if (old_pkg && !conf->force_reinstall) {
4695 +              old_pkg->state_status = SS_NOT_INSTALLED;
4696 +         }
4697 +
4698 +         time(&pkg->installed_time);
4699 +
4700 +         ipkg_message(conf, IPKG_INFO,
4701 +                      "  cleanup temp files\n");
4702 +         cleanup_temporary_files(conf, pkg);
4703 +
4704 +         ab_pkg = pkg->parent;
4705 +         if (ab_pkg)
4706 +              ab_pkg->state_status = pkg->state_status;
4707 +
4708 +         ipkg_message(conf, IPKG_INFO, "Done.\n");
4709 +
4710 +         if (use_signal)
4711 +              signal(SIGINT, old_handler);
4712 +         else
4713 +              sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4714 +
4715 +         return 0;
4716 +     
4717 +
4718 +     UNWIND_POSTRM_UPGRADE_OLD_PKG:
4719 +         postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4720 +     UNWIND_CHECK_DATA_FILE_CLASHES:
4721 +         check_data_file_clashes_unwind(conf, pkg, old_pkg);
4722 +     UNWIND_BACKUP_MODIFIED_CONFFILES:
4723 +         backup_modified_conffiles_unwind(conf, pkg, old_pkg);
4724 +     UNWIND_PREINST_CONFIGURE:
4725 +         preinst_configure_unwind(conf, pkg, old_pkg);
4726 +     UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
4727 +         prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
4728 +     UNWIND_PRERM_UPGRADE_OLD_PKG:
4729 +         prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4730 +     UNWIND_REMOVE_INSTALLED_REPLACEES:
4731 +         pkg_remove_installed_replacees_unwind(conf, replacees);
4732 +
4733 +         ipkg_message(conf, IPKG_INFO,
4734 +                      "  cleanup temp files\n");
4735 +         cleanup_temporary_files(conf, pkg);
4736 +
4737 +         ipkg_message(conf, IPKG_INFO,
4738 +                      "Failed.\n");
4739 +         if (use_signal)
4740 +              signal(SIGINT, old_handler);
4741 +         else
4742 +              sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4743 +
4744 +         return err;
4745 +     }
4746 +}
4747 +
4748 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4749 +{
4750 +     /* DPKG_INCOMPATIBILITY:
4751 +       dpkg does some things here that we don't do yet. Do we care?
4752 +       
4753 +       1. If a version of the package is already installed, call
4754 +          old-prerm upgrade new-version
4755 +       2. If the script runs but exits with a non-zero exit status
4756 +          new-prerm failed-upgrade old-version
4757 +          Error unwind, for both the above cases:
4758 +          old-postinst abort-upgrade new-version
4759 +     */
4760 +     return 0;
4761 +}
4762 +
4763 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4764 +{
4765 +     /* DPKG_INCOMPATIBILITY:
4766 +       dpkg does some things here that we don't do yet. Do we care?
4767 +       (See prerm_upgrade_old_package for details)
4768 +     */
4769 +     return 0;
4770 +}
4771 +
4772 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4773 +{
4774 +     /* DPKG_INCOMPATIBILITY:
4775 +       dpkg does some things here that we don't do yet. Do we care?
4776 +       2. If a 'conflicting' package is being removed at the same time:
4777 +               1. If any packages depended on that conflicting package and
4778 +                  --auto-deconfigure is specified, call, for each such package:
4779 +                  deconfigured's-prerm deconfigure \
4780 +                  in-favour package-being-installed version \
4781 +                  removing conflicting-package version
4782 +               Error unwind:
4783 +                  deconfigured's-postinst abort-deconfigure \
4784 +                  in-favour package-being-installed-but-failed version \
4785 +                  removing conflicting-package version
4786 +
4787 +                  The deconfigured packages are marked as requiring
4788 +                  configuration, so that if --install is used they will be
4789 +                  configured again if possible.
4790 +               2. To prepare for removal of the conflicting package, call:
4791 +                  conflictor's-prerm remove in-favour package new-version
4792 +               Error unwind:
4793 +                  conflictor's-postinst abort-remove in-favour package new-version
4794 +     */
4795 +     return 0;
4796 +}
4797 +
4798 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4799 +{
4800 +     /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
4801 +       do yet. Do we care?  (See prerm_deconfigure_conflictors for
4802 +       details) */
4803 +     return 0;
4804 +}
4805 +
4806 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4807 +{
4808 +     int err;
4809 +     char *preinst_args;
4810 +
4811 +     if (old_pkg) {
4812 +         char *old_version = pkg_version_str_alloc(old_pkg);
4813 +         sprintf_alloc(&preinst_args, "upgrade %s", old_version);
4814 +         free(old_version);
4815 +     } else if (pkg->state_status == SS_CONFIG_FILES) {
4816 +         char *pkg_version = pkg_version_str_alloc(pkg);
4817 +         sprintf_alloc(&preinst_args, "install %s", pkg_version);
4818 +         free(pkg_version);
4819 +     } else {
4820 +         preinst_args = strdup("install");
4821 +     }
4822 +
4823 +     err = pkg_run_script(conf, pkg, "preinst", preinst_args);
4824 +     if (err) {
4825 +         ipkg_message(conf, IPKG_ERROR,
4826 +                      "Aborting installation of %s\n", pkg->name);
4827 +         return 1;
4828 +     }
4829 +
4830 +     free(preinst_args);
4831 +
4832 +     return 0;
4833 +}
4834 +
4835 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4836 +{
4837 +     /* DPKG_INCOMPATIBILITY:
4838 +       dpkg does the following error unwind, should we?
4839 +       pkg->postrm abort-upgrade old-version
4840 +       OR pkg->postrm abort-install old-version
4841 +       OR pkg->postrm abort-install
4842 +     */
4843 +     return 0;
4844 +}
4845 +
4846 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4847 +{
4848 +     int err;
4849 +     conffile_list_elt_t *iter;
4850 +     conffile_t *cf;
4851 +
4852 +     if (conf->noaction) return 0;
4853 +
4854 +     /* Backup all modified conffiles */
4855 +     if (old_pkg) {
4856 +         for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4857 +              char *cf_name;
4858 +              
4859 +              cf = iter->data;
4860 +              cf_name = root_filename_alloc(conf, cf->name);
4861 +
4862 +              /* Don't worry if the conffile is just plain gone */
4863 +              if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
4864 +                   err = backup_make_backup(conf, cf_name);
4865 +                   if (err) {
4866 +                        return err;
4867 +                   }
4868 +              }
4869 +              free(cf_name);
4870 +         }
4871 +     }
4872 +
4873 +     /* Backup all conffiles that were not conffiles in old_pkg */
4874 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4875 +         char *cf_name;
4876 +         cf = iter->data;
4877 +         cf_name = root_filename_alloc(conf, cf->name);
4878 +         /* Ignore if this was a conffile in old_pkg as well */
4879 +         if (pkg_get_conffile(old_pkg, cf->name)) {
4880 +              continue;
4881 +         }
4882 +
4883 +         if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
4884 +              err = backup_make_backup(conf, cf_name);
4885 +              if (err) {
4886 +                   return err;
4887 +              }
4888 +         }
4889 +         free(cf_name);
4890 +     }
4891 +
4892 +     return 0;
4893 +}
4894 +
4895 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4896 +{
4897 +     conffile_list_elt_t *iter;
4898 +
4899 +     if (old_pkg) {
4900 +         for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4901 +              backup_remove(iter->data->name);
4902 +         }
4903 +     }
4904 +
4905 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4906 +         backup_remove(iter->data->name);
4907 +     }
4908 +
4909 +     return 0;
4910 +}
4911 +
4912 +
4913 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4914 +{
4915 +     /* DPKG_INCOMPATIBILITY:
4916 +       ipkg takes a slightly different approach than dpkg at this
4917 +       point.  dpkg installs each file in the new package while
4918 +       creating a backup for any file that is replaced, (so that it
4919 +       can unwind if necessary).  To avoid complexity and redundant
4920 +       storage, ipkg doesn't do any installation until later, (at the
4921 +       point at which dpkg removes the backups.
4922 +       
4923 +       But, we do have to check for data file clashes, since after
4924 +       installing a package with a file clash, removing either of the
4925 +       packages involved in the clash has the potential to break the
4926 +       other package.
4927 +     */
4928 +     str_list_t *files_list;
4929 +     str_list_elt_t *iter;
4930 +
4931 +     int clashes = 0;
4932 +
4933 +     files_list = pkg_get_installed_files(pkg);
4934 +     for (iter = files_list->head; iter; iter = iter->next) {
4935 +         char *root_filename;
4936 +         char *filename = iter->data;
4937 +         root_filename = root_filename_alloc(conf, filename);
4938 +         if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
4939 +              pkg_t *owner;
4940 +              pkg_t *obs;
4941 +              /* Pre-existing conffiles are OK */
4942 +              /* @@@@ should have way to check that it is a conffile -Jamey */
4943 +              if (backup_exists_for(root_filename)) {
4944 +                   continue;
4945 +              }
4946 +
4947 +              /* Pre-existing files are OK if force-overwrite was asserted. */ 
4948 +              if (conf->force_overwrite) {
4949 +                   /* but we need to change who owns this file */
4950 +                   file_hash_set_file_owner(conf, filename, pkg);
4951 +                   continue;
4952 +              }
4953 +
4954 +              owner = file_hash_get_file_owner(conf, filename);
4955 +
4956 +              /* Pre-existing files are OK if owned by the pkg being upgraded. */
4957 +              if (owner && old_pkg) {
4958 +                   if (strcmp(owner->name, old_pkg->name) == 0) {
4959 +                        continue;
4960 +                   }
4961 +              }
4962 +
4963 +              /* Pre-existing files are OK if owned by a package replaced by new pkg. */
4964 +              if (owner) {
4965 +                    ipkg_message(conf, IPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
4966 +                   if (pkg_replaces(pkg, owner)) {
4967 +                        continue;
4968 +                   }
4969 +/* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
4970 +   then it's ok to overwrite. */
4971 +                    if (strcmp(owner->name,pkg->name)==0){
4972 +                        ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
4973 +                        continue;
4974 +                    }
4975 +              }
4976 +
4977 +              /* Pre-existing files are OK if they are obsolete */
4978 +              obs = hash_table_get(&conf->obs_file_hash, filename);
4979 +              if (obs) {
4980 +                   ipkg_message(conf, IPKG_INFO, "Pre-exiting file %s is obsolete.  obs_pkg=%s\n", filename, obs->name);
4981 +                   continue;
4982 +              }
4983 +
4984 +              /* We have found a clash. */
4985 +              ipkg_message(conf, IPKG_ERROR,
4986 +                           "Package %s wants to install file %s\n"
4987 +                           "\tBut that file is already provided by package ",
4988 +                           pkg->name, filename);
4989 +              if (owner) {
4990 +                   ipkg_message(conf, IPKG_ERROR,
4991 +                                "%s\n", owner->name);
4992 +              } else {
4993 +                   ipkg_message(conf, IPKG_ERROR,
4994 +                                "<no package>\nPlease move this file out of the way and try again.\n");
4995 +              }
4996 +              clashes++;
4997 +         }
4998 +         free(root_filename);
4999 +     }
5000 +     pkg_free_installed_files(pkg);
5001 +
5002 +     return clashes;
5003 +}
5004 +
5005 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5006 +{
5007 +    /* Basically that's the worst hack I could do to be able to change ownership of
5008 +       file list, but, being that we have no way to unwind the mods, due to structure
5009 +       of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
5010 +       What we do here is change the ownership of file in hash if a replace ( or similar events
5011 +       happens )
5012 +       Only the action that are needed to change name should be considered.
5013 +       @@@ To change after 1.0 release.
5014 +     */
5015 +     str_list_t *files_list;
5016 +     str_list_elt_t *iter;
5017 +
5018 +     int clashes = 0;
5019 +
5020 +     files_list = pkg_get_installed_files(pkg);
5021 +     for (iter = files_list->head; iter; iter = iter->next) {
5022 +         char *root_filename;
5023 +         char *filename = iter->data;
5024 +         root_filename = root_filename_alloc(conf, filename);
5025 +         if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
5026 +              pkg_t *owner;
5027 +
5028 +              if (conf->force_overwrite) {
5029 +                   /* but we need to change who owns this file */
5030 +                   file_hash_set_file_owner(conf, filename, pkg);
5031 +                   continue;
5032 +              }
5033 +
5034 +              owner = file_hash_get_file_owner(conf, filename);
5035 +
5036 +              /* Pre-existing files are OK if owned by a package replaced by new pkg. */
5037 +              if (owner) {
5038 +                   if (pkg_replaces(pkg, owner)) {
5039 +/* It's now time to change the owner of that file. 
5040 +   It has been "replaced" from the new "Replaces", then I need to inform lists file about that.  */
5041 +                        ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
5042 +                        file_hash_set_file_owner(conf, filename, pkg);
5043 +                        continue;
5044 +                   }
5045 +              }
5046 +
5047 +         }
5048 +         free(root_filename);
5049 +     }
5050 +     pkg_free_installed_files(pkg);
5051 +
5052 +     return clashes;
5053 +}
5054 +
5055 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5056 +{
5057 +     /* Nothing to do since check_data_file_clashes doesn't change state */
5058 +     return 0;
5059 +}
5060 +
5061 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5062 +{
5063 +     /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
5064 +       1. If the package is being upgraded, call
5065 +          old-postrm upgrade new-version
5066 +       2. If this fails, attempt:
5067 +          new-postrm failed-upgrade old-version
5068 +       Error unwind, for both cases:
5069 +          old-preinst abort-upgrade new-version    */
5070 +     return 0;
5071 +}
5072 +
5073 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5074 +{
5075 +     /* DPKG_INCOMPATIBILITY:
5076 +       dpkg does some things here that we don't do yet. Do we care?
5077 +       (See postrm_upgrade_old_pkg for details)
5078 +     */
5079 +    return 0;
5080 +}
5081 +
5082 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5083 +{
5084 +     int err;
5085 +     str_list_t *old_files;
5086 +     str_list_elt_t *of;
5087 +     str_list_t *new_files;
5088 +     str_list_elt_t *nf;
5089 +
5090 +     if (old_pkg == NULL) {
5091 +         return 0;
5092 +     }
5093 +
5094 +     old_files = pkg_get_installed_files(old_pkg);
5095 +     new_files = pkg_get_installed_files(pkg);
5096 +
5097 +     for (of = old_files->head; of; of = of->next) {
5098 +         pkg_t *owner;
5099 +         char *old, *new;
5100 +         old = of->data;
5101 +         for (nf = new_files->head; nf; nf = nf->next) {
5102 +              new = nf->data;
5103 +              if (strcmp(old, new) == 0) {
5104 +                   goto NOT_OBSOLETE;
5105 +              }
5106 +         }
5107 +         if (file_is_dir(old)) {
5108 +              continue;
5109 +         }
5110 +         owner = file_hash_get_file_owner(conf, old);
5111 +         if (owner != old_pkg) {
5112 +              /* in case obsolete file no longer belongs to old_pkg */
5113 +              continue;
5114 +         }
5115
5116 +         /* old file is obsolete */
5117 +         ipkg_message(conf, IPKG_INFO,
5118 +                      "    removing obsolete file %s\n", old);
5119 +         if (!conf->noaction) {
5120 +              err = unlink(old);
5121 +              if (err) {
5122 +                   ipkg_message(conf, IPKG_ERROR, "    Warning: remove %s failed: %s\n", old,
5123 +                                strerror(errno));
5124 +              }
5125 +         }
5126 +
5127 +     NOT_OBSOLETE:
5128 +         ;
5129 +     }
5130 +
5131 +     pkg_free_installed_files(old_pkg);
5132 +     pkg_free_installed_files(pkg);
5133 +
5134 +     return 0;
5135 +}
5136 +
5137 +static int remove_obsolete_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5138 +{
5139 +     int i;
5140 +     int err = 0;
5141 +     char *globpattern;
5142 +     glob_t globbuf;
5143 +     if (0) {
5144 +         if (!pkg->dest) {
5145 +              ipkg_message(conf, IPKG_ERROR, "%s: no dest for package %s\n", __FUNCTION__, pkg->name);
5146 +              return -1;
5147 +         }
5148 +         sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name);
5149 +         err = glob(globpattern, 0, NULL, &globbuf);
5150 +         free(globpattern);
5151 +         if (err) {
5152 +              return err;
5153 +         }
5154 +         /* XXXX this should perhaps only remove the ones that are not overwritten in new package.  Jamey 11/11/2003 */
5155 +         for (i = 0; i < globbuf.gl_pathc; i++) {
5156 +              ipkg_message(conf, IPKG_DEBUG, "Removing control file %s from old_pkg %s\n",
5157 +                           globbuf.gl_pathv[i], old_pkg->name);
5158 +              if (!conf->noaction)
5159 +                   unlink(globbuf.gl_pathv[i]);
5160 +         }
5161 +         globfree(&globbuf);
5162 +     }
5163 +     return err;
5164 +}
5165 +
5166 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5167 +{
5168 +     int ret;
5169 +     char *prefix;
5170 +
5171 +     if (old_pkg)
5172 +         remove_obsolete_maintainer_scripts(conf, pkg, old_pkg);
5173 +     sprintf_alloc(&prefix, "%s.", pkg->name);
5174 +     ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
5175 +                                                       pkg->dest->info_dir,
5176 +                                                       prefix);
5177 +     free(prefix);
5178 +     return ret;
5179 +}
5180 +
5181 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg)
5182 +{
5183 +     /* DPKG_INCOMPATIBILITY:
5184 +       This is a fairly sophisticated dpkg operation. Shall we
5185 +       skip it? */
5186 +     
5187 +     /* Any packages all of whose files have been overwritten during the
5188 +       installation, and which aren't required for dependencies, are
5189 +       considered to have been removed. For each such package
5190 +       1. disappearer's-postrm disappear overwriter overwriter-version
5191 +       2. The package's maintainer scripts are removed
5192 +       3. It is noted in the status database as being in a sane state,
5193 +           namely not installed (any conffiles it may have are ignored,
5194 +          rather than being removed by dpkg). Note that disappearing
5195 +          packages do not have their prerm called, because dpkg doesn't
5196 +          know in advance that the package is going to vanish.
5197 +     */
5198 +     return 0;
5199 +}
5200 +
5201 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg)
5202 +{
5203 +     int err;
5204 +
5205 +     /* ipkg takes a slightly different approach to data file backups
5206 +       than dpkg. Rather than removing backups at this point, we
5207 +       actually do the data file installation now. See comments in
5208 +       check_data_file_clashes() for more details. */
5209 +    
5210 +     ipkg_message(conf, IPKG_INFO,
5211 +                 "    extracting data files to %s\n", pkg->dest->root_dir);
5212 +     err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
5213 +     if (err) {
5214 +         return err;
5215 +     }
5216 +
5217 +     /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
5218 +        so we can't save ourself from removing important packages
5219 +        At this point we (should) have extracted the .control file, so it
5220 +        would be a good idea to reload the data in it, and set the Essential 
5221 +        state in *pkg. From now on the Essential is back in status file and
5222 +        we can protect again.
5223 +        We should operate this way:
5224 +        fopen the file ( pkg->dest->root_dir/pkg->name.control )
5225 +        check for "Essential" in it 
5226 +        set the value in pkg->essential.
5227 +        This new routine could be useful also for every other flag
5228 +        Pigi: 16/03/2004 */
5229 +     set_flags_from_control(conf, pkg) ;
5230 +     
5231 +     ipkg_message(conf, IPKG_DEBUG, "    Calling pkg_write_filelist from %s\n", __FUNCTION__);
5232 +     err = pkg_write_filelist(conf, pkg);
5233 +     if (err)
5234 +         return err;
5235 +
5236 +     /* XXX: FEATURE: ipkg should identify any files which existed
5237 +       before installation and which were overwritten, (see
5238 +       check_data_file_clashes()). What it must do is remove any such
5239 +       files from the filelist of the old package which provided the
5240 +       file. Otherwise, if the old package were removed at some point
5241 +       it would break the new package. Removing the new package will
5242 +       also break the old one, but this cannot be helped since the old
5243 +       package's file has already been deleted. This is the importance
5244 +       of check_data_file_clashes(), and only allowing ipkg to install
5245 +       a clashing package with a user force. */
5246 +
5247 +     return 0;
5248 +}
5249 +
5250 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg)
5251 +{
5252 +     conffile_list_elt_t *iter;
5253 +     conffile_t *cf;
5254 +     char *cf_backup;
5255 +
5256 +    char *md5sum;
5257 +
5258 +    
5259 +     if (conf->noaction) return 0;
5260 +
5261 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
5262 +         char *root_filename;
5263 +         cf = iter->data;
5264 +         root_filename = root_filename_alloc(conf, cf->name);
5265 +
5266 +         /* Might need to initialize the md5sum for each conffile */
5267 +         if (cf->value == NULL) {
5268 +              cf->value = file_md5sum_alloc(root_filename);
5269 +         }
5270 +
5271 +         if (!file_exists(root_filename)) {
5272 +              free(root_filename);
5273 +              continue;
5274 +         }
5275 +
5276 +         cf_backup = backup_filename_alloc(root_filename);
5277 +
5278 +
5279 +         if (file_exists(cf_backup)) {
5280 + /* Let's compute md5 to test if files are changed */
5281 +             md5sum = file_md5sum_alloc(cf_backup);
5282 +               if (strcmp( cf->value,md5sum) != 0 ) {
5283 +                 if (conf->force_defaults
5284 +                     || user_prefers_old_conffile(cf->name, cf_backup) ) {
5285 +                      rename(cf_backup, root_filename);
5286 +                 }
5287 +              }
5288 +              unlink(cf_backup);
5289 +              free(md5sum);
5290 +         }
5291 +
5292 +         free(cf_backup);
5293 +         free(root_filename);
5294 +     }
5295 +
5296 +     return 0;
5297 +}
5298 +
5299 +static int user_prefers_old_conffile(const char *file_name, const char *backup)
5300 +{
5301 +     char *response;
5302 +     const char *short_file_name;
5303 +
5304 +     short_file_name = strrchr(file_name, '/');
5305 +     if (short_file_name) {
5306 +         short_file_name++;
5307 +     } else {
5308 +         short_file_name = file_name;
5309 +     }
5310 +
5311 +     while (1) {
5312 +         response = get_user_response("    Configuration file '%s'\n"
5313 +                                      "    ==> File on system created by you or by a script.\n"
5314 +                                      "    ==> File also in package provided by package maintainer.\n"
5315 +                                      "       What would you like to do about it ?  Your options are:\n"
5316 +                                      "        Y or I  : install the package maintainer's version\n"
5317 +                                      "        N or O  : keep your currently-installed version\n"
5318 +                                      "          D     : show the differences between the versions (if diff is installed)\n"
5319 +                                      "     The default action is to keep your current version.\n"
5320 +                                      "    *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
5321 +         if (strcmp(response, "y") == 0
5322 +             || strcmp(response, "i") == 0
5323 +             || strcmp(response, "yes") == 0) {
5324 +              free(response);
5325 +              return 0;
5326 +         }
5327 +
5328 +         if (strcmp(response, "d") == 0) {
5329 +              char *cmd;
5330 +
5331 +              free(response);
5332 +              /* XXX: BUG rewrite to use exec or busybox's internal diff */
5333 +              sprintf_alloc(&cmd, "diff -u %s %s", backup, file_name);
5334 +              xsystem(cmd);
5335 +              free(cmd);
5336 +              printf("    [Press ENTER to continue]\n");
5337 +              response = file_read_line_alloc(stdin);
5338 +              free(response);
5339 +              continue;
5340 +         }
5341 +
5342 +         free(response);
5343 +         return 1;
5344 +     }
5345 +}
5346 +
5347 +/* XXX: CLEANUP: I'd like to move all of the code for
5348 +   creating/cleaning pkg->tmp_unpack_dir directly into pkg.c. (Then,
5349 +   it would make sense to cleanup pkg->tmp_unpack_dir directly from
5350 +   pkg_deinit for example). */
5351 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg)
5352 +{
5353 +     DIR *tmp_dir;
5354 +     struct dirent *dirent;
5355 +     char *tmp_file;
5356 +
5357 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
5358 +#error
5359 +     ipkg_message(conf, IPKG_DEBUG,
5360 +                 "%s: Not cleaning up %s since ipkg compiled with IPKG_DEBUG_NO_TMP_CLEANUP\n",
5361 +                 __FUNCTION__, pkg->tmp_unpack_dir);
5362 +     return 0;
5363 +#endif
5364 +
5365 +     if (pkg->tmp_unpack_dir && file_is_dir(pkg->tmp_unpack_dir)) {
5366 +         tmp_dir = opendir(pkg->tmp_unpack_dir);
5367 +         if (tmp_dir) {
5368 +              while (1) {
5369 +                   dirent = readdir(tmp_dir);
5370 +                   if (dirent == NULL) {
5371 +                        break;
5372 +                   }
5373 +                   sprintf_alloc(&tmp_file, "%s/%s",
5374 +                                 pkg->tmp_unpack_dir, dirent->d_name);
5375 +                   if (! file_is_dir(tmp_file)) {
5376 +                        unlink(tmp_file);
5377 +                   }
5378 +                   free(tmp_file);
5379 +              }
5380 +              closedir(tmp_dir);
5381 +              rmdir(pkg->tmp_unpack_dir);
5382 +              free(pkg->tmp_unpack_dir);
5383 +              pkg->tmp_unpack_dir = NULL;
5384 +         }
5385 +     }
5386 +
5387 +     ipkg_message(conf, IPKG_INFO, "cleanup_temporary_files: pkg=%s local_filename=%s tmp_dir=%s\n",
5388 +                 pkg->name, pkg->local_filename, conf->tmp_dir);
5389 +     if (pkg->local_filename && strncmp(pkg->local_filename, conf->tmp_dir, strlen(conf->tmp_dir)) == 0) {
5390 +         unlink(pkg->local_filename);
5391 +         free(pkg->local_filename);
5392 +         pkg->local_filename = NULL;
5393 +     }
5394 +
5395 +     return 0;
5396 +}
5397 +
5398 +static char *backup_filename_alloc(const char *file_name)
5399 +{
5400 +     char *backup;
5401 +
5402 +     sprintf_alloc(&backup, "%s%s", file_name, IPKG_BACKUP_SUFFIX);
5403 +
5404 +     return backup;
5405 +}
5406 +
5407 +int backup_make_backup(ipkg_conf_t *conf, const char *file_name)
5408 +{
5409 +     int err;
5410 +     char *backup;
5411 +    
5412 +     backup = backup_filename_alloc(file_name);
5413 +     err = file_copy(file_name, backup);
5414 +     if (err) {
5415 +         ipkg_message(conf, IPKG_ERROR,
5416 +                      "%s: Failed to copy %s to %s\n",
5417 +                      __FUNCTION__, file_name, backup);
5418 +     }
5419 +
5420 +     free(backup);
5421 +
5422 +     return err;
5423 +}
5424 +
5425 +static int backup_exists_for(const char *file_name)
5426 +{
5427 +     int ret;
5428 +     char *backup;
5429 +
5430 +     backup = backup_filename_alloc(file_name);
5431 +
5432 +     ret = file_exists(backup);
5433 +
5434 +     free(backup);
5435 +
5436 +     return ret;
5437 +}
5438 +
5439 +static int backup_remove(const char *file_name)
5440 +{
5441 +     char *backup;
5442 +
5443 +     backup = backup_filename_alloc(file_name);
5444 +     unlink(backup);
5445 +     free(backup);
5446 +
5447 +     return 0;
5448 +}
5449 +
5450 +\f
5451 +
5452 +#ifdef CONFIG_IPKG_PROCESS_ACTIONS
5453 +
5454 +int ipkg_remove_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove) 
5455 +{
5456 +     /* first, remove the packages that need removing */
5457 +     for (i = 0 ; i < pkgs_to_remove->len; i++ ) {
5458 +         pkg_t *pkg = pkgs_to_remove->pkgs[i];
5459 +         err = ipkg_remove_pkg(conf, pkg,0);
5460 +         if (err) return err;
5461 +     }
5462 +     return 0;
5463 +}
5464 +
5465 +int ipkg_process_actions_sanity_check(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_superseded, pkg_vec_t *pkgs_to_install)
5466 +{
5467 +     int i;
5468 +     /* now one more pass checking on the ones that need to be installed */
5469 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5470 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5471 +         if (pkg->dest == NULL)
5472 +              pkg->dest = conf->default_dest;
5473 +
5474 +         pkg->state_want = SW_INSTALL;
5475 +
5476 +         /* Abhaya: conflicts check */
5477 +         err = check_conflicts_for(conf, pkg);
5478 +         if (err) { return err; }
5479 +     }
5480 +     return 0;
5481 +}
5482 +
5483 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5484 +{
5485 +     int i;
5486 +     /* now one more pass checking on the ones that need to be installed */
5487 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5488 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5489 +
5490 +         /* XXX: FEATURE: Need to really support Provides/Replaces: here at some point */
5491 +         pkg_vec_t *replacees = pkg_vec_alloc();
5492 +         pkg_get_installed_replacees(conf, pkg, replacees);
5493 +
5494 +         /* XXX: BUG: we really should treat replacement more like an upgrade
5495 +          *      Instead, we're going to remove the replacees 
5496 +          */
5497 +         err = pkg_remove_installed_replacees(conf, replacees);
5498 +         if (err) return err;
5499 +         pkg->state_flag |= SF_REMOVED_REPLACEES;
5500 +     }
5501 +     return 0;
5502 +}
5503 +
5504 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5505 +{
5506 +     int i;
5507 +     /* now one more pass checking on the ones that need to be installed */
5508 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5509 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5510 +         if (pkg->local_filename == NULL) {
5511 +              err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
5512 +              if (err) {
5513 +                   ipkg_message(conf, IPKG_ERROR,
5514 +                                "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
5515 +                                pkg->name);
5516 +                   return err;
5517 +              }
5518 +         }
5519 +         if (pkg->tmp_unpack_dir == NULL) {
5520 +              err = unpack_pkg_control_files(conf, pkg);
5521 +              if (err) return err;
5522 +         }
5523 +     }
5524 +     return 0;
5525 +}
5526 +
5527 +int ipkg_process_actions_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5528 +{
5529 +     int i;
5530 +     /* now one more pass checking on the ones that need to be installed */
5531 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5532 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5533 +         pkg_t *old_pkg = pkg->old_pkg;
5534 +
5535 +         err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
5536 +         if (err) return err;
5537 +
5538 +         err = prerm_deconfigure_conflictors(conf, pkg, replacees);
5539 +         if (err) return err;
5540 +
5541 +         err = preinst_configure(conf, pkg, old_pkg);
5542 +         if (err) return err;
5543 +
5544 +         err = backup_modified_conffiles(conf, pkg, old_pkg);
5545 +         if (err) return err;
5546 +
5547 +         err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
5548 +         if (err) return err;
5549 +     }
5550 +     return 0;
5551 +}
5552 +
5553 +int ipkg_process_actions_install(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5554 +{
5555 +     int i;
5556 +     /* now one more pass checking on the ones that need to be installed */
5557 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5558 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5559 +         pkg_t *old_pkg = pkg->old_pkg;
5560 +
5561 +         if (old_pkg) {
5562 +              old_pkg->state_want = SW_DEINSTALL;
5563 +
5564 +              if (old_pkg->state_flag & SF_NOPRUNE) {
5565 +                   ipkg_message(conf, IPKG_INFO,
5566 +                                "  not removing obsolesced files because package marked noprune\n");
5567 +              } else {
5568 +                   ipkg_message(conf, IPKG_INFO,
5569 +                                "  removing obsolesced files\n");
5570 +                   remove_obsolesced_files(conf, pkg, old_pkg);
5571 +              }
5572 +         }
5573 +
5574 +         ipkg_message(conf, IPKG_INFO,
5575 +                      "  installing maintainer scripts\n");
5576 +         install_maintainer_scripts(conf, pkg, old_pkg);
5577 +
5578 +         /* the following just returns 0 */
5579 +         remove_disappeared(conf, pkg);
5580 +
5581 +         ipkg_message(conf, IPKG_INFO,
5582 +                      "  installing data files\n");
5583 +         install_data_files(conf, pkg);
5584 +
5585 +         ipkg_message(conf, IPKG_INFO,
5586 +                      "  resolving conf files\n");
5587 +         resolve_conffiles(conf, pkg);
5588 +
5589 +         pkg->state_status = SS_UNPACKED;
5590 +
5591 +         if (old_pkg) {
5592 +              old_pkg->state_status = SS_NOT_INSTALLED;
5593 +         }
5594 +
5595 +         time(&pkg->installed_time);
5596 +
5597 +         ipkg_message(conf, IPKG_INFO,
5598 +                      "  cleanup temp files\n");
5599 +         cleanup_temporary_files(conf, pkg);
5600 +
5601 +         if (pkg->parent)
5602 +              pkg->parent->state_status = pkg->state_status;
5603 +     }
5604 +     return 0;
5605 +}
5606 +
5607 +int ipkg_process_actions_unwind_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5608 +{
5609 +     int i;
5610 +     /* now one more pass checking on the ones that need to be installed */
5611 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5612 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5613 +         pkg_t *old_pkg = pkg->old_pkg;
5614 +
5615 +         if (old_pkg) {
5616 +              if (old_pkg->state_flags & SF_POSTRM_UPGRADE)
5617 +                   postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5618 +              if (old_pkg->state_flags & SF_CHECK_DATA_FILE_CLASHES)
5619 +                   check_data_file_clashes_unwind(conf, pkg, old_pkg);
5620 +              if (old_pkg->state_flags & SF_BACKUP_MODIFIED_CONFFILES)
5621 +                   backup_modified_conffiles_unwind(conf, pkg, old_pkg);
5622 +              if (old_pkg->state_flags & SF_PREINST_CONFIGURE)
5623 +                   preinst_configure_unwind(conf, pkg, old_pkg);
5624 +              if (old_pkg->state_flags & SF_DECONFIGURE_CONFLICTORS)
5625 +                   prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
5626 +              if (old_pkg->state_flags & SF_PRERM_UPGRADE)
5627 +                   prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5628 +
5629 +              if (old_pkg->state_flags & SF_REMOVED_REPLACEES)
5630 +                   remove_installed_replacees_unwind(conf, pkg, old_pkg);
5631 +
5632 +         }
5633 +     }
5634 +     return 0;
5635 +}
5636 +
5637 +/* 
5638 + * Perform all the actions.
5639 + *
5640 + * pkgs_to_remove are packages marked for removal.
5641 + * pkgs_superseded are the old packages being replaced by upgrades.
5642 + *
5643 + * Assumes pkgs_to_install includes all dependences, recursively, sorted in installable order.
5644 + */
5645 +int ipkg_process_actions(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_superseded, pkg_vec_t *pkgs_to_install)
5646 +{
5647 +     int err;
5648 +     int i;
5649 +
5650 +     err = ipkg_remove_packages(conf, pkgs_to_remove);
5651 +     if (err) return err;
5652 +
5653 +     err = ipkg_process_actions_sanity_check(conf, pkgs_superseded, pkgs_to_install);
5654 +     if (err) return err;
5655 +
5656 +     err = ipkg_process_actions_remove_replacees(conf, pkgs_to_install);
5657 +     if (err) goto UNWIND;
5658 +
5659 +     /* @@@@ look at ipkg_install_pkg for handling replacements */
5660 +     err = ipkg_process_actions_unpack_packages(conf, pkgs_to_install);
5661 +     if (err) goto UNWIND;
5662 +
5663 +     /* 
5664 +      * Now that we have the packages unpacked, we can look for data
5665 +      * file clashes.  First, we mark the files from the superseded
5666 +      * packages as obsolete.  Then we scan the files in
5667 +      * pkgs_to_install, and only complain about clashes with
5668 +      * non-obsolete files.
5669 +      */
5670 +
5671 +     err = ipkg_process_actions_check_data_file_clashes(conf, pkgs_superseded, pkgs_to_install);
5672 +     if (err) goto UNWIND;
5673 +
5674 +     /* this was before checking data file clashes */
5675 +     err = ipkg_process_actions_prerm(conf, pkgs_superseded, pkgs_to_install);
5676 +     if (err) goto UNWIND;
5677 +
5678 +     /* point of no return: no unwinding after this */
5679 +     err = ipkg_process_actions_install(conf, pkgs_to_install);
5680 +     if (err) return err;
5681 +
5682 +     ipkg_message(conf, IPKG_INFO, "Done.\n");
5683 +     return 0;
5684 +
5685 + UNWIND:
5686 +     ipkg_process_actions_unwind(conf, pkgs_to_install);
5687 +
5688 +     ipkg_message(conf, IPKG_INFO,
5689 +                 "  cleanup temp files\n");
5690 +     cleanup_temporary_files(conf, pkg);
5691 +
5692 +     ipkg_message(conf, IPKG_INFO,
5693 +                 "Failed.\n");
5694 +     return err;
5695 +}
5696 +
5697 +#endif
5698 diff -urN busybox.old/archival/libipkg/ipkg_install.h busybox.dev/archival/libipkg/ipkg_install.h
5699 --- busybox.old/archival/libipkg/ipkg_install.h 1970-01-01 01:00:00.000000000 +0100
5700 +++ busybox.dev/archival/libipkg/ipkg_install.h 2007-01-22 13:41:03.000000000 +0100
5701 @@ -0,0 +1,35 @@
5702 +/* ipkg_install.h - the itsy package management system
5703 +
5704 +   Carl D. Worth
5705 +
5706 +   Copyright (C) 2001 University of Southern California
5707 +
5708 +   This program is free software; you can redistribute it and/or
5709 +   modify it under the terms of the GNU General Public License as
5710 +   published by the Free Software Foundation; either version 2, or (at
5711 +   your option) any later version.
5712 +
5713 +   This program is distributed in the hope that it will be useful, but
5714 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5715 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5716 +   General Public License for more details.
5717 +*/
5718 +
5719 +#ifndef IPKG_INSTALL_H
5720 +#define IPKG_INSTALL_H
5721 +
5722 +#include "pkg.h"
5723 +#include "ipkg_conf.h"
5724 +
5725 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name);
5726 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name);
5727 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename);
5728 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg,int from_upgrading);
5729 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
5730 +
5731 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf);
5732 +
5733 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg_name, pkg_vec_t *pkgs_needed);
5734 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed);
5735 +
5736 +#endif
5737 diff -urN busybox.old/archival/libipkg/ipkg_message.c busybox.dev/archival/libipkg/ipkg_message.c
5738 --- busybox.old/archival/libipkg/ipkg_message.c 1970-01-01 01:00:00.000000000 +0100
5739 +++ busybox.dev/archival/libipkg/ipkg_message.c 2007-01-22 13:41:03.000000000 +0100
5740 @@ -0,0 +1,61 @@
5741 +/* ipkg_message.c - the itsy package management system
5742 +
5743 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5744 +
5745 +   This program is free software; you can redistribute it and/or
5746 +   modify it under the terms of the GNU General Public License as
5747 +   published by the Free Software Foundation; either version 2, or (at
5748 +   your option) any later version.
5749 +
5750 +   This program is distributed in the hope that it will be useful, but
5751 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5752 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5753 +   General Public License for more details.
5754 +*/
5755 +
5756 +
5757 +#include "ipkg.h"
5758 +#include "ipkg_conf.h"
5759 +#include "ipkg_message.h"
5760 +
5761 +#ifndef IPKG_LIB
5762 +
5763 +void
5764 +ipkg_message (ipkg_conf_t * conf, message_level_t level, char *fmt, ...)
5765 +{
5766 +       va_list ap;
5767 +
5768 +       if (conf && (conf->verbosity < level))
5769 +       {
5770 +               return;
5771 +       }
5772 +       else
5773 +       {
5774 +
5775 +               va_start (ap, fmt);
5776 +               vprintf (fmt, ap);
5777 +               va_end (ap);
5778 +       }
5779 +}
5780 +
5781 +#else
5782 +
5783 +#include "libipkg.h"
5784 +
5785 +//#define ipkg_message(conf, level, fmt, arg...) ipkg_cb_message(conf, level, fmt, ## arg)
5786 +
5787 +void
5788 +ipkg_message (ipkg_conf_t * conf, message_level_t level, char *fmt, ...)
5789 +{
5790 +       va_list ap;
5791 +       char ts[256];
5792 +
5793 +       if (ipkg_cb_message)
5794 +       {
5795 +               va_start (ap, fmt);
5796 +               vsnprintf (ts,256,fmt, ap);
5797 +               va_end (ap);
5798 +               ipkg_cb_message(conf,level,ts);
5799 +       }
5800 +}
5801 +#endif
5802 diff -urN busybox.old/archival/libipkg/ipkg_message.h busybox.dev/archival/libipkg/ipkg_message.h
5803 --- busybox.old/archival/libipkg/ipkg_message.h 1970-01-01 01:00:00.000000000 +0100
5804 +++ busybox.dev/archival/libipkg/ipkg_message.h 2007-01-22 13:41:03.000000000 +0100
5805 @@ -0,0 +1,32 @@
5806 +/* ipkg_message.h - the itsy package management system
5807 +
5808 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5809 +
5810 +   This program is free software; you can redistribute it and/or
5811 +   modify it under the terms of the GNU General Public License as
5812 +   published by the Free Software Foundation; either version 2, or (at
5813 +   your option) any later version.
5814 +
5815 +   This program is distributed in the hope that it will be useful, but
5816 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5817 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5818 +   General Public License for more details.
5819 +*/
5820 +
5821 +#ifndef _IPKG_MESSAGE_H_
5822 +#define _IPKG_MESSAGE_H_
5823 +
5824 +#include "ipkg.h"
5825 +#include "ipkg_conf.h"
5826 +
5827 +typedef enum {
5828 +     IPKG_ERROR,       /* error conditions */
5829 +     IPKG_NOTICE,      /* normal but significant condition */
5830 +     IPKG_INFO,                /* informational message */
5831 +     IPKG_DEBUG,       /* debug level message */
5832 +     IPKG_DEBUG2,      /* more debug level message */
5833 +} message_level_t;
5834 +
5835 +extern void ipkg_message(ipkg_conf_t *conf, message_level_t level, char *fmt, ...);
5836 +
5837 +#endif /* _IPKG_MESSAGE_H_ */
5838 diff -urN busybox.old/archival/libipkg/ipkg_remove.c busybox.dev/archival/libipkg/ipkg_remove.c
5839 --- busybox.old/archival/libipkg/ipkg_remove.c  1970-01-01 01:00:00.000000000 +0100
5840 +++ busybox.dev/archival/libipkg/ipkg_remove.c  2007-01-22 13:41:03.000000000 +0100
5841 @@ -0,0 +1,383 @@
5842 +/* ipkg_remove.c - the itsy package management system
5843 +
5844 +   Carl D. Worth
5845 +
5846 +   Copyright (C) 2001 University of Southern California
5847 +
5848 +   This program is free software; you can redistribute it and/or
5849 +   modify it under the terms of the GNU General Public License as
5850 +   published by the Free Software Foundation; either version 2, or (at
5851 +   your option) any later version.
5852 +
5853 +   This program is distributed in the hope that it will be useful, but
5854 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5855 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5856 +   General Public License for more details.
5857 +*/
5858 +
5859 +#include "ipkg.h"
5860 +#include "ipkg_message.h"
5861 +
5862 +#include <glob.h>
5863 +
5864 +#include "ipkg_remove.h"
5865 +
5866 +#include "file_util.h"
5867 +#include "sprintf_alloc.h"
5868 +#include "str_util.h"
5869 +
5870 +#include "ipkg_cmd.h"
5871 +
5872 +/*
5873 + * Returns number of the number of packages depending on the packages provided by this package.
5874 + * Every package implicitly provides itself.
5875 + */
5876 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
5877 +{
5878 +     int nprovides = pkg->provides_count;
5879 +     abstract_pkg_t **provides = pkg->provides;
5880 +     int n_installed_dependents = 0;
5881 +     int i;
5882 +     for (i = 0; i <= nprovides; i++) {
5883 +         abstract_pkg_t *providee = provides[i];
5884 +         abstract_pkg_t **dependers = providee->depended_upon_by;
5885 +         abstract_pkg_t *dep_ab_pkg;
5886 +         if (dependers == NULL)
5887 +              continue;
5888 +         while ((dep_ab_pkg = *dependers++) != NULL) {
5889 +              if (dep_ab_pkg->state_status == SS_INSTALLED){
5890 +                   n_installed_dependents++;
5891 +               }
5892 +         }
5893 +
5894 +     }
5895 +     /* if caller requested the set of installed dependents */
5896 +     if (pdependents) {
5897 +         int p = 0;
5898 +         abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
5899 +
5900 +          if ( dependents == NULL ){
5901 +              fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
5902 +              return -1;  
5903 +          }
5904 +
5905 +         *pdependents = dependents;
5906 +         for (i = 0; i <= nprovides; i++) {
5907 +              abstract_pkg_t *providee = provides[i];
5908 +              abstract_pkg_t **dependers = providee->depended_upon_by;
5909 +              abstract_pkg_t *dep_ab_pkg;
5910 +              if (dependers == NULL)
5911 +                   continue;
5912 +              while ((dep_ab_pkg = *dependers++) != NULL) {
5913 +                   if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
5914 +                        dependents[p++] = dep_ab_pkg;
5915 +                        dep_ab_pkg->state_flag |= SF_MARKED;
5916 +                   }
5917 +              }
5918 +         }
5919 +         dependents[p] = NULL;
5920 +         /* now clear the marks */
5921 +         for (i = 0; i < p; i++) {
5922 +              abstract_pkg_t *dep_ab_pkg = dependents[i];
5923 +              dep_ab_pkg->state_flag &= ~SF_MARKED;
5924 +         }
5925 +     }
5926 +     return n_installed_dependents;
5927 +}
5928 +
5929 +int ipkg_remove_dependent_pkgs (ipkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
5930 +{
5931 +    int i;
5932 +    int a;
5933 +    int count;
5934 +    pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
5935 +    abstract_pkg_t * ab_pkg;
5936 +
5937 +    if((ab_pkg = pkg->parent) == NULL){
5938 +       fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
5939 +               __FUNCTION__, pkg->name);
5940 +       return 0;
5941 +    }
5942 +    
5943 +    if (dependents == NULL)
5944 +           return 0;
5945 +
5946 +    // here i am using the dependencies_checked
5947 +    if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
5948 +       return 0;                          // has already been encountered in the process
5949 +                                          // of marking packages for removal - Karthik
5950 +    ab_pkg->dependencies_checked = 2;
5951 +
5952 +    i = 0;
5953 +    count = 1;
5954 +    while (dependents [i] != NULL) {
5955 +        abstract_pkg_t *dep_ab_pkg = dependents[i];
5956 +       
5957 +       if (dep_ab_pkg->dependencies_checked == 2){
5958 +           i++;
5959 +           continue;   
5960 +        }
5961 +        if (dep_ab_pkg->state_status == SS_INSTALLED) {
5962 +            for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
5963 +                pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
5964 +                if (dep_pkg->state_status == SS_INSTALLED) {
5965 +                    pkg_vec_insert(dependent_pkgs, dep_pkg);
5966 +                    count++;
5967 +                }
5968 +            }
5969 +        }
5970 +       i++;
5971 +       /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
5972 +        * 2 - to keep track of pkgs whose deps have been checked alrdy  - Karthik */   
5973 +    }
5974 +    
5975 +    if (count == 1)
5976 +           return 0;
5977 +    
5978 +    
5979 +    for (i = 0; i < dependent_pkgs->len; i++) {
5980 +        int err = ipkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
5981 +        if (err)
5982 +            return err;
5983 +    }
5984 +    return 0;
5985 +}
5986 +
5987 +static int user_prefers_removing_dependents(ipkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
5988 +{
5989 +    abstract_pkg_t *dep_ab_pkg;
5990 +    ipkg_message(conf, IPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
5991 +    while ((dep_ab_pkg = *dependents++) != NULL) {
5992 +        if (dep_ab_pkg->state_status == SS_INSTALLED)
5993 +             ipkg_message(conf, IPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
5994 +    }
5995 +    ipkg_message(conf, IPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
5996 +    ipkg_message(conf, IPKG_ERROR, "");
5997 +    ipkg_message(conf, IPKG_ERROR, "You can force removal of this package with -force-depends.\n");
5998 +    ipkg_message(conf, IPKG_ERROR, "You can force removal of this package and its dependents\n");
5999 +    ipkg_message(conf, IPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
6000 +    ipkg_message(conf, IPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
6001 +    ipkg_message(conf, IPKG_ERROR, "in ipkg.conf.\n");
6002 +    return 0;
6003 +}
6004 +
6005 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message)
6006 +{
6007 +/* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
6008 +   thus I wan't check for essential, as I'm upgrading.
6009 +   I hope it won't break anything :) 
6010 +*/
6011 +     int err;
6012 +     abstract_pkg_t *parent_pkg = NULL;
6013 +       
6014 +     if (pkg->essential && !message) {
6015 +         if (conf->force_removal_of_essential_packages) {
6016 +              fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
6017 +                      "\tIf your system breaks, you get to keep both pieces\n",
6018 +                      pkg->name);
6019 +         } else {
6020 +              fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
6021 +                      "\tRemoving an essential package may lead to an unusable system, but if\n"
6022 +                      "\tyou enjoy that kind of pain, you can force ipkg to proceed against\n"
6023 +                      "\tits will with the option: -force-removal-of-essential-packages\n",
6024 +                      pkg->name);
6025 +              return IPKG_PKG_IS_ESSENTIAL;
6026 +         }
6027 +     }
6028 +
6029 +     if ((parent_pkg = pkg->parent) == NULL)
6030 +         return 0;
6031 +
6032 +     /* only attempt to remove dependent installed packages if
6033 +      * force_depends is not specified or the package is being
6034 +      * replaced.
6035 +      */
6036 +     if (!conf->force_depends
6037 +        && !(pkg->state_flag & SF_REPLACE)) {
6038 +         abstract_pkg_t **dependents;
6039 +         int has_installed_dependents = 
6040 +              pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
6041 +
6042 +         if (has_installed_dependents) {
6043 +              /*
6044 +               * if this package is depended up by others, then either we should
6045 +               * not remove it or we should remove it and all of its dependents 
6046 +               */
6047 +
6048 +              if (!conf->force_removal_of_dependent_packages
6049 +                  && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
6050 +                   return IPKG_PKG_HAS_DEPENDENTS;
6051 +              }
6052 +
6053 +              /* remove packages depending on this package - Karthik */
6054 +              err = ipkg_remove_dependent_pkgs (conf, pkg, dependents);
6055 +              free(dependents);
6056 +              if (err) return err;
6057 +         }
6058 +     }
6059 +
6060 +     if ( message==0 ){
6061 +         printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
6062 +         fflush(stdout);
6063 +     }
6064 +     pkg->state_flag |= SF_FILELIST_CHANGED;
6065 +
6066 +     pkg->state_want = SW_DEINSTALL;
6067 +     ipkg_state_changed++;
6068 +
6069 +     pkg_run_script(conf, pkg, "prerm", "remove");
6070 +
6071 +     /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
6072 +       maintains an empty filelist rather than deleting it. That seems
6073 +       like a big pain, and I don't see that that should make a big
6074 +       difference, but for anyone who wants tighter compatibility,
6075 +       feel free to fix this. */
6076 +     remove_data_files_and_list(conf, pkg);
6077 +
6078 +     pkg_run_script(conf, pkg, "postrm", "remove");
6079 +
6080 +     remove_maintainer_scripts_except_postrm(conf, pkg);
6081 +
6082 +     /* Aman Gupta - Since ipkg is made for handheld devices with limited
6083 +      * space, it doesn't make sense to leave extra configurations, files, 
6084 +      * and maintainer scripts left around. So, we make remove like purge, 
6085 +      * and take out all the crap :) */
6086 +
6087 +     remove_postrm(conf, pkg);
6088 +     pkg->state_status = SS_NOT_INSTALLED;
6089 +
6090 +     if (parent_pkg) 
6091 +         parent_pkg->state_status = SS_NOT_INSTALLED;
6092 +
6093 +     return 0;
6094 +}
6095 +
6096 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg)
6097 +{
6098 +    ipkg_remove_pkg(conf, pkg,0);
6099 +    return 0;
6100 +}
6101 +
6102 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg)
6103 +{
6104 +     str_list_t installed_dirs;
6105 +     str_list_t *installed_files;
6106 +     str_list_elt_t *iter;
6107 +     char *file_name;
6108 +     conffile_t *conffile;
6109 +     int removed_a_dir;
6110 +     pkg_t *owner;
6111 +
6112 +     str_list_init(&installed_dirs);
6113 +     installed_files = pkg_get_installed_files(pkg);
6114 +
6115 +     for (iter = installed_files->head; iter; iter = iter->next) {
6116 +         file_name = iter->data;
6117 +
6118 +         if (file_is_dir(file_name)) {
6119 +              str_list_append(&installed_dirs, strdup(file_name));
6120 +              continue;
6121 +         }
6122 +
6123 +         conffile = pkg_get_conffile(pkg, file_name);
6124 +         if (conffile) {
6125 +              /* XXX: QUESTION: Is this right? I figure we only need to
6126 +                 save the conffile if it has been modified. Is that what
6127 +                 dpkg does? Or does dpkg preserve all conffiles? If so,
6128 +                 this seems like a better thing to do to conserve
6129 +                 space. */
6130 +              if (conffile_has_been_modified(conf, conffile)) {
6131 +                   printf("  not deleting modified conffile %s\n", file_name);
6132 +                   fflush(stdout);
6133 +                   continue;
6134 +              }
6135 +         }
6136 +
6137 +         ipkg_message(conf, IPKG_INFO, "  deleting %s (noaction=%d)\n", file_name, conf->noaction);
6138 +         if (!conf->noaction)
6139 +              unlink(file_name);
6140 +     }
6141 +
6142 +     if (!conf->noaction) {
6143 +         do {
6144 +              removed_a_dir = 0;
6145 +              for (iter = installed_dirs.head; iter; iter = iter->next) {
6146 +                   file_name = iter->data;
6147 +           
6148 +                   if (rmdir(file_name) == 0) {
6149 +                        ipkg_message(conf, IPKG_INFO, "  deleting %s\n", file_name);
6150 +                        removed_a_dir = 1;
6151 +                        str_list_remove(&installed_dirs, &iter);
6152 +                   }
6153 +              }
6154 +         } while (removed_a_dir);
6155 +     }
6156 +
6157 +     pkg_free_installed_files(pkg);
6158 +     /* We have to remove the file list now, so that
6159 +       find_pkg_owning_file does not always just report this package */
6160 +     pkg_remove_installed_files_list(conf, pkg);
6161 +
6162 +     /* Don't print warning for dirs that are provided by other packages */
6163 +     for (iter = installed_dirs.head; iter; iter = iter->next) {
6164 +         file_name = iter->data;
6165 +
6166 +         owner = file_hash_get_file_owner(conf, file_name);
6167 +         if (owner) {
6168 +              free(iter->data);
6169 +              iter->data = NULL;
6170 +              str_list_remove(&installed_dirs, &iter);
6171 +         }
6172 +     }
6173 +
6174 +     /* cleanup */
6175 +     for (iter = installed_dirs.head; iter; iter = iter->next) {
6176 +         free(iter->data);
6177 +         iter->data = NULL;
6178 +     }
6179 +     str_list_deinit(&installed_dirs);
6180 +
6181 +     return 0;
6182 +}
6183 +
6184 +int remove_maintainer_scripts_except_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6185 +{
6186 +    int i, err;
6187 +    char *globpattern;
6188 +    glob_t globbuf;
6189 +    
6190 +    if (conf->noaction) return 0;
6191 +
6192 +    sprintf_alloc(&globpattern, "%s/%s.*",
6193 +                 pkg->dest->info_dir, pkg->name);
6194 +    err = glob(globpattern, 0, NULL, &globbuf);
6195 +    free(globpattern);
6196 +    if (err) {
6197 +       return 0;
6198 +    }
6199 +
6200 +    for (i = 0; i < globbuf.gl_pathc; i++) {
6201 +       if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
6202 +           continue;
6203 +       }
6204 +        ipkg_message(conf, IPKG_INFO, "  deleting %s\n", globbuf.gl_pathv[i]);
6205 +       unlink(globbuf.gl_pathv[i]);
6206 +    }
6207 +    globfree(&globbuf);
6208 +
6209 +    return 0;
6210 +}
6211 +
6212 +int remove_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6213 +{
6214 +    char *postrm_file_name;
6215 +
6216 +    if (conf->noaction) return 0;
6217 +
6218 +    sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
6219 +                 pkg->dest->info_dir, pkg->name);
6220 +    unlink(postrm_file_name);
6221 +    free(postrm_file_name);
6222 +
6223 +    return 0;
6224 +}
6225 diff -urN busybox.old/archival/libipkg/ipkg_remove.h busybox.dev/archival/libipkg/ipkg_remove.h
6226 --- busybox.old/archival/libipkg/ipkg_remove.h  1970-01-01 01:00:00.000000000 +0100
6227 +++ busybox.dev/archival/libipkg/ipkg_remove.h  2007-01-22 13:41:03.000000000 +0100
6228 @@ -0,0 +1,33 @@
6229 +/* ipkg_remove.h - the itsy package management system
6230 +
6231 +   Carl D. Worth
6232 +
6233 +   Copyright (C) 2001 University of Southern California
6234 +
6235 +   This program is free software; you can redistribute it and/or
6236 +   modify it under the terms of the GNU General Public License as
6237 +   published by the Free Software Foundation; either version 2, or (at
6238 +   your option) any later version.
6239 +
6240 +   This program is distributed in the hope that it will be useful, but
6241 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6242 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6243 +   General Public License for more details.
6244 +*/
6245 +
6246 +#ifndef IPKG_REMOVE_H
6247 +#define IPKG_REMOVE_H
6248 +
6249 +#include "pkg.h"
6250 +#include "ipkg_conf.h"
6251 +
6252 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message);
6253 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg);
6254 +int possible_broken_removal_of_packages (ipkg_conf_t *conf, pkg_t *pkg);
6255 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents);
6256 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg);
6257 +int remove_maintainer_scripts_except_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6258 +int remove_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6259 +
6260 +
6261 +#endif
6262 diff -urN busybox.old/archival/libipkg/ipkg_upgrade.c busybox.dev/archival/libipkg/ipkg_upgrade.c
6263 --- busybox.old/archival/libipkg/ipkg_upgrade.c 1970-01-01 01:00:00.000000000 +0100
6264 +++ busybox.dev/archival/libipkg/ipkg_upgrade.c 2007-01-22 13:41:03.000000000 +0100
6265 @@ -0,0 +1,77 @@
6266 +/* ipkg_upgrade.c - the itsy package management system
6267 +
6268 +   Carl D. Worth
6269 +   Copyright (C) 2001 University of Southern California
6270 +
6271 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6272 +
6273 +   This program is free software; you can redistribute it and/or
6274 +   modify it under the terms of the GNU General Public License as
6275 +   published by the Free Software Foundation; either version 2, or (at
6276 +   your option) any later version.
6277 +
6278 +   This program is distributed in the hope that it will be useful, but
6279 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6280 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6281 +   General Public License for more details.
6282 +*/
6283 +
6284 +#include "ipkg.h"
6285 +#include "ipkg_install.h"
6286 +#include "ipkg_message.h"
6287 +
6288 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old)
6289 +{
6290 +     pkg_t *new;
6291 +     int cmp;
6292 +     char *old_version, *new_version;
6293 +
6294 +     if (old->state_flag & SF_HOLD) {
6295 +          ipkg_message(conf, IPKG_NOTICE,
6296 +                       "Not upgrading package %s which is marked "
6297 +                       "hold (flags=%#x)\n", old->name, old->state_flag);
6298 +          return 0;
6299 +     }
6300 +
6301 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name);
6302 +     if (new == NULL) {
6303 +          old_version = pkg_version_str_alloc(old);
6304 +          ipkg_message(conf, IPKG_NOTICE,
6305 +                       "Assuming locally installed package %s (%s) "
6306 +                       "is up to date.\n", old->name, old_version);
6307 +          free(old_version);
6308 +          return 0;
6309 +     }
6310 +          
6311 +     old_version = pkg_version_str_alloc(old);
6312 +     new_version = pkg_version_str_alloc(new);
6313 +               
6314 +     cmp = pkg_compare_versions(old, new);
6315 +     ipkg_message(conf, IPKG_DEBUG,
6316 +                  "comparing visible versions of pkg %s:"
6317 +                  "\n\t%s is installed "
6318 +                  "\n\t%s is available "
6319 +                  "\n\t%d was comparison result\n",
6320 +                  old->name, old_version, new_version, cmp);
6321 +     if (cmp == 0) {
6322 +          ipkg_message(conf, IPKG_INFO,
6323 +                       "Package %s (%s) installed in %s is up to date.\n",
6324 +                       old->name, old_version, old->dest->name);
6325 +          free(old_version);
6326 +          free(new_version);
6327 +          return 0;
6328 +     } else if (cmp > 0) {
6329 +          ipkg_message(conf, IPKG_NOTICE,
6330 +                       "Not downgrading package %s on %s from %s to %s.\n",
6331 +                       old->name, old->dest->name, old_version, new_version);
6332 +          free(old_version);
6333 +          free(new_version);
6334 +          return 0;
6335 +     } else if (cmp < 0) {
6336 +          new->dest = old->dest;
6337 +          old->state_want = SW_DEINSTALL;
6338 +     }
6339 +
6340 +     new->state_flag |= SF_USER;
6341 +     return ipkg_install_pkg(conf, new,1);
6342 +}
6343 diff -urN busybox.old/archival/libipkg/ipkg_upgrade.h busybox.dev/archival/libipkg/ipkg_upgrade.h
6344 --- busybox.old/archival/libipkg/ipkg_upgrade.h 1970-01-01 01:00:00.000000000 +0100
6345 +++ busybox.dev/archival/libipkg/ipkg_upgrade.h 2007-01-22 13:41:03.000000000 +0100
6346 @@ -0,0 +1,18 @@
6347 +/* ipkg_upgrade.c - the itsy package management system
6348 +
6349 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6350 +
6351 +   This program is free software; you can redistribute it and/or
6352 +   modify it under the terms of the GNU General Public License as
6353 +   published by the Free Software Foundation; either version 2, or (at
6354 +   your option) any later version.
6355 +
6356 +   This program is distributed in the hope that it will be useful, but
6357 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6358 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6359 +   General Public License for more details.
6360 +*/
6361 +
6362 +#include "ipkg.h"
6363 +
6364 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old);
6365 diff -urN busybox.old/archival/libipkg/ipkg_utils.c busybox.dev/archival/libipkg/ipkg_utils.c
6366 --- busybox.old/archival/libipkg/ipkg_utils.c   1970-01-01 01:00:00.000000000 +0100
6367 +++ busybox.dev/archival/libipkg/ipkg_utils.c   2007-01-22 13:41:03.000000000 +0100
6368 @@ -0,0 +1,181 @@
6369 +/* ipkg_utils.c - the itsy package management system
6370 +
6371 +   Steven M. Ayer
6372 +   
6373 +   Copyright (C) 2002 Compaq Computer Corporation
6374 +
6375 +   This program is free software; you can redistribute it and/or
6376 +   modify it under the terms of the GNU General Public License as
6377 +   published by the Free Software Foundation; either version 2, or (at
6378 +   your option) any later version.
6379 +
6380 +   This program is distributed in the hope that it will be useful, but
6381 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6382 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6383 +   General Public License for more details.
6384 +*/
6385 +
6386 +#include "ipkg.h"
6387 +#include <errno.h>
6388 +#include <ctype.h>
6389 +#include <sys/vfs.h>
6390 +
6391 +#include "ipkg_utils.h"
6392 +#include "pkg.h"
6393 +#include "pkg_hash.h"
6394 +
6395 +struct errlist* error_list;
6396 +
6397 +int get_available_blocks(char * filesystem)
6398 +{
6399 +     struct statfs sfs;
6400 +
6401 +     if(statfs(filesystem, &sfs)){
6402 +         fprintf(stderr, "bad statfs\n");
6403 +         return 0;
6404 +     }
6405 +     /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
6406 +     return ((sfs.f_bavail * sfs.f_bsize) / 1024);
6407 +}
6408 +
6409 +char **read_raw_pkgs_from_file(const char *file_name)
6410 +{
6411 +     FILE *fp; 
6412 +     char **ret;
6413 +    
6414 +     if(!(fp = fopen(file_name, "r"))){
6415 +         fprintf(stderr, "can't get %s open for read\n", file_name);
6416 +         return NULL;
6417 +     }
6418 +
6419 +     ret = read_raw_pkgs_from_stream(fp);
6420 +
6421 +     fclose(fp);
6422 +
6423 +     return ret;
6424 +}
6425 +
6426 +char **read_raw_pkgs_from_stream(FILE *fp)
6427 +{    
6428 +     char **raw = NULL, *buf, *scout;
6429 +     int count = 0;
6430 +     size_t size = 512;
6431 +     
6432 +     buf = malloc (size);
6433 +
6434 +     while (fgets(buf, size, fp)) {
6435 +         while (strlen (buf) == (size - 1)
6436 +                && buf[size-2] != '\n') {
6437 +              size_t o = size - 1;
6438 +              size *= 2;
6439 +              buf = realloc (buf, size);
6440 +              if (fgets (buf + o, size - o, fp) == NULL)
6441 +                   break;
6442 +         }
6443 +         
6444 +         if(!(count % 50))
6445 +              raw = realloc(raw, (count + 50) * sizeof(char *));
6446 +       
6447 +         if((scout = strchr(buf, '\n')))
6448 +              *scout = '\0';
6449 +
6450 +         raw[count++] = strdup(buf);
6451 +     }
6452 +    
6453 +     raw = realloc(raw, (count + 1) * sizeof(char *));
6454 +     raw[count] = NULL;
6455 +
6456 +     free (buf);
6457 +    
6458 +     return raw;
6459 +}
6460 +
6461 +/* something to remove whitespace, a hash pooper */
6462 +char *trim_alloc(char *line)
6463 +{
6464 +     char *new; 
6465 +     char *dest, *src, *end;
6466 +    
6467 +     new = malloc(strlen(line) + 1);
6468 +     if ( new == NULL ){
6469 +        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
6470 +        return NULL;
6471 +     }
6472 +     dest = new, src = line, end = line + (strlen(line) - 1);
6473 +
6474 +     /* remove it from the front */    
6475 +     while(src && 
6476 +          isspace(*src) &&
6477 +          *src)
6478 +         src++;
6479 +     /* and now from the back */
6480 +     while((end > src) &&
6481 +          isspace(*end))
6482 +         end--;
6483 +     end++;
6484 +     *end = '\0';
6485 +     strcpy(new, src);
6486 +     /* this does from the first space
6487 +      *  blasting away any versions stuff in depends
6488 +      while(src && 
6489 +      !isspace(*src) &&
6490 +      *src)
6491 +      *dest++ = *src++;
6492 +      *dest = '\0';
6493 +      */
6494 +    
6495 +     return new;
6496 +}
6497 +
6498 +int line_is_blank(const char *line)
6499 +{
6500 +     const char *s;
6501 +
6502 +     for (s = line; *s; s++) {
6503 +         if (!isspace(*s))
6504 +              return 0;
6505 +     }
6506 +     return 1;
6507 +}
6508 +
6509 +void push_error_list(struct errlist ** errors, char * msg){
6510 +  struct errlist *err_lst_tmp;
6511 +
6512 +
6513 +  err_lst_tmp = malloc ( sizeof (err_lst_tmp) );
6514 +  err_lst_tmp->errmsg=strdup(msg) ;
6515 +  err_lst_tmp->next = *errors;
6516 +  *errors = err_lst_tmp;
6517 +}
6518 +
6519 +
6520 +void reverse_error_list(struct errlist **errors){
6521 +   struct errlist *result=NULL;
6522 +   struct errlist *current= *errors;
6523 +   struct errlist *next;
6524 +
6525 +   while ( current != NULL ) {
6526 +      next = current->next;
6527 +      current->next=result;
6528 +      result=current;
6529 +      current=next;
6530 +   }
6531 +   *errors=result;
6532 +
6533 +}
6534 +
6535 +       
6536 +void free_error_list(struct errlist **errors){
6537 +  struct errlist *current = *errors;
6538 +
6539 +    while (current != NULL) {
6540 +      free(current->errmsg);
6541 +      current = (*errors)->next;
6542 +      free(*errors);
6543 +      *errors = current;
6544 +    }
6545 +
6546 +
6547 +}
6548 +
6549 +       
6550 diff -urN busybox.old/archival/libipkg/ipkg_utils.h busybox.dev/archival/libipkg/ipkg_utils.h
6551 --- busybox.old/archival/libipkg/ipkg_utils.h   1970-01-01 01:00:00.000000000 +0100
6552 +++ busybox.dev/archival/libipkg/ipkg_utils.h   2007-01-22 13:41:03.000000000 +0100
6553 @@ -0,0 +1,29 @@
6554 +/* ipkg_utils.h - the itsy package management system
6555 +
6556 +   Steven M. Ayer
6557 +   
6558 +   Copyright (C) 2002 Compaq Computer Corporation
6559 +
6560 +   This program is free software; you can redistribute it and/or
6561 +   modify it under the terms of the GNU General Public License as
6562 +   published by the Free Software Foundation; either version 2, or (at
6563 +   your option) any later version.
6564 +
6565 +   This program is distributed in the hope that it will be useful, but
6566 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6567 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6568 +   General Public License for more details.
6569 +*/
6570 +
6571 +#ifndef IPKG_UTILS_H
6572 +#define IPKG_UTILS_H
6573 +
6574 +#include "pkg.h"
6575 +
6576 +int get_available_blocks(char * filesystem);
6577 +char **read_raw_pkgs_from_file(const char *file_name);
6578 +char **read_raw_pkgs_from_stream(FILE *fp);
6579 +char *trim_alloc(char * line);
6580 +int line_is_blank(const char *line);
6581 +
6582 +#endif
6583 diff -urN busybox.old/archival/libipkg/Kbuild busybox.dev/archival/libipkg/Kbuild
6584 --- busybox.old/archival/libipkg/Kbuild 1970-01-01 01:00:00.000000000 +0100
6585 +++ busybox.dev/archival/libipkg/Kbuild 2007-01-22 14:01:06.000000000 +0100
6586 @@ -0,0 +1,60 @@
6587 +# Makefile for busybox
6588 +#
6589 +# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6590 +# Copyright (C) 2006 OpenWrt.org
6591 +#
6592 +# Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6593 +
6594 +LIBIPKG_CORE_OBJS:= \
6595 +       args.o \
6596 +       libipkg.o \
6597 +       user.o \
6598 +
6599 +LIBIPKG_CMD_OBJS:= \
6600 +       ipkg_cmd.o \
6601 +       ipkg_configure.o \
6602 +       ipkg_download.o \
6603 +       ipkg_install.o \
6604 +       ipkg_remove.o \
6605 +       ipkg_upgrade.o \
6606 +
6607 +LIBIPKG_DB_OBJS:= \
6608 +       hash_table.o \
6609 +       ipkg_conf.o \
6610 +       ipkg_utils.o \
6611 +       pkg.o \
6612 +       pkg_depends.o \
6613 +       pkg_extract.o \
6614 +       pkg_hash.o \
6615 +       pkg_parse.o \
6616 +       pkg_vec.o \
6617 +
6618 +LIBIPKG_LIST_OBJS:= \
6619 +       conffile.o \
6620 +       conffile_list.o \
6621 +       nv_pair.o \
6622 +       nv_pair_list.o \
6623 +       pkg_dest.o \
6624 +       pkg_dest_list.o \
6625 +       pkg_src.o \
6626 +       pkg_src_list.o \
6627 +       str_list.o \
6628 +       void_list.o \
6629 +
6630 +LIBIPKG_UTIL_OBJS:= \
6631 +       file_util.o \
6632 +       ipkg_message.o \
6633 +       str_util.o \
6634 +       xsystem.o \
6635 +
6636 +lib-y :=
6637 +lib-$(CONFIG_IPKG) += $(LIBIPKG_CORE_OBJS)
6638 +lib-$(CONFIG_IPKG) += $(LIBIPKG_CMD_OBJS)
6639 +lib-$(CONFIG_IPKG) += $(LIBIPKG_DB_OBJS)
6640 +lib-$(CONFIG_IPKG) += $(LIBIPKG_LIST_OBJS)
6641 +lib-$(CONFIG_IPKG) += $(LIBIPKG_UTIL_OBJS)
6642 +
6643 +ifeq ($(strip $(IPKG_ARCH)),)
6644 +IPKG_ARCH:=$(TARGET_ARCH)
6645 +endif
6646 +CFLAGS += -DIPKG_LIB -DIPKGLIBDIR="\"/usr/lib\"" -DHOST_CPU_STR="\"$(IPKG_ARCH)\""
6647 diff -urN busybox.old/archival/libipkg/libipkg.c busybox.dev/archival/libipkg/libipkg.c
6648 --- busybox.old/archival/libipkg/libipkg.c      1970-01-01 01:00:00.000000000 +0100
6649 +++ busybox.dev/archival/libipkg/libipkg.c      2007-01-22 13:41:06.000000000 +0100
6650 @@ -0,0 +1,527 @@
6651 +/* ipkglib.c - the itsy package management system
6652 +
6653 +   Florina Boor
6654 +
6655 +   Copyright (C) 2003 kernel concepts
6656 +
6657 +   This program is free software; you can redistribute it and/or
6658 +   modify it under the terms of the GNU General Public License as
6659 +   published by the Free Software Foundation; either version 2, or (at
6660 +   your option) any later version.
6661 +
6662 +   This program is distributed in the hope that it will be useful, but
6663 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6664 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6665 +   General Public License for more details.
6666 +*/
6667 +
6668 +#ifdef IPKG_LIB
6669 +
6670 +#include "ipkg.h"
6671 +#include "ipkg_includes.h"
6672 +#include "libipkg.h"
6673 +
6674 +#include "args.h"
6675 +#include "ipkg_conf.h"
6676 +#include "ipkg_cmd.h"
6677 +#include "file_util.h"
6678 +
6679 +
6680 +
6681 +ipkg_message_callback ipkg_cb_message = NULL;
6682 +ipkg_response_callback ipkg_cb_response = NULL;
6683 +ipkg_status_callback ipkg_cb_status = NULL;
6684 +ipkg_list_callback ipkg_cb_list = NULL;
6685 +
6686 +
6687 +int
6688 +ipkg_init (ipkg_message_callback mcall, 
6689 +           ipkg_response_callback rcall,
6690 +           args_t * args)
6691 +{
6692 +       ipkg_cb_message = mcall;
6693 +       ipkg_cb_response = rcall;
6694 +
6695 +       args_init (args);
6696 +
6697 +       return 0;
6698 +}
6699 +
6700 +
6701 +int
6702 +ipkg_deinit (args_t * args)
6703 +{
6704 +       args_deinit (args);
6705 +       ipkg_cb_message = NULL;
6706 +       ipkg_cb_response = NULL;
6707 +
6708 +       /* place other cleanup stuff here */
6709 +
6710 +       return 0;
6711 +}
6712 +
6713 +
6714 +int
6715 +ipkg_packages_list(args_t *args, 
6716 +                   const char *packages, 
6717 +                   ipkg_list_callback cblist,
6718 +                   void *userdata)
6719 +{
6720 +       ipkg_cmd_t *cmd;
6721 +       ipkg_conf_t ipkg_conf;
6722 +       int err;
6723 +
6724 +       err = ipkg_conf_init (&ipkg_conf, args);
6725 +       if (err)
6726 +       {
6727 +               return err;
6728 +       }
6729 +
6730 +       ipkg_cb_list = cblist;
6731 +       /* we need to do this because of static declarations, 
6732 +        * maybe a good idea to change */
6733 +       cmd = ipkg_cmd_find ("list");
6734 +       if (packages)
6735 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6736 +       else
6737 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6738 +       ipkg_cb_list = NULL;
6739 +       ipkg_conf_deinit (&ipkg_conf);
6740 +       return (err);
6741 +}
6742 +
6743 +
6744 +int
6745 +ipkg_packages_status(args_t *args,
6746 +                     const char *packages,
6747 +                     ipkg_status_callback cbstatus,
6748 +                     void *userdata)
6749 +{
6750 +       ipkg_cmd_t *cmd;
6751 +       ipkg_conf_t ipkg_conf;
6752 +       int err;
6753 +
6754 +       err = ipkg_conf_init (&ipkg_conf, args);
6755 +       if (err)
6756 +       {
6757 +               return err;
6758 +       }
6759 +
6760 +       ipkg_cb_status = cbstatus;
6761 +
6762 +       /* we need to do this because of static declarations,
6763 +        * maybe a good idea to change */
6764 +       cmd = ipkg_cmd_find ("status");
6765 +       if (packages)
6766 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6767 +       else
6768 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6769 +
6770 +       ipkg_cb_status = NULL;
6771 +       ipkg_conf_deinit (&ipkg_conf);
6772 +       return (err);
6773 +}
6774 +
6775 +
6776 +int
6777 +ipkg_packages_info(args_t *args,
6778 +                   const char *packages,
6779 +                   ipkg_status_callback cbstatus,
6780 +                   void *userdata)
6781 +{
6782 +       ipkg_cmd_t *cmd;
6783 +       ipkg_conf_t ipkg_conf;
6784 +       int err;
6785 +
6786 +       err = ipkg_conf_init (&ipkg_conf, args);
6787 +       if (err)
6788 +       {
6789 +               return err;
6790 +       }
6791 +
6792 +       ipkg_cb_status = cbstatus;
6793 +
6794 +       /* we need to do this because of static declarations,
6795 +        * maybe a good idea to change */
6796 +       cmd = ipkg_cmd_find ("info");
6797 +       if (packages)
6798 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6799 +       else
6800 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6801 +
6802 +       ipkg_cb_status = NULL;
6803 +       ipkg_conf_deinit (&ipkg_conf);
6804 +       return (err);
6805 +}
6806 +
6807 +
6808 +int
6809 +ipkg_packages_install (args_t * args, const char *name)
6810 +{
6811 +       ipkg_cmd_t *cmd;
6812 +       ipkg_conf_t ipkg_conf;
6813 +       int err;
6814 +
6815 +       /* this error should be handled in application */
6816 +       if (!name || !strlen (name))
6817 +               return (-1);
6818 +
6819 +       err = ipkg_conf_init (&ipkg_conf, args);
6820 +       if (err)
6821 +       {
6822 +               return err;
6823 +       }
6824 +
6825 +       /* we need to do this because of static declarations,
6826 +        * maybe a good idea to change */
6827 +       cmd = ipkg_cmd_find ("install");
6828 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6829 +
6830 +       ipkg_conf_deinit(&ipkg_conf);
6831 +       return (err);
6832 +}
6833 +
6834 +
6835 +int
6836 +ipkg_packages_remove(args_t *args, const char *name, int purge)
6837 +{
6838 +       ipkg_cmd_t *cmd;
6839 +       ipkg_conf_t ipkg_conf;
6840 +       int err;
6841 +
6842 +       /* this error should be handled in application */
6843 +       if (!name || !strlen (name))
6844 +               return (-1);
6845 +
6846 +       err = ipkg_conf_init (&ipkg_conf, args);
6847 +       if (err)
6848 +       {
6849 +               return err;
6850 +       }
6851 +
6852 +       /* we need to do this because of static declarations, 
6853 +        * maybe a good idea to change */
6854 +       if (purge)
6855 +               cmd = ipkg_cmd_find ("purge");
6856 +       else
6857 +               cmd = ipkg_cmd_find ("remove");
6858 +
6859 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6860 +       
6861 +       ipkg_conf_deinit(&ipkg_conf);
6862 +       return (err);
6863 +}
6864 +
6865 +
6866 +int 
6867 +ipkg_lists_update(args_t *args)
6868 +{
6869 +       ipkg_cmd_t *cmd;
6870 +       ipkg_conf_t ipkg_conf;
6871 +       int err;
6872 +
6873 +       err = ipkg_conf_init (&ipkg_conf, args);
6874 +       if (err)
6875 +       {
6876 +               return err;
6877 +       }
6878 +
6879 +       /* we need to do this because of static declarations, 
6880 +        * maybe a good idea to change */
6881 +       cmd = ipkg_cmd_find ("update");
6882 +
6883 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6884 +       
6885 +       ipkg_conf_deinit(&ipkg_conf);
6886 +       return (err);
6887 +}
6888 +
6889 +
6890 +int 
6891 +ipkg_packages_upgrade(args_t *args)
6892 +{
6893 +       ipkg_cmd_t *cmd;
6894 +       ipkg_conf_t ipkg_conf;
6895 +       int err;
6896 +
6897 +       err = ipkg_conf_init (&ipkg_conf, args);
6898 +       if (err)
6899 +       {
6900 +               return err;
6901 +       }
6902 +
6903 +       /* we need to do this because of static declarations, 
6904 +        * maybe a good idea to change */
6905 +       cmd = ipkg_cmd_find ("upgrade");
6906 +
6907 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6908 +       
6909 +       ipkg_conf_deinit(&ipkg_conf);
6910 +       return (err);
6911 +}
6912 +
6913 +
6914 +int
6915 +ipkg_packages_download (args_t * args, const char *name)
6916 +{
6917 +       ipkg_cmd_t *cmd;
6918 +       ipkg_conf_t ipkg_conf;
6919 +       int err;
6920 +
6921 +       /* this error should be handled in application */
6922 +       if (!name || !strlen (name))
6923 +               return (-1);
6924 +
6925 +       err = ipkg_conf_init (&ipkg_conf, args);
6926 +       if (err)
6927 +       {
6928 +               return err;
6929 +       }
6930 +
6931 +       /* we need to do this because of static declarations,
6932 +        * maybe a good idea to change */
6933 +       cmd = ipkg_cmd_find ("download");
6934 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6935 +
6936 +       ipkg_conf_deinit(&ipkg_conf);
6937 +       return (err);
6938 +}
6939 +
6940 +
6941 +int
6942 +ipkg_package_files(args_t *args, 
6943 +                   const char *name, 
6944 +                   ipkg_list_callback cblist,
6945 +                   void *userdata)
6946 +{
6947 +       ipkg_cmd_t *cmd;
6948 +       ipkg_conf_t ipkg_conf;
6949 +       int err;
6950 +
6951 +       /* this error should be handled in application */
6952 +       if (!name || !strlen (name))
6953 +               return (-1);
6954 +
6955 +       err = ipkg_conf_init (&ipkg_conf, args);
6956 +       if (err)
6957 +       {
6958 +               return err;
6959 +       }
6960 +
6961 +       ipkg_cb_list = cblist;
6962 +       
6963 +       /* we need to do this because of static declarations, 
6964 +        * maybe a good idea to change */
6965 +       cmd = ipkg_cmd_find ("files");
6966 +
6967 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, userdata);
6968 +       
6969 +       ipkg_cb_list = NULL;
6970 +       ipkg_conf_deinit(&ipkg_conf);
6971 +       return (err);
6972 +}
6973 +
6974 +
6975 +int 
6976 +ipkg_file_search(args_t *args, 
6977 +                const char *file,
6978 +                               ipkg_list_callback cblist,
6979 +                void *userdata)
6980 +{
6981 +       ipkg_cmd_t *cmd;
6982 +       ipkg_conf_t ipkg_conf;
6983 +       int err;
6984 +       
6985 +       /* this error should be handled in application */
6986 +       if (!file || !strlen (file))
6987 +               return (-1);
6988 +
6989 +       err = ipkg_conf_init (&ipkg_conf, args);
6990 +       if (err)
6991 +       {
6992 +               return err;
6993 +       }
6994 +
6995 +       ipkg_cb_list = cblist;
6996 +
6997 +       /* we need to do this because of static declarations, 
6998 +        * maybe a good idea to change */
6999 +       cmd = ipkg_cmd_find ("search");
7000 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, userdata);
7001 +       
7002 +       ipkg_cb_list = NULL;
7003 +       ipkg_conf_deinit(&ipkg_conf);
7004 +       return(err);
7005 +}
7006 +
7007 +
7008 +int 
7009 +ipkg_file_what(args_t *args, const char *file, const char* command)
7010 +{
7011 +       ipkg_cmd_t *cmd;
7012 +       ipkg_conf_t ipkg_conf;
7013 +       int err;
7014 +       
7015 +       /* this error should be handled in application */
7016 +       if (!file || !strlen (file))
7017 +               return (-1);
7018 +
7019 +       err = ipkg_conf_init (&ipkg_conf, args);
7020 +       if (err)
7021 +       {
7022 +               return err;
7023 +       }
7024 +
7025 +       /* we need to do this because of static declarations, 
7026 +        * maybe a good idea to change */
7027 +       cmd = ipkg_cmd_find (command);
7028 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, NULL);
7029 +       
7030 +       ipkg_conf_deinit(&ipkg_conf);
7031 +       return(err);
7032 +}
7033 +
7034 +#define ipkg_package_whatdepends(args,file) ipkg_file_what(args,file,"whatdepends")
7035 +#define ipkg_package_whatrecommends(args, file) ipkg_file_what(args,file,"whatrecommends")
7036 +#define ipkg_package_whatprovides(args, file) ipkg_file_what(args,file,"whatprovides")
7037 +#define ipkg_package_whatconflicts(args, file) ipkg_file_what(args,file,"whatconflicts")
7038 +#define ipkg_package_whatreplaces(args, file) ipkg_file_what(args,file,"whatreplaces")
7039 +
7040 +
7041 +int default_ipkg_message_callback(ipkg_conf_t *conf, message_level_t level, 
7042 +                                 char *msg)
7043 +{
7044 +     if (conf && (conf->verbosity < level)) {
7045 +         return 0;
7046 +     } else {
7047 +#ifdef IPKG_LIB
7048 +          if ( level == IPKG_ERROR ){
7049 +             push_error_list(&error_list, msg); 
7050 +//          printf(msg);
7051 +          } else
7052 +#endif
7053 +            printf(msg);
7054 +     }
7055 +     return 0;
7056 +}
7057 +
7058 +int default_ipkg_list_callback(char *name, char *desc, char *version, 
7059 +                              pkg_state_status_t status, void *userdata)
7060 +{
7061 +     if (desc)
7062 +         printf("%s - %s - %s\n", name, version, desc);
7063 +     else
7064 +         printf("%s - %s\n", name, version);
7065 +     return 0;
7066 +}
7067 +
7068 +int default_ipkg_files_callback(char *name, char *desc, char *version,
7069 +                   pkg_state_status_t status, void *userdata)
7070 +{
7071 +     if (desc)
7072 +         printf("%s\n", desc);
7073 +     return 0;
7074 +}
7075 +
7076 +int default_ipkg_status_callback(char *name, int istatus, char *desc,
7077 +                                void *userdata)
7078 +{
7079 +     printf("%s\n", desc);
7080 +     return 0;
7081 +}
7082 +
7083 +char* default_ipkg_response_callback(char *question)
7084 +{
7085 +     char *response = NULL;
7086 +     printf(question);
7087 +     fflush(stdout);
7088 +     do {
7089 +         response = (char *)file_read_line_alloc(stdin);
7090 +     } while (response == NULL);
7091 +     return response;
7092 +}
7093 +
7094 +/* This is used for backward compatibility */
7095 +int
7096 +ipkg_op (int argc, char *argv[])
7097 +{
7098 +       int err, opt_index;
7099 +       args_t args;
7100 +       char *cmd_name;
7101 +       ipkg_cmd_t *cmd;
7102 +       ipkg_conf_t ipkg_conf;
7103 +
7104 +       args_init (&args);
7105 +
7106 +       opt_index = args_parse (&args, argc, argv);
7107 +       if (opt_index == argc || opt_index < 0)
7108 +       {
7109 +               args_usage ("ipkg must have one sub-command argument");
7110 +       }
7111 +
7112 +       cmd_name = argv[opt_index++];
7113 +/* Pigi: added a flag to disable the checking of structures if the command does not need to 
7114 +         read anything from there.
7115 +*/
7116 +        if ( !strcmp(cmd_name,"print-architecture") ||
7117 +             !strcmp(cmd_name,"print_architecture") ||
7118 +             !strcmp(cmd_name,"print-installation-architecture") ||
7119 +             !strcmp(cmd_name,"print_installation_architecture") )
7120 +           args.nocheckfordirorfile = 1;
7121 +
7122 +/* Pigi: added a flag to disable the reading of feed files  if the command does not need to 
7123 +         read anything from there.
7124 +*/
7125 +        if ( !strcmp(cmd_name,"flag") ||
7126 +             !strcmp(cmd_name,"configure") ||
7127 +             !strcmp(cmd_name,"remove") ||
7128 +             !strcmp(cmd_name,"files") ||
7129 +             !strcmp(cmd_name,"search") ||
7130 +             !strcmp(cmd_name,"compare_versions") ||
7131 +             !strcmp(cmd_name,"compare-versions") ||
7132 +             !strcmp(cmd_name,"list_installed") ||
7133 +             !strcmp(cmd_name,"list-installed") ||
7134 +             !strcmp(cmd_name,"status") )
7135 +           args.noreadfeedsfile = 1;
7136 +
7137 +
7138 +       err = ipkg_conf_init (&ipkg_conf, &args);
7139 +       if (err)
7140 +       {
7141 +               return err;
7142 +       }
7143 +
7144 +       args_deinit (&args);
7145 +
7146 +       ipkg_cb_message = default_ipkg_message_callback;
7147 +       ipkg_cb_response = default_ipkg_response_callback;
7148 +       ipkg_cb_status = default_ipkg_status_callback;
7149 +       if ( strcmp(cmd_name, "files")==0)
7150 +            ipkg_cb_list = default_ipkg_files_callback;
7151 +       else
7152 +            ipkg_cb_list = default_ipkg_list_callback;
7153 +
7154 +       cmd = ipkg_cmd_find (cmd_name);
7155 +       if (cmd == NULL)
7156 +       {
7157 +               fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
7158 +                        cmd_name);
7159 +               args_usage (NULL);
7160 +       }
7161 +
7162 +       if (cmd->requires_args && opt_index == argc)
7163 +       {
7164 +               fprintf (stderr,
7165 +                        "%s: the ``%s'' command requires at least one argument\n",
7166 +                        __FUNCTION__, cmd_name);
7167 +               args_usage (NULL);
7168 +       }
7169 +
7170 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, argc - opt_index, (const char **) (argv + opt_index), NULL);
7171 +
7172 +       ipkg_conf_deinit (&ipkg_conf);
7173 +
7174 +       return err;
7175 +}
7176 +
7177 +#endif /* IPKG_LIB */
7178 diff -urN busybox.old/archival/libipkg/libipkg.h busybox.dev/archival/libipkg/libipkg.h
7179 --- busybox.old/archival/libipkg/libipkg.h      1970-01-01 01:00:00.000000000 +0100
7180 +++ busybox.dev/archival/libipkg/libipkg.h      2007-01-22 13:41:03.000000000 +0100
7181 @@ -0,0 +1,87 @@
7182 +/* ipkglib.h - the itsy package management system
7183 +
7184 +   Florian Boor <florian.boor@kernelconcepts.de>
7185 +
7186 +   This program is free software; you can redistribute it and/or
7187 +   modify it under the terms of the GNU General Public License as
7188 +   published by the Free Software Foundation; either version 2, or (at
7189 +   your option) any later version.
7190 +
7191 +   This program is distributed in the hope that it will be useful, but
7192 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7193 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7194 +   General Public License for more details.
7195 +*/
7196 +
7197 +#ifndef IPKGLIB_H
7198 +#define IPKGLIB_H
7199 +
7200 +#ifdef IPKG_LIB
7201 +
7202 +#include "ipkg_conf.h"
7203 +#include "ipkg_message.h"
7204 +
7205 +#include "args.h"
7206 +#include "pkg.h"
7207 +
7208 +typedef int (*ipkg_message_callback)(ipkg_conf_t *conf, message_level_t level, 
7209 +       char *msg);
7210 +typedef int (*ipkg_list_callback)(char *name, char *desc, char *version, 
7211 +       pkg_state_status_t status, void *userdata);
7212 +typedef int (*ipkg_status_callback)(char *name, int istatus, char *desc,
7213 +       void *userdata);
7214 +typedef char* (*ipkg_response_callback)(char *question);
7215 +
7216 +extern int ipkg_op(int argc, char *argv[]); /* ipkglib.c */
7217 +extern int ipkg_init (ipkg_message_callback mcall, 
7218 +                      ipkg_response_callback rcall,
7219 +                                         args_t * args);
7220 +
7221 +extern int ipkg_deinit (args_t *args);
7222 +extern int ipkg_packages_list(args_t *args, 
7223 +                              const char *packages, 
7224 +                              ipkg_list_callback cblist,
7225 +                              void *userdata);
7226 +extern int ipkg_packages_status(args_t *args, 
7227 +                                const char *packages, 
7228 +                                ipkg_status_callback cbstatus,
7229 +                                                               void *userdata);
7230 +extern int ipkg_packages_info(args_t *args,
7231 +                              const char *packages,
7232 +                              ipkg_status_callback cbstatus,
7233 +                              void *userdata);
7234 +extern int ipkg_packages_install(args_t *args, const char *name);
7235 +extern int ipkg_packages_remove(args_t *args, const char *name, int purge);
7236 +extern int ipkg_lists_update(args_t *args);
7237 +extern int ipkg_packages_upgrade(args_t *args);
7238 +extern int ipkg_packages_download(args_t *args, const char *name);
7239 +extern int ipkg_package_files(args_t *args,
7240 +                              const char *name,
7241 +                                                         ipkg_list_callback cblist,
7242 +                                                         void *userdata);
7243 +extern int ipkg_file_search(args_t *args,
7244 +                            const char *file,
7245 +                                                       ipkg_list_callback cblist,
7246 +                                                       void *userdata);
7247 +extern int ipkg_package_whatdepends(args_t *args, const char *file);
7248 +extern int ipkg_package_whatrecommends(args_t *args, const char *file);
7249 +extern int ipkg_package_whatprovides(args_t *args, const char *file);
7250 +extern int ipkg_package_whatconflicts(args_t *args, const char *file);
7251 +extern int ipkg_package_whatreplaces(args_t *args, const char *file);
7252 +
7253 +extern ipkg_message_callback ipkg_cb_message; /* ipkglib.c */
7254 +extern ipkg_response_callback ipkg_cb_response;
7255 +extern ipkg_status_callback ipkg_cb_status;
7256 +extern ipkg_list_callback ipkg_cb_list;
7257 +extern void push_error_list(struct errlist **errors,char * msg);
7258 +extern void reverse_error_list(struct errlist **errors);
7259 +extern void free_error_list(struct errlist **errors);
7260 +
7261 +#else
7262 +
7263 +extern int ipkg_op(int argc, char *argv[]);
7264 +
7265 +#endif
7266 +
7267 +
7268 +#endif
7269 diff -urN busybox.old/archival/libipkg/nv_pair.c busybox.dev/archival/libipkg/nv_pair.c
7270 --- busybox.old/archival/libipkg/nv_pair.c      1970-01-01 01:00:00.000000000 +0100
7271 +++ busybox.dev/archival/libipkg/nv_pair.c      2007-01-22 13:41:03.000000000 +0100
7272 @@ -0,0 +1,40 @@
7273 +/* nv_pair.c - the itsy package management system
7274 +
7275 +   Carl D. Worth
7276 +
7277 +   Copyright (C) 2001 University of Southern California
7278 +
7279 +   This program is free software; you can redistribute it and/or
7280 +   modify it under the terms of the GNU General Public License as
7281 +   published by the Free Software Foundation; either version 2, or (at
7282 +   your option) any later version.
7283 +
7284 +   This program is distributed in the hope that it will be useful, but
7285 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7286 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7287 +   General Public License for more details.
7288 +*/
7289 +
7290 +#include "ipkg.h"
7291 +
7292 +#include "nv_pair.h"
7293 +#include "str_util.h"
7294 +
7295 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value)
7296 +{
7297 +    nv_pair->name = str_dup_safe(name);
7298 +    nv_pair->value = str_dup_safe(value);
7299 +
7300 +    return 0;
7301 +}
7302 +
7303 +void nv_pair_deinit(nv_pair_t *nv_pair)
7304 +{
7305 +    free(nv_pair->name);
7306 +    nv_pair->name = NULL;
7307 +
7308 +    free(nv_pair->value);
7309 +    nv_pair->value = NULL;
7310 +}
7311 +
7312 +
7313 diff -urN busybox.old/archival/libipkg/nv_pair.h busybox.dev/archival/libipkg/nv_pair.h
7314 --- busybox.old/archival/libipkg/nv_pair.h      1970-01-01 01:00:00.000000000 +0100
7315 +++ busybox.dev/archival/libipkg/nv_pair.h      2007-01-22 13:41:03.000000000 +0100
7316 @@ -0,0 +1,32 @@
7317 +/* nv_pair.h - the itsy package management system
7318 +
7319 +   Carl D. Worth
7320 +
7321 +   Copyright (C) 2001 University of Southern California
7322 +
7323 +   This program is free software; you can redistribute it and/or
7324 +   modify it under the terms of the GNU General Public License as
7325 +   published by the Free Software Foundation; either version 2, or (at
7326 +   your option) any later version.
7327 +
7328 +   This program is distributed in the hope that it will be useful, but
7329 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7330 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7331 +   General Public License for more details.
7332 +*/
7333 +
7334 +#ifndef NV_PAIR_H
7335 +#define NV_PAIR_H
7336 +
7337 +typedef struct nv_pair nv_pair_t;
7338 +struct nv_pair
7339 +{
7340 +    char *name;
7341 +    char *value;
7342 +};
7343 +
7344 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value);
7345 +void nv_pair_deinit(nv_pair_t *nv_pair);
7346 +
7347 +#endif
7348 +
7349 diff -urN busybox.old/archival/libipkg/nv_pair_list.c busybox.dev/archival/libipkg/nv_pair_list.c
7350 --- busybox.old/archival/libipkg/nv_pair_list.c 1970-01-01 01:00:00.000000000 +0100
7351 +++ busybox.dev/archival/libipkg/nv_pair_list.c 2007-01-22 13:41:03.000000000 +0100
7352 @@ -0,0 +1,98 @@
7353 +/* nv_pair_list.c - the itsy package management system
7354 +
7355 +   Carl D. Worth
7356 +
7357 +   Copyright (C) 2001 University of Southern California
7358 +
7359 +   This program is free software; you can redistribute it and/or
7360 +   modify it under the terms of the GNU General Public License as
7361 +   published by the Free Software Foundation; either version 2, or (at
7362 +   your option) any later version.
7363 +
7364 +   This program is distributed in the hope that it will be useful, but
7365 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7366 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7367 +   General Public License for more details.
7368 +*/
7369 +
7370 +#include "ipkg.h"
7371 +
7372 +#include "nv_pair.h"
7373 +#include "void_list.h"
7374 +#include "nv_pair_list.h"
7375 +
7376 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data)
7377 +{
7378 +    return void_list_elt_init((void_list_elt_t *) elt, data);
7379 +}
7380 +
7381 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt)
7382 +{
7383 +    void_list_elt_deinit((void_list_elt_t *) elt);
7384 +}
7385 +
7386 +int nv_pair_list_init(nv_pair_list_t *list)
7387 +{
7388 +    return void_list_init((void_list_t *) list);
7389 +}
7390 +
7391 +void nv_pair_list_deinit(nv_pair_list_t *list)
7392 +{
7393 +    nv_pair_list_elt_t *iter;
7394 +    nv_pair_t *nv_pair;
7395 +
7396 +    for (iter = list->head; iter; iter = iter->next) {
7397 +       nv_pair = iter->data;
7398 +       nv_pair_deinit(nv_pair);
7399 +
7400 +       /* malloced in nv_pair_list_append */
7401 +       free(nv_pair);
7402 +       iter->data = NULL;
7403 +    }
7404 +    void_list_deinit((void_list_t *) list);
7405 +}
7406 +
7407 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value)
7408 +{
7409 +    int err;
7410 +
7411 +    /* freed in nv_pair_list_deinit */
7412 +    nv_pair_t *nv_pair = malloc(sizeof(nv_pair_t));
7413 +
7414 +    if (nv_pair == NULL) {
7415 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7416 +       return NULL;
7417 +    }
7418 +    nv_pair_init(nv_pair, name, value);
7419 +
7420 +    err = void_list_append((void_list_t *) list, nv_pair);
7421 +    if (err) {
7422 +       return NULL;
7423 +    }
7424 +
7425 +    return nv_pair;
7426 +}
7427 +
7428 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data)
7429 +{
7430 +    return void_list_push((void_list_t *) list, data);
7431 +}
7432 +
7433 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list)
7434 +{
7435 +    return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list);
7436 +}
7437 +
7438 +char *nv_pair_list_find(nv_pair_list_t *list, char *name)
7439 +{
7440 +     nv_pair_list_elt_t *iter;
7441 +     nv_pair_t *nv_pair;
7442 +
7443 +     for (iter = list->head; iter; iter = iter->next) {
7444 +         nv_pair = iter->data;
7445 +         if (strcmp(nv_pair->name, name) == 0) {
7446 +              return nv_pair->value;
7447 +         }
7448 +     }    
7449 +     return NULL;
7450 +}
7451 diff -urN busybox.old/archival/libipkg/nv_pair_list.h busybox.dev/archival/libipkg/nv_pair_list.h
7452 --- busybox.old/archival/libipkg/nv_pair_list.h 1970-01-01 01:00:00.000000000 +0100
7453 +++ busybox.dev/archival/libipkg/nv_pair_list.h 2007-01-22 13:41:03.000000000 +0100
7454 @@ -0,0 +1,60 @@
7455 +/* nv_pair_list.h - the itsy package management system
7456 +
7457 +   Carl D. Worth
7458 +
7459 +   Copyright (C) 2001 University of Southern California
7460 +
7461 +   This program is free software; you can redistribute it and/or
7462 +   modify it under the terms of the GNU General Public License as
7463 +   published by the Free Software Foundation; either version 2, or (at
7464 +   your option) any later version.
7465 +
7466 +   This program is distributed in the hope that it will be useful, but
7467 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7468 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7469 +   General Public License for more details.
7470 +*/
7471 +
7472 +#ifndef NV_PAIR_LIST_H
7473 +#define NV_PAIR_LIST_H
7474 +
7475 +#include "nv_pair.h"
7476 +#include "void_list.h"
7477 +
7478 +typedef struct nv_pair_list_elt nv_pair_list_elt_t;
7479 +struct nv_pair_list_elt
7480 +{
7481 +    nv_pair_list_elt_t *next;
7482 +    nv_pair_t *data;
7483 +};
7484 +
7485 +typedef struct nv_pair_list nv_pair_list_t;
7486 +struct nv_pair_list
7487 +{
7488 +    nv_pair_list_elt_t pre_head;
7489 +    nv_pair_list_elt_t *head;
7490 +    nv_pair_list_elt_t *tail;
7491 +};
7492 +
7493 +static inline int nv_pair_list_empty(nv_pair_list_t *list)
7494 +{
7495 +     if (list->head == NULL)
7496 +         return 1;
7497 +     else
7498 +         return 0;
7499 +}
7500 +
7501 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data);
7502 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt);
7503 +
7504 +int nv_pair_list_init(nv_pair_list_t *list);
7505 +void nv_pair_list_deinit(nv_pair_list_t *list);
7506 +
7507 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list,
7508 +                              const char *name, const char *value);
7509 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data);
7510 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list);
7511 +char *nv_pair_list_find(nv_pair_list_t *list, char *name);
7512 +
7513 +#endif
7514 +
7515 diff -urN busybox.old/archival/libipkg/pkg.c busybox.dev/archival/libipkg/pkg.c
7516 --- busybox.old/archival/libipkg/pkg.c  1970-01-01 01:00:00.000000000 +0100
7517 +++ busybox.dev/archival/libipkg/pkg.c  2007-01-22 13:41:10.000000000 +0100
7518 @@ -0,0 +1,1747 @@
7519 +/* pkg.c - the itsy package management system
7520 +
7521 +   Carl D. Worth
7522 +
7523 +   Copyright (C) 2001 University of Southern California
7524 +
7525 +   This program is free software; you can redistribute it and/or
7526 +   modify it under the terms of the GNU General Public License as
7527 +   published by the Free Software Foundation; either version 2, or (at
7528 +   your option) any later version.
7529 +
7530 +   This program is distributed in the hope that it will be useful, but
7531 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7532 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7533 +   General Public License for more details.
7534 +*/
7535 +
7536 +#include "ipkg.h"
7537 +#include <ctype.h>
7538 +#include <string.h>
7539 +#include <errno.h>
7540 +
7541 +#include "pkg.h"
7542 +
7543 +#include "pkg_parse.h"
7544 +#include "pkg_extract.h"
7545 +#include "ipkg_message.h"
7546 +#include "ipkg_utils.h"
7547 +
7548 +#include "sprintf_alloc.h"
7549 +#include "file_util.h"
7550 +#include "str_util.h"
7551 +#include "xsystem.h"
7552 +#include "ipkg_conf.h"
7553 +
7554 +typedef struct enum_map enum_map_t;
7555 +struct enum_map
7556 +{
7557 +     int value;
7558 +     char *str;
7559 +};
7560 +
7561 +static const enum_map_t pkg_state_want_map[] = {
7562 +     { SW_UNKNOWN, "unknown"},
7563 +     { SW_INSTALL, "install"},
7564 +     { SW_DEINSTALL, "deinstall"},
7565 +     { SW_PURGE, "purge"}
7566 +};
7567 +
7568 +static const enum_map_t pkg_state_flag_map[] = {
7569 +     { SF_OK, "ok"},
7570 +     { SF_REINSTREQ, "reinstreq"},
7571 +     { SF_HOLD, "hold"},
7572 +     { SF_REPLACE, "replace"},
7573 +     { SF_NOPRUNE, "noprune"},
7574 +     { SF_PREFER, "prefer"},
7575 +     { SF_OBSOLETE, "obsolete"},
7576 +     { SF_USER, "user"},
7577 +};
7578 +
7579 +static const enum_map_t pkg_state_status_map[] = {
7580 +     { SS_NOT_INSTALLED, "not-installed" },
7581 +     { SS_UNPACKED, "unpacked" },
7582 +     { SS_HALF_CONFIGURED, "half-configured" },
7583 +     { SS_INSTALLED, "installed" },
7584 +     { SS_HALF_INSTALLED, "half-installed" },
7585 +     { SS_CONFIG_FILES, "config-files" },
7586 +     { SS_POST_INST_FAILED, "post-inst-failed" },
7587 +     { SS_REMOVAL_FAILED, "removal-failed" }
7588 +};
7589 +
7590 +static int verrevcmp(const char *val, const char *ref);
7591 +
7592 +
7593 +pkg_t *pkg_new(void)
7594 +{
7595 +     pkg_t *pkg;
7596 +
7597 +     pkg = malloc(sizeof(pkg_t));
7598 +     if (pkg == NULL) {
7599 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7600 +         return NULL;
7601 +     }
7602 +
7603 +     pkg_init(pkg);
7604 +
7605 +     return pkg;
7606 +}
7607 +
7608 +int pkg_init(pkg_t *pkg)
7609 +{
7610 +     memset(pkg, 0, sizeof(pkg_t));
7611 +     pkg->name = NULL;
7612 +     pkg->epoch = 0;
7613 +     pkg->version = NULL;
7614 +     pkg->revision = NULL;
7615 +     pkg->familiar_revision = NULL;
7616 +     pkg->dest = NULL;
7617 +     pkg->src = NULL;
7618 +     pkg->architecture = NULL;
7619 +     pkg->maintainer = NULL;
7620 +     pkg->section = NULL;
7621 +     pkg->description = NULL;
7622 +     pkg->state_want = SW_UNKNOWN;
7623 +     pkg->state_flag = SF_OK;
7624 +     pkg->state_status = SS_NOT_INSTALLED;
7625 +     pkg->depends_str = NULL;
7626 +     pkg->provides_str = NULL;
7627 +     pkg->depends_count = 0;
7628 +     pkg->depends = NULL;
7629 +     pkg->suggests_str = NULL;
7630 +     pkg->recommends_str = NULL;
7631 +     pkg->suggests_count = 0;
7632 +     pkg->recommends_count = 0;
7633 +
7634 +     /* Abhaya: added init for conflicts fields */
7635 +     pkg->conflicts = NULL;
7636 +     pkg->conflicts_count = 0;
7637 +
7638 +     /* added for replaces.  Jamey 7/23/2002 */
7639 +     pkg->replaces = NULL;
7640 +     pkg->replaces_count = 0;
7641 +    
7642 +     pkg->pre_depends_count = 0;
7643 +     pkg->pre_depends_str = NULL;
7644 +     pkg->provides_count = 0;
7645 +     pkg->provides = NULL;
7646 +     pkg->filename = NULL;
7647 +     pkg->local_filename = NULL;
7648 +     pkg->tmp_unpack_dir = NULL;
7649 +     pkg->md5sum = NULL;
7650 +     pkg->size = NULL;
7651 +     pkg->installed_size = NULL;
7652 +     pkg->priority = NULL;
7653 +     pkg->source = NULL;
7654 +     conffile_list_init(&pkg->conffiles);
7655 +     pkg->installed_files = NULL;
7656 +     pkg->installed_files_ref_cnt = 0;
7657 +     pkg->essential = 0;
7658 +     pkg->provided_by_hand = 0;
7659 +
7660 +     return 0;
7661 +}
7662 +
7663 +void pkg_deinit(pkg_t *pkg)
7664 +{
7665 +     free(pkg->name);
7666 +     pkg->name = NULL;
7667 +     pkg->epoch = 0;
7668 +     free(pkg->version);
7669 +     pkg->version = NULL;
7670 +     /* revision and familiar_revision share storage with version, so
7671 +       don't free */
7672 +     pkg->revision = NULL;
7673 +     pkg->familiar_revision = NULL;
7674 +     /* owned by ipkg_conf_t */
7675 +     pkg->dest = NULL;
7676 +     /* owned by ipkg_conf_t */
7677 +     pkg->src = NULL;
7678 +     free(pkg->architecture);
7679 +     pkg->architecture = NULL;
7680 +     free(pkg->maintainer);
7681 +     pkg->maintainer = NULL;
7682 +     free(pkg->section);
7683 +     pkg->section = NULL;
7684 +     free(pkg->description);
7685 +     pkg->description = NULL;
7686 +     pkg->state_want = SW_UNKNOWN;
7687 +     pkg->state_flag = SF_OK;
7688 +     pkg->state_status = SS_NOT_INSTALLED;
7689 +     free(pkg->depends_str);
7690 +     pkg->depends_str = NULL;
7691 +     free(pkg->provides_str);
7692 +     pkg->provides_str = NULL;
7693 +     pkg->depends_count = 0;
7694 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->depends ? */
7695 +     pkg->pre_depends_count = 0;
7696 +     free(pkg->pre_depends_str);
7697 +     pkg->pre_depends_str = NULL;
7698 +     pkg->provides_count = 0;
7699 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->provides ? */
7700 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->suggests ? */
7701 +     free(pkg->filename);
7702 +     pkg->filename = NULL;
7703 +     free(pkg->local_filename);
7704 +     pkg->local_filename = NULL;
7705 +     /* CLEANUP: It'd be nice to pullin the cleanup function from
7706 +       ipkg_install.c here. See comment in
7707 +       ipkg_install.c:cleanup_temporary_files */
7708 +     free(pkg->tmp_unpack_dir);
7709 +     pkg->tmp_unpack_dir = NULL;
7710 +     free(pkg->md5sum);
7711 +     pkg->md5sum = NULL;
7712 +     free(pkg->size);
7713 +     pkg->size = NULL;
7714 +     free(pkg->installed_size);
7715 +     pkg->installed_size = NULL;
7716 +     free(pkg->priority);
7717 +     pkg->priority = NULL;
7718 +     free(pkg->source);
7719 +     pkg->source = NULL;
7720 +     conffile_list_deinit(&pkg->conffiles);
7721 +     /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
7722 +       since if they are calling deinit, they should know. Maybe do an
7723 +       assertion here instead? */
7724 +     pkg->installed_files_ref_cnt = 1;
7725 +     pkg_free_installed_files(pkg);
7726 +     pkg->essential = 0;
7727 +}
7728 +
7729 +int pkg_init_from_file(pkg_t *pkg, const char *filename)
7730 +{
7731 +     int err;
7732 +     char **raw;
7733 +     FILE *control_file;
7734 +
7735 +     err = pkg_init(pkg);
7736 +     if (err) { return err; }
7737 +
7738 +     pkg->local_filename = strdup(filename);
7739 +    
7740 +     control_file = tmpfile();
7741 +     err = pkg_extract_control_file_to_stream(pkg, control_file);
7742 +     if (err) { return err; }
7743 +
7744 +     rewind(control_file);
7745 +     raw = read_raw_pkgs_from_stream(control_file);
7746 +     pkg_parse_raw(pkg, &raw, NULL, NULL);
7747 +
7748 +     fclose(control_file);
7749 +
7750 +     return 0;
7751 +}
7752 +
7753 +/* Merge any new information in newpkg into oldpkg */
7754 +/* XXX: CLEANUP: This function shouldn't actually modify anything in
7755 +   newpkg, but should leave it usable. This rework is so that
7756 +   pkg_hash_insert doesn't clobber the pkg that you pass into it. */
7757 +/* 
7758 + * uh, i thought that i had originally written this so that it took 
7759 + * two pkgs and returned a new one?  we can do that again... -sma
7760 + */
7761 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status)
7762 +{
7763 +     if (oldpkg == newpkg) {
7764 +         return 0;
7765 +     }
7766 +
7767 +     if (!oldpkg->src)
7768 +         oldpkg->src = newpkg->src;
7769 +     if (!oldpkg->dest)
7770 +         oldpkg->dest = newpkg->dest;
7771 +     if (!oldpkg->architecture)
7772 +         oldpkg->architecture = str_dup_safe(newpkg->architecture);
7773 +     if (!oldpkg->arch_priority)
7774 +         oldpkg->arch_priority = newpkg->arch_priority;
7775 +     if (!oldpkg->section)
7776 +         oldpkg->section = str_dup_safe(newpkg->section);
7777 +     if(!oldpkg->maintainer)
7778 +         oldpkg->maintainer = str_dup_safe(newpkg->maintainer);
7779 +     if(!oldpkg->description)
7780 +         oldpkg->description = str_dup_safe(newpkg->description);
7781 +     if (set_status) {
7782 +         /* merge the state_flags from the new package */
7783 +         oldpkg->state_want = newpkg->state_want;
7784 +         oldpkg->state_status = newpkg->state_status;
7785 +         oldpkg->state_flag = newpkg->state_flag;
7786 +     } else {
7787 +         if (oldpkg->state_want == SW_UNKNOWN)
7788 +              oldpkg->state_want = newpkg->state_want;
7789 +         if (oldpkg->state_status == SS_NOT_INSTALLED)
7790 +              oldpkg->state_status = newpkg->state_status;
7791 +         oldpkg->state_flag |= newpkg->state_flag;
7792 +     }
7793 +
7794 +     if (!oldpkg->depends_str && !oldpkg->pre_depends_str && !oldpkg->recommends_str && !oldpkg->suggests_str) {
7795 +         oldpkg->depends_str = newpkg->depends_str;
7796 +         newpkg->depends_str = NULL;
7797 +         oldpkg->depends_count = newpkg->depends_count;
7798 +         newpkg->depends_count = 0;
7799 +
7800 +         oldpkg->depends = newpkg->depends;
7801 +         newpkg->depends = NULL;
7802 +
7803 +         oldpkg->pre_depends_str = newpkg->pre_depends_str;
7804 +         newpkg->pre_depends_str = NULL;
7805 +         oldpkg->pre_depends_count = newpkg->pre_depends_count;
7806 +         newpkg->pre_depends_count = 0;
7807 +
7808 +         oldpkg->recommends_str = newpkg->recommends_str;
7809 +         newpkg->recommends_str = NULL;
7810 +         oldpkg->recommends_count = newpkg->recommends_count;
7811 +         newpkg->recommends_count = 0;
7812 +
7813 +         oldpkg->suggests_str = newpkg->suggests_str;
7814 +         newpkg->suggests_str = NULL;
7815 +         oldpkg->suggests_count = newpkg->suggests_count;
7816 +         newpkg->suggests_count = 0;
7817 +     }
7818 +
7819 +     if (!oldpkg->provides_str) {
7820 +         oldpkg->provides_str = newpkg->provides_str;
7821 +         newpkg->provides_str = NULL;
7822 +         oldpkg->provides_count = newpkg->provides_count;
7823 +         newpkg->provides_count = 0;
7824 +
7825 +         oldpkg->provides = newpkg->provides;
7826 +         newpkg->provides = NULL;
7827 +     }
7828 +
7829 +     if (!oldpkg->conflicts_str) {
7830 +         oldpkg->conflicts_str = newpkg->conflicts_str;
7831 +         newpkg->conflicts_str = NULL;
7832 +         oldpkg->conflicts_count = newpkg->conflicts_count;
7833 +         newpkg->conflicts_count = 0;
7834 +
7835 +         oldpkg->conflicts = newpkg->conflicts;
7836 +         newpkg->conflicts = NULL;
7837 +     }
7838 +
7839 +     if (!oldpkg->replaces_str) {
7840 +         oldpkg->replaces_str = newpkg->replaces_str;
7841 +         newpkg->replaces_str = NULL;
7842 +         oldpkg->replaces_count = newpkg->replaces_count;
7843 +         newpkg->replaces_count = 0;
7844 +
7845 +         oldpkg->replaces = newpkg->replaces;
7846 +         newpkg->replaces = NULL;
7847 +     }
7848 +
7849 +     if (!oldpkg->filename)
7850 +         oldpkg->filename = str_dup_safe(newpkg->filename);
7851 +     if (0)
7852 +     fprintf(stdout, "pkg=%s old local_filename=%s new local_filename=%s\n", 
7853 +            oldpkg->name, oldpkg->local_filename, newpkg->local_filename);
7854 +     if (!oldpkg->local_filename)
7855 +         oldpkg->local_filename = str_dup_safe(newpkg->local_filename);
7856 +     if (!oldpkg->tmp_unpack_dir)
7857 +         oldpkg->tmp_unpack_dir = str_dup_safe(newpkg->tmp_unpack_dir);
7858 +     if (!oldpkg->md5sum)
7859 +         oldpkg->md5sum = str_dup_safe(newpkg->md5sum);
7860 +     if (!oldpkg->size)
7861 +         oldpkg->size = str_dup_safe(newpkg->size);
7862 +     if (!oldpkg->installed_size)
7863 +         oldpkg->installed_size = str_dup_safe(newpkg->installed_size);
7864 +     if (!oldpkg->priority)
7865 +         oldpkg->priority = str_dup_safe(newpkg->priority);
7866 +     if (!oldpkg->source)
7867 +         oldpkg->source = str_dup_safe(newpkg->source);
7868 +     if (oldpkg->conffiles.head == NULL){
7869 +         oldpkg->conffiles = newpkg->conffiles;
7870 +         conffile_list_init(&newpkg->conffiles);
7871 +     }
7872 +     if (!oldpkg->installed_files){
7873 +         oldpkg->installed_files = newpkg->installed_files;
7874 +         oldpkg->installed_files_ref_cnt = newpkg->installed_files_ref_cnt;
7875 +         newpkg->installed_files = NULL;
7876 +     }
7877 +     if (!oldpkg->essential)
7878 +         oldpkg->essential = newpkg->essential;
7879 +
7880 +     oldpkg->provided_by_hand |= newpkg->provided_by_hand;
7881 +
7882 +     return 0;
7883 +}
7884 +
7885 +abstract_pkg_t *abstract_pkg_new(void)
7886 +{
7887 +     abstract_pkg_t * ab_pkg;
7888 +
7889 +     ab_pkg = malloc(sizeof(abstract_pkg_t));
7890 +
7891 +     if (ab_pkg == NULL) {
7892 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7893 +         return NULL;
7894 +     }
7895 +
7896 +     if ( abstract_pkg_init(ab_pkg) < 0 ) 
7897 +        return NULL;
7898 +
7899 +     return ab_pkg;
7900 +}
7901 +
7902 +int abstract_pkg_init(abstract_pkg_t *ab_pkg)
7903 +{
7904 +     memset(ab_pkg, 0, sizeof(abstract_pkg_t));
7905 +
7906 +     ab_pkg->provided_by = abstract_pkg_vec_alloc();
7907 +     if (ab_pkg->provided_by==NULL){
7908 +        return -1;
7909 +     }
7910 +     ab_pkg->dependencies_checked = 0;
7911 +     ab_pkg->state_status = SS_NOT_INSTALLED;
7912 +
7913 +     return 0;
7914 +}
7915 +
7916 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg){
7917 +     char * temp_str;
7918 +     char **raw =NULL;
7919 +     char **raw_start=NULL; 
7920 +
7921 +     temp_str = (char *) malloc (strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
7922 +     if (temp_str == NULL ){
7923 +        ipkg_message(conf, IPKG_INFO, "Out of memory in  %s\n", __FUNCTION__);
7924 +        return;
7925 +     }
7926 +     sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
7927 +   
7928 +     raw = raw_start = read_raw_pkgs_from_file(temp_str);
7929 +     if (raw == NULL ){
7930 +        ipkg_message(conf, IPKG_ERROR, "Unable to open the control file in  %s\n", __FUNCTION__);
7931 +        return;
7932 +     }
7933 +
7934 +     while(*raw){
7935 +        if (!pkg_valorize_other_field(pkg, &raw ) == 0) {
7936 +            ipkg_message(conf, IPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
7937 +        }
7938 +     }
7939 +     raw = raw_start;
7940 +     while (*raw) {
7941 +        if (raw!=NULL)
7942 +          free(*raw++);
7943 +     }
7944 +
7945 +     free(raw_start); 
7946 +     free(temp_str);
7947 +
7948 +     return ;
7949 +
7950 +}
7951 +
7952 +char * pkg_formatted_info(pkg_t *pkg )
7953 +{
7954 +     char *line;
7955 +     char * buff;
7956 +
7957 +     buff = malloc(8192);
7958 +     if (buff == NULL) {
7959 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7960 +         return NULL;
7961 +     }
7962 +
7963 +     buff[0] = '\0';
7964 +
7965 +     line = pkg_formatted_field(pkg, "Package");
7966 +     strncat(buff ,line, strlen(line));
7967 +     free(line);
7968 +
7969 +     line = pkg_formatted_field(pkg, "Version");
7970 +     strncat(buff ,line, strlen(line));
7971 +     free(line);
7972 +
7973 +     line = pkg_formatted_field(pkg, "Depends");
7974 +     strncat(buff ,line, strlen(line));
7975 +     free(line);
7976 +     
7977 +     line = pkg_formatted_field(pkg, "Recommends");
7978 +     strncat(buff ,line, strlen(line));
7979 +     free(line);
7980 +
7981 +     line = pkg_formatted_field(pkg, "Suggests");
7982 +     strncat(buff ,line, strlen(line));
7983 +     free(line);
7984 +
7985 +     line = pkg_formatted_field(pkg, "Provides");
7986 +     strncat(buff ,line, strlen(line));
7987 +     free(line);
7988 +
7989 +     line = pkg_formatted_field(pkg, "Replaces");
7990 +     strncat(buff ,line, strlen(line));
7991 +     free(line);
7992 +
7993 +     line = pkg_formatted_field(pkg, "Conflicts");
7994 +     strncat(buff ,line, strlen(line));
7995 +     free(line);
7996 +
7997 +     line = pkg_formatted_field(pkg, "Status");
7998 +     strncat(buff ,line, strlen(line));
7999 +     free(line);
8000 +
8001 +     line = pkg_formatted_field(pkg, "Section");
8002 +     strncat(buff ,line, strlen(line));
8003 +     free(line);
8004 +
8005 +     line = pkg_formatted_field(pkg, "Essential"); /* @@@@ should be removed in future release. *//* I do not agree with this Pigi*/
8006 +     strncat(buff ,line, strlen(line));
8007 +     free(line);
8008 +
8009 +     line = pkg_formatted_field(pkg, "Architecture");
8010 +     strncat(buff ,line, strlen(line));
8011 +     free(line);
8012 +
8013 +     line = pkg_formatted_field(pkg, "Maintainer");
8014 +     strncat(buff ,line, strlen(line));
8015 +     free(line);
8016 +
8017 +     line = pkg_formatted_field(pkg, "MD5sum");
8018 +     strncat(buff ,line, strlen(line));
8019 +     free(line);
8020 +
8021 +     line = pkg_formatted_field(pkg, "Size");
8022 +     strncat(buff ,line, strlen(line));
8023 +     free(line);
8024 +
8025 +     line = pkg_formatted_field(pkg, "Filename");
8026 +     strncat(buff ,line, strlen(line));
8027 +     free(line);
8028 +
8029 +     line = pkg_formatted_field(pkg, "Conffiles");
8030 +     strncat(buff ,line, strlen(line));
8031 +     free(line);
8032 +
8033 +     line = pkg_formatted_field(pkg, "Source");
8034 +     strncat(buff ,line, strlen(line));
8035 +     free(line);
8036 +
8037 +     line = pkg_formatted_field(pkg, "Description");
8038 +     strncat(buff ,line, strlen(line));
8039 +     free(line);
8040 +
8041 +     line = pkg_formatted_field(pkg, "Installed-Time");
8042 +     strncat(buff ,line, strlen(line));
8043 +     free(line);
8044 +
8045 +     return buff;
8046 +}
8047 +
8048 +char * pkg_formatted_field(pkg_t *pkg, const char *field )
8049 +{
8050 +     static size_t LINE_LEN = 128;
8051 +     char line_str[LINE_LEN];
8052 +     char * temp = (char *)malloc(1);
8053 +     int len = 0;
8054 +     int flag_provide_false = 0;
8055 +
8056 +/*
8057 +  Pigi: After some discussion with Florian we decided to modify the full procedure in 
8058 +        dynamic memory allocation. This should avoid any other segv in this area ( except for bugs )
8059 +*/
8060 +
8061 +     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8062 +         goto UNKNOWN_FMT_FIELD;
8063 +     }
8064 +
8065 +     temp[0]='\0'; 
8066 +
8067 +     switch (field[0])
8068 +     {
8069 +     case 'a':
8070 +     case 'A':
8071 +         if (strcasecmp(field, "Architecture") == 0) {
8072 +              /* Architecture */
8073 +              if (pkg->architecture) {
8074 +                   temp = (char *)realloc(temp,strlen(pkg->architecture)+17);
8075 +                   if ( temp == NULL ){
8076 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8077 +                     return NULL;
8078 +                   }
8079 +                   temp[0]='\0';
8080 +                   snprintf(temp, (strlen(pkg->architecture)+17), "Architecture: %s\n", pkg->architecture);
8081 +              }
8082 +         } else {
8083 +              goto UNKNOWN_FMT_FIELD;
8084 +         }
8085 +         break;
8086 +     case 'c':
8087 +     case 'C':
8088 +         if (strcasecmp(field, "Conffiles") == 0) {
8089 +              /* Conffiles */
8090 +              conffile_list_elt_t *iter;
8091 +
8092 +              if (pkg->conffiles.head == NULL) {
8093 +                   return temp;
8094 +              }
8095 +
8096 +               len = 14 ;
8097 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8098 +                   if (iter->data->name && iter->data->value) {
8099 +                       len = len + (strlen(iter->data->name)+strlen(iter->data->value)+5);
8100 +                   }
8101 +              }
8102 +               temp = (char *)realloc(temp,len);
8103 +               if ( temp == NULL ){
8104 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8105 +                 return NULL;
8106 +               }
8107 +               temp[0]='\0';
8108 +               strncpy(temp, "Conffiles:\n", 12);
8109 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8110 +                   if (iter->data->name && iter->data->value) {
8111 +                         snprintf(line_str, LINE_LEN, "%s %s\n", iter->data->name, iter->data->value);
8112 +                         strncat(temp, line_str, strlen(line_str));           
8113 +                   }
8114 +              }
8115 +         } else if (strcasecmp(field, "Conflicts") == 0) {
8116 +              int i;
8117 +
8118 +              if (pkg->conflicts_count) {
8119 +                    len = 14 ;
8120 +                   for(i = 0; i < pkg->conflicts_count; i++) {
8121 +                        len = len + (strlen(pkg->conflicts_str[i])+5);
8122 +                    }
8123 +                    temp = (char *)realloc(temp,len);
8124 +                    if ( temp == NULL ){
8125 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8126 +                      return NULL;
8127 +                    }
8128 +                    temp[0]='\0';
8129 +                    strncpy(temp, "Conflicts:", 11);
8130 +                   for(i = 0; i < pkg->conflicts_count; i++) {
8131 +                        snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->conflicts_str[i]);
8132 +                        strncat(temp, line_str, strlen(line_str));           
8133 +                    }
8134 +                    strncat(temp, "\n", strlen("\n")); 
8135 +              }
8136 +         } else {
8137 +              goto UNKNOWN_FMT_FIELD;
8138 +         }
8139 +         break;
8140 +     case 'd':
8141 +     case 'D':
8142 +         if (strcasecmp(field, "Depends") == 0) {
8143 +              /* Depends */
8144 +              int i;
8145 +
8146 +              if (pkg->depends_count) {
8147 +                    len = 14 ;
8148 +                   for(i = 0; i < pkg->depends_count; i++) {
8149 +                        len = len + (strlen(pkg->depends_str[i])+4);
8150 +                    }
8151 +                    temp = (char *)realloc(temp,len);
8152 +                    if ( temp == NULL ){
8153 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8154 +                      return NULL;
8155 +                    }
8156 +                    temp[0]='\0';
8157 +                    strncpy(temp, "Depends:", 10);
8158 +                   for(i = 0; i < pkg->depends_count; i++) {
8159 +                        snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
8160 +                        strncat(temp, line_str, strlen(line_str));           
8161 +                    }
8162 +                    strncat(temp, "\n", strlen("\n")); 
8163 +              }
8164 +         } else if (strcasecmp(field, "Description") == 0) {
8165 +              /* Description */
8166 +              if (pkg->description) {
8167 +                   temp = (char *)realloc(temp,strlen(pkg->description)+16);
8168 +                   if ( temp == NULL ){
8169 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8170 +                     return NULL;
8171 +                   }
8172 +                   temp[0]='\0';
8173 +                   snprintf(temp, (strlen(pkg->description)+16), "Description: %s\n", pkg->description);
8174 +              }
8175 +         } else {
8176 +              goto UNKNOWN_FMT_FIELD;
8177 +         }
8178 +      break;
8179 +     case 'e':
8180 +     case 'E': {
8181 +         /* Essential */
8182 +         if (pkg->essential) {
8183 +              temp = (char *)realloc(temp,16);
8184 +              if ( temp == NULL ){
8185 +                fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8186 +                return NULL;
8187 +              }
8188 +              temp[0]='\0';
8189 +              snprintf(temp, (16), "Essential: yes\n");
8190 +         }
8191 +     }
8192 +         break;
8193 +     case 'f':
8194 +     case 'F': {
8195 +         /* Filename */
8196 +         if (pkg->filename) {
8197 +              temp = (char *)realloc(temp,strlen(pkg->filename)+12);
8198 +              if ( temp == NULL ){
8199 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8200 +                 return NULL;
8201 +              }
8202 +              temp[0]='\0';
8203 +              snprintf(temp, (strlen(pkg->filename)+12), "Filename: %s\n", pkg->filename);
8204 +         }
8205 +     }
8206 +         break;
8207 +     case 'i':
8208 +     case 'I': {
8209 +         if (strcasecmp(field, "Installed-Size") == 0) {
8210 +              /* Installed-Size */
8211 +               temp = (char *)realloc(temp,strlen(pkg->installed_size)+17);
8212 +               if ( temp == NULL ){
8213 +                  fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8214 +                  return NULL;
8215 +               }
8216 +               temp[0]='\0';
8217 +               snprintf(temp, (strlen(pkg->installed_size)+17), "Installed-Size: %s\n", pkg->installed_size);
8218 +         } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) {
8219 +               temp = (char *)realloc(temp,29);
8220 +               if ( temp == NULL ){
8221 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8222 +                 return NULL;
8223 +               }
8224 +               temp[0]='\0';
8225 +               snprintf(temp, 29, "Installed-Time: %lu\n", pkg->installed_time);
8226 +         }
8227 +     }
8228 +         break;
8229 +     case 'm':
8230 +     case 'M': {
8231 +         /* Maintainer | MD5sum */
8232 +         if (strcasecmp(field, "Maintainer") == 0) {
8233 +              /* Maintainer */
8234 +              if (pkg->maintainer) {
8235 +                   temp = (char *)realloc(temp,strlen(pkg->maintainer)+14);
8236 +                   if ( temp == NULL ){
8237 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8238 +                     return NULL;
8239 +                   }
8240 +                   temp[0]='\0';
8241 +                   snprintf(temp, (strlen(pkg->maintainer)+14), "maintainer: %s\n", pkg->maintainer);
8242 +              }
8243 +         } else if (strcasecmp(field, "MD5sum") == 0) {
8244 +              /* MD5sum */
8245 +              if (pkg->md5sum) {
8246 +                   temp = (char *)realloc(temp,strlen(pkg->md5sum)+11);
8247 +                   if ( temp == NULL ){
8248 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8249 +                     return NULL;
8250 +                   }
8251 +                   temp[0]='\0';
8252 +                   snprintf(temp, (strlen(pkg->md5sum)+11), "MD5Sum: %s\n", pkg->md5sum);
8253 +              }
8254 +         } else {
8255 +              goto UNKNOWN_FMT_FIELD;
8256 +         }
8257 +     }
8258 +         break;
8259 +     case 'p':
8260 +     case 'P': {
8261 +         if (strcasecmp(field, "Package") == 0) {
8262 +              /* Package */
8263 +               temp = (char *)realloc(temp,strlen(pkg->name)+11);
8264 +               if ( temp == NULL ){
8265 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8266 +                 return NULL;
8267 +               }
8268 +               temp[0]='\0';
8269 +               snprintf(temp, (strlen(pkg->name)+11), "Package: %s\n", pkg->name);
8270 +         } else if (strcasecmp(field, "Priority") == 0) {
8271 +              /* Priority */
8272 +               temp = (char *)realloc(temp,strlen(pkg->priority)+12);
8273 +               if ( temp == NULL ){
8274 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8275 +                 return NULL;
8276 +               }
8277 +               temp[0]='\0';
8278 +               snprintf(temp, (strlen(pkg->priority)+12), "Priority: %s\n", pkg->priority);
8279 +         } else if (strcasecmp(field, "Provides") == 0) {
8280 +              /* Provides */
8281 +              int i;
8282 +
8283 +              if (pkg->provides_count) {
8284 +               /* Here we check if the ipkg_internal_use_only is used, and we discard it.*/
8285 +                  for ( i=0; i < pkg->provides_count; i++ ){
8286 +                     if (strstr(pkg->provides_str[i],"ipkg_internal_use_only")!=NULL) {
8287 +                         memset (pkg->provides_str[i],'\x0',strlen(pkg->provides_str[i])); /* Pigi clear my trick flag, just in case */
8288 +                         flag_provide_false = 1;
8289 +                      }
8290 +                  }
8291 +                  if ( !flag_provide_false ||                                             /* Pigi there is not my trick flag */
8292 +                     ((flag_provide_false) &&  (pkg->provides_count > 1))){             /* Pigi There is, but we also have others Provides */
8293 +                     char provstr[LINE_LEN];
8294 +                     len = 15;
8295 +                    for(i = 0; i < pkg->provides_count; i++) {
8296 +                         len = len + (strlen(pkg->provides_str[i])+5);
8297 +                     }
8298 +                     temp = (char *)realloc(temp,len);
8299 +                     if ( temp == NULL ){
8300 +                       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8301 +                       return NULL;
8302 +                     }
8303 +                     temp[0]='\0';
8304 +                     strncpy(temp, "Provides:", 12);
8305 +                    for(i = 0; i < pkg->provides_count; i++) {
8306 +                         if (strlen(pkg->provides_str[i])>0){;
8307 +                            snprintf(provstr, LINE_LEN, "%s %s", i == 1 ? "" : ",", pkg->provides_str[i]);
8308 +                            strncat(temp, provstr, strlen(provstr));           
8309 +                         }
8310 +                     }
8311 +                     strncat(temp, "\n", strlen("\n")); 
8312 +                  }
8313 +               }
8314 +         } else {
8315 +              goto UNKNOWN_FMT_FIELD;
8316 +         }
8317 +     }
8318 +         break;
8319 +     case 'r':
8320 +     case 'R': {
8321 +         int i;
8322 +         /* Replaces | Recommends*/
8323 +         if (strcasecmp (field, "Replaces") == 0) {
8324 +              if (pkg->replaces_count) {
8325 +                    len = 14;
8326 +                   for (i = 0; i < pkg->replaces_count; i++) {
8327 +                        len = len + (strlen(pkg->replaces_str[i])+5);
8328 +                    }
8329 +                    temp = (char *)realloc(temp,len);
8330 +                    if ( temp == NULL ){
8331 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8332 +                      return NULL;
8333 +                    }
8334 +                    temp[0]='\0';
8335 +                    strncpy(temp, "Replaces:", 12);
8336 +                   for (i = 0; i < pkg->replaces_count; i++) {
8337 +                        snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->replaces_str[i]);
8338 +                        strncat(temp, line_str, strlen(line_str));           
8339 +                    }
8340 +                    strncat(temp, "\n", strlen("\n")); 
8341 +              }
8342 +         } else if (strcasecmp (field, "Recommends") == 0) {
8343 +              if (pkg->recommends_count) {
8344 +                    len = 15;
8345 +                   for(i = 0; i < pkg->recommends_count; i++) {
8346 +                         len = len + (strlen( pkg->recommends_str[i])+5);
8347 +                    }
8348 +                    temp = (char *)realloc(temp,len);
8349 +                   if ( temp == NULL ){
8350 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8351 +                     return NULL;
8352 +                   }
8353 +                    temp[0]='\0';
8354 +                    strncpy(temp, "Recommends:", 13);
8355 +                   for(i = 0; i < pkg->recommends_count; i++) {
8356 +                        snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->recommends_str[i]);
8357 +                        strncat(temp, line_str, strlen(line_str));           
8358 +                    }
8359 +                    strncat(temp, "\n", strlen("\n")); 
8360 +              }
8361 +         } else {
8362 +              goto UNKNOWN_FMT_FIELD;
8363 +         }
8364 +     }
8365 +         break;
8366 +     case 's':
8367 +     case 'S': {
8368 +         /* Section | Size | Source | Status | Suggests */
8369 +         if (strcasecmp(field, "Section") == 0) {
8370 +              /* Section */
8371 +              if (pkg->section) {
8372 +                   temp = (char *)realloc(temp,strlen(pkg->section)+11);
8373 +                   if ( temp == NULL ){
8374 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8375 +                     return NULL;
8376 +                   }
8377 +                   temp[0]='\0';
8378 +                   snprintf(temp, (strlen(pkg->section)+11), "Section: %s\n", pkg->section);
8379 +              }
8380 +         } else if (strcasecmp(field, "Size") == 0) {
8381 +              /* Size */
8382 +              if (pkg->size) {
8383 +                   temp = (char *)realloc(temp,strlen(pkg->size)+8);
8384 +                   if ( temp == NULL ){
8385 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8386 +                     return NULL;
8387 +                   }
8388 +                   temp[0]='\0';
8389 +                   snprintf(temp, (strlen(pkg->size)+8), "Size: %s\n", pkg->size);
8390 +              }
8391 +         } else if (strcasecmp(field, "Source") == 0) {
8392 +              /* Source */
8393 +              if (pkg->source) {
8394 +                   temp = (char *)realloc(temp,strlen(pkg->source)+10);
8395 +                   if ( temp == NULL ){
8396 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8397 +                     return NULL;
8398 +                   }
8399 +                   temp[0]='\0';
8400 +                   snprintf(temp, (strlen(pkg->source)+10), "Source: %s\n", pkg->source);
8401 +               }
8402 +         } else if (strcasecmp(field, "Status") == 0) {
8403 +              /* Status */
8404 +               /* Benjamin Pineau note: we should avoid direct usage of 
8405 +                * strlen(arg) without keeping "arg" for later free()
8406 +                */
8407 +               char *pflag=pkg_state_flag_to_str(pkg->state_flag);
8408 +               char *pstat=pkg_state_status_to_str(pkg->state_status);
8409 +               char *pwant=pkg_state_want_to_str(pkg->state_want);
8410 +
8411 +               size_t sum_of_sizes = (size_t) ( strlen(pwant)+ strlen(pflag)+ strlen(pstat) + 12 );
8412 +               temp = (char *)realloc(temp,sum_of_sizes);
8413 +               if ( temp == NULL ){
8414 +                   fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8415 +                   return NULL;
8416 +                }
8417 +                temp[0]='\0';
8418 +                snprintf(temp, sum_of_sizes , "Status: %s %s %s\n", pwant, pflag, pstat);
8419 +                free(pflag);
8420 +                free(pwant);
8421 +               if(pstat) /* pfstat can be NULL if ENOMEM */
8422 +                   free(pstat);
8423 +         } else if (strcasecmp(field, "Suggests") == 0) {
8424 +              if (pkg->suggests_count) {
8425 +                   int i;
8426 +                    len = 13;
8427 +                   for(i = 0; i < pkg->suggests_count; i++) {
8428 +                        len = len + (strlen(pkg->suggests_str[i])+5);
8429 +                    }
8430 +                    temp = (char *)realloc(temp,len);
8431 +                    if ( temp == NULL ){
8432 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8433 +                      return NULL;
8434 +                    }
8435 +                    temp[0]='\0';
8436 +                    strncpy(temp, "Suggests:", 10);
8437 +                   for(i = 0; i < pkg->suggests_count; i++) {
8438 +                        snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->suggests_str[i]);
8439 +                        strncat(temp, line_str, strlen(line_str));           
8440 +                    }
8441 +                    strncat(temp, "\n", strlen("\n")); 
8442 +              }
8443 +         } else {
8444 +              goto UNKNOWN_FMT_FIELD;
8445 +         }
8446 +     }
8447 +         break;
8448 +     case 'v':
8449 +     case 'V': {
8450 +         /* Version */
8451 +         char *version = pkg_version_str_alloc(pkg);
8452 +          temp = (char *)realloc(temp,strlen(version)+14);
8453 +          if ( temp == NULL ){
8454 +             fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8455 +             return NULL;
8456 +          }
8457 +          temp[0]='\0';
8458 +          snprintf(temp, (strlen(version)+12), "Version: %s\n", version);
8459 +         free(version);
8460 +     }
8461 +         break;
8462 +     default:
8463 +         goto UNKNOWN_FMT_FIELD;
8464 +     }
8465 +
8466 +     if ( strlen(temp)<2 ) {
8467 +          temp[0]='\0';
8468 +     }
8469 +     return temp;
8470 +
8471 + UNKNOWN_FMT_FIELD:
8472 +     fprintf(stderr, "%s: ERROR: Unknown field name: %s\n", __FUNCTION__, field);
8473 +     if ( strlen(temp)<2 ) {
8474 +          temp[0]='\0';
8475 +     }
8476 +
8477 +     return temp;
8478 +}
8479 +
8480 +void pkg_print_info(pkg_t *pkg, FILE *file)
8481 +{
8482 +     char * buff;
8483 +     if (pkg == NULL) {
8484 +       return;
8485 +     }
8486 +
8487 +     buff = pkg_formatted_info(pkg);
8488 +     if ( buff == NULL ) 
8489 +         return;
8490 +     if (strlen(buff)>2){
8491 +         fwrite(buff, 1, strlen(buff), file);
8492 +     } 
8493 +     free(buff);
8494 +}
8495 +
8496 +void pkg_print_status(pkg_t * pkg, FILE * file)
8497 +{
8498 +     if (pkg == NULL) {
8499 +         return;
8500 +     }
8501 +
8502 +     /* XXX: QUESTION: Do we actually want more fields here? The
8503 +       original idea was to save space by installing only what was
8504 +       needed for actual computation, (package, version, status,
8505 +       essential, conffiles). The assumption is that all other fields
8506 +       can be found in th available file.
8507 +
8508 +       But, someone proposed the idea to make it possible to
8509 +       reconstruct a .ipk from an installed package, (ie. for beaming
8510 +       from one handheld to another). So, maybe we actually want a few
8511 +       more fields here, (depends, suggests, etc.), so that that would
8512 +       be guaranteed to work even in the absence of more information
8513 +       from the available file.
8514 +
8515 +       28-MAR-03: kergoth and I discussed this yesterday.  We think
8516 +       the essential info needs to be here for all installed packages
8517 +       because they may not appear in the Packages files on various
8518 +       feeds.  Furthermore, one should be able to install from URL or
8519 +       local storage without requiring a Packages file from any feed.
8520 +       -Jamey
8521 +     */
8522 +     pkg_print_field(pkg, file, "Package");
8523 +     pkg_print_field(pkg, file, "Version");
8524 +     pkg_print_field(pkg, file, "Depends");
8525 +     pkg_print_field(pkg, file, "Recommends");
8526 +     pkg_print_field(pkg, file, "Suggests");
8527 +     pkg_print_field(pkg, file, "Provides");
8528 +     pkg_print_field(pkg, file, "Replaces");
8529 +     pkg_print_field(pkg, file, "Conflicts");
8530 +     pkg_print_field(pkg, file, "Status");
8531 +     pkg_print_field(pkg, file, "Essential"); /* @@@@ should be removed in future release. */
8532 +     pkg_print_field(pkg, file, "Architecture");
8533 +     pkg_print_field(pkg, file, "Conffiles");
8534 +     pkg_print_field(pkg, file, "Installed-Time");
8535 +     fputs("\n", file);
8536 +}
8537 +
8538 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field)
8539 +{
8540 +     char *buff;
8541 +     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8542 +       fprintf(stderr, "%s: ERROR: Unknown field name: %s\n",
8543 +            __FUNCTION__, field);
8544 +     }
8545 +     buff = pkg_formatted_field(pkg, field);
8546 +     if (strlen(buff)>2) {
8547 +       fprintf(file, "%s", buff);
8548 +       fflush(file);
8549 +     }
8550 +     free(buff);
8551 +     return;
8552 +}
8553 +
8554 +/*
8555 + * libdpkg - Debian packaging suite library routines
8556 + * vercmp.c - comparison of version numbers
8557 + *
8558 + * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
8559 + */
8560 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg)
8561 +{
8562 +     int r;
8563 +
8564 +     if (pkg->epoch > ref_pkg->epoch) {
8565 +         return 1;
8566 +     }
8567 +
8568 +     if (pkg->epoch < ref_pkg->epoch) {
8569 +         return -1;
8570 +     }
8571 +
8572 +     r = verrevcmp(pkg->version, ref_pkg->version);
8573 +     if (r) {
8574 +         return r;
8575 +     }
8576 +
8577 +#ifdef USE_DEBVERSION
8578 +     r = verrevcmp(pkg->revision, ref_pkg->revision);
8579 +     if (r) {
8580 +         return r;
8581 +     }
8582 +
8583 +     r = verrevcmp(pkg->familiar_revision, ref_pkg->familiar_revision);
8584 +#endif
8585 +
8586 +     return r;
8587 +}
8588 +
8589 +int verrevcmp(const char *val, const char *ref)
8590 +{
8591 +     int vc, rc;
8592 +     long vl, rl;
8593 +     const char *vp, *rp;
8594 +     const char *vsep, *rsep;
8595 +    
8596 +     if (!val) val= "";
8597 +     if (!ref) ref= "";
8598 +     for (;;) {
8599 +         vp= val;  while (*vp && !isdigit(*vp)) vp++;
8600 +         rp= ref;  while (*rp && !isdigit(*rp)) rp++;
8601 +         for (;;) {
8602 +              vc= (val == vp) ? 0 : *val++;
8603 +              rc= (ref == rp) ? 0 : *ref++;
8604 +              if (!rc && !vc) break;
8605 +              if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
8606 +              if (rc && !isalpha(rc)) rc += 256;
8607 +              if (vc != rc) return vc - rc;
8608 +         }
8609 +         val= vp;
8610 +         ref= rp;
8611 +         vl=0;  if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
8612 +         rl=0;  if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
8613 +         if (vl != rl) return vl - rl;
8614 +
8615 +         vc = *val;
8616 +         rc = *ref;
8617 +         vsep = strchr(".-", vc);
8618 +         rsep = strchr(".-", rc);
8619 +         if (vsep && !rsep) return -1;
8620 +         if (!vsep && rsep) return +1;
8621 +
8622 +         if (!*val && !*ref) return 0;
8623 +         if (!*val) return -1;
8624 +         if (!*ref) return +1;
8625 +     }
8626 +}
8627 +
8628 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op)
8629 +{
8630 +     int r;
8631 +
8632 +     r = pkg_compare_versions(it, ref);
8633 +
8634 +     if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
8635 +         return r <= 0;
8636 +     }
8637 +
8638 +     if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
8639 +         return r >= 0;
8640 +     }
8641 +
8642 +     if (strcmp(op, "<<") == 0) {
8643 +         return r < 0;
8644 +     }
8645 +
8646 +     if (strcmp(op, ">>") == 0) {
8647 +         return r > 0;
8648 +     }
8649 +
8650 +     if (strcmp(op, "=") == 0) {
8651 +         return r == 0;
8652 +     }
8653 +
8654 +     fprintf(stderr, "unknown operator: %s", op);
8655 +     return 0;
8656 +}
8657 +
8658 +int pkg_name_version_and_architecture_compare(pkg_t *a, pkg_t *b)
8659 +{
8660 +     int namecmp;
8661 +     int vercmp;
8662 +     if (!a->name || !b->name) {
8663 +       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->name=%p b=%p b->name=%p\n",
8664 +              a, a->name, b, b->name);
8665 +       return 0;
8666 +     }
8667 +       
8668 +     namecmp = strcmp(a->name, b->name);
8669 +     if (namecmp)
8670 +         return namecmp;
8671 +     vercmp = pkg_compare_versions(a, b);
8672 +     if (vercmp)
8673 +         return vercmp;
8674 +     if (!a->arch_priority || !b->arch_priority) {
8675 +       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->arch_priority=%i b=%p b->arch_priority=%i\n",
8676 +              a, a->arch_priority, b, b->arch_priority);
8677 +       return 0;
8678 +     }
8679 +     if (a->arch_priority > b->arch_priority)
8680 +         return 1;
8681 +     if (a->arch_priority < b->arch_priority)
8682 +         return -1;
8683 +     return 0;
8684 +}
8685 +
8686 +int abstract_pkg_name_compare(abstract_pkg_t *a, abstract_pkg_t *b)
8687 +{
8688 +     if (!a->name || !b->name) {
8689 +       fprintf(stderr, "abstract_pkg_name_compare: a=%p a->name=%p b=%p b->name=%p\n",
8690 +              a, a->name, b, b->name);
8691 +       return 0;
8692 +     }
8693 +     return strcmp(a->name, b->name);
8694 +}
8695 +
8696 +
8697 +char *pkg_version_str_alloc(pkg_t *pkg)
8698 +{
8699 +     char *complete_version;
8700 +     char *epoch_str;
8701 +#ifdef USE_DEBVERSION
8702 +     char *revision_str;
8703 +     char *familiar_revision_str;
8704 +#endif
8705 +
8706 +     if (pkg->epoch) {
8707 +         sprintf_alloc(&epoch_str, "%d:", (int)(pkg->epoch));
8708 +     } else {
8709 +         epoch_str = strdup("");
8710 +     }
8711 +
8712 +#ifdef USE_DEBVERSION
8713 +     if (pkg->revision && strlen(pkg->revision)) {
8714 +         sprintf_alloc(&revision_str, "-%s", pkg->revision);
8715 +     } else {
8716 +         revision_str = strdup("");
8717 +     }
8718 +
8719 +     if (pkg->familiar_revision && strlen(pkg->familiar_revision)) {
8720 +         sprintf_alloc(&familiar_revision_str, "-fam%s", pkg->familiar_revision);
8721 +     } else {
8722 +         familiar_revision_str = strdup("");
8723 +     }
8724 +#endif
8725 +
8726 +#ifdef USE_DEBVERSION
8727 +     sprintf_alloc(&complete_version, "%s%s%s%s",
8728 +                  epoch_str, pkg->version, revision_str, familiar_revision_str);
8729 +#else
8730 +     sprintf_alloc(&complete_version, "%s%s",
8731 +                  epoch_str, pkg->version);
8732 +#endif
8733 +
8734 +     free(epoch_str);
8735 +#ifdef USE_DEBVERSION
8736 +     free(revision_str);
8737 +     free(familiar_revision_str);
8738 +#endif
8739 +
8740 +     return complete_version;
8741 +}
8742 +
8743 +str_list_t *pkg_get_installed_files(pkg_t *pkg)
8744 +{
8745 +     int err;
8746 +     char *list_file_name = NULL;
8747 +     FILE *list_file = NULL;
8748 +     char *line;
8749 +     char *installed_file_name;
8750 +     int rootdirlen;
8751 +
8752 +     pkg->installed_files_ref_cnt++;
8753 +
8754 +     if (pkg->installed_files) {
8755 +         return pkg->installed_files;
8756 +     }
8757 +
8758 +     pkg->installed_files = str_list_alloc();
8759 +     if (pkg->installed_files == NULL) {
8760 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8761 +         return NULL;
8762 +     }
8763 +
8764 +     /* For uninstalled packages, get the file list firectly from the package.
8765 +       For installed packages, look at the package.list file in the database.
8766 +     */
8767 +     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
8768 +         if (pkg->local_filename == NULL) {
8769 +              return pkg->installed_files;
8770 +         }
8771 +         /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
8772 +            file. In other words, change deb_extract so that it can
8773 +            simply return the file list as a char *[] rather than
8774 +            insisting on writing in to a FILE * as it does now. */
8775 +         list_file = tmpfile();
8776 +         err = pkg_extract_data_file_names_to_stream(pkg, list_file);
8777 +         if (err) {
8778 +              fclose(list_file);
8779 +              fprintf(stderr, "%s: Error extracting file list from %s: %s\n",
8780 +                      __FUNCTION__, pkg->local_filename, strerror(err));
8781 +              return pkg->installed_files;
8782 +         }
8783 +         rewind(list_file);
8784 +     } else {
8785 +         sprintf_alloc(&list_file_name, "%s/%s.list",
8786 +                       pkg->dest->info_dir, pkg->name);
8787 +         if (! file_exists(list_file_name)) {
8788 +              free(list_file_name);
8789 +              return pkg->installed_files;
8790 +         }
8791 +
8792 +         list_file = fopen(list_file_name, "r");
8793 +         if (list_file == NULL) {
8794 +              fprintf(stderr, "WARNING: Cannot open %s: %s\n",
8795 +                      list_file_name, strerror(errno));
8796 +              free(list_file_name);
8797 +              return pkg->installed_files;
8798 +         }
8799 +         free(list_file_name);
8800 +     }
8801 +
8802 +     rootdirlen = strlen( pkg->dest->root_dir );
8803 +     while (1) {
8804 +         char *file_name;
8805 +       
8806 +         line = file_read_line_alloc(list_file);
8807 +         if (line == NULL) {
8808 +              break;
8809 +         }
8810 +         str_chomp(line);
8811 +         file_name = line;
8812 +
8813 +         /* Take pains to avoid uglies like "/./" in the middle of file_name. */
8814 +         if( strncmp( pkg->dest->root_dir, 
8815 +                      file_name, 
8816 +                      rootdirlen ) ) {
8817 +              if (*file_name == '.') {
8818 +                   file_name++;
8819 +              }
8820 +              if (*file_name == '/') {
8821 +                   file_name++;
8822 +              }
8823 +
8824 +              /* Freed in pkg_free_installed_files */
8825 +              sprintf_alloc(&installed_file_name, "%s%s", pkg->dest->root_dir, file_name);
8826 +         } else {
8827 +              // already contains root_dir as header -> ABSOLUTE
8828 +              sprintf_alloc(&installed_file_name, "%s", file_name);
8829 +         }
8830 +         str_list_append(pkg->installed_files, installed_file_name);
8831 +         free(line);
8832 +     }
8833 +
8834 +     fclose(list_file);
8835 +
8836 +     return pkg->installed_files;
8837 +}
8838 +
8839 +/* XXX: CLEANUP: This function and it's counterpart,
8840 +   (pkg_get_installed_files), do not match our init/deinit naming
8841 +   convention. Nor the alloc/free convention. But, then again, neither
8842 +   of these conventions currrently fit the way these two functions
8843 +   work. */
8844 +int pkg_free_installed_files(pkg_t *pkg)
8845 +{
8846 +     str_list_elt_t *iter;
8847 +
8848 +     pkg->installed_files_ref_cnt--;
8849 +     if (pkg->installed_files_ref_cnt > 0) {
8850 +         return 0;
8851 +     }
8852 +
8853 +     if (pkg->installed_files) {
8854 +
8855 +         for (iter = pkg->installed_files->head; iter; iter = iter->next) {
8856 +              /* malloced in pkg_get_installed_files */
8857 +              free (iter->data);
8858 +              iter->data = NULL;
8859 +         }
8860 +
8861 +         str_list_deinit(pkg->installed_files);
8862 +     }
8863 +
8864 +     pkg->installed_files = NULL;
8865 +
8866 +     return 0;
8867 +}
8868 +
8869 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg)
8870 +{
8871 +     int err;
8872 +     char *list_file_name;
8873 +
8874 +     //I don't think pkg_free_installed_files should be called here. Jamey
8875 +     //pkg_free_installed_files(pkg);
8876 +
8877 +     sprintf_alloc(&list_file_name, "%s/%s.list",
8878 +                  pkg->dest->info_dir, pkg->name);
8879 +     if (!conf->noaction) {
8880 +         err = unlink(list_file_name);
8881 +         free(list_file_name);
8882 +
8883 +         if (err) {
8884 +              return errno;
8885 +         }
8886 +     }
8887 +     return 0;
8888 +}
8889 +
8890 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name)
8891 +{
8892 +     conffile_list_elt_t *iter;
8893 +     conffile_t *conffile;
8894 +
8895 +     if (pkg == NULL) {
8896 +         return NULL;
8897 +     }
8898 +
8899 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8900 +         conffile = iter->data;
8901 +
8902 +         if (strcmp(conffile->name, file_name) == 0) {
8903 +              return conffile;
8904 +         }
8905 +     }
8906 +
8907 +     return NULL;
8908 +}
8909 +
8910 +int pkg_run_script(ipkg_conf_t *conf, pkg_t *pkg,
8911 +                  const char *script, const char *args)
8912 +{
8913 +     int err;
8914 +     char *path;
8915 +     char *cmd;
8916 +
8917 +     /* XXX: FEATURE: When conf->offline_root is set, we should run the
8918 +       maintainer script within a chroot environment. */
8919 +
8920 +     /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
8921 +       have scripts in pkg->tmp_unpack_dir. */
8922 +     if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
8923 +         if (pkg->dest == NULL) {
8924 +              fprintf(stderr, "%s: ERROR: installed package %s has a NULL dest\n",
8925 +                      __FUNCTION__, pkg->name);
8926 +              return EINVAL;
8927 +         }
8928 +         sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, script);
8929 +     } else {
8930 +         if (pkg->tmp_unpack_dir == NULL) {
8931 +              fprintf(stderr, "%s: ERROR: uninstalled package %s has a NULL tmp_unpack_dir\n",
8932 +                      __FUNCTION__, pkg->name);
8933 +              return EINVAL;
8934 +         }
8935 +         sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script);
8936 +     }
8937 +
8938 +     ipkg_message(conf, IPKG_INFO, "Running script %s\n", path);
8939 +     if (conf->noaction) return 0;
8940 +
8941 +     /* XXX: CLEANUP: There must be a better way to handle maintainer
8942 +       scripts when running with offline_root mode and/or a dest other
8943 +       than '/'. I've been playing around with some clever chroot
8944 +       tricks and I might come up with something workable. */
8945 +     if (conf->offline_root) {
8946 +         setenv("IPKG_OFFLINE_ROOT", conf->offline_root, 1);
8947 +     }
8948 +
8949 +     setenv("PKG_ROOT",
8950 +           pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, 1);
8951 +
8952 +     if (! file_exists(path)) {
8953 +         free(path);
8954 +         return 0;
8955 +     }
8956 +
8957 +     if (conf->offline_root) {
8958 +         fprintf(stderr, "(offline root mode: not running %s.%s)\n", pkg->name, script);
8959 +         free(path);
8960 +         return 0;
8961 +     }
8962 +
8963 +     sprintf_alloc(&cmd, "%s %s", path, args);
8964 +     free(path);
8965 +
8966 +     err = xsystem(cmd);
8967 +     free(cmd);
8968 +
8969 +     if (err) {
8970 +         fprintf(stderr, "%s script returned status %d\n", script, err);
8971 +         return err;
8972 +     }
8973 +
8974 +     return 0;
8975 +}
8976 +
8977 +char *pkg_state_want_to_str(pkg_state_want_t sw)
8978 +{
8979 +     int i;
8980 +
8981 +     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
8982 +         if (pkg_state_want_map[i].value == sw) {
8983 +              return strdup(pkg_state_want_map[i].str);
8984 +         }
8985 +     }
8986 +
8987 +     fprintf(stderr, "%s: ERROR: Illegal value for state_want: %d\n",
8988 +            __FUNCTION__, sw);
8989 +     return strdup("<STATE_WANT_UNKNOWN>");
8990 +}
8991 +
8992 +pkg_state_want_t pkg_state_want_from_str(char *str)
8993 +{
8994 +     int i;
8995 +
8996 +     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
8997 +         if (strcmp(str, pkg_state_want_map[i].str) == 0) {
8998 +              return pkg_state_want_map[i].value;
8999 +         }
9000 +     }
9001 +
9002 +     fprintf(stderr, "%s: ERROR: Illegal value for state_want string: %s\n",
9003 +            __FUNCTION__, str);
9004 +     return SW_UNKNOWN;
9005 +}
9006 +
9007 +char *pkg_state_flag_to_str(pkg_state_flag_t sf)
9008 +{
9009 +     int i;
9010 +     int len = 3; /* ok\000 is minimum */
9011 +     char *str = NULL;
9012 +
9013 +     /* clear the temporary flags before converting to string */
9014 +     sf &= SF_NONVOLATILE_FLAGS;
9015 +
9016 +     if (sf == 0) {
9017 +         return strdup("ok");
9018 +     } else {
9019 +
9020 +         for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9021 +              if (sf & pkg_state_flag_map[i].value) {
9022 +                   len += strlen(pkg_state_flag_map[i].str) + 1;
9023 +              }
9024 +         }
9025 +         str = malloc(len);
9026 +          if ( str == NULL ) {
9027 +             fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9028 +              return NULL;
9029 +          }
9030 +         str[0] = 0;
9031 +         for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9032 +              if (sf & pkg_state_flag_map[i].value) {
9033 +                   strcat(str, pkg_state_flag_map[i].str);
9034 +                   strcat(str, ",");
9035 +              }
9036 +         }
9037 +         len = strlen(str);
9038 +         str[len-1] = 0; /* squash last comma */
9039 +         return str;
9040 +     }
9041 +}
9042 +
9043 +pkg_state_flag_t pkg_state_flag_from_str(char *str)
9044 +{
9045 +     int i;
9046 +     int sf = SF_OK;
9047 +
9048 +     if (strcmp(str, "ok") == 0) {
9049 +         return SF_OK;
9050 +     }
9051 +     for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9052 +         const char *sfname = pkg_state_flag_map[i].str;
9053 +         int sfname_len = strlen(sfname);
9054 +         if (strncmp(str, sfname, sfname_len) == 0) {
9055 +              sf |= pkg_state_flag_map[i].value;
9056 +              str += sfname_len;
9057 +              if (str[0] == ',') {
9058 +                   str++;
9059 +              } else {
9060 +                   break;
9061 +              }
9062 +         }
9063 +     }
9064 +
9065 +     return sf;
9066 +}
9067 +
9068 +char *pkg_state_status_to_str(pkg_state_status_t ss)
9069 +{
9070 +     int i;
9071 +
9072 +     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9073 +         if (pkg_state_status_map[i].value == ss) {
9074 +              return strdup(pkg_state_status_map[i].str);
9075 +         }
9076 +     }
9077 +
9078 +     fprintf(stderr, "%s: ERROR: Illegal value for state_status: %d\n",
9079 +            __FUNCTION__, ss);
9080 +     return strdup("<STATE_STATUS_UNKNOWN>");
9081 +}
9082 +
9083 +pkg_state_status_t pkg_state_status_from_str(char *str)
9084 +{
9085 +     int i;
9086 +
9087 +     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9088 +         if (strcmp(str, pkg_state_status_map[i].str) == 0) {
9089 +              return pkg_state_status_map[i].value;
9090 +         }
9091 +     }
9092 +
9093 +     fprintf(stderr, "%s: ERROR: Illegal value for state_status string: %s\n",
9094 +            __FUNCTION__, str);
9095 +     return SS_NOT_INSTALLED;
9096 +}
9097 +
9098 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg)
9099 +{
9100 +     nv_pair_list_elt_t *l;
9101 +
9102 +     if (!pkg->architecture)
9103 +         return 1;
9104 +
9105 +     l = conf->arch_list.head;
9106 +
9107 +     while (l) {
9108 +         nv_pair_t *nv = l->data;
9109 +         if (strcmp(nv->name, pkg->architecture) == 0) {
9110 +              ipkg_message(conf, IPKG_DEBUG, "arch %s (priority %s) supported for pkg %s\n", nv->name, nv->value, pkg->name);
9111 +              return 1;
9112 +         }
9113 +         l = l->next;
9114 +     }
9115 +
9116 +     ipkg_message(conf, IPKG_DEBUG, "arch %s unsupported for pkg %s\n", pkg->architecture, pkg->name);
9117 +     return 0;
9118 +}
9119 +
9120 +int pkg_get_arch_priority(ipkg_conf_t *conf, const char *archname)
9121 +{
9122 +     nv_pair_list_elt_t *l;
9123 +
9124 +     l = conf->arch_list.head;
9125 +
9126 +     while (l) {
9127 +         nv_pair_t *nv = l->data;
9128 +         if (strcmp(nv->name, archname) == 0) {
9129 +              int priority = strtol(nv->value, NULL, 0);
9130 +              return priority;
9131 +         }
9132 +         l = l->next;
9133 +     }
9134 +     return 0;
9135 +}
9136 +
9137 +int pkg_info_preinstall_check(ipkg_conf_t *conf)
9138 +{
9139 +     int i;
9140 +     hash_table_t *pkg_hash = &conf->pkg_hash;
9141 +     pkg_vec_t *available_pkgs = pkg_vec_alloc();
9142 +     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9143 +
9144 +     ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: updating arch priority for each package\n");
9145 +     pkg_hash_fetch_available(pkg_hash, available_pkgs);
9146 +     /* update arch_priority for each package */
9147 +     for (i = 0; i < available_pkgs->len; i++) {
9148 +         pkg_t *pkg = available_pkgs->pkgs[i];
9149 +         int arch_priority = 1;
9150 +         if (!pkg)
9151 +              continue;
9152 +         // ipkg_message(conf, IPKG_DEBUG2, " package %s version=%s arch=%p:", pkg->name, pkg->version, pkg->architecture);
9153 +         if (pkg->architecture) 
9154 +              arch_priority = pkg_get_arch_priority(conf, pkg->architecture);
9155 +         else 
9156 +              ipkg_message(conf, IPKG_ERROR, "pkg_info_preinstall_check: no architecture for package %s\n", pkg->name);
9157 +         // ipkg_message(conf, IPKG_DEBUG2, "%s arch_priority=%d\n", pkg->architecture, arch_priority);
9158 +         pkg->arch_priority = arch_priority;
9159 +     }
9160 +
9161 +     for (i = 0; i < available_pkgs->len; i++) {
9162 +         pkg_t *pkg = available_pkgs->pkgs[i];
9163 +         if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
9164 +              /* clear flags and want for any uninstallable package */
9165 +              ipkg_message(conf, IPKG_NOTICE, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n", 
9166 +                           pkg->name, pkg->arch_priority, pkg->state_flag, pkg->state_want);
9167 +              pkg->state_want = SW_UNKNOWN;
9168 +              pkg->state_flag = 0;
9169 +         }
9170 +     }
9171 +     pkg_vec_free(available_pkgs);
9172 +
9173 +     /* update the file owner data structure */
9174 +     ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: update file owner list\n");
9175 +     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9176 +     for (i = 0; i < installed_pkgs->len; i++) {
9177 +         pkg_t *pkg = installed_pkgs->pkgs[i];
9178 +         str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
9179 +         str_list_elt_t *iter;
9180 +         if (installed_files == NULL) {
9181 +              ipkg_message(conf, IPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
9182 +              break;
9183 +         }
9184 +         for (iter = installed_files->head; iter; iter = iter->next) {
9185 +              char *installed_file = iter->data;
9186 +              // ipkg_message(conf, IPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
9187 +              file_hash_set_file_owner(conf, installed_file, pkg);
9188 +         }
9189 +     }
9190 +     pkg_vec_free(installed_pkgs);
9191 +
9192 +     return 0;
9193 +}
9194 +
9195 +struct pkg_write_filelist_data {
9196 +     ipkg_conf_t *conf;
9197 +     pkg_t *pkg;
9198 +     FILE *stream;
9199 +};
9200 +
9201 +void pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
9202 +{
9203 +     struct pkg_write_filelist_data *data = data_;
9204 +     pkg_t *entry = entry_;
9205 +     if (entry == data->pkg) {
9206 +         fprintf(data->stream, "%s\n", key);
9207 +     }
9208 +}
9209 +
9210 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg)
9211 +{
9212 +     struct pkg_write_filelist_data data;
9213 +     char *list_file_name = NULL;
9214 +     int err = 0;
9215 +
9216 +     if (!pkg) {
9217 +         ipkg_message(conf, IPKG_ERROR, "Null pkg\n");
9218 +         return -EINVAL;
9219 +     }
9220 +     ipkg_message(conf, IPKG_INFO,
9221 +                 "    creating %s.list file\n", pkg->name);
9222 +     sprintf_alloc(&list_file_name, "%s/%s.list", pkg->dest->info_dir, pkg->name);
9223 +     if (!list_file_name) {
9224 +         ipkg_message(conf, IPKG_ERROR, "Failed to alloc list_file_name\n");
9225 +         return -ENOMEM;
9226 +     }
9227 +     ipkg_message(conf, IPKG_INFO,
9228 +                 "    creating %s file for pkg %s\n", list_file_name, pkg->name);
9229 +     data.stream = fopen(list_file_name, "w");
9230 +     if (!data.stream) {
9231 +         ipkg_message(conf, IPKG_ERROR, "Could not open %s for writing: %s\n",
9232 +                      list_file_name, strerror(errno));
9233 +                      return errno;
9234 +     }
9235 +     data.pkg = pkg;
9236 +     data.conf = conf;
9237 +     hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
9238 +     fclose(data.stream);
9239 +     free(list_file_name);
9240 +
9241 +     return err;
9242 +}
9243 +
9244 +int pkg_write_changed_filelists(ipkg_conf_t *conf)
9245 +{
9246 +     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9247 +     hash_table_t *pkg_hash = &conf->pkg_hash;
9248 +     int i;
9249 +     int err;
9250 +     if (conf->noaction)
9251 +         return 0;
9252 +
9253 +     ipkg_message(conf, IPKG_INFO, "%s: saving changed filelists\n", __FUNCTION__);
9254 +     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9255 +     for (i = 0; i < installed_pkgs->len; i++) {
9256 +         pkg_t *pkg = installed_pkgs->pkgs[i];
9257 +         if (pkg->state_flag & SF_FILELIST_CHANGED) {
9258 +               ipkg_message(conf, IPKG_DEBUG, "Calling pkg_write_filelist for pkg=%s from %s\n", pkg->name, __FUNCTION__);
9259 +              err = pkg_write_filelist(conf, pkg);
9260 +              if (err)
9261 +                   ipkg_message(conf, IPKG_NOTICE, "pkg_write_filelist pkg=%s returned %d\n", pkg->name, err);
9262 +         }
9263 +     }
9264 +     return 0;
9265 +}
9266 diff -urN busybox.old/archival/libipkg/pkg_depends.c busybox.dev/archival/libipkg/pkg_depends.c
9267 --- busybox.old/archival/libipkg/pkg_depends.c  1970-01-01 01:00:00.000000000 +0100
9268 +++ busybox.dev/archival/libipkg/pkg_depends.c  2007-01-22 13:41:06.000000000 +0100
9269 @@ -0,0 +1,1031 @@
9270 +/* pkg_depends.c - the itsy package management system
9271 +
9272 +   Steven M. Ayer
9273 +   
9274 +   Copyright (C) 2002 Compaq Computer Corporation
9275 +
9276 +   This program is free software; you can redistribute it and/or
9277 +   modify it under the terms of the GNU General Public License as
9278 +   published by the Free Software Foundation; either version 2, or (at
9279 +   your option) any later version.
9280 +
9281 +   This program is distributed in the hope that it will be useful, but
9282 +   WITHOUT ANY WARRANTY; without even the implied warranty of
9283 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9284 +   General Public License for more details.
9285 +*/
9286 +
9287 +#include "ipkg.h"
9288 +#include <errno.h>
9289 +#include <ctype.h>
9290 +   
9291 +#include "pkg.h"
9292 +#include "ipkg_utils.h"
9293 +#include "pkg_hash.h"
9294 +#include "ipkg_message.h"
9295 +#include "pkg_parse.h"
9296 +#include "hash_table.h"
9297 +
9298 +static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str);
9299 +static depend_t * depend_init(void);
9300 +static void depend_deinit(depend_t *d);
9301 +static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
9302 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
9303 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
9304 +
9305 +static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
9306 +{
9307 +     depend_t *depend = (depend_t *)cdata;
9308 +     if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
9309 +         return 1;
9310 +     else
9311 +         return 0;
9312 +}
9313 +
9314 +static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
9315 +{
9316 +     depend_t *depend = (depend_t *)cdata;
9317 +#if 0
9318 +     pkg_t * temp = pkg_new();
9319 +     int comparison;
9320 +     parseVersion(temp, depend->version);
9321 +     comparison = pkg_compare_versions(pkg, temp);
9322 +     free(temp);
9323 +
9324 +     fprintf(stderr, "%s: pkg=%s pkg->version=%s constraint=%p type=%d version=%s comparison=%d satisfied=%d\n", 
9325 +            __FUNCTION__, pkg->name, pkg->version, 
9326 +            depend, depend->constraint, depend->version,
9327 +            comparison, version_constraints_satisfied(depend, pkg));
9328 +#endif
9329 +     if (version_constraints_satisfied(depend, pkg))
9330 +         return 1;
9331 +     else
9332 +         return 0;
9333 +}
9334 +
9335 +/* returns ndependences or negative error value */ 
9336 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, 
9337 +                                           pkg_vec_t *unsatisfied, char *** unresolved)
9338 +{
9339 +     pkg_t * satisfier_entry_pkg;
9340 +     register int i, j, k, l;
9341 +     int count, found;
9342 +     char ** the_lost;
9343 +     abstract_pkg_t * ab_pkg;
9344 +
9345 +     /* 
9346 +      * this is a setup to check for redundant/cyclic dependency checks, 
9347 +      * which are marked at the abstract_pkg level
9348 +      */
9349 +     if (!(ab_pkg = pkg->parent)) {
9350 +         fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
9351 +         *unresolved = NULL;
9352 +         return 0;
9353 +     }
9354 +     if (ab_pkg->dependencies_checked) {    /* avoid duplicate or cyclic checks */
9355 +         *unresolved = NULL;
9356 +         return 0;
9357 +     } else { 
9358 +         ab_pkg->dependencies_checked = 1;  /* mark it for subsequent visits */
9359 +     }
9360 +     /**/
9361 +
9362 +     count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
9363 +     if (!count){
9364 +         *unresolved = NULL;
9365 +         return 0;
9366 +     }
9367 +
9368 +     the_lost = NULL;
9369 +       
9370 +     /* foreach dependency */
9371 +     for (i = 0; i < count; i++) {
9372 +         compound_depend_t * compound_depend = &pkg->depends[i];
9373 +         depend_t ** possible_satisfiers = compound_depend->possibilities;;
9374 +         found = 0;
9375 +         satisfier_entry_pkg = NULL;
9376 +
9377 +         if (compound_depend->type == GREEDY_DEPEND) {
9378 +              /* foreach possible satisfier */
9379 +              for (j = 0; j < compound_depend->possibility_count; j++) {
9380 +                   /* foreach provided_by, which includes the abstract_pkg itself */
9381 +                   abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
9382 +                   abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
9383 +                   int nposs = ab_provider_vec->len;
9384 +                   abstract_pkg_t **ab_providers = ab_provider_vec->pkgs; 
9385 +                   for (l = 0; l < nposs; l++) {
9386 +                        pkg_vec_t *test_vec = ab_providers[l]->pkgs;
9387 +                        /* if no depends on this one, try the first package that Provides this one */
9388 +                        if (!test_vec){   /* no pkg_vec hooked up to the abstract_pkg!  (need another feed?) */
9389 +                             continue;
9390 +                        }
9391 +             
9392 +                        /* cruise this possiblity's pkg_vec looking for an installed version */
9393 +                        for (k = 0; k < test_vec->len; k++) {
9394 +                             pkg_t *pkg_scout = test_vec->pkgs[k];
9395 +                             /* not installed, and not already known about? */
9396 +                             if ((pkg_scout->state_want != SW_INSTALL)
9397 +                                 && !pkg_scout->parent->dependencies_checked
9398 +                                 && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
9399 +                                  char ** newstuff = NULL;
9400 +                                  int rc;
9401 +                                  pkg_vec_t *tmp_vec = pkg_vec_alloc ();
9402 +                                  /* check for not-already-installed dependencies */
9403 +                                  rc = pkg_hash_fetch_unsatisfied_dependencies(conf, 
9404 +                                                                               pkg_scout, 
9405 +                                                                               tmp_vec,
9406 +                                                                               &newstuff);
9407 +                                  if (newstuff == NULL) {
9408 +                                       int ok = 1;
9409 +                                       for (l = 0; l < rc; l++) {
9410 +                                           pkg_t *p = tmp_vec->pkgs[l];
9411 +                                           if (p->state_want == SW_INSTALL)
9412 +                                               continue;
9413 +                                           ipkg_message(conf, IPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
9414 +                                           ok = 0;
9415 +                                           break;
9416 +                                       }
9417 +                                       pkg_vec_free (tmp_vec);
9418 +                                       if (ok) {
9419 +                                           /* mark this one for installation */
9420 +                                           ipkg_message(conf, IPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
9421 +                                           pkg_vec_insert(unsatisfied, pkg_scout);
9422 +                                       }
9423 +                                  } else  {
9424 +                                       ipkg_message(conf, IPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
9425 +                                       free (newstuff);
9426 +                                  }
9427 +                             }
9428 +                        }
9429 +                   }
9430 +              }
9431 +
9432 +              continue;
9433 +         }
9434 +
9435 +         /* foreach possible satisfier, look for installed package  */
9436 +         for (j = 0; j < compound_depend->possibility_count; j++) {
9437 +              /* foreach provided_by, which includes the abstract_pkg itself */
9438 +              depend_t *dependence_to_satisfy = possible_satisfiers[j];
9439 +              abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9440 +              pkg_t *satisfying_pkg = 
9441 +                   pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
9442 +                                                              pkg_installed_and_constraint_satisfied, 
9443 +                                                              dependence_to_satisfy, 1);
9444 +               /* Being that I can't test constraing in pkg_hash, I will test it here */
9445 +              if (satisfying_pkg != NULL) {
9446 +                  if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9447 +                     satisfying_pkg = NULL;
9448 +                  }
9449 +               }
9450 +              ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
9451 +              if (satisfying_pkg != NULL) {
9452 +                   found = 1;
9453 +                   break;
9454 +              }
9455 +
9456 +         }
9457 +         /* if nothing installed matches, then look for uninstalled satisfier */
9458 +         if (!found) {
9459 +              /* foreach possible satisfier, look for installed package  */
9460 +              for (j = 0; j < compound_depend->possibility_count; j++) {
9461 +                   /* foreach provided_by, which includes the abstract_pkg itself */
9462 +                   depend_t *dependence_to_satisfy = possible_satisfiers[j];
9463 +                   abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9464 +                   pkg_t *satisfying_pkg = 
9465 +                        pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
9466 +                                                                   pkg_constraint_satisfied, 
9467 +                                                                   dependence_to_satisfy, 1);
9468 +                    /* Being that I can't test constraing in pkg_hash, I will test it here too */
9469 +                   if (satisfying_pkg != NULL) {
9470 +                         if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9471 +                            satisfying_pkg = NULL;
9472 +                         }
9473 +                    }
9474 +
9475 +                   /* user request overrides package recommendation */
9476 +                   if (satisfying_pkg != NULL
9477 +                       && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
9478 +                       && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
9479 +                        ipkg_message (conf, IPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
9480 +                                      pkg->name, satisfying_pkg->name);
9481 +                        continue;
9482 +                   }
9483 +
9484 +                   ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
9485 +                   if (satisfying_pkg != NULL) {
9486 +                        satisfier_entry_pkg = satisfying_pkg;
9487 +                        break;
9488 +                   }
9489 +              }
9490 +         }
9491 +
9492 +         /* we didn't find one, add something to the unsatisfied vector */
9493 +         if (!found) {
9494 +              if (!satisfier_entry_pkg) {
9495 +                   /* failure to meet recommendations is not an error */
9496 +                   if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
9497 +                        the_lost = add_unresolved_dep(pkg, the_lost, i);
9498 +                   else
9499 +                        ipkg_message (conf, IPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
9500 +                                      pkg->name, compound_depend->possibilities[0]->pkg->name);
9501 +              }
9502 +              else {
9503 +                   if (compound_depend->type == SUGGEST) {
9504 +                        /* just mention it politely */
9505 +                        ipkg_message (conf, IPKG_NOTICE, "package %s suggests installing %s\n",
9506 +                                      pkg->name, satisfier_entry_pkg->name);
9507 +                   } else {
9508 +                        char ** newstuff = NULL;
9509 +                        
9510 +                        if (satisfier_entry_pkg != pkg &&
9511 +                            !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
9512 +                             pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
9513 +                             pkg_hash_fetch_unsatisfied_dependencies(conf, 
9514 +                                                                     satisfier_entry_pkg, 
9515 +                                                                     unsatisfied,
9516 +                                                                     &newstuff);
9517 +                             the_lost = merge_unresolved(the_lost, newstuff);
9518 +                        }
9519 +                   }
9520 +              }
9521 +         }
9522 +     }
9523 +     *unresolved = the_lost;
9524 +
9525 +     return unsatisfied->len;
9526 +}
9527 +
9528 +/*checking for conflicts !in replaces 
9529 +  If a packages conflicts with another but is also replacing it, I should not consider it a 
9530 +  really conflicts 
9531 +  returns 0 if conflicts <> replaces or 1 if conflicts == replaces 
9532 +*/
9533 +int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
9534 +{
9535 +    int i ;
9536 +    int replaces_count = pkg->replaces_count;
9537 +    abstract_pkg_t **replaces;
9538 +
9539 +    if (pkg->replaces_count==0)    // No replaces, it's surely a conflict
9540 +        return 0;
9541 +
9542 +    replaces = pkg->replaces;
9543 +
9544 +    for (i = 0; i < replaces_count; i++) {
9545 +        if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) {      // Found
9546 +            ipkg_message(NULL, IPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
9547 +            return 1;
9548 +        }
9549 +    }
9550 +    return 0;
9551 +
9552 +}
9553 +
9554 +
9555 +/* Abhaya: added support for conflicts */
9556 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
9557 +{
9558 +    pkg_vec_t * installed_conflicts, * test_vec;
9559 +    compound_depend_t * conflicts;
9560 +    depend_t ** possible_satisfiers;
9561 +    depend_t * possible_satisfier;
9562 +    register int i, j, k;
9563 +    int count;
9564 +    abstract_pkg_t * ab_pkg;
9565 +    pkg_t **pkg_scouts; 
9566 +    pkg_t *pkg_scout; 
9567 +
9568 +    /* 
9569 +     * this is a setup to check for redundant/cyclic dependency checks, 
9570 +     * which are marked at the abstract_pkg level
9571 +     */
9572 +    if(!(ab_pkg = pkg->parent)){
9573 +       fprintf(stderr, "dependency check error.  pkg %s isn't in hash table\n", pkg->name);
9574 +       return (pkg_vec_t *)NULL;
9575 +    }
9576 +
9577 +    conflicts = pkg->conflicts;
9578 +    if(!conflicts){
9579 +       return (pkg_vec_t *)NULL;
9580 +    }
9581 +    installed_conflicts = pkg_vec_alloc();
9582 +
9583 +    count = pkg->conflicts_count;
9584 +
9585 +
9586 +
9587 +    /* foreach conflict */
9588 +    for(i = 0; i < pkg->conflicts_count; i++){
9589 +
9590 +       possible_satisfiers = conflicts->possibilities;
9591 +
9592 +       /* foreach possible satisfier */
9593 +       for(j = 0; j < conflicts->possibility_count; j++){
9594 +            possible_satisfier = possible_satisfiers[j];
9595 +            if (!possible_satisfier)
9596 +                fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
9597 +            if (!possible_satisfier->pkg)
9598 +                fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
9599 +           test_vec = possible_satisfier->pkg->pkgs;
9600 +           if (test_vec) {
9601 +                /* pkg_vec found, it is an actual package conflict
9602 +                * cruise this possiblity's pkg_vec looking for an installed version */
9603 +               pkg_scouts = test_vec->pkgs;
9604 +               for(k = 0; k < test_vec->len; k++){
9605 +                    pkg_scout = pkg_scouts[k];
9606 +                    if (!pkg_scout) {
9607 +                        fprintf(stderr,  "%s: null pkg scout\n", __FUNCTION__);
9608 +                        continue; 
9609 +                    }
9610 +                   if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
9611 +                      version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
9612 +                       if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
9613 +                           pkg_vec_insert(installed_conflicts, pkg_scout);
9614 +                       }
9615 +                   }
9616 +               }
9617 +           }
9618 +       }
9619 +       conflicts++;
9620 +    }
9621 +
9622 +    if (installed_conflicts->len)
9623 +           return installed_conflicts;
9624 +    pkg_vec_free(installed_conflicts);
9625 +       return (pkg_vec_t *)NULL;
9626 +}
9627 +
9628 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
9629 +{
9630 +    pkg_t * temp;
9631 +    int comparison;
9632 +
9633 +    if(depends->constraint == NONE)
9634 +       return 1;
9635 +
9636 +    temp = pkg_new();
9637 +
9638 +    parseVersion(temp, depends->version);
9639 +
9640 +    comparison = pkg_compare_versions(pkg, temp);
9641 +
9642 +    free(temp);
9643 +
9644 +    if((depends->constraint == EARLIER) && 
9645 +       (comparison < 0))
9646 +       return 1;
9647 +    else if((depends->constraint == LATER) && 
9648 +           (comparison > 0))
9649 +       return 1;
9650 +    else if(comparison == 0)
9651 +       return 1;
9652 +    else if((depends->constraint == LATER_EQUAL) && 
9653 +           (comparison >= 0))
9654 +       return 1;
9655 +    else if((depends->constraint == EARLIER_EQUAL) && 
9656 +           (comparison <= 0))
9657 +       return 1;
9658 +    
9659 +    return 0;
9660 +}
9661 +
9662 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend)
9663 +{
9664 +     abstract_pkg_t *apkg = depend->pkg;
9665 +     abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9666 +     int n_providers = provider_apkgs->len;
9667 +     abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9668 +     pkg_vec_t *pkg_vec;
9669 +     int n_pkgs ; 
9670 +     int i;
9671 +     int j;
9672 +
9673 +     for (i = 0; i < n_providers; i++) {
9674 +         abstract_pkg_t *papkg = apkgs[i];
9675 +         pkg_vec = papkg->pkgs;
9676 +         if (pkg_vec) {
9677 +              n_pkgs = pkg_vec->len;
9678 +              for (j = 0; j < n_pkgs; j++) {
9679 +                   pkg_t *pkg = pkg_vec->pkgs[j];
9680 +                   if (version_constraints_satisfied(depend, pkg)) {
9681 +                        return 1;
9682 +                   }
9683 +              }
9684 +         }
9685 +     }
9686 +     return 0;
9687 +}
9688 +
9689 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend)
9690 +{
9691 +     abstract_pkg_t *apkg = depend->pkg;
9692 +     abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9693 +     int n_providers = provider_apkgs->len;
9694 +     abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9695 +     int i;
9696 +     int n_pkgs;
9697 +     int j;
9698 +
9699 +     for (i = 0; i < n_providers; i++) {
9700 +         abstract_pkg_t *papkg = apkgs[i];
9701 +         pkg_vec_t *pkg_vec = papkg->pkgs;
9702 +         if (pkg_vec) {
9703 +              n_pkgs = pkg_vec->len;
9704 +              for (j = 0; j < n_pkgs; j++) {
9705 +                   pkg_t *pkg = pkg_vec->pkgs[j];
9706 +                   if (version_constraints_satisfied(depend, pkg)) {
9707 +                        if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
9708 +                             return 1;
9709 +                   }
9710 +              }
9711 +         }
9712 +     }
9713 +     return 0;
9714 +}
9715 +
9716 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
9717 +{
9718 +    register int i;
9719 +    pkg_t ** pkgs = vec->pkgs;
9720 +
9721 +    for(i = 0; i < vec->len; i++)
9722 +       if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
9723 +          && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
9724 +          && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
9725 +           return 1;
9726 +    return 0;
9727 +}
9728 +
9729 +
9730 +#ifdef DeadCode
9731 +/**
9732 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
9733 + * the same abstract package and 0 otherwise.
9734 + */
9735 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
9736 +{
9737 +     abstract_pkg_t **provides = pkg->provides;
9738 +     int provides_count = pkg->provides_count;
9739 +     abstract_pkg_t **replacee_provides = replacee->provides;
9740 +     int replacee_provides_count = replacee->provides_count;
9741 +     int i, j;
9742 +     for (i = 0; i < provides_count; i++) {
9743 +         abstract_pkg_t *apkg = provides[i];
9744 +         for (j = 0; j < replacee_provides_count; j++) {
9745 +              abstract_pkg_t *replacee_apkg = replacee_provides[i];
9746 +              if (apkg == replacee_apkg)
9747 +                   return 1;
9748 +         }
9749 +     }
9750 +     return 0;
9751 +}
9752 +#endif
9753 +
9754 +/**
9755 + * pkg_provides_abstract returns 1 if pkg->provides contains providee
9756 + * and 0 otherwise.
9757 + */
9758 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
9759 +{
9760 +     abstract_pkg_t **provides = pkg->provides;
9761 +     int provides_count = pkg->provides_count;
9762 +     int i;
9763 +     for (i = 0; i < provides_count; i++) {
9764 +         if (provides[i] == providee)
9765 +              return 1;
9766 +     }
9767 +     return 0;
9768 +}
9769 +
9770 +/**
9771 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
9772 + * otherwise.
9773 + */
9774 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
9775 +{
9776 +     abstract_pkg_t **replaces = pkg->replaces;
9777 +     int replaces_count = pkg->replaces_count;
9778 +     /* abstract_pkg_t **replacee_provides = pkg->provides;
9779 +     int replacee_provides_count = pkg->provides_count; */
9780 +     int i, j;
9781 +     for (i = 0; i < replaces_count; i++) {
9782 +         abstract_pkg_t *abstract_replacee = replaces[i];
9783 +         for (j = 0; j < replaces_count; j++) {
9784 +   /*            ipkg_message(NULL, IPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
9785 +                 pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
9786 +              if (replacee->provides[j] == abstract_replacee)
9787 +                   return 1;
9788 +         }
9789 +     }
9790 +     return 0;
9791 +}
9792 +
9793 +
9794 +/**
9795 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
9796 + * otherwise.
9797 + */
9798 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
9799 +{
9800 +     compound_depend_t *conflicts = pkg->conflicts;
9801 +     int conflicts_count = pkg->conflicts_count;
9802 +     int i, j;
9803 +     for (i = 0; i < conflicts_count; i++) {
9804 +         int possibility_count = conflicts[i].possibility_count;
9805 +         struct depend **possibilities = conflicts[i].possibilities;
9806 +         for (j = 0; j < possibility_count; j++) {
9807 +              if (possibilities[j]->pkg == conflictee) {
9808 +                   return 1;
9809 +              }
9810 +         }
9811 +     }
9812 +     return 0;
9813 +}
9814 +
9815 +/**
9816 + * pkg_conflicts returns 1 if pkg->conflicts contains one of
9817 + * conflictee's provides and 0 otherwise.
9818 + */
9819 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
9820 +{
9821 +     compound_depend_t *conflicts = pkg->conflicts;
9822 +     int conflicts_count = pkg->conflicts_count;
9823 +     abstract_pkg_t **conflictee_provides = conflictee->provides;
9824 +     int conflictee_provides_count = conflictee->provides_count;
9825 +     int i, j, k;
9826 +     int possibility_count;
9827 +     struct depend **possibilities;
9828 +     abstract_pkg_t *possibility ;
9829 +
9830 +     for (i = 0; i < conflicts_count; i++) {
9831 +         possibility_count = conflicts[i].possibility_count;
9832 +         possibilities = conflicts[i].possibilities;
9833 +         for (j = 0; j < possibility_count; j++) {
9834 +              possibility = possibilities[j]->pkg;
9835 +              for (k = 0; k < conflictee_provides_count; k++) {
9836 +                   if (possibility == conflictee_provides[k]) {
9837 +                        return 1;
9838 +                   }
9839 +              }
9840 +         }
9841 +     }
9842 +     return 0;
9843 +}
9844 +
9845 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
9846 +{
9847 +    int oldlen = 0, newlen = 0;
9848 +    char ** result;
9849 +    register int i, j;
9850 +
9851 +    if(!newstuff)
9852 +       return oldstuff;
9853 +    
9854 +    while(oldstuff && oldstuff[oldlen]) oldlen++;
9855 +    while(newstuff && newstuff[newlen]) newlen++;
9856 +    
9857 +    result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
9858 +    if (result == NULL) {
9859 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9860 +        return NULL;
9861 +    }
9862 +    
9863 +    for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
9864 +       *(result + i) = *(newstuff + j);
9865 +    
9866 +    *(result + i) = NULL;
9867 +
9868 +    return result;
9869 +}
9870 +    
9871 +/* 
9872 + * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
9873 + * this is null terminated, no count is carried around 
9874 + */
9875 +char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
9876 +{
9877 +    int count;
9878 +    char ** resized;
9879 +    char *depend_str = pkg_depend_str(pkg, ref_ndx);
9880 +
9881 +    count = 0;
9882 +    while(the_lost && the_lost[count]) count++;
9883 +
9884 +    count++;  /* need one to hold the null */
9885 +    resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
9886 +    if (resized == NULL) {
9887 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9888 +       return NULL;
9889 +    }
9890 +    resized[count - 1] = strdup(depend_str);
9891 +    resized[count] = NULL;
9892 +    
9893 +    return resized;
9894 +}
9895 +       
9896 +void printDepends(pkg_t * pkg)
9897 +{
9898 +    register int i, j;
9899 +    compound_depend_t * depend;
9900 +    int count;
9901 +    
9902 +    count = pkg->pre_depends_count + pkg->depends_count;
9903 +    
9904 +    depend = pkg->depends;
9905 +    if(!depend){
9906 +       fprintf(stderr, "Depends pointer is NULL\n");
9907 +       return;
9908 +    }
9909 +    for(i = 0; i < count; i++){
9910 +       fprintf(stderr, "%s has %d possibilities:\n", 
9911 +               (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
9912 +               depend->possibility_count);
9913 +       for(j = 0; j < depend->possibility_count; j++)
9914 +           fprintf(stderr, "\t%s version %s (%d)\n",
9915 +                   depend->possibilities[j]->pkg->name,
9916 +                   depend->possibilities[j]->version,
9917 +                   depend->possibilities[j]->constraint);
9918 +       depend++;
9919 +    }
9920 +}
9921 +
9922 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
9923 +{
9924 +    register int i, j;
9925 +
9926 +    /* every pkg provides itself */
9927 +    abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
9928 +
9929 +    if (!pkg->provides_count)
9930 +      return 0;
9931 +
9932 +    pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1));
9933 +    if (pkg->provides == NULL) {
9934 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9935 +       return -1 ;
9936 +    }
9937 +    pkg->provides[0] = ab_pkg;
9938 +
9939 +    // if (strcmp(ab_pkg->name, pkg->name))
9940 +    //     fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
9941 +
9942 +    for(i = 0; i < pkg->provides_count; i++){
9943 +        abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
9944 +
9945 +       pkg->provides[i+1] = provided_abpkg;
9946 +
9947 +       j = 0;
9948 +       abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
9949 +    }
9950 +    return 0;
9951 +}
9952 +
9953 +/* Abhaya: added conflicts support */
9954 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
9955 +{
9956 +    register int i;
9957 +    compound_depend_t * conflicts;
9958 +
9959 +    if (!pkg->conflicts_count)
9960 +       return 0;
9961 +
9962 +    conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) *
9963 +                                       pkg->conflicts_count);
9964 +    if (conflicts == NULL) {
9965 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9966 +       return  -1;
9967 +    }
9968 +    for (i = 0; i < pkg->conflicts_count; i++) {
9969 +        conflicts->type = CONFLICTS;
9970 +        parseDepends(conflicts, hash,
9971 +                     pkg->conflicts_str[i]);
9972 +#if 0
9973 +        for (j = 0; j < conflicts->possibility_count; j++) {
9974 +             depend_t *possibility = conflicts->possibilities[j];
9975 +             abstract_pkg_t *conflicting_apkg = possibility->pkg;
9976 +             pkg_add_conflict_pair(ab_pkg, conflicting_apkg);
9977 +        }
9978 +#endif
9979 +        conflicts++;
9980 +    }
9981 +    return 0;
9982 +}
9983 +
9984 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
9985 +{
9986 +     register int i, j;
9987 +
9988 +     if (!pkg->replaces_count)
9989 +         return 0;
9990 +
9991 +     pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count);
9992 +     if (pkg->replaces == NULL) {
9993 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9994 +        return  -1;
9995 +     }
9996 +
9997 +     // if (strcmp(ab_pkg->name, pkg->name))
9998 +     //     fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
9999 +
10000 +     for(i = 0; i < pkg->replaces_count; i++){
10001 +         abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
10002 +
10003 +         pkg->replaces[i] = old_abpkg;
10004 +
10005 +         j = 0;
10006 +         if (!old_abpkg->replaced_by)
10007 +              old_abpkg->replaced_by = abstract_pkg_vec_alloc();
10008 +               if ( old_abpkg->replaced_by == NULL ){
10009 +                  return -1;
10010 +               }
10011 +         /* if a package pkg both replaces and conflicts old_abpkg,
10012 +          * then add it to the replaced_by vector so that old_abpkg
10013 +          * will be upgraded to ab_pkg automatically */
10014 +         if (pkg_conflicts_abstract(pkg, old_abpkg))
10015 +              abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
10016 +     }
10017 +     return 0;
10018 +}
10019 +
10020 +int buildDepends(hash_table_t * hash, pkg_t * pkg)
10021 +{
10022 +     int count;
10023 +     register int i;
10024 +     compound_depend_t * depends;
10025 +
10026 +     if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
10027 +         return 0;
10028 +
10029 +     if (0 && pkg->pre_depends_count)
10030 +         fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", 
10031 +                 pkg->name, pkg->pre_depends_count, pkg->depends_count);
10032 +     depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
10033 +     if (depends == NULL) {
10034 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10035 +        return  -1;
10036 +     }
10037 +     
10038 +
10039 +     for(i = 0; i < pkg->pre_depends_count; i++){
10040 +         parseDepends(depends, hash, pkg->pre_depends_str[i]);
10041 +         if (0 && pkg->pre_depends_count)
10042 +              fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n", 
10043 +                      pkg->pre_depends_str[i], depends, depends->possibility_count);
10044 +         depends->type = PREDEPEND;
10045 +         depends++;
10046 +     }
10047 +
10048 +     for(i = 0; i < pkg->recommends_count; i++){
10049 +         parseDepends(depends, hash, pkg->recommends_str[i]);
10050 +         if (0 && pkg->recommends_count)
10051 +              fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n", 
10052 +                      pkg->recommends_str[i], depends, depends->possibility_count);
10053 +         depends->type = RECOMMEND;
10054 +         depends++;
10055 +     }
10056 +
10057 +     for(i = 0; i < pkg->suggests_count; i++){
10058 +         parseDepends(depends, hash, pkg->suggests_str[i]);
10059 +         if (0 && pkg->suggests_count)
10060 +              fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n", 
10061 +                      pkg->suggests_str[i], depends, depends->possibility_count);
10062 +         depends->type = SUGGEST;
10063 +         depends++;
10064 +     }
10065 +
10066 +     for(i = 0; i < pkg->depends_count; i++){
10067 +         parseDepends(depends, hash, pkg->depends_str[i]);
10068 +         if (0 && pkg->depends_count)
10069 +              fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
10070 +                      pkg->depends_str[i], depends, depends->possibility_count);
10071 +         depends++;
10072 +     }
10073 +     return 0;
10074 +}    
10075 +
10076 +/*
10077 + * pkg_depend_string: returns the depends string specified by index.
10078 + *   All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
10079 + *   [0,npredepends) -> returns pre_depends_str[index]
10080 + *   [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
10081 + *   [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
10082 + *   [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
10083 + */
10084 +char *pkg_depend_str(pkg_t *pkg, int pkg_index)
10085 +{
10086 +     if (pkg_index < pkg->pre_depends_count) {
10087 +         return pkg->pre_depends_str[pkg_index];
10088 +     }
10089 +     pkg_index -= pkg->pre_depends_count;
10090 +
10091 +     if (pkg_index < pkg->recommends_count) {
10092 +         return pkg->recommends_str[pkg_index];
10093 +     }
10094 +     pkg_index -= pkg->recommends_count;
10095 +
10096 +     if (pkg_index < pkg->suggests_count) {
10097 +         return pkg->suggests_str[pkg_index];
10098 +     }
10099 +     pkg_index -= pkg->suggests_count;
10100 +
10101 +     if (pkg_index < pkg->depends_count) {
10102 +         return pkg->depends_str[pkg_index];
10103 +     }
10104 +     fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", pkg_index, pkg->name);
10105 +     return NULL;
10106 +}
10107 +
10108 +void freeDepends(pkg_t *pkg)
10109 +{
10110 +    int i;
10111 +
10112 +    if (pkg == NULL || pkg->depends == NULL) {
10113 +       return;
10114 +    }
10115 +
10116 +    fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
10117 +    for (i=0; i < pkg->depends->possibility_count; i++) {
10118 +       depend_deinit(pkg->depends->possibilities[i]);
10119 +    }
10120 +    free(pkg->depends->possibilities);
10121 +    free(pkg->depends);
10122 +    pkg->depends = NULL;
10123 +}
10124 +
10125 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
10126 +{
10127 +     compound_depend_t * depends;
10128 +     int count, othercount;
10129 +     register int i, j;
10130 +     abstract_pkg_t * ab_depend;
10131 +     abstract_pkg_t ** temp;
10132 +
10133 +     count = pkg->pre_depends_count + pkg->depends_count;
10134 +     depends = pkg->depends;
10135 +
10136 +         if (0 && pkg->pre_depends_count)
10137 +              fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
10138 +                      pkg->name, pkg->pre_depends_count, pkg->depends_count);
10139 +     for (i = 0; i < count; i++) {
10140 +         if (0 && pkg->pre_depends_count)
10141 +              fprintf(stderr, "  i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
10142 +         for (j = 0; j < depends->possibility_count; j++){
10143 +              ab_depend = depends->possibilities[j]->pkg;
10144 +              if(!ab_depend->depended_upon_by)
10145 +                   ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
10146 +
10147 +              temp = ab_depend->depended_upon_by;
10148 +              othercount = 1;
10149 +              while(*temp){
10150 +                   temp++;
10151 +                   othercount++;
10152 +              }
10153 +              *temp = ab_pkg;
10154 +
10155 +              ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by, 
10156 +                                                                       (othercount + 1) * sizeof(abstract_pkg_t *));
10157 +              /* the array may have moved */
10158 +              temp = ab_depend->depended_upon_by + othercount;
10159 +              *temp = NULL;
10160 +         }
10161 +         depends++;
10162 +     }
10163 +}
10164 +
10165 +static depend_t * depend_init(void)
10166 +{
10167 +    depend_t * d = (depend_t *)malloc(sizeof(depend_t));    
10168 +    if ( d==NULL ){
10169 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10170 +        return NULL; 
10171 +     }
10172 +    d->constraint = NONE;
10173 +    d->version = NULL;
10174 +    d->pkg = NULL;
10175 +    
10176 +    return d;
10177 +}
10178 +
10179 +static void depend_deinit(depend_t *d)
10180 +{
10181 +    free(d);
10182 +}
10183 +
10184 +static int parseDepends(compound_depend_t *compound_depend, 
10185 +                       hash_table_t * hash, char * depend_str)
10186 +{
10187 +     char * pkg_name, buffer[2048];
10188 +     int num_of_ors = 0;
10189 +     register int i;
10190 +     register char * src, * dest;
10191 +     depend_t ** possibilities;
10192 +
10193 +     /* first count the number of ored possibilities for satisfying dependency */
10194 +     src = depend_str;
10195 +     while(*src)
10196 +         if(*src++ == '|')
10197 +              num_of_ors++;
10198 +
10199 +     compound_depend->type = DEPEND;
10200 +
10201 +     compound_depend->possibility_count = num_of_ors + 1;
10202 +     possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
10203 +     if (!possibilities)
10204 +         return -ENOMEM;
10205 +     compound_depend->possibilities = possibilities;
10206 +
10207 +     src = depend_str;
10208 +     for(i = 0; i < num_of_ors + 1; i++){
10209 +         possibilities[i] = depend_init();
10210 +          if (!possibilities[i])
10211 +              return -ENOMEM;
10212 +         /* gobble up just the name first */
10213 +         dest = buffer;
10214 +         while(*src &&
10215 +               !isspace(*src) &&
10216 +               (*src != '(') &&
10217 +               (*src != '*') &&
10218 +               (*src != '|'))
10219 +              *dest++ = *src++;
10220 +         *dest = '\0';
10221 +         pkg_name = trim_alloc(buffer);
10222 +          if (pkg_name == NULL )
10223 +              return -ENOMEM;
10224 +       
10225 +         /* now look at possible version info */
10226 +       
10227 +         /* skip to next chars */
10228 +         if(isspace(*src))
10229 +              while(*src && isspace(*src)) src++;
10230 +
10231 +         /* extract constraint and version */
10232 +         if(*src == '('){
10233 +              src++;
10234 +              if(!strncmp(src, "<<", 2)){
10235 +                   possibilities[i]->constraint = EARLIER;
10236 +                   src += 2;
10237 +              }
10238 +              else if(!strncmp(src, "<=", 2)){
10239 +                   possibilities[i]->constraint = EARLIER_EQUAL;
10240 +                   src += 2;
10241 +              }
10242 +              else if(!strncmp(src, ">=", 2)){
10243 +                   possibilities[i]->constraint = LATER_EQUAL;
10244 +                   src += 2;
10245 +              }
10246 +              else if(!strncmp(src, ">>", 2)){
10247 +                   possibilities[i]->constraint = LATER;
10248 +                   src += 2;
10249 +              }
10250 +              else if(!strncmp(src, "=", 1)){
10251 +                   possibilities[i]->constraint = EQUAL;
10252 +                   src++;
10253 +              }
10254 +              /* should these be here to support deprecated designations; dpkg does */
10255 +              else if(!strncmp(src, "<", 1)){
10256 +                   possibilities[i]->constraint = EARLIER_EQUAL;
10257 +                   src++;
10258 +              }
10259 +              else if(!strncmp(src, ">", 1)){
10260 +                   possibilities[i]->constraint = LATER_EQUAL;
10261 +                   src++; 
10262 +              }
10263 +
10264 +              /* now we have any constraint, pass space to version string */
10265 +              while(isspace(*src)) src++;
10266 +
10267 +              /* this would be the version string */
10268 +              dest = buffer;
10269 +              while(*src && *src != ')')
10270 +                   *dest++ = *src++;
10271 +              *dest = '\0';
10272 +           
10273 +              possibilities[i]->version = trim_alloc(buffer);
10274 +              /*                   fprintf(stderr, "let's print the depends version string:");
10275 +                                   fprintf(stderr, "version %s\n", possibilities[i]->version);*/
10276 +               if (possibilities[i]->version == NULL )
10277 +                   return -ENOMEM;
10278 +
10279 +        
10280 +         }
10281 +         /* hook up the dependency to its abstract pkg */
10282 +         possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
10283 +
10284 +         free(pkg_name);
10285 +       
10286 +         /* now get past the ) and any possible | chars */
10287 +         while(*src &&
10288 +               (isspace(*src) ||
10289 +                (*src == ')') ||
10290 +                (*src == '|')))
10291 +              src++;
10292 +         if (*src == '*')
10293 +         {
10294 +              compound_depend->type = GREEDY_DEPEND;
10295 +              src++;
10296 +         }
10297 +     }
10298 +
10299 +     return 0;
10300 +}
10301 diff -urN busybox.old/archival/libipkg/pkg_depends.h busybox.dev/archival/libipkg/pkg_depends.h
10302 --- busybox.old/archival/libipkg/pkg_depends.h  1970-01-01 01:00:00.000000000 +0100
10303 +++ busybox.dev/archival/libipkg/pkg_depends.h  2007-01-22 13:41:06.000000000 +0100
10304 @@ -0,0 +1,105 @@
10305 +/* pkg_depends.h - the itsy package management system
10306 +
10307 +   Steven M. Ayer
10308 +   
10309 +   Copyright (C) 2002 Compaq Computer Corporation
10310 +
10311 +   This program is free software; you can redistribute it and/or
10312 +   modify it under the terms of the GNU General Public License as
10313 +   published by the Free Software Foundation; either version 2, or (at
10314 +   your option) any later version.
10315 +
10316 +   This program is distributed in the hope that it will be useful, but
10317 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10318 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10319 +   General Public License for more details.
10320 +*/
10321 +
10322 +#ifndef PKG_DEPENDS_H
10323 +#define PKG_DEPENDS_H
10324 +
10325 +#include "pkg.h"
10326 +#include "pkg_hash.h"
10327 +
10328 +enum depend_type {
10329 +    PREDEPEND,
10330 +    DEPEND,
10331 +    CONFLICTS,
10332 +    GREEDY_DEPEND,
10333 +    RECOMMEND,
10334 +    SUGGEST
10335 +};
10336 +typedef enum depend_type depend_type_t;
10337 +
10338 +enum version_constraint {
10339 +    NONE,
10340 +    EARLIER,
10341 +    EARLIER_EQUAL,
10342 +    EQUAL,
10343 +    LATER_EQUAL,
10344 +    LATER
10345 +};
10346 +typedef enum version_constraint version_constraint_t;
10347 +
10348 +struct depend{
10349 +    version_constraint_t constraint;
10350 +    char * version;
10351 +    abstract_pkg_t * pkg;
10352 +};
10353 +typedef struct depend depend_t;
10354 +    
10355 +struct compound_depend{
10356 +    depend_type_t type;
10357 +    int possibility_count;
10358 +    struct depend ** possibilities;
10359 +};
10360 +typedef struct compound_depend compound_depend_t;
10361 +
10362 +#include "hash_table.h"
10363 +
10364 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10365 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10366 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10367 +int buildDepends(hash_table_t * hash, pkg_t * pkg);
10368 +
10369 +/**
10370 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
10371 + * the same abstract package and 0 otherwise.
10372 + */
10373 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee);
10374 +
10375 +/**
10376 + * pkg_provides returns 1 if pkg->provides contains providee and 0
10377 + * otherwise.
10378 + */
10379 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee);
10380 +
10381 +/**
10382 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
10383 + * otherwise.
10384 + */
10385 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee);
10386 +
10387 +/**
10388 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee provides and 0
10389 + * otherwise.
10390 + */
10391 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflicts);
10392 +
10393 +/**
10394 + * pkg_conflicts returns 1 if pkg->conflicts contains one of conflictee's provides and 0
10395 + * otherwise.
10396 + */
10397 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflicts);
10398 +
10399 +char *pkg_depend_str(pkg_t *pkg, int pkg_index);
10400 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg);
10401 +void freeDepends(pkg_t *pkg);
10402 +void printDepends(pkg_t * pkg);
10403 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg);
10404 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, pkg_vec_t *depends, char *** unresolved);
10405 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg);
10406 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend);
10407 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend);
10408 +
10409 +#endif
10410 diff -urN busybox.old/archival/libipkg/pkg_dest.c busybox.dev/archival/libipkg/pkg_dest.c
10411 --- busybox.old/archival/libipkg/pkg_dest.c     1970-01-01 01:00:00.000000000 +0100
10412 +++ busybox.dev/archival/libipkg/pkg_dest.c     2007-01-22 13:41:03.000000000 +0100
10413 @@ -0,0 +1,92 @@
10414 +/* pkg_dest.c - the itsy package management system
10415 +
10416 +   Carl D. Worth
10417 +
10418 +   Copyright (C) 2001 University of Southern California
10419 +
10420 +   This program is free software; you can redistribute it and/or
10421 +   modify it under the terms of the GNU General Public License as
10422 +   published by the Free Software Foundation; either version 2, or (at
10423 +   your option) any later version.
10424 +
10425 +   This program is distributed in the hope that it will be useful, but
10426 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10427 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10428 +   General Public License for more details.
10429 +*/
10430 +
10431 +#include "ipkg.h"
10432 +
10433 +#include "pkg_dest.h"
10434 +#include "file_util.h"
10435 +#include "str_util.h"
10436 +#include "sprintf_alloc.h"
10437 +
10438 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir)
10439 +{
10440 +    dest->name = strdup(name);
10441 +
10442 +    /* Guarantee that dest->root_dir ends with a '/' */
10443 +    if (str_ends_with(root_dir, "/")) {
10444 +       dest->root_dir = strdup(root_dir);
10445 +    } else {
10446 +       sprintf_alloc(&dest->root_dir, "%s/", root_dir);
10447 +    }
10448 +    file_mkdir_hier(dest->root_dir, 0755);
10449 +
10450 +    sprintf_alloc(&dest->ipkg_dir, "%s%s",
10451 +                 dest->root_dir, IPKG_STATE_DIR_PREFIX);
10452 +    file_mkdir_hier(dest->ipkg_dir, 0755);
10453 +
10454 +    if (str_starts_with (lists_dir, "/")) 
10455 +        sprintf_alloc(&dest->lists_dir, "%s", lists_dir);
10456 +    else
10457 +        sprintf_alloc(&dest->lists_dir, "/%s", lists_dir);
10458 +
10459 +    file_mkdir_hier(dest->lists_dir, 0755);
10460 +
10461 +    sprintf_alloc(&dest->info_dir, "%s/%s",
10462 +                 dest->ipkg_dir, IPKG_INFO_DIR_SUFFIX);
10463 +    file_mkdir_hier(dest->info_dir, 0755);
10464 +
10465 +    sprintf_alloc(&dest->status_file_name, "%s/%s",
10466 +                 dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10467 +
10468 +    sprintf_alloc(&dest->status_file_tmp_name, "%s/%s.tmp",
10469 +                 dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10470 +
10471 +    dest->status_file = NULL;
10472 +
10473 +    return 0;
10474 +}
10475 +
10476 +void pkg_dest_deinit(pkg_dest_t *dest)
10477 +{
10478 +    free(dest->name);
10479 +    dest->name = NULL;
10480 +
10481 +    free(dest->root_dir);
10482 +    dest->root_dir = NULL;
10483 +
10484 +    free(dest->ipkg_dir);
10485 +    dest->ipkg_dir = NULL;
10486 +
10487 +    free(dest->lists_dir);
10488 +    dest->lists_dir = NULL;
10489 +
10490 +    free(dest->info_dir);
10491 +    dest->info_dir = NULL;
10492 +
10493 +    free(dest->status_file_name);
10494 +    dest->status_file_name = NULL;
10495 +
10496 +    free(dest->status_file_tmp_name);
10497 +    dest->status_file_tmp_name = NULL;
10498 +
10499 +    if (dest->status_file) {
10500 +       fclose(dest->status_file);
10501 +    }
10502 +    dest->status_file = NULL;
10503 +
10504 +    dest->root_dir = NULL;
10505 +}
10506 diff -urN busybox.old/archival/libipkg/pkg_dest.h busybox.dev/archival/libipkg/pkg_dest.h
10507 --- busybox.old/archival/libipkg/pkg_dest.h     1970-01-01 01:00:00.000000000 +0100
10508 +++ busybox.dev/archival/libipkg/pkg_dest.h     2007-01-22 13:41:03.000000000 +0100
10509 @@ -0,0 +1,38 @@
10510 +/* pkg_dest.h - the itsy package management system
10511 +
10512 +   Carl D. Worth
10513 +
10514 +   Copyright (C) 2001 University of Southern California
10515 +
10516 +   This program is free software; you can redistribute it and/or
10517 +   modify it under the terms of the GNU General Public License as
10518 +   published by the Free Software Foundation; either version 2, or (at
10519 +   your option) any later version.
10520 +
10521 +   This program is distributed in the hope that it will be useful, but
10522 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10523 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10524 +   General Public License for more details.
10525 +*/
10526 +
10527 +#ifndef PKG_DEST_H
10528 +#define PKG_DEST_H
10529 +
10530 +typedef struct pkg_dest pkg_dest_t;
10531 +struct pkg_dest
10532 +{
10533 +    char *name;
10534 +    char *root_dir;
10535 +    char *ipkg_dir;
10536 +    char *lists_dir;
10537 +    char *info_dir;
10538 +    char *status_file_name;
10539 +    char *status_file_tmp_name;
10540 +    FILE *status_file;
10541 +};
10542 +
10543 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char *lists_dir);
10544 +void pkg_dest_deinit(pkg_dest_t *dest);
10545 +
10546 +#endif
10547 +
10548 diff -urN busybox.old/archival/libipkg/pkg_dest_list.c busybox.dev/archival/libipkg/pkg_dest_list.c
10549 --- busybox.old/archival/libipkg/pkg_dest_list.c        1970-01-01 01:00:00.000000000 +0100
10550 +++ busybox.dev/archival/libipkg/pkg_dest_list.c        2007-01-22 13:41:03.000000000 +0100
10551 @@ -0,0 +1,85 @@
10552 +/* pkg_dest_list.c - the itsy package management system
10553 +
10554 +   Carl D. Worth
10555 +
10556 +   Copyright (C) 2001 University of Southern California
10557 +
10558 +   This program is free software; you can redistribute it and/or
10559 +   modify it under the terms of the GNU General Public License as
10560 +   published by the Free Software Foundation; either version 2, or (at
10561 +   your option) any later version.
10562 +
10563 +   This program is distributed in the hope that it will be useful, but
10564 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10565 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10566 +   General Public License for more details.
10567 +*/
10568 +
10569 +#include "ipkg.h"
10570 +
10571 +#include "pkg_dest.h"
10572 +#include "void_list.h"
10573 +#include "pkg_dest_list.h"
10574 +
10575 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data)
10576 +{
10577 +    return void_list_elt_init((void_list_elt_t *) elt, data);
10578 +}
10579 +
10580 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt)
10581 +{
10582 +    void_list_elt_deinit((void_list_elt_t *) elt);
10583 +}
10584 +
10585 +int pkg_dest_list_init(pkg_dest_list_t *list)
10586 +{
10587 +    return void_list_init((void_list_t *) list);
10588 +}
10589 +
10590 +void pkg_dest_list_deinit(pkg_dest_list_t *list)
10591 +{
10592 +    pkg_dest_list_elt_t *iter;
10593 +    pkg_dest_t *pkg_dest;
10594 +
10595 +    for (iter = list->head; iter; iter = iter->next) {
10596 +       pkg_dest = iter->data;
10597 +       pkg_dest_deinit(pkg_dest);
10598 +
10599 +       /* malloced in pkg_dest_list_append */
10600 +       free(pkg_dest);
10601 +       iter->data = NULL;
10602 +    }
10603 +    void_list_deinit((void_list_t *) list);
10604 +}
10605 +
10606 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10607 +                                const char *root_dir,const char *lists_dir)
10608 +{
10609 +    int err;
10610 +    pkg_dest_t *pkg_dest;
10611 +
10612 +    /* freed in plg_dest_list_deinit */
10613 +    pkg_dest = malloc(sizeof(pkg_dest_t));
10614 +    if (pkg_dest == NULL) {
10615 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10616 +       return NULL;
10617 +    }
10618 +
10619 +    pkg_dest_init(pkg_dest, name, root_dir,lists_dir);
10620 +    err = void_list_append((void_list_t *) list, pkg_dest);
10621 +    if (err) {
10622 +       return NULL;
10623 +    }
10624 +
10625 +    return pkg_dest;
10626 +}
10627 +
10628 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data)
10629 +{
10630 +    return void_list_push((void_list_t *) list, data);
10631 +}
10632 +
10633 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list)
10634 +{
10635 +    return (pkg_dest_list_elt_t *) void_list_pop((void_list_t *) list);
10636 +}
10637 diff -urN busybox.old/archival/libipkg/pkg_dest_list.h busybox.dev/archival/libipkg/pkg_dest_list.h
10638 --- busybox.old/archival/libipkg/pkg_dest_list.h        1970-01-01 01:00:00.000000000 +0100
10639 +++ busybox.dev/archival/libipkg/pkg_dest_list.h        2007-01-22 13:41:03.000000000 +0100
10640 @@ -0,0 +1,50 @@
10641 +/* pkg_dest_list.h - the itsy package management system
10642 +
10643 +   Carl D. Worth
10644 +
10645 +   Copyright (C) 2001 University of Southern California
10646 +
10647 +   This program is free software; you can redistribute it and/or
10648 +   modify it under the terms of the GNU General Public License as
10649 +   published by the Free Software Foundation; either version 2, or (at
10650 +   your option) any later version.
10651 +
10652 +   This program is distributed in the hope that it will be useful, but
10653 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10654 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10655 +   General Public License for more details.
10656 +*/
10657 +
10658 +#ifndef PKG_DEST_LIST_H
10659 +#define PKG_DEST_LIST_H
10660 +
10661 +#include "pkg_dest.h"
10662 +
10663 +typedef struct pkg_dest_list_elt pkg_dest_list_elt_t;
10664 +struct pkg_dest_list_elt
10665 +{
10666 +    pkg_dest_list_elt_t *next;
10667 +    pkg_dest_t *data;
10668 +};
10669 +
10670 +typedef struct pkg_dest_list pkg_dest_list_t;
10671 +struct pkg_dest_list
10672 +{
10673 +    pkg_dest_list_elt_t pre_head;
10674 +    pkg_dest_list_elt_t *head;
10675 +    pkg_dest_list_elt_t *tail;
10676 +};
10677 +
10678 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data);
10679 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt);
10680 +
10681 +int pkg_dest_list_init(pkg_dest_list_t *list);
10682 +void pkg_dest_list_deinit(pkg_dest_list_t *list);
10683 +
10684 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10685 +                                const char *root_dir,const char* lists_dir);
10686 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data);
10687 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list);
10688 +
10689 +#endif
10690 +
10691 diff -urN busybox.old/archival/libipkg/pkg_extract.c busybox.dev/archival/libipkg/pkg_extract.c
10692 --- busybox.old/archival/libipkg/pkg_extract.c  1970-01-01 01:00:00.000000000 +0100
10693 +++ busybox.dev/archival/libipkg/pkg_extract.c  2007-01-22 13:41:03.000000000 +0100
10694 @@ -0,0 +1,224 @@
10695 +/* pkg_extract.c - the itsy package management system
10696 +
10697 +   Carl D. Worth
10698 +
10699 +   Copyright (C) 2001 University of Southern California
10700 +
10701 +   This program is free software; you can redistribute it and/or
10702 +   modify it under the terms of the GNU General Public License as
10703 +   published by the Free Software Foundation; either version 2, or (at
10704 +   your option) any later version.
10705 +
10706 +   This program is distributed in the hope that it will be useful, but
10707 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10708 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10709 +   General Public License for more details.
10710 +*/
10711 +
10712 +#include "ipkg.h"
10713 +#include <errno.h>
10714 +#include <fcntl.h>
10715 +#include <stdio.h>
10716 +
10717 +#include "pkg_extract.h"
10718 +
10719 +#include "libbb.h"
10720 +#include "file_util.h"
10721 +#include "sprintf_alloc.h"
10722 +#include "unarchive.h"
10723 +
10724 +#define IPKG_CONTROL_ARCHIVE  "control.tar.gz"
10725 +#define IPKG_DATA_ARCHIVE  "data.tar.gz"
10726 +#define IPKG_CONTROL_FILE  "control"
10727 +
10728 +static void extract_ipkg_file_to_dir(pkg_t *pkg, const char *dir, const char *filename)
10729 +{
10730 +       archive_handle_t *archive;
10731 +       char *path;
10732 +
10733 +       sprintf_alloc(&path, "%s/", dir);
10734 +       archive = init_handle();
10735 +       archive->src_fd = xopen(pkg->local_filename, O_RDONLY);
10736 +       archive->filter = filter_accept_list;
10737 +       llist_add_to(&(archive->accept), (char *)filename);
10738 +       archive->buffer = path;
10739 +       archive->action_data = data_extract_all_prefix;
10740 +       archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
10741 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10742 +       close(archive->src_fd);
10743 +       free(archive->accept);
10744 +       free(archive);
10745 +       free(path);
10746 +}
10747 +
10748 +static void data_extract_file_name_to_buffer(archive_handle_t *archive)
10749 +{
10750 +       unsigned int size = strlen(archive->file_header->name) + 2;
10751 +
10752 +       if (archive->buffer == NULL) {
10753 +               archive->buffer = xmalloc(size);
10754 +               strcpy(archive->buffer, archive->file_header->name);
10755 +       } else {
10756 +               size += strlen(archive->buffer);
10757 +               archive->buffer = xrealloc(archive->buffer, size);
10758 +               strcat(archive->buffer, archive->file_header->name);
10759 +       } 
10760 +       strcat(archive->buffer, "\n");
10761 +       data_skip(archive);
10762 +}
10763 +
10764 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
10765 +{
10766 +       archive_handle_t *archive;
10767 +       char *name;
10768 +
10769 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE); 
10770 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10771 +       archive = init_handle();
10772 +       archive->src_fd = xopen(name, O_RDONLY);
10773 +       archive->filter = filter_accept_list;
10774 +       llist_add_to(&(archive->accept), "./" IPKG_CONTROL_FILE);
10775 +       archive->action_data = data_extract_to_buffer;
10776 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10777 +       close(archive->src_fd);
10778 +       fputs(archive->buffer, stream);
10779 +       free(archive->buffer);
10780 +       free(archive->accept);
10781 +       free(archive);
10782 +       free(name);
10783 +
10784 +       return 0;
10785 +}
10786 +
10787 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
10788 +{
10789 +    return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
10790 +}
10791 +
10792 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir, const char *prefix)
10793 +{
10794 +       archive_handle_t *archive;
10795 +       char *name;
10796 +       char *path;
10797 +
10798 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE); 
10799 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10800 +       sprintf_alloc(&path, "%s/%s", dir, prefix);
10801 +       archive = init_handle();
10802 +       archive->src_fd = xopen(name, O_RDONLY);
10803 +       archive->filter = filter_accept_all;
10804 +       archive->buffer = path;
10805 +       archive->action_data = data_extract_all_prefix;
10806 +       archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
10807 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10808 +       close(archive->src_fd);
10809 +       free(archive);
10810 +       free(path);
10811 +       free(name);
10812 +
10813 +       return 0;
10814 +}
10815 +
10816 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
10817 +{
10818 +       archive_handle_t *archive;
10819 +       char *name;
10820 +       char *path;
10821 +
10822 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
10823 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
10824 +       sprintf_alloc(&path, "%s/", dir);
10825 +       archive = init_handle();
10826 +       archive->src_fd = xopen(name, O_RDONLY);
10827 +       archive->filter = filter_accept_all;
10828 +       archive->buffer = path;
10829 +       archive->action_data = data_extract_all_prefix;
10830 +       archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
10831 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10832 +       close(archive->src_fd);
10833 +       free(archive);
10834 +       free(path);
10835 +       free(name);
10836 +
10837 +       return 0;
10838 +}
10839 +
10840 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
10841 +{
10842 +     int err=0;
10843 +     char *line, *data_file;
10844 +     FILE *file;
10845 +     FILE *tmp;
10846 +
10847 +     file = fopen(file_name, "w");
10848 +     if (file == NULL) {
10849 +         fprintf(stderr, "%s: ERROR: Failed to open %s for writing.\n",
10850 +                 __FUNCTION__, file_name);
10851 +         return EINVAL;
10852 +     }
10853 +
10854 +     tmp = tmpfile();
10855 +     if (pkg->installed_files) {
10856 +         str_list_elt_t *elt;
10857 +         for (elt = pkg->installed_files->head; elt; elt = elt->next) {
10858 +              fprintf(file, "%s\n", elt->data);
10859 +         }
10860 +     } else {
10861 +         err = pkg_extract_data_file_names_to_stream(pkg, tmp);
10862 +         if (err) {
10863 +              fclose(file);
10864 +              fclose(tmp);
10865 +              return err;
10866 +         }
10867 +
10868 +         /* Fixup data file names by removing the initial '.' */
10869 +         rewind(tmp);
10870 +         while (1) {
10871 +              line = file_read_line_alloc(tmp);
10872 +              if (line == NULL) {
10873 +                   break;
10874 +              }
10875 +
10876 +              data_file = line;
10877 +              if (*data_file == '.') {
10878 +                   data_file++;
10879 +              }
10880 +
10881 +              if (*data_file != '/') {
10882 +                   fputs("/", file);
10883 +              }
10884 +
10885 +              /* I have no idea why, but this is what dpkg does */
10886 +              if (strcmp(data_file, "/\n") == 0) {
10887 +                   fputs("/.\n", file);
10888 +              } else {
10889 +                   fputs(data_file, file);
10890 +              }
10891 +         }
10892 +     }
10893 +     fclose(tmp);
10894 +     fclose(file);
10895 +
10896 +     return err;
10897 +}
10898 +
10899 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
10900 +{
10901 +       archive_handle_t *archive;
10902 +       char *name;
10903 +       
10904 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
10905 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
10906 +       archive = init_handle();
10907 +       archive->src_fd = xopen(name, O_RDONLY);
10908 +       archive->filter = filter_accept_all;
10909 +       archive->action_data = data_extract_file_name_to_buffer;
10910 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10911 +       close(archive->src_fd);
10912 +       fputs(archive->buffer, file);
10913 +       free(archive->buffer);
10914 +       free(archive);
10915 +       free(name);
10916 +       
10917 +       return 0;
10918 +}
10919 diff -urN busybox.old/archival/libipkg/pkg_extract.h busybox.dev/archival/libipkg/pkg_extract.h
10920 --- busybox.old/archival/libipkg/pkg_extract.h  1970-01-01 01:00:00.000000000 +0100
10921 +++ busybox.dev/archival/libipkg/pkg_extract.h  2007-01-22 13:41:03.000000000 +0100
10922 @@ -0,0 +1,32 @@
10923 +/* pkg_extract.c - the itsy package management system
10924 +
10925 +   Carl D. Worth
10926 +
10927 +   Copyright (C) 2001 University of Southern California
10928 +
10929 +   This program is free software; you can redistribute it and/or
10930 +   modify it under the terms of the GNU General Public License as
10931 +   published by the Free Software Foundation; either version 2, or (at
10932 +   your option) any later version.
10933 +
10934 +   This program is distributed in the hope that it will be useful, but
10935 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10936 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10937 +   General Public License for more details.
10938 +*/
10939 +
10940 +#ifndef PKG_EXTRACT_H
10941 +#define PKG_EXTRACT_H
10942 +
10943 +#include "pkg.h"
10944 +
10945 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream);
10946 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir);
10947 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
10948 +                                                const char *dir,
10949 +                                                const char *prefix);
10950 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir);
10951 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name);
10952 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file);
10953 +
10954 +#endif
10955 diff -urN busybox.old/archival/libipkg/pkg.h busybox.dev/archival/libipkg/pkg.h
10956 --- busybox.old/archival/libipkg/pkg.h  1970-01-01 01:00:00.000000000 +0100
10957 +++ busybox.dev/archival/libipkg/pkg.h  2007-01-22 13:41:06.000000000 +0100
10958 @@ -0,0 +1,232 @@
10959 +/* pkg.h - the itsy package management system
10960 +
10961 +   Carl D. Worth
10962 +
10963 +   Copyright (C) 2001 University of Southern California
10964 +
10965 +   This program is free software; you can redistribute it and/or
10966 +   modify it under the terms of the GNU General Public License as
10967 +   published by the Free Software Foundation; either version 2, or (at
10968 +   your option) any later version.
10969 +
10970 +   This program is distributed in the hope that it will be useful, but
10971 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10972 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10973 +   General Public License for more details.
10974 +*/
10975 +
10976 +#ifndef PKG_H
10977 +#define PKG_H
10978 +
10979 +#include <sys/types.h>
10980 +#include <sys/stat.h>
10981 +#include <unistd.h>
10982 +
10983 +#include "pkg_vec.h"
10984 +#include "str_list.h"
10985 +#include "pkg_src.h"
10986 +#include "pkg_dest.h"
10987 +#include "ipkg_conf.h"
10988 +#include "conffile_list.h"
10989 +
10990 +struct ipkg_conf;
10991 +
10992 +
10993 +#define ARRAY_SIZE(array) sizeof(array) / sizeof((array)[0])
10994 +
10995 +/* I think "Size" is currently the shortest field name */
10996 +#define PKG_MINIMUM_FIELD_NAME_LEN 4
10997 +
10998 +enum pkg_state_want
10999 +{
11000 +    SW_UNKNOWN = 1,
11001 +    SW_INSTALL,
11002 +    SW_DEINSTALL,
11003 +    SW_PURGE,
11004 +    SW_LAST_STATE_WANT
11005 +};
11006 +typedef enum pkg_state_want pkg_state_want_t;
11007 +
11008 +enum pkg_state_flag
11009 +{
11010 +    SF_OK        = 0,
11011 +    SF_REINSTREQ = 1,
11012 +    SF_HOLD      = 2,  /* do not upgrade version */
11013 +    SF_REPLACE   = 4,  /* replace this package */
11014 +    SF_NOPRUNE   = 8,  /* do not remove obsolete files */
11015 +    SF_PREFER    = 16, /* prefer this version */
11016 +    SF_OBSOLETE  = 32, /* old package in upgrade pair */
11017 +    SF_MARKED    = 64, /* temporary mark */
11018 +    SF_FILELIST_CHANGED = 128, /* needs filelist written */
11019 +    SF_USER      = 256,
11020 +    SF_LAST_STATE_FLAG
11021 +};
11022 +typedef enum pkg_state_flag pkg_state_flag_t;
11023 +#define SF_NONVOLATILE_FLAGS (SF_HOLD|SF_NOPRUNE|SF_PREFER|SF_OBSOLETE|SF_USER)
11024 +
11025 +enum pkg_state_status
11026 +{
11027 +    SS_NOT_INSTALLED = 1,
11028 +    SS_UNPACKED,
11029 +    SS_HALF_CONFIGURED,
11030 +    SS_INSTALLED,
11031 +    SS_HALF_INSTALLED,
11032 +    SS_CONFIG_FILES,
11033 +    SS_POST_INST_FAILED,
11034 +    SS_REMOVAL_FAILED,
11035 +    SS_LAST_STATE_STATUS
11036 +};
11037 +typedef enum pkg_state_status pkg_state_status_t;
11038 +
11039 +struct abstract_pkg{
11040 +    char * name;
11041 +    int dependencies_checked;
11042 +    pkg_vec_t * pkgs;
11043 +    pkg_state_status_t state_status;
11044 +    pkg_state_flag_t state_flag;
11045 +    struct abstract_pkg ** depended_upon_by; /* @@@@ this should be abstract_pkg_vec_t -Jamey */
11046 +    abstract_pkg_vec_t * provided_by;
11047 +    abstract_pkg_vec_t * replaced_by;
11048 +};
11049 +
11050 +#include "pkg_depends.h"
11051 +
11052 +/* XXX: CLEANUP: I'd like to clean up pkg_t in several ways:
11053 +
11054 +   The 3 version fields should go into a single version struct. (This
11055 +   is especially important since, currently, pkg->version can easily
11056 +   be mistaken for pkg_verson_str_alloc(pkg) although they are very
11057 +   distinct. This has been the source of multiple bugs.
11058 +
11059 +   The 3 state fields could possibly also go into their own struct.
11060 +
11061 +   All fields which deal with lists of packages, (Depends,
11062 +   Pre-Depends, Provides, Suggests, Recommends, Enhances), should each
11063 +   be handled by a single struct in pkg_t
11064 +
11065 +   All string fields for which there is a small set of possible
11066 +   values, (section, maintainer, architecture, maybe version?), that
11067 +   are reused among different packages -- for all such packages we
11068 +   should move from "char *"s to some atom datatype to share data
11069 +   storage and use less memory. We might even do reference counting,
11070 +   but probably not since most often we only create new pkg_t structs,
11071 +   we don't often free them.  */
11072 +struct pkg
11073 +{
11074 +     char *name;
11075 +     unsigned long epoch;
11076 +     char *version;
11077 +     char *revision;
11078 +     char *familiar_revision;
11079 +     pkg_src_t *src;
11080 +     pkg_dest_t *dest;
11081 +     char *architecture;
11082 +     char *section;
11083 +     char *maintainer;
11084 +     char *description;
11085 +     pkg_state_want_t state_want;
11086 +     pkg_state_flag_t state_flag;
11087 +     pkg_state_status_t state_status;
11088 +     char **depends_str;
11089 +     int depends_count;
11090 +     char **pre_depends_str;
11091 +     int pre_depends_count;
11092 +     char **recommends_str;
11093 +     int recommends_count;
11094 +     char **suggests_str;
11095 +     int suggests_count;
11096 +     compound_depend_t * depends;
11097 +
11098 +     /* Abhaya: new conflicts */
11099 +     char **conflicts_str;
11100 +     compound_depend_t * conflicts;
11101 +     int conflicts_count;
11102 +       
11103 +     char **replaces_str;
11104 +     int replaces_count;
11105 +     abstract_pkg_t ** replaces;
11106 +
11107 +     char **provides_str;
11108 +     int provides_count;
11109 +     abstract_pkg_t ** provides;
11110 +
11111 +     abstract_pkg_t *parent;
11112 +
11113 +     pkg_t *old_pkg; /* during upgrade, points from installee to previously installed */
11114 +
11115 +     char *filename;
11116 +     char *local_filename;
11117 +     char *url;
11118 +     char *tmp_unpack_dir;
11119 +     char *md5sum;
11120 +     char *size;
11121 +     char *installed_size;
11122 +     char *priority;
11123 +     char *source;
11124 +     conffile_list_t conffiles;
11125 +     time_t installed_time;
11126 +     /* As pointer for lazy evaluation */
11127 +     str_list_t *installed_files;
11128 +     /* XXX: CLEANUP: I'd like to perhaps come up with a better
11129 +       mechanism to avoid the problem here, (which is that the
11130 +       installed_files list was being freed from an inner loop while
11131 +       still being used within an outer loop. */
11132 +     int installed_files_ref_cnt;
11133 +     int essential;
11134 +     int arch_priority;
11135 +/* Adding this flag, to "force" ipkg to choose a "provided_by_hand" package, if there are multiple choice */
11136 +     int provided_by_hand;
11137 +};
11138 +
11139 +pkg_t *pkg_new(void);
11140 +int pkg_init(pkg_t *pkg);
11141 +void pkg_deinit(pkg_t *pkg);
11142 +int pkg_init_from_file(pkg_t *pkg, const char *filename);
11143 +abstract_pkg_t *abstract_pkg_new(void);
11144 +int abstract_pkg_init(abstract_pkg_t *ab_pkg);
11145 +
11146 +/* 
11147 + * merges fields from newpkg into oldpkg.
11148 + * Forcibly sets oldpkg state_status, state_want and state_flags if set_status is nonzero 
11149 + */
11150 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status);
11151 +
11152 +char *pkg_version_str_alloc(pkg_t *pkg);
11153 +
11154 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg);
11155 +int pkg_name_version_and_architecture_compare(pkg_t *a, pkg_t *b);
11156 +int abstract_pkg_name_compare(abstract_pkg_t *a, abstract_pkg_t *b);
11157 +
11158 +char * pkg_formatted_info(pkg_t *pkg );
11159 +char * pkg_formatted_field(pkg_t *pkg, const char *field );
11160 +
11161 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg);
11162 +
11163 +void pkg_print_info(pkg_t *pkg, FILE *file);
11164 +void pkg_print_status(pkg_t * pkg, FILE * file);
11165 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field);
11166 +str_list_t *pkg_get_installed_files(pkg_t *pkg);
11167 +int pkg_free_installed_files(pkg_t *pkg);
11168 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg);
11169 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name);
11170 +int pkg_run_script(struct ipkg_conf *conf, pkg_t *pkg,
11171 +                  const char *script, const char *args);
11172 +
11173 +/* enum mappings */
11174 +char *pkg_state_want_to_str(pkg_state_want_t sw);
11175 +pkg_state_want_t pkg_state_want_from_str(char *str);
11176 +char *pkg_state_flag_to_str(pkg_state_flag_t sf);
11177 +pkg_state_flag_t pkg_state_flag_from_str(char *str);
11178 +char *pkg_state_status_to_str(pkg_state_status_t ss);
11179 +pkg_state_status_t pkg_state_status_from_str(char *str);
11180 +
11181 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op);
11182 +
11183 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg);
11184 +int pkg_info_preinstall_check(ipkg_conf_t *conf);
11185 +int pkg_free_installed_files(pkg_t *pkg);
11186 +
11187 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg);
11188 +int pkg_write_changed_filelists(ipkg_conf_t *conf);
11189 +
11190 +#endif
11191 diff -urN busybox.old/archival/libipkg/pkg_hash.c busybox.dev/archival/libipkg/pkg_hash.c
11192 --- busybox.old/archival/libipkg/pkg_hash.c     1970-01-01 01:00:00.000000000 +0100
11193 +++ busybox.dev/archival/libipkg/pkg_hash.c     2007-01-22 13:41:06.000000000 +0100
11194 @@ -0,0 +1,616 @@
11195 +/* ipkg_hash.c - the itsy package management system
11196 +
11197 +   Steven M. Ayer
11198 +   
11199 +   Copyright (C) 2002 Compaq Computer Corporation
11200 +
11201 +   This program is free software; you can redistribute it and/or
11202 +   modify it under the terms of the GNU General Public License as
11203 +   published by the Free Software Foundation; either version 2, or (at
11204 +   your option) any later version.
11205 +
11206 +   This program is distributed in the hope that it will be useful, but
11207 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11208 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11209 +   General Public License for more details.
11210 +*/
11211 +
11212 +#include "ipkg.h"
11213 +#include <errno.h>
11214 +#include <ctype.h>
11215 +#include <stdlib.h>
11216 +#include <string.h>
11217 +
11218 +#include "hash_table.h"
11219 +#include "pkg.h"
11220 +#include "ipkg_message.h"
11221 +#include "pkg_vec.h"
11222 +#include "pkg_hash.h"
11223 +#include "pkg_parse.h"
11224 +#include "ipkg_utils.h"
11225 +
11226 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
11227 +
11228 +/*
11229 + * this will talk to both feeds-lists files and installed status files
11230 + * example api:
11231 + *
11232 + * hash_table_t hash;
11233 + * pkg_hash_init(name, &hash, 1000);
11234 + * pkg_hash_add_from_file(<feed filename>);
11235 + *
11236 + * the query function is just there as a shell to prove to me that this
11237 + * sort of works, but isn't far from doing something useful
11238 + * 
11239 + * -sma, 12/21/01
11240 + * modified: CDW 3 Jan. 2002
11241 + */
11242 +
11243 +
11244 +\f
11245 +int pkg_hash_init(const char *name, hash_table_t *hash, int len)
11246 +{
11247 +  return hash_table_init(name, hash, len);
11248 +}
11249 +
11250 +void pkg_hash_deinit(hash_table_t *hash)
11251 +{
11252 +  hash_table_deinit(hash);
11253 +}
11254 +
11255 +
11256 +/* Find the default arch for a given package status file if none is given. */
11257 +static char *pkg_get_default_arch(ipkg_conf_t *conf)
11258 +{
11259 +     nv_pair_list_elt_t *l;
11260 +     char *def_arch = HOST_CPU_STR;            /* Default arch */
11261 +     int def_prio = 0;                         /* Other archs override this */
11262 +
11263 +     l = conf->arch_list.head;
11264 +
11265 +     while (l) {
11266 +         nv_pair_t *nv = l->data;
11267 +         int priority = strtol(nv->value, NULL, 0);
11268 +
11269 +         /* Check if this arch has higher priority, and is valid */
11270 +         if ((priority > def_prio) &&
11271 +             (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
11272 +              /* Our new default */
11273 +              def_prio = priority;
11274 +              def_arch = nv->name;
11275 +         }
11276 +         l = l->next;
11277 +     }
11278 +
11279 +     return strdup(def_arch);
11280 +}
11281 +
11282 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
11283 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
11284 +{
11285 +     hash_table_t *hash = &conf->pkg_hash;
11286 +     char **raw;
11287 +     char **raw_start;
11288 +     pkg_t *pkg;
11289 +    
11290 +     raw = raw_start = read_raw_pkgs_from_file(file_name);
11291 +     if (!raw)
11292 +        return -ENOMEM;
11293 +
11294 +     while(*raw){         /* don't worry, we'll increment raw in the parsing function */
11295 +         pkg = pkg_new();
11296 +         if (!pkg)
11297 +              return -ENOMEM;
11298 +
11299 +         if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
11300 +              if (!pkg->architecture) {
11301 +                   char *version_str = pkg_version_str_alloc(pkg);
11302 +                   pkg->architecture = pkg_get_default_arch(conf);
11303 +                   ipkg_message(conf, IPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
11304 +                                pkg->name, version_str, pkg->architecture);
11305 +                   free(version_str);
11306 +              }
11307 +              hash_insert_pkg(hash, pkg, is_status_file,conf);
11308 +         } else {
11309 +              free(pkg);
11310 +         }
11311 +     }
11312 +
11313 +     /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
11314 +       memory after read_raw_pkgs_from_file */
11315 +     raw = raw_start;
11316 +     while (*raw) {
11317 +         free(*raw++);
11318 +     }
11319 +     free(raw_start);
11320 +     return 0;
11321 +}
11322 +
11323 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
11324 +{
11325 +  return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
11326 +}
11327 +
11328 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name)
11329 +{
11330 +    abstract_pkg_t *apkg = abstract_pkg_fetch_by_name(hash, name);
11331 +    if (apkg)
11332 +       return NULL;
11333 +    return apkg->provided_by;
11334 +}
11335 +
11336 +
11337 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg, 
11338 +                                                 int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet)
11339 +{
11340 +     int i, j;
11341 +     int nprovides = 0;
11342 +     int nmatching = 0;
11343 +     pkg_vec_t *matching_pkgs = pkg_vec_alloc();
11344 +     abstract_pkg_vec_t *matching_apkgs = abstract_pkg_vec_alloc();
11345 +     abstract_pkg_vec_t *provided_apkg_vec;
11346 +     abstract_pkg_t **provided_apkgs;
11347 +     abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
11348 +     pkg_t *latest_installed_parent = NULL;
11349 +     pkg_t *latest_matching = NULL;
11350 +     pkg_t *held_pkg = NULL;
11351 +     pkg_t *good_pkg_by_name = NULL;
11352 +
11353 +     if (matching_apkgs == NULL || providers == NULL || 
11354 +         apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
11355 +         return NULL;
11356 +
11357 +     ipkg_message(conf, IPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
11358 +
11359 +     provided_apkg_vec = apkg->provided_by;
11360 +     nprovides = provided_apkg_vec->len;
11361 +     provided_apkgs = provided_apkg_vec->pkgs;
11362 +     if (nprovides > 1)
11363 +         ipkg_message(conf, IPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
11364 +
11365 +     /* accumulate all the providers */
11366 +     for (i = 0; i < nprovides; i++) {
11367 +         abstract_pkg_t *provider_apkg = provided_apkgs[i];
11368 +         ipkg_message(conf, IPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
11369 +         abstract_pkg_vec_insert(providers, provider_apkg);
11370 +     }
11371 +     nprovides = providers->len;
11372 +
11373 +     for (i = 0; i < nprovides; i++) {
11374 +         abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
11375 +         abstract_pkg_t *replacement_apkg = NULL;
11376 +         pkg_vec_t *vec;
11377 +
11378 +         if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
11379 +              replacement_apkg = provider_apkg->replaced_by->pkgs[0];
11380 +              if (provider_apkg->replaced_by->len > 1) {
11381 +                   ipkg_message(conf, IPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
11382 +                                provider_apkg->name, replacement_apkg->name);
11383 +              }
11384 +         }
11385 +
11386 +         if (replacement_apkg)
11387 +              ipkg_message(conf, IPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
11388 +                           replacement_apkg->name, provider_apkg->name);
11389 +
11390 +         if (replacement_apkg && (replacement_apkg != provider_apkg)) {
11391 +              if (abstract_pkg_vec_contains(providers, replacement_apkg))
11392 +                   continue;
11393 +              else
11394 +                   provider_apkg = replacement_apkg;
11395 +         }
11396 +
11397 +         if (!(vec = provider_apkg->pkgs)) {
11398 +              ipkg_message(conf, IPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
11399 +              continue;
11400 +         }
11401 +    
11402 +
11403 +         /* now check for supported architecture */
11404 +         {
11405 +              int max_count = 0;
11406 +
11407 +              /* count packages matching max arch priority and keep track of last one */
11408 +              for (j = 0; j < vec->len; j++) {
11409 +                   pkg_t *maybe = vec->pkgs[j];
11410 +                   ipkg_message(conf, IPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
11411 +                                maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
11412 +                   if (maybe->arch_priority > 0)  {
11413 +                        max_count++;
11414 +                        abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
11415 +                        pkg_vec_insert(matching_pkgs, maybe);
11416 +                   }
11417 +              }
11418 +         }
11419 +     }
11420 +
11421 +     if (matching_pkgs->len > 1)
11422 +         pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
11423 +     if (matching_apkgs->len > 1)
11424 +         abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
11425 +
11426 +/* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
11427 +   needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
11428 +/* The problem is what to do when there are more than a mathing package, with the same name and several version ?
11429 +   Until now I always got the latest, but that breaks the downgrade option.
11430 +   If I stop at the first one, I would probably miss the new ones 
11431 +   Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
11432 +   or from a Packages feed.
11433 +   It it is from a file it always need to be checked whatever version I have in feeds or everywhere, according to force-down or whatever options*/
11434 +/*Pigi*/
11435 +
11436 +     for (i = 0; i < matching_pkgs->len; i++) {
11437 +         pkg_t *matching = matching_pkgs->pkgs[i];
11438 +          if (constraint_fcn(matching, cdata)) {  /* We found it */
11439 +             ipkg_message(conf, IPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
11440 +             good_pkg_by_name = matching;
11441 +             if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
11442 +                break;                                 
11443 +          }
11444 +     }
11445 +
11446 +
11447 +     for (i = 0; i < matching_pkgs->len; i++) {
11448 +         pkg_t *matching = matching_pkgs->pkgs[i];
11449 +         latest_matching = matching;
11450 +         if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
11451 +              latest_installed_parent = matching;
11452 +         if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
11453 +              if (held_pkg)
11454 +                   ipkg_message(conf, IPKG_ERROR, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
11455 +                                held_pkg->name, matching->name);
11456 +              held_pkg = matching;
11457 +         }
11458 +     }
11459 +
11460 +     if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
11461 +         ipkg_message(conf, IPKG_ERROR, "Package=%s, %d matching providers\n",
11462 +                      apkg->name, matching_apkgs->len);
11463 +         for (i = 0; i < matching_apkgs->len; i++) {
11464 +              abstract_pkg_t *matching = matching_apkgs->pkgs[i];
11465 +              ipkg_message(conf, IPKG_ERROR, "    %s\n", matching->name);
11466 +         }
11467 +         ipkg_message(conf, IPKG_ERROR, "Please select one with ipkg install or ipkg flag prefer\n");
11468 +     }
11469 +
11470 +     if (matching_apkgs->len > 1 && conf->verbosity > 1) {
11471 +         ipkg_message(conf, IPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
11472 +                      __FUNCTION__, apkg->name, matching_pkgs->len);
11473 +         for (i = 0; i < matching_pkgs->len; i++) {
11474 +              pkg_t *matching = matching_pkgs->pkgs[i];
11475 +              ipkg_message(conf, IPKG_INFO, "    %s %s %s\n",
11476 +                           matching->name, matching->version, matching->architecture);
11477 +         }
11478 +     }
11479 +
11480 +     nmatching = matching_apkgs->len;
11481 +
11482 +     pkg_vec_free(matching_pkgs);
11483 +     abstract_pkg_vec_free(matching_apkgs);
11484 +     abstract_pkg_vec_free(providers);
11485 +
11486 +     if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
11487 +         return good_pkg_by_name;
11488 +     }
11489 +     if (held_pkg) {
11490 +         ipkg_message(conf, IPKG_INFO, "  using held package %s\n", held_pkg->name);
11491 +         return held_pkg;
11492 +     }
11493 +     if (latest_installed_parent) {
11494 +         ipkg_message(conf, IPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
11495 +         return latest_installed_parent;
11496 +     }
11497 +     if (nmatching > 1) {
11498 +         ipkg_message(conf, IPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
11499 +         return NULL;
11500 +     }
11501 +     if (latest_matching) {
11502 +         ipkg_message(conf, IPKG_INFO, "  using latest matching %s %s %s\n",
11503 +                      latest_matching->name, latest_matching->version, latest_matching->architecture);
11504 +         return latest_matching;
11505 +     }
11506 +     return NULL;
11507 +}
11508 +
11509 +static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
11510 +{
11511 +     const char *name = (const char *)cdata;
11512 +     if (strcmp(pkg->name, name) == 0)
11513 +         return 1;
11514 +     else
11515 +         return 0;   
11516 +}
11517 +
11518 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name)
11519 +{
11520 +     hash_table_t *hash = &conf->pkg_hash;
11521 +     abstract_pkg_t *apkg = NULL;
11522 +
11523 +     if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
11524 +         return NULL;
11525 +     
11526 +     return pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0);
11527 +}
11528 +
11529 +
11530 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
11531 +                                      const char *pkg_name,
11532 +                                      const char * version)
11533 +{
11534 +    pkg_vec_t * vec;
11535 +    register int i;
11536 +    char *version_str = NULL;
11537 +    
11538 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
11539 +       return NULL;
11540 +    
11541 +    for(i = 0; i < vec->len; i++) {
11542 +       version_str = pkg_version_str_alloc(vec->pkgs[i]);
11543 +       if(!strcmp(version_str, version)) {
11544 +           free(version_str);
11545 +           break;
11546 +       }
11547 +       free(version_str);
11548 +    }
11549 +       
11550 +    if(i == vec->len)
11551 +       return NULL;
11552 +    
11553 +    return vec->pkgs[i];
11554 +}
11555 +
11556 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
11557 +                                            const char *pkg_name,
11558 +                                            pkg_dest_t *dest)
11559 +{
11560 +    pkg_vec_t * vec;
11561 +    register int i;
11562 +
11563 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
11564 +       return NULL;
11565 +    }
11566 +    
11567 +    for(i = 0; i < vec->len; i++)
11568 +       if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
11569 +           return vec->pkgs[i];
11570 +        }
11571 +    return NULL;
11572 +}
11573 +
11574 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
11575 +                                       const char *pkg_name)
11576 +{
11577 +    pkg_vec_t * vec;
11578 +    register int i;
11579 +
11580 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
11581 +       return NULL;
11582 +    } 
11583 +
11584 +    for(i = 0; i < vec->len; i++)
11585 +       if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
11586 +           return vec->pkgs[i];
11587 +        } 
11588 +    
11589 +    return NULL;
11590 +}
11591 +
11592 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
11593 +{
11594 +    abstract_pkg_t * ab_pkg;
11595 +
11596 +    if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
11597 +       return NULL;
11598 +    }
11599 +    
11600 +    if (ab_pkg->pkgs) {
11601 +      return ab_pkg->pkgs;
11602 +    } else if (ab_pkg->provided_by) {
11603 +      abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
11604 +      if (abpkg != NULL){
11605 +         return abpkg->pkgs;
11606 +      } else {
11607 +         return ab_pkg->pkgs;
11608 +      }
11609 +    } else {
11610 +      return NULL;
11611 +    }
11612 +}
11613 +
11614 +static int pkg_compare_names(const void *p1, const void *p2)
11615 +{
11616 +  const pkg_t *pkg1 = *(const pkg_t **)p1;
11617 +  const pkg_t *pkg2 = *(const pkg_t **)p2;
11618 +  if (pkg1->name == NULL)
11619 +    return 1;
11620 +  if (pkg2->name == NULL)
11621 +    return -1;
11622 +  return(strcmp(pkg1->name, pkg2->name));
11623 +}
11624 +
11625 +
11626 +static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
11627 +{
11628 +  int j;
11629 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11630 +  pkg_vec_t *all = (pkg_vec_t *)data;
11631 +  pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11632 +  if (pkg_vec) {
11633 +    for (j = 0; j < pkg_vec->len; j++) {
11634 +      pkg_t *pkg = pkg_vec->pkgs[j];
11635 +      pkg_vec_insert(all, pkg);
11636 +    }
11637 +  }
11638 +}
11639 +
11640 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
11641 +{
11642 +    hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
11643 +    qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
11644 +}
11645 +
11646 +static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
11647 +{
11648 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11649 +  pkg_vec_t *all = (pkg_vec_t *)data;
11650 +  pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11651 +  int j;
11652 +  if (pkg_vec) {
11653 +    for (j = 0; j < pkg_vec->len; j++) {
11654 +      pkg_t *pkg = pkg_vec->pkgs[j];
11655 +      if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
11656 +       pkg_vec_insert(all, pkg);
11657 +      }
11658 +    }
11659 +  }
11660 +}
11661 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
11662 +{
11663 +    hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
11664 +    qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
11665 +}
11666 +
11667 +static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
11668 +{
11669 +  int i;
11670 +  pkg_t *pkg;
11671 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11672 +  ipkg_conf_t *conf = (ipkg_conf_t *)data;
11673 +  abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
11674 +  fprintf(stdout, "%s\n", ab_pkg->name);
11675 +  i = 0;
11676 +  if (dependents != NULL)
11677 +    while (dependents [i] != NULL)
11678 +      printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
11679 +  dependents = ab_pkg->provided_by->pkgs;
11680 +  i = 0;
11681 +  if (dependents != NULL)
11682 +    while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
11683 +      printf ("\tprovided by - %s\n", dependents [i ++]->name);
11684 +  pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name);
11685 +  if (pkg) {
11686 +    i = 0;
11687 +    while (i < pkg->depends_count)
11688 +      printf ("\tdepends on - %s\n", pkg->depends_str [i ++]); 
11689 +  }
11690 +}
11691 +void pkg_hash_dump(hash_table_t *hash, void *data)
11692 +{
11693 +
11694 +  printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
11695 +  hash_table_foreach(hash, pkg_hash_dump_helper, data);
11696 +  printf ("\n+=+%s+=+\n\n", __FUNCTION__);    
11697 +}
11698 +
11699 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11700 +{
11701 +  abstract_pkg_t * ab_pkg;
11702 +
11703 +  if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
11704 +    ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
11705 +
11706 +  return ab_pkg;
11707 +}
11708 +
11709 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
11710 +{
11711 +     abstract_pkg_t * ab_pkg;
11712 +     int arch_priority;
11713 +
11714 +     if(!pkg)
11715 +         return pkg;
11716 +
11717 +     arch_priority = pkg->arch_priority;
11718 +
11719 +     if (buildDepends(hash, pkg)<0){
11720 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11721 +        return NULL;
11722 +     }
11723 +     ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
11724 +
11725 +     if (set_status) {
11726 +         if (pkg->state_status == SS_INSTALLED) {
11727 +              ab_pkg->state_status = SS_INSTALLED;
11728 +         } else if (pkg->state_status == SS_UNPACKED) {
11729 +              ab_pkg->state_status = SS_UNPACKED;
11730 +         }
11731 +     }
11732 +
11733 +     if(!ab_pkg->pkgs)
11734 +         ab_pkg->pkgs = pkg_vec_alloc();
11735 +    
11736 +     /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
11737 +     pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
11738 +     pkg->parent = ab_pkg;
11739 +
11740 +     if (buildProvides(hash, ab_pkg, pkg)<0){
11741 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11742 +        return NULL;
11743 +     }
11744 +     /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
11745 +     if (buildConflicts(hash, ab_pkg, pkg)<0){
11746 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11747 +        return NULL;
11748 +     }
11749 +     if (buildReplaces(hash, ab_pkg, pkg)<0) {
11750 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11751 +        return NULL;
11752 +     }
11753 +    
11754 +     buildDependedUponBy(pkg, ab_pkg);
11755 +     return pkg;
11756 +}
11757 +
11758 +/*
11759 + * this will assume that we've already determined that
11760 + * the abstract pkg doesn't exist, 'cause we should know these things...
11761 + */
11762 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11763 +{
11764 +  abstract_pkg_t * ab_pkg;
11765 +
11766 +  ab_pkg = abstract_pkg_new();
11767 +  if (ab_pkg == NULL) { return NULL; }
11768 +
11769 +  ab_pkg->name = strdup(pkg_name);
11770 +  hash_table_insert(hash, pkg_name, ab_pkg);
11771 +
11772 +  return ab_pkg;
11773 +}
11774 +
11775 +
11776 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name)
11777 +{
11778 +     hash_table_t *file_hash = &conf->file_hash;
11779 +
11780 +     return hash_table_get(file_hash, file_name); 
11781 +}
11782 +
11783 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
11784 +{
11785 +     hash_table_t *file_hash = &conf->file_hash;
11786 +     pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
11787 +     int file_name_len = strlen(file_name);
11788 +
11789 +     if (file_name[file_name_len -1] == '/')
11790 +         return 0;
11791 +
11792 +     if (conf->offline_root) {
11793 +         int len = strlen(conf->offline_root);
11794 +         if (strncmp(file_name, conf->offline_root, len) == 0) {
11795 +              file_name += len;
11796 +         }
11797 +     }
11798 +
11799 +     // ipkg_message(conf, IPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
11800 +     hash_table_insert(file_hash, file_name, owning_pkg); 
11801 +     if (old_owning_pkg) {
11802 +         str_list_remove_elt(old_owning_pkg->installed_files, file_name);
11803 +         /* mark this package to have its filelist written */
11804 +         old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
11805 +         owning_pkg->state_flag |= SF_FILELIST_CHANGED;
11806 +     }
11807 +     return 0;
11808 +}
11809 +
11810 +
11811 diff -urN busybox.old/archival/libipkg/pkg_hash.h busybox.dev/archival/libipkg/pkg_hash.h
11812 --- busybox.old/archival/libipkg/pkg_hash.h     1970-01-01 01:00:00.000000000 +0100
11813 +++ busybox.dev/archival/libipkg/pkg_hash.h     2007-01-22 13:41:03.000000000 +0100
11814 @@ -0,0 +1,61 @@
11815 +/* pkg_hash.h - the itsy package management system
11816 +
11817 +   Steven M. Ayer
11818 +   
11819 +   Copyright (C) 2002 Compaq Computer Corporation
11820 +
11821 +   This program is free software; you can redistribute it and/or
11822 +   modify it under the terms of the GNU General Public License as
11823 +   published by the Free Software Foundation; either version 2, or (at
11824 +   your option) any later version.
11825 +
11826 +   This program is distributed in the hope that it will be useful, but
11827 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11828 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11829 +   General Public License for more details.
11830 +*/
11831 +
11832 +#ifndef PKG_HASH_H
11833 +#define PKG_HASH_H
11834 +
11835 +#include "pkg.h"
11836 +#include "pkg_vec.h"
11837 +#include "hash_table.h"
11838 +
11839 +
11840 +int pkg_hash_init(const char *name, hash_table_t *hash, int len);
11841 +void pkg_hash_deinit(hash_table_t *hash);
11842 +void pkg_hash_map(hash_table_t *hash, void (*f)(void *data, void *entry), void *data);
11843 +
11844 +void pkg_hash_dump(hash_table_t *hash, void *data);
11845 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *available);
11846 +
11847 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
11848 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file);
11849 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf);
11850 +
11851 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
11852 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name);
11853 +pkg_vec_t *pkg_hash_fetch_by_name(hash_table_t *hash, const char *pkg_name);
11854 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *installed);
11855 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
11856 +                                      const char *pkg_name,
11857 +                                      const char * version);
11858 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name);
11859 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg, 
11860 +                                                 int (*constraint_fcn)(pkg_t *pkg, void *data), void *cdata, int quiet);
11861 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name);
11862 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
11863 +                                       const char *pkg_name);
11864 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
11865 +                                            const char *pkg_name,
11866 +                                            pkg_dest_t *dest);
11867 +
11868 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name);
11869 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *pkg);
11870 +
11871 +/* XXX: shouldn't this go in pkg_vec.[ch]? */
11872 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name);
11873 +
11874 +#endif
11875 +
11876 diff -urN busybox.old/archival/libipkg/pkg_parse.c busybox.dev/archival/libipkg/pkg_parse.c
11877 --- busybox.old/archival/libipkg/pkg_parse.c    1970-01-01 01:00:00.000000000 +0100
11878 +++ busybox.dev/archival/libipkg/pkg_parse.c    2007-01-22 13:41:03.000000000 +0100
11879 @@ -0,0 +1,366 @@
11880 +/* pkg_parse.c - the itsy package management system
11881 +
11882 +   Steven M. Ayer
11883 +   
11884 +   Copyright (C) 2002 Compaq Computer Corporation
11885 +
11886 +   This program is free software; you can redistribute it and/or
11887 +   modify it under the terms of the GNU General Public License as
11888 +   published by the Free Software Foundation; either version 2, or (at
11889 +   your option) any later version.
11890 +
11891 +   This program is distributed in the hope that it will be useful, but
11892 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11893 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11894 +   General Public License for more details.
11895 +*/
11896 +
11897 +#include "ipkg.h"
11898 +#include <errno.h>
11899 +#include <ctype.h>
11900 +   
11901 +#include "pkg.h"
11902 +#include "ipkg_utils.h"
11903 +#include "pkg_parse.h"
11904 +
11905 +int isGenericFieldType(char * type, char * line)
11906 +{
11907 +    if(!strncmp(line, type, strlen(type)))
11908 +       return 1;
11909 +    return 0;
11910 +}
11911 +
11912 +char * parseGenericFieldType(char * type, char * raw)
11913 +{
11914 +    char * field_value = raw + (strlen(type) + 1);
11915 +    return trim_alloc(field_value);
11916 +}
11917 +
11918 +void parseStatus(pkg_t *pkg, char * raw)
11919 +{
11920 +    char sw_str[64], sf_str[64], ss_str[64];
11921 +
11922 +    sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
11923 +    pkg->state_want = pkg_state_want_from_str(sw_str);
11924 +    pkg->state_flag = pkg_state_flag_from_str(sf_str);
11925 +    pkg->state_status = pkg_state_status_from_str(ss_str);
11926 +}
11927 +
11928 +char ** parseDependsString(char * raw, int * depends_count)
11929 +{
11930 +    char ** depends = NULL;
11931 +    int line_count = 0;
11932 +    char buff[2048], * dest;
11933 +
11934 +    while(raw && *raw && !isspace(*raw)) {
11935 +       raw++;
11936 +    }
11937 +
11938 +    if(line_is_blank(raw)){
11939 +       *depends_count = line_count;
11940 +       return NULL;
11941 +    }
11942 +    while(raw && *raw){
11943 +       depends = (char **)realloc(depends, sizeof(char *) * (line_count + 1));
11944 +       
11945 +       while(isspace(*raw)) raw++;
11946 +
11947 +       dest = buff;
11948 +       while((*raw != ',') && *raw)
11949 +           *dest++ = *raw++;
11950 +
11951 +       *dest = '\0';
11952 +       depends[line_count] = trim_alloc(buff);
11953 +       if(depends[line_count] ==NULL)
11954 +          return NULL;
11955 +        line_count++;
11956 +       if(*raw == ',')
11957 +           raw++;
11958 +    }
11959 +    *depends_count = line_count;
11960 +    return depends;
11961 +}
11962 +
11963 +void parseConffiles(pkg_t * pkg, char * raw)
11964 +{
11965 +    char file_name[1048], md5sum[1048];  /* please tell me there aren't any longer that 1k */
11966 +
11967 +    if(!strncmp(raw, "Conffiles:", 10))
11968 +       raw += strlen("Conffiles:");
11969 +
11970 +    while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
11971 +       conffile_list_append(&pkg->conffiles, file_name, md5sum);
11972 +       /*      fprintf(stderr, "%s %s ", file_name, md5sum);*/
11973 +       while (*raw && isspace(*raw)) {
11974 +           raw++;
11975 +       }
11976 +       raw += strlen(file_name);
11977 +       while (*raw && isspace(*raw)) {
11978 +           raw++;
11979 +       }
11980 +       raw += strlen(md5sum);
11981 +    }
11982 +}    
11983 +
11984 +int parseVersion(pkg_t *pkg, char *raw)
11985 +{
11986 +  char *colon, *eepochcolon;
11987 +#ifdef USE_DEBVERSION
11988 +  char *hyphen;
11989 +#endif
11990 +  unsigned long epoch;
11991 +
11992 +  if (!*raw) {
11993 +      fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
11994 +      return EINVAL;
11995 +  }
11996 +
11997 +  if (strncmp(raw, "Version:", 8) == 0) {
11998 +      raw += 8;
11999 +  }
12000 +  while (*raw && isspace(*raw)) {
12001 +      raw++;
12002 +  }
12003 +  
12004 +  colon= strchr(raw,':');
12005 +  if (colon) {
12006 +    epoch= strtoul(raw,&eepochcolon,10);
12007 +    if (colon != eepochcolon) {
12008 +       fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
12009 +       return EINVAL;
12010 +    }
12011 +    if (!*++colon) {
12012 +       fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
12013 +       return EINVAL;
12014 +    }
12015 +    raw= colon;
12016 +    pkg->epoch= epoch;
12017 +  } else {
12018 +    pkg->epoch= 0;
12019 +  }
12020 +
12021 +  pkg->revision = "";
12022 +  pkg->familiar_revision = "";
12023 +
12024 +  pkg->version= malloc(strlen(raw)+1);
12025 +  if ( pkg->version == NULL ) {
12026 +     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12027 +     return ENOMEM;
12028 +  }
12029 +  strcpy(pkg->version, raw);
12030 +
12031 +#ifdef USE_DEBVERSION
12032 +  hyphen= strrchr(pkg->version,'-');
12033 +
12034 +  if (hyphen) {
12035 +    *hyphen++= 0;
12036 +    if (strncmp("fam", hyphen, 3) == 0) {
12037 +      pkg->familiar_revision=hyphen+3;
12038 +      hyphen= strrchr(pkg->version,'-');
12039 +      if (hyphen) {
12040 +       *hyphen++= 0;
12041 +       pkg->revision = hyphen;
12042 +      }
12043 +    } else {
12044 +      pkg->revision = hyphen;
12045 +    }
12046 +  }
12047 +#endif
12048 +
12049 +/*
12050 +  fprintf(stderr,"Parsed version: %lu, %s, %s, %s\n",
12051 +         pkg->epoch,
12052 +         pkg->version,
12053 +         pkg->revision,
12054 +         pkg->familiar_revision);
12055 +*/
12056 +         
12057 +  return 0;
12058 +}
12059 +
12060 +
12061 +/* This code is needed to insert in first position the keyword for the aligning bug */
12062 +
12063 +int alterProvidesLine(char *raw, char *temp)
12064 +{
12065 +
12066 +
12067 +  if (!*raw) {
12068 +      fprintf(stderr, "%s: ERROR: Provides string is empty", __FUNCTION__);
12069 +      return -EINVAL;
12070 +  }
12071 +
12072 +  if ( temp == NULL ) {
12073 +     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12074 +     return -ENOMEM;
12075 +  }
12076 +
12077 +  if (strncmp(raw, "Provides:", 9) == 0) {
12078 +      raw += 9;
12079 +  }
12080 +  while (*raw && isspace(*raw)) {
12081 +      raw++;
12082 +  }      
12083 +  
12084 +  snprintf ( temp, 35, "Provides: ipkg_internal_use_only, ");           /* First part of the line */
12085 +  while (*raw) {
12086 +     strncat( temp, raw++, 1);
12087 +  }
12088 +  return 0;
12089
12090 +}
12091 +
12092 +/* Some random thoughts from Carl:
12093 +
12094 +   This function could be considerably simplified if we just kept
12095 +   an array of all the generic string-valued field names, and looped
12096 +   through those looking for a match. Also, these fields could perhaps
12097 +   be stored in the package as an array as well, (or, probably better,
12098 +   as an nv_pair_list_t).
12099 +
12100 +   Fields which require special parsing or storage, (such as Depends:
12101 +   and Status:) could be handled as they are now. 
12102 +*/
12103 +/* XXX: FEATURE: The Suggests: field needs to be changed from a string
12104 +   to a dependency list. And, since we already have
12105 +   Depends/Pre-Depends and need to add Conflicts, Recommends, and
12106 +   Enhances, perhaps we could generalize all of these and save some
12107 +   code duplication.
12108 +*/
12109 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
12110 +{
12111 +    int reading_conffiles, reading_description;
12112 +    int pkg_false_provides=1;
12113 +    char ** lines;
12114 +    char * provide=NULL;
12115 +
12116 +    pkg->src = src;
12117 +    pkg->dest = dest;
12118 +
12119 +    reading_conffiles = reading_description = 0;
12120 +
12121 +    for (lines = *raw; *lines; lines++) {
12122 +       /*      fprintf(stderr, "PARSING %s\n", *lines);*/
12123 +       if(isGenericFieldType("Package:", *lines)) 
12124 +           pkg->name = parseGenericFieldType("Package", *lines);
12125 +       else if(isGenericFieldType("Architecture:", *lines))
12126 +           pkg->architecture = parseGenericFieldType("Architecture", *lines);
12127 +       else if(isGenericFieldType("Filename:", *lines))
12128 +           pkg->filename = parseGenericFieldType("Filename", *lines);
12129 +       else if(isGenericFieldType("Section:", *lines))
12130 +           pkg->section = parseGenericFieldType("Section", *lines);
12131 +       else if(isGenericFieldType("MD5sum:", *lines))
12132 +           pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
12133 +       /* The old ipkg wrote out status files with the wrong case for MD5sum,
12134 +          let's parse it either way */
12135 +       else if(isGenericFieldType("MD5Sum:", *lines))
12136 +           pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
12137 +       else if(isGenericFieldType("Size:", *lines))
12138 +           pkg->size = parseGenericFieldType("Size", *lines);
12139 +       else if(isGenericFieldType("Source:", *lines))
12140 +           pkg->source = parseGenericFieldType("Source", *lines);
12141 +       else if(isGenericFieldType("Installed-Size:", *lines))
12142 +           pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
12143 +       else if(isGenericFieldType("Installed-Time:", *lines)) {
12144 +            char *time_str = parseGenericFieldType("Installed-Time", *lines);
12145 +            pkg->installed_time = strtoul(time_str, NULL, 0);
12146 +       } else if(isGenericFieldType("Priority:", *lines))
12147 +           pkg->priority = parseGenericFieldType("Priority", *lines);
12148 +       else if(isGenericFieldType("Essential:", *lines)) {
12149 +           char *essential_value;
12150 +           essential_value = parseGenericFieldType("Essential", *lines);
12151 +           if (strcmp(essential_value, "yes") == 0) {
12152 +               pkg->essential = 1;
12153 +           }
12154 +           free(essential_value);
12155 +       }
12156 +       else if(isGenericFieldType("Status", *lines))
12157 +           parseStatus(pkg, *lines);
12158 +       else if(isGenericFieldType("Version", *lines))
12159 +           parseVersion(pkg, *lines);
12160 +       else if(isGenericFieldType("Maintainer", *lines))
12161 +           pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
12162 +       else if(isGenericFieldType("Conffiles", *lines)){
12163 +           parseConffiles(pkg, *lines);
12164 +           reading_conffiles = 1;
12165 +       }
12166 +       else if(isGenericFieldType("Description", *lines)) {
12167 +           pkg->description = parseGenericFieldType("Description", *lines);
12168 +           reading_conffiles = 0;
12169 +           reading_description = 1;
12170 +       }
12171 +
12172 +       else if(isGenericFieldType("Provides", *lines)){
12173 +/* Here we add the internal_use to align the off by one problem between provides_str and provides */
12174 +            provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new ipkg_internal_use_only */
12175 +            if ( alterProvidesLine(*lines,provide) ){
12176 +               return EINVAL;
12177 +            }
12178 +           pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
12179 +/* Let's try to hack a bit here.
12180 +   The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
12181 +   in alot of other places. We will remove it before writing down the status database */
12182 +            pkg_false_provides=0;
12183 +            free(provide);
12184 +        } 
12185 +
12186 +       else if(isGenericFieldType("Depends", *lines))
12187 +           pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
12188 +       else if(isGenericFieldType("Pre-Depends", *lines))
12189 +           pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
12190 +       else if(isGenericFieldType("Recommends", *lines))
12191 +           pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
12192 +       else if(isGenericFieldType("Suggests", *lines))
12193 +           pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
12194 +       /* Abhaya: support for conflicts */
12195 +       else if(isGenericFieldType("Conflicts", *lines))
12196 +           pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
12197 +       else if(isGenericFieldType("Replaces", *lines))
12198 +           pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
12199 +       else if(line_is_blank(*lines)) {
12200 +           lines++;
12201 +           break;
12202 +       }
12203 +       else if(**lines == ' '){
12204 +           if(reading_description) {
12205 +               /* we already know it's not blank, so the rest of description */      
12206 +               pkg->description = realloc(pkg->description,
12207 +                                          strlen(pkg->description)
12208 +                                          + 1 + strlen(*lines) + 1);
12209 +               strcat(pkg->description, "\n");
12210 +               strcat(pkg->description, (*lines));
12211 +           }
12212 +           else if(reading_conffiles)
12213 +               parseConffiles(pkg, *lines);
12214 +       }
12215 +    }
12216 +    *raw = lines;
12217 +/* If the ipk has not a Provides line, we insert our false line */ 
12218 +    if ( pkg_false_provides==1)
12219 +       pkg->provides_str = parseDependsString ((char *)"Provides: ipkg_internal_use_only ", &pkg->provides_count);
12220 +
12221 +    if (pkg->name) {
12222 +       return 0;
12223 +    } else {
12224 +       return EINVAL;
12225 +    }
12226 +}
12227 +
12228 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
12229 +{
12230 +    char ** lines;
12231 +
12232 +    for (lines = *raw; *lines; lines++) {
12233 +       if(isGenericFieldType("Essential:", *lines)) {
12234 +           char *essential_value;
12235 +           essential_value = parseGenericFieldType("Essential", *lines);
12236 +           if (strcmp(essential_value, "yes") == 0) {
12237 +               pkg->essential = 1;
12238 +           }
12239 +           free(essential_value);
12240 +       }
12241 +    }
12242 +    *raw = lines;
12243 +
12244 +    return 0;
12245 +}
12246 diff -urN busybox.old/archival/libipkg/pkg_parse.h busybox.dev/archival/libipkg/pkg_parse.h
12247 --- busybox.old/archival/libipkg/pkg_parse.h    1970-01-01 01:00:00.000000000 +0100
12248 +++ busybox.dev/archival/libipkg/pkg_parse.h    2007-01-22 13:41:03.000000000 +0100
12249 @@ -0,0 +1,31 @@
12250 +/* pkg_parse.h - the itsy package management system
12251 +
12252 +   Steven M. Ayer
12253 +   
12254 +   Copyright (C) 2002 Compaq Computer Corporation
12255 +
12256 +   This program is free software; you can redistribute it and/or
12257 +   modify it under the terms of the GNU General Public License as
12258 +   published by the Free Software Foundation; either version 2, or (at
12259 +   your option) any later version.
12260 +
12261 +   This program is distributed in the hope that it will be useful, but
12262 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12263 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12264 +   General Public License for more details.
12265 +*/
12266 +
12267 +#ifndef PKG_PARSE_H
12268 +#define PKG_PARSE_H
12269 +
12270 +int isGenericFieldType(char * type, char * line);
12271 +char * parseGenericFieldType(char * type, char * raw);
12272 +void parseStatus(pkg_t *pkg, char * raw);
12273 +int parseVersion(pkg_t *pkg, char *raw);
12274 +char ** parseDependsString(char * raw, int * depends_count);
12275 +int parseVersion(pkg_t *pkg, char *raw);
12276 +void parseConffiles(pkg_t * pkg, char * raw);
12277 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
12278 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
12279 +
12280 +#endif
12281 diff -urN busybox.old/archival/libipkg/pkg_src.c busybox.dev/archival/libipkg/pkg_src.c
12282 --- busybox.old/archival/libipkg/pkg_src.c      1970-01-01 01:00:00.000000000 +0100
12283 +++ busybox.dev/archival/libipkg/pkg_src.c      2007-01-22 13:41:03.000000000 +0100
12284 @@ -0,0 +1,43 @@
12285 +/* pkg_src.c - the itsy package management system
12286 +
12287 +   Carl D. Worth
12288 +
12289 +   Copyright (C) 2001 University of Southern California
12290 +
12291 +   This program is free software; you can redistribute it and/or
12292 +   modify it under the terms of the GNU General Public License as
12293 +   published by the Free Software Foundation; either version 2, or (at
12294 +   your option) any later version.
12295 +
12296 +   This program is distributed in the hope that it will be useful, but
12297 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12298 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12299 +   General Public License for more details.
12300 +*/
12301 +
12302 +#include "ipkg.h"
12303 +
12304 +#include "pkg_src.h"
12305 +#include "str_util.h"
12306 +
12307 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip)
12308 +{
12309 +    src->gzip = gzip;
12310 +    src->name = str_dup_safe (name);
12311 +    src->value = str_dup_safe (base_url);
12312 +    if (extra_data)
12313 +       src->extra_data = str_dup_safe (extra_data);
12314 +    else
12315 +       src->extra_data = NULL;
12316 +    return 0;
12317 +}
12318 +
12319 +void pkg_src_deinit(pkg_src_t *src)
12320 +{
12321 +    free (src->name);
12322 +    free (src->value);
12323 +    if (src->extra_data)
12324 +       free (src->extra_data);
12325 +}
12326 +
12327 +
12328 diff -urN busybox.old/archival/libipkg/pkg_src.h busybox.dev/archival/libipkg/pkg_src.h
12329 --- busybox.old/archival/libipkg/pkg_src.h      1970-01-01 01:00:00.000000000 +0100
12330 +++ busybox.dev/archival/libipkg/pkg_src.h      2007-01-22 13:41:03.000000000 +0100
12331 @@ -0,0 +1,34 @@
12332 +/* pkg_src.h - the itsy package management system
12333 +
12334 +   Carl D. Worth
12335 +
12336 +   Copyright (C) 2001 University of Southern California
12337 +
12338 +   This program is free software; you can redistribute it and/or
12339 +   modify it under the terms of the GNU General Public License as
12340 +   published by the Free Software Foundation; either version 2, or (at
12341 +   your option) any later version.
12342 +
12343 +   This program is distributed in the hope that it will be useful, but
12344 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12345 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12346 +   General Public License for more details.
12347 +*/
12348 +
12349 +#ifndef PKG_SRC_H
12350 +#define PKG_SRC_H
12351 +
12352 +#include "nv_pair.h"
12353 +
12354 +typedef struct 
12355 +{
12356 +  char *name;
12357 +  char *value;
12358 +  char *extra_data;
12359 +  int gzip;
12360 +} pkg_src_t;
12361 +
12362 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip);
12363 +void pkg_src_deinit(pkg_src_t *src);
12364 +
12365 +#endif
12366 diff -urN busybox.old/archival/libipkg/pkg_src_list.c busybox.dev/archival/libipkg/pkg_src_list.c
12367 --- busybox.old/archival/libipkg/pkg_src_list.c 1970-01-01 01:00:00.000000000 +0100
12368 +++ busybox.dev/archival/libipkg/pkg_src_list.c 2007-01-22 13:41:03.000000000 +0100
12369 @@ -0,0 +1,75 @@
12370 +/* pkg_src_list.c - the itsy package management system
12371 +
12372 +   Carl D. Worth
12373 +
12374 +   Copyright (C) 2001 University of Southern California
12375 +
12376 +   This program is free software; you can redistribute it and/or
12377 +   modify it under the terms of the GNU General Public License as
12378 +   published by the Free Software Foundation; either version 2, or (at
12379 +   your option) any later version.
12380 +
12381 +   This program is distributed in the hope that it will be useful, but
12382 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12383 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12384 +   General Public License for more details.
12385 +*/
12386 +
12387 +#include "ipkg.h"
12388 +
12389 +#include "pkg_src_list.h"
12390 +#include "void_list.h"
12391 +
12392 +int pkg_src_list_init(pkg_src_list_t *list)
12393 +{
12394 +    return void_list_init((void_list_t *) list);
12395 +}
12396 +
12397 +void pkg_src_list_deinit(pkg_src_list_t *list)
12398 +{
12399 +    pkg_src_list_elt_t *iter;
12400 +    pkg_src_t *pkg_src;
12401 +
12402 +    for (iter = list->head; iter; iter = iter->next) {
12403 +      pkg_src = iter->data;
12404 +      pkg_src_deinit(pkg_src);
12405 +
12406 +      /* malloced in pkg_src_list_append */
12407 +      free(pkg_src);
12408 +      iter->data = NULL;
12409 +    }
12410 +    void_list_deinit((void_list_t *) list);
12411 +}
12412 +
12413 +pkg_src_t *pkg_src_list_append(pkg_src_list_t *list,
12414 +                              const char *name, const char *base_url, const char *extra_data,
12415 +                              int gzip)
12416 +{
12417 +    int err;
12418 +
12419 +    /* freed in pkg_src_list_deinit */
12420 +    pkg_src_t *pkg_src = malloc(sizeof(pkg_src_t));
12421 +
12422 +    if (pkg_src == NULL) {
12423 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12424 +       return NULL;
12425 +    }
12426 +    pkg_src_init(pkg_src, name, base_url, extra_data, gzip);
12427 +
12428 +    err = void_list_append((void_list_t *) list, pkg_src);
12429 +    if (err) {
12430 +       return NULL;
12431 +    }
12432 +
12433 +    return pkg_src;
12434 +}
12435 +
12436 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data)
12437 +{
12438 +    return void_list_push((void_list_t *) list, data);
12439 +}
12440 +
12441 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list)
12442 +{
12443 +    return (pkg_src_list_elt_t *) void_list_pop((void_list_t *) list);
12444 +}
12445 diff -urN busybox.old/archival/libipkg/pkg_src_list.h busybox.dev/archival/libipkg/pkg_src_list.h
12446 --- busybox.old/archival/libipkg/pkg_src_list.h 1970-01-01 01:00:00.000000000 +0100
12447 +++ busybox.dev/archival/libipkg/pkg_src_list.h 2007-01-22 13:41:03.000000000 +0100
12448 @@ -0,0 +1,57 @@
12449 +/* pkg_src_list.h - the itsy package management system
12450 +
12451 +   Carl D. Worth
12452 +
12453 +   Copyright (C) 2001 University of Southern California
12454 +
12455 +   This program is free software; you can redistribute it and/or
12456 +   modify it under the terms of the GNU General Public License as
12457 +   published by the Free Software Foundation; either version 2, or (at
12458 +   your option) any later version.
12459 +
12460 +   This program is distributed in the hope that it will be useful, but
12461 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12462 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12463 +   General Public License for more details.
12464 +*/
12465 +
12466 +#ifndef PKG_SRC_LIST_H
12467 +#define PKG_SRC_LIST_H
12468 +
12469 +#include "pkg_src.h"
12470 +
12471 +typedef struct pkg_src_list_elt pkg_src_list_elt_t;
12472 +struct pkg_src_list_elt
12473 +{
12474 +    pkg_src_list_elt_t *next;
12475 +    pkg_src_t *data;
12476 +};
12477 +
12478 +typedef struct pkg_src_list pkg_src_list_t;
12479 +struct pkg_src_list
12480 +{
12481 +    pkg_src_list_elt_t pre_head;
12482 +    pkg_src_list_elt_t *head;
12483 +    pkg_src_list_elt_t *tail;
12484 +};
12485 +
12486 +static inline int pkg_src_list_empty(pkg_src_list_t *list)
12487 +{
12488 +     if (list->head == NULL)
12489 +         return 1;
12490 +     else
12491 +         return 0;
12492 +}
12493 +
12494 +int pkg_src_list_elt_init(pkg_src_list_elt_t *elt, nv_pair_t *data);
12495 +void pkg_src_list_elt_deinit(pkg_src_list_elt_t *elt);
12496 +
12497 +int pkg_src_list_init(pkg_src_list_t *list);
12498 +void pkg_src_list_deinit(pkg_src_list_t *list);
12499 +
12500 +pkg_src_t *pkg_src_list_append(pkg_src_list_t *list, const char *name, const char *root_dir, const char *extra_data, int gzip);
12501 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data);
12502 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list);
12503 +
12504 +#endif
12505 +
12506 diff -urN busybox.old/archival/libipkg/pkg_vec.c busybox.dev/archival/libipkg/pkg_vec.c
12507 --- busybox.old/archival/libipkg/pkg_vec.c      1970-01-01 01:00:00.000000000 +0100
12508 +++ busybox.dev/archival/libipkg/pkg_vec.c      2007-01-22 13:41:03.000000000 +0100
12509 @@ -0,0 +1,230 @@
12510 +/* pkg_vec.c - the itsy package management system
12511 +
12512 +   Steven M. Ayer
12513 +   
12514 +   Copyright (C) 2002 Compaq Computer Corporation
12515 +
12516 +   This program is free software; you can redistribute it and/or
12517 +   modify it under the terms of the GNU General Public License as
12518 +   published by the Free Software Foundation; either version 2, or (at
12519 +   your option) any later version.
12520 +
12521 +   This program is distributed in the hope that it will be useful, but
12522 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12523 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12524 +   General Public License for more details.
12525 +*/
12526 +
12527 +#include <stdlib.h>
12528 +#include <fnmatch.h>
12529 +#include "xregex.h"
12530 +#include "ipkg.h"
12531 +#include "pkg.h"
12532 +
12533 +pkg_vec_t * pkg_vec_alloc(void)
12534 +{
12535 +    pkg_vec_t * vec = (pkg_vec_t *)malloc(sizeof(pkg_vec_t));
12536 +    if (!vec) {
12537 +      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12538 +      return NULL;
12539 +    }
12540 +    vec->pkgs = NULL;
12541 +    vec->len = 0;
12542 +
12543 +    return vec;
12544 +}
12545 +
12546 +void pkg_vec_free(pkg_vec_t *vec)
12547 +{
12548 +    free(vec->pkgs);
12549 +    free(vec);
12550 +}
12551 +
12552 +/*
12553 + * assumption: all names in a vector are identical
12554 + * assumption: all version strings are trimmed,
12555 + *             so identical versions have identical version strings,
12556 + *             implying identical packages; let's marry these
12557 + */
12558 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
12559 +{
12560 +     int i;
12561 +     int found = 0;
12562 +
12563 +     /* look for a duplicate pkg by name, version, and architecture */
12564 +     for (i = 0; i < vec->len; i++){
12565 +         ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found pkg=%s version=%s arch=%s cmp=%s version=%s arch=%s \n", 
12566 +                      __FUNCTION__, pkg->name, pkg->version, pkg->architecture, 
12567 +                       vec->pkgs[i]->name, vec->pkgs[i]->version,vec->pkgs[i]->architecture );
12568 +         if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12569 +             && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12570 +             && (strcmp(pkg->architecture, vec->pkgs[i]->architecture) == 0)) {
12571 +              found  = 1;
12572 +               ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found duplicate for pkg=%s version=%s arch=%s\n",
12573 +                             __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12574 +              break;
12575 +         }
12576 +     }
12577 +
12578 +     /* we didn't find one, add it */
12579 +     if (!found){   
12580 +         ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
12581 +                      __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12582 +
12583 +         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12584 +         vec->pkgs[vec->len] = pkg;
12585 +         vec->len++;
12586 +         return pkg;
12587 +     }
12588 +     /* update the one that we have */
12589 +     else {
12590 +          ipkg_message(conf, IPKG_DEBUG2, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
12591 +                        __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12592 +         if (set_status) {
12593 +              /* this is from the status file, so need to merge with existing database */
12594 +               ipkg_message(conf, IPKG_DEBUG2, " with set_status\n");
12595 +              pkg_merge(vec->pkgs[i], pkg, set_status);
12596 +              /* XXX: CLEANUP: It's not so polite to free something here
12597 +                 that was passed in from above. */
12598 +              pkg_deinit(pkg);
12599 +              free(pkg);
12600 +         } else {
12601 +               ipkg_message(conf, IPKG_DEBUG2, " WITHOUT set_status\n");
12602 +              /* just overwrite the old one */
12603 +              pkg_deinit(vec->pkgs[i]);
12604 +              free(vec->pkgs[i]);
12605 +              vec->pkgs[i] = pkg;
12606 +         }
12607 +         return vec->pkgs[i];
12608 +     }
12609 +}
12610 +
12611 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
12612 +{
12613 +     int i;
12614 +     int found = 0;
12615 +
12616 +     /* look for a duplicate pkg by name, version, and architecture */
12617 +     for (i = 0; i < vec->len; i++)
12618 +         if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12619 +             && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12620 +             && (strcmp(pkg->architecture, vec->pkgs[i]->name) == 0)) {
12621 +              found = 1;
12622 +              break;
12623 +         }
12624 +
12625 +     /* we didn't find one, add it */
12626 +     if(!found){   
12627 +         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12628 +         *(const pkg_t **)&vec->pkgs[vec->len] = pkg;
12629 +         vec->len++;
12630 +     }
12631 +}
12632 +
12633 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
12634 +{
12635 +     int i;
12636 +     for (i = 0; i < vec->len; i++)
12637 +         if (vec->pkgs[i] == apkg)
12638 +              return 1;
12639 +     return 0;
12640 +}
12641 +
12642 +typedef int (*compare_fcn_t)(const void *, const void *);
12643 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *))
12644 +{
12645 +     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12646 +}
12647 +
12648 +int pkg_vec_clear_marks(pkg_vec_t *vec)
12649 +{
12650 +     int npkgs = vec->len;
12651 +     int i;
12652 +     for (i = 0; i < npkgs; i++) {
12653 +         pkg_t *pkg = vec->pkgs[i];
12654 +         pkg->state_flag &= ~SF_MARKED;
12655 +     }
12656 +     return 0;
12657 +}
12658 +
12659 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
12660 +{
12661 +     int matching_count = 0;
12662 +     pkg_t **pkgs = vec->pkgs;
12663 +     int npkgs = vec->len;
12664 +     int i;
12665 +     for (i = 0; i < npkgs; i++) {
12666 +         pkg_t *pkg = pkgs[i];
12667 +         if (fnmatch(pattern, pkg->name, 0)==0) {
12668 +              pkg->state_flag |= SF_MARKED;
12669 +              matching_count++;
12670 +         }
12671 +     }
12672 +     return matching_count;
12673 +}
12674 +
12675 +
12676 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
12677 +{
12678 +    abstract_pkg_vec_t * vec ; 
12679 +    vec = (abstract_pkg_vec_t *)malloc(sizeof(abstract_pkg_vec_t));
12680 +    if (!vec) {
12681 +      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12682 +      return NULL;
12683 +    }
12684 +    vec->pkgs = NULL;
12685 +    vec->len = 0;
12686 +
12687 +    return vec;
12688 +}
12689 +
12690 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
12691 +{
12692 +    free(vec->pkgs);
12693 +    free(vec);
12694 +}
12695 +
12696 +/*
12697 + * assumption: all names in a vector are unique
12698 + */
12699 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
12700 +{
12701 +    int i;
12702 +
12703 +    /* look for a duplicate pkg by name */
12704 +    for(i = 0; i < vec->len; i++)
12705 +       if (strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12706 +           break;
12707 +
12708 +    /* we didn't find one, add it */
12709 +    if(i == vec->len){   
12710 +       vec->pkgs = 
12711 +         (abstract_pkg_t **)
12712 +           realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
12713 +       vec->pkgs[vec->len] = pkg;
12714 +       vec->len++;
12715 +    }
12716 +}
12717 +
12718 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
12719 +{
12720 +    if (vec->len > i) 
12721 +       return vec->pkgs[i];
12722 +    else
12723 +       return NULL;
12724 +}
12725 +
12726 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
12727 +{
12728 +     int i;
12729 +     for (i = 0; i < vec->len; i++)
12730 +         if (vec->pkgs[i] == apkg)
12731 +              return 1;
12732 +     return 0;
12733 +}
12734 +
12735 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *))
12736 +{
12737 +     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12738 +}
12739 +
12740 diff -urN busybox.old/archival/libipkg/pkg_vec.h busybox.dev/archival/libipkg/pkg_vec.h
12741 --- busybox.old/archival/libipkg/pkg_vec.h      1970-01-01 01:00:00.000000000 +0100
12742 +++ busybox.dev/archival/libipkg/pkg_vec.h      2007-01-22 13:41:06.000000000 +0100
12743 @@ -0,0 +1,64 @@
12744 +/* pkg_vec.h - the itsy package management system
12745 +
12746 +   Steven M. Ayer
12747 +   
12748 +   Copyright (C) 2002 Compaq Computer Corporation
12749 +
12750 +   This program is free software; you can redistribute it and/or
12751 +   modify it under the terms of the GNU General Public License as
12752 +   published by the Free Software Foundation; either version 2, or (at
12753 +   your option) any later version.
12754 +
12755 +   This program is distributed in the hope that it will be useful, but
12756 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12757 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12758 +   General Public License for more details.
12759 +*/
12760 +
12761 +#ifndef PKG_VEC_H
12762 +#define PKG_VEC_H
12763 +
12764 +typedef struct pkg pkg_t;
12765 +typedef struct abstract_pkg abstract_pkg_t;
12766 +
12767 +struct pkg_vec
12768 +{
12769 +    pkg_t **pkgs;
12770 +    int len;
12771 +};
12772 +typedef struct pkg_vec pkg_vec_t;
12773 +
12774 +struct abstract_pkg_vec
12775 +{
12776 +    abstract_pkg_t **pkgs;
12777 +    int len;
12778 +};
12779 +typedef struct abstract_pkg_vec abstract_pkg_vec_t;
12780 +
12781 +typedef int (*pkg_compar_t)(pkg_t *, pkg_t *);
12782 +typedef int (*abstract_pkg_compar_t)(abstract_pkg_t *, abstract_pkg_t *);
12783 +
12784 +pkg_vec_t * pkg_vec_alloc(void);
12785 +void pkg_vec_free(pkg_vec_t *vec);
12786 +void marry_two_packages(pkg_t * newpkg, pkg_t * oldpkg);
12787 +
12788 +void pkg_vec_add(pkg_vec_t *vec, pkg_t *pkg);
12789 +/* pkg_vec_insert_merge: might munge pkg.
12790 +*  returns the pkg that is in the pkg graph */
12791 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status, ipkg_conf_t *conf);
12792 +/* this one never munges pkg */
12793 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg);
12794 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg);
12795 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *));
12796 +
12797 +int pkg_vec_clear_marks(pkg_vec_t *vec);
12798 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern);
12799 +
12800 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void);
12801 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec);
12802 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg);
12803 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i);
12804 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg);
12805 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *));
12806 +#endif
12807 +
12808 diff -urN busybox.old/archival/libipkg/sprintf_alloc.h busybox.dev/archival/libipkg/sprintf_alloc.h
12809 --- busybox.old/archival/libipkg/sprintf_alloc.h        1970-01-01 01:00:00.000000000 +0100
12810 +++ busybox.dev/archival/libipkg/sprintf_alloc.h        2007-01-22 13:41:03.000000000 +0100
12811 @@ -0,0 +1,25 @@
12812 +/* sprintf_alloca.c -- like sprintf with memory allocation
12813 +
12814 +   Carl D. Worth
12815 +
12816 +   Copyright (C) 2001 University of Southern California
12817 +
12818 +   This program is free software; you can redistribute it and/or modify
12819 +   it under the terms of the GNU General Public License as published by
12820 +   the Free Software Foundation; either version 2, or (at your option)
12821 +   any later version.
12822 +
12823 +   This program is distributed in the hope that it will be useful,
12824 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
12825 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12826 +   GNU General Public License for more details.
12827 +*/
12828 +
12829 +#ifndef SPRINTF_ALLOC_H
12830 +#define SPRINTF_ALLOC_H
12831 +
12832 +#include "libbb.h"
12833 +
12834 +#define sprintf_alloc(str, fmt, args...)  *str = xasprintf(fmt, ## args)
12835 +
12836 +#endif
12837 diff -urN busybox.old/archival/libipkg/str_list.c busybox.dev/archival/libipkg/str_list.c
12838 --- busybox.old/archival/libipkg/str_list.c     1970-01-01 01:00:00.000000000 +0100
12839 +++ busybox.dev/archival/libipkg/str_list.c     2007-01-22 13:41:03.000000000 +0100
12840 @@ -0,0 +1,76 @@
12841 +/* str_list.c - the itsy package management system
12842 +
12843 +   Carl D. Worth
12844 +
12845 +   Copyright (C) 2001 University of Southern California
12846 +
12847 +   This program is free software; you can redistribute it and/or
12848 +   modify it under the terms of the GNU General Public License as
12849 +   published by the Free Software Foundation; either version 2, or (at
12850 +   your option) any later version.
12851 +
12852 +   This program is distributed in the hope that it will be useful, but
12853 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12854 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12855 +   General Public License for more details.
12856 +*/
12857 +
12858 +#include "ipkg.h"
12859 +
12860 +#include "str_list.h"
12861 +
12862 +int str_list_elt_init(str_list_elt_t *elt, char *data)
12863 +{
12864 +    return void_list_elt_init((void_list_elt_t *) elt, data);
12865 +}
12866 +
12867 +void str_list_elt_deinit(str_list_elt_t *elt)
12868 +{
12869 +    void_list_elt_deinit((void_list_elt_t *) elt);
12870 +}
12871 +
12872 +str_list_t *str_list_alloc()
12873 +{
12874 +     str_list_t *list = (str_list_t *)malloc(sizeof(str_list_t));
12875 +     if (list)
12876 +         str_list_init(list);
12877 +     return list;
12878 +}
12879 +
12880 +int str_list_init(str_list_t *list)
12881 +{
12882 +    return void_list_init((void_list_t *) list);
12883 +}
12884 +
12885 +void str_list_deinit(str_list_t *list)
12886 +{
12887 +    void_list_deinit((void_list_t *) list);
12888 +}
12889 +
12890 +int str_list_append(str_list_t *list, char *data)
12891 +{
12892 +    return void_list_append((void_list_t *) list, data);
12893 +}
12894 +
12895 +int str_list_push(str_list_t *list, char *data)
12896 +{
12897 +    return void_list_push((void_list_t *) list, data);
12898 +}
12899 +
12900 +str_list_elt_t *str_list_pop(str_list_t *list)
12901 +{
12902 +    return (str_list_elt_t *) void_list_pop((void_list_t *) list);
12903 +}
12904 +
12905 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter)
12906 +{
12907 +    return (str_list_elt_t *) void_list_remove((void_list_t *) list,
12908 +                                              (void_list_elt_t **) iter);
12909 +}
12910 +
12911 +char *str_list_remove_elt(str_list_t *list, const char *target_str)
12912 +{
12913 +     return (char *)void_list_remove_elt((void_list_t *) list,
12914 +                                        (void *)target_str,
12915 +                                        (void_list_cmp_t)strcmp);
12916 +}
12917 diff -urN busybox.old/archival/libipkg/str_list.h busybox.dev/archival/libipkg/str_list.h
12918 --- busybox.old/archival/libipkg/str_list.h     1970-01-01 01:00:00.000000000 +0100
12919 +++ busybox.dev/archival/libipkg/str_list.h     2007-01-22 13:41:03.000000000 +0100
12920 @@ -0,0 +1,51 @@
12921 +/* str_list.h - the itsy package management system
12922 +
12923 +   Carl D. Worth
12924 +
12925 +   Copyright (C) 2001 University of Southern California
12926 +
12927 +   This program is free software; you can redistribute it and/or
12928 +   modify it under the terms of the GNU General Public License as
12929 +   published by the Free Software Foundation; either version 2, or (at
12930 +   your option) any later version.
12931 +
12932 +   This program is distributed in the hope that it will be useful, but
12933 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12934 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12935 +   General Public License for more details.
12936 +*/
12937 +
12938 +#ifndef STR_LIST_H
12939 +#define STR_LIST_H
12940 +
12941 +#include "void_list.h"
12942 +
12943 +typedef struct str_list_elt str_list_elt_t;
12944 +struct str_list_elt
12945 +{
12946 +    str_list_elt_t *next;
12947 +    char *data;
12948 +};
12949 +
12950 +typedef struct xstr_list str_list_t;
12951 +struct xstr_list
12952 +{
12953 +    str_list_elt_t pre_head;
12954 +    str_list_elt_t *head;
12955 +    str_list_elt_t *tail;
12956 +};
12957 +
12958 +int str_list_elt_init(str_list_elt_t *elt, char *data);
12959 +void str_list_elt_deinit(str_list_elt_t *elt);
12960 +
12961 +str_list_t *str_list_alloc(void);
12962 +int str_list_init(str_list_t *list);
12963 +void str_list_deinit(str_list_t *list);
12964 +
12965 +int str_list_append(str_list_t *list, char *data);
12966 +int str_list_push(str_list_t *list, char *data);
12967 +str_list_elt_t *str_list_pop(str_list_t *list);
12968 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter);
12969 +char *str_list_remove_elt(str_list_t *list, const char *target_str);
12970 +
12971 +#endif
12972 diff -urN busybox.old/archival/libipkg/str_util.c busybox.dev/archival/libipkg/str_util.c
12973 --- busybox.old/archival/libipkg/str_util.c     1970-01-01 01:00:00.000000000 +0100
12974 +++ busybox.dev/archival/libipkg/str_util.c     2007-01-22 13:41:03.000000000 +0100
12975 @@ -0,0 +1,73 @@
12976 +/* str_utils.c - the itsy package management system
12977 +
12978 +   Carl D. Worth
12979 +
12980 +   Copyright (C) 2001 University of Southern California
12981 +
12982 +   This program is free software; you can redistribute it and/or
12983 +   modify it under the terms of the GNU General Public License as
12984 +   published by the Free Software Foundation; either version 2, or (at
12985 +   your option) any later version.
12986 +
12987 +   This program is distributed in the hope that it will be useful, but
12988 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12989 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12990 +   General Public License for more details.
12991 +*/
12992 +
12993 +#include "ipkg.h"
12994 +
12995 +int str_starts_with(const char *str, const char *prefix)
12996 +{
12997 +    return (strncmp(str, prefix, strlen(prefix)) == 0);
12998 +}
12999 +
13000 +int str_ends_with(const char *str, const char *suffix)
13001 +{
13002 +    int suffix_len;
13003 +    int str_len;
13004 +
13005 +    str_len = strlen(str);
13006 +    suffix_len = strlen(suffix);
13007 +
13008 +    if (str_len < suffix_len) {
13009 +       return 0;
13010 +    }
13011 +
13012 +    return (strcmp(str + str_len - suffix_len, suffix) == 0);
13013 +}
13014 +
13015 +int str_chomp(char *str)
13016 +{
13017 +    if (str[strlen(str) - 1] == '\n') {
13018 +       str[strlen(str) - 1] = '\0';
13019 +       return 1;
13020 +    }
13021 +    return 0;
13022 +}
13023 +
13024 +int str_tolower(char *str)
13025 +{
13026 +    while (*str) {
13027 +       *str = tolower(*str);
13028 +       str++;
13029 +    }
13030 +
13031 +    return 0;
13032 +}
13033 +
13034 +int str_toupper(char *str)
13035 +{
13036 +    while (*str) {
13037 +       *str = toupper(*str);
13038 +       str++;
13039 +    }
13040 +
13041 +    return 0;
13042 +}
13043 +
13044 +char *str_dup_safe(const char *str)
13045 +{
13046 +    return str ? strdup(str) : NULL;
13047 +}
13048 +
13049 diff -urN busybox.old/archival/libipkg/str_util.h busybox.dev/archival/libipkg/str_util.h
13050 --- busybox.old/archival/libipkg/str_util.h     1970-01-01 01:00:00.000000000 +0100
13051 +++ busybox.dev/archival/libipkg/str_util.h     2007-01-22 13:41:03.000000000 +0100
13052 @@ -0,0 +1,28 @@
13053 +/* str_utils.h - the itsy package management system
13054 +
13055 +   Carl D. Worth
13056 +
13057 +   Copyright (C) 2001 University of Southern California
13058 +
13059 +   This program is free software; you can redistribute it and/or
13060 +   modify it under the terms of the GNU General Public License as
13061 +   published by the Free Software Foundation; either version 2, or (at
13062 +   your option) any later version.
13063 +
13064 +   This program is distributed in the hope that it will be useful, but
13065 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13066 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13067 +   General Public License for more details.
13068 +*/
13069 +
13070 +#ifndef STR_UTILS_H
13071 +#define STR_UTILS_H
13072 +
13073 +int str_starts_with(const char *str, const char *prefix);
13074 +int str_ends_with(const char *str, const char *suffix);
13075 +int str_chomp(char *str);
13076 +int str_tolower(char *str);
13077 +int str_toupper(char *str);
13078 +char *str_dup_safe(const char *str);
13079 +
13080 +#endif
13081 diff -urN busybox.old/archival/libipkg/user.c busybox.dev/archival/libipkg/user.c
13082 --- busybox.old/archival/libipkg/user.c 1970-01-01 01:00:00.000000000 +0100
13083 +++ busybox.dev/archival/libipkg/user.c 2007-01-22 13:41:03.000000000 +0100
13084 @@ -0,0 +1,58 @@
13085 +/* user.c - the itsy package management system
13086 +
13087 +   Jamey Hicks
13088 +
13089 +   Copyright (C) 2002 Hewlett Packard Company
13090 +   Copyright (C) 2001 University of Southern California
13091 +
13092 +   This program is free software; you can redistribute it and/or
13093 +   modify it under the terms of the GNU General Public License as
13094 +   published by the Free Software Foundation; either version 2, or (at
13095 +   your option) any later version.
13096 +
13097 +   This program is distributed in the hope that it will be useful, but
13098 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13099 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13100 +   General Public License for more details.
13101 +*/
13102 +
13103 +#include <stdio.h>
13104 +#include <stdarg.h>
13105 +#include "file_util.h"
13106 +#include "str_util.h"
13107 +#ifdef IPKG_LIB        
13108 +#include "libipkg.h"
13109 +#endif 
13110 +
13111 +
13112 +#ifdef IPKG_LIB
13113 +static char *question = NULL;
13114 +static int question_len = 255;
13115 +#endif 
13116 +char *get_user_response(const char *format, ...)
13117 +{
13118 +     int len = question_len;
13119 +     va_list ap;
13120 +     char *response;
13121 +     va_start(ap, format);
13122 +
13123 +#ifndef IPKG_LIB
13124 +     vprintf(format, ap);
13125 +     do {
13126 +         response = file_read_line_alloc(stdin);
13127 +     } while (response == NULL);
13128 +#else
13129 +     do {
13130 +         if (question == NULL || len > question_len) {
13131 +              question = realloc(question, len + 1);
13132 +              question_len = len;
13133 +         }
13134 +         len = vsnprintf(question,question_len,format,ap);
13135 +     } while (len > question_len);
13136 +     response = strdup(ipkg_cb_response(question));
13137 +#endif
13138 +     str_chomp(response);
13139 +     str_tolower(response);
13140 +
13141 +     return response;
13142 +}
13143 diff -urN busybox.old/archival/libipkg/user.h busybox.dev/archival/libipkg/user.h
13144 --- busybox.old/archival/libipkg/user.h 1970-01-01 01:00:00.000000000 +0100
13145 +++ busybox.dev/archival/libipkg/user.h 2007-01-22 13:41:03.000000000 +0100
13146 @@ -0,0 +1,23 @@
13147 +/* user.c - the itsy package management system
13148 +
13149 +   Jamey Hicks
13150 +
13151 +   Copyright (C) 2002 Hewlett Packard Company
13152 +   Copyright (C) 2001 University of Southern California
13153 +
13154 +   This program is free software; you can redistribute it and/or
13155 +   modify it under the terms of the GNU General Public License as
13156 +   published by the Free Software Foundation; either version 2, or (at
13157 +   your option) any later version.
13158 +
13159 +   This program is distributed in the hope that it will be useful, but
13160 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13161 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13162 +   General Public License for more details.
13163 +*/
13164 +
13165 +#include <stdio.h>
13166 +#include <stdarg.h>
13167 +
13168 +char *get_user_response(const char *format, ...);
13169 +
13170 diff -urN busybox.old/archival/libipkg/void_list.c busybox.dev/archival/libipkg/void_list.c
13171 --- busybox.old/archival/libipkg/void_list.c    1970-01-01 01:00:00.000000000 +0100
13172 +++ busybox.dev/archival/libipkg/void_list.c    2007-01-22 13:41:03.000000000 +0100
13173 @@ -0,0 +1,194 @@
13174 +/* void_list.c - the itsy package management system
13175 +
13176 +   Carl D. Worth
13177 +
13178 +   Copyright (C) 2001 University of Southern California
13179 +
13180 +   This program is free software; you can redistribute it and/or
13181 +   modify it under the terms of the GNU General Public License as
13182 +   published by the Free Software Foundation; either version 2, or (at
13183 +   your option) any later version.
13184 +
13185 +   This program is distributed in the hope that it will be useful, but
13186 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13187 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13188 +   General Public License for more details.
13189 +*/
13190 +
13191 +#include "ipkg.h"
13192 +#include <errno.h>
13193 +
13194 +#include "void_list.h"
13195 +
13196 +int void_list_elt_init(void_list_elt_t *elt, void *data)
13197 +{
13198 +    elt->next = NULL;
13199 +    elt->data = data;
13200 +
13201 +    return 0;
13202 +}
13203 +
13204 +void void_list_elt_deinit(void_list_elt_t *elt)
13205 +{
13206 +    void_list_elt_init(elt, NULL);
13207 +}
13208 +
13209 +int void_list_init(void_list_t *list)
13210 +{
13211 +    void_list_elt_init(&list->pre_head, NULL);
13212 +    list->head = NULL;
13213 +    list->pre_head.next = list->head;
13214 +    list->tail = NULL;
13215 +
13216 +    return 0;
13217 +}
13218 +
13219 +void void_list_deinit(void_list_t *list)
13220 +{
13221 +    void_list_elt_t *elt;
13222 +
13223 +    while (list->head) {
13224 +       elt = void_list_pop(list);
13225 +       void_list_elt_deinit(elt);
13226 +       /* malloced in void_list_append */
13227 +       free(elt);
13228 +    }
13229 +}
13230 +
13231 +int void_list_append(void_list_t *list, void *data)
13232 +{
13233 +    void_list_elt_t *elt;
13234 +
13235 +    /* freed in void_list_deinit */
13236 +    elt = malloc(sizeof(void_list_elt_t));
13237 +    if (elt == NULL) {
13238 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13239 +       return ENOMEM;
13240 +    }
13241 +
13242 +    void_list_elt_init(elt, data);
13243 +
13244 +    if (list->tail) {
13245 +       list->tail->next = elt;
13246 +       list->tail = elt;
13247 +    } else {
13248 +       list->head = elt;
13249 +       list->pre_head.next = list->head;
13250 +       list->tail = elt;
13251 +    }
13252 +
13253 +    return 0;
13254 +}
13255 +
13256 +int void_list_push(void_list_t *list, void *data)
13257 +{
13258 +    void_list_elt_t *elt;
13259 +
13260 +    elt = malloc(sizeof(void_list_elt_t));
13261 +    if (elt == NULL) {
13262 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13263 +       return ENOMEM;
13264 +    }
13265 +
13266 +    void_list_elt_init(elt, data);
13267 +
13268 +    elt->next = list->head;
13269 +    list->head->next = elt;
13270 +    if (list->tail == NULL) {
13271 +       list->tail = list->head;
13272 +    }
13273 +
13274 +    return 0;
13275 +}
13276 +
13277 +void_list_elt_t *void_list_pop(void_list_t *list)
13278 +{
13279 +    void_list_elt_t *elt;
13280 +
13281 +    elt = list->head;
13282 +
13283 +    if (list->head) {
13284 +       list->head = list->head->next;
13285 +       list->pre_head.next = list->head;
13286 +       if (list->head == NULL) {
13287 +           list->tail = NULL;
13288 +       }
13289 +    }
13290 +
13291 +    return elt;
13292 +}
13293 +
13294 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
13295 +{
13296 +    void_list_elt_t *prior;
13297 +    void_list_elt_t *old_elt;
13298 +    void *old_data;
13299 +
13300 +    old_elt = *iter;
13301 +    old_data = old_elt->data;
13302 +
13303 +    if (old_elt == list->head) {
13304 +       prior = &list->pre_head;
13305 +       void_list_pop(list);
13306 +    } else {
13307 +       for (prior = list->head; prior; prior = prior->next) {
13308 +           if (prior->next == old_elt) {
13309 +               break;
13310 +           }
13311 +       }
13312 +       if (prior == NULL || prior->next != old_elt) {
13313 +           fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
13314 +           return NULL;
13315 +       }
13316 +       prior->next = old_elt->next;
13317 +
13318 +       if (old_elt == list->tail) {
13319 +           list->tail = prior;
13320 +       }
13321 +    }
13322 +
13323 +    void_list_elt_deinit(old_elt);
13324 +    *iter = prior;
13325 +
13326 +    return old_data;
13327 +}
13328 +
13329 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13330 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
13331 +{
13332 +     void_list_elt_t *prior;
13333 +     void_list_elt_t *old_elt = NULL;
13334 +     void *old_data = NULL;
13335 +
13336 +     /* first element */
13337 +     if (list->head && list->head->data && (cmp(list->head->data, target_data) == 0)) {
13338 +         old_elt = list->head;
13339 +         old_data = list->head->data;
13340 +         void_list_pop(list);
13341 +     } else {
13342 +         int found = 0;
13343 +         for (prior = list->head; prior && prior->next; prior = prior->next) {
13344 +              if (prior->next->data && (cmp(prior->next->data, target_data) == 0)) {
13345 +                   old_elt = prior->next;
13346 +                   old_data = old_elt->data;
13347 +                   found = 1;
13348 +                   break;
13349 +              }
13350 +         }
13351 +         if (!found) {
13352 +              return NULL;
13353 +         }
13354 +         prior->next = old_elt->next;
13355 +
13356 +         if (old_elt == list->tail) {
13357 +              list->tail = prior;
13358 +         }
13359 +     }
13360 +     if (old_elt)
13361 +         void_list_elt_deinit(old_elt);
13362 +
13363 +     if (old_data)
13364 +         return old_data;
13365 +     else
13366 +         return NULL;
13367 +}
13368 diff -urN busybox.old/archival/libipkg/void_list.h busybox.dev/archival/libipkg/void_list.h
13369 --- busybox.old/archival/libipkg/void_list.h    1970-01-01 01:00:00.000000000 +0100
13370 +++ busybox.dev/archival/libipkg/void_list.h    2007-01-22 13:41:03.000000000 +0100
13371 @@ -0,0 +1,59 @@
13372 +/* void_list.h - the itsy package management system
13373 +
13374 +   Carl D. Worth
13375 +
13376 +   Copyright (C) 2001 University of Southern California
13377 +
13378 +   This program is free software; you can redistribute it and/or
13379 +   modify it under the terms of the GNU General Public License as
13380 +   published by the Free Software Foundation; either version 2, or (at
13381 +   your option) any later version.
13382 +
13383 +   This program is distributed in the hope that it will be useful, but
13384 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13385 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13386 +   General Public License for more details.
13387 +*/
13388 +
13389 +#ifndef VOID_LIST_H
13390 +#define VOID_LIST_H
13391 +
13392 +typedef struct void_list_elt void_list_elt_t;
13393 +struct void_list_elt
13394 +{
13395 +    void_list_elt_t *next;
13396 +    void *data;
13397 +};
13398 +
13399 +typedef struct void_list void_list_t;
13400 +struct void_list
13401 +{
13402 +    void_list_elt_t pre_head;
13403 +    void_list_elt_t *head;
13404 +    void_list_elt_t *tail;
13405 +};
13406 +
13407 +static inline int void_list_empty(void_list_t *list)
13408 +{
13409 +     if (list->head == NULL)
13410 +         return 1;
13411 +     else
13412 +         return 0;
13413 +}
13414 +
13415 +int void_list_elt_init(void_list_elt_t *elt, void *data);
13416 +void void_list_elt_deinit(void_list_elt_t *elt);
13417 +
13418 +int void_list_init(void_list_t *list);
13419 +void void_list_deinit(void_list_t *list);
13420 +
13421 +int void_list_append(void_list_t *list, void *data);
13422 +int void_list_push(void_list_t *list, void *data);
13423 +void_list_elt_t *void_list_pop(void_list_t *list);
13424 +
13425 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter);
13426 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13427 +typedef int (*void_list_cmp_t)(const void *, const void *);
13428 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp);
13429 +
13430 +#endif
13431 diff -urN busybox.old/archival/libipkg/xsystem.c busybox.dev/archival/libipkg/xsystem.c
13432 --- busybox.old/archival/libipkg/xsystem.c      1970-01-01 01:00:00.000000000 +0100
13433 +++ busybox.dev/archival/libipkg/xsystem.c      2007-01-22 13:41:03.000000000 +0100
13434 @@ -0,0 +1,64 @@
13435 +/* xsystem.c - system(3) with error messages
13436 +
13437 +   Carl D. Worth
13438 +
13439 +   Copyright (C) 2001 University of Southern California
13440 +
13441 +   This program is free software; you can redistribute it and/or
13442 +   modify it under the terms of the GNU General Public License as
13443 +   published by the Free Software Foundation; either version 2, or (at
13444 +   your option) any later version.
13445 +
13446 +   This program is distributed in the hope that it will be useful, but
13447 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13448 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13449 +   General Public License for more details.
13450 +*/
13451 +
13452 +#include "ipkg.h"
13453 +#include <sys/wait.h>
13454 +
13455 +#include "xsystem.h"
13456 +
13457 +/* XXX: FEATURE: I shouldn't actually use system(3) at all. I don't
13458 +   really need the /bin/sh invocation which takes resources and
13459 +   introduces security problems. I should switch all of this to a sort
13460 +   of execl() or execv() interface/implementation.
13461 +*/
13462 +
13463 +/* Like system(3), but with error messages printed if the fork fails
13464 +   or if the child process dies due to an uncaught signal. Also, the
13465 +   return value is a bit simpler:
13466 +
13467 +   -1 if there was any problem
13468 +   Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13469 +   as defined in <sys/wait.h>.
13470 +*/
13471 +int xsystem(const char *cmd)
13472 +{
13473 +    int err;
13474 +
13475 +    err = system(cmd);
13476 +
13477 +    if (err == -1) {
13478 +       fprintf(stderr, "%s: ERROR: fork failed before execution: `%s'\n",
13479 +               __FUNCTION__, cmd);
13480 +       return -1;
13481 +    }
13482 +
13483 +    if (WIFSIGNALED(err)) {
13484 +       fprintf(stderr, "%s: ERROR: Child process died due to signal %d: `%s'\n",
13485 +               __FUNCTION__, WTERMSIG(err), cmd);
13486 +       return -1;
13487 +    }
13488 +
13489 +    if (WIFEXITED(err)) {
13490 +       /* Normal child exit */
13491 +       return WEXITSTATUS(err);
13492 +    }
13493 +
13494 +    fprintf(stderr, "%s: ERROR: Received unintelligible return value from system: %d",
13495 +           __FUNCTION__, err);
13496 +    return -1;
13497 +}
13498 +        
13499 diff -urN busybox.old/archival/libipkg/xsystem.h busybox.dev/archival/libipkg/xsystem.h
13500 --- busybox.old/archival/libipkg/xsystem.h      1970-01-01 01:00:00.000000000 +0100
13501 +++ busybox.dev/archival/libipkg/xsystem.h      2007-01-22 13:41:03.000000000 +0100
13502 @@ -0,0 +1,34 @@
13503 +/* xsystem.h - system(3) with error messages
13504 +
13505 +   Carl D. Worth
13506 +
13507 +   Copyright (C) 2001 University of Southern California
13508 +
13509 +   This program is free software; you can redistribute it and/or
13510 +   modify it under the terms of the GNU General Public License as
13511 +   published by the Free Software Foundation; either version 2, or (at
13512 +   your option) any later version.
13513 +
13514 +   This program is distributed in the hope that it will be useful, but
13515 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13516 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13517 +   General Public License for more details.
13518 +*/
13519 +
13520 +#ifndef XSYSTEM_H
13521 +#define XSYSTEM_H
13522 +
13523 +#include <stdlib.h>
13524 +
13525 +/* Like system(3), but with error messages printed if the fork fails
13526 +   or if the child process dies due to an uncaught signal. Also, the
13527 +   return value is a bit simpler:
13528 +
13529 +   -1 if there was any problem
13530 +   Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13531 +   as defined in <sys/wait.h>.
13532 +*/
13533 +int xsystem(const char *cmd);
13534 +
13535 +#endif
13536 +        
13537 diff -urN busybox.old/archival/libunarchive/data_extract_all.c busybox.dev/archival/libunarchive/data_extract_all.c
13538 --- busybox.old/archival/libunarchive/data_extract_all.c        2007-01-19 22:23:02.000000000 +0100
13539 +++ busybox.dev/archival/libunarchive/data_extract_all.c        2007-01-22 13:41:03.000000000 +0100
13540 @@ -117,3 +117,17 @@
13541                 utime(file_header->name, &t);
13542         }
13543  }
13544 +
13545 +extern void data_extract_all_prefix(archive_handle_t *archive_handle)
13546 +{
13547 +       char *name_ptr = archive_handle->file_header->name;
13548 +
13549 +       name_ptr += strspn(name_ptr, "./");
13550 +       if (name_ptr[0] != '\0') {
13551 +               archive_handle->file_header->name = xmalloc(strlen(archive_handle->buffer) + 1 + strlen(name_ptr) + 1);
13552 +               strcpy(archive_handle->file_header->name, archive_handle->buffer);
13553 +               strcat(archive_handle->file_header->name, name_ptr);
13554 +               data_extract_all(archive_handle);
13555 +       }
13556 +}
13557 +
13558 diff -urN busybox.old/archival/libunarchive/Kbuild busybox.dev/archival/libunarchive/Kbuild
13559 --- busybox.old/archival/libunarchive/Kbuild    2007-01-19 22:23:02.000000000 +0100
13560 +++ busybox.dev/archival/libunarchive/Kbuild    2007-01-22 13:41:03.000000000 +0100
13561 @@ -47,6 +47,7 @@
13562  lib-$(CONFIG_FEATURE_DEB_TAR_LZMA)     += decompress_unlzma.o get_header_tar_lzma.o
13563  lib-$(CONFIG_GUNZIP)                   += $(GUNZIP_FILES)
13564  lib-$(CONFIG_FEATURE_GUNZIP_UNCOMPRESS)        += decompress_uncompress.o
13565 +lib-$(CONFIG_IPKG)                     += $(GUNZIP_FILES) get_header_tar.o get_header_tar_gz.o
13566  lib-$(CONFIG_RPM2CPIO)                 += $(GUNZIP_FILES) get_header_cpio.o
13567  lib-$(CONFIG_RPM)                      += $(GUNZIP_FILES) get_header_cpio.o
13568  lib-$(CONFIG_TAR)                      += get_header_tar.o
13569 diff -urN busybox.old/include/applets.h busybox.dev/include/applets.h
13570 --- busybox.old/include/applets.h       2007-01-20 16:34:47.000000000 +0100
13571 +++ busybox.dev/include/applets.h       2007-01-22 13:41:03.000000000 +0100
13572 @@ -160,6 +160,7 @@
13573  USE_IPCALC(APPLET(ipcalc, _BB_DIR_BIN, _BB_SUID_NEVER))
13574  USE_IPCRM(APPLET(ipcrm, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13575  USE_IPCS(APPLET(ipcs, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13576 +USE_IPKG(APPLET(ipkg, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
13577  USE_IPLINK(APPLET(iplink, _BB_DIR_BIN, _BB_SUID_NEVER))
13578  USE_IPROUTE(APPLET(iproute, _BB_DIR_BIN, _BB_SUID_NEVER))
13579  USE_IPRULE(APPLET(iprule, _BB_DIR_BIN, _BB_SUID_NEVER))
13580 diff -urN busybox.old/include/unarchive.h busybox.dev/include/unarchive.h
13581 --- busybox.old/include/unarchive.h     2007-01-19 22:23:10.000000000 +0100
13582 +++ busybox.dev/include/unarchive.h     2007-01-22 13:41:03.000000000 +0100
13583 @@ -76,6 +76,7 @@
13584  
13585  extern void data_skip(archive_handle_t *archive_handle);
13586  extern void data_extract_all(archive_handle_t *archive_handle);
13587 +extern void data_extract_all_prefix(archive_handle_t *archive_handle);
13588  extern void data_extract_to_stdout(archive_handle_t *archive_handle);
13589  extern void data_extract_to_buffer(archive_handle_t *archive_handle);
13590  
13591 diff -urN busybox.old/include/usage.h busybox.dev/include/usage.h
13592 --- busybox.old/include/usage.h 2007-01-19 22:23:10.000000000 +0100
13593 +++ busybox.dev/include/usage.h 2007-01-22 13:41:03.000000000 +0100
13594 @@ -1133,6 +1133,82 @@
13595         "$ ls -la /tmp/busybox*\n" \
13596         "-rw-rw-r--    1 andersen andersen   554058 Apr 14 17:49 /tmp/busybox.tar.gz\n"
13597  
13598 +#define ipkg_trivial_usage \
13599 +       "[options]... sub-command [arguments]..."
13600 +#define ipkg_full_usage \
13601 +       "ipkg is an utility to install, remove and manage .ipk packages.\n" \
13602 +       "\n" \
13603 +       "Sub-commands:\n" \
13604 +       "\nPackage Manipulation:\n" \
13605 +       "\tupdate               Update list of available packages\n" \
13606 +       "\tupgrade                      Upgrade all installed packages to latest version\n" \
13607 +       "\tinstall <pkg>                Download and install <pkg> (and dependencies)\n" \
13608 +       "\tinstall <file.ipk>   Install package <file.ipk>\n" \
13609 +       "\tconfigure [<pkg>]    Configure unpacked packages\n" \
13610 +       "\tremove <pkg|regexp>  Remove package <pkg|packages following regexp>\n" \
13611 +       "\tflag <flag> <pkg> ...        Flag package(s) <pkg>\n" \
13612 +       "\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation) \n" \
13613 +       "\n" \
13614 +       "Informational Commands:\n" \
13615 +       "\tlist                 List available packages and descriptions\n" \
13616 +       "\tlist_installed               List all and only the installed packages and description \n" \
13617 +       "\tfiles <pkg>          List all files belonging to <pkg>\n" \
13618 +       "\tsearch <file|regexp>         Search for a package providing <file>\n" \
13619 +       "\tinfo [pkg|regexp [<field>]]  Display all/some info fields for <pkg> or all\n" \
13620 +       "\tstatus [pkg|regexp [<field>]]        Display all/some status fields for <pkg> or all\n" \
13621 +       "\tdownload <pkg>               Download <pkg> to current directory.\n" \
13622 +       "\tcompare_versions <v1> <op> <v2>\n" \
13623 +       "\t                          compare versions using <= < > >= = << >>\n" \
13624 +       "\tprint_architecture      prints the architecture.\n" \
13625 +       "\tprint_installation_architecture\n" \
13626 +       "\twhatdepends [-A] [pkgname|pat]+\n" \
13627 +       "\twhatdependsrec [-A] [pkgname|pat]+\n" \
13628 +       "\twhatprovides [-A] [pkgname|pat]+\n" \
13629 +       "\twhatconflicts [-A] [pkgname|pat]+\n" \
13630 +       "\twhatreplaces [-A] [pkgname|pat]+\n" \
13631 +       "\t                        prints the installation architecture.\n" \
13632 +       "\n" \
13633 +       "\nOptions:\n" \
13634 +       "\t-A                      Query all packages with whatdepends, whatprovides, whatreplaces, whatconflicts\n" \
13635 +       "\t-V <level>               Set verbosity level to <level>. If no value is\n" \
13636 +       "\t--verbosity <level>      provided increase verbosity by one. Verbosity levels:\n" \
13637 +       "\t                         0 errors only\n" \
13638 +       "\t                         1 normal messages (default)\n" \
13639 +       "\t                         2 informative messages\n" \
13640 +       "\t                         3 debug output\n" \
13641 +       "\t-f <conf_file>               Use <conf_file> as the ipkg configuration file\n" \
13642 +       "\t-conf <conf_file>    Default configuration file location\n" \
13643 +       "                               is /etc/ipkg.conf\n" \
13644 +       "\t-d <dest_name>               Use <dest_name> as the the root directory for\n" \
13645 +       "\t-dest <dest_name>    package installation, removal, upgrading.\n" \
13646 +       "                               <dest_name> should be a defined dest name from\n" \
13647 +       "                               the configuration file, (but can also be a\n" \
13648 +       "                               directory name in a pinch).\n" \
13649 +       "\t-o <offline_root>    Use <offline_root> as the root directory for\n" \
13650 +       "\t-offline <offline_root>      offline installation of packages.\n" \
13651 +       "\t-verbose_wget                more wget messages\n" \
13652 +       "\n" \
13653 +       "Force Options (use when ipkg is too smart for its own good):\n" \
13654 +       "\t-force-depends               Make dependency checks warnings instead of errors\n" \
13655 +       "\t                             Install/remove package in spite of failed dependences\n" \
13656 +       "\t-force-defaults              Use default options for questions asked by ipkg.\n" \
13657 +       "                               (no prompts). Note that this will not prevent\n" \
13658 +       "                               package installation scripts from prompting.\n" \
13659 +       "\t-force-reinstall     Allow ipkg to reinstall a package.\n" \
13660 +       "\t-force-overwrite     Allow ipkg to overwrite files from another package during an install.\n" \
13661 +       "\t-force-downgrade     Allow ipkg to downgrade packages.\n" \
13662 +       "\t-force_space            Install even if there does not seem to be enough space.\n" \
13663 +       "\t-noaction               No action -- test only\n" \
13664 +       "\t-nodeps                 Do not follow dependences\n" \
13665 +       "\t-force-removal-of-dependent-packages\n" \
13666 +       "\t-recursive           Allow ipkg to remove package and all that depend on it.\n" \
13667 +       "\t-test                   No action -- test only\n" \
13668 +       "\t-t                   Specify tmp-dir.\n" \
13669 +       "\t--tmp-dir            Specify tmp-dir.\n" \
13670 +       "\n" \
13671 +       "\tregexp could be something like 'pkgname*' '*file*' or similar\n" \
13672 +       "\teg: ipkg info 'libstd*' or ipkg search '*libop*' or ipkg remove 'libncur*'\n"
13673 +
13674  #define halt_trivial_usage \
13675         "[-d<delay>] [-n<nosync>] [-f<force>]"
13676  #define halt_full_usage \
13677 diff -urN busybox.old/Makefile busybox.dev/Makefile
13678 --- busybox.old/Makefile        2007-01-20 21:21:39.000000000 +0100
13679 +++ busybox.dev/Makefile        2007-01-22 13:41:03.000000000 +0100
13680 @@ -423,6 +423,7 @@
13681  
13682  libs-y         := \
13683                 archival/ \
13684 +               archival/libipkg/ \
13685                 archival/libunarchive/ \
13686                 console-tools/ \
13687                 coreutils/ \