revert [4599], the bug was fixed elsewhere
[openwrt.git] / package / busybox / patches / 911-ipkg.patch
1 # Copyright (C) 2006 OpenWrt.org
2 #
3 # This is free software, licensed under the GNU General Public License v2.
4 # See /LICENSE for more information.
5 #
6 # add ipkg support to busybox
7 #
8 diff -ruN busybox-1.2.0-orig/archival/Config.in busybox-1.2.0+ipkg-0.99.162/archival/Config.in
9 --- busybox-1.2.0-orig/archival/Config.in       2006-07-01 00:42:04.000000000 +0200
10 +++ busybox-1.2.0+ipkg-0.99.162/archival/Config.in      2006-07-22 16:31:25.000000000 +0200
11 @@ -121,6 +121,14 @@
12           gzip is used to compress files.
13           It's probably the most widely used UNIX compression program.
14  
15 +config CONFIG_IPKG
16 +       bool "ipkg"
17 +       default n
18 +       select CONFIG_MD5SUM
19 +       select CONFIG_WGET
20 +       help
21 +         ipkg is the itsy package management system.
22 +       
23  config CONFIG_RPM2CPIO
24         bool "rpm2cpio"
25         default n
26 diff -ruN busybox-1.2.0-orig/archival/dpkg.c busybox-1.2.0+ipkg-0.99.162/archival/dpkg.c
27 --- busybox-1.2.0-orig/archival/dpkg.c  2006-07-01 00:42:04.000000000 +0200
28 +++ busybox-1.2.0+ipkg-0.99.162/archival/dpkg.c 2006-07-22 16:31:25.000000000 +0200
29 @@ -1430,6 +1430,10 @@
30         return(ar_handle->sub_archive->buffer);
31  }
32  
33 +/*
34 +
35 +// moved to data_extract_all.c
36 +
37  static void data_extract_all_prefix(archive_handle_t *archive_handle)
38  {
39         char *name_ptr = archive_handle->file_header->name;
40 @@ -1442,6 +1446,8 @@
41         return;
42  }
43  
44 +*/
45 +
46  static void unpack_package(deb_file_t *deb_file)
47  {
48         const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
49 diff -ruN busybox-1.2.0-orig/archival/ipkg.c busybox-1.2.0+ipkg-0.99.162/archival/ipkg.c
50 --- busybox-1.2.0-orig/archival/ipkg.c  1970-01-01 01:00:00.000000000 +0100
51 +++ busybox-1.2.0+ipkg-0.99.162/archival/ipkg.c 2006-07-22 16:31:25.000000000 +0200
52 @@ -0,0 +1,26 @@
53 +/* ipkg.c - the itsy package management system
54 +
55 +   Florina Boor
56 +
57 +   Copyright (C) 2003 kernel concepts
58 +
59 +   This program is free software; you can redistribute it and/or
60 +   modify it under the terms of the GNU General Public License as
61 +   published by the Free Software Foundation; either version 2, or (at
62 +   your option) any later version.
63 +
64 +   This program is distributed in the hope that it will be useful, but
65 +   WITHOUT ANY WARRANTY; without even the implied warranty of
66 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
67 +   General Public License for more details.
68 +   
69 +   ipkg command line frontend using libipkg
70 +   
71 +*/
72 +
73 +#include "libipkg/libipkg.h"
74 +
75 +int ipkg_main(int argc, char **argv)
76 +{
77 +       return ipkg_op(argc, argv);
78 +}
79 diff -ruN busybox-1.2.0-orig/archival/libipkg/args.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.c
80 --- busybox-1.2.0-orig/archival/libipkg/args.c  1970-01-01 01:00:00.000000000 +0100
81 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.c 2006-07-22 16:31:25.000000000 +0200
82 @@ -0,0 +1,242 @@
83 +/* args.c - parse command-line args
84
85 +  Carl D. Worth
86 +
87 +  Copyright 2001 University of Southern California
88
89 +  This program is free software; you can redistribute it and/or modify
90 +  it under the terms of the GNU General Public License as published by
91 +  the Free Software Foundation; either version 2, or (at your option)
92 +  any later version.
93
94 +  This program is distributed in the hope that it will be useful,
95 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
96 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
97 +  GNU General Public License for more details.
98 + */
99 +
100 +#include <getopt.h>
101 +#include <stdlib.h>
102 +#include <string.h>
103 +#include <unistd.h>
104 +
105 +#include "ipkg.h"
106 +#include "ipkg_message.h"
107 +
108 +#include "args.h"
109 +#include "sprintf_alloc.h"
110 +
111 +#include "libbb.h"
112 +
113 +
114 +static void print_version(void);
115 +
116 +enum long_args_opt
117 +{
118 +     ARGS_OPT_FORCE_DEFAULTS = 129,
119 +     ARGS_OPT_FORCE_DEPENDS,
120 +     ARGS_OPT_FORCE_OVERWRITE,
121 +     ARGS_OPT_FORCE_DOWNGRADE,
122 +     ARGS_OPT_FORCE_REINSTALL,
123 +     ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
124 +     ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
125 +     ARGS_OPT_FORCE_SPACE,
126 +     ARGS_OPT_NOACTION,
127 +     ARGS_OPT_NODEPS,
128 +     ARGS_OPT_VERBOSE_WGET,
129 +     ARGS_OPT_VERBOSITY,
130 +     ARGS_OPT_MULTIPLE_PROVIDERS
131 +};
132 +
133 +int args_init(args_t *args)
134 +{
135 +     char *conf_file_dir;
136 +
137 +     memset(args, 0, sizeof(args_t));
138 +
139 +     args->dest = ARGS_DEFAULT_DEST;
140 +
141 +     conf_file_dir = getenv("IPKG_CONF_DIR");
142 +     if (conf_file_dir == NULL || conf_file_dir[0] == '\0') {
143 +         conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
144 +     }
145 +     sprintf_alloc(&args->conf_file, "%s/%s", conf_file_dir,
146 +                  ARGS_DEFAULT_CONF_FILE_NAME);
147 +
148 +     args->force_defaults = ARGS_DEFAULT_FORCE_DEFAULTS;
149 +     args->force_depends = ARGS_DEFAULT_FORCE_DEPENDS;
150 +     args->force_overwrite = ARGS_DEFAULT_FORCE_OVERWRITE;
151 +     args->force_downgrade = ARGS_DEFAULT_FORCE_DOWNGRADE;
152 +     args->force_reinstall = ARGS_DEFAULT_FORCE_REINSTALL;
153 +     args->force_removal_of_dependent_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES;
154 +     args->force_removal_of_essential_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES;
155 +     args->noaction = ARGS_DEFAULT_NOACTION;
156 +     args->nodeps = ARGS_DEFAULT_NODEPS;
157 +     args->verbose_wget = ARGS_DEFAULT_VERBOSE_WGET;
158 +     args->verbosity = ARGS_DEFAULT_VERBOSITY;
159 +     args->offline_root = ARGS_DEFAULT_OFFLINE_ROOT;
160 +     args->offline_root_pre_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD;
161 +     args->offline_root_post_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD;
162 +     args->multiple_providers = 0;
163 +     args->nocheckfordirorfile = 0;
164 +     args->noreadfeedsfile = 0;
165 +
166 +     return 1;
167 +}
168 +
169 +void args_deinit(args_t *args)
170 +{
171 +     free(args->conf_file);
172 +     args->conf_file = NULL;
173 +}
174 +
175 +int args_parse(args_t *args, int argc, char *argv[])
176 +{
177 +     int c;
178 +     int option_index = 0;
179 +     int parse_err = 0;
180 +     static struct option long_options[] = {
181 +         {"query-all", 0, 0, 'A'},
182 +         {"conf-file", 1, 0, 'f'},
183 +         {"conf", 1, 0, 'f'},
184 +         {"dest", 1, 0, 'd'},
185 +         {"force-defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
186 +         {"force_defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
187 +         {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
188 +         {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
189 +         {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
190 +         {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
191 +         {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
192 +         {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
193 +         {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
194 +         {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
195 +         {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
196 +         {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
197 +         {"recursive", 0, 0,
198 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
199 +         {"force-removal-of-dependent-packages", 0, 0,
200 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
201 +         {"force_removal_of_dependent_packages", 0, 0,
202 +          ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
203 +         {"force-removal-of-essential-packages", 0, 0,
204 +          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
205 +         {"force_removal_of_essential_packages", 0, 0,
206 +          ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
207 +         {"multiple-providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
208 +         {"multiple_providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
209 +         {"noaction", 0, 0, ARGS_OPT_NOACTION},
210 +         {"nodeps", 0, 0, ARGS_OPT_NODEPS},
211 +         {"offline", 1, 0, 'o'},
212 +         {"offline-root", 1, 0, 'o'},
213 +         {"test", 0, 0, ARGS_OPT_NOACTION},
214 +         {"tmp-dir", 1, 0, 't'},
215 +         {"verbose-wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
216 +         {"verbose_wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
217 +         {"verbosity", 2, 0, 'V'},
218 +         {"version", 0, 0, 'v'},
219 +         {0, 0, 0, 0}
220 +     };
221 +
222 +     while (1) {
223 +         c = getopt_long_only(argc, argv, "Ad:f:no:t:vV:", long_options, &option_index);
224 +         if (c == -1)
225 +              break;
226 +
227 +         switch (c) {
228 +         case 'A':
229 +              args->query_all = 1;
230 +              break;
231 +         case 'd':
232 +              args->dest = optarg;
233 +              break;
234 +         case 'f':
235 +              free(args->conf_file);
236 +              args->conf_file = strdup(optarg);
237 +              break;
238 +         case 'o':
239 +              args->offline_root = optarg;
240 +              break;
241 +         case 'n':
242 +              args->noaction = 1;
243 +              break;
244 +         case 't':
245 +              args->tmp_dir = strdup(optarg);
246 +              break;
247 +         case 'v':
248 +              print_version();
249 +              exit(0);
250 +         case 'V':
251 +         case ARGS_OPT_VERBOSITY:
252 +              if (optarg)
253 +                   args->verbosity = atoi(optarg);
254 +              else
255 +                   args->verbosity += 1;
256 +              break;
257 +         case ARGS_OPT_FORCE_DEFAULTS:
258 +              args->force_defaults = 1;
259 +              break;
260 +         case ARGS_OPT_FORCE_DEPENDS:
261 +              args->force_depends = 1;
262 +              break;
263 +         case ARGS_OPT_FORCE_OVERWRITE:
264 +              args->force_overwrite = 1;
265 +              break;
266 +         case ARGS_OPT_FORCE_DOWNGRADE:
267 +              args->force_downgrade = 1;
268 +              break;
269 +         case ARGS_OPT_FORCE_REINSTALL:
270 +              args->force_reinstall = 1;
271 +              break;
272 +         case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
273 +              args->force_removal_of_essential_packages = 1;
274 +              break;
275 +         case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
276 +              args->force_removal_of_dependent_packages = 1;
277 +              break;
278 +         case ARGS_OPT_FORCE_SPACE:
279 +              args->force_space = 1;
280 +              break;
281 +         case ARGS_OPT_VERBOSE_WGET:
282 +              args->verbose_wget = 1;
283 +              break;
284 +         case ARGS_OPT_MULTIPLE_PROVIDERS:
285 +              args->multiple_providers = 1;
286 +              break;
287 +         case ARGS_OPT_NODEPS:
288 +              args->nodeps = 1;
289 +              break;
290 +         case ARGS_OPT_NOACTION:
291 +              args->noaction = 1;
292 +              break;
293 +         case ':':
294 +              parse_err++;
295 +              break;
296 +         case '?':
297 +              parse_err++;
298 +              break;
299 +         default:
300 +              bb_error_msg("Confusion: getopt_long returned %d\n", c);
301 +         }
302 +     }
303 +    
304 +     if (parse_err) {
305 +         return -parse_err;
306 +     } else {
307 +         return optind;
308 +     }
309 +}
310 +
311 +void args_usage(char *complaint)
312 +{
313 +     if (complaint) {
314 +          bb_error_msg("%s\n", complaint);
315 +     }
316 +     print_version();
317 +     bb_show_usage();
318 +     exit(1);
319 +}
320 +
321 +static void print_version(void)
322 +{
323 +       bb_error_msg("version %s\n", IPKG_VERSION);
324 +}
325 diff -ruN busybox-1.2.0-orig/archival/libipkg/args.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.h
326 --- busybox-1.2.0-orig/archival/libipkg/args.h  1970-01-01 01:00:00.000000000 +0100
327 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/args.h 2006-07-22 16:31:25.000000000 +0200
328 @@ -0,0 +1,72 @@
329 +/* args.h - parse command-line args
330 +
331 +  Carl D. Worth
332 +
333 +  Copyright 2001 University of Southern California
334
335 +  This program is free software; you can redistribute it and/or modify
336 +  it under the terms of the GNU General Public License as published by
337 +  the Free Software Foundation; either version 2, or (at your option)
338 +  any later version.
339
340 +  This program is distributed in the hope that it will be useful,
341 +  but WITHOUT ANY WARRANTY; without even the implied warranty of
342 +  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
343 +  GNU General Public License for more details.
344 +*/
345 +
346 +#ifndef ARGS_H
347 +#define ARGS_H
348 +
349 +struct args
350 +{
351 +    char *conf_file;
352 +    char *dest;
353 +    char *tmp_dir;
354 +    int force_defaults;
355 +    int force_depends;
356 +    int force_overwrite;
357 +    int force_downgrade;
358 +    int force_reinstall;
359 +    int force_removal_of_essential_packages;
360 +    int force_removal_of_dependent_packages;
361 +    int force_space;
362 +    int noaction;
363 +    int nodeps;
364 +    int multiple_providers;
365 +    int query_all;
366 +    int verbose_wget;
367 +    int verbosity;
368 +    int nocheckfordirorfile;
369 +    int noreadfeedsfile;
370 +    char *offline_root;
371 +    char *offline_root_pre_script_cmd;
372 +    char *offline_root_post_script_cmd;
373 +};
374 +typedef struct args args_t;
375 +
376 +#define ARGS_DEFAULT_CONF_FILE_DIR "/etc"
377 +#define ARGS_DEFAULT_CONF_FILE_NAME "ipkg.conf"
378 +#define ARGS_DEFAULT_DEST NULL
379 +#define ARGS_DEFAULT_FORCE_DEFAULTS 0
380 +#define ARGS_DEFAULT_FORCE_DEPENDS 0
381 +#define ARGS_DEFAULT_FORCE_OVERWRITE 0 
382 +#define ARGS_DEFAULT_FORCE_DOWNGRADE 0 
383 +#define ARGS_DEFAULT_FORCE_REINSTALL 0
384 +#define ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES 0
385 +#define ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES 0
386 +#define ARGS_DEFAULT_FORCE_SPACE 0
387 +#define ARGS_DEFAULT_OFFLINE_ROOT NULL
388 +#define ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD NULL
389 +#define ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD NULL
390 +#define ARGS_DEFAULT_NOACTION 0
391 +#define ARGS_DEFAULT_NODEPS 0
392 +#define ARGS_DEFAULT_VERBOSE_WGET 0
393 +#define ARGS_DEFAULT_VERBOSITY 1
394 +
395 +int args_init(args_t *args);
396 +void args_deinit(args_t *args);
397 +int args_parse(args_t *args, int argc, char *argv[]);
398 +void args_usage(char *complaint);
399 +
400 +#endif
401 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.c
402 --- busybox-1.2.0-orig/archival/libipkg/conffile.c      1970-01-01 01:00:00.000000000 +0100
403 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.c     2006-07-22 16:31:25.000000000 +0200
404 @@ -0,0 +1,64 @@
405 +/* conffile.c - the itsy package management system
406 +
407 +   Carl D. Worth
408 +
409 +   Copyright (C) 2001 University of Southern California
410 +
411 +   This program is free software; you can redistribute it and/or
412 +   modify it under the terms of the GNU General Public License as
413 +   published by the Free Software Foundation; either version 2, or (at
414 +   your option) any later version.
415 +
416 +   This program is distributed in the hope that it will be useful, but
417 +   WITHOUT ANY WARRANTY; without even the implied warranty of
418 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
419 +   General Public License for more details.
420 +*/
421 +
422 +#include <string.h>
423 +#include <stdlib.h>
424 +
425 +#include "ipkg.h"
426 +#include "ipkg_message.h"
427 +
428 +#include "conffile.h"
429 +#include "file_util.h"
430 +#include "sprintf_alloc.h"
431 +
432 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum)
433 +{
434 +    return nv_pair_init(conffile, file_name, md5sum);
435 +}
436 +
437 +void conffile_deinit(conffile_t *conffile)
438 +{
439 +    nv_pair_deinit(conffile);
440 +}
441 +
442 +int conffile_has_been_modified(ipkg_conf_t *conf, conffile_t *conffile)
443 +{
444 +    char *md5sum;
445 +    char *filename = conffile->name;
446 +    char *root_filename;
447 +    int ret;
448 +
449 +    if (conffile->value == NULL) {
450 +        ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s has no md5sum\n", __FUNCTION__, conffile->name);
451 +        return 1;
452 +    }
453 +
454 +    root_filename = root_filename_alloc(conf, filename);
455 +
456 +    md5sum = file_md5sum_alloc(root_filename);
457 +
458 +    ret = strcmp(md5sum, conffile->value);
459 +    if (ret) {
460 +      ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s: \t\nold md5=%s \t\nnew md5=%s\n", __FUNCTION__,
461 +              conffile->name, md5sum, conffile->value);
462 +    }
463 +
464 +    free(root_filename);
465 +    free(md5sum);
466 +
467 +    return ret;
468 +}
469 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.h
470 --- busybox-1.2.0-orig/archival/libipkg/conffile.h      1970-01-01 01:00:00.000000000 +0100
471 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile.h     2006-07-22 16:31:25.000000000 +0200
472 @@ -0,0 +1,30 @@
473 +/* conffile.h - the itsy package management system
474 +
475 +   Carl D. Worth
476 +
477 +   Copyright (C) 2001 University of Southern California
478 +
479 +   This program is free software; you can redistribute it and/or
480 +   modify it under the terms of the GNU General Public License as
481 +   published by the Free Software Foundation; either version 2, or (at
482 +   your option) any later version.
483 +
484 +   This program is distributed in the hope that it will be useful, but
485 +   WITHOUT ANY WARRANTY; without even the implied warranty of
486 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
487 +   General Public License for more details.
488 +*/
489 +
490 +#ifndef CONFFILE_H
491 +#define CONFFILE_H
492 +
493 +#include "nv_pair.h"
494 +
495 +typedef struct nv_pair conffile_t;
496 +
497 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum);
498 +void conffile_deinit(conffile_t *conffile);
499 +int conffile_has_been_modified(struct ipkg_conf *conf, conffile_t *conffile);
500 +
501 +#endif
502 +
503 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.c
504 --- busybox-1.2.0-orig/archival/libipkg/conffile_list.c 1970-01-01 01:00:00.000000000 +0100
505 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.c        2006-07-22 16:31:25.000000000 +0200
506 @@ -0,0 +1,47 @@
507 +/* conffile_list.c - the itsy package management system
508 +
509 +   Carl D. Worth
510 +
511 +   Copyright (C) 2001 University of Southern California
512 +
513 +   This program is free software; you can redistribute it and/or
514 +   modify it under the terms of the GNU General Public License as
515 +   published by the Free Software Foundation; either version 2, or (at
516 +   your option) any later version.
517 +
518 +   This program is distributed in the hope that it will be useful, but
519 +   WITHOUT ANY WARRANTY; without even the implied warranty of
520 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
521 +   General Public License for more details.
522 +*/
523 +
524 +#include "ipkg.h"
525 +
526 +#include "conffile_list.h"
527 +
528 +int conffile_list_init(conffile_list_t *list)
529 +{
530 +    return nv_pair_list_init(list);
531 +}
532 +
533 +void conffile_list_deinit(conffile_list_t *list)
534 +{
535 +    nv_pair_list_deinit(list);
536 +}
537 +
538 +conffile_t *conffile_list_append(conffile_list_t *list, const char *file_name,
539 +                        const char *md5sum)
540 +{
541 +    return nv_pair_list_append(list, file_name, md5sum);
542 +}
543 +
544 +int conffile_list_push(conffile_list_t *list, conffile_t *data)
545 +{
546 +    return nv_pair_list_push(list, data);
547 +}
548 +
549 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list)
550 +{
551 +    return nv_pair_list_pop(list);
552 +}
553 +
554 diff -ruN busybox-1.2.0-orig/archival/libipkg/conffile_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.h
555 --- busybox-1.2.0-orig/archival/libipkg/conffile_list.h 1970-01-01 01:00:00.000000000 +0100
556 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/conffile_list.h        2006-07-22 16:31:25.000000000 +0200
557 @@ -0,0 +1,36 @@
558 +/* conffile_list.h - the itsy package management system
559 +
560 +   Carl D. Worth
561 +
562 +   Copyright (C) 2001 University of Southern California
563 +
564 +   This program is free software; you can redistribute it and/or
565 +   modify it under the terms of the GNU General Public License as
566 +   published by the Free Software Foundation; either version 2, or (at
567 +   your option) any later version.
568 +
569 +   This program is distributed in the hope that it will be useful, but
570 +   WITHOUT ANY WARRANTY; without even the implied warranty of
571 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
572 +   General Public License for more details.
573 +*/
574 +
575 +#ifndef CONFFILE_LIST_H
576 +#define CONFFILE_LIST_H
577 +
578 +#include "conffile.h"
579 +#include "nv_pair_list.h"
580 +
581 +typedef struct nv_pair_list_elt conffile_list_elt_t;
582 +typedef struct nv_pair_list conffile_list_t;
583 +
584 +int conffile_list_init(conffile_list_t *list);
585 +void conffile_list_deinit(conffile_list_t *list);
586 +
587 +conffile_t *conffile_list_append(conffile_list_t *list, const char *name,
588 +                              const char *root_dir);
589 +int conffile_list_push(conffile_list_t *list, conffile_t *data);
590 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list);
591 +
592 +#endif
593 +
594 diff -ruN busybox-1.2.0-orig/archival/libipkg/file_util.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.c
595 --- busybox-1.2.0-orig/archival/libipkg/file_util.c     1970-01-01 01:00:00.000000000 +0100
596 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.c    2006-07-22 16:31:25.000000000 +0200
597 @@ -0,0 +1,177 @@
598 +/* file_util.c - convenience routines for common stat operations
599 +
600 +   Carl D. Worth
601 +
602 +   Copyright (C) 2001 University of Southern California
603 +
604 +   This program is free software; you can redistribute it and/or
605 +   modify it under the terms of the GNU General Public License as
606 +   published by the Free Software Foundation; either version 2, or (at
607 +   your option) any later version.
608 +
609 +   This program is distributed in the hope that it will be useful, but
610 +   WITHOUT ANY WARRANTY; without even the implied warranty of
611 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
612 +   General Public License for more details.
613 +*/
614 +
615 +#include "ipkg.h"
616 +#include <sys/types.h>
617 +#include <sys/stat.h>
618 +
619 +#include "sprintf_alloc.h"
620 +#include "file_util.h"
621 +#include "md5.h"
622 +#include "libbb.h"
623 +#undef strlen
624 +
625 +int file_exists(const char *file_name)
626 +{
627 +    int err;
628 +    struct stat stat_buf;
629 +
630 +    err = stat(file_name, &stat_buf);
631 +    if (err == 0) {
632 +       return 1;
633 +    } else {
634 +       return 0;
635 +    }
636 +}
637 +
638 +int file_is_dir(const char *file_name)
639 +{
640 +    int err;
641 +    struct stat stat_buf;
642 +
643 +    err = stat(file_name, &stat_buf);
644 +    if (err) {
645 +       return 0;
646 +    }
647 +
648 +    return S_ISDIR(stat_buf.st_mode);
649 +}
650 +
651 +/* read a single line from a file, stopping at a newline or EOF.
652 +   If a newline is read, it will appear in the resulting string.
653 +   Return value is a malloc'ed char * which should be freed at
654 +   some point by the caller.
655 +
656 +   Return value is NULL if the file is at EOF when called.
657 +*/
658 +#define FILE_READ_LINE_BUF_SIZE 1024
659 +char *file_read_line_alloc(FILE *file)
660 +{
661 +    char buf[FILE_READ_LINE_BUF_SIZE];
662 +    int buf_len;
663 +    char *line = NULL;
664 +    int line_size = 0;
665 +
666 +    memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
667 +    while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
668 +       buf_len = strlen(buf);
669 +       if (line) {
670 +           line_size += buf_len;
671 +           line = realloc(line, line_size);
672 +           if (line == NULL) {
673 +               fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
674 +               break;
675 +           }
676 +           strcat(line, buf);
677 +       } else {
678 +           line_size = buf_len + 1;
679 +           line = strdup(buf);
680 +       }
681 +       if (buf[buf_len - 1] == '\n') {
682 +           break;
683 +       }
684 +    }
685 +
686 +    return line;
687 +}
688 +
689 +int file_move(const char *src, const char *dest)
690 +{
691 +    int err;
692 +
693 +    err = rename(src, dest);
694 +
695 +    if (err && errno == EXDEV) {
696 +       err = file_copy(src, dest);
697 +       unlink(src);
698 +    } else if (err) {
699 +       fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
700 +               __FUNCTION__, src, dest, strerror(errno));
701 +    }
702 +
703 +    return err;
704 +}
705 +
706 +/* I put these here to keep libbb dependencies from creeping all over
707 +   the ipkg code */
708 +int file_copy(const char *src, const char *dest)
709 +{
710 +    int err;
711 +
712 +    err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
713 +    if (err) {
714 +       fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
715 +               __FUNCTION__, src, dest);
716 +    }
717 +
718 +    return err;
719 +}
720 +
721 +int file_mkdir_hier(const char *path, long mode)
722 +{
723 +    return bb_make_directory(path, mode, FILEUTILS_RECUR);
724 +}
725 +
726 +char *file_md5sum_alloc(const char *file_name)
727 +{
728 +    static const int md5sum_bin_len = 16;
729 +    static const int md5sum_hex_len = 32;
730 +
731 +    static const unsigned char bin2hex[16] = {
732 +       '0', '1', '2', '3',
733 +       '4', '5', '6', '7',
734 +       '8', '9', 'a', 'b',
735 +       'c', 'd', 'e', 'f'
736 +    };
737 +
738 +    int i, err;
739 +    FILE *file;
740 +    unsigned char *md5sum_hex;
741 +    unsigned char md5sum_bin[md5sum_bin_len];
742 +
743 +    md5sum_hex = malloc(md5sum_hex_len + 1);
744 +    if (md5sum_hex == NULL) {
745 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
746 +       return strdup("");
747 +    }
748 +
749 +    file = fopen(file_name, "r");
750 +    if (file == NULL) {
751 +       fprintf(stderr, "%s: Failed to open file %s: %s\n",
752 +               __FUNCTION__, file_name, strerror(errno));
753 +       return strdup("");
754 +    }
755 +
756 +    err = md5_stream(file, md5sum_bin);
757 +    if (err) {
758 +       fprintf(stderr, "%s: ERROR computing md5sum for %s: %s\n",
759 +               __FUNCTION__, file_name, strerror(err));
760 +       return strdup("");
761 +    }
762 +
763 +    fclose(file);
764 +
765 +    for (i=0; i < md5sum_bin_len; i++) {
766 +       md5sum_hex[i*2] = bin2hex[md5sum_bin[i] >> 4];
767 +       md5sum_hex[i*2+1] = bin2hex[md5sum_bin[i] & 0xf];
768 +    }
769 +    
770 +    md5sum_hex[md5sum_hex_len] = '\0';
771 +    
772 +    return md5sum_hex;
773 +}
774 +
775 diff -ruN busybox-1.2.0-orig/archival/libipkg/file_util.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.h
776 --- busybox-1.2.0-orig/archival/libipkg/file_util.h     1970-01-01 01:00:00.000000000 +0100
777 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/file_util.h    2006-07-22 16:31:25.000000000 +0200
778 @@ -0,0 +1,29 @@
779 +/* file_util.h - convenience routines for common file operations
780 +
781 +   Carl D. Worth
782 +
783 +   Copyright (C) 2001 University of Southern California
784 +
785 +   This program is free software; you can redistribute it and/or
786 +   modify it under the terms of the GNU General Public License as
787 +   published by the Free Software Foundation; either version 2, or (at
788 +   your option) any later version.
789 +
790 +   This program is distributed in the hope that it will be useful, but
791 +   WITHOUT ANY WARRANTY; without even the implied warranty of
792 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
793 +   General Public License for more details.
794 +*/
795 +
796 +#ifndef FILE_UTIL_H
797 +#define FILE_UTIL_H
798 +
799 +int file_exists(const char *file_name);
800 +int file_is_dir(const char *file_name);
801 +char *file_read_line_alloc(FILE *file);
802 +int file_move(const char *src, const char *dest);
803 +int file_copy(const char *src, const char *dest);
804 +int file_mkdir_hier(const char *path, long mode);
805 +char *file_md5sum_alloc(const char *file_name);
806 +
807 +#endif
808 diff -ruN busybox-1.2.0-orig/archival/libipkg/hash_table.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.c
809 --- busybox-1.2.0-orig/archival/libipkg/hash_table.c    1970-01-01 01:00:00.000000000 +0100
810 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.c   2006-07-22 16:31:25.000000000 +0200
811 @@ -0,0 +1,155 @@
812 +/* hash.c - hash tables for ipkg
813 +
814 +   Steven M. Ayer, Jamey Hicks
815 +   
816 +   Copyright (C) 2002 Compaq Computer Corporation
817 +
818 +   This program is free software; you can redistribute it and/or
819 +   modify it under the terms of the GNU General Public License as
820 +   published by the Free Software Foundation; either version 2, or (at
821 +   your option) any later version.
822 +
823 +   This program is distributed in the hope that it will be useful, but
824 +   WITHOUT ANY WARRANTY; without even the implied warranty of
825 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
826 +   General Public License for more details.
827 +*/
828 +
829 +#include <errno.h>
830 +#include <stdio.h>
831 +#include <stdlib.h>
832 +#include <string.h>
833 +#include "hash_table.h"
834 +#include "ipkg_message.h"
835 +
836 +
837 +static int hash_index(hash_table_t *hash, const char *pkg_name);
838 +static int rotating(const char *key, int len, int prime);
839 +
840 +static int hash_index(hash_table_t *hash, const char *pkg_name)
841 +{
842 +    return rotating(pkg_name, strlen(pkg_name), hash->n_entries);
843 +}
844 +  
845 +static int rotating(const char *key, int len, int prime)
846 +{
847 +    unsigned int hash, i;
848 +    for (hash=len, i=0; i<len; ++i)
849 +       hash = (hash<<4)^(hash>>28)^key[i];
850 +    return (hash % prime);
851 +}
852 +
853 +
854 +/*
855 + * this is an open table keyed by strings
856 + */
857 +int hash_table_init(const char *name, hash_table_t *hash, int len)
858 +{
859 +    static int primes_table[] = {
860 +       379, 761, 983, 1423, 2711, 3361, 3931, 4679, 5519, 6701, 9587,
861 +       19471, 23143, 33961, 46499, 49727, 99529, 0
862 +    };
863 +    int *picker;
864 +
865 +    if (hash->entries != NULL) {
866 +       /* we have been here already */
867 +       return 0;
868 +    }
869 +
870 +    hash->name = name;
871 +    hash->entries = NULL;
872 +    hash->n_entries = 0;
873 +    hash->hash_entry_key = NULL;
874 +
875 +    picker = primes_table;
876 +    while(*picker && (*picker++ < len));
877 +    if(!*picker)
878 +       fprintf(stderr, "%s: primes table might not be big enough (! << %d)\n", __FUNCTION__, len);
879 +    --picker;
880 +
881 +    hash->n_entries = *picker;
882 +    hash->entries = (hash_entry_t *)calloc(hash->n_entries, sizeof(hash_entry_t));
883 +    if (hash->entries == NULL) {
884 +       fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__);
885 +       return ENOMEM;
886 +    }
887 +    return 0;
888 +}
889 +
890 +void hash_table_deinit(hash_table_t *hash)
891 +{
892 +    free(hash->entries);
893 +    hash->entries = NULL;
894 +    hash->n_entries = 0;
895 +}
896 +
897 +void *hash_table_get(hash_table_t *hash, const char *key)
898 +{
899 +  int ndx= hash_index(hash, key);
900 +  hash_entry_t *hash_entry = hash->entries + ndx;
901 +  while (hash_entry) 
902 +  {
903 +    if (hash_entry->key) 
904 +    {
905 +      if (strcmp(key, hash_entry->key) == 0) {
906 +         // ipkg_message(NULL, IPKG_DEBUG, "Function: %s. Key found for '%s' \n", __FUNCTION__, key);
907 +        return hash_entry->data;
908 +      }
909 +    }
910 +    hash_entry = hash_entry->next;
911 +  }
912 +  return NULL;
913 +}
914 +
915 +int hash_table_insert(hash_table_t *hash, const char *key, void *value)
916 +{
917 +     int ndx= hash_index(hash, key);
918 +     hash_entry_t *hash_entry = hash->entries + ndx;
919 +     if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Inserting in hash for '%s' \n", __FUNCTION__, key);
920 +     if (hash_entry->key) {
921 +         if (strcmp(hash_entry->key, key) == 0) {
922 +              /* alread in table, update the value */
923 +               if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash for '%s' \n", __FUNCTION__, key);
924 +              hash_entry->data = value;
925 +              return 0;
926 +         } else {
927 +              /* 
928 +               * if this is a collision, we have to go to the end of the ll,
929 +               * then add a new entry
930 +               * before we can hook up the value
931 +               */
932 +               if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash by collision for '%s' \n", __FUNCTION__, key);
933 +              while (hash_entry->next)
934 +                   hash_entry = hash_entry->next;
935 +              hash_entry->next = (hash_entry_t *)malloc(sizeof(hash_entry_t));
936 +              if (!hash_entry->next) {
937 +                   return -ENOMEM;
938 +              }
939 +              hash_entry = hash_entry->next;
940 +              hash_entry->next = NULL;
941 +         }
942 +     }
943 +     hash->n_elements++;
944 +     hash_entry->key = strdup(key);
945 +     hash_entry->data = value;
946 +
947 +     return 0;
948 +}
949 +
950 +
951 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data)
952 +{ 
953 +    int i;
954 +    if (!hash || !f)
955 +       return;
956 +
957 +    for (i = 0; i < hash->n_entries; i++) {
958 +       hash_entry_t *hash_entry = (hash->entries + i);
959 +       do {
960 +           if(hash_entry->key) {
961 +               f(hash_entry->key, hash_entry->data, data);
962 +           }
963 +       } while((hash_entry = hash_entry->next));
964 +    }
965 +}
966 +
967 diff -ruN busybox-1.2.0-orig/archival/libipkg/hash_table.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.h
968 --- busybox-1.2.0-orig/archival/libipkg/hash_table.h    1970-01-01 01:00:00.000000000 +0100
969 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/hash_table.h   2006-07-22 16:31:25.000000000 +0200
970 @@ -0,0 +1,44 @@
971 +/* hash.h - hash tables for ipkg
972 +
973 +   Steven M. Ayer, Jamey Hicks
974 +   
975 +   Copyright (C) 2002 Compaq Computer Corporation
976 +
977 +   This program is free software; you can redistribute it and/or
978 +   modify it under the terms of the GNU General Public License as
979 +   published by the Free Software Foundation; either version 2, or (at
980 +   your option) any later version.
981 +
982 +   This program is distributed in the hope that it will be useful, but
983 +   WITHOUT ANY WARRANTY; without even the implied warranty of
984 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
985 +   General Public License for more details.
986 +*/
987 +
988 +#ifndef _HASH_TABLE_H_
989 +#define _HASH_TABLE_H_
990 +
991 +typedef struct hash_entry hash_entry_t;
992 +typedef struct hash_table hash_table_t;
993 +
994 +struct hash_entry {
995 +  const char * key;
996 +  void * data;
997 +  struct hash_entry * next;
998 +};
999 +
1000 +struct hash_table {
1001 +  const char *name; 
1002 +  hash_entry_t * entries;  
1003 +  int n_entries; /* number of buckets */
1004 +  int n_elements;
1005 +  const char * (*hash_entry_key)(void * data);
1006 +};
1007 +
1008 +int hash_table_init(const char *name, hash_table_t *hash, int len);
1009 +void hash_table_deinit(hash_table_t *hash);
1010 +void *hash_table_get(hash_table_t *hash, const char *key);
1011 +int hash_table_insert(hash_table_t *hash, const char *key, void *value);
1012 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data);
1013 +
1014 +#endif /* _HASH_TABLE_H_ */
1015 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.c
1016 --- busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.c      1970-01-01 01:00:00.000000000 +0100
1017 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.c     2006-07-22 16:31:25.000000000 +0200
1018 @@ -0,0 +1,1431 @@
1019 +/* ipkg_cmd.c - the itsy package management system
1020 +
1021 +   Carl D. Worth
1022 +
1023 +   Copyright (C) 2001 University of Southern California
1024 +
1025 +   This program is free software; you can redistribute it and/or
1026 +   modify it under the terms of the GNU General Public License as
1027 +   published by the Free Software Foundation; either version 2, or (at
1028 +   your option) any later version.
1029 +
1030 +   This program is distributed in the hope that it will be useful, but
1031 +   WITHOUT ANY WARRANTY; without even the implied warranty of
1032 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1033 +   General Public License for more details.
1034 +*/
1035 +
1036 +#include <string.h>
1037 +
1038 +#include "ipkg.h"
1039 +#include <libgen.h>
1040 +#include <glob.h>
1041 +#include <errno.h>
1042 +#include <stdlib.h>
1043 +#include <unistd.h>
1044 +#include <signal.h>
1045 +#include <stdio.h>
1046 +#include <dirent.h>
1047 +
1048 +#include "ipkg_conf.h"
1049 +#include "ipkg_cmd.h"
1050 +#include "ipkg_message.h"
1051 +#include "pkg.h"
1052 +#include "pkg_dest.h"
1053 +#include "pkg_parse.h"
1054 +#include "sprintf_alloc.h"
1055 +#include "pkg.h"
1056 +#include "file_util.h"
1057 +#include "str_util.h"
1058 +#include "libbb.h"
1059 +#include "unarchive.h"
1060 +
1061 +#include <fnmatch.h>
1062 +
1063 +
1064 +#include "ipkg_download.h"
1065 +#include "ipkg_install.h"
1066 +#include "ipkg_upgrade.h"
1067 +#include "ipkg_remove.h"
1068 +#include "ipkg_configure.h"
1069 +#include "ipkg_message.h"
1070 +
1071 +#ifdef IPKG_LIB
1072 +#include "libipkg.h"
1073 +static void *p_userdata = NULL;
1074 +#endif
1075 +
1076 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv);
1077 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv);
1078 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv);
1079 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv);
1080 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv);
1081 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv);
1082 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv);
1083 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv);
1084 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv);
1085 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv);
1086 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv);
1087 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv);
1088 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv);
1089 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv);
1090 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1091 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1092 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv);
1093 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv);
1094 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1095 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv);
1096 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv);
1097 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv);
1098 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv);
1099 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv);
1100 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv);
1101 +
1102 +/* XXX: CLEANUP: The usage strings should be incorporated into this
1103 +   array for easier maintenance */
1104 +static ipkg_cmd_t cmds[] = {
1105 +     {"update", 0, (ipkg_cmd_fun_t)ipkg_update_cmd}, 
1106 +     {"upgrade", 0, (ipkg_cmd_fun_t)ipkg_upgrade_cmd},
1107 +     {"list", 0, (ipkg_cmd_fun_t)ipkg_list_cmd},
1108 +     {"list_installed", 0, (ipkg_cmd_fun_t)ipkg_list_installed_cmd},
1109 +     {"info", 0, (ipkg_cmd_fun_t)ipkg_info_cmd},
1110 +     {"flag", 1, (ipkg_cmd_fun_t)ipkg_flag_cmd},
1111 +     {"status", 0, (ipkg_cmd_fun_t)ipkg_status_cmd},
1112 +     {"install_pending", 0, (ipkg_cmd_fun_t)ipkg_install_pending_cmd},
1113 +     {"install", 1, (ipkg_cmd_fun_t)ipkg_install_cmd},
1114 +     {"remove", 1, (ipkg_cmd_fun_t)ipkg_remove_cmd},
1115 +     {"purge", 1, (ipkg_cmd_fun_t)ipkg_purge_cmd},
1116 +     {"configure", 0, (ipkg_cmd_fun_t)ipkg_configure_cmd},
1117 +     {"files", 1, (ipkg_cmd_fun_t)ipkg_files_cmd},
1118 +     {"search", 1, (ipkg_cmd_fun_t)ipkg_search_cmd},
1119 +     {"download", 1, (ipkg_cmd_fun_t)ipkg_download_cmd},
1120 +     {"compare_versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1121 +     {"compare-versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1122 +     {"print-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1123 +     {"print_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1124 +     {"print-installation-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1125 +     {"print_installation_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1126 +     {"depends", 1, (ipkg_cmd_fun_t)ipkg_depends_cmd},
1127 +     {"whatdepends", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_cmd},
1128 +     {"whatdependsrec", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_recursively_cmd},
1129 +     {"whatrecommends", 1, (ipkg_cmd_fun_t)ipkg_whatrecommends_cmd},
1130 +     {"whatsuggests", 1, (ipkg_cmd_fun_t)ipkg_whatsuggests_cmd},
1131 +     {"whatprovides", 1, (ipkg_cmd_fun_t)ipkg_whatprovides_cmd},
1132 +     {"whatreplaces", 1, (ipkg_cmd_fun_t)ipkg_whatreplaces_cmd},
1133 +     {"whatconflicts", 1, (ipkg_cmd_fun_t)ipkg_whatconflicts_cmd},
1134 +};
1135 +
1136 +int ipkg_state_changed;
1137 +static void write_status_files_if_changed(ipkg_conf_t *conf)
1138 +{
1139 +     if (ipkg_state_changed && !conf->noaction) {
1140 +         ipkg_message(conf, IPKG_INFO,
1141 +                      "  writing status file\n");
1142 +         ipkg_conf_write_status_files(conf);
1143 +         pkg_write_changed_filelists(conf);
1144 +     } else { 
1145 +         ipkg_message(conf, IPKG_NOTICE, "Nothing to be done\n");
1146 +     }
1147 +}
1148 +
1149 +
1150 +static int num_cmds = sizeof(cmds) / sizeof(ipkg_cmd_t);
1151 +
1152 +ipkg_cmd_t *ipkg_cmd_find(const char *name)
1153 +{
1154 +     int i;
1155 +     ipkg_cmd_t *cmd;
1156 +
1157 +     for (i=0; i < num_cmds; i++) {
1158 +         cmd = &cmds[i];
1159 +         if (strcmp(name, cmd->name) == 0) {
1160 +              return cmd;
1161 +         }
1162 +     }
1163 +
1164 +     return NULL;
1165 +}
1166 +
1167 +#ifdef IPKG_LIB
1168 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv, void *userdata)
1169 +{
1170 +       int result;
1171 +       p_userdata = userdata;
1172 +      
1173 +
1174 +       result = (cmd->fun)(conf, argc, argv);
1175 +        if ( result == 0 ) {
1176 +           ipkg_message(conf, IPKG_NOTICE, "Done.\n");
1177 +        } else {
1178 +           ipkg_message(conf, IPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
1179 +
1180 +        }
1181 +        if ( error_list ) {
1182 +           reverse_error_list(&error_list);
1183 +
1184 +           ipkg_message(conf, IPKG_NOTICE, "Collected errors:\n");
1185 +           /* Here we print the errors collected and free the list */
1186 +           while (error_list != NULL) {
1187 +                 ipkg_message(conf, IPKG_NOTICE, "%s",error_list->errmsg);
1188 +                 error_list = error_list->next;
1189 +
1190 +           }
1191 +           free_error_list(&error_list);
1192 +
1193 +        }
1194 +   
1195 +       p_userdata = NULL;
1196 +       return result;
1197 +}
1198 +#else
1199 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv)
1200 +{
1201 +     return (cmd->fun)(conf, argc, argv);
1202 +}
1203 +#endif
1204 +
1205 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv)
1206 +{
1207 +     int err;
1208 +     int failures;
1209 +     char *lists_dir;
1210 +     pkg_src_list_elt_t *iter;
1211 +     pkg_src_t *src;
1212 +
1213
1214 +    sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
1215
1216 +    if (! file_is_dir(lists_dir)) {
1217 +         if (file_exists(lists_dir)) {
1218 +              ipkg_message(conf, IPKG_ERROR,
1219 +                           "%s: ERROR: %s exists, but is not a directory\n",
1220 +                           __FUNCTION__, lists_dir);
1221 +              free(lists_dir);
1222 +              return EINVAL;
1223 +         }
1224 +         err = file_mkdir_hier(lists_dir, 0755);
1225 +         if (err) {
1226 +              ipkg_message(conf, IPKG_ERROR,
1227 +                           "%s: ERROR: failed to make directory %s: %s\n",
1228 +                           __FUNCTION__, lists_dir, strerror(errno));
1229 +              free(lists_dir);
1230 +              return EINVAL;
1231 +         }     
1232 +     } 
1233 +
1234 +     failures = 0;
1235 +     for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
1236 +         char *url, *list_file_name;
1237 +
1238 +         src = iter->data;
1239 +
1240 +         if (src->extra_data)  /* debian style? */
1241 +             sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data, 
1242 +                           src->gzip ? "Packages.gz" : "Packages");
1243 +         else
1244 +             sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
1245 +
1246 +         sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
1247 +         if (src->gzip) {
1248 +             char *tmp;
1249 +             char *tmp_file_name;
1250 +             FILE *in, *out;
1251 +
1252 +             tmp = strdup ("/tmp/ipkg.XXXXXX");
1253 +
1254 +             if (mkdtemp (tmp) == NULL) {
1255 +                 perror ("mkdtemp");
1256 +                 failures++;
1257 +                 continue;
1258 +             }
1259 +             
1260 +             sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
1261 +             err = ipkg_download(conf, url, tmp_file_name);
1262 +             if (err == 0) {
1263 +                  ipkg_message (conf, IPKG_NOTICE, "Inflating %s\n", url);
1264 +                  in = fopen (tmp_file_name, "r");
1265 +                  out = fopen (list_file_name, "w");
1266 +                  if (in && out)
1267 +                       inflate_unzip (in, out);
1268 +                  else
1269 +                       err = 1;
1270 +                  if (in)
1271 +                       fclose (in);
1272 +                  if (out)
1273 +                       fclose (out);
1274 +                  unlink (tmp_file_name);
1275 +                  rmdir (tmp);
1276 +                  free (tmp);
1277 +             }
1278 +         } else
1279 +             err = ipkg_download(conf, url, list_file_name);
1280 +         if (err) {
1281 +              failures++;
1282 +         } else {
1283 +              ipkg_message(conf, IPKG_NOTICE,
1284 +                           "Updated list of available packages in %s\n",
1285 +                           list_file_name);
1286 +         }
1287 +         free(url);
1288 +         free(list_file_name);
1289 +     }
1290 +     free(lists_dir);
1291 +
1292 +#ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
1293 +#warning here
1294 +     /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
1295 +      * this is a hack to work around poor bookkeeping in old ipkg upgrade code 
1296 +      * -Jamey 3/1/03
1297 +      */
1298 +     {
1299 +         int i;
1300 +         int changed = 0;
1301 +         pkg_vec_t *available = pkg_vec_alloc();
1302 +         pkg_hash_fetch_available(&conf->pkg_hash, available);
1303 +         ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
1304 +         for (i = 0; i < available->len; i++) {
1305 +              pkg_t *pkg = available->pkgs[i];
1306 +              if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
1307 +                   ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
1308 +                   pkg->state_want = SW_UNKNOWN;
1309 +                   changed = 1;
1310 +              }
1311 +         }
1312 +         pkg_vec_free(available);
1313 +         if (changed) {
1314 +              write_status_files_if_changed(conf);
1315 +         }
1316 +     }
1317 +#endif
1318 +
1319 +     return failures;
1320 +}
1321 +
1322 +
1323 +/* scan the args passed and cache the local filenames of the packages */
1324 +int ipkg_multiple_files_scan(ipkg_conf_t *conf, int argc, char **argv)
1325 +{
1326 +     int i;
1327 +     int err;
1328 +    
1329 +     /* 
1330 +      * First scan through package names/urls
1331 +      * For any urls, download the packages and install in database.
1332 +      * For any files, install package info in database.
1333 +      */
1334 +     for (i = 0; i < argc; i ++) {
1335 +         char *filename = argv [i];
1336 +         //char *tmp = basename (tmp);
1337 +         //int tmplen = strlen (tmp);
1338 +
1339 +         //if (strcmp (tmp + (tmplen - strlen (IPKG_PKG_EXTENSION)), IPKG_PKG_EXTENSION) != 0)
1340 +         //     continue;
1341 +         //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
1342 +         //     continue;
1343 +       
1344 +          ipkg_message(conf, IPKG_DEBUG2, "Debug mfs: %s  \n",filename );
1345 +
1346 +         err = ipkg_prepare_url_for_install(conf, filename, &argv[i]);
1347 +         if (err)
1348 +           return err;
1349 +     }
1350 +     return 0;
1351 +}
1352 +
1353 +struct ipkg_intercept
1354 +{
1355 +    char *oldpath;
1356 +    char *statedir;
1357 +};
1358 +
1359 +typedef struct ipkg_intercept *ipkg_intercept_t;
1360 +
1361 +ipkg_intercept_t ipkg_prep_intercepts(ipkg_conf_t *conf)
1362 +{
1363 +    ipkg_intercept_t ctx;
1364 +    char *newpath;
1365 +    int gen;
1366 +
1367 +    ctx = malloc (sizeof (*ctx));
1368 +    ctx->oldpath = strdup (getenv ("PATH"));
1369 +
1370 +    sprintf_alloc (&newpath, "%s/ipkg/intercept:%s", IPKGLIBDIR, ctx->oldpath);
1371 +    setenv ("PATH", newpath, 1);
1372 +    free (newpath);
1373 +    
1374 +    gen = 0;
1375 + retry:
1376 +    sprintf_alloc (&ctx->statedir, "/tmp/ipkg-intercept-%d-%d", getpid (), gen);
1377 +    if (mkdir (ctx->statedir, 0770) < 0) {
1378 +       if (errno == EEXIST) {
1379 +           free (ctx->statedir);
1380 +           gen++;
1381 +           goto retry;
1382 +       }
1383 +       perror (ctx->statedir);
1384 +       return NULL;
1385 +    }
1386 +    setenv ("IPKG_INTERCEPT_DIR", ctx->statedir, 1);
1387 +    return ctx;
1388 +}
1389 +
1390 +int ipkg_finalize_intercepts(ipkg_intercept_t ctx)
1391 +{
1392 +    char *cmd;
1393 +    DIR *dir;
1394 +    int err = 0;
1395 +
1396 +    setenv ("PATH", ctx->oldpath, 1);
1397 +    free (ctx->oldpath);
1398 +
1399 +    dir = opendir (ctx->statedir);
1400 +    if (dir) {
1401 +       struct dirent *de;
1402 +       while (de = readdir (dir), de != NULL) {
1403 +           char *path;
1404 +           
1405 +           if (de->d_name[0] == '.')
1406 +               continue;
1407 +           
1408 +           sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
1409 +           if (access (path, X_OK) == 0) {
1410 +               if (system (path)) {
1411 +                   err = errno;
1412 +                   perror (de->d_name);
1413 +               }
1414 +           }
1415 +           free (path);
1416 +       }
1417 +    } else
1418 +       perror (ctx->statedir);
1419 +       
1420 +    sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
1421 +    system (cmd);
1422 +    free (cmd);
1423 +
1424 +    free (ctx->statedir);
1425 +    free (ctx);
1426 +
1427 +    return err;
1428 +}
1429 +
1430 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name)
1431 +{
1432 +     pkg_vec_t *all;
1433 +     int i;
1434 +     pkg_t *pkg;
1435 +     ipkg_intercept_t ic;
1436 +     int r, err = 0;
1437 +
1438 +     ipkg_message(conf, IPKG_INFO,
1439 +                 "Configuring unpacked packages\n");
1440 +     fflush( stdout );
1441 +
1442 +     all = pkg_vec_alloc();
1443 +     pkg_hash_fetch_available(&conf->pkg_hash, all);
1444 +
1445 +     ic = ipkg_prep_intercepts (conf);
1446 +    
1447 +     for(i = 0; i < all->len; i++) {
1448 +         pkg = all->pkgs[i];
1449 +
1450 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1451 +              continue;
1452 +
1453 +         if (pkg->state_status == SS_UNPACKED) {
1454 +              ipkg_message(conf, IPKG_NOTICE,
1455 +                           "Configuring %s\n", pkg->name);
1456 +              fflush( stdout );
1457 +              r = ipkg_configure(conf, pkg);
1458 +              if (r == 0) {
1459 +                   pkg->state_status = SS_INSTALLED;
1460 +                   pkg->parent->state_status = SS_INSTALLED;
1461 +                   pkg->state_flag &= ~SF_PREFER;
1462 +              } else {
1463 +                   if (!err)
1464 +                       err = r;
1465 +              }
1466 +         }
1467 +     }
1468 +
1469 +     r = ipkg_finalize_intercepts (ic);
1470 +     if (r && !err)
1471 +        err = r;
1472 +
1473 +     pkg_vec_free(all);
1474 +     return err;
1475 +}
1476 +
1477 +static void sigint_handler(int sig)
1478 +{
1479 +     signal(sig, SIG_DFL);
1480 +     ipkg_message(NULL, IPKG_NOTICE,
1481 +                 "ipkg: interrupted. writing out status database\n");
1482 +     write_status_files_if_changed(global_conf);
1483 +     exit(128 + sig);
1484 +}
1485 +
1486 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv)
1487 +{
1488 +     int i;
1489 +     char *arg;
1490 +     int err=0;
1491 +
1492 +     global_conf = conf;
1493 +     signal(SIGINT, sigint_handler);
1494 +
1495 +     /*
1496 +      * Now scan through package names and install
1497 +      */
1498 +     for (i=0; i < argc; i++) {
1499 +         arg = argv[i];
1500 +
1501 +          ipkg_message(conf, IPKG_DEBUG2, "Debug install_cmd: %s  \n",arg );
1502 +          err = ipkg_prepare_url_for_install(conf, arg, &argv[i]);
1503 +          if (err != EINVAL && err != 0)
1504 +              return err;
1505 +     }
1506 +     pkg_info_preinstall_check(conf);
1507 +
1508 +     for (i=0; i < argc; i++) {
1509 +         arg = argv[i];
1510 +         if (conf->multiple_providers)
1511 +              err = ipkg_install_multi_by_name(conf, arg);
1512 +         else{
1513 +              err = ipkg_install_by_name(conf, arg);
1514 +          }
1515 +         if (err == IPKG_PKG_HAS_NO_CANDIDATE) {
1516 +              ipkg_message(conf, IPKG_ERROR,
1517 +                           "Cannot find package %s.\n"
1518 +                           "Check the spelling or perhaps run 'ipkg update'\n",
1519 +                           arg);
1520 +         }
1521 +     }
1522 +
1523 +     /* recheck to verify that all dependences are satisfied */
1524 +     if (0) ipkg_satisfy_all_dependences(conf);
1525 +
1526 +     ipkg_configure_packages(conf, NULL);
1527 +
1528 +     write_status_files_if_changed(conf);
1529 +
1530 +     return err;
1531 +}
1532 +
1533 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv)
1534 +{
1535 +     int i;
1536 +     pkg_t *pkg;
1537 +     int err;
1538 +
1539 +     global_conf = conf;
1540 +     signal(SIGINT, sigint_handler);
1541 +
1542 +     if (argc) {
1543 +         for (i=0; i < argc; i++) {
1544 +              char *arg = argv[i];
1545 +
1546 +               err = ipkg_prepare_url_for_install(conf, arg, &arg);
1547 +               if (err != EINVAL && err != 0)
1548 +                   return err;
1549 +         }
1550 +         pkg_info_preinstall_check(conf);
1551 +
1552 +         for (i=0; i < argc; i++) {
1553 +              char *arg = argv[i];
1554 +              if (conf->restrict_to_default_dest) {
1555 +                   pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1556 +                                                               argv[i],
1557 +                                                               conf->default_dest);
1558 +                   if (pkg == NULL) {
1559 +                        ipkg_message(conf, IPKG_NOTICE,
1560 +                                     "Package %s not installed in %s\n",
1561 +                                     argv[i], conf->default_dest->name);
1562 +                        continue;
1563 +                   }
1564 +              } else {
1565 +                   pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1566 +                                                          argv[i]);
1567 +              }
1568 +              if (pkg)
1569 +                   ipkg_upgrade_pkg(conf, pkg);
1570 +              else {
1571 +                   ipkg_install_by_name(conf, arg);
1572 +               }
1573 +         }
1574 +     } else {
1575 +         pkg_vec_t *installed = pkg_vec_alloc();
1576 +
1577 +         pkg_info_preinstall_check(conf);
1578 +
1579 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1580 +         for (i = 0; i < installed->len; i++) {
1581 +              pkg = installed->pkgs[i];
1582 +              ipkg_upgrade_pkg(conf, pkg);
1583 +         }
1584 +         pkg_vec_free(installed);
1585 +     }
1586 +
1587 +     /* recheck to verify that all dependences are satisfied */
1588 +     if (0) ipkg_satisfy_all_dependences(conf);
1589 +
1590 +     ipkg_configure_packages(conf, NULL);
1591 +
1592 +     write_status_files_if_changed(conf);
1593 +
1594 +     return 0;
1595 +}
1596 +
1597 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv)
1598 +{
1599 +     int i, err;
1600 +     char *arg;
1601 +     pkg_t *pkg;
1602 +
1603 +     pkg_info_preinstall_check(conf);
1604 +     for (i = 0; i < argc; i++) {
1605 +         arg = argv[i];
1606 +
1607 +         pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
1608 +         if (pkg == NULL) {
1609 +              ipkg_message(conf, IPKG_ERROR,
1610 +                           "Cannot find package %s.\n"
1611 +                           "Check the spelling or perhaps run 'ipkg update'\n",
1612 +                           arg);
1613 +              continue;
1614 +         }
1615 +
1616 +         err = ipkg_download_pkg(conf, pkg, ".");
1617 +
1618 +         if (err) {
1619 +              ipkg_message(conf, IPKG_ERROR,
1620 +                           "Failed to download %s\n", pkg->name);
1621 +         } else {
1622 +              ipkg_message(conf, IPKG_NOTICE,
1623 +                           "Downloaded %s as %s\n",
1624 +                           pkg->name, pkg->local_filename);
1625 +         }
1626 +     }
1627 +
1628 +     return 0;
1629 +}
1630 +
1631 +
1632 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv)
1633 +{
1634 +     int i ;
1635 +     pkg_vec_t *available;
1636 +     pkg_t *pkg;
1637 +     char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1638 +     char *newline;
1639 +     char *pkg_name = NULL;
1640 +     char *version_str;
1641 +
1642 +     if (argc > 0) {
1643 +         pkg_name = argv[0];
1644 +     }
1645 +     available = pkg_vec_alloc();
1646 +     pkg_hash_fetch_available(&conf->pkg_hash, available);
1647 +     for (i=0; i < available->len; i++) {
1648 +         pkg = available->pkgs[i];
1649 +         /* if we have package name or pattern and pkg does not match, then skip it */
1650 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1651 +              continue;
1652 +         if (pkg->description) {
1653 +              strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1654 +         } else {
1655 +              desc_short[0] = '\0';
1656 +         }
1657 +         desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1658 +         newline = strchr(desc_short, '\n');
1659 +         if (newline) {
1660 +              *newline = '\0';
1661 +         }
1662 +#ifndef IPKG_LIB
1663 +         printf("%s - %s\n", pkg->name, desc_short);
1664 +#else
1665 +         if (ipkg_cb_list) {
1666 +               version_str = pkg_version_str_alloc(pkg);
1667 +               ipkg_cb_list(pkg->name,desc_short,
1668 +                                            version_str,
1669 +                                        pkg->state_status,
1670 +                                        p_userdata);
1671 +               free(version_str);
1672 +         }
1673 +#endif
1674 +     }
1675 +     pkg_vec_free(available);
1676 +
1677 +     return 0;
1678 +}
1679 +
1680 +
1681 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv)
1682 +{
1683 +     int i ;
1684 +     pkg_vec_t *available;
1685 +     pkg_t *pkg;
1686 +     char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1687 +     char *newline;
1688 +     char *pkg_name = NULL;
1689 +     char *version_str;
1690 +
1691 +     if (argc > 0) {
1692 +         pkg_name = argv[0];
1693 +     }
1694 +     available = pkg_vec_alloc();
1695 +     pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1696 +     for (i=0; i < available->len; i++) {
1697 +         pkg = available->pkgs[i];
1698 +         /* if we have package name or pattern and pkg does not match, then skip it */
1699 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) 
1700 +              continue;
1701 +         if (pkg->description) {
1702 +              strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1703 +         } else {
1704 +              desc_short[0] = '\0';
1705 +         }
1706 +         desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1707 +         newline = strchr(desc_short, '\n');
1708 +         if (newline) {
1709 +              *newline = '\0';
1710 +         }
1711 +#ifndef IPKG_LIB
1712 +         printf("%s - %s\n", pkg->name, desc_short);
1713 +#else
1714 +         if (ipkg_cb_list) {
1715 +               version_str = pkg_version_str_alloc(pkg);
1716 +               ipkg_cb_list(pkg->name,desc_short,
1717 +                                            version_str,
1718 +                                        pkg->state_status,
1719 +                                        p_userdata);
1720 +               free(version_str);
1721 +         }
1722 +#endif
1723 +     }
1724 +
1725 +     return 0;
1726 +}
1727 +
1728 +static int ipkg_info_status_cmd(ipkg_conf_t *conf, int argc, char **argv, int installed_only)
1729 +{
1730 +     int i;
1731 +     pkg_vec_t *available;
1732 +     pkg_t *pkg;
1733 +     char *pkg_name = NULL;
1734 +     char **pkg_fields = NULL;
1735 +     int n_fields = 0;
1736 +     char *buff ; // = (char *)malloc(1);
1737 +
1738 +     if (argc > 0) {
1739 +         pkg_name = argv[0];
1740 +     }
1741 +     if (argc > 1) {
1742 +         pkg_fields = &argv[1];
1743 +         n_fields = argc - 1;
1744 +     }
1745 +
1746 +     available = pkg_vec_alloc();
1747 +     if (installed_only)
1748 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1749 +     else
1750 +         pkg_hash_fetch_available(&conf->pkg_hash, available);
1751 +     for (i=0; i < available->len; i++) {
1752 +         pkg = available->pkgs[i];
1753 +         if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1754 +              continue;
1755 +         }
1756 +#ifndef IPKG_LIB
1757 +         if (n_fields) {
1758 +              for (j = 0; j < n_fields; j++)
1759 +                   pkg_print_field(pkg, stdout, pkg_fields[j]);
1760 +         } else {
1761 +              pkg_print_info(pkg, stdout);
1762 +         }
1763 +#else
1764 +
1765 +         buff = pkg_formatted_info(pkg);
1766 +          if ( buff ) {
1767 +              if (ipkg_cb_status) ipkg_cb_status(pkg->name,
1768 +                                                 pkg->state_status,
1769 +                                                 buff,
1770 +                                                 p_userdata);
1771 +/* 
1772 +   We should not forget that actually the pointer is allocated. 
1773 +   We need to free it :)  ( Thanks florian for seeing the error )
1774 +*/
1775 +               free(buff);
1776 +          }
1777 +#endif
1778 +         if (conf->verbosity > 1) {
1779 +              conffile_list_elt_t *iter;
1780 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
1781 +                   conffile_t *cf = iter->data;
1782 +                   int modified = conffile_has_been_modified(conf, cf);
1783 +                   ipkg_message(conf, IPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
1784 +                                cf->name, cf->value, modified);
1785 +              }
1786 +         }
1787 +     }
1788 +#ifndef IPKG_LIB
1789 +     if (buff)
1790 +         free(buff);
1791 +#endif
1792 +     pkg_vec_free(available);
1793 +
1794 +     return 0;
1795 +}
1796 +
1797 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv)
1798 +{
1799 +     return ipkg_info_status_cmd(conf, argc, argv, 0);
1800 +}
1801 +
1802 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv)
1803 +{
1804 +     return ipkg_info_status_cmd(conf, argc, argv, 1);
1805 +}
1806 +
1807 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv)
1808 +{
1809 +     
1810 +     int err;
1811 +     if (argc > 0) {
1812 +         char *pkg_name = NULL;
1813 +
1814 +         pkg_name = argv[0];
1815 +
1816 +         err = ipkg_configure_packages (conf, pkg_name);
1817 +     
1818 +     } else {
1819 +         err = ipkg_configure_packages (conf, NULL);
1820 +     }
1821 +
1822 +     write_status_files_if_changed(conf);
1823 +
1824 +     return err;
1825 +}
1826 +
1827 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv)
1828 +{
1829 +     int i, err;
1830 +     char *globpattern;
1831 +     glob_t globbuf;
1832 +    
1833 +     sprintf_alloc(&globpattern, "%s/*" IPKG_PKG_EXTENSION, conf->pending_dir);
1834 +     err = glob(globpattern, 0, NULL, &globbuf);
1835 +     free(globpattern);
1836 +     if (err) {
1837 +         return 0;
1838 +     }
1839 +
1840 +     ipkg_message(conf, IPKG_NOTICE,
1841 +                 "The following packages in %s will now be installed.\n",
1842 +                 conf->pending_dir);
1843 +     for (i = 0; i < globbuf.gl_pathc; i++) {
1844 +         ipkg_message(conf, IPKG_NOTICE,
1845 +                      "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
1846 +     }
1847 +     ipkg_message(conf, IPKG_NOTICE, "\n");
1848 +     for (i = 0; i < globbuf.gl_pathc; i++) {
1849 +         err = ipkg_install_from_file(conf, globbuf.gl_pathv[i]);
1850 +         if (err == 0) {
1851 +              err = unlink(globbuf.gl_pathv[i]);
1852 +              if (err) {
1853 +                   ipkg_message(conf, IPKG_ERROR,
1854 +                                "%s: ERROR: failed to unlink %s: %s\n",
1855 +                                __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
1856 +                   return err;
1857 +              }
1858 +         }
1859 +     }
1860 +     globfree(&globbuf);
1861 +
1862 +     return err;
1863 +}
1864 +
1865 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv)
1866 +{
1867 +     int i,a,done;
1868 +     pkg_t *pkg;
1869 +     pkg_t *pkg_to_remove;
1870 +     pkg_vec_t *available;
1871 +     char *pkg_name = NULL;
1872 +     global_conf = conf;
1873 +     signal(SIGINT, sigint_handler);
1874 +
1875 +// ENH: Add the "no pkg removed" just in case.
1876 +
1877 +    done = 0;
1878 +
1879 +     available = pkg_vec_alloc();
1880 +     pkg_info_preinstall_check(conf);
1881 +     if ( argc > 0 ) {
1882 +        pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1883 +        for (i=0; i < argc; i++) {
1884 +           pkg_name = malloc(strlen(argv[i])+2);
1885 +           strcpy(pkg_name,argv[i]);
1886 +           for (a=0; a < available->len; a++) {
1887 +               pkg = available->pkgs[a];
1888 +              if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1889 +                  continue;
1890 +               }
1891 +               if (conf->restrict_to_default_dest) {
1892 +                   pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1893 +                                                               pkg->name,
1894 +                                                               conf->default_dest);
1895 +               } else {
1896 +                   pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
1897 +               }
1898 +        
1899 +               if (pkg == NULL) {
1900 +                   ipkg_message(conf, IPKG_ERROR, "Package %s is not installed.\n", pkg->name);
1901 +                   continue;
1902 +               }
1903 +               if (pkg->state_status == SS_NOT_INSTALLED) {    // Added the control, so every already removed package could be skipped
1904 +                   ipkg_message(conf, IPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
1905 +                    continue;
1906 +               }
1907 +               ipkg_remove_pkg(conf, pkg_to_remove,0);
1908 +               done = 1;
1909 +           }
1910 +           free (pkg_name);
1911 +        }
1912 +        pkg_vec_free(available);
1913 +     } else {
1914 +         pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1915 +         int i;
1916 +         int flagged_pkg_count = 0;
1917 +         int removed;
1918 +
1919 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
1920 +
1921 +         for (i = 0; i < installed_pkgs->len; i++) {
1922 +              pkg_t *pkg = installed_pkgs->pkgs[i];
1923 +              if (pkg->state_flag & SF_USER) {
1924 +                   flagged_pkg_count++;
1925 +              } else {
1926 +                   if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
1927 +                        ipkg_message(conf, IPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
1928 +              }
1929 +         }
1930 +         if (!flagged_pkg_count) {
1931 +              ipkg_message(conf, IPKG_NOTICE, "No packages flagged as installed by user, \n"
1932 +                           "so refusing to uninstall unflagged non-leaf packages\n");
1933 +              return 0;
1934 +         }
1935 +
1936 +         /* find packages not flagged SF_USER (i.e., installed to
1937 +          * satisfy a dependence) and not having any dependents, and
1938 +          * remove them */
1939 +         do {
1940 +              removed = 0;
1941 +              for (i = 0; i < installed_pkgs->len; i++) {
1942 +                   pkg_t *pkg = installed_pkgs->pkgs[i];
1943 +                   if (!(pkg->state_flag & SF_USER)
1944 +                       && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
1945 +                        removed++;
1946 +                        ipkg_message(conf, IPKG_NOTICE, "Removing non-user leaf package %s\n");
1947 +                        ipkg_remove_pkg(conf, pkg,0);
1948 +                         done = 1;
1949 +                   }
1950 +              }
1951 +         } while (removed);
1952 +         pkg_vec_free(installed_pkgs);
1953 +     }
1954 +
1955 +     if ( done == 0 ) 
1956 +        ipkg_message(conf, IPKG_NOTICE, "No packages removed.\n");
1957 +
1958 +     write_status_files_if_changed(conf);
1959 +     return 0;
1960 +}
1961 +
1962 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv)
1963 +{
1964 +     int i;
1965 +     pkg_t *pkg;
1966 +
1967 +     global_conf = conf;
1968 +     signal(SIGINT, sigint_handler);
1969 +
1970 +     pkg_info_preinstall_check(conf);
1971 +
1972 +     for (i=0; i < argc; i++) {
1973 +         if (conf->restrict_to_default_dest) {
1974 +              pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1975 +                                                          argv[i],
1976 +                                                          conf->default_dest);
1977 +         } else {
1978 +              pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1979 +         }
1980 +
1981 +         if (pkg == NULL) {
1982 +              ipkg_message(conf, IPKG_ERROR,
1983 +                           "Package %s is not installed.\n", argv[i]);
1984 +              continue;
1985 +         }
1986 +         ipkg_purge_pkg(conf, pkg);
1987 +     }
1988 +
1989 +     write_status_files_if_changed(conf);
1990 +     return 0;
1991 +}
1992 +
1993 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv)
1994 +{
1995 +     int i;
1996 +     pkg_t *pkg;
1997 +     const char *flags = argv[0];
1998 +    
1999 +     global_conf = conf;
2000 +     signal(SIGINT, sigint_handler);
2001 +
2002 +     for (i=1; i < argc; i++) {
2003 +         if (conf->restrict_to_default_dest) {
2004 +              pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
2005 +                                                          argv[i],
2006 +                                                          conf->default_dest);
2007 +         } else {
2008 +              pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
2009 +         }
2010 +
2011 +         if (pkg == NULL) {
2012 +              ipkg_message(conf, IPKG_ERROR,
2013 +                           "Package %s is not installed.\n", argv[i]);
2014 +              continue;
2015 +         }
2016 +          if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
2017 +              ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
2018 +             pkg->state_flag = pkg_state_flag_from_str(flags);
2019 +          }
2020 +/* pb_ asked this feature 03292004 */
2021 +/* Actually I will use only this two, but this is an open for various status */
2022 +          if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
2023 +             pkg->state_status = pkg_state_status_from_str(flags);
2024 +          }
2025 +         ipkg_state_changed++;
2026 +         ipkg_message(conf, IPKG_NOTICE,
2027 +                      "Setting flags for package %s to %s\n",
2028 +                      pkg->name, flags);
2029 +     }
2030 +
2031 +     write_status_files_if_changed(conf);
2032 +     return 0;
2033 +}
2034 +
2035 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv)
2036 +{
2037 +     pkg_t *pkg;
2038 +     str_list_t *installed_files;
2039 +     str_list_elt_t *iter;
2040 +     char *pkg_version;
2041 +     size_t buff_len = 8192;
2042 +     size_t used_len;
2043 +     char *buff ;
2044 +
2045 +     buff = (char *)malloc(buff_len);
2046 +     if ( buff == NULL ) {
2047 +        fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
2048 +        return ENOMEM;
2049 +     }
2050
2051 +     if (argc < 1) {
2052 +         return EINVAL;
2053 +     }
2054 +
2055 +     pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
2056 +                                           argv[0]);
2057 +     if (pkg == NULL) {
2058 +         ipkg_message(conf, IPKG_ERROR,
2059 +                      "Package %s not installed.\n", argv[0]);
2060 +         return 0;
2061 +     }
2062 +
2063 +     installed_files = pkg_get_installed_files(pkg);
2064 +     pkg_version = pkg_version_str_alloc(pkg);
2065 +
2066 +#ifndef IPKG_LIB
2067 +     printf("Package %s (%s) is installed on %s and has the following files:\n",
2068 +           pkg->name, pkg_version, pkg->dest->name);
2069 +     for (iter = installed_files->head; iter; iter = iter->next) {
2070 +         puts(iter->data);
2071 +     }
2072 +#else
2073 +     if (buff) {
2074 +     try_again:
2075 +         used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
2076 +                             pkg->name, pkg_version, pkg->dest->name) + 1;
2077 +         if (used_len > buff_len) {
2078 +              buff_len *= 2;
2079 +              buff = realloc (buff, buff_len);
2080 +              goto try_again;
2081 +         }
2082 +         for (iter = installed_files->head; iter; iter = iter->next) {
2083 +              used_len += strlen (iter->data) + 1;
2084 +              while (buff_len <= used_len) {
2085 +                   buff_len *= 2;
2086 +                   buff = realloc (buff, buff_len);
2087 +              }
2088 +              strncat(buff, iter->data, buff_len);
2089 +              strncat(buff, "\n", buff_len);
2090 +         } 
2091 +         if (ipkg_cb_list) ipkg_cb_list(pkg->name,
2092 +                                        buff,
2093 +                                        pkg_version_str_alloc(pkg),
2094 +                                        pkg->state_status,
2095 +                                        p_userdata);
2096 +         free(buff);
2097 +     }
2098 +#endif
2099 +
2100 +     free(pkg_version);
2101 +     pkg_free_installed_files(pkg);
2102 +
2103 +     return 0;
2104 +}
2105 +
2106 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2107 +{
2108 +
2109 +     if (argc > 0) {
2110 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2111 +         const char *rel_str = "depends on";
2112 +         int i;
2113 +     
2114 +         pkg_info_preinstall_check(conf);
2115 +
2116 +         if (conf->query_all)
2117 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2118 +         else
2119 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2120 +         for (i = 0; i < argc; i++) {
2121 +              const char *target = argv[i];
2122 +              int j;
2123 +
2124 +              ipkg_message(conf, IPKG_ERROR, "target=%s\n", target);
2125 +
2126 +              for (j = 0; j < available_pkgs->len; j++) {
2127 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2128 +                   if (fnmatch(target, pkg->name, 0) == 0) {
2129 +                        int k;
2130 +                        int count = pkg->depends_count + pkg->pre_depends_count;
2131 +                        ipkg_message(conf, IPKG_ERROR, "What %s (arch=%s) %s\n",
2132 +                                     target, pkg->architecture, rel_str);
2133 +                        for (k = 0; k < count; k++) {
2134 +                             compound_depend_t *cdepend = &pkg->depends[k];
2135 +                             int l;
2136 +                             for (l = 0; l < cdepend->possibility_count; l++) {
2137 +                                  depend_t *possibility = cdepend->possibilities[l];
2138 +                                  ipkg_message(conf, IPKG_ERROR, "    %s", possibility->pkg->name);
2139 +                                  if (conf->verbosity > 0) {
2140 +                                       // char *ver = abstract_pkg_version_str_alloc(possibility->pkg); 
2141 +                                       ipkg_message(conf, IPKG_NOTICE, " %s", possibility->version);
2142 +                                       if (possibility->version) {
2143 +                                            char *typestr = NULL;
2144 +                                            switch (possibility->constraint) {
2145 +                                            case NONE: typestr = "none"; break;
2146 +                                            case EARLIER: typestr = "<"; break;
2147 +                                            case EARLIER_EQUAL: typestr = "<="; break;
2148 +                                            case EQUAL: typestr = "="; break;
2149 +                                            case LATER_EQUAL: typestr = ">="; break;
2150 +                                            case LATER: typestr = ">"; break;
2151 +                                            }
2152 +                                            ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2153 +                                       }
2154 +                                       // free(ver);
2155 +                                  }
2156 +                                  ipkg_message(conf, IPKG_ERROR, "\n");
2157 +                             }
2158 +                        }
2159 +                   }
2160 +              }
2161 +         }
2162 +         pkg_vec_free(available_pkgs);
2163 +     }
2164 +     return 0;
2165 +}
2166 +
2167 +enum what_field_type {
2168 +  WHATDEPENDS,
2169 +  WHATCONFLICTS,
2170 +  WHATPROVIDES,
2171 +  WHATREPLACES,
2172 +  WHATRECOMMENDS,
2173 +  WHATSUGGESTS
2174 +};
2175 +
2176 +static int ipkg_what_depends_conflicts_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
2177 +{
2178 +
2179 +     if (argc > 0) {
2180 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2181 +         const char *rel_str = NULL;
2182 +         int i;
2183 +         int changed;
2184 +
2185 +         switch (what_field_type) {
2186 +         case WHATDEPENDS: rel_str = "depends on"; break;
2187 +         case WHATCONFLICTS: rel_str = "conflicts with"; break;
2188 +         case WHATSUGGESTS: rel_str = "suggests"; break;
2189 +         case WHATRECOMMENDS: rel_str = "recommends"; break;
2190 +         case WHATPROVIDES: rel_str = "provides"; break;
2191 +         case WHATREPLACES: rel_str = "replaces"; break;
2192 +         }
2193 +     
2194 +         if (conf->query_all)
2195 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2196 +         else
2197 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2198 +
2199 +         /* mark the root set */
2200 +         pkg_vec_clear_marks(available_pkgs);
2201 +         ipkg_message(conf, IPKG_NOTICE, "Root set:\n");
2202 +         for (i = 0; i < argc; i++) {
2203 +              const char *dependee_pattern = argv[i];
2204 +              pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
2205 +         }
2206 +         for (i = 0; i < available_pkgs->len; i++) {
2207 +              pkg_t *pkg = available_pkgs->pkgs[i];
2208 +              if (pkg->state_flag & SF_MARKED) {
2209 +                   /* mark the parent (abstract) package */
2210 +                   pkg_mark_provides(pkg);
2211 +                   ipkg_message(conf, IPKG_NOTICE, "  %s\n", pkg->name);
2212 +              }
2213 +         }
2214 +
2215 +         ipkg_message(conf, IPKG_NOTICE, "What %s root set\n", rel_str);
2216 +         do {
2217 +              int j;
2218 +              changed = 0;
2219 +
2220 +              for (j = 0; j < available_pkgs->len; j++) {
2221 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2222 +                   int k;
2223 +                   int count = ((what_field_type == WHATCONFLICTS)
2224 +                                ? pkg->conflicts_count
2225 +                                : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
2226 +                   /* skip this package if it is already marked */
2227 +                   if (pkg->parent->state_flag & SF_MARKED) {
2228 +                        continue;
2229 +                   }
2230 +                   for (k = 0; k < count; k++) {
2231 +                        compound_depend_t *cdepend = 
2232 +                             (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
2233 +                        int l;
2234 +                        for (l = 0; l < cdepend->possibility_count; l++) {
2235 +                             depend_t *possibility = cdepend->possibilities[l];
2236 +                             if (possibility->pkg->state_flag & SF_MARKED) {
2237 +                                  /* mark the depending package so we won't visit it again */
2238 +                                  pkg->state_flag |= SF_MARKED;
2239 +                                  pkg_mark_provides(pkg);
2240 +                                  changed++;
2241 +
2242 +                                  ipkg_message(conf, IPKG_NOTICE, "    %s", pkg->name);
2243 +                                  if (conf->verbosity > 0) {
2244 +                                       char *ver = pkg_version_str_alloc(pkg); 
2245 +                                       ipkg_message(conf, IPKG_NOTICE, " %s", ver);
2246 +                                       ipkg_message(conf, IPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
2247 +                                       if (possibility->version) {
2248 +                                            char *typestr = NULL;
2249 +                                            switch (possibility->constraint) {
2250 +                                            case NONE: typestr = "none"; break;
2251 +                                            case EARLIER: typestr = "<"; break;
2252 +                                            case EARLIER_EQUAL: typestr = "<="; break;
2253 +                                            case EQUAL: typestr = "="; break;
2254 +                                            case LATER_EQUAL: typestr = ">="; break;
2255 +                                            case LATER: typestr = ">"; break;
2256 +                                            }
2257 +                                            ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2258 +                                       }
2259 +                                       free(ver);
2260 +                                       if (!pkg_dependence_satisfiable(conf, possibility))
2261 +                                            ipkg_message(conf, IPKG_NOTICE, " unsatisfiable");
2262 +                                  }
2263 +                                  ipkg_message(conf, IPKG_NOTICE, "\n");
2264 +                                  goto next_package;
2265 +                             }
2266 +                        }
2267 +                   }
2268 +              next_package:
2269 +                   ;
2270 +              }
2271 +         } while (changed && recursive);
2272 +         pkg_vec_free(available_pkgs);
2273 +     }
2274 +
2275 +     return 0;
2276 +}
2277 +
2278 +int pkg_mark_provides(pkg_t *pkg)
2279 +{
2280 +     int provides_count = pkg->provides_count;
2281 +     abstract_pkg_t **provides = pkg->provides;
2282 +     int i;
2283 +     pkg->parent->state_flag |= SF_MARKED;
2284 +     for (i = 0; i < provides_count; i++) {
2285 +         provides[i]->state_flag |= SF_MARKED;
2286 +     }
2287 +     return 0;
2288 +}
2289 +
2290 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv)
2291 +{
2292 +     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
2293 +}
2294 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2295 +{
2296 +     return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
2297 +}
2298 +
2299 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv)
2300 +{
2301 +     return ipkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
2302 +}
2303 +
2304 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2305 +{
2306 +     return ipkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
2307 +}
2308 +
2309 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv)
2310 +{
2311 +     return ipkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
2312 +}
2313 +
2314 +static int ipkg_what_provides_replaces_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
2315 +{
2316 +
2317 +     if (argc > 0) {
2318 +         pkg_vec_t *available_pkgs = pkg_vec_alloc();
2319 +         const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
2320 +         int i;
2321 +     
2322 +         pkg_info_preinstall_check(conf);
2323 +
2324 +         if (conf->query_all)
2325 +              pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2326 +         else
2327 +              pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2328 +         for (i = 0; i < argc; i++) {
2329 +              const char *target = argv[i];
2330 +              int j;
2331 +
2332 +              ipkg_message(conf, IPKG_ERROR, "What %s %s\n",
2333 +                           rel_str, target);
2334 +              for (j = 0; j < available_pkgs->len; j++) {
2335 +                   pkg_t *pkg = available_pkgs->pkgs[j];
2336 +                   int k;
2337 +                   int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
2338 +                   for (k = 0; k < count; k++) {
2339 +                        abstract_pkg_t *apkg = 
2340 +                             ((what_field_type == WHATPROVIDES) 
2341 +                              ? pkg->provides[k]
2342 +                              : pkg->replaces[k]);
2343 +                        if (fnmatch(target, apkg->name, 0) == 0) {
2344 +                             ipkg_message(conf, IPKG_ERROR, "    %s", pkg->name);
2345 +                             if (strcmp(target, apkg->name) != 0)
2346 +                                  ipkg_message(conf, IPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
2347 +                             ipkg_message(conf, IPKG_ERROR, "\n");
2348 +                        }
2349 +                   }
2350 +              }
2351 +         }
2352 +         pkg_vec_free(available_pkgs);
2353 +     }
2354 +     return 0;
2355 +}
2356 +
2357 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv)
2358 +{
2359 +     return ipkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
2360 +}
2361 +
2362 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv)
2363 +{
2364 +     return ipkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
2365 +}
2366 +
2367 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv)
2368 +{
2369 +     int i;
2370 +
2371 +     pkg_vec_t *installed;
2372 +     pkg_t *pkg;
2373 +     str_list_t *installed_files;
2374 +     str_list_elt_t *iter;
2375 +     char *installed_file;
2376 +
2377 +     if (argc < 1) {
2378 +         return EINVAL;
2379 +     }
2380
2381 +     installed = pkg_vec_alloc();
2382 +     pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
2383 +
2384 +     for (i=0; i < installed->len; i++) {
2385 +         pkg = installed->pkgs[i];
2386 +
2387 +         installed_files = pkg_get_installed_files(pkg);
2388 +
2389 +         for (iter = installed_files->head; iter; iter = iter->next) {
2390 +              installed_file = iter->data;
2391 +              if (fnmatch(argv[0], installed_file, 0)==0)  {
2392 +#ifndef IPKG_LIB
2393 +                   printf("%s: %s\n", pkg->name, installed_file);
2394 +#else
2395 +                       if (ipkg_cb_list) ipkg_cb_list(pkg->name, 
2396 +                                                      installed_file, 
2397 +                                                      pkg_version_str_alloc(pkg), 
2398 +                                                      pkg->state_status, p_userdata);
2399 +#endif                    
2400 +              }                
2401 +         }
2402 +
2403 +         pkg_free_installed_files(pkg);
2404 +     }
2405 +
2406 +     /* XXX: CLEANUP: It's not obvious from the name of
2407 +       pkg_hash_fetch_all_installed that we need to call
2408 +       pkg_vec_free to avoid a memory leak. */
2409 +     pkg_vec_free(installed);
2410 +
2411 +     return 0;
2412 +}
2413 +
2414 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv)
2415 +{
2416 +     if (argc == 3) {
2417 +         /* this is a bit gross */
2418 +         struct pkg p1, p2;
2419 +         parseVersion(&p1, argv[0]); 
2420 +         parseVersion(&p2, argv[2]); 
2421 +         return pkg_version_satisfied(&p1, &p2, argv[1]);
2422 +     } else {
2423 +         ipkg_message(conf, IPKG_ERROR,
2424 +                      "ipkg compare_versions <v1> <op> <v2>\n"
2425 +                      "<op> is one of <= >= << >> =\n");
2426 +         return -1;
2427 +     }
2428 +}
2429 +
2430 +#ifndef HOST_CPU_STR
2431 +#define HOST_CPU_STR__(X) #X
2432 +#define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
2433 +#define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
2434 +#endif
2435 +
2436 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv)
2437 +{
2438 +     nv_pair_list_elt_t *l;
2439 +
2440 +     l = conf->arch_list.head;
2441 +     while (l) {
2442 +         nv_pair_t *nv = l->data;
2443 +         printf("arch %s %s\n", nv->name, nv->value);
2444 +         l = l->next;
2445 +     }
2446 +     return 0;
2447 +}
2448 +
2449 +
2450 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.h
2451 --- busybox-1.2.0-orig/archival/libipkg/ipkg_cmd.h      1970-01-01 01:00:00.000000000 +0100
2452 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_cmd.h     2006-07-22 16:31:25.000000000 +0200
2453 @@ -0,0 +1,46 @@
2454 +/* ipkg_cmd.h - the itsy package management system
2455 +
2456 +   Carl D. Worth
2457 +
2458 +   Copyright (C) 2001 University of Southern California
2459 +
2460 +   This program is free software; you can redistribute it and/or
2461 +   modify it under the terms of the GNU General Public License as
2462 +   published by the Free Software Foundation; either version 2, or (at
2463 +   your option) any later version.
2464 +
2465 +   This program is distributed in the hope that it will be useful, but
2466 +   WITHOUT ANY WARRANTY; without even the implied warranty of
2467 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2468 +   General Public License for more details.
2469 +*/
2470 +
2471 +#ifndef IPKG_CMD_H
2472 +#define IPKG_CMD_H
2473 +
2474 +typedef int (*ipkg_cmd_fun_t)(ipkg_conf_t *conf, int argc, const char **argv);
2475 +
2476 +struct ipkg_cmd
2477 +{
2478 +    char *name;
2479 +    int requires_args;
2480 +    ipkg_cmd_fun_t fun;
2481 +};
2482 +typedef struct ipkg_cmd ipkg_cmd_t;
2483 +
2484 +ipkg_cmd_t *ipkg_cmd_find(const char *name);
2485 +#ifdef IPKG_LIB
2486 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, 
2487 +                  const char **argv, void *userdata);
2488 +#else
2489 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv);
2490 +#endif
2491 +int ipkg_multiple_files_scan (ipkg_conf_t *conf, int argc, char *argv[]);
2492 +/* install any packges with state_want == SW_INSTALL */
2493 +int ipkg_install_wanted_packages(ipkg_conf_t *conf);
2494 +/* ensure that all dependences are satisfied */
2495 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name);
2496 +
2497 +int pkg_mark_provides(pkg_t *pkg);
2498 +
2499 +#endif
2500 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_conf.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.c
2501 --- busybox-1.2.0-orig/archival/libipkg/ipkg_conf.c     1970-01-01 01:00:00.000000000 +0100
2502 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.c    2006-07-22 16:31:25.000000000 +0200
2503 @@ -0,0 +1,711 @@
2504 +/* ipkg_conf.c - the itsy package management system
2505 +
2506 +   Carl D. Worth
2507 +
2508 +   Copyright (C) 2001 University of Southern California
2509 +
2510 +   This program is free software; you can redistribute it and/or
2511 +   modify it under the terms of the GNU General Public License as
2512 +   published by the Free Software Foundation; either version 2, or (at
2513 +   your option) any later version.
2514 +
2515 +   This program is distributed in the hope that it will be useful, but
2516 +   WITHOUT ANY WARRANTY; without even the implied warranty of
2517 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2518 +   General Public License for more details.
2519 +*/
2520 +
2521 +#include <glob.h>
2522 +
2523 +#include "ipkg.h"
2524 +#include "ipkg_conf.h"
2525 +
2526 +#include "xregex.h"
2527 +#include "sprintf_alloc.h"
2528 +#include "ipkg_conf.h"
2529 +#include "ipkg_message.h"
2530 +#include "file_util.h"
2531 +#include "str_util.h"
2532 +#include "xsystem.h"
2533 +
2534 +
2535 +ipkg_conf_t *global_conf;
2536 +
2537 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2538 +                               pkg_src_list_t *pkg_src_list,
2539 +                               nv_pair_list_t *tmp_dest_nv_pair_list,
2540 +                               char **tmp_lists_dir);
2541 +static int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options);
2542 +static int ipkg_conf_set_option(const ipkg_option_t *options,
2543 +                               const char *name, const char *value);
2544 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2545 +                                     const char *default_dest_name);
2546 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf,
2547 +                                    pkg_src_list_t *nv_pair_list);
2548 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf,
2549 +                                     nv_pair_list_t *nv_pair_list, char * lists_dir);
2550 +
2551 +int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options)
2552 +{
2553 +     ipkg_option_t tmp[] = {
2554 +         { "force_defaults", IPKG_OPT_TYPE_BOOL, &conf->force_defaults },
2555 +         { "force_depends", IPKG_OPT_TYPE_BOOL, &conf->force_depends },
2556 +         { "force_overwrite", IPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
2557 +         { "force_downgrade", IPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
2558 +         { "force_reinstall", IPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
2559 +         { "force_space", IPKG_OPT_TYPE_BOOL, &conf->force_space },
2560 +         { "ftp_proxy", IPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
2561 +         { "http_proxy", IPKG_OPT_TYPE_STRING, &conf->http_proxy },
2562 +         { "multiple_providers", IPKG_OPT_TYPE_BOOL, &conf->multiple_providers },
2563 +         { "no_proxy", IPKG_OPT_TYPE_STRING, &conf->no_proxy },
2564 +         { "test", IPKG_OPT_TYPE_INT, &conf->noaction },
2565 +         { "noaction", IPKG_OPT_TYPE_INT, &conf->noaction },
2566 +         { "nodeps", IPKG_OPT_TYPE_BOOL, &conf->nodeps },
2567 +         { "offline_root", IPKG_OPT_TYPE_STRING, &conf->offline_root },
2568 +         { "offline_root_post_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
2569 +         { "offline_root_pre_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
2570 +         { "proxy_passwd", IPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
2571 +         { "proxy_user", IPKG_OPT_TYPE_STRING, &conf->proxy_user },
2572 +         { "query-all", IPKG_OPT_TYPE_BOOL, &conf->query_all },
2573 +         { "verbose-wget", IPKG_OPT_TYPE_BOOL, &conf->verbose_wget },
2574 +         { "verbosity", IPKG_OPT_TYPE_BOOL, &conf->verbosity },
2575 +         { NULL }
2576 +     };
2577 +
2578 +     *options = (ipkg_option_t *)malloc(sizeof(tmp));
2579 +     if ( options == NULL ){
2580 +        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
2581 +        return -1;
2582 +     }
2583 +
2584 +     memcpy(*options, tmp, sizeof(tmp));
2585 +     return 0;
2586 +};
2587 +
2588 +static void ipkg_conf_override_string(char **conf_str, char *arg_str) 
2589 +{
2590 +     if (arg_str) {
2591 +         if (*conf_str) {
2592 +              free(*conf_str);
2593 +         }
2594 +         *conf_str = strdup(arg_str);
2595 +     }
2596 +}
2597 +
2598 +static void ipkg_conf_free_string(char **conf_str)
2599 +{
2600 +     if (*conf_str) {
2601 +         free(*conf_str);
2602 +         *conf_str = NULL;
2603 +     }
2604 +}
2605 +
2606 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args)
2607 +{
2608 +     int err;
2609 +     char *tmp_dir_base;
2610 +     nv_pair_list_t tmp_dest_nv_pair_list;
2611 +     char * lists_dir =NULL;
2612 +     glob_t globbuf;
2613 +     char *etc_ipkg_conf_pattern = "/etc/ipkg/*.conf";
2614 +     char *pending_dir  =NULL;
2615 +
2616 +     memset(conf, 0, sizeof(ipkg_conf_t));
2617 +
2618 +     pkg_src_list_init(&conf->pkg_src_list);
2619 +
2620 +     nv_pair_list_init(&tmp_dest_nv_pair_list);
2621 +     pkg_dest_list_init(&conf->pkg_dest_list);
2622 +
2623 +     nv_pair_list_init(&conf->arch_list);
2624 +
2625 +     conf->restrict_to_default_dest = 0;
2626 +     conf->default_dest = NULL;
2627 +
2628 +
2629 +     if (args->tmp_dir)
2630 +         tmp_dir_base = args->tmp_dir;
2631 +     else 
2632 +         tmp_dir_base = getenv("TMPDIR");
2633 +     sprintf_alloc(&conf->tmp_dir, "%s/%s",
2634 +                  tmp_dir_base ? tmp_dir_base : IPKG_CONF_DEFAULT_TMP_DIR_BASE,
2635 +                  IPKG_CONF_TMP_DIR_SUFFIX);
2636 +     conf->tmp_dir = mkdtemp(conf->tmp_dir);
2637 +     if (conf->tmp_dir == NULL) {
2638 +         fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
2639 +                 __FUNCTION__, conf->tmp_dir, strerror(errno));
2640 +         return errno;
2641 +     }
2642 +
2643 +     conf->force_depends = 0;
2644 +     conf->force_defaults = 0;
2645 +     conf->force_overwrite = 0;
2646 +     conf->force_downgrade = 0;
2647 +     conf->force_reinstall = 0;
2648 +     conf->force_space = 0;
2649 +     conf->force_removal_of_essential_packages = 0;
2650 +     conf->force_removal_of_dependent_packages = 0;
2651 +     conf->nodeps = 0;
2652 +     conf->verbose_wget = 0;
2653 +     conf->offline_root = NULL;
2654 +     conf->offline_root_pre_script_cmd = NULL;
2655 +     conf->offline_root_post_script_cmd = NULL;
2656 +     conf->multiple_providers = 0;
2657 +     conf->verbosity = 1;
2658 +     conf->noaction = 0;
2659 +
2660 +     conf->http_proxy = NULL;
2661 +     conf->ftp_proxy = NULL;
2662 +     conf->no_proxy = NULL;
2663 +     conf->proxy_user = NULL;
2664 +     conf->proxy_passwd = NULL;
2665 +
2666 +     pkg_hash_init("pkg-hash", &conf->pkg_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2667 +     hash_table_init("file-hash", &conf->file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2668 +     hash_table_init("obs-file-hash", &conf->obs_file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2669 +     lists_dir=(char *)malloc(1);
2670 +     lists_dir[0]='\0';
2671 +     if (args->conf_file) {
2672 +         struct stat stat_buf;
2673 +         err = stat(args->conf_file, &stat_buf);
2674 +         if (err == 0)
2675 +              if (ipkg_conf_parse_file(conf, args->conf_file,
2676 +                                   &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2677 +                   /* Memory leakage from ipkg_conf_parse-file */
2678 +                   return -1;
2679 +               }
2680 +                   
2681 +     }
2682 +
2683 +     /* if (!lists_dir ){*/
2684 +     if (strlen(lists_dir)<=1 ){
2685 +        lists_dir = realloc(lists_dir,strlen(IPKG_CONF_LISTS_DIR)+2);
2686 +        sprintf (lists_dir,"%s",IPKG_CONF_LISTS_DIR);
2687 +     }
2688 +
2689 +     if (args->offline_root) {
2690 +            char *tmp = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
2691 +            sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
2692 +            free(lists_dir);
2693 +            lists_dir = tmp;
2694 +     }
2695 +
2696 +     pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
2697 +     snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
2698 +
2699 +     conf->lists_dir = strdup(lists_dir);
2700 +     conf->pending_dir = strdup(pending_dir);
2701 +
2702 +     if (args->offline_root) 
2703 +         sprintf_alloc(&etc_ipkg_conf_pattern, "%s/etc/ipkg/*.conf", args->offline_root);
2704 +     memset(&globbuf, 0, sizeof(globbuf));
2705 +     err = glob(etc_ipkg_conf_pattern, 0, NULL, &globbuf);
2706 +     if (!err) {
2707 +         int i;
2708 +         for (i = 0; i < globbuf.gl_pathc; i++) {
2709 +              if (globbuf.gl_pathv[i]) 
2710 +                   if ( ipkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
2711 +                                        &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2712 +                        /* Memory leakage from ipkg_conf_parse-file */
2713 +                        return -1;
2714 +                   }
2715 +         }
2716 +     }
2717 +     globfree(&globbuf);
2718 +
2719 +     /* if no architectures were defined, then default all, noarch, and host architecture */
2720 +     if (nv_pair_list_empty(&conf->arch_list)) {
2721 +         nv_pair_list_append(&conf->arch_list, "all", "1");
2722 +         nv_pair_list_append(&conf->arch_list, "noarch", "1");
2723 +         nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
2724 +     }
2725 +
2726 +     /* Even if there is no conf file, we'll need at least one dest. */
2727 +     if (tmp_dest_nv_pair_list.head == NULL) {
2728 +         nv_pair_list_append(&tmp_dest_nv_pair_list,
2729 +                             IPKG_CONF_DEFAULT_DEST_NAME,
2730 +                             IPKG_CONF_DEFAULT_DEST_ROOT_DIR);
2731 +     }
2732 +
2733 +     /* After parsing the file, set options from command-line, (so that
2734 +       command-line arguments take precedence) */
2735 +     /* XXX: CLEANUP: The interaction between args.c and ipkg_conf.c
2736 +       really needs to be cleaned up. There is so much duplication
2737 +       right now it is ridiculous. Maybe ipkg_conf_t should just save
2738 +       a pointer to args_t (which could then not be freed), rather
2739 +       than duplicating every field here? */
2740 +     if (args->force_depends) {
2741 +         conf->force_depends = 1;
2742 +     }
2743 +     if (args->force_defaults) {
2744 +         conf->force_defaults = 1;
2745 +     }
2746 +     if (args->force_overwrite) {
2747 +         conf->force_overwrite = 1;
2748 +     }
2749 +     if (args->force_downgrade) {
2750 +         conf->force_downgrade = 1;
2751 +     }
2752 +     if (args->force_reinstall) {
2753 +         conf->force_reinstall = 1;
2754 +     }
2755 +     if (args->force_removal_of_dependent_packages) {
2756 +         conf->force_removal_of_dependent_packages = 1;
2757 +     }
2758 +     if (args->force_removal_of_essential_packages) {
2759 +         conf->force_removal_of_essential_packages = 1;
2760 +     }
2761 +     if (args->nodeps) {
2762 +         conf->nodeps = 1;
2763 +     }
2764 +     if (args->noaction) {
2765 +         conf->noaction = 1;
2766 +     }
2767 +     if (args->query_all) {
2768 +         conf->query_all = 1;
2769 +     }
2770 +     if (args->verbose_wget) {
2771 +         conf->verbose_wget = 1;
2772 +     }
2773 +     if (args->multiple_providers) {
2774 +         conf->multiple_providers = 1;
2775 +     }
2776 +     if (args->verbosity != conf->verbosity) {
2777 +         conf->verbosity = args->verbosity;
2778 +     } 
2779 +
2780 +     ipkg_conf_override_string(&conf->offline_root, 
2781 +                              args->offline_root);
2782 +     ipkg_conf_override_string(&conf->offline_root_pre_script_cmd, 
2783 +                              args->offline_root_pre_script_cmd);
2784 +     ipkg_conf_override_string(&conf->offline_root_post_script_cmd, 
2785 +                              args->offline_root_post_script_cmd);
2786 +
2787 +/* Pigi: added a flag to disable the checking of structures if the command does not need to 
2788 +         read anything from there.
2789 +*/
2790 +     if ( !(args->nocheckfordirorfile)){
2791 +        /* need to run load the source list before dest list -Jamey */
2792 +        if ( !(args->noreadfeedsfile))
2793 +           set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
2794 +   
2795 +        /* Now that we have resolved conf->offline_root, we can commit to
2796 +          the directory names for the dests and load in all the package
2797 +          lists. */
2798 +        set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
2799 +   
2800 +        if (args->dest) {
2801 +            err = ipkg_conf_set_default_dest(conf, args->dest);
2802 +            if (err) {
2803 +                 return err;
2804 +            }
2805 +        }
2806 +     }
2807 +     nv_pair_list_deinit(&tmp_dest_nv_pair_list);
2808 +     free(lists_dir);
2809 +     free(pending_dir);
2810 +
2811 +     return 0;
2812 +}
2813 +
2814 +void ipkg_conf_deinit(ipkg_conf_t *conf)
2815 +{
2816 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
2817 +#error
2818 +     fprintf(stderr, "%s: Not cleaning up %s since ipkg compiled "
2819 +            "with IPKG_DEBUG_NO_TMP_CLEANUP\n",
2820 +            __FUNCTION__, conf->tmp_dir);
2821 +#else
2822 +     int err;
2823 +
2824 +     err = rmdir(conf->tmp_dir);
2825 +     if (err) {
2826 +         if (errno == ENOTEMPTY) {
2827 +              char *cmd;
2828 +              sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
2829 +              err = xsystem(cmd);
2830 +              free(cmd);
2831 +         }
2832 +         if (err)
2833 +              fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
2834 +     }
2835 +#endif /* IPKG_DEBUG_NO_TMP_CLEANUP */
2836 +
2837 +     free(conf->tmp_dir); /*XXX*/
2838 +
2839 +     pkg_src_list_deinit(&conf->pkg_src_list);
2840 +     pkg_dest_list_deinit(&conf->pkg_dest_list);
2841 +     nv_pair_list_deinit(&conf->arch_list);
2842 +     if (&conf->pkg_hash)
2843 +                   pkg_hash_deinit(&conf->pkg_hash);
2844 +     if (&conf->file_hash)
2845 +                   hash_table_deinit(&conf->file_hash);
2846 +     if (&conf->obs_file_hash)
2847 +                   hash_table_deinit(&conf->obs_file_hash);
2848 +
2849 +     ipkg_conf_free_string(&conf->offline_root);
2850 +     ipkg_conf_free_string(&conf->offline_root_pre_script_cmd);
2851 +     ipkg_conf_free_string(&conf->offline_root_post_script_cmd);
2852 +
2853 +     if (conf->verbosity > 1) { 
2854 +         int i;
2855 +         hash_table_t *hashes[] = {
2856 +              &conf->pkg_hash,
2857 +              &conf->file_hash,
2858 +              &conf->obs_file_hash };
2859 +         for (i = 0; i < 3; i++) {
2860 +              hash_table_t *hash = hashes[i];
2861 +              int c = 0;
2862 +              int n_conflicts = 0;
2863 +              int j;
2864 +              for (j = 0; j < hash->n_entries; j++) {
2865 +                   int len = 0;
2866 +                   hash_entry_t *e = &hash->entries[j];
2867 +                   if (e->next)
2868 +                        n_conflicts++;
2869 +                   while (e && e->key) {
2870 +                        len++;
2871 +                        e = e->next;
2872 +                   }
2873 +                   if (len > c) 
2874 +                        c = len;
2875 +              }
2876 +              ipkg_message(conf, IPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 
2877 +                           hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
2878 +              hash_table_deinit(hash);
2879 +         }
2880 +     }
2881 +}
2882 +
2883 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2884 +                                     const char *default_dest_name)
2885 +{
2886 +     pkg_dest_list_elt_t *iter;
2887 +     pkg_dest_t *dest;
2888 +
2889 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
2890 +         dest = iter->data;
2891 +         if (strcmp(dest->name, default_dest_name) == 0) {
2892 +              conf->default_dest = dest;
2893 +              conf->restrict_to_default_dest = 1;
2894 +              return 0;
2895 +         }
2896 +     }
2897 +
2898 +     fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
2899 +
2900 +     return 1;
2901 +}
2902 +
2903 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
2904 +{
2905 +     pkg_src_list_elt_t *iter;
2906 +     pkg_src_t *src;
2907 +     char *list_file;
2908 +
2909 +     for (iter = pkg_src_list->head; iter; iter = iter->next) {
2910 +          src = iter->data;
2911 +         if (src == NULL) {
2912 +              continue;
2913 +         }
2914 +
2915 +         sprintf_alloc(&list_file, "%s/%s", 
2916 +                         conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
2917 +                         src->name);
2918 +
2919 +         if (file_exists(list_file)) {
2920 +              pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
2921 +         }
2922 +         free(list_file);
2923 +     }
2924 +
2925 +     return 0;
2926 +}
2927 +
2928 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
2929 +{
2930 +     nv_pair_list_elt_t *iter;
2931 +     nv_pair_t *nv_pair;
2932 +     pkg_dest_t *dest;
2933 +     char *root_dir;
2934 +
2935 +     for (iter = nv_pair_list->head; iter; iter = iter->next) {
2936 +         nv_pair = iter->data;
2937 +
2938 +         if (conf->offline_root) {
2939 +              sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
2940 +         } else {
2941 +              root_dir = strdup(nv_pair->value);
2942 +         }
2943 +         dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
2944 +         free(root_dir);
2945 +         if (dest == NULL) {
2946 +              continue;
2947 +         }
2948 +         if (conf->default_dest == NULL) {
2949 +              conf->default_dest = dest;
2950 +         }
2951 +         if (file_exists(dest->status_file_name)) {
2952 +              pkg_hash_add_from_file(conf, dest->status_file_name,
2953 +                                     NULL, dest, 1);
2954 +         }
2955 +     }
2956 +
2957 +     return 0;
2958 +}
2959 +
2960 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2961 +                               pkg_src_list_t *pkg_src_list,
2962 +                               nv_pair_list_t *tmp_dest_nv_pair_list,
2963 +                               char **lists_dir)
2964 +{
2965 +     ipkg_option_t * options;
2966 +     FILE *file = fopen(filename, "r");
2967 +     regex_t valid_line_re, comment_re;
2968 +#define regmatch_size 12
2969 +     regmatch_t regmatch[regmatch_size];
2970 +
2971 +     if (ipkg_init_options_array(conf, &options)<0)
2972 +        return ENOMEM;
2973 +
2974 +     if (file == NULL) {
2975 +         fprintf(stderr, "%s: failed to open %s: %s\n",
2976 +                 __FUNCTION__, filename, strerror(errno));
2977 +         free(options);
2978 +         return errno;
2979 +     }
2980 +     ipkg_message(conf, IPKG_NOTICE, "loading conf file %s\n", filename);
2981 +
2982 +     xregcomp(&comment_re, 
2983 +                   "^[[:space:]]*(#.*|[[:space:]]*)$",
2984 +                   REG_EXTENDED);
2985 +     xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
2986 +
2987 +     while(1) {
2988 +         int line_num = 0;
2989 +         char *line;
2990 +         char *type, *name, *value, *extra;
2991 +
2992 +         line = file_read_line_alloc(file);
2993 +         line_num++;
2994 +         if (line == NULL) {
2995 +              break;
2996 +         }
2997 +
2998 +         str_chomp(line);
2999 +
3000 +         if (regexec(&comment_re, line, 0, 0, 0) == 0) {
3001 +              goto NEXT_LINE;
3002 +         }
3003 +
3004 +         if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
3005 +              str_chomp(line);
3006 +              fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
3007 +                      filename, line_num, line);
3008 +              goto NEXT_LINE;
3009 +         }
3010 +
3011 +         /* This has to be so ugly to deal with optional quotation marks */
3012 +         if (regmatch[2].rm_so > 0) {
3013 +              type = strndup(line + regmatch[2].rm_so,
3014 +                             regmatch[2].rm_eo - regmatch[2].rm_so);
3015 +         } else {
3016 +              type = strndup(line + regmatch[3].rm_so,
3017 +                             regmatch[3].rm_eo - regmatch[3].rm_so);
3018 +         }
3019 +         if (regmatch[5].rm_so > 0) {
3020 +              name = strndup(line + regmatch[5].rm_so,
3021 +                             regmatch[5].rm_eo - regmatch[5].rm_so);
3022 +         } else {
3023 +              name = strndup(line + regmatch[6].rm_so,
3024 +                             regmatch[6].rm_eo - regmatch[6].rm_so);
3025 +         }
3026 +         if (regmatch[8].rm_so > 0) {
3027 +              value = strndup(line + regmatch[8].rm_so,
3028 +                              regmatch[8].rm_eo - regmatch[8].rm_so);
3029 +         } else {
3030 +              value = strndup(line + regmatch[9].rm_so,
3031 +                              regmatch[9].rm_eo - regmatch[9].rm_so);
3032 +         }
3033 +         extra = NULL;
3034 +         if (regmatch[11].rm_so > 0) {
3035 +              extra = strndup (line + regmatch[11].rm_so,
3036 +                               regmatch[11].rm_eo - regmatch[11].rm_so);
3037 +         }
3038 +
3039 +         /* We use the tmp_dest_nv_pair_list below instead of
3040 +            conf->pkg_dest_list because we might encounter an
3041 +            offline_root option later and that would invalidate the
3042 +            directories we would have computed in
3043 +            pkg_dest_list_init. (We do a similar thing with
3044 +            tmp_src_nv_pair_list for sake of symmetry.) */
3045 +         if (strcmp(type, "option") == 0) {
3046 +              ipkg_conf_set_option(options, name, value);
3047 +         } else if (strcmp(type, "src") == 0) {
3048 +              if (!nv_pair_list_find(pkg_src_list, name)) {
3049 +                   pkg_src_list_append (pkg_src_list, name, value, extra, 0);
3050 +              } else {
3051 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
3052 +                                name, value);
3053 +              }
3054 +         } else if (strcmp(type, "src/gz") == 0) {
3055 +              if (!nv_pair_list_find(pkg_src_list, name)) {
3056 +                   pkg_src_list_append (pkg_src_list, name, value, extra, 1);
3057 +              } else {
3058 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
3059 +                                name, value);
3060 +              }
3061 +         } else if (strcmp(type, "dest") == 0) {
3062 +              nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
3063 +         } else if (strcmp(type, "lists_dir") == 0) {
3064 +              *lists_dir = realloc(*lists_dir,strlen(value)+1);
3065 +               if (*lists_dir == NULL) {
3066 +                   ipkg_message(conf, IPKG_ERROR, "ERROR: Not enough memory\n");
3067 +                   free(options);
3068 +                   return EINVAL;
3069 +               }
3070 +               sprintf (*lists_dir,"%s",value);
3071 +         } else if (strcmp(type, "arch") == 0) {
3072 +              ipkg_message(conf, IPKG_INFO, "supported arch %s priority (%s)\n", name, value);
3073 +              if (!value) {
3074 +                   ipkg_message(conf, IPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
3075 +                   value = strdup("10");
3076 +              }
3077 +              nv_pair_list_append(&conf->arch_list, strdup(name), strdup(value));
3078 +         } else {
3079 +              fprintf(stderr, "WARNING: Ignoring unknown configuration "
3080 +                      "parameter: %s %s %s\n", type, name, value);
3081 +              free(options);
3082 +              return EINVAL;
3083 +         }
3084 +
3085 +         free(type);
3086 +         free(name);
3087 +         free(value);
3088 +         if (extra)
3089 +              free (extra);
3090 +
3091 +     NEXT_LINE:
3092 +         free(line);
3093 +     }
3094 +
3095 +     free(options);
3096 +     regfree(&comment_re);
3097 +     regfree(&valid_line_re);
3098 +     fclose(file);
3099 +
3100 +     return 0;
3101 +}
3102 +
3103 +static int ipkg_conf_set_option(const ipkg_option_t *options,
3104 +                               const char *name, const char *value)
3105 +{
3106 +     int i = 0;
3107 +     while (options[i].name) {
3108 +         if (strcmp(options[i].name, name) == 0) {
3109 +              switch (options[i].type) {
3110 +              case IPKG_OPT_TYPE_BOOL:
3111 +                   *((int *)options[i].value) = 1;
3112 +                   return 0;
3113 +              case IPKG_OPT_TYPE_INT:
3114 +                   if (value) {
3115 +                        *((int *)options[i].value) = atoi(value);
3116 +                        return 0;
3117 +                   } else {
3118 +                        printf("%s: Option %s need an argument\n",
3119 +                               __FUNCTION__, name);
3120 +                        return EINVAL;
3121 +                   }               
3122 +              case IPKG_OPT_TYPE_STRING:
3123 +                   if (value) {
3124 +                        *((char **)options[i].value) = strdup(value);
3125 +                        return 0;
3126 +                   } else {
3127 +                        printf("%s: Option %s need an argument\n",
3128 +                               __FUNCTION__, name);
3129 +                        return EINVAL;
3130 +                   }
3131 +              }
3132 +         }
3133 +         i++;
3134 +     }
3135 +    
3136 +     fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
3137 +            __FUNCTION__, name, value);
3138 +     return EINVAL;
3139 +}
3140 +
3141 +int ipkg_conf_write_status_files(ipkg_conf_t *conf)
3142 +{
3143 +     pkg_dest_list_elt_t *iter;
3144 +     pkg_dest_t *dest;
3145 +     pkg_vec_t *all;
3146 +     pkg_t *pkg;
3147 +     register int i;
3148 +     int err;
3149 +
3150 +     if (conf->noaction)
3151 +         return 0;
3152 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3153 +         dest = iter->data;
3154 +         dest->status_file = fopen(dest->status_file_tmp_name, "w");
3155 +         if (dest->status_file == NULL) {
3156 +              fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
3157 +                      __FUNCTION__, dest->status_file_name, strerror(errno));
3158 +         }
3159 +     }
3160 +
3161 +     all = pkg_vec_alloc();
3162 +     pkg_hash_fetch_available(&conf->pkg_hash, all);
3163 +
3164 +     for(i = 0; i < all->len; i++) {
3165 +         pkg = all->pkgs[i];
3166 +         /* We don't need most uninstalled packages in the status file */
3167 +         if (pkg->state_status == SS_NOT_INSTALLED
3168 +             && (pkg->state_want == SW_UNKNOWN
3169 +                 || pkg->state_want == SW_DEINSTALL
3170 +                 || pkg->state_want == SW_PURGE)) {
3171 +              continue;
3172 +         }
3173 +         if (!pkg) {
3174 +           fprintf(stderr, "Null package\n");
3175 +         }
3176 +         if (pkg->dest == NULL) {
3177 +              fprintf(stderr, "%s: ERROR: Can't write status for "
3178 +                      "package %s since it has a NULL dest\n",
3179 +                      __FUNCTION__, pkg->name);
3180 +              continue;
3181 +         }
3182 +         if (pkg->dest->status_file) {
3183 +              pkg_print_status(pkg, pkg->dest->status_file);
3184 +         }
3185 +     }
3186 +
3187 +     pkg_vec_free(all);
3188 +
3189 +     for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3190 +         dest = iter->data;
3191 +         if (dest->status_file) {
3192 +              err = ferror(dest->status_file);
3193 +              fclose(dest->status_file);
3194 +              dest->status_file = NULL;
3195 +              if (!err) {
3196 +                   file_move(dest->status_file_tmp_name, dest->status_file_name);
3197 +              } else {
3198 +                   fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
3199 +                           "retaining old %s\n", __FUNCTION__, 
3200 +                           dest->status_file_tmp_name, dest->status_file_name);
3201 +              }
3202 +         }
3203 +     }
3204 +
3205 +     return 0;
3206 +}
3207 +
3208 +
3209 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename)
3210 +{
3211 +     char *root_filename;
3212 +     sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
3213 +     return root_filename;
3214 +}
3215 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_conf.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.h
3216 --- busybox-1.2.0-orig/archival/libipkg/ipkg_conf.h     1970-01-01 01:00:00.000000000 +0100
3217 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_conf.h    2006-07-22 16:31:25.000000000 +0200
3218 @@ -0,0 +1,107 @@
3219 +/* ipkg_conf.h - the itsy package management system
3220 +
3221 +   Carl D. Worth
3222 +
3223 +   Copyright (C) 2001 University of Southern California
3224 +
3225 +   This program is free software; you can redistribute it and/or
3226 +   modify it under the terms of the GNU General Public License as
3227 +   published by the Free Software Foundation; either version 2, or (at
3228 +   your option) any later version.
3229 +
3230 +   This program is distributed in the hope that it will be useful, but
3231 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3232 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3233 +   General Public License for more details.
3234 +*/
3235 +
3236 +#ifndef IPKG_CONF_H
3237 +#define IPKG_CONF_H
3238 +
3239 +typedef struct ipkg_conf ipkg_conf_t;
3240 +
3241 +#include "hash_table.h"
3242 +#include "ipkg.h"
3243 +#include "args.h"
3244 +#include "pkg.h"
3245 +#include "pkg_hash.h"
3246 +#include "pkg_src_list.h"
3247 +#include "pkg_dest_list.h"
3248 +#include "nv_pair_list.h"
3249 +
3250 +#define IPKG_CONF_DEFAULT_TMP_DIR_BASE "/tmp"
3251 +#define IPKG_CONF_TMP_DIR_SUFFIX "ipkg-XXXXXX"
3252 +#define IPKG_CONF_LISTS_DIR  IPKG_STATE_DIR_PREFIX "/lists"
3253 +#define IPKG_CONF_PENDING_DIR IPKG_STATE_DIR_PREFIX "/pending"
3254 +
3255 +/* In case the config file defines no dest */
3256 +#define IPKG_CONF_DEFAULT_DEST_NAME "root"
3257 +#define IPKG_CONF_DEFAULT_DEST_ROOT_DIR "/"
3258 +
3259 +#define IPKG_CONF_DEFAULT_HASH_LEN 1024
3260 +
3261 +struct ipkg_conf
3262 +{
3263 +     pkg_src_list_t pkg_src_list;
3264 +     pkg_dest_list_t pkg_dest_list;
3265 +     nv_pair_list_t arch_list;
3266 +
3267 +     int restrict_to_default_dest;
3268 +     pkg_dest_t *default_dest;
3269 +
3270 +     char *tmp_dir;
3271 +     const char *lists_dir;
3272 +     const char *pending_dir;
3273 +
3274 +     /* options */
3275 +     int force_depends;
3276 +     int force_defaults;
3277 +     int force_overwrite;
3278 +     int force_downgrade;
3279 +     int force_reinstall;
3280 +     int force_space;
3281 +     int force_removal_of_dependent_packages;
3282 +     int force_removal_of_essential_packages;
3283 +     int nodeps; /* do not follow dependences */
3284 +     int verbose_wget;
3285 +     int multiple_providers;
3286 +     char *offline_root;
3287 +     char *offline_root_pre_script_cmd;
3288 +     char *offline_root_post_script_cmd;
3289 +     int query_all;
3290 +     int verbosity;
3291 +     int noaction;
3292 +
3293 +     /* proxy options */
3294 +     char *http_proxy;
3295 +     char *ftp_proxy;
3296 +     char *no_proxy;
3297 +     char *proxy_user;
3298 +     char *proxy_passwd;
3299 +
3300 +     hash_table_t pkg_hash;
3301 +     hash_table_t file_hash;
3302 +     hash_table_t obs_file_hash;
3303 +};
3304 +
3305 +enum ipkg_option_type {
3306 +     IPKG_OPT_TYPE_BOOL,
3307 +     IPKG_OPT_TYPE_INT,
3308 +     IPKG_OPT_TYPE_STRING
3309 +};
3310 +typedef enum ipkg_option_type ipkg_option_type_t;
3311 +
3312 +typedef struct ipkg_option ipkg_option_t;
3313 +struct ipkg_option {
3314 +     const char *name;
3315 +     const ipkg_option_type_t type;
3316 +     const void *value;
3317 +};
3318 +
3319 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args);
3320 +void ipkg_conf_deinit(ipkg_conf_t *conf);
3321 +
3322 +int ipkg_conf_write_status_files(ipkg_conf_t *conf);
3323 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename);
3324 +
3325 +#endif
3326 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_configure.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.c
3327 --- busybox-1.2.0-orig/archival/libipkg/ipkg_configure.c        1970-01-01 01:00:00.000000000 +0100
3328 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.c       2006-07-22 16:31:25.000000000 +0200
3329 @@ -0,0 +1,40 @@
3330 +/* ipkg_configure.c - the itsy package management system
3331 +
3332 +   Carl D. Worth
3333 +
3334 +   Copyright (C) 2001 University of Southern California
3335 +
3336 +   This program is free software; you can redistribute it and/or
3337 +   modify it under the terms of the GNU General Public License as
3338 +   published by the Free Software Foundation; either version 2, or (at
3339 +   your option) any later version.
3340 +
3341 +   This program is distributed in the hope that it will be useful, but
3342 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3343 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3344 +   General Public License for more details.
3345 +*/
3346 +
3347 +#include "ipkg.h"
3348 +
3349 +#include "ipkg_configure.h"
3350 +
3351 +int ipkg_configure(ipkg_conf_t *conf, pkg_t *pkg)
3352 +{
3353 +    int err;
3354 +
3355 +    /* DPKG_INCOMPATIBILITY:
3356 +       dpkg actually does some conffile handling here, rather than at the
3357 +       end of ipkg_install(). Do we care? */
3358 +    /* DPKG_INCOMPATIBILITY:
3359 +       dpkg actually includes a version number to this script call */
3360 +    err = pkg_run_script(conf, pkg, "postinst", "configure");
3361 +    if (err) {
3362 +       printf("ERROR: %s.postinst returned %d\n", pkg->name, err);
3363 +       return err;
3364 +    }
3365 +
3366 +    ipkg_state_changed++;
3367 +    return 0;
3368 +}
3369 +
3370 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_configure.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.h
3371 --- busybox-1.2.0-orig/archival/libipkg/ipkg_configure.h        1970-01-01 01:00:00.000000000 +0100
3372 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_configure.h       2006-07-22 16:31:25.000000000 +0200
3373 @@ -0,0 +1,25 @@
3374 +/* ipkg_configure.h - the itsy package management system
3375 +
3376 +   Carl D. Worth
3377 +
3378 +   Copyright (C) 2001 University of Southern California
3379 +
3380 +   This program is free software; you can redistribute it and/or
3381 +   modify it under the terms of the GNU General Public License as
3382 +   published by the Free Software Foundation; either version 2, or (at
3383 +   your option) any later version.
3384 +
3385 +   This program is distributed in the hope that it will be useful, but
3386 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3387 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3388 +   General Public License for more details.
3389 +*/
3390 +
3391 +#ifndef IPKG_CONFIGURE_H
3392 +#define IPKG_CONFIGURE_H
3393 +
3394 +#include "ipkg_conf.h"
3395 +
3396 +int ipkg_configure(ipkg_conf_t *ipkg_conf, pkg_t *pkg);
3397 +
3398 +#endif
3399 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_download.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.c
3400 --- busybox-1.2.0-orig/archival/libipkg/ipkg_download.c 1970-01-01 01:00:00.000000000 +0100
3401 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.c        2006-07-22 16:31:25.000000000 +0200
3402 @@ -0,0 +1,195 @@
3403 +/* ipkg_download.c - the itsy package management system
3404 +
3405 +   Carl D. Worth
3406 +
3407 +   Copyright (C) 2001 University of Southern California
3408 +
3409 +   This program is free software; you can redistribute it and/or
3410 +   modify it under the terms of the GNU General Public License as
3411 +   published by the Free Software Foundation; either version 2, or (at
3412 +   your option) any later version.
3413 +
3414 +   This program is distributed in the hope that it will be useful, but
3415 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3416 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3417 +   General Public License for more details.
3418 +*/
3419 +
3420 +#include "ipkg.h"
3421 +#include "ipkg_download.h"
3422 +#include "ipkg_message.h"
3423 +
3424 +#include "sprintf_alloc.h"
3425 +#include "xsystem.h"
3426 +#include "file_util.h"
3427 +#include "str_util.h"
3428 +
3429 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
3430 +{
3431 +    int err = 0;
3432 +
3433 +    char *src_basec = strdup(src);
3434 +    char *src_base = basename(src_basec);
3435 +    char *tmp_file_location;
3436 +    char *cmd;
3437 +
3438 +    ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
3439 +       
3440 +    fflush(stdout);
3441 +    
3442 +    if (str_starts_with(src, "file:")) {
3443 +       int ret;
3444 +       const char *file_src = src + 5;
3445 +       ipkg_message(conf,IPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
3446 +       ret = file_copy(src + 5, dest_file_name);
3447 +       ipkg_message(conf,IPKG_INFO,"Done.\n");
3448 +       return ret;
3449 +    }
3450 +
3451 +    sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
3452 +    err = unlink(tmp_file_location);
3453 +    if (err && errno != ENOENT) {
3454 +       ipkg_message(conf,IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
3455 +               __FUNCTION__, tmp_file_location, strerror(errno));
3456 +       free(tmp_file_location);
3457 +       return errno;
3458 +    }
3459 +
3460 +    if (conf->http_proxy) {
3461 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
3462 +       setenv("http_proxy", conf->http_proxy, 1);
3463 +    }
3464 +    if (conf->ftp_proxy) {
3465 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
3466 +       setenv("ftp_proxy", conf->ftp_proxy, 1);
3467 +    }
3468 +    if (conf->no_proxy) {
3469 +       ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
3470 +       setenv("no_proxy", conf->no_proxy, 1);
3471 +    }
3472 +
3473 +    /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */ 
3474 +    sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
3475 +                 (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
3476 +                 conf->proxy_user ? "--proxy-user=" : "",
3477 +                 conf->proxy_user ? conf->proxy_user : "",
3478 +                 conf->proxy_passwd ? "--proxy-passwd=" : "",
3479 +                 conf->proxy_passwd ? conf->proxy_passwd : "",
3480 +                 conf->verbose_wget ? "" : "-q",
3481 +                 conf->tmp_dir,
3482 +                 src);
3483 +    err = xsystem(cmd);
3484 +    if (err) {
3485 +       if (err != -1) {
3486 +           ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
3487 +                   __FUNCTION__, err, cmd);
3488 +       } 
3489 +       unlink(tmp_file_location);
3490 +       free(tmp_file_location);
3491 +       free(src_basec);
3492 +       free(cmd);
3493 +       return EINVAL;
3494 +    }
3495 +    free(cmd);
3496 +
3497 +    err = file_move(tmp_file_location, dest_file_name);
3498 +
3499 +    free(tmp_file_location);
3500 +    free(src_basec);
3501 +
3502 +    if (err) {
3503 +       return err;
3504 +    }
3505 +
3506 +    return 0;
3507 +}
3508 +
3509 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
3510 +{
3511 +    int err;
3512 +    char *url;
3513 +
3514 +    if (pkg->src == NULL) {
3515 +       ipkg_message(conf,IPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
3516 +               pkg->name, pkg->parent->name);
3517 +       return -1;
3518 +    }
3519 +
3520 +    sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
3521 +
3522 +    /* XXX: BUG: The pkg->filename might be something like
3523 +       "../../foo.ipk". While this is correct, and exactly what we
3524 +       want to use to construct url above, here we actually need to
3525 +       use just the filename part, without any directory. */
3526 +    sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
3527 +
3528 +    err = ipkg_download(conf, url, pkg->local_filename);
3529 +    free(url);
3530 +
3531 +    return err;
3532 +}
3533 +
3534 +/*
3535 + * Downloads file from url, installs in package database, return package name. 
3536 + */
3537 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
3538 +{
3539 +     int err = 0;
3540 +     pkg_t *pkg;
3541 +     pkg = pkg_new();
3542 +     if (pkg == NULL)
3543 +         return ENOMEM;
3544 +
3545 +     if (str_starts_with(url, "http://")
3546 +        || str_starts_with(url, "ftp://")) {
3547 +         char *tmp_file;
3548 +         char *file_basec = strdup(url);
3549 +         char *file_base = basename(file_basec);
3550 +
3551 +         sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
3552 +         err = ipkg_download(conf, url, tmp_file);
3553 +         if (err)
3554 +              return err;
3555 +
3556 +         err = pkg_init_from_file(pkg, tmp_file);
3557 +         if (err)
3558 +              return err;
3559 +         pkg->local_filename = strdup(tmp_file);
3560 +
3561 +         free(tmp_file);
3562 +         free(file_basec);
3563 +
3564 +     } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
3565 +               || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
3566 +
3567 +         err = pkg_init_from_file(pkg, url);
3568 +         if (err)
3569 +              return err;
3570 +         pkg->local_filename = strdup(url);
3571 +         ipkg_message(conf, IPKG_DEBUG2, "Package %s provided by hand \(%s\).\n", pkg->name,pkg->local_filename);
3572 +          pkg->provided_by_hand = 1;
3573 +
3574 +     } else {
3575 +       pkg_deinit(pkg);
3576 +       free(pkg);
3577 +       return 0;
3578 +     }
3579 +
3580 +     if (!pkg->architecture) {
3581 +         ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3582 +         return -EINVAL;
3583 +     }
3584 +
3585 +     pkg->dest = conf->default_dest;
3586 +     pkg->state_want = SW_INSTALL;
3587 +     pkg->state_flag |= SF_PREFER;
3588 +     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);  
3589 +     if ( pkg == NULL ){
3590 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
3591 +        return 0;
3592 +     }
3593 +     if (namep) {
3594 +         *namep = strdup(pkg->name);
3595 +     }
3596 +     return 0;
3597 +}
3598 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_download.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.h
3599 --- busybox-1.2.0-orig/archival/libipkg/ipkg_download.h 1970-01-01 01:00:00.000000000 +0100
3600 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_download.h        2006-07-22 16:31:25.000000000 +0200
3601 @@ -0,0 +1,30 @@
3602 +/* ipkg_download.h - the itsy package management system
3603 +
3604 +   Carl D. Worth
3605 +
3606 +   Copyright (C) 2001 University of Southern California
3607 +
3608 +   This program is free software; you can redistribute it and/or
3609 +   modify it under the terms of the GNU General Public License as
3610 +   published by the Free Software Foundation; either version 2, or (at
3611 +   your option) any later version.
3612 +
3613 +   This program is distributed in the hope that it will be useful, but
3614 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3615 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3616 +   General Public License for more details.
3617 +*/
3618 +
3619 +#ifndef IPKG_DOWNLOAD_H
3620 +#define IPKG_DOWNLOAD_H
3621 +
3622 +#include "ipkg_conf.h"
3623 +
3624 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name);
3625 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir);
3626 +/*
3627 + * Downloads file from url, installs in package database, return package name. 
3628 + */
3629 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep);
3630 +
3631 +#endif
3632 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg.h
3633 --- busybox-1.2.0-orig/archival/libipkg/ipkg.h  1970-01-01 01:00:00.000000000 +0100
3634 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg.h 2006-07-22 16:31:25.000000000 +0200
3635 @@ -0,0 +1,74 @@
3636 +/* ipkg.h - the itsy package management system
3637 +
3638 +   Carl D. Worth
3639 +
3640 +   Copyright (C) 2001 University of Southern California
3641 +
3642 +   This program is free software; you can redistribute it and/or
3643 +   modify it under the terms of the GNU General Public License as
3644 +   published by the Free Software Foundation; either version 2, or (at
3645 +   your option) any later version.
3646 +
3647 +   This program is distributed in the hope that it will be useful, but
3648 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3649 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3650 +   General Public License for more details.
3651 +*/
3652 +
3653 +#ifndef IPKG_H
3654 +#define IPKG_H
3655 +
3656 +/*
3657 +#ifdef HAVE_CONFIG_H
3658 +#include "config.h"
3659 +#endif
3660 +*/
3661 +
3662 +#if 0
3663 +#define IPKG_DEBUG_NO_TMP_CLEANUP
3664 +#endif
3665 +
3666 +#include "ipkg_includes.h"
3667 +#include "ipkg_conf.h"
3668 +#include "ipkg_message.h"
3669 +
3670 +#define IPKG_PKG_EXTENSION ".ipk"
3671 +#define DPKG_PKG_EXTENSION ".deb"
3672 +
3673 +#define IPKG_LEGAL_PKG_NAME_CHARS "abcdefghijklmnopqrstuvwxyz0123456789.+-"
3674 +#define IPKG_PKG_VERSION_SEP_CHAR '_'
3675 +
3676 +#define IPKG_STATE_DIR_PREFIX IPKGLIBDIR"/ipkg"
3677 +#define IPKG_LISTS_DIR_SUFFIX "lists"
3678 +#define IPKG_INFO_DIR_SUFFIX "info"
3679 +#define IPKG_STATUS_FILE_SUFFIX "status"
3680 +
3681 +#define IPKG_BACKUP_SUFFIX "-ipkg.backup"
3682 +
3683 +#define IPKG_LIST_DESCRIPTION_LENGTH 128
3684 +
3685 +#define IPKG_VERSION "0.99.162"
3686 +
3687 +
3688 +enum ipkg_error {
3689 +    IPKG_SUCCESS = 0,
3690 +    IPKG_PKG_DEPS_UNSATISFIED,
3691 +    IPKG_PKG_IS_ESSENTIAL,
3692 +    IPKG_PKG_HAS_DEPENDENTS,
3693 +    IPKG_PKG_HAS_NO_CANDIDATE
3694 +};
3695 +typedef enum ipkg_error ipkg_error_t;
3696 +
3697 +extern int ipkg_state_changed;
3698 +
3699 +
3700 +struct errlist {
3701 +    char * errmsg;
3702 +    struct errlist * next;
3703 +} ;
3704 +
3705 +extern struct errlist* error_list;
3706 +
3707 +extern ipkg_conf_t *global_conf;
3708 +
3709 +#endif
3710 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_includes.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_includes.h
3711 --- busybox-1.2.0-orig/archival/libipkg/ipkg_includes.h 1970-01-01 01:00:00.000000000 +0100
3712 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_includes.h        2006-07-22 16:31:25.000000000 +0200
3713 @@ -0,0 +1,79 @@
3714 +#ifndef IPKG_INCLUDES_H
3715 +#define IPKG_INCLUDES_H
3716 +
3717 +/* Define to 1 if you have the <memory.h> header file. */
3718 +#define HAVE_MEMORY_H 1
3719 +
3720 +/* Define to 1 if you have the <regex.h> header file. */
3721 +#define HAVE_REGEX_H 1
3722 +
3723 +/* Define to 1 if you have the <stdlib.h> header file. */
3724 +#define HAVE_STDLIB_H 1
3725 +
3726 +/* Define to 1 if you have the <strings.h> header file. */
3727 +#define HAVE_STRINGS_H 1
3728 +
3729 +/* Define to 1 if you have the <string.h> header file. */
3730 +#define HAVE_STRING_H 1
3731 +
3732 +/* Define to 1 if you have the <sys/stat.h> header file. */
3733 +#define HAVE_SYS_STAT_H 1
3734 +
3735 +/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
3736 +#define HAVE_SYS_WAIT_H 1
3737 +
3738 +/* Define to 1 if you have the <unistd.h> header file. */
3739 +#define HAVE_UNISTD_H 1
3740 +
3741 +/* Define to 1 if you have the ANSI C header files. */
3742 +#define STDC_HEADERS 1
3743 +
3744 +
3745 +#include <stdio.h>
3746 +
3747 +#if STDC_HEADERS
3748 +# include <stdlib.h>
3749 +# include <stdarg.h>
3750 +# include <stddef.h>
3751 +# include <ctype.h>
3752 +# include <errno.h>
3753 +#else
3754 +# if HAVE_STDLIB_H
3755 +#  include <stdlib.h>
3756 +# endif
3757 +#endif
3758 +
3759 +#if HAVE_REGEX_H
3760 +# include <regex.h>
3761 +#endif
3762 +
3763 +#if HAVE_STRING_H
3764 +# if !STDC_HEADERS && HAVE_MEMORY_H
3765 +#  include <memory.h>
3766 +# endif
3767 +/* XXX: What's the right way to pick up GNU's strndup declaration? */
3768 +# if __GNUC__
3769 +#   define __USE_GNU 1
3770 +# endif
3771 +# include <string.h>
3772 +# undef __USE_GNU
3773 +#endif
3774 +
3775 +#if HAVE_STRINGS_H
3776 +# include <strings.h>
3777 +#endif
3778 +
3779 +#if HAVE_SYS_STAT_H
3780 +# include <sys/stat.h>
3781 +#endif
3782 +
3783 +#if HAVE_SYS_WAIT_H
3784 +# include <sys/wait.h>
3785 +#endif
3786 +
3787 +#if HAVE_UNISTD_H
3788 +# include <sys/types.h>
3789 +# include <unistd.h>
3790 +#endif
3791 +
3792 +#endif /* IPKG_INCLUDES_H */
3793 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_install.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.c
3794 --- busybox-1.2.0-orig/archival/libipkg/ipkg_install.c  1970-01-01 01:00:00.000000000 +0100
3795 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.c 2006-07-22 16:31:25.000000000 +0200
3796 @@ -0,0 +1,1942 @@
3797 +/* ipkg_install.c - the itsy package management system
3798 +
3799 +   Carl D. Worth
3800 +
3801 +   Copyright (C) 2001 University of Southern California
3802 +
3803 +   This program is free software; you can redistribute it and/or
3804 +   modify it under the terms of the GNU General Public License as
3805 +   published by the Free Software Foundation; either version 2, or (at
3806 +   your option) any later version.
3807 +
3808 +   This program is distributed in the hope that it will be useful, but
3809 +   WITHOUT ANY WARRANTY; without even the implied warranty of
3810 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
3811 +   General Public License for more details.
3812 +*/
3813 +
3814 +#include "ipkg.h"
3815 +#include <errno.h>
3816 +#include <dirent.h>
3817 +#include <glob.h>
3818 +#include <time.h>
3819 +#include <signal.h>
3820 +typedef void (*sighandler_t)(int);
3821 +
3822 +#include "pkg.h"
3823 +#include "pkg_hash.h"
3824 +#include "pkg_extract.h"
3825 +
3826 +#include "ipkg_install.h"
3827 +#include "ipkg_configure.h"
3828 +#include "ipkg_download.h"
3829 +#include "ipkg_remove.h"
3830 +
3831 +#include "ipkg_utils.h"
3832 +#include "ipkg_message.h"
3833 +
3834 +#include "sprintf_alloc.h"
3835 +#include "file_util.h"
3836 +#include "str_util.h"
3837 +#include "xsystem.h"
3838 +#include "user.h"
3839 +
3840 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
3841 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg);
3842 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg);
3843 +
3844 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3845 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3846 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3847 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3848 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3849 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3850 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3851 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3852 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3853 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3854 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3855 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3856 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3857 +
3858 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3859 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3860 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg);
3861 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg);
3862 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg);
3863 +
3864 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg);
3865 +
3866 +static int user_prefers_old_conffile(const char *file, const char *backup);
3867 +
3868 +static char *backup_filename_alloc(const char *file_name);
3869 +static int backup_make_backup(ipkg_conf_t *conf, const char *file_name);
3870 +static int backup_exists_for(const char *file_name);
3871 +static int backup_remove(const char *file_name);
3872 +
3873 +
3874 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename)
3875 +{
3876 +     int err, cmp;
3877 +     pkg_t *pkg, *old;
3878 +     char *old_version, *new_version;
3879 +
3880 +     pkg = pkg_new();
3881 +     if (pkg == NULL) {
3882 +         return ENOMEM;
3883 +     }
3884 +
3885 +     err = pkg_init_from_file(pkg, filename);
3886 +     if (err) {
3887 +         return err;
3888 +     }
3889 +
3890 +     if (!pkg->architecture) {
3891 +         ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3892 +         return -EINVAL;
3893 +     }
3894 +
3895 +     /* XXX: CLEANUP: hash_insert_pkg has a nasty side effect of possibly
3896 +       freeing the pkg that we pass in. It might be nice to clean this up
3897 +       if possible.  */
3898 +     pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
3899 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
3900 +
3901 +     pkg->local_filename = strdup(filename);
3902 +
3903 +     if (old) {
3904 +         old_version = pkg_version_str_alloc(old);
3905 +         new_version = pkg_version_str_alloc(pkg);
3906 +
3907 +         cmp = pkg_compare_versions(old, pkg);
3908 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
3909 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
3910 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
3911 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
3912 +          } 
3913 +         if (cmp > 0) {
3914 +                ipkg_message(conf, IPKG_NOTICE,
3915 +                             "Not downgrading package %s on %s from %s to %s.\n",
3916 +                             old->name, old->dest->name, old_version, new_version);
3917 +                pkg->state_want = SW_DEINSTALL;
3918 +                pkg->state_flag |= SF_OBSOLETE;
3919 +                free(old_version);
3920 +                free(new_version);
3921 +                return 0;
3922 +         } else {
3923 +              free(old_version);
3924 +              free(new_version);
3925 +         }
3926 +     }
3927 +
3928 +     ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
3929 +     return ipkg_install_pkg(conf, pkg,0);
3930 +}
3931 +
3932 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name)
3933 +{
3934 +     int cmp;
3935 +     pkg_t *old, *new;
3936 +     char *old_version, *new_version;
3937 +
3938 +     ipkg_message(conf, IPKG_DEBUG2, " Getting old  from pkg_hash_fetch \n" );
3939 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
3940 +     if ( old ) 
3941 +        ipkg_message(conf, IPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n",  old->version );
3942 +    
3943 +     ipkg_message(conf, IPKG_DEBUG2, " Getting new  from pkg_hash_fetch \n" );
3944 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
3945 +     if ( new ) 
3946 +        ipkg_message(conf, IPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n",  new->version );
3947 +
3948 +/* Pigi Basically here is broken the version stuff.
3949 +   What's happening is that nothing provide the version to differents 
3950 +   functions, so the returned struct is always the latest.
3951 +   That's why the install by name don't work.
3952 +*/
3953 +     ipkg_message(conf, IPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
3954 +
3955 +     if ( old ) 
3956 +        ipkg_message(conf, IPKG_DEBUG2, " old %s ", old->version );
3957 +     if ( new ) 
3958 +        ipkg_message(conf, IPKG_DEBUG2, " new %s ", new->version );
3959 +     ipkg_message(conf, IPKG_DEBUG2, " \n");
3960 +
3961 +     if (new == NULL) {
3962 +         return IPKG_PKG_HAS_NO_CANDIDATE;
3963 +     }
3964 +
3965 +     new->state_flag |= SF_USER;
3966 +     if (old) {
3967 +         old_version = pkg_version_str_alloc(old);
3968 +         new_version = pkg_version_str_alloc(new);
3969 +
3970 +         cmp = pkg_compare_versions(old, new);
3971 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
3972 +            ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade \n");
3973 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
3974 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
3975 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
3976 +          } 
3977 +         ipkg_message(conf, IPKG_DEBUG, 
3978 +                      "Comparing visible versions of pkg %s:"
3979 +                      "\n\t%s is installed "
3980 +                      "\n\t%s is available "
3981 +                      "\n\t%d was comparison result\n",
3982 +                      pkg_name, old_version, new_version, cmp);
3983 +         if (cmp == 0 && !conf->force_reinstall) {
3984 +              ipkg_message(conf, IPKG_NOTICE,
3985 +                           "Package %s (%s) installed in %s is up to date.\n",
3986 +                           old->name, old_version, old->dest->name);
3987 +              free(old_version);
3988 +              free(new_version);
3989 +              return 0;
3990 +         } else if (cmp > 0) {
3991 +              ipkg_message(conf, IPKG_NOTICE,
3992 +                           "Not downgrading package %s on %s from %s to %s.\n",
3993 +                           old->name, old->dest->name, old_version, new_version);
3994 +              free(old_version);
3995 +              free(new_version);
3996 +              return 0;
3997 +         } else if (cmp < 0) {
3998 +              new->dest = old->dest;
3999 +              old->state_want = SW_DEINSTALL;    /* Here probably the problem for bug 1277 */
4000 +         }
4001 +     }
4002 +
4003 +     /* XXX: CLEANUP: The error code of ipkg_install_by_name is really
4004 +       supposed to be an ipkg_error_t, but ipkg_install_pkg could
4005 +       return any kind of integer, (might be errno from a syscall,
4006 +       etc.). This is a real mess and will need to be cleaned up if
4007 +       anyone ever wants to make a nice libipkg. */
4008 +
4009 +     ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4010 +     return ipkg_install_pkg(conf, new,0);
4011 +}
4012 +
4013 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name)
4014 +{
4015 +     abstract_pkg_vec_t *providers = pkg_hash_fetch_all_installation_candidates (&conf->pkg_hash, pkg_name);
4016 +     int i;
4017 +     ipkg_error_t err;
4018 +     abstract_pkg_t *ppkg ;
4019 +
4020 +     if (providers == NULL)
4021 +         return IPKG_PKG_HAS_NO_CANDIDATE;
4022 +
4023 +     for (i = 0; i < providers->len; i++) {
4024 +         ppkg = abstract_pkg_vec_get(providers, i);
4025 +          ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_by_name %d \n",__FUNCTION__, i);
4026 +         err = ipkg_install_by_name(conf, ppkg->name);
4027 +         if (err)
4028 +              return err;
4029 +/* XXX Maybe ppkg should be freed ? */
4030 +     }
4031 +     return 0;
4032 +}
4033 +
4034 +/*
4035 + * Walk dependence graph starting with pkg, collect packages to be
4036 + * installed into pkgs_needed, in dependence order.
4037 + */
4038 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *pkgs_needed)
4039 +{
4040 +     int i, err;
4041 +     pkg_vec_t *depends = pkg_vec_alloc();
4042 +     char **unresolved = NULL;
4043 +     int ndepends;
4044 +
4045 +     ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
4046 +                                                       pkg, depends, 
4047 +                                                       &unresolved);
4048 +
4049 +     if (unresolved) {
4050 +         ipkg_message(conf, IPKG_ERROR,
4051 +                      "%s: Cannot satisfy the following dependencies for %s:\n\t",
4052 +                      conf->force_depends ? "Warning" : "ERROR", pkg->name);
4053 +         while (*unresolved) {
4054 +              ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4055 +              unresolved++;
4056 +         }
4057 +         ipkg_message(conf, IPKG_ERROR, "\n");
4058 +         if (! conf->force_depends) {
4059 +              ipkg_message(conf, IPKG_INFO,
4060 +                           "This could mean that your package list is out of date or that the packages\n"
4061 +                           "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4062 +                           "of this problem try again with the '-force-depends' option.\n");
4063 +              pkg_vec_free(depends);
4064 +              return IPKG_PKG_DEPS_UNSATISFIED;
4065 +         }
4066 +     }
4067 +
4068 +     if (ndepends <= 0) {
4069 +         pkg_vec_free(depends);
4070 +         return 0;
4071 +     }
4072 +
4073 +     for (i = 0; i < depends->len; i++) {
4074 +         pkg_t *dep = depends->pkgs[i];
4075 +         /* The package was uninstalled when we started, but another
4076 +            dep earlier in this loop may have depended on it and pulled
4077 +            it in, so check first. */
4078 +         if ((dep->state_status != SS_INSTALLED)
4079 +             && (dep->state_status != SS_UNPACKED)
4080 +             && (dep->state_want != SW_INSTALL)) {
4081 +
4082 +              /* Mark packages as to-be-installed */
4083 +              dep->state_want = SW_INSTALL;
4084 +
4085 +              /* Dependencies should be installed the same place as pkg */
4086 +              if (dep->dest == NULL) {
4087 +                   dep->dest = pkg->dest;
4088 +              }
4089 +
4090 +              err = pkg_mark_dependencies_for_installation(conf, dep, pkgs_needed);
4091 +              if (err) {
4092 +                   pkg_vec_free(depends);
4093 +                   return err;
4094 +              }
4095 +         }
4096 +     }
4097 +     if (pkgs_needed)
4098 +         pkg_vec_insert(pkgs_needed, pkg);
4099 +
4100 +     pkg_vec_free(depends);
4101 +
4102 +     return 0;
4103 +}
4104 +
4105 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed)
4106 +{
4107 +     int cmp;
4108 +     pkg_t *old, *new;
4109 +     char *old_version, *new_version;
4110 +
4111 +     old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
4112 +    
4113 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
4114 +     if (new == NULL) {
4115 +         return IPKG_PKG_HAS_NO_CANDIDATE;
4116 +     }
4117 +     if (old) {
4118 +         old_version = pkg_version_str_alloc(old);
4119 +         new_version = pkg_version_str_alloc(new);
4120 +
4121 +         cmp = pkg_compare_versions(old, new);
4122 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
4123 +           ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade ");
4124 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
4125 +                                                              /* We need to use a value < 0 because in the 0 case we are asking to */
4126 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
4127 +          } 
4128 +         ipkg_message(conf, IPKG_DEBUG, 
4129 +                      "comparing visible versions of pkg %s:"
4130 +                      "\n\t%s is installed "
4131 +                      "\n\t%s is available "
4132 +                      "\n\t%d was comparison result\n",
4133 +                      pkg_name, old_version, new_version, cmp);
4134 +         if (cmp == 0 && !conf->force_reinstall) {
4135 +              ipkg_message(conf, IPKG_NOTICE,
4136 +                           "Package %s (%s) installed in %s is up to date.\n",
4137 +                           old->name, old_version, old->dest->name);
4138 +              free(old_version);
4139 +              free(new_version);
4140 +              return 0;
4141 +         } else if (cmp > 0) {
4142 +              ipkg_message(conf, IPKG_NOTICE,
4143 +                           "Not downgrading package %s on %s from %s to %s.\n",
4144 +                           old->name, old->dest->name, old_version, new_version);
4145 +              free(old_version);
4146 +              free(new_version);
4147 +              return 0;
4148 +         } else if (cmp < 0) {
4149 +              new->dest = old->dest;
4150 +              old->state_want = SW_DEINSTALL;
4151 +              old->state_flag |= SF_OBSOLETE;
4152 +         }
4153 +     }
4154 +     return pkg_mark_dependencies_for_installation(conf, new, pkgs_needed);
4155 +}
4156 +
4157 +\f
4158 +
4159 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg)
4160 +{
4161 +     int i, err;
4162 +     pkg_vec_t *depends = pkg_vec_alloc();
4163 +     pkg_t *dep;
4164 +     char **unresolved = NULL;
4165 +     int ndepends;
4166 +
4167 +     ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf, 
4168 +                                                       pkg, depends, 
4169 +                                                       &unresolved);
4170 +
4171 +     if (unresolved) {
4172 +         ipkg_message(conf, IPKG_ERROR,
4173 +                      "%s: Cannot satisfy the following dependencies for %s:\n\t",
4174 +                      conf->force_depends ? "Warning" : "ERROR", pkg->name);
4175 +         while (*unresolved) {
4176 +              ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4177 +              unresolved++;
4178 +         }
4179 +         ipkg_message(conf, IPKG_ERROR, "\n");
4180 +         if (! conf->force_depends) {
4181 +              ipkg_message(conf, IPKG_INFO,
4182 +                           "This could mean that your package list is out of date or that the packages\n"
4183 +                           "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4184 +                           "of this problem try again with the '-force-depends' option.\n");
4185 +              pkg_vec_free(depends);
4186 +              return IPKG_PKG_DEPS_UNSATISFIED;
4187 +         }
4188 +     }
4189 +
4190 +     if (ndepends <= 0) {
4191 +         return 0;
4192 +     }
4193 +
4194 +     /* Mark packages as to-be-installed */
4195 +     for (i=0; i < depends->len; i++) {
4196 +         /* Dependencies should be installed the same place as pkg */
4197 +         if (depends->pkgs[i]->dest == NULL) {
4198 +              depends->pkgs[i]->dest = pkg->dest;
4199 +         }
4200 +         depends->pkgs[i]->state_want = SW_INSTALL;
4201 +     }
4202 +
4203 +     for (i = 0; i < depends->len; i++) {
4204 +         dep = depends->pkgs[i];
4205 +         /* The package was uninstalled when we started, but another
4206 +            dep earlier in this loop may have depended on it and pulled
4207 +            it in, so check first. */
4208 +         if ((dep->state_status != SS_INSTALLED)
4209 +             && (dep->state_status != SS_UNPACKED)) {
4210 +               ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4211 +              err = ipkg_install_pkg(conf, dep,0);
4212 +              if (err) {
4213 +                   pkg_vec_free(depends);
4214 +                   return err;
4215 +              }
4216 +         }
4217 +     }
4218 +
4219 +     pkg_vec_free(depends);
4220 +
4221 +     return 0;
4222 +}
4223 +
4224 +
4225 +/* check all packages have their dependences satisfied, e.g., in case an upgraded package split */ 
4226 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf)
4227 +{
4228 +     if (conf->nodeps == 0) {
4229 +         int i;
4230 +         pkg_vec_t *installed = pkg_vec_alloc();
4231 +         pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
4232 +         for (i = 0; i < installed->len; i++) {
4233 +              pkg_t *pkg = installed->pkgs[i];
4234 +              satisfy_dependencies_for(conf, pkg);
4235 +         }
4236 +         pkg_vec_free(installed);
4237 +     }
4238 +     return 0;
4239 +}
4240 +
4241 +\f
4242 +
4243 +static int check_conflicts_for(ipkg_conf_t *conf, pkg_t *pkg)
4244 +{
4245 +     int i;
4246 +     pkg_vec_t *conflicts = NULL;
4247 +     int level;
4248 +     const char *prefix;
4249 +     if (conf->force_depends) {
4250 +         level = IPKG_NOTICE;
4251 +         prefix = "Warning";
4252 +     } else {
4253 +         level = IPKG_ERROR;
4254 +         prefix = "ERROR";
4255 +     }
4256 +
4257 +     if (!conf->force_depends)
4258 +         conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
4259 +
4260 +     if (conflicts) {
4261 +         ipkg_message(conf, level,
4262 +                      "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
4263 +         i = 0;
4264 +         while (i < conflicts->len)
4265 +              ipkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
4266 +         ipkg_message(conf, level, "\n");
4267 +         pkg_vec_free(conflicts);
4268 +         return IPKG_PKG_DEPS_UNSATISFIED;
4269 +     }
4270 +     return 0;
4271 +}
4272 +
4273 +static int update_file_ownership(ipkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
4274 +{
4275 +     str_list_t *new_list = pkg_get_installed_files(new_pkg);
4276 +     str_list_elt_t *iter;
4277 +
4278 +     for (iter = new_list->head; iter; iter = iter->next) {
4279 +         char *new_file = iter->data;
4280 +         pkg_t *owner = file_hash_get_file_owner(conf, new_file);
4281 +         if (!new_file)
4282 +              ipkg_message(conf, IPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
4283 +         if (!owner || (owner == old_pkg))
4284 +              file_hash_set_file_owner(conf, new_file, new_pkg);
4285 +     }
4286 +     if (old_pkg) {
4287 +         str_list_t *old_list = pkg_get_installed_files(old_pkg);
4288 +         for (iter = old_list->head; iter; iter = iter->next) {
4289 +              char *old_file = iter->data;
4290 +              pkg_t *owner = file_hash_get_file_owner(conf, old_file);
4291 +              if (owner == old_pkg) {
4292 +                   /* obsolete */
4293 +                   hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
4294 +              }
4295 +         }
4296 +     }
4297 +     return 0;
4298 +}
4299 +
4300 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg)
4301 +{
4302 +    /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
4303 +
4304 +    /* sma 6.20.02:  yup; here's the first bit */
4305 +    /* 
4306 +     * XXX: BUG easy for cworth
4307 +     * 1) please point the call below to the correct current root destination
4308 +     * 2) we need to resolve how to check the required space for a pending pkg, 
4309 +     *    my diddling with the .ipk file size below isn't going to cut it.
4310 +     * 3) return a proper error code instead of 1
4311 +     */
4312 +     int comp_size, blocks_available;
4313 +    
4314 +     if (!conf->force_space && pkg->installed_size != NULL) {
4315 +         blocks_available = get_available_blocks(conf->default_dest->root_dir);
4316 +
4317 +         comp_size = strtoul(pkg->installed_size, NULL, 0);
4318 +         /* round up a blocks count without doing fancy-but-slow casting jazz */ 
4319 +         comp_size = (int)((comp_size + 1023) / 1024);
4320 +
4321 +         if (comp_size >= blocks_available) {
4322 +              ipkg_message(conf, IPKG_ERROR,
4323 +                           "Only have %d available blocks on filesystem %s, pkg %s needs %d\n", 
4324 +                           blocks_available, conf->default_dest->root_dir, pkg->name, comp_size);
4325 +              return ENOSPC;
4326 +         }
4327 +     }
4328 +     return 0;
4329 +}
4330 +
4331 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg)
4332 +{
4333 +     int err;
4334 +     char *conffiles_file_name;
4335 +     char *root_dir;
4336 +     FILE *conffiles_file;
4337 +
4338 +     sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
4339 +
4340 +     pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
4341 +     if (pkg->tmp_unpack_dir == NULL) {
4342 +         ipkg_message(conf, IPKG_ERROR,
4343 +                      "%s: Failed to create temporary directory '%s': %s\n",
4344 +                      __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
4345 +         return errno;
4346 +     }
4347 +
4348 +     err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
4349 +     if (err) {
4350 +         return err;
4351 +     }
4352 +
4353 +     /* XXX: CLEANUP: There might be a cleaner place to read in the
4354 +       conffiles. Seems like I should be able to get everything to go
4355 +       through pkg_init_from_file. If so, maybe it would make sense to
4356 +       move all of unpack_pkg_control_files to that function. */
4357 +
4358 +     /* Don't need to re-read conffiles if we already have it */
4359 +     if (pkg->conffiles.head) {
4360 +         return 0;
4361 +     }
4362 +
4363 +     sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
4364 +     if (! file_exists(conffiles_file_name)) {
4365 +         free(conffiles_file_name);
4366 +         return 0;
4367 +     }
4368 +    
4369 +     conffiles_file = fopen(conffiles_file_name, "r");
4370 +     if (conffiles_file == NULL) {
4371 +         fprintf(stderr, "%s: failed to open %s: %s\n",
4372 +                 __FUNCTION__, conffiles_file_name, strerror(errno));
4373 +         free(conffiles_file_name);
4374 +         return errno;
4375 +     }
4376 +     free(conffiles_file_name);
4377 +
4378 +     while (1) {
4379 +         char *cf_name;
4380 +         char *cf_name_in_dest;
4381 +
4382 +         cf_name = file_read_line_alloc(conffiles_file);
4383 +         if (cf_name == NULL) {
4384 +              break;
4385 +         }
4386 +         str_chomp(cf_name);
4387 +         if (cf_name[0] == '\0') {
4388 +              continue;
4389 +         }
4390 +
4391 +         /* Prepend dest->root_dir to conffile name.
4392 +            Take pains to avoid multiple slashes. */
4393 +         root_dir = pkg->dest->root_dir;
4394 +         if (conf->offline_root)
4395 +              /* skip the offline_root prefix */
4396 +              root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
4397 +         sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
4398 +                       cf_name[0] == '/' ? (cf_name + 1) : cf_name);
4399 +
4400 +         /* Can't get an md5sum now, (file isn't extracted yet).
4401 +            We'll wait until resolve_conffiles */
4402 +         conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
4403 +
4404 +         free(cf_name);
4405 +         free(cf_name_in_dest);
4406 +     }
4407 +
4408 +     fclose(conffiles_file);
4409 +
4410 +     return 0;
4411 +}
4412 +
4413 +/* returns number of installed replacees */
4414 +int pkg_get_installed_replacees(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
4415 +{
4416 +     abstract_pkg_t **replaces = pkg->replaces;
4417 +     int replaces_count = pkg->replaces_count;
4418 +     int i, j;
4419 +     for (i = 0; i < replaces_count; i++) {
4420 +         abstract_pkg_t *ab_pkg = replaces[i];
4421 +         pkg_vec_t *pkg_vec = ab_pkg->pkgs;
4422 +         if (pkg_vec) {
4423 +              for (j = 0; j < pkg_vec->len; j++) {
4424 +                   pkg_t *replacee = pkg_vec->pkgs[j];
4425 +                   if (!pkg_conflicts(pkg, replacee))
4426 +                        continue;
4427 +                   if (replacee->state_status == SS_INSTALLED) {
4428 +                        pkg_vec_insert(installed_replacees, replacee);
4429 +                   }
4430 +              }
4431 +         }
4432 +     }
4433 +     return installed_replacees->len;
4434 +}
4435 +
4436 +int pkg_remove_installed_replacees(ipkg_conf_t *conf, pkg_vec_t *replacees)
4437 +{
4438 +     int i;
4439 +     int replaces_count = replacees->len;
4440 +     for (i = 0; i < replaces_count; i++) {
4441 +         pkg_t *replacee = replacees->pkgs[i];
4442 +         int err;
4443 +         replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
4444 +         err = ipkg_remove_pkg(conf, replacee,0);
4445 +         if (err)
4446 +              return err;
4447 +     }
4448 +     return 0;
4449 +}
4450 +
4451 +/* to unwind the removal: make sure they are installed */
4452 +int pkg_remove_installed_replacees_unwind(ipkg_conf_t *conf, pkg_vec_t *replacees)
4453 +{
4454 +     int i, err;
4455 +     int replaces_count = replacees->len;
4456 +     for (i = 0; i < replaces_count; i++) {
4457 +         pkg_t *replacee = replacees->pkgs[i];
4458 +         if (replacee->state_status != SS_INSTALLED) {
4459 +               ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4460 +              err = ipkg_install_pkg(conf, replacee,0);
4461 +              if (err)
4462 +                   return err;
4463 +         }
4464 +     }
4465 +     return 0;
4466 +}
4467 +
4468 +int caught_sigint = 0;
4469 +static void ipkg_install_pkg_sigint_handler(int sig)
4470 +{
4471 +     caught_sigint = sig;
4472 +}
4473 +
4474 +/* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
4475 +static int ipkg_install_check_downgrade(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
4476 +{        
4477 +     if (old_pkg) {
4478 +          char message_out[15];
4479 +         char *old_version = pkg_version_str_alloc(old_pkg);
4480 +         char *new_version = pkg_version_str_alloc(pkg);
4481 +         int cmp = pkg_compare_versions(old_pkg, pkg);
4482 +         int rc = 0;
4483 +
4484 +          memset(message_out,'\x0',15);
4485 +          strncpy (message_out,"Upgrading ",strlen("Upgrading ")); 
4486 +          if ( (conf->force_downgrade==1) && (cmp > 0) ){     /* We've been asked to allow downgrade  and version is precedent */
4487 +             cmp = -1 ;                                       /* then we force ipkg to downgrade */ 
4488 +             strncpy (message_out,"Downgrading ",strlen("Downgrading "));         /* We need to use a value < 0 because in the 0 case we are asking to */
4489 +                                                              /* reinstall, and some check could fail asking the "force-reinstall" option */
4490 +          } 
4491 +
4492 +         if (cmp > 0) {
4493 +              ipkg_message(conf, IPKG_NOTICE,
4494 +                           "Not downgrading package %s on %s from %s to %s.\n",
4495 +                           old_pkg->name, old_pkg->dest->name, old_version, new_version);
4496 +              rc = 1;
4497 +         } else if (cmp < 0) {
4498 +              ipkg_message(conf, IPKG_NOTICE,
4499 +                           "%s%s on %s from %s to %s...\n",
4500 +                           message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
4501 +              pkg->dest = old_pkg->dest;
4502 +              rc = 0;
4503 +         } else /* cmp == 0 */ {
4504 +              if (conf->force_reinstall) {
4505 +                   ipkg_message(conf, IPKG_NOTICE,
4506 +                                "Reinstalling %s (%s) on %s...\n",
4507 +                                pkg->name, new_version, old_pkg->dest->name);
4508 +                   pkg->dest = old_pkg->dest;
4509 +                   rc = 0;
4510 +              } else {
4511 +                   ipkg_message(conf, IPKG_NOTICE,
4512 +                                "Not installing %s (%s) on %s -- already installed.\n",
4513 +                                pkg->name, new_version, old_pkg->dest->name);
4514 +                   rc = 1;
4515 +              }
4516 +         } 
4517 +         free(old_version);
4518 +         free(new_version);
4519 +         return rc;
4520 +     } else {
4521 +      char message_out[15] ;
4522 +      memset(message_out,'\x0',15);
4523 +      if ( message ) 
4524 +          strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
4525 +      else
4526 +          strncpy( message_out,"Installing ",strlen("Installing ") );
4527 +         char *version = pkg_version_str_alloc(pkg);
4528 +      
4529 +         ipkg_message(conf, IPKG_NOTICE,
4530 +                      "%s%s (%s) to %s...\n", message_out,
4531 +                      pkg->name, version, pkg->dest->name);
4532 +         free(version);
4533 +         return 0;
4534 +     }
4535 +}
4536 +
4537 +/* and now the meat... */
4538 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
4539 +{
4540 +     int err = 0;
4541 +     int message = 0;
4542 +     pkg_t *old_pkg = NULL;
4543 +     pkg_vec_t *replacees;
4544 +     abstract_pkg_t *ab_pkg = NULL;
4545 +     int old_state_flag;
4546 +     char* file_md5;
4547 +
4548 +    
4549 +     if ( from_upgrade ) 
4550 +        message = 1;            /* Coming from an upgrade, and should change the output message */
4551 +
4552 +     if (!pkg) {
4553 +         ipkg_message(conf, IPKG_ERROR,
4554 +                      "INTERNAL ERROR: null pkg passed to ipkg_install_pkg\n");
4555 +         return -EINVAL;
4556 +     }
4557 +
4558 +     ipkg_message(conf, IPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
4559 +
4560 +     if (!pkg_arch_supported(conf, pkg)) {
4561 +         ipkg_message(conf, IPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
4562 +                      pkg->architecture, pkg->name);
4563 +         return -EINVAL;
4564 +     }
4565 +     if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
4566 +         err = satisfy_dependencies_for(conf, pkg);
4567 +         if (err) { return err; }
4568 +
4569 +         ipkg_message(conf, IPKG_NOTICE,
4570 +                      "Package %s is already installed in %s.\n", 
4571 +                      pkg->name, pkg->dest->name);
4572 +         return 0;
4573 +     }
4574 +
4575 +     if (pkg->dest == NULL) {
4576 +         pkg->dest = conf->default_dest;
4577 +     }
4578 +
4579 +     old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
4580 +
4581 +     err = ipkg_install_check_downgrade(conf, pkg, old_pkg, message);
4582 +     if (err) { return err; }
4583 +
4584 +     pkg->state_want = SW_INSTALL;
4585 +     if (old_pkg){                          
4586 +         old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
4587 +     }
4588 +
4589 +
4590 +     /* Abhaya: conflicts check */
4591 +     err = check_conflicts_for(conf, pkg);
4592 +     if (err) { return err; }
4593 +    
4594 +     /* this setup is to remove the upgrade scenario in the end when
4595 +       installing pkg A, A deps B & B deps on A. So both B and A are
4596 +       installed. Then A's installation is started resulting in an
4597 +       uncecessary upgrade */ 
4598 +     if (pkg->state_status == SS_INSTALLED
4599 +        && conf->force_reinstall == 0) return 0;
4600 +    
4601 +     err = verify_pkg_installable(conf, pkg);
4602 +     if (err) { return err; }
4603 +
4604 +     if (pkg->local_filename == NULL) {
4605 +         err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
4606 +         if (err) {
4607 +              ipkg_message(conf, IPKG_ERROR,
4608 +                           "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
4609 +                           pkg->name);
4610 +              return err;
4611 +         }
4612 +     }
4613 +
4614 +/* Check for md5 values */
4615 +     if (pkg->md5sum)
4616 +     {
4617 +         file_md5 = file_md5sum_alloc(pkg->local_filename);
4618 +         if (strcmp(file_md5, pkg->md5sum))
4619 +         {
4620 +              ipkg_message(conf, IPKG_ERROR,
4621 +                           "Package %s md5sum mismatch. Either the ipkg or the package index are corrupt. Try 'ipkg update'.\n",
4622 +                           pkg->name);
4623 +              free(file_md5);
4624 +              return err;
4625 +         }
4626 +         free(file_md5);
4627 +     }
4628 +
4629 +     if (pkg->tmp_unpack_dir == NULL) {
4630 +         unpack_pkg_control_files(conf, pkg);
4631 +     }
4632 +
4633 +     /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
4634 +/* Pigi: check if it will pass from here when replacing. It seems to fail */
4635 +/* That's rather strange that files don't change owner. Investigate !!!!!!*/
4636 +     err = update_file_ownership(conf, pkg, old_pkg);
4637 +     if (err) { return err; }
4638 +
4639 +     if (conf->nodeps == 0) {
4640 +         err = satisfy_dependencies_for(conf, pkg);
4641 +         if (err) { return err; }
4642 +     }
4643 +
4644 +     replacees = pkg_vec_alloc();
4645 +     pkg_get_installed_replacees(conf, pkg, replacees);
4646 +
4647 +     /* this next section we do with SIGINT blocked to prevent inconsistency between ipkg database and filesystem */
4648 +     {
4649 +         sigset_t newset, oldset;
4650 +         sighandler_t old_handler = NULL;
4651 +         int use_signal = 0;
4652 +         caught_sigint = 0;
4653 +         if (use_signal) {
4654 +              old_handler = signal(SIGINT, ipkg_install_pkg_sigint_handler);
4655 +         } else {
4656 +              sigemptyset(&newset);
4657 +              sigaddset(&newset, SIGINT);
4658 +              sigprocmask(SIG_BLOCK, &newset, &oldset);
4659 +         }
4660 +
4661 +         ipkg_state_changed++;
4662 +         pkg->state_flag |= SF_FILELIST_CHANGED;
4663 +
4664 +         /* XXX: BUG: we really should treat replacement more like an upgrade
4665 +          *      Instead, we're going to remove the replacees 
4666 +          */
4667 +         err = pkg_remove_installed_replacees(conf, replacees);
4668 +         if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
4669 +
4670 +         err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
4671 +         if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
4672 +
4673 +         err = prerm_deconfigure_conflictors(conf, pkg, replacees);
4674 +         if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
4675 +
4676 +         err = preinst_configure(conf, pkg, old_pkg);
4677 +         if (err) goto UNWIND_PREINST_CONFIGURE;
4678 +
4679 +         err = backup_modified_conffiles(conf, pkg, old_pkg);
4680 +         if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
4681 +
4682 +         err = check_data_file_clashes(conf, pkg, old_pkg);
4683 +         if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
4684 +
4685 +         err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
4686 +         if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
4687 +
4688 +         if (conf->noaction) return 0;
4689 +
4690 +         /* point of no return: no unwinding after this */
4691 +         if (old_pkg && !conf->force_reinstall) {
4692 +              old_pkg->state_want = SW_DEINSTALL;
4693 +
4694 +              if (old_pkg->state_flag & SF_NOPRUNE) {
4695 +                   ipkg_message(conf, IPKG_INFO,
4696 +                                "  not removing obsolesced files because package marked noprune\n");
4697 +              } else {
4698 +                   ipkg_message(conf, IPKG_INFO,
4699 +                                "  removing obsolesced files\n");
4700 +                   remove_obsolesced_files(conf, pkg, old_pkg);
4701 +              }
4702 +               /* removing files from old package, to avoid ghost files */ 
4703 +               remove_data_files_and_list(conf, old_pkg);
4704 +/* Pigi : It should be better to remove also maintainer and postrem scripts here, just in case*/
4705 +               remove_maintainer_scripts_except_postrm(conf, old_pkg);
4706 +               remove_postrm(conf, old_pkg);
4707 +/* Pigi */
4708 +
4709 +         }
4710 +
4711 +
4712 +         ipkg_message(conf, IPKG_INFO,
4713 +                      "  installing maintainer scripts\n");
4714 +         install_maintainer_scripts(conf, pkg, old_pkg);
4715 +
4716 +         /* the following just returns 0 */
4717 +         remove_disappeared(conf, pkg);
4718 +
4719 +         ipkg_message(conf, IPKG_INFO,
4720 +                      "  installing data files\n");
4721 +         install_data_files(conf, pkg);
4722 +
4723 +/* read comments from function for detail but I will execute this here as all other tests are ok.*/
4724 +         err = check_data_file_clashes_change(conf, pkg, old_pkg);
4725 +
4726 +         ipkg_message(conf, IPKG_INFO,
4727 +                      "  resolving conf files\n");
4728 +         resolve_conffiles(conf, pkg);
4729 +
4730 +         pkg->state_status = SS_UNPACKED;
4731 +         old_state_flag = pkg->state_flag;
4732 +         pkg->state_flag &= ~SF_PREFER;
4733 +         ipkg_message(conf, IPKG_DEBUG, "   pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
4734 +
4735 +         if (old_pkg && !conf->force_reinstall) {
4736 +              old_pkg->state_status = SS_NOT_INSTALLED;
4737 +         }
4738 +
4739 +         time(&pkg->installed_time);
4740 +
4741 +         ipkg_message(conf, IPKG_INFO,
4742 +                      "  cleanup temp files\n");
4743 +         cleanup_temporary_files(conf, pkg);
4744 +
4745 +         ab_pkg = pkg->parent;
4746 +         if (ab_pkg)
4747 +              ab_pkg->state_status = pkg->state_status;
4748 +
4749 +         ipkg_message(conf, IPKG_INFO, "Done.\n");
4750 +
4751 +         if (use_signal)
4752 +              signal(SIGINT, old_handler);
4753 +         else
4754 +              sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4755 +
4756 +         return 0;
4757 +     
4758 +
4759 +     UNWIND_POSTRM_UPGRADE_OLD_PKG:
4760 +         postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4761 +     UNWIND_CHECK_DATA_FILE_CLASHES:
4762 +         check_data_file_clashes_unwind(conf, pkg, old_pkg);
4763 +     UNWIND_BACKUP_MODIFIED_CONFFILES:
4764 +         backup_modified_conffiles_unwind(conf, pkg, old_pkg);
4765 +     UNWIND_PREINST_CONFIGURE:
4766 +         preinst_configure_unwind(conf, pkg, old_pkg);
4767 +     UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
4768 +         prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
4769 +     UNWIND_PRERM_UPGRADE_OLD_PKG:
4770 +         prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4771 +     UNWIND_REMOVE_INSTALLED_REPLACEES:
4772 +         pkg_remove_installed_replacees_unwind(conf, replacees);
4773 +
4774 +         ipkg_message(conf, IPKG_INFO,
4775 +                      "  cleanup temp files\n");
4776 +         cleanup_temporary_files(conf, pkg);
4777 +
4778 +         ipkg_message(conf, IPKG_INFO,
4779 +                      "Failed.\n");
4780 +         if (use_signal)
4781 +              signal(SIGINT, old_handler);
4782 +         else
4783 +              sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4784 +
4785 +         return err;
4786 +     }
4787 +}
4788 +
4789 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4790 +{
4791 +     /* DPKG_INCOMPATIBILITY:
4792 +       dpkg does some things here that we don't do yet. Do we care?
4793 +       
4794 +       1. If a version of the package is already installed, call
4795 +          old-prerm upgrade new-version
4796 +       2. If the script runs but exits with a non-zero exit status
4797 +          new-prerm failed-upgrade old-version
4798 +          Error unwind, for both the above cases:
4799 +          old-postinst abort-upgrade new-version
4800 +     */
4801 +     return 0;
4802 +}
4803 +
4804 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4805 +{
4806 +     /* DPKG_INCOMPATIBILITY:
4807 +       dpkg does some things here that we don't do yet. Do we care?
4808 +       (See prerm_upgrade_old_package for details)
4809 +     */
4810 +     return 0;
4811 +}
4812 +
4813 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4814 +{
4815 +     /* DPKG_INCOMPATIBILITY:
4816 +       dpkg does some things here that we don't do yet. Do we care?
4817 +       2. If a 'conflicting' package is being removed at the same time:
4818 +               1. If any packages depended on that conflicting package and
4819 +                  --auto-deconfigure is specified, call, for each such package:
4820 +                  deconfigured's-prerm deconfigure \
4821 +                  in-favour package-being-installed version \
4822 +                  removing conflicting-package version
4823 +               Error unwind:
4824 +                  deconfigured's-postinst abort-deconfigure \
4825 +                  in-favour package-being-installed-but-failed version \
4826 +                  removing conflicting-package version
4827 +
4828 +                  The deconfigured packages are marked as requiring
4829 +                  configuration, so that if --install is used they will be
4830 +                  configured again if possible.
4831 +               2. To prepare for removal of the conflicting package, call:
4832 +                  conflictor's-prerm remove in-favour package new-version
4833 +               Error unwind:
4834 +                  conflictor's-postinst abort-remove in-favour package new-version
4835 +     */
4836 +     return 0;
4837 +}
4838 +
4839 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4840 +{
4841 +     /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
4842 +       do yet. Do we care?  (See prerm_deconfigure_conflictors for
4843 +       details) */
4844 +     return 0;
4845 +}
4846 +
4847 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4848 +{
4849 +     int err;
4850 +     char *preinst_args;
4851 +
4852 +     if (old_pkg) {
4853 +         char *old_version = pkg_version_str_alloc(old_pkg);
4854 +         sprintf_alloc(&preinst_args, "upgrade %s", old_version);
4855 +         free(old_version);
4856 +     } else if (pkg->state_status == SS_CONFIG_FILES) {
4857 +         char *pkg_version = pkg_version_str_alloc(pkg);
4858 +         sprintf_alloc(&preinst_args, "install %s", pkg_version);
4859 +         free(pkg_version);
4860 +     } else {
4861 +         preinst_args = strdup("install");
4862 +     }
4863 +
4864 +     err = pkg_run_script(conf, pkg, "preinst", preinst_args);
4865 +     if (err) {
4866 +         ipkg_message(conf, IPKG_ERROR,
4867 +                      "Aborting installation of %s\n", pkg->name);
4868 +         return 1;
4869 +     }
4870 +
4871 +     free(preinst_args);
4872 +
4873 +     return 0;
4874 +}
4875 +
4876 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4877 +{
4878 +     /* DPKG_INCOMPATIBILITY:
4879 +       dpkg does the following error unwind, should we?
4880 +       pkg->postrm abort-upgrade old-version
4881 +       OR pkg->postrm abort-install old-version
4882 +       OR pkg->postrm abort-install
4883 +     */
4884 +     return 0;
4885 +}
4886 +
4887 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4888 +{
4889 +     int err;
4890 +     conffile_list_elt_t *iter;
4891 +     conffile_t *cf;
4892 +
4893 +     if (conf->noaction) return 0;
4894 +
4895 +     /* Backup all modified conffiles */
4896 +     if (old_pkg) {
4897 +         for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4898 +              char *cf_name;
4899 +              
4900 +              cf = iter->data;
4901 +              cf_name = root_filename_alloc(conf, cf->name);
4902 +
4903 +              /* Don't worry if the conffile is just plain gone */
4904 +              if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
4905 +                   err = backup_make_backup(conf, cf_name);
4906 +                   if (err) {
4907 +                        return err;
4908 +                   }
4909 +              }
4910 +              free(cf_name);
4911 +         }
4912 +     }
4913 +
4914 +     /* Backup all conffiles that were not conffiles in old_pkg */
4915 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4916 +         char *cf_name;
4917 +         cf = iter->data;
4918 +         cf_name = root_filename_alloc(conf, cf->name);
4919 +         /* Ignore if this was a conffile in old_pkg as well */
4920 +         if (pkg_get_conffile(old_pkg, cf->name)) {
4921 +              continue;
4922 +         }
4923 +
4924 +         if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
4925 +              err = backup_make_backup(conf, cf_name);
4926 +              if (err) {
4927 +                   return err;
4928 +              }
4929 +         }
4930 +         free(cf_name);
4931 +     }
4932 +
4933 +     return 0;
4934 +}
4935 +
4936 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4937 +{
4938 +     conffile_list_elt_t *iter;
4939 +
4940 +     if (old_pkg) {
4941 +         for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4942 +              backup_remove(iter->data->name);
4943 +         }
4944 +     }
4945 +
4946 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4947 +         backup_remove(iter->data->name);
4948 +     }
4949 +
4950 +     return 0;
4951 +}
4952 +
4953 +
4954 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4955 +{
4956 +     /* DPKG_INCOMPATIBILITY:
4957 +       ipkg takes a slightly different approach than dpkg at this
4958 +       point.  dpkg installs each file in the new package while
4959 +       creating a backup for any file that is replaced, (so that it
4960 +       can unwind if necessary).  To avoid complexity and redundant
4961 +       storage, ipkg doesn't do any installation until later, (at the
4962 +       point at which dpkg removes the backups.
4963 +       
4964 +       But, we do have to check for data file clashes, since after
4965 +       installing a package with a file clash, removing either of the
4966 +       packages involved in the clash has the potential to break the
4967 +       other package.
4968 +     */
4969 +     str_list_t *files_list;
4970 +     str_list_elt_t *iter;
4971 +
4972 +     int clashes = 0;
4973 +
4974 +     files_list = pkg_get_installed_files(pkg);
4975 +     for (iter = files_list->head; iter; iter = iter->next) {
4976 +         char *root_filename;
4977 +         char *filename = iter->data;
4978 +         root_filename = root_filename_alloc(conf, filename);
4979 +         if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
4980 +              pkg_t *owner;
4981 +              pkg_t *obs;
4982 +              /* Pre-existing conffiles are OK */
4983 +              /* @@@@ should have way to check that it is a conffile -Jamey */
4984 +              if (backup_exists_for(root_filename)) {
4985 +                   continue;
4986 +              }
4987 +
4988 +              /* Pre-existing files are OK if force-overwrite was asserted. */ 
4989 +              if (conf->force_overwrite) {
4990 +                   /* but we need to change who owns this file */
4991 +                   file_hash_set_file_owner(conf, filename, pkg);
4992 +                   continue;
4993 +              }
4994 +
4995 +              owner = file_hash_get_file_owner(conf, filename);
4996 +
4997 +              /* Pre-existing files are OK if owned by the pkg being upgraded. */
4998 +              if (owner && old_pkg) {
4999 +                   if (strcmp(owner->name, old_pkg->name) == 0) {
5000 +                        continue;
5001 +                   }
5002 +              }
5003 +
5004 +              /* Pre-existing files are OK if owned by a package replaced by new pkg. */
5005 +              if (owner) {
5006 +                    ipkg_message(conf, IPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
5007 +                   if (pkg_replaces(pkg, owner)) {
5008 +                        continue;
5009 +                   }
5010 +/* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
5011 +   then it's ok to overwrite. */
5012 +                    if (strcmp(owner->name,pkg->name)==0){
5013 +                        ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
5014 +                        continue;
5015 +                    }
5016 +              }
5017 +
5018 +              /* Pre-existing files are OK if they are obsolete */
5019 +              obs = hash_table_get(&conf->obs_file_hash, filename);
5020 +              if (obs) {
5021 +                   ipkg_message(conf, IPKG_INFO, "Pre-exiting file %s is obsolete.  obs_pkg=%s\n", filename, obs->name);
5022 +                   continue;
5023 +              }
5024 +
5025 +              /* We have found a clash. */
5026 +              ipkg_message(conf, IPKG_ERROR,
5027 +                           "Package %s wants to install file %s\n"
5028 +                           "\tBut that file is already provided by package ",
5029 +                           pkg->name, filename);
5030 +              if (owner) {
5031 +                   ipkg_message(conf, IPKG_ERROR,
5032 +                                "%s\n", owner->name);
5033 +              } else {
5034 +                   ipkg_message(conf, IPKG_ERROR,
5035 +                                "<no package>\nPlease move this file out of the way and try again.\n");
5036 +              }
5037 +              clashes++;
5038 +         }
5039 +         free(root_filename);
5040 +     }
5041 +     pkg_free_installed_files(pkg);
5042 +
5043 +     return clashes;
5044 +}
5045 +
5046 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5047 +{
5048 +    /* Basically that's the worst hack I could do to be able to change ownership of
5049 +       file list, but, being that we have no way to unwind the mods, due to structure
5050 +       of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
5051 +       What we do here is change the ownership of file in hash if a replace ( or similar events
5052 +       happens )
5053 +       Only the action that are needed to change name should be considered.
5054 +       @@@ To change after 1.0 release.
5055 +     */
5056 +     str_list_t *files_list;
5057 +     str_list_elt_t *iter;
5058 +
5059 +     int clashes = 0;
5060 +
5061 +     files_list = pkg_get_installed_files(pkg);
5062 +     for (iter = files_list->head; iter; iter = iter->next) {
5063 +         char *root_filename;
5064 +         char *filename = iter->data;
5065 +         root_filename = root_filename_alloc(conf, filename);
5066 +         if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
5067 +              pkg_t *owner;
5068 +
5069 +              if (conf->force_overwrite) {
5070 +                   /* but we need to change who owns this file */
5071 +                   file_hash_set_file_owner(conf, filename, pkg);
5072 +                   continue;
5073 +              }
5074 +
5075 +              owner = file_hash_get_file_owner(conf, filename);
5076 +
5077 +              /* Pre-existing files are OK if owned by a package replaced by new pkg. */
5078 +              if (owner) {
5079 +                   if (pkg_replaces(pkg, owner)) {
5080 +/* It's now time to change the owner of that file. 
5081 +   It has been "replaced" from the new "Replaces", then I need to inform lists file about that.  */
5082 +                        ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
5083 +                        file_hash_set_file_owner(conf, filename, pkg);
5084 +                        continue;
5085 +                   }
5086 +              }
5087 +
5088 +         }
5089 +         free(root_filename);
5090 +     }
5091 +     pkg_free_installed_files(pkg);
5092 +
5093 +     return clashes;
5094 +}
5095 +
5096 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5097 +{
5098 +     /* Nothing to do since check_data_file_clashes doesn't change state */
5099 +     return 0;
5100 +}
5101 +
5102 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5103 +{
5104 +     /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
5105 +       1. If the package is being upgraded, call
5106 +          old-postrm upgrade new-version
5107 +       2. If this fails, attempt:
5108 +          new-postrm failed-upgrade old-version
5109 +       Error unwind, for both cases:
5110 +          old-preinst abort-upgrade new-version    */
5111 +     return 0;
5112 +}
5113 +
5114 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5115 +{
5116 +     /* DPKG_INCOMPATIBILITY:
5117 +       dpkg does some things here that we don't do yet. Do we care?
5118 +       (See postrm_upgrade_old_pkg for details)
5119 +     */
5120 +    return 0;
5121 +}
5122 +
5123 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5124 +{
5125 +     int err;
5126 +     str_list_t *old_files;
5127 +     str_list_elt_t *of;
5128 +     str_list_t *new_files;
5129 +     str_list_elt_t *nf;
5130 +
5131 +     if (old_pkg == NULL) {
5132 +         return 0;
5133 +     }
5134 +
5135 +     old_files = pkg_get_installed_files(old_pkg);
5136 +     new_files = pkg_get_installed_files(pkg);
5137 +
5138 +     for (of = old_files->head; of; of = of->next) {
5139 +         pkg_t *owner;
5140 +         char *old, *new;
5141 +         old = of->data;
5142 +         for (nf = new_files->head; nf; nf = nf->next) {
5143 +              new = nf->data;
5144 +              if (strcmp(old, new) == 0) {
5145 +                   goto NOT_OBSOLETE;
5146 +              }
5147 +         }
5148 +         if (file_is_dir(old)) {
5149 +              continue;
5150 +         }
5151 +         owner = file_hash_get_file_owner(conf, old);
5152 +         if (owner != old_pkg) {
5153 +              /* in case obsolete file no longer belongs to old_pkg */
5154 +              continue;
5155 +         }
5156
5157 +         /* old file is obsolete */
5158 +         ipkg_message(conf, IPKG_INFO,
5159 +                      "    removing obsolete file %s\n", old);
5160 +         if (!conf->noaction) {
5161 +              err = unlink(old);
5162 +              if (err) {
5163 +                   ipkg_message(conf, IPKG_ERROR, "    Warning: remove %s failed: %s\n", old,
5164 +                                strerror(errno));
5165 +              }
5166 +         }
5167 +
5168 +     NOT_OBSOLETE:
5169 +         ;
5170 +     }
5171 +
5172 +     pkg_free_installed_files(old_pkg);
5173 +     pkg_free_installed_files(pkg);
5174 +
5175 +     return 0;
5176 +}
5177 +
5178 +static int remove_obsolete_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5179 +{
5180 +     int i;
5181 +     int err = 0;
5182 +     char *globpattern;
5183 +     glob_t globbuf;
5184 +     if (0) {
5185 +         if (!pkg->dest) {
5186 +              ipkg_message(conf, IPKG_ERROR, "%s: no dest for package %s\n", __FUNCTION__, pkg->name);
5187 +              return -1;
5188 +         }
5189 +         sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name);
5190 +         err = glob(globpattern, 0, NULL, &globbuf);
5191 +         free(globpattern);
5192 +         if (err) {
5193 +              return err;
5194 +         }
5195 +         /* XXXX this should perhaps only remove the ones that are not overwritten in new package.  Jamey 11/11/2003 */
5196 +         for (i = 0; i < globbuf.gl_pathc; i++) {
5197 +              ipkg_message(conf, IPKG_DEBUG, "Removing control file %s from old_pkg %s\n",
5198 +                           globbuf.gl_pathv[i], old_pkg->name);
5199 +              if (!conf->noaction)
5200 +                   unlink(globbuf.gl_pathv[i]);
5201 +         }
5202 +         globfree(&globbuf);
5203 +     }
5204 +     return err;
5205 +}
5206 +
5207 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5208 +{
5209 +     int ret;
5210 +     char *prefix;
5211 +
5212 +     if (old_pkg)
5213 +         remove_obsolete_maintainer_scripts(conf, pkg, old_pkg);
5214 +     sprintf_alloc(&prefix, "%s.", pkg->name);
5215 +     ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
5216 +                                                       pkg->dest->info_dir,
5217 +                                                       prefix);
5218 +     free(prefix);
5219 +     return ret;
5220 +}
5221 +
5222 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg)
5223 +{
5224 +     /* DPKG_INCOMPATIBILITY:
5225 +       This is a fairly sophisticated dpkg operation. Shall we
5226 +       skip it? */
5227 +     
5228 +     /* Any packages all of whose files have been overwritten during the
5229 +       installation, and which aren't required for dependencies, are
5230 +       considered to have been removed. For each such package
5231 +       1. disappearer's-postrm disappear overwriter overwriter-version
5232 +       2. The package's maintainer scripts are removed
5233 +       3. It is noted in the status database as being in a sane state,
5234 +           namely not installed (any conffiles it may have are ignored,
5235 +          rather than being removed by dpkg). Note that disappearing
5236 +          packages do not have their prerm called, because dpkg doesn't
5237 +          know in advance that the package is going to vanish.
5238 +     */
5239 +     return 0;
5240 +}
5241 +
5242 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg)
5243 +{
5244 +     int err;
5245 +
5246 +     /* ipkg takes a slightly different approach to data file backups
5247 +       than dpkg. Rather than removing backups at this point, we
5248 +       actually do the data file installation now. See comments in
5249 +       check_data_file_clashes() for more details. */
5250 +    
5251 +     ipkg_message(conf, IPKG_INFO,
5252 +                 "    extracting data files to %s\n", pkg->dest->root_dir);
5253 +     err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
5254 +     if (err) {
5255 +         return err;
5256 +     }
5257 +
5258 +     /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
5259 +        so we can't save ourself from removing important packages
5260 +        At this point we (should) have extracted the .control file, so it
5261 +        would be a good idea to reload the data in it, and set the Essential 
5262 +        state in *pkg. From now on the Essential is back in status file and
5263 +        we can protect again.
5264 +        We should operate this way:
5265 +        fopen the file ( pkg->dest->root_dir/pkg->name.control )
5266 +        check for "Essential" in it 
5267 +        set the value in pkg->essential.
5268 +        This new routine could be useful also for every other flag
5269 +        Pigi: 16/03/2004 */
5270 +     set_flags_from_control(conf, pkg) ;
5271 +     
5272 +     ipkg_message(conf, IPKG_DEBUG, "    Calling pkg_write_filelist from %s\n", __FUNCTION__);
5273 +     err = pkg_write_filelist(conf, pkg);
5274 +     if (err)
5275 +         return err;
5276 +
5277 +     /* XXX: FEATURE: ipkg should identify any files which existed
5278 +       before installation and which were overwritten, (see
5279 +       check_data_file_clashes()). What it must do is remove any such
5280 +       files from the filelist of the old package which provided the
5281 +       file. Otherwise, if the old package were removed at some point
5282 +       it would break the new package. Removing the new package will
5283 +       also break the old one, but this cannot be helped since the old
5284 +       package's file has already been deleted. This is the importance
5285 +       of check_data_file_clashes(), and only allowing ipkg to install
5286 +       a clashing package with a user force. */
5287 +
5288 +     return 0;
5289 +}
5290 +
5291 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg)
5292 +{
5293 +     conffile_list_elt_t *iter;
5294 +     conffile_t *cf;
5295 +     char *cf_backup;
5296 +
5297 +    char *md5sum;
5298 +
5299 +    
5300 +     if (conf->noaction) return 0;
5301 +
5302 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
5303 +         char *root_filename;
5304 +         cf = iter->data;
5305 +         root_filename = root_filename_alloc(conf, cf->name);
5306 +
5307 +         /* Might need to initialize the md5sum for each conffile */
5308 +         if (cf->value == NULL) {
5309 +              cf->value = file_md5sum_alloc(root_filename);
5310 +         }
5311 +
5312 +         if (!file_exists(root_filename)) {
5313 +              free(root_filename);
5314 +              continue;
5315 +         }
5316 +
5317 +         cf_backup = backup_filename_alloc(root_filename);
5318 +
5319 +
5320 +         if (file_exists(cf_backup)) {
5321 + /* Let's compute md5 to test if files are changed */
5322 +             md5sum = file_md5sum_alloc(cf_backup);
5323 +               if (strcmp( cf->value,md5sum) != 0 ) {
5324 +                 if (conf->force_defaults
5325 +                     || user_prefers_old_conffile(cf->name, cf_backup) ) {
5326 +                      rename(cf_backup, root_filename);
5327 +                 }
5328 +              }
5329 +              unlink(cf_backup);
5330 +              free(md5sum);
5331 +         }
5332 +
5333 +         free(cf_backup);
5334 +         free(root_filename);
5335 +     }
5336 +
5337 +     return 0;
5338 +}
5339 +
5340 +static int user_prefers_old_conffile(const char *file_name, const char *backup)
5341 +{
5342 +     char *response;
5343 +     const char *short_file_name;
5344 +
5345 +     short_file_name = strrchr(file_name, '/');
5346 +     if (short_file_name) {
5347 +         short_file_name++;
5348 +     } else {
5349 +         short_file_name = file_name;
5350 +     }
5351 +
5352 +     while (1) {
5353 +         response = get_user_response("    Configuration file '%s'\n"
5354 +                                      "    ==> File on system created by you or by a script.\n"
5355 +                                      "    ==> File also in package provided by package maintainer.\n"
5356 +                                      "       What would you like to do about it ?  Your options are:\n"
5357 +                                      "        Y or I  : install the package maintainer's version\n"
5358 +                                      "        N or O  : keep your currently-installed version\n"
5359 +                                      "          D     : show the differences between the versions (if diff is installed)\n"
5360 +                                      "     The default action is to keep your current version.\n"
5361 +                                      "    *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
5362 +         if (strcmp(response, "y") == 0
5363 +             || strcmp(response, "i") == 0
5364 +             || strcmp(response, "yes") == 0) {
5365 +              free(response);
5366 +              return 0;
5367 +         }
5368 +
5369 +         if (strcmp(response, "d") == 0) {
5370 +              char *cmd;
5371 +
5372 +              free(response);
5373 +              /* XXX: BUG rewrite to use exec or busybox's internal diff */
5374 +              sprintf_alloc(&cmd, "diff -u %s %s", backup, file_name);
5375 +              xsystem(cmd);
5376 +              free(cmd);
5377 +              printf("    [Press ENTER to continue]\n");
5378 +              response = file_read_line_alloc(stdin);
5379 +              free(response);
5380 +              continue;
5381 +         }
5382 +
5383 +         free(response);
5384 +         return 1;
5385 +     }
5386 +}
5387 +
5388 +/* XXX: CLEANUP: I'd like to move all of the code for
5389 +   creating/cleaning pkg->tmp_unpack_dir directly into pkg.c. (Then,
5390 +   it would make sense to cleanup pkg->tmp_unpack_dir directly from
5391 +   pkg_deinit for example). */
5392 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg)
5393 +{
5394 +     DIR *tmp_dir;
5395 +     struct dirent *dirent;
5396 +     char *tmp_file;
5397 +
5398 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
5399 +#error
5400 +     ipkg_message(conf, IPKG_DEBUG,
5401 +                 "%s: Not cleaning up %s since ipkg compiled with IPKG_DEBUG_NO_TMP_CLEANUP\n",
5402 +                 __FUNCTION__, pkg->tmp_unpack_dir);
5403 +     return 0;
5404 +#endif
5405 +
5406 +     if (pkg->tmp_unpack_dir && file_is_dir(pkg->tmp_unpack_dir)) {
5407 +         tmp_dir = opendir(pkg->tmp_unpack_dir);
5408 +         if (tmp_dir) {
5409 +              while (1) {
5410 +                   dirent = readdir(tmp_dir);
5411 +                   if (dirent == NULL) {
5412 +                        break;
5413 +                   }
5414 +                   sprintf_alloc(&tmp_file, "%s/%s",
5415 +                                 pkg->tmp_unpack_dir, dirent->d_name);
5416 +                   if (! file_is_dir(tmp_file)) {
5417 +                        unlink(tmp_file);
5418 +                   }
5419 +                   free(tmp_file);
5420 +              }
5421 +              closedir(tmp_dir);
5422 +              rmdir(pkg->tmp_unpack_dir);
5423 +              free(pkg->tmp_unpack_dir);
5424 +              pkg->tmp_unpack_dir = NULL;
5425 +         }
5426 +     }
5427 +
5428 +     ipkg_message(conf, IPKG_INFO, "cleanup_temporary_files: pkg=%s local_filename=%s tmp_dir=%s\n",
5429 +                 pkg->name, pkg->local_filename, conf->tmp_dir);
5430 +     if (pkg->local_filename && strncmp(pkg->local_filename, conf->tmp_dir, strlen(conf->tmp_dir)) == 0) {
5431 +         unlink(pkg->local_filename);
5432 +         free(pkg->local_filename);
5433 +         pkg->local_filename = NULL;
5434 +     }
5435 +
5436 +     return 0;
5437 +}
5438 +
5439 +static char *backup_filename_alloc(const char *file_name)
5440 +{
5441 +     char *backup;
5442 +
5443 +     sprintf_alloc(&backup, "%s%s", file_name, IPKG_BACKUP_SUFFIX);
5444 +
5445 +     return backup;
5446 +}
5447 +
5448 +int backup_make_backup(ipkg_conf_t *conf, const char *file_name)
5449 +{
5450 +     int err;
5451 +     char *backup;
5452 +    
5453 +     backup = backup_filename_alloc(file_name);
5454 +     err = file_copy(file_name, backup);
5455 +     if (err) {
5456 +         ipkg_message(conf, IPKG_ERROR,
5457 +                      "%s: Failed to copy %s to %s\n",
5458 +                      __FUNCTION__, file_name, backup);
5459 +     }
5460 +
5461 +     free(backup);
5462 +
5463 +     return err;
5464 +}
5465 +
5466 +static int backup_exists_for(const char *file_name)
5467 +{
5468 +     int ret;
5469 +     char *backup;
5470 +
5471 +     backup = backup_filename_alloc(file_name);
5472 +
5473 +     ret = file_exists(backup);
5474 +
5475 +     free(backup);
5476 +
5477 +     return ret;
5478 +}
5479 +
5480 +static int backup_remove(const char *file_name)
5481 +{
5482 +     char *backup;
5483 +
5484 +     backup = backup_filename_alloc(file_name);
5485 +     unlink(backup);
5486 +     free(backup);
5487 +
5488 +     return 0;
5489 +}
5490 +
5491 +\f
5492 +
5493 +#ifdef CONFIG_IPKG_PROCESS_ACTIONS
5494 +
5495 +int ipkg_remove_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove) 
5496 +{
5497 +     /* first, remove the packages that need removing */
5498 +     for (i = 0 ; i < pkgs_to_remove->len; i++ ) {
5499 +         pkg_t *pkg = pkgs_to_remove->pkgs[i];
5500 +         err = ipkg_remove_pkg(conf, pkg,0);
5501 +         if (err) return err;
5502 +     }
5503 +     return 0;
5504 +}
5505 +
5506 +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)
5507 +{
5508 +     int i;
5509 +     /* now one more pass checking on the ones that need to be installed */
5510 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5511 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5512 +         if (pkg->dest == NULL)
5513 +              pkg->dest = conf->default_dest;
5514 +
5515 +         pkg->state_want = SW_INSTALL;
5516 +
5517 +         /* Abhaya: conflicts check */
5518 +         err = check_conflicts_for(conf, pkg);
5519 +         if (err) { return err; }
5520 +     }
5521 +     return 0;
5522 +}
5523 +
5524 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5525 +{
5526 +     int i;
5527 +     /* now one more pass checking on the ones that need to be installed */
5528 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5529 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5530 +
5531 +         /* XXX: FEATURE: Need to really support Provides/Replaces: here at some point */
5532 +         pkg_vec_t *replacees = pkg_vec_alloc();
5533 +         pkg_get_installed_replacees(conf, pkg, replacees);
5534 +
5535 +         /* XXX: BUG: we really should treat replacement more like an upgrade
5536 +          *      Instead, we're going to remove the replacees 
5537 +          */
5538 +         err = pkg_remove_installed_replacees(conf, replacees);
5539 +         if (err) return err;
5540 +         pkg->state_flag |= SF_REMOVED_REPLACEES;
5541 +     }
5542 +     return 0;
5543 +}
5544 +
5545 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5546 +{
5547 +     int i;
5548 +     /* now one more pass checking on the ones that need to be installed */
5549 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5550 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5551 +         if (pkg->local_filename == NULL) {
5552 +              err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
5553 +              if (err) {
5554 +                   ipkg_message(conf, IPKG_ERROR,
5555 +                                "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
5556 +                                pkg->name);
5557 +                   return err;
5558 +              }
5559 +         }
5560 +         if (pkg->tmp_unpack_dir == NULL) {
5561 +              err = unpack_pkg_control_files(conf, pkg);
5562 +              if (err) return err;
5563 +         }
5564 +     }
5565 +     return 0;
5566 +}
5567 +
5568 +int ipkg_process_actions_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5569 +{
5570 +     int i;
5571 +     /* now one more pass checking on the ones that need to be installed */
5572 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5573 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5574 +         pkg_t *old_pkg = pkg->old_pkg;
5575 +
5576 +         err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
5577 +         if (err) return err;
5578 +
5579 +         err = prerm_deconfigure_conflictors(conf, pkg, replacees);
5580 +         if (err) return err;
5581 +
5582 +         err = preinst_configure(conf, pkg, old_pkg);
5583 +         if (err) return err;
5584 +
5585 +         err = backup_modified_conffiles(conf, pkg, old_pkg);
5586 +         if (err) return err;
5587 +
5588 +         err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
5589 +         if (err) return err;
5590 +     }
5591 +     return 0;
5592 +}
5593 +
5594 +int ipkg_process_actions_install(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5595 +{
5596 +     int i;
5597 +     /* now one more pass checking on the ones that need to be installed */
5598 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5599 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5600 +         pkg_t *old_pkg = pkg->old_pkg;
5601 +
5602 +         if (old_pkg) {
5603 +              old_pkg->state_want = SW_DEINSTALL;
5604 +
5605 +              if (old_pkg->state_flag & SF_NOPRUNE) {
5606 +                   ipkg_message(conf, IPKG_INFO,
5607 +                                "  not removing obsolesced files because package marked noprune\n");
5608 +              } else {
5609 +                   ipkg_message(conf, IPKG_INFO,
5610 +                                "  removing obsolesced files\n");
5611 +                   remove_obsolesced_files(conf, pkg, old_pkg);
5612 +              }
5613 +         }
5614 +
5615 +         ipkg_message(conf, IPKG_INFO,
5616 +                      "  installing maintainer scripts\n");
5617 +         install_maintainer_scripts(conf, pkg, old_pkg);
5618 +
5619 +         /* the following just returns 0 */
5620 +         remove_disappeared(conf, pkg);
5621 +
5622 +         ipkg_message(conf, IPKG_INFO,
5623 +                      "  installing data files\n");
5624 +         install_data_files(conf, pkg);
5625 +
5626 +         ipkg_message(conf, IPKG_INFO,
5627 +                      "  resolving conf files\n");
5628 +         resolve_conffiles(conf, pkg);
5629 +
5630 +         pkg->state_status = SS_UNPACKED;
5631 +
5632 +         if (old_pkg) {
5633 +              old_pkg->state_status = SS_NOT_INSTALLED;
5634 +         }
5635 +
5636 +         time(&pkg->installed_time);
5637 +
5638 +         ipkg_message(conf, IPKG_INFO,
5639 +                      "  cleanup temp files\n");
5640 +         cleanup_temporary_files(conf, pkg);
5641 +
5642 +         if (pkg->parent)
5643 +              pkg->parent->state_status = pkg->state_status;
5644 +     }
5645 +     return 0;
5646 +}
5647 +
5648 +int ipkg_process_actions_unwind_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5649 +{
5650 +     int i;
5651 +     /* now one more pass checking on the ones that need to be installed */
5652 +     for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5653 +         pkg_t *pkg = pkgs_to_install->pkgs[i];
5654 +         pkg_t *old_pkg = pkg->old_pkg;
5655 +
5656 +         if (old_pkg) {
5657 +              if (old_pkg->state_flags & SF_POSTRM_UPGRADE)
5658 +                   postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5659 +              if (old_pkg->state_flags & SF_CHECK_DATA_FILE_CLASHES)
5660 +                   check_data_file_clashes_unwind(conf, pkg, old_pkg);
5661 +              if (old_pkg->state_flags & SF_BACKUP_MODIFIED_CONFFILES)
5662 +                   backup_modified_conffiles_unwind(conf, pkg, old_pkg);
5663 +              if (old_pkg->state_flags & SF_PREINST_CONFIGURE)
5664 +                   preinst_configure_unwind(conf, pkg, old_pkg);
5665 +              if (old_pkg->state_flags & SF_DECONFIGURE_CONFLICTORS)
5666 +                   prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
5667 +              if (old_pkg->state_flags & SF_PRERM_UPGRADE)
5668 +                   prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5669 +
5670 +              if (old_pkg->state_flags & SF_REMOVED_REPLACEES)
5671 +                   remove_installed_replacees_unwind(conf, pkg, old_pkg);
5672 +
5673 +         }
5674 +     }
5675 +     return 0;
5676 +}
5677 +
5678 +/* 
5679 + * Perform all the actions.
5680 + *
5681 + * pkgs_to_remove are packages marked for removal.
5682 + * pkgs_superseded are the old packages being replaced by upgrades.
5683 + *
5684 + * Assumes pkgs_to_install includes all dependences, recursively, sorted in installable order.
5685 + */
5686 +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)
5687 +{
5688 +     int err;
5689 +     int i;
5690 +
5691 +     err = ipkg_remove_packages(conf, pkgs_to_remove);
5692 +     if (err) return err;
5693 +
5694 +     err = ipkg_process_actions_sanity_check(conf, pkgs_superseded, pkgs_to_install);
5695 +     if (err) return err;
5696 +
5697 +     err = ipkg_process_actions_remove_replacees(conf, pkgs_to_install);
5698 +     if (err) goto UNWIND;
5699 +
5700 +     /* @@@@ look at ipkg_install_pkg for handling replacements */
5701 +     err = ipkg_process_actions_unpack_packages(conf, pkgs_to_install);
5702 +     if (err) goto UNWIND;
5703 +
5704 +     /* 
5705 +      * Now that we have the packages unpacked, we can look for data
5706 +      * file clashes.  First, we mark the files from the superseded
5707 +      * packages as obsolete.  Then we scan the files in
5708 +      * pkgs_to_install, and only complain about clashes with
5709 +      * non-obsolete files.
5710 +      */
5711 +
5712 +     err = ipkg_process_actions_check_data_file_clashes(conf, pkgs_superseded, pkgs_to_install);
5713 +     if (err) goto UNWIND;
5714 +
5715 +     /* this was before checking data file clashes */
5716 +     err = ipkg_process_actions_prerm(conf, pkgs_superseded, pkgs_to_install);
5717 +     if (err) goto UNWIND;
5718 +
5719 +     /* point of no return: no unwinding after this */
5720 +     err = ipkg_process_actions_install(conf, pkgs_to_install);
5721 +     if (err) return err;
5722 +
5723 +     ipkg_message(conf, IPKG_INFO, "Done.\n");
5724 +     return 0;
5725 +
5726 + UNWIND:
5727 +     ipkg_process_actions_unwind(conf, pkgs_to_install);
5728 +
5729 +     ipkg_message(conf, IPKG_INFO,
5730 +                 "  cleanup temp files\n");
5731 +     cleanup_temporary_files(conf, pkg);
5732 +
5733 +     ipkg_message(conf, IPKG_INFO,
5734 +                 "Failed.\n");
5735 +     return err;
5736 +}
5737 +
5738 +#endif
5739 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_install.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.h
5740 --- busybox-1.2.0-orig/archival/libipkg/ipkg_install.h  1970-01-01 01:00:00.000000000 +0100
5741 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_install.h 2006-07-22 16:31:25.000000000 +0200
5742 @@ -0,0 +1,35 @@
5743 +/* ipkg_install.h - the itsy package management system
5744 +
5745 +   Carl D. Worth
5746 +
5747 +   Copyright (C) 2001 University of Southern California
5748 +
5749 +   This program is free software; you can redistribute it and/or
5750 +   modify it under the terms of the GNU General Public License as
5751 +   published by the Free Software Foundation; either version 2, or (at
5752 +   your option) any later version.
5753 +
5754 +   This program is distributed in the hope that it will be useful, but
5755 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5756 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5757 +   General Public License for more details.
5758 +*/
5759 +
5760 +#ifndef IPKG_INSTALL_H
5761 +#define IPKG_INSTALL_H
5762 +
5763 +#include "pkg.h"
5764 +#include "ipkg_conf.h"
5765 +
5766 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name);
5767 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name);
5768 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename);
5769 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg,int from_upgrading);
5770 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
5771 +
5772 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf);
5773 +
5774 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg_name, pkg_vec_t *pkgs_needed);
5775 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed);
5776 +
5777 +#endif
5778 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_message.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.c
5779 --- busybox-1.2.0-orig/archival/libipkg/ipkg_message.c  1970-01-01 01:00:00.000000000 +0100
5780 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.c 2006-07-22 16:31:25.000000000 +0200
5781 @@ -0,0 +1,61 @@
5782 +/* ipkg_message.c - the itsy package management system
5783 +
5784 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5785 +
5786 +   This program is free software; you can redistribute it and/or
5787 +   modify it under the terms of the GNU General Public License as
5788 +   published by the Free Software Foundation; either version 2, or (at
5789 +   your option) any later version.
5790 +
5791 +   This program is distributed in the hope that it will be useful, but
5792 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5793 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5794 +   General Public License for more details.
5795 +*/
5796 +
5797 +
5798 +#include "ipkg.h"
5799 +#include "ipkg_conf.h"
5800 +#include "ipkg_message.h"
5801 +
5802 +#ifndef IPKG_LIB
5803 +
5804 +void
5805 +ipkg_message (ipkg_conf_t * conf, message_level_t level, char *fmt, ...)
5806 +{
5807 +       va_list ap;
5808 +
5809 +       if (conf && (conf->verbosity < level))
5810 +       {
5811 +               return;
5812 +       }
5813 +       else
5814 +       {
5815 +
5816 +               va_start (ap, fmt);
5817 +               vprintf (fmt, ap);
5818 +               va_end (ap);
5819 +       }
5820 +}
5821 +
5822 +#else
5823 +
5824 +#include "libipkg.h"
5825 +
5826 +//#define ipkg_message(conf, level, fmt, arg...) ipkg_cb_message(conf, level, fmt, ## arg)
5827 +
5828 +void
5829 +ipkg_message (ipkg_conf_t * conf, message_level_t level, char *fmt, ...)
5830 +{
5831 +       va_list ap;
5832 +       char ts[256];
5833 +
5834 +       if (ipkg_cb_message)
5835 +       {
5836 +               va_start (ap, fmt);
5837 +               vsnprintf (ts,256,fmt, ap);
5838 +               va_end (ap);
5839 +               ipkg_cb_message(conf,level,ts);
5840 +       }
5841 +}
5842 +#endif
5843 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_message.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.h
5844 --- busybox-1.2.0-orig/archival/libipkg/ipkg_message.h  1970-01-01 01:00:00.000000000 +0100
5845 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_message.h 2006-07-22 16:31:25.000000000 +0200
5846 @@ -0,0 +1,32 @@
5847 +/* ipkg_message.h - the itsy package management system
5848 +
5849 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5850 +
5851 +   This program is free software; you can redistribute it and/or
5852 +   modify it under the terms of the GNU General Public License as
5853 +   published by the Free Software Foundation; either version 2, or (at
5854 +   your option) any later version.
5855 +
5856 +   This program is distributed in the hope that it will be useful, but
5857 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5858 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5859 +   General Public License for more details.
5860 +*/
5861 +
5862 +#ifndef _IPKG_MESSAGE_H_
5863 +#define _IPKG_MESSAGE_H_
5864 +
5865 +#include "ipkg.h"
5866 +#include "ipkg_conf.h"
5867 +
5868 +typedef enum {
5869 +     IPKG_ERROR,       /* error conditions */
5870 +     IPKG_NOTICE,      /* normal but significant condition */
5871 +     IPKG_INFO,                /* informational message */
5872 +     IPKG_DEBUG,       /* debug level message */
5873 +     IPKG_DEBUG2,      /* more debug level message */
5874 +} message_level_t;
5875 +
5876 +extern void ipkg_message(ipkg_conf_t *conf, message_level_t level, char *fmt, ...);
5877 +
5878 +#endif /* _IPKG_MESSAGE_H_ */
5879 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_remove.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.c
5880 --- busybox-1.2.0-orig/archival/libipkg/ipkg_remove.c   1970-01-01 01:00:00.000000000 +0100
5881 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.c  2006-07-22 16:31:25.000000000 +0200
5882 @@ -0,0 +1,383 @@
5883 +/* ipkg_remove.c - the itsy package management system
5884 +
5885 +   Carl D. Worth
5886 +
5887 +   Copyright (C) 2001 University of Southern California
5888 +
5889 +   This program is free software; you can redistribute it and/or
5890 +   modify it under the terms of the GNU General Public License as
5891 +   published by the Free Software Foundation; either version 2, or (at
5892 +   your option) any later version.
5893 +
5894 +   This program is distributed in the hope that it will be useful, but
5895 +   WITHOUT ANY WARRANTY; without even the implied warranty of
5896 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
5897 +   General Public License for more details.
5898 +*/
5899 +
5900 +#include "ipkg.h"
5901 +#include "ipkg_message.h"
5902 +
5903 +#include <glob.h>
5904 +
5905 +#include "ipkg_remove.h"
5906 +
5907 +#include "file_util.h"
5908 +#include "sprintf_alloc.h"
5909 +#include "str_util.h"
5910 +
5911 +#include "ipkg_cmd.h"
5912 +
5913 +/*
5914 + * Returns number of the number of packages depending on the packages provided by this package.
5915 + * Every package implicitly provides itself.
5916 + */
5917 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
5918 +{
5919 +     int nprovides = pkg->provides_count;
5920 +     abstract_pkg_t **provides = pkg->provides;
5921 +     int n_installed_dependents = 0;
5922 +     int i;
5923 +     for (i = 0; i <= nprovides; i++) {
5924 +         abstract_pkg_t *providee = provides[i];
5925 +         abstract_pkg_t **dependers = providee->depended_upon_by;
5926 +         abstract_pkg_t *dep_ab_pkg;
5927 +         if (dependers == NULL)
5928 +              continue;
5929 +         while ((dep_ab_pkg = *dependers++) != NULL) {
5930 +              if (dep_ab_pkg->state_status == SS_INSTALLED){
5931 +                   n_installed_dependents++;
5932 +               }
5933 +         }
5934 +
5935 +     }
5936 +     /* if caller requested the set of installed dependents */
5937 +     if (pdependents) {
5938 +         int p = 0;
5939 +         abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
5940 +
5941 +          if ( dependents == NULL ){
5942 +              fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
5943 +              return -1;  
5944 +          }
5945 +
5946 +         *pdependents = dependents;
5947 +         for (i = 0; i <= nprovides; i++) {
5948 +              abstract_pkg_t *providee = provides[i];
5949 +              abstract_pkg_t **dependers = providee->depended_upon_by;
5950 +              abstract_pkg_t *dep_ab_pkg;
5951 +              if (dependers == NULL)
5952 +                   continue;
5953 +              while ((dep_ab_pkg = *dependers++) != NULL) {
5954 +                   if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
5955 +                        dependents[p++] = dep_ab_pkg;
5956 +                        dep_ab_pkg->state_flag |= SF_MARKED;
5957 +                   }
5958 +              }
5959 +         }
5960 +         dependents[p] = NULL;
5961 +         /* now clear the marks */
5962 +         for (i = 0; i < p; i++) {
5963 +              abstract_pkg_t *dep_ab_pkg = dependents[i];
5964 +              dep_ab_pkg->state_flag &= ~SF_MARKED;
5965 +         }
5966 +     }
5967 +     return n_installed_dependents;
5968 +}
5969 +
5970 +int ipkg_remove_dependent_pkgs (ipkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
5971 +{
5972 +    int i;
5973 +    int a;
5974 +    int count;
5975 +    pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
5976 +    abstract_pkg_t * ab_pkg;
5977 +
5978 +    if((ab_pkg = pkg->parent) == NULL){
5979 +       fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
5980 +               __FUNCTION__, pkg->name);
5981 +       return 0;
5982 +    }
5983 +    
5984 +    if (dependents == NULL)
5985 +           return 0;
5986 +
5987 +    // here i am using the dependencies_checked
5988 +    if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
5989 +       return 0;                          // has already been encountered in the process
5990 +                                          // of marking packages for removal - Karthik
5991 +    ab_pkg->dependencies_checked = 2;
5992 +
5993 +    i = 0;
5994 +    count = 1;
5995 +    while (dependents [i] != NULL) {
5996 +        abstract_pkg_t *dep_ab_pkg = dependents[i];
5997 +       
5998 +       if (dep_ab_pkg->dependencies_checked == 2){
5999 +           i++;
6000 +           continue;   
6001 +        }
6002 +        if (dep_ab_pkg->state_status == SS_INSTALLED) {
6003 +            for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
6004 +                pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
6005 +                if (dep_pkg->state_status == SS_INSTALLED) {
6006 +                    pkg_vec_insert(dependent_pkgs, dep_pkg);
6007 +                    count++;
6008 +                }
6009 +            }
6010 +        }
6011 +       i++;
6012 +       /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
6013 +        * 2 - to keep track of pkgs whose deps have been checked alrdy  - Karthik */   
6014 +    }
6015 +    
6016 +    if (count == 1)
6017 +           return 0;
6018 +    
6019 +    
6020 +    for (i = 0; i < dependent_pkgs->len; i++) {
6021 +        int err = ipkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
6022 +        if (err)
6023 +            return err;
6024 +    }
6025 +    return 0;
6026 +}
6027 +
6028 +static int user_prefers_removing_dependents(ipkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
6029 +{
6030 +    abstract_pkg_t *dep_ab_pkg;
6031 +    ipkg_message(conf, IPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
6032 +    while ((dep_ab_pkg = *dependents++) != NULL) {
6033 +        if (dep_ab_pkg->state_status == SS_INSTALLED)
6034 +             ipkg_message(conf, IPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
6035 +    }
6036 +    ipkg_message(conf, IPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
6037 +    ipkg_message(conf, IPKG_ERROR, "");
6038 +    ipkg_message(conf, IPKG_ERROR, "You can force removal of this package with -force-depends.\n");
6039 +    ipkg_message(conf, IPKG_ERROR, "You can force removal of this package and its dependents\n");
6040 +    ipkg_message(conf, IPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
6041 +    ipkg_message(conf, IPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
6042 +    ipkg_message(conf, IPKG_ERROR, "in ipkg.conf.\n");
6043 +    return 0;
6044 +}
6045 +
6046 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message)
6047 +{
6048 +/* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
6049 +   thus I wan't check for essential, as I'm upgrading.
6050 +   I hope it won't break anything :) 
6051 +*/
6052 +     int err;
6053 +     abstract_pkg_t *parent_pkg = NULL;
6054 +       
6055 +     if (pkg->essential && !message) {
6056 +         if (conf->force_removal_of_essential_packages) {
6057 +              fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
6058 +                      "\tIf your system breaks, you get to keep both pieces\n",
6059 +                      pkg->name);
6060 +         } else {
6061 +              fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
6062 +                      "\tRemoving an essential package may lead to an unusable system, but if\n"
6063 +                      "\tyou enjoy that kind of pain, you can force ipkg to proceed against\n"
6064 +                      "\tits will with the option: -force-removal-of-essential-packages\n",
6065 +                      pkg->name);
6066 +              return IPKG_PKG_IS_ESSENTIAL;
6067 +         }
6068 +     }
6069 +
6070 +     if ((parent_pkg = pkg->parent) == NULL)
6071 +         return 0;
6072 +
6073 +     /* only attempt to remove dependent installed packages if
6074 +      * force_depends is not specified or the package is being
6075 +      * replaced.
6076 +      */
6077 +     if (!conf->force_depends
6078 +        && !(pkg->state_flag & SF_REPLACE)) {
6079 +         abstract_pkg_t **dependents;
6080 +         int has_installed_dependents = 
6081 +              pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
6082 +
6083 +         if (has_installed_dependents) {
6084 +              /*
6085 +               * if this package is depended up by others, then either we should
6086 +               * not remove it or we should remove it and all of its dependents 
6087 +               */
6088 +
6089 +              if (!conf->force_removal_of_dependent_packages
6090 +                  && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
6091 +                   return IPKG_PKG_HAS_DEPENDENTS;
6092 +              }
6093 +
6094 +              /* remove packages depending on this package - Karthik */
6095 +              err = ipkg_remove_dependent_pkgs (conf, pkg, dependents);
6096 +              free(dependents);
6097 +              if (err) return err;
6098 +         }
6099 +     }
6100 +
6101 +     if ( message==0 ){
6102 +         printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
6103 +         fflush(stdout);
6104 +     }
6105 +     pkg->state_flag |= SF_FILELIST_CHANGED;
6106 +
6107 +     pkg->state_want = SW_DEINSTALL;
6108 +     ipkg_state_changed++;
6109 +
6110 +     pkg_run_script(conf, pkg, "prerm", "remove");
6111 +
6112 +     /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
6113 +       maintains an empty filelist rather than deleting it. That seems
6114 +       like a big pain, and I don't see that that should make a big
6115 +       difference, but for anyone who wants tighter compatibility,
6116 +       feel free to fix this. */
6117 +     remove_data_files_and_list(conf, pkg);
6118 +
6119 +     pkg_run_script(conf, pkg, "postrm", "remove");
6120 +
6121 +     remove_maintainer_scripts_except_postrm(conf, pkg);
6122 +
6123 +     /* Aman Gupta - Since ipkg is made for handheld devices with limited
6124 +      * space, it doesn't make sense to leave extra configurations, files, 
6125 +      * and maintainer scripts left around. So, we make remove like purge, 
6126 +      * and take out all the crap :) */
6127 +
6128 +     remove_postrm(conf, pkg);
6129 +     pkg->state_status = SS_NOT_INSTALLED;
6130 +
6131 +     if (parent_pkg) 
6132 +         parent_pkg->state_status = SS_NOT_INSTALLED;
6133 +
6134 +     return 0;
6135 +}
6136 +
6137 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg)
6138 +{
6139 +    ipkg_remove_pkg(conf, pkg,0);
6140 +    return 0;
6141 +}
6142 +
6143 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg)
6144 +{
6145 +     str_list_t installed_dirs;
6146 +     str_list_t *installed_files;
6147 +     str_list_elt_t *iter;
6148 +     char *file_name;
6149 +     conffile_t *conffile;
6150 +     int removed_a_dir;
6151 +     pkg_t *owner;
6152 +
6153 +     str_list_init(&installed_dirs);
6154 +     installed_files = pkg_get_installed_files(pkg);
6155 +
6156 +     for (iter = installed_files->head; iter; iter = iter->next) {
6157 +         file_name = iter->data;
6158 +
6159 +         if (file_is_dir(file_name)) {
6160 +              str_list_append(&installed_dirs, strdup(file_name));
6161 +              continue;
6162 +         }
6163 +
6164 +         conffile = pkg_get_conffile(pkg, file_name);
6165 +         if (conffile) {
6166 +              /* XXX: QUESTION: Is this right? I figure we only need to
6167 +                 save the conffile if it has been modified. Is that what
6168 +                 dpkg does? Or does dpkg preserve all conffiles? If so,
6169 +                 this seems like a better thing to do to conserve
6170 +                 space. */
6171 +              if (conffile_has_been_modified(conf, conffile)) {
6172 +                   printf("  not deleting modified conffile %s\n", file_name);
6173 +                   fflush(stdout);
6174 +                   continue;
6175 +              }
6176 +         }
6177 +
6178 +         ipkg_message(conf, IPKG_INFO, "  deleting %s (noaction=%d)\n", file_name, conf->noaction);
6179 +         if (!conf->noaction)
6180 +              unlink(file_name);
6181 +     }
6182 +
6183 +     if (!conf->noaction) {
6184 +         do {
6185 +              removed_a_dir = 0;
6186 +              for (iter = installed_dirs.head; iter; iter = iter->next) {
6187 +                   file_name = iter->data;
6188 +           
6189 +                   if (rmdir(file_name) == 0) {
6190 +                        ipkg_message(conf, IPKG_INFO, "  deleting %s\n", file_name);
6191 +                        removed_a_dir = 1;
6192 +                        str_list_remove(&installed_dirs, &iter);
6193 +                   }
6194 +              }
6195 +         } while (removed_a_dir);
6196 +     }
6197 +
6198 +     pkg_free_installed_files(pkg);
6199 +     /* We have to remove the file list now, so that
6200 +       find_pkg_owning_file does not always just report this package */
6201 +     pkg_remove_installed_files_list(conf, pkg);
6202 +
6203 +     /* Don't print warning for dirs that are provided by other packages */
6204 +     for (iter = installed_dirs.head; iter; iter = iter->next) {
6205 +         file_name = iter->data;
6206 +
6207 +         owner = file_hash_get_file_owner(conf, file_name);
6208 +         if (owner) {
6209 +              free(iter->data);
6210 +              iter->data = NULL;
6211 +              str_list_remove(&installed_dirs, &iter);
6212 +         }
6213 +     }
6214 +
6215 +     /* cleanup */
6216 +     for (iter = installed_dirs.head; iter; iter = iter->next) {
6217 +         free(iter->data);
6218 +         iter->data = NULL;
6219 +     }
6220 +     str_list_deinit(&installed_dirs);
6221 +
6222 +     return 0;
6223 +}
6224 +
6225 +int remove_maintainer_scripts_except_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6226 +{
6227 +    int i, err;
6228 +    char *globpattern;
6229 +    glob_t globbuf;
6230 +    
6231 +    if (conf->noaction) return 0;
6232 +
6233 +    sprintf_alloc(&globpattern, "%s/%s.*",
6234 +                 pkg->dest->info_dir, pkg->name);
6235 +    err = glob(globpattern, 0, NULL, &globbuf);
6236 +    free(globpattern);
6237 +    if (err) {
6238 +       return 0;
6239 +    }
6240 +
6241 +    for (i = 0; i < globbuf.gl_pathc; i++) {
6242 +       if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
6243 +           continue;
6244 +       }
6245 +        ipkg_message(conf, IPKG_INFO, "  deleting %s\n", globbuf.gl_pathv[i]);
6246 +       unlink(globbuf.gl_pathv[i]);
6247 +    }
6248 +    globfree(&globbuf);
6249 +
6250 +    return 0;
6251 +}
6252 +
6253 +int remove_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6254 +{
6255 +    char *postrm_file_name;
6256 +
6257 +    if (conf->noaction) return 0;
6258 +
6259 +    sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
6260 +                 pkg->dest->info_dir, pkg->name);
6261 +    unlink(postrm_file_name);
6262 +    free(postrm_file_name);
6263 +
6264 +    return 0;
6265 +}
6266 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_remove.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.h
6267 --- busybox-1.2.0-orig/archival/libipkg/ipkg_remove.h   1970-01-01 01:00:00.000000000 +0100
6268 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_remove.h  2006-07-22 16:31:25.000000000 +0200
6269 @@ -0,0 +1,33 @@
6270 +/* ipkg_remove.h - the itsy package management system
6271 +
6272 +   Carl D. Worth
6273 +
6274 +   Copyright (C) 2001 University of Southern California
6275 +
6276 +   This program is free software; you can redistribute it and/or
6277 +   modify it under the terms of the GNU General Public License as
6278 +   published by the Free Software Foundation; either version 2, or (at
6279 +   your option) any later version.
6280 +
6281 +   This program is distributed in the hope that it will be useful, but
6282 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6283 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6284 +   General Public License for more details.
6285 +*/
6286 +
6287 +#ifndef IPKG_REMOVE_H
6288 +#define IPKG_REMOVE_H
6289 +
6290 +#include "pkg.h"
6291 +#include "ipkg_conf.h"
6292 +
6293 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message);
6294 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg);
6295 +int possible_broken_removal_of_packages (ipkg_conf_t *conf, pkg_t *pkg);
6296 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents);
6297 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg);
6298 +int remove_maintainer_scripts_except_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6299 +int remove_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6300 +
6301 +
6302 +#endif
6303 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.c
6304 --- busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.c  1970-01-01 01:00:00.000000000 +0100
6305 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.c 2006-07-22 16:31:25.000000000 +0200
6306 @@ -0,0 +1,77 @@
6307 +/* ipkg_upgrade.c - the itsy package management system
6308 +
6309 +   Carl D. Worth
6310 +   Copyright (C) 2001 University of Southern California
6311 +
6312 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6313 +
6314 +   This program is free software; you can redistribute it and/or
6315 +   modify it under the terms of the GNU General Public License as
6316 +   published by the Free Software Foundation; either version 2, or (at
6317 +   your option) any later version.
6318 +
6319 +   This program is distributed in the hope that it will be useful, but
6320 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6321 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6322 +   General Public License for more details.
6323 +*/
6324 +
6325 +#include "ipkg.h"
6326 +#include "ipkg_install.h"
6327 +#include "ipkg_message.h"
6328 +
6329 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old)
6330 +{
6331 +     pkg_t *new;
6332 +     int cmp;
6333 +     char *old_version, *new_version;
6334 +
6335 +     if (old->state_flag & SF_HOLD) {
6336 +          ipkg_message(conf, IPKG_NOTICE,
6337 +                       "Not upgrading package %s which is marked "
6338 +                       "hold (flags=%#x)\n", old->name, old->state_flag);
6339 +          return 0;
6340 +     }
6341 +
6342 +     new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name);
6343 +     if (new == NULL) {
6344 +          old_version = pkg_version_str_alloc(old);
6345 +          ipkg_message(conf, IPKG_NOTICE,
6346 +                       "Assuming locally installed package %s (%s) "
6347 +                       "is up to date.\n", old->name, old_version);
6348 +          free(old_version);
6349 +          return 0;
6350 +     }
6351 +          
6352 +     old_version = pkg_version_str_alloc(old);
6353 +     new_version = pkg_version_str_alloc(new);
6354 +               
6355 +     cmp = pkg_compare_versions(old, new);
6356 +     ipkg_message(conf, IPKG_DEBUG,
6357 +                  "comparing visible versions of pkg %s:"
6358 +                  "\n\t%s is installed "
6359 +                  "\n\t%s is available "
6360 +                  "\n\t%d was comparison result\n",
6361 +                  old->name, old_version, new_version, cmp);
6362 +     if (cmp == 0) {
6363 +          ipkg_message(conf, IPKG_INFO,
6364 +                       "Package %s (%s) installed in %s is up to date.\n",
6365 +                       old->name, old_version, old->dest->name);
6366 +          free(old_version);
6367 +          free(new_version);
6368 +          return 0;
6369 +     } else if (cmp > 0) {
6370 +          ipkg_message(conf, IPKG_NOTICE,
6371 +                       "Not downgrading package %s on %s from %s to %s.\n",
6372 +                       old->name, old->dest->name, old_version, new_version);
6373 +          free(old_version);
6374 +          free(new_version);
6375 +          return 0;
6376 +     } else if (cmp < 0) {
6377 +          new->dest = old->dest;
6378 +          old->state_want = SW_DEINSTALL;
6379 +     }
6380 +
6381 +     new->state_flag |= SF_USER;
6382 +     return ipkg_install_pkg(conf, new,1);
6383 +}
6384 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.h
6385 --- busybox-1.2.0-orig/archival/libipkg/ipkg_upgrade.h  1970-01-01 01:00:00.000000000 +0100
6386 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_upgrade.h 2006-07-22 16:31:25.000000000 +0200
6387 @@ -0,0 +1,18 @@
6388 +/* ipkg_upgrade.c - the itsy package management system
6389 +
6390 +   Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6391 +
6392 +   This program is free software; you can redistribute it and/or
6393 +   modify it under the terms of the GNU General Public License as
6394 +   published by the Free Software Foundation; either version 2, or (at
6395 +   your option) any later version.
6396 +
6397 +   This program is distributed in the hope that it will be useful, but
6398 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6399 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6400 +   General Public License for more details.
6401 +*/
6402 +
6403 +#include "ipkg.h"
6404 +
6405 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old);
6406 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_utils.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.c
6407 --- busybox-1.2.0-orig/archival/libipkg/ipkg_utils.c    1970-01-01 01:00:00.000000000 +0100
6408 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.c   2006-07-22 16:31:25.000000000 +0200
6409 @@ -0,0 +1,181 @@
6410 +/* ipkg_utils.c - the itsy package management system
6411 +
6412 +   Steven M. Ayer
6413 +   
6414 +   Copyright (C) 2002 Compaq Computer Corporation
6415 +
6416 +   This program is free software; you can redistribute it and/or
6417 +   modify it under the terms of the GNU General Public License as
6418 +   published by the Free Software Foundation; either version 2, or (at
6419 +   your option) any later version.
6420 +
6421 +   This program is distributed in the hope that it will be useful, but
6422 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6423 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6424 +   General Public License for more details.
6425 +*/
6426 +
6427 +#include "ipkg.h"
6428 +#include <errno.h>
6429 +#include <ctype.h>
6430 +#include <sys/vfs.h>
6431 +
6432 +#include "ipkg_utils.h"
6433 +#include "pkg.h"
6434 +#include "pkg_hash.h"
6435 +
6436 +struct errlist* error_list;
6437 +
6438 +int get_available_blocks(char * filesystem)
6439 +{
6440 +     struct statfs sfs;
6441 +
6442 +     if(statfs(filesystem, &sfs)){
6443 +         fprintf(stderr, "bad statfs\n");
6444 +         return 0;
6445 +     }
6446 +     /*    fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
6447 +     return ((sfs.f_bavail * sfs.f_bsize) / 1024);
6448 +}
6449 +
6450 +char **read_raw_pkgs_from_file(const char *file_name)
6451 +{
6452 +     FILE *fp; 
6453 +     char **ret;
6454 +    
6455 +     if(!(fp = fopen(file_name, "r"))){
6456 +         fprintf(stderr, "can't get %s open for read\n", file_name);
6457 +         return NULL;
6458 +     }
6459 +
6460 +     ret = read_raw_pkgs_from_stream(fp);
6461 +
6462 +     fclose(fp);
6463 +
6464 +     return ret;
6465 +}
6466 +
6467 +char **read_raw_pkgs_from_stream(FILE *fp)
6468 +{    
6469 +     char **raw = NULL, *buf, *scout;
6470 +     int count = 0;
6471 +     size_t size = 512;
6472 +     
6473 +     buf = malloc (size);
6474 +
6475 +     while (fgets(buf, size, fp)) {
6476 +         while (strlen (buf) == (size - 1)
6477 +                && buf[size-2] != '\n') {
6478 +              size_t o = size - 1;
6479 +              size *= 2;
6480 +              buf = realloc (buf, size);
6481 +              if (fgets (buf + o, size - o, fp) == NULL)
6482 +                   break;
6483 +         }
6484 +         
6485 +         if(!(count % 50))
6486 +              raw = realloc(raw, (count + 50) * sizeof(char *));
6487 +       
6488 +         if((scout = strchr(buf, '\n')))
6489 +              *scout = '\0';
6490 +
6491 +         raw[count++] = strdup(buf);
6492 +     }
6493 +    
6494 +     raw = realloc(raw, (count + 1) * sizeof(char *));
6495 +     raw[count] = NULL;
6496 +
6497 +     free (buf);
6498 +    
6499 +     return raw;
6500 +}
6501 +
6502 +/* something to remove whitespace, a hash pooper */
6503 +char *trim_alloc(char *line)
6504 +{
6505 +     char *new; 
6506 +     char *dest, *src, *end;
6507 +    
6508 +     new = malloc(strlen(line) + 1);
6509 +     if ( new == NULL ){
6510 +        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
6511 +        return NULL;
6512 +     }
6513 +     dest = new, src = line, end = line + (strlen(line) - 1);
6514 +
6515 +     /* remove it from the front */    
6516 +     while(src && 
6517 +          isspace(*src) &&
6518 +          *src)
6519 +         src++;
6520 +     /* and now from the back */
6521 +     while((end > src) &&
6522 +          isspace(*end))
6523 +         end--;
6524 +     end++;
6525 +     *end = '\0';
6526 +     strcpy(new, src);
6527 +     /* this does from the first space
6528 +      *  blasting away any versions stuff in depends
6529 +      while(src && 
6530 +      !isspace(*src) &&
6531 +      *src)
6532 +      *dest++ = *src++;
6533 +      *dest = '\0';
6534 +      */
6535 +    
6536 +     return new;
6537 +}
6538 +
6539 +int line_is_blank(const char *line)
6540 +{
6541 +     const char *s;
6542 +
6543 +     for (s = line; *s; s++) {
6544 +         if (!isspace(*s))
6545 +              return 0;
6546 +     }
6547 +     return 1;
6548 +}
6549 +
6550 +void push_error_list(struct errlist ** errors, char * msg){
6551 +  struct errlist *err_lst_tmp;
6552 +
6553 +
6554 +  err_lst_tmp = malloc ( sizeof (err_lst_tmp) );
6555 +  err_lst_tmp->errmsg=strdup(msg) ;
6556 +  err_lst_tmp->next = *errors;
6557 +  *errors = err_lst_tmp;
6558 +}
6559 +
6560 +
6561 +void reverse_error_list(struct errlist **errors){
6562 +   struct errlist *result=NULL;
6563 +   struct errlist *current= *errors;
6564 +   struct errlist *next;
6565 +
6566 +   while ( current != NULL ) {
6567 +      next = current->next;
6568 +      current->next=result;
6569 +      result=current;
6570 +      current=next;
6571 +   }
6572 +   *errors=result;
6573 +
6574 +}
6575 +
6576 +       
6577 +void free_error_list(struct errlist **errors){
6578 +  struct errlist *current = *errors;
6579 +
6580 +    while (current != NULL) {
6581 +      free(current->errmsg);
6582 +      current = (*errors)->next;
6583 +      free(*errors);
6584 +      *errors = current;
6585 +    }
6586 +
6587 +
6588 +}
6589 +
6590 +       
6591 diff -ruN busybox-1.2.0-orig/archival/libipkg/ipkg_utils.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.h
6592 --- busybox-1.2.0-orig/archival/libipkg/ipkg_utils.h    1970-01-01 01:00:00.000000000 +0100
6593 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/ipkg_utils.h   2006-07-22 16:31:25.000000000 +0200
6594 @@ -0,0 +1,29 @@
6595 +/* ipkg_utils.h - the itsy package management system
6596 +
6597 +   Steven M. Ayer
6598 +   
6599 +   Copyright (C) 2002 Compaq Computer Corporation
6600 +
6601 +   This program is free software; you can redistribute it and/or
6602 +   modify it under the terms of the GNU General Public License as
6603 +   published by the Free Software Foundation; either version 2, or (at
6604 +   your option) any later version.
6605 +
6606 +   This program is distributed in the hope that it will be useful, but
6607 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6608 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6609 +   General Public License for more details.
6610 +*/
6611 +
6612 +#ifndef IPKG_UTILS_H
6613 +#define IPKG_UTILS_H
6614 +
6615 +#include "pkg.h"
6616 +
6617 +int get_available_blocks(char * filesystem);
6618 +char **read_raw_pkgs_from_file(const char *file_name);
6619 +char **read_raw_pkgs_from_stream(FILE *fp);
6620 +char *trim_alloc(char * line);
6621 +int line_is_blank(const char *line);
6622 +
6623 +#endif
6624 diff -ruN busybox-1.2.0-orig/archival/libipkg/libipkg.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.c
6625 --- busybox-1.2.0-orig/archival/libipkg/libipkg.c       1970-01-01 01:00:00.000000000 +0100
6626 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.c      2006-07-22 16:31:25.000000000 +0200
6627 @@ -0,0 +1,527 @@
6628 +/* ipkglib.c - the itsy package management system
6629 +
6630 +   Florina Boor
6631 +
6632 +   Copyright (C) 2003 kernel concepts
6633 +
6634 +   This program is free software; you can redistribute it and/or
6635 +   modify it under the terms of the GNU General Public License as
6636 +   published by the Free Software Foundation; either version 2, or (at
6637 +   your option) any later version.
6638 +
6639 +   This program is distributed in the hope that it will be useful, but
6640 +   WITHOUT ANY WARRANTY; without even the implied warranty of
6641 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
6642 +   General Public License for more details.
6643 +*/
6644 +
6645 +#ifdef IPKG_LIB
6646 +
6647 +#include "ipkg.h"
6648 +#include "ipkg_includes.h"
6649 +#include "libipkg.h"
6650 +
6651 +#include "args.h"
6652 +#include "ipkg_conf.h"
6653 +#include "ipkg_cmd.h"
6654 +#include "file_util.h"
6655 +
6656 +
6657 +
6658 +ipkg_message_callback ipkg_cb_message = NULL;
6659 +ipkg_response_callback ipkg_cb_response = NULL;
6660 +ipkg_status_callback ipkg_cb_status = NULL;
6661 +ipkg_list_callback ipkg_cb_list = NULL;
6662 +
6663 +
6664 +int
6665 +ipkg_init (ipkg_message_callback mcall, 
6666 +           ipkg_response_callback rcall,
6667 +           args_t * args)
6668 +{
6669 +       ipkg_cb_message = mcall;
6670 +       ipkg_cb_response = rcall;
6671 +
6672 +       args_init (args);
6673 +
6674 +       return 0;
6675 +}
6676 +
6677 +
6678 +int
6679 +ipkg_deinit (args_t * args)
6680 +{
6681 +       args_deinit (args);
6682 +       ipkg_cb_message = NULL;
6683 +       ipkg_cb_response = NULL;
6684 +
6685 +       /* place other cleanup stuff here */
6686 +
6687 +       return 0;
6688 +}
6689 +
6690 +
6691 +int
6692 +ipkg_packages_list(args_t *args, 
6693 +                   const char *packages, 
6694 +                   ipkg_list_callback cblist,
6695 +                   void *userdata)
6696 +{
6697 +       ipkg_cmd_t *cmd;
6698 +       ipkg_conf_t ipkg_conf;
6699 +       int err;
6700 +
6701 +       err = ipkg_conf_init (&ipkg_conf, args);
6702 +       if (err)
6703 +       {
6704 +               return err;
6705 +       }
6706 +
6707 +       ipkg_cb_list = cblist;
6708 +       /* we need to do this because of static declarations, 
6709 +        * maybe a good idea to change */
6710 +       cmd = ipkg_cmd_find ("list");
6711 +       if (packages)
6712 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6713 +       else
6714 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6715 +       ipkg_cb_list = NULL;
6716 +       ipkg_conf_deinit (&ipkg_conf);
6717 +       return (err);
6718 +}
6719 +
6720 +
6721 +int
6722 +ipkg_packages_status(args_t *args,
6723 +                     const char *packages,
6724 +                     ipkg_status_callback cbstatus,
6725 +                     void *userdata)
6726 +{
6727 +       ipkg_cmd_t *cmd;
6728 +       ipkg_conf_t ipkg_conf;
6729 +       int err;
6730 +
6731 +       err = ipkg_conf_init (&ipkg_conf, args);
6732 +       if (err)
6733 +       {
6734 +               return err;
6735 +       }
6736 +
6737 +       ipkg_cb_status = cbstatus;
6738 +
6739 +       /* we need to do this because of static declarations,
6740 +        * maybe a good idea to change */
6741 +       cmd = ipkg_cmd_find ("status");
6742 +       if (packages)
6743 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6744 +       else
6745 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6746 +
6747 +       ipkg_cb_status = NULL;
6748 +       ipkg_conf_deinit (&ipkg_conf);
6749 +       return (err);
6750 +}
6751 +
6752 +
6753 +int
6754 +ipkg_packages_info(args_t *args,
6755 +                   const char *packages,
6756 +                   ipkg_status_callback cbstatus,
6757 +                   void *userdata)
6758 +{
6759 +       ipkg_cmd_t *cmd;
6760 +       ipkg_conf_t ipkg_conf;
6761 +       int err;
6762 +
6763 +       err = ipkg_conf_init (&ipkg_conf, args);
6764 +       if (err)
6765 +       {
6766 +               return err;
6767 +       }
6768 +
6769 +       ipkg_cb_status = cbstatus;
6770 +
6771 +       /* we need to do this because of static declarations,
6772 +        * maybe a good idea to change */
6773 +       cmd = ipkg_cmd_find ("info");
6774 +       if (packages)
6775 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6776 +       else
6777 +               err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6778 +
6779 +       ipkg_cb_status = NULL;
6780 +       ipkg_conf_deinit (&ipkg_conf);
6781 +       return (err);
6782 +}
6783 +
6784 +
6785 +int
6786 +ipkg_packages_install (args_t * args, const char *name)
6787 +{
6788 +       ipkg_cmd_t *cmd;
6789 +       ipkg_conf_t ipkg_conf;
6790 +       int err;
6791 +
6792 +       /* this error should be handled in application */
6793 +       if (!name || !strlen (name))
6794 +               return (-1);
6795 +
6796 +       err = ipkg_conf_init (&ipkg_conf, args);
6797 +       if (err)
6798 +       {
6799 +               return err;
6800 +       }
6801 +
6802 +       /* we need to do this because of static declarations,
6803 +        * maybe a good idea to change */
6804 +       cmd = ipkg_cmd_find ("install");
6805 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6806 +
6807 +       ipkg_conf_deinit(&ipkg_conf);
6808 +       return (err);
6809 +}
6810 +
6811 +
6812 +int
6813 +ipkg_packages_remove(args_t *args, const char *name, int purge)
6814 +{
6815 +       ipkg_cmd_t *cmd;
6816 +       ipkg_conf_t ipkg_conf;
6817 +       int err;
6818 +
6819 +       /* this error should be handled in application */
6820 +       if (!name || !strlen (name))
6821 +               return (-1);
6822 +
6823 +       err = ipkg_conf_init (&ipkg_conf, args);
6824 +       if (err)
6825 +       {
6826 +               return err;
6827 +       }
6828 +
6829 +       /* we need to do this because of static declarations, 
6830 +        * maybe a good idea to change */
6831 +       if (purge)
6832 +               cmd = ipkg_cmd_find ("purge");
6833 +       else
6834 +               cmd = ipkg_cmd_find ("remove");
6835 +
6836 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6837 +       
6838 +       ipkg_conf_deinit(&ipkg_conf);
6839 +       return (err);
6840 +}
6841 +
6842 +
6843 +int 
6844 +ipkg_lists_update(args_t *args)
6845 +{
6846 +       ipkg_cmd_t *cmd;
6847 +       ipkg_conf_t ipkg_conf;
6848 +       int err;
6849 +
6850 +       err = ipkg_conf_init (&ipkg_conf, args);
6851 +       if (err)
6852 +       {
6853 +               return err;
6854 +       }
6855 +
6856 +       /* we need to do this because of static declarations, 
6857 +        * maybe a good idea to change */
6858 +       cmd = ipkg_cmd_find ("update");
6859 +
6860 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6861 +       
6862 +       ipkg_conf_deinit(&ipkg_conf);
6863 +       return (err);
6864 +}
6865 +
6866 +
6867 +int 
6868 +ipkg_packages_upgrade(args_t *args)
6869 +{
6870 +       ipkg_cmd_t *cmd;
6871 +       ipkg_conf_t ipkg_conf;
6872 +       int err;
6873 +
6874 +       err = ipkg_conf_init (&ipkg_conf, args);
6875 +       if (err)
6876 +       {
6877 +               return err;
6878 +       }
6879 +
6880 +       /* we need to do this because of static declarations, 
6881 +        * maybe a good idea to change */
6882 +       cmd = ipkg_cmd_find ("upgrade");
6883 +
6884 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6885 +       
6886 +       ipkg_conf_deinit(&ipkg_conf);
6887 +       return (err);
6888 +}
6889 +
6890 +
6891 +int
6892 +ipkg_packages_download (args_t * args, const char *name)
6893 +{
6894 +       ipkg_cmd_t *cmd;
6895 +       ipkg_conf_t ipkg_conf;
6896 +       int err;
6897 +
6898 +       /* this error should be handled in application */
6899 +       if (!name || !strlen (name))
6900 +               return (-1);
6901 +
6902 +       err = ipkg_conf_init (&ipkg_conf, args);
6903 +       if (err)
6904 +       {
6905 +               return err;
6906 +       }
6907 +
6908 +       /* we need to do this because of static declarations,
6909 +        * maybe a good idea to change */
6910 +       cmd = ipkg_cmd_find ("download");
6911 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6912 +
6913 +       ipkg_conf_deinit(&ipkg_conf);
6914 +       return (err);
6915 +}
6916 +
6917 +
6918 +int
6919 +ipkg_package_files(args_t *args, 
6920 +                   const char *name, 
6921 +                   ipkg_list_callback cblist,
6922 +                   void *userdata)
6923 +{
6924 +       ipkg_cmd_t *cmd;
6925 +       ipkg_conf_t ipkg_conf;
6926 +       int err;
6927 +
6928 +       /* this error should be handled in application */
6929 +       if (!name || !strlen (name))
6930 +               return (-1);
6931 +
6932 +       err = ipkg_conf_init (&ipkg_conf, args);
6933 +       if (err)
6934 +       {
6935 +               return err;
6936 +       }
6937 +
6938 +       ipkg_cb_list = cblist;
6939 +       
6940 +       /* we need to do this because of static declarations, 
6941 +        * maybe a good idea to change */
6942 +       cmd = ipkg_cmd_find ("files");
6943 +
6944 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, userdata);
6945 +       
6946 +       ipkg_cb_list = NULL;
6947 +       ipkg_conf_deinit(&ipkg_conf);
6948 +       return (err);
6949 +}
6950 +
6951 +
6952 +int 
6953 +ipkg_file_search(args_t *args, 
6954 +                const char *file,
6955 +                               ipkg_list_callback cblist,
6956 +                void *userdata)
6957 +{
6958 +       ipkg_cmd_t *cmd;
6959 +       ipkg_conf_t ipkg_conf;
6960 +       int err;
6961 +       
6962 +       /* this error should be handled in application */
6963 +       if (!file || !strlen (file))
6964 +               return (-1);
6965 +
6966 +       err = ipkg_conf_init (&ipkg_conf, args);
6967 +       if (err)
6968 +       {
6969 +               return err;
6970 +       }
6971 +
6972 +       ipkg_cb_list = cblist;
6973 +
6974 +       /* we need to do this because of static declarations, 
6975 +        * maybe a good idea to change */
6976 +       cmd = ipkg_cmd_find ("search");
6977 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, userdata);
6978 +       
6979 +       ipkg_cb_list = NULL;
6980 +       ipkg_conf_deinit(&ipkg_conf);
6981 +       return(err);
6982 +}
6983 +
6984 +
6985 +int 
6986 +ipkg_file_what(args_t *args, const char *file, const char* command)
6987 +{
6988 +       ipkg_cmd_t *cmd;
6989 +       ipkg_conf_t ipkg_conf;
6990 +       int err;
6991 +       
6992 +       /* this error should be handled in application */
6993 +       if (!file || !strlen (file))
6994 +               return (-1);
6995 +
6996 +       err = ipkg_conf_init (&ipkg_conf, args);
6997 +       if (err)
6998 +       {
6999 +               return err;
7000 +       }
7001 +
7002 +       /* we need to do this because of static declarations, 
7003 +        * maybe a good idea to change */
7004 +       cmd = ipkg_cmd_find (command);
7005 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, NULL);
7006 +       
7007 +       ipkg_conf_deinit(&ipkg_conf);
7008 +       return(err);
7009 +}
7010 +
7011 +#define ipkg_package_whatdepends(args,file) ipkg_file_what(args,file,"whatdepends")
7012 +#define ipkg_package_whatrecommends(args, file) ipkg_file_what(args,file,"whatrecommends")
7013 +#define ipkg_package_whatprovides(args, file) ipkg_file_what(args,file,"whatprovides")
7014 +#define ipkg_package_whatconflicts(args, file) ipkg_file_what(args,file,"whatconflicts")
7015 +#define ipkg_package_whatreplaces(args, file) ipkg_file_what(args,file,"whatreplaces")
7016 +
7017 +
7018 +int default_ipkg_message_callback(ipkg_conf_t *conf, message_level_t level, 
7019 +                                 char *msg)
7020 +{
7021 +     if (conf && (conf->verbosity < level)) {
7022 +         return 0;
7023 +     } else {
7024 +#ifdef IPKG_LIB
7025 +          if ( level == IPKG_ERROR ){
7026 +             push_error_list(&error_list, msg); 
7027 +//          printf(msg);
7028 +          } else
7029 +#endif
7030 +            printf(msg);
7031 +     }
7032 +     return 0;
7033 +}
7034 +
7035 +int default_ipkg_list_callback(char *name, char *desc, char *version, 
7036 +                              pkg_state_status_t status, void *userdata)
7037 +{
7038 +     if (desc)
7039 +         printf("%s - %s - %s\n", name, version, desc);
7040 +     else
7041 +         printf("%s - %s\n", name, version);
7042 +     return 0;
7043 +}
7044 +
7045 +int default_ipkg_files_callback(char *name, char *desc, char *version,
7046 +                   pkg_state_status_t status, void *userdata)
7047 +{
7048 +     if (desc)
7049 +         printf("%s\n", desc);
7050 +     return 0;
7051 +}
7052 +
7053 +int default_ipkg_status_callback(char *name, int istatus, char *desc,
7054 +                                void *userdata)
7055 +{
7056 +     printf("%s\n", desc);
7057 +     return 0;
7058 +}
7059 +
7060 +char* default_ipkg_response_callback(char *question)
7061 +{
7062 +     char *response = NULL;
7063 +     printf(question);
7064 +     fflush(stdout);
7065 +     do {
7066 +         response = (char *)file_read_line_alloc(stdin);
7067 +     } while (response == NULL);
7068 +     return response;
7069 +}
7070 +
7071 +/* This is used for backward compatibility */
7072 +int
7073 +ipkg_op (int argc, char *argv[])
7074 +{
7075 +       int err, optind;
7076 +       args_t args;
7077 +       char *cmd_name;
7078 +       ipkg_cmd_t *cmd;
7079 +       ipkg_conf_t ipkg_conf;
7080 +
7081 +       args_init (&args);
7082 +
7083 +       optind = args_parse (&args, argc, argv);
7084 +       if (optind == argc || optind < 0)
7085 +       {
7086 +               args_usage ("ipkg must have one sub-command argument");
7087 +       }
7088 +
7089 +       cmd_name = argv[optind++];
7090 +/* Pigi: added a flag to disable the checking of structures if the command does not need to 
7091 +         read anything from there.
7092 +*/
7093 +        if ( !strcmp(cmd_name,"print-architecture") ||
7094 +             !strcmp(cmd_name,"print_architecture") ||
7095 +             !strcmp(cmd_name,"print-installation-architecture") ||
7096 +             !strcmp(cmd_name,"print_installation_architecture") )
7097 +           args.nocheckfordirorfile = 1;
7098 +
7099 +/* Pigi: added a flag to disable the reading of feed files  if the command does not need to 
7100 +         read anything from there.
7101 +*/
7102 +        if ( !strcmp(cmd_name,"flag") ||
7103 +             !strcmp(cmd_name,"configure") ||
7104 +             !strcmp(cmd_name,"remove") ||
7105 +             !strcmp(cmd_name,"files") ||
7106 +             !strcmp(cmd_name,"search") ||
7107 +             !strcmp(cmd_name,"compare_versions") ||
7108 +             !strcmp(cmd_name,"compare-versions") ||
7109 +             !strcmp(cmd_name,"list_installed") ||
7110 +             !strcmp(cmd_name,"list-installed") ||
7111 +             !strcmp(cmd_name,"status") )
7112 +           args.noreadfeedsfile = 1;
7113 +
7114 +
7115 +       err = ipkg_conf_init (&ipkg_conf, &args);
7116 +       if (err)
7117 +       {
7118 +               return err;
7119 +       }
7120 +
7121 +       args_deinit (&args);
7122 +
7123 +       ipkg_cb_message = default_ipkg_message_callback;
7124 +       ipkg_cb_response = default_ipkg_response_callback;
7125 +       ipkg_cb_status = default_ipkg_status_callback;
7126 +       if ( strcmp(cmd_name, "files")==0)
7127 +            ipkg_cb_list = default_ipkg_files_callback;
7128 +       else
7129 +            ipkg_cb_list = default_ipkg_list_callback;
7130 +
7131 +       cmd = ipkg_cmd_find (cmd_name);
7132 +       if (cmd == NULL)
7133 +       {
7134 +               fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
7135 +                        cmd_name);
7136 +               args_usage (NULL);
7137 +       }
7138 +
7139 +       if (cmd->requires_args && optind == argc)
7140 +       {
7141 +               fprintf (stderr,
7142 +                        "%s: the ``%s'' command requires at least one argument\n",
7143 +                        __FUNCTION__, cmd_name);
7144 +               args_usage (NULL);
7145 +       }
7146 +
7147 +       err = ipkg_cmd_exec (cmd, &ipkg_conf, argc - optind, (const char **) (argv + optind), NULL);
7148 +
7149 +       ipkg_conf_deinit (&ipkg_conf);
7150 +
7151 +       return err;
7152 +}
7153 +
7154 +#endif /* IPKG_LIB */
7155 diff -ruN busybox-1.2.0-orig/archival/libipkg/libipkg.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.h
7156 --- busybox-1.2.0-orig/archival/libipkg/libipkg.h       1970-01-01 01:00:00.000000000 +0100
7157 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/libipkg.h      2006-07-22 16:31:25.000000000 +0200
7158 @@ -0,0 +1,87 @@
7159 +/* ipkglib.h - the itsy package management system
7160 +
7161 +   Florian Boor <florian.boor@kernelconcepts.de>
7162 +
7163 +   This program is free software; you can redistribute it and/or
7164 +   modify it under the terms of the GNU General Public License as
7165 +   published by the Free Software Foundation; either version 2, or (at
7166 +   your option) any later version.
7167 +
7168 +   This program is distributed in the hope that it will be useful, but
7169 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7170 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7171 +   General Public License for more details.
7172 +*/
7173 +
7174 +#ifndef IPKGLIB_H
7175 +#define IPKGLIB_H
7176 +
7177 +#ifdef IPKG_LIB
7178 +
7179 +#include "ipkg_conf.h"
7180 +#include "ipkg_message.h"
7181 +
7182 +#include "args.h"
7183 +#include "pkg.h"
7184 +
7185 +typedef int (*ipkg_message_callback)(ipkg_conf_t *conf, message_level_t level, 
7186 +       char *msg);
7187 +typedef int (*ipkg_list_callback)(char *name, char *desc, char *version, 
7188 +       pkg_state_status_t status, void *userdata);
7189 +typedef int (*ipkg_status_callback)(char *name, int istatus, char *desc,
7190 +       void *userdata);
7191 +typedef char* (*ipkg_response_callback)(char *question);
7192 +
7193 +extern int ipkg_op(int argc, char *argv[]); /* ipkglib.c */
7194 +extern int ipkg_init (ipkg_message_callback mcall, 
7195 +                      ipkg_response_callback rcall,
7196 +                                         args_t * args);
7197 +
7198 +extern int ipkg_deinit (args_t *args);
7199 +extern int ipkg_packages_list(args_t *args, 
7200 +                              const char *packages, 
7201 +                              ipkg_list_callback cblist,
7202 +                              void *userdata);
7203 +extern int ipkg_packages_status(args_t *args, 
7204 +                                const char *packages, 
7205 +                                ipkg_status_callback cbstatus,
7206 +                                                               void *userdata);
7207 +extern int ipkg_packages_info(args_t *args,
7208 +                              const char *packages,
7209 +                              ipkg_status_callback cbstatus,
7210 +                              void *userdata);
7211 +extern int ipkg_packages_install(args_t *args, const char *name);
7212 +extern int ipkg_packages_remove(args_t *args, const char *name, int purge);
7213 +extern int ipkg_lists_update(args_t *args);
7214 +extern int ipkg_packages_upgrade(args_t *args);
7215 +extern int ipkg_packages_download(args_t *args, const char *name);
7216 +extern int ipkg_package_files(args_t *args,
7217 +                              const char *name,
7218 +                                                         ipkg_list_callback cblist,
7219 +                                                         void *userdata);
7220 +extern int ipkg_file_search(args_t *args,
7221 +                            const char *file,
7222 +                                                       ipkg_list_callback cblist,
7223 +                                                       void *userdata);
7224 +extern int ipkg_package_whatdepends(args_t *args, const char *file);
7225 +extern int ipkg_package_whatrecommends(args_t *args, const char *file);
7226 +extern int ipkg_package_whatprovides(args_t *args, const char *file);
7227 +extern int ipkg_package_whatconflicts(args_t *args, const char *file);
7228 +extern int ipkg_package_whatreplaces(args_t *args, const char *file);
7229 +
7230 +extern ipkg_message_callback ipkg_cb_message; /* ipkglib.c */
7231 +extern ipkg_response_callback ipkg_cb_response;
7232 +extern ipkg_status_callback ipkg_cb_status;
7233 +extern ipkg_list_callback ipkg_cb_list;
7234 +extern void push_error_list(struct errlist **errors,char * msg);
7235 +extern void reverse_error_list(struct errlist **errors);
7236 +extern void free_error_list(struct errlist **errors);
7237 +
7238 +#else
7239 +
7240 +extern int ipkg_op(int argc, char *argv[]);
7241 +
7242 +#endif
7243 +
7244 +
7245 +#endif
7246 diff -ruN busybox-1.2.0-orig/archival/libipkg/Makefile busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile
7247 --- busybox-1.2.0-orig/archival/libipkg/Makefile        1970-01-01 01:00:00.000000000 +0100
7248 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile       2006-07-22 16:31:25.000000000 +0200
7249 @@ -0,0 +1,32 @@
7250 +# Makefile for busybox
7251 +#
7252 +# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7253 +#
7254 +# This program is free software; you can redistribute it and/or modify
7255 +# it under the terms of the GNU General Public License as published by
7256 +# the Free Software Foundation; either version 2 of the License, or
7257 +# (at your option) any later version.
7258 +#
7259 +# This program is distributed in the hope that it will be useful,
7260 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7261 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7262 +# General Public License for more details.
7263 +#
7264 +# You should have received a copy of the GNU General Public License
7265 +# along with this program; if not, write to the Free Software
7266 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7267 +#
7268 +
7269 +top_srcdir=../..
7270 +top_builddir=../..
7271 +srcdir=$(top_srcdir)/archival/libipkg
7272 +LIBIPKG_DIR:=./
7273 +include $(top_builddir)/Rules.mak
7274 +include $(top_builddir)/.config
7275 +include $(srcdir)/Makefile.in
7276 +all: $(libraries-y)
7277 +-include $(top_builddir)/.depend
7278 +
7279 +clean:
7280 +       rm -f *.o *.a $(AR_TARGET)
7281 +
7282 diff -ruN busybox-1.2.0-orig/archival/libipkg/Makefile.in busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile.in
7283 --- busybox-1.2.0-orig/archival/libipkg/Makefile.in     1970-01-01 01:00:00.000000000 +0100
7284 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/Makefile.in    2006-07-22 16:31:25.000000000 +0200
7285 @@ -0,0 +1,85 @@
7286 +# Makefile for busybox
7287 +#
7288 +# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7289 +#
7290 +# This program is free software; you can redistribute it and/or modify
7291 +# it under the terms of the GNU General Public License as published by
7292 +# the Free Software Foundation; either version 2 of the License, or
7293 +# (at your option) any later version.
7294 +#
7295 +# This program is distributed in the hope that it will be useful,
7296 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
7297 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7298 +# General Public License for more details.
7299 +#
7300 +# You should have received a copy of the GNU General Public License
7301 +# along with this program; if not, write to the Free Software
7302 +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
7303 +#
7304 +
7305 +LIBIPKG_AR:=libipkg.a
7306 +ifndef $(LIBIPKG_DIR)
7307 +LIBIPKG_DIR:=$(top_builddir)/archival/libipkg/
7308 +endif
7309 +srcdir=$(top_srcdir)/archival/libipkg
7310 +
7311 +LIBIPKG_CORE_SOURCES:= \
7312 +       args.c \
7313 +       libipkg.c \
7314 +       user.c \
7315 +
7316 +LIBIPKG_CMD_SOURCES:= \
7317 +       ipkg_cmd.c \
7318 +       ipkg_configure.c \
7319 +       ipkg_download.c \
7320 +       ipkg_install.c \
7321 +       ipkg_remove.c \
7322 +       ipkg_upgrade.c \
7323 +
7324 +LIBIPKG_DB_SOURCES:= \
7325 +       hash_table.c \
7326 +       ipkg_conf.c \
7327 +       ipkg_utils.c \
7328 +       pkg.c \
7329 +       pkg_depends.c \
7330 +       pkg_extract.c \
7331 +       pkg_hash.c \
7332 +       pkg_parse.c \
7333 +       pkg_vec.c \
7334 +
7335 +LIBIPKG_LIST_SOURCES:= \
7336 +       conffile.c \
7337 +       conffile_list.c \
7338 +       nv_pair.c \
7339 +       nv_pair_list.c \
7340 +       pkg_dest.c \
7341 +       pkg_dest_list.c \
7342 +       pkg_src.c \
7343 +       pkg_src_list.c \
7344 +       str_list.c \
7345 +       void_list.c \
7346 +
7347 +LIBIPKG_UTIL_SOURCES:= \
7348 +       file_util.c \
7349 +       ipkg_message.c \
7350 +       md5.c \
7351 +       str_util.c \
7352 +       xsystem.c \
7353 +
7354 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_CORE_SOURCES)
7355 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_CMD_SOURCES)
7356 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_DB_SOURCES)
7357 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_LIST_SOURCES)
7358 +LIBIPKG-$(CONFIG_IPKG) += $(LIBIPKG_UTIL_SOURCES)
7359 +LIBIPKG_OBJS=$(patsubst %.c,$(LIBIPKG_DIR)%.o, $(LIBIPKG-y))
7360 +
7361 +CFLAGS += -DIPKG_LIB -DIPKGLIBDIR="\"/usr/lib\"" -DHOST_CPU_STR="\"$(TARGET_ARCH)\""
7362 +
7363 +libraries-$(CONFIG_IPKG) += $(LIBIPKG_DIR)$(LIBIPKG_AR)
7364 +
7365 +$(LIBIPKG_DIR)$(LIBIPKG_AR): $(LIBIPKG_OBJS)
7366 +       $(do_ar)
7367 +
7368 +$(LIBIPKG_OBJS): $(LIBIPKG_DIR)%.o : $(srcdir)/%.c
7369 +       $(compile.c)
7370 +
7371 diff -ruN busybox-1.2.0-orig/archival/libipkg/md5.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.c
7372 --- busybox-1.2.0-orig/archival/libipkg/md5.c   1970-01-01 01:00:00.000000000 +0100
7373 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.c  2006-07-22 16:31:25.000000000 +0200
7374 @@ -0,0 +1,48 @@
7375 +/* md5.c - wrappers to busybox md5 functions
7376 + *
7377 + * Copyright (C) 1995-1999 Free Software Foundation, Inc.
7378 + *
7379 + * This program is free software; you can redistribute it and/or modify
7380 + * it under the terms of the GNU General Public License as published by
7381 + * the Free Software Foundation; either version 2, or (at your option)
7382 + * any later version.
7383 + *
7384 + * This program is distributed in the hope that it will be useful,
7385 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7386 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7387 + * GNU General Public License for more details.
7388 + *
7389 + * You should have received a copy of the GNU General Public License
7390 + * along with this program; if not, write to the Free Software Foundation,
7391 + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7392 + */
7393 +
7394 +#include <stdio.h>
7395 +#include "libbb.h"
7396 +
7397 +#include "md5.h"
7398 +
7399 +int md5_stream(FILE *stream, void *resblock)
7400 +{
7401 +       int fd;
7402 +       int sum;
7403 +       
7404 +       if( (fd = fileno(stream)) == -1 ) {
7405 +               bb_error_msg("bad file descriptor");
7406 +               return 1;
7407 +       }
7408 +
7409 +       hash_fd(fd, HASH_MD5, (uint8_t *)resblock);
7410 +
7411 +       return 0;
7412 +}
7413 +
7414 +void *md5_buffer(const char *buffer, size_t len, void *resblock)
7415 +{
7416 +       md5_ctx_t md5_cx;
7417 +
7418 +       md5_begin(&md5_cx);
7419 +       md5_hash(buffer, len, &md5_cx);
7420 +       return md5_end(resblock, &md5_cx);
7421 +}
7422 +
7423 diff -ruN busybox-1.2.0-orig/archival/libipkg/md5.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.h
7424 --- busybox-1.2.0-orig/archival/libipkg/md5.h   1970-01-01 01:00:00.000000000 +0100
7425 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/md5.h  2006-07-22 16:31:25.000000000 +0200
7426 @@ -0,0 +1,35 @@
7427 +/* md5.h - Compute MD5 checksum of files or strings according to the
7428 + *         definition of MD5 in RFC 1321 from April 1992.
7429 + * Copyright (C) 1995-1999 Free Software Foundation, Inc.
7430 + *
7431 + * This program is free software; you can redistribute it and/or modify
7432 + * it under the terms of the GNU General Public License as published by
7433 + * the Free Software Foundation; either version 2, or (at your option)
7434 + * any later version.
7435 + *
7436 + * This program is distributed in the hope that it will be useful,
7437 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7438 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7439 + * GNU General Public License for more details.
7440 + *
7441 + * You should have received a copy of the GNU General Public License
7442 + * along with this program; if not, write to the Free Software Foundation,
7443 + * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
7444 + */
7445 +
7446 +#ifndef MD5_H
7447 +#define MD5_H
7448 +
7449 +/* Compute MD5 message digest for bytes read from STREAM.  The
7450 +   resulting message digest number will be written into the 16 bytes
7451 +   beginning at RESBLOCK.  */
7452 +int md5_stream(FILE *stream, void *resblock);
7453 +
7454 +/* Compute MD5 message digest for LEN bytes beginning at BUFFER.  The
7455 +   result is always in little endian byte order, so that a byte-wise
7456 +   output yields to the wanted ASCII representation of the message
7457 +   digest.  */
7458 +void *md5_buffer(const char *buffer, size_t len, void *resblock);
7459 +
7460 +#endif
7461 +
7462 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.c
7463 --- busybox-1.2.0-orig/archival/libipkg/nv_pair.c       1970-01-01 01:00:00.000000000 +0100
7464 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.c      2006-07-22 16:31:25.000000000 +0200
7465 @@ -0,0 +1,40 @@
7466 +/* nv_pair.c - the itsy package management system
7467 +
7468 +   Carl D. Worth
7469 +
7470 +   Copyright (C) 2001 University of Southern California
7471 +
7472 +   This program is free software; you can redistribute it and/or
7473 +   modify it under the terms of the GNU General Public License as
7474 +   published by the Free Software Foundation; either version 2, or (at
7475 +   your option) any later version.
7476 +
7477 +   This program is distributed in the hope that it will be useful, but
7478 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7479 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7480 +   General Public License for more details.
7481 +*/
7482 +
7483 +#include "ipkg.h"
7484 +
7485 +#include "nv_pair.h"
7486 +#include "str_util.h"
7487 +
7488 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value)
7489 +{
7490 +    nv_pair->name = str_dup_safe(name);
7491 +    nv_pair->value = str_dup_safe(value);
7492 +
7493 +    return 0;
7494 +}
7495 +
7496 +void nv_pair_deinit(nv_pair_t *nv_pair)
7497 +{
7498 +    free(nv_pair->name);
7499 +    nv_pair->name = NULL;
7500 +
7501 +    free(nv_pair->value);
7502 +    nv_pair->value = NULL;
7503 +}
7504 +
7505 +
7506 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.h
7507 --- busybox-1.2.0-orig/archival/libipkg/nv_pair.h       1970-01-01 01:00:00.000000000 +0100
7508 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair.h      2006-07-22 16:31:25.000000000 +0200
7509 @@ -0,0 +1,32 @@
7510 +/* nv_pair.h - the itsy package management system
7511 +
7512 +   Carl D. Worth
7513 +
7514 +   Copyright (C) 2001 University of Southern California
7515 +
7516 +   This program is free software; you can redistribute it and/or
7517 +   modify it under the terms of the GNU General Public License as
7518 +   published by the Free Software Foundation; either version 2, or (at
7519 +   your option) any later version.
7520 +
7521 +   This program is distributed in the hope that it will be useful, but
7522 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7523 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7524 +   General Public License for more details.
7525 +*/
7526 +
7527 +#ifndef NV_PAIR_H
7528 +#define NV_PAIR_H
7529 +
7530 +typedef struct nv_pair nv_pair_t;
7531 +struct nv_pair
7532 +{
7533 +    char *name;
7534 +    char *value;
7535 +};
7536 +
7537 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value);
7538 +void nv_pair_deinit(nv_pair_t *nv_pair);
7539 +
7540 +#endif
7541 +
7542 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.c
7543 --- busybox-1.2.0-orig/archival/libipkg/nv_pair_list.c  1970-01-01 01:00:00.000000000 +0100
7544 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.c 2006-07-22 16:31:25.000000000 +0200
7545 @@ -0,0 +1,98 @@
7546 +/* nv_pair_list.c - the itsy package management system
7547 +
7548 +   Carl D. Worth
7549 +
7550 +   Copyright (C) 2001 University of Southern California
7551 +
7552 +   This program is free software; you can redistribute it and/or
7553 +   modify it under the terms of the GNU General Public License as
7554 +   published by the Free Software Foundation; either version 2, or (at
7555 +   your option) any later version.
7556 +
7557 +   This program is distributed in the hope that it will be useful, but
7558 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7559 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7560 +   General Public License for more details.
7561 +*/
7562 +
7563 +#include "ipkg.h"
7564 +
7565 +#include "nv_pair.h"
7566 +#include "void_list.h"
7567 +#include "nv_pair_list.h"
7568 +
7569 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data)
7570 +{
7571 +    return void_list_elt_init((void_list_elt_t *) elt, data);
7572 +}
7573 +
7574 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt)
7575 +{
7576 +    void_list_elt_deinit((void_list_elt_t *) elt);
7577 +}
7578 +
7579 +int nv_pair_list_init(nv_pair_list_t *list)
7580 +{
7581 +    return void_list_init((void_list_t *) list);
7582 +}
7583 +
7584 +void nv_pair_list_deinit(nv_pair_list_t *list)
7585 +{
7586 +    nv_pair_list_elt_t *iter;
7587 +    nv_pair_t *nv_pair;
7588 +
7589 +    for (iter = list->head; iter; iter = iter->next) {
7590 +       nv_pair = iter->data;
7591 +       nv_pair_deinit(nv_pair);
7592 +
7593 +       /* malloced in nv_pair_list_append */
7594 +       free(nv_pair);
7595 +       iter->data = NULL;
7596 +    }
7597 +    void_list_deinit((void_list_t *) list);
7598 +}
7599 +
7600 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value)
7601 +{
7602 +    int err;
7603 +
7604 +    /* freed in nv_pair_list_deinit */
7605 +    nv_pair_t *nv_pair = malloc(sizeof(nv_pair_t));
7606 +
7607 +    if (nv_pair == NULL) {
7608 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7609 +       return NULL;
7610 +    }
7611 +    nv_pair_init(nv_pair, name, value);
7612 +
7613 +    err = void_list_append((void_list_t *) list, nv_pair);
7614 +    if (err) {
7615 +       return NULL;
7616 +    }
7617 +
7618 +    return nv_pair;
7619 +}
7620 +
7621 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data)
7622 +{
7623 +    return void_list_push((void_list_t *) list, data);
7624 +}
7625 +
7626 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list)
7627 +{
7628 +    return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list);
7629 +}
7630 +
7631 +char *nv_pair_list_find(nv_pair_list_t *list, char *name)
7632 +{
7633 +     nv_pair_list_elt_t *iter;
7634 +     nv_pair_t *nv_pair;
7635 +
7636 +     for (iter = list->head; iter; iter = iter->next) {
7637 +         nv_pair = iter->data;
7638 +         if (strcmp(nv_pair->name, name) == 0) {
7639 +              return nv_pair->value;
7640 +         }
7641 +     }    
7642 +     return NULL;
7643 +}
7644 diff -ruN busybox-1.2.0-orig/archival/libipkg/nv_pair_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.h
7645 --- busybox-1.2.0-orig/archival/libipkg/nv_pair_list.h  1970-01-01 01:00:00.000000000 +0100
7646 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/nv_pair_list.h 2006-07-22 16:31:25.000000000 +0200
7647 @@ -0,0 +1,60 @@
7648 +/* nv_pair_list.h - the itsy package management system
7649 +
7650 +   Carl D. Worth
7651 +
7652 +   Copyright (C) 2001 University of Southern California
7653 +
7654 +   This program is free software; you can redistribute it and/or
7655 +   modify it under the terms of the GNU General Public License as
7656 +   published by the Free Software Foundation; either version 2, or (at
7657 +   your option) any later version.
7658 +
7659 +   This program is distributed in the hope that it will be useful, but
7660 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7661 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7662 +   General Public License for more details.
7663 +*/
7664 +
7665 +#ifndef NV_PAIR_LIST_H
7666 +#define NV_PAIR_LIST_H
7667 +
7668 +#include "nv_pair.h"
7669 +#include "void_list.h"
7670 +
7671 +typedef struct nv_pair_list_elt nv_pair_list_elt_t;
7672 +struct nv_pair_list_elt
7673 +{
7674 +    nv_pair_list_elt_t *next;
7675 +    nv_pair_t *data;
7676 +};
7677 +
7678 +typedef struct nv_pair_list nv_pair_list_t;
7679 +struct nv_pair_list
7680 +{
7681 +    nv_pair_list_elt_t pre_head;
7682 +    nv_pair_list_elt_t *head;
7683 +    nv_pair_list_elt_t *tail;
7684 +};
7685 +
7686 +static inline int nv_pair_list_empty(nv_pair_list_t *list)
7687 +{
7688 +     if (list->head == NULL)
7689 +         return 1;
7690 +     else
7691 +         return 0;
7692 +}
7693 +
7694 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data);
7695 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt);
7696 +
7697 +int nv_pair_list_init(nv_pair_list_t *list);
7698 +void nv_pair_list_deinit(nv_pair_list_t *list);
7699 +
7700 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list,
7701 +                              const char *name, const char *value);
7702 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data);
7703 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list);
7704 +char *nv_pair_list_find(nv_pair_list_t *list, char *name);
7705 +
7706 +#endif
7707 +
7708 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.c
7709 --- busybox-1.2.0-orig/archival/libipkg/pkg.c   1970-01-01 01:00:00.000000000 +0100
7710 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.c  2006-07-22 16:31:25.000000000 +0200
7711 @@ -0,0 +1,1754 @@
7712 +/* pkg.c - the itsy package management system
7713 +
7714 +   Carl D. Worth
7715 +
7716 +   Copyright (C) 2001 University of Southern California
7717 +
7718 +   This program is free software; you can redistribute it and/or
7719 +   modify it under the terms of the GNU General Public License as
7720 +   published by the Free Software Foundation; either version 2, or (at
7721 +   your option) any later version.
7722 +
7723 +   This program is distributed in the hope that it will be useful, but
7724 +   WITHOUT ANY WARRANTY; without even the implied warranty of
7725 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7726 +   General Public License for more details.
7727 +*/
7728 +
7729 +#include "ipkg.h"
7730 +#include <ctype.h>
7731 +#include <string.h>
7732 +#include <errno.h>
7733 +
7734 +#include "pkg.h"
7735 +
7736 +#include "pkg_parse.h"
7737 +#include "pkg_extract.h"
7738 +#include "ipkg_message.h"
7739 +#include "ipkg_utils.h"
7740 +
7741 +#include "sprintf_alloc.h"
7742 +#include "file_util.h"
7743 +#include "str_util.h"
7744 +#include "xsystem.h"
7745 +#include "ipkg_conf.h"
7746 +
7747 +typedef struct enum_map enum_map_t;
7748 +struct enum_map
7749 +{
7750 +     int value;
7751 +     char *str;
7752 +};
7753 +
7754 +static const enum_map_t pkg_state_want_map[] = {
7755 +     { SW_UNKNOWN, "unknown"},
7756 +     { SW_INSTALL, "install"},
7757 +     { SW_DEINSTALL, "deinstall"},
7758 +     { SW_PURGE, "purge"}
7759 +};
7760 +
7761 +static const enum_map_t pkg_state_flag_map[] = {
7762 +     { SF_OK, "ok"},
7763 +     { SF_REINSTREQ, "reinstreq"},
7764 +     { SF_HOLD, "hold"},
7765 +     { SF_REPLACE, "replace"},
7766 +     { SF_NOPRUNE, "noprune"},
7767 +     { SF_PREFER, "prefer"},
7768 +     { SF_OBSOLETE, "obsolete"},
7769 +     { SF_USER, "user"},
7770 +};
7771 +
7772 +static const enum_map_t pkg_state_status_map[] = {
7773 +     { SS_NOT_INSTALLED, "not-installed" },
7774 +     { SS_UNPACKED, "unpacked" },
7775 +     { SS_HALF_CONFIGURED, "half-configured" },
7776 +     { SS_INSTALLED, "installed" },
7777 +     { SS_HALF_INSTALLED, "half-installed" },
7778 +     { SS_CONFIG_FILES, "config-files" },
7779 +     { SS_POST_INST_FAILED, "post-inst-failed" },
7780 +     { SS_REMOVAL_FAILED, "removal-failed" }
7781 +};
7782 +
7783 +static int verrevcmp(const char *val, const char *ref);
7784 +
7785 +
7786 +pkg_t *pkg_new(void)
7787 +{
7788 +     pkg_t *pkg;
7789 +
7790 +     pkg = malloc(sizeof(pkg_t));
7791 +     if (pkg == NULL) {
7792 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7793 +         return NULL;
7794 +     }
7795 +
7796 +     pkg_init(pkg);
7797 +
7798 +     return pkg;
7799 +}
7800 +
7801 +int pkg_init(pkg_t *pkg)
7802 +{
7803 +     memset(pkg, 0, sizeof(pkg_t));
7804 +     pkg->name = NULL;
7805 +     pkg->epoch = 0;
7806 +     pkg->version = NULL;
7807 +     pkg->revision = NULL;
7808 +     pkg->familiar_revision = NULL;
7809 +     pkg->dest = NULL;
7810 +     pkg->src = NULL;
7811 +     pkg->architecture = NULL;
7812 +     pkg->maintainer = NULL;
7813 +     pkg->section = NULL;
7814 +     pkg->description = NULL;
7815 +     pkg->state_want = SW_UNKNOWN;
7816 +     pkg->state_flag = SF_OK;
7817 +     pkg->state_status = SS_NOT_INSTALLED;
7818 +     pkg->depends_str = NULL;
7819 +     pkg->provides_str = NULL;
7820 +     pkg->depends_count = 0;
7821 +     pkg->depends = NULL;
7822 +     pkg->suggests_str = NULL;
7823 +     pkg->recommends_str = NULL;
7824 +     pkg->suggests_count = 0;
7825 +     pkg->recommends_count = 0;
7826 +
7827 +     /* Abhaya: added init for conflicts fields */
7828 +     pkg->conflicts = NULL;
7829 +     pkg->conflicts_count = 0;
7830 +
7831 +     /* added for replaces.  Jamey 7/23/2002 */
7832 +     pkg->replaces = NULL;
7833 +     pkg->replaces_count = 0;
7834 +    
7835 +     pkg->pre_depends_count = 0;
7836 +     pkg->pre_depends_str = NULL;
7837 +     pkg->provides_count = 0;
7838 +     pkg->provides = NULL;
7839 +     pkg->filename = NULL;
7840 +     pkg->local_filename = NULL;
7841 +     pkg->tmp_unpack_dir = NULL;
7842 +     pkg->md5sum = NULL;
7843 +     pkg->size = NULL;
7844 +     pkg->installed_size = NULL;
7845 +     pkg->priority = NULL;
7846 +     pkg->source = NULL;
7847 +     conffile_list_init(&pkg->conffiles);
7848 +     pkg->installed_files = NULL;
7849 +     pkg->installed_files_ref_cnt = 0;
7850 +     pkg->essential = 0;
7851 +     pkg->provided_by_hand = 0;
7852 +
7853 +     return 0;
7854 +}
7855 +
7856 +void pkg_deinit(pkg_t *pkg)
7857 +{
7858 +     free(pkg->name);
7859 +     pkg->name = NULL;
7860 +     pkg->epoch = 0;
7861 +     free(pkg->version);
7862 +     pkg->version = NULL;
7863 +     /* revision and familiar_revision share storage with version, so
7864 +       don't free */
7865 +     pkg->revision = NULL;
7866 +     pkg->familiar_revision = NULL;
7867 +     /* owned by ipkg_conf_t */
7868 +     pkg->dest = NULL;
7869 +     /* owned by ipkg_conf_t */
7870 +     pkg->src = NULL;
7871 +     free(pkg->architecture);
7872 +     pkg->architecture = NULL;
7873 +     free(pkg->maintainer);
7874 +     pkg->maintainer = NULL;
7875 +     free(pkg->section);
7876 +     pkg->section = NULL;
7877 +     free(pkg->description);
7878 +     pkg->description = NULL;
7879 +     pkg->state_want = SW_UNKNOWN;
7880 +     pkg->state_flag = SF_OK;
7881 +     pkg->state_status = SS_NOT_INSTALLED;
7882 +     free(pkg->depends_str);
7883 +     pkg->depends_str = NULL;
7884 +     free(pkg->provides_str);
7885 +     pkg->provides_str = NULL;
7886 +     pkg->depends_count = 0;
7887 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->depends ? */
7888 +     pkg->pre_depends_count = 0;
7889 +     free(pkg->pre_depends_str);
7890 +     pkg->pre_depends_str = NULL;
7891 +     pkg->provides_count = 0;
7892 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->provides ? */
7893 +     /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->suggests ? */
7894 +     free(pkg->filename);
7895 +     pkg->filename = NULL;
7896 +     free(pkg->local_filename);
7897 +     pkg->local_filename = NULL;
7898 +     /* CLEANUP: It'd be nice to pullin the cleanup function from
7899 +       ipkg_install.c here. See comment in
7900 +       ipkg_install.c:cleanup_temporary_files */
7901 +     free(pkg->tmp_unpack_dir);
7902 +     pkg->tmp_unpack_dir = NULL;
7903 +     free(pkg->md5sum);
7904 +     pkg->md5sum = NULL;
7905 +     free(pkg->size);
7906 +     pkg->size = NULL;
7907 +     free(pkg->installed_size);
7908 +     pkg->installed_size = NULL;
7909 +     free(pkg->priority);
7910 +     pkg->priority = NULL;
7911 +     free(pkg->source);
7912 +     pkg->source = NULL;
7913 +     conffile_list_deinit(&pkg->conffiles);
7914 +     /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
7915 +       since if they are calling deinit, they should know. Maybe do an
7916 +       assertion here instead? */
7917 +     pkg->installed_files_ref_cnt = 1;
7918 +     pkg_free_installed_files(pkg);
7919 +     pkg->essential = 0;
7920 +}
7921 +
7922 +int pkg_init_from_file(pkg_t *pkg, const char *filename)
7923 +{
7924 +     int err;
7925 +     char **raw;
7926 +     FILE *control_file;
7927 +
7928 +     err = pkg_init(pkg);
7929 +     if (err) { return err; }
7930 +
7931 +     pkg->local_filename = strdup(filename);
7932 +    
7933 +     control_file = tmpfile();
7934 +     err = pkg_extract_control_file_to_stream(pkg, control_file);
7935 +     if (err) { return err; }
7936 +
7937 +     rewind(control_file);
7938 +     raw = read_raw_pkgs_from_stream(control_file);
7939 +     pkg_parse_raw(pkg, &raw, NULL, NULL);
7940 +
7941 +     fclose(control_file);
7942 +
7943 +     return 0;
7944 +}
7945 +
7946 +/* Merge any new information in newpkg into oldpkg */
7947 +/* XXX: CLEANUP: This function shouldn't actually modify anything in
7948 +   newpkg, but should leave it usable. This rework is so that
7949 +   pkg_hash_insert doesn't clobber the pkg that you pass into it. */
7950 +/* 
7951 + * uh, i thought that i had originally written this so that it took 
7952 + * two pkgs and returned a new one?  we can do that again... -sma
7953 + */
7954 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status)
7955 +{
7956 +     if (oldpkg == newpkg) {
7957 +         return 0;
7958 +     }
7959 +
7960 +     if (!oldpkg->src)
7961 +         oldpkg->src = newpkg->src;
7962 +     if (!oldpkg->dest)
7963 +         oldpkg->dest = newpkg->dest;
7964 +     if (!oldpkg->architecture)
7965 +         oldpkg->architecture = str_dup_safe(newpkg->architecture);
7966 +     if (!oldpkg->arch_priority)
7967 +         oldpkg->arch_priority = newpkg->arch_priority;
7968 +     if (!oldpkg->section)
7969 +         oldpkg->section = str_dup_safe(newpkg->section);
7970 +     if(!oldpkg->maintainer)
7971 +         oldpkg->maintainer = str_dup_safe(newpkg->maintainer);
7972 +     if(!oldpkg->description)
7973 +         oldpkg->description = str_dup_safe(newpkg->description);
7974 +     if (set_status) {
7975 +         /* merge the state_flags from the new package */
7976 +         oldpkg->state_want = newpkg->state_want;
7977 +         oldpkg->state_status = newpkg->state_status;
7978 +         oldpkg->state_flag = newpkg->state_flag;
7979 +     } else {
7980 +         if (oldpkg->state_want == SW_UNKNOWN)
7981 +              oldpkg->state_want = newpkg->state_want;
7982 +         if (oldpkg->state_status == SS_NOT_INSTALLED)
7983 +              oldpkg->state_status = newpkg->state_status;
7984 +         oldpkg->state_flag |= newpkg->state_flag;
7985 +     }
7986 +
7987 +     if (!oldpkg->depends_str && !oldpkg->pre_depends_str && !oldpkg->recommends_str && !oldpkg->suggests_str) {
7988 +         oldpkg->depends_str = newpkg->depends_str;
7989 +         newpkg->depends_str = NULL;
7990 +         oldpkg->depends_count = newpkg->depends_count;
7991 +         newpkg->depends_count = 0;
7992 +
7993 +         oldpkg->depends = newpkg->depends;
7994 +         newpkg->depends = NULL;
7995 +
7996 +         oldpkg->pre_depends_str = newpkg->pre_depends_str;
7997 +         newpkg->pre_depends_str = NULL;
7998 +         oldpkg->pre_depends_count = newpkg->pre_depends_count;
7999 +         newpkg->pre_depends_count = 0;
8000 +
8001 +         oldpkg->recommends_str = newpkg->recommends_str;
8002 +         newpkg->recommends_str = NULL;
8003 +         oldpkg->recommends_count = newpkg->recommends_count;
8004 +         newpkg->recommends_count = 0;
8005 +
8006 +         oldpkg->suggests_str = newpkg->suggests_str;
8007 +         newpkg->suggests_str = NULL;
8008 +         oldpkg->suggests_count = newpkg->suggests_count;
8009 +         newpkg->suggests_count = 0;
8010 +     }
8011 +
8012 +     if (!oldpkg->provides_str) {
8013 +         oldpkg->provides_str = newpkg->provides_str;
8014 +         newpkg->provides_str = NULL;
8015 +         oldpkg->provides_count = newpkg->provides_count;
8016 +         newpkg->provides_count = 0;
8017 +
8018 +         oldpkg->provides = newpkg->provides;
8019 +         newpkg->provides = NULL;
8020 +     }
8021 +
8022 +     if (!oldpkg->conflicts_str) {
8023 +         oldpkg->conflicts_str = newpkg->conflicts_str;
8024 +         newpkg->conflicts_str = NULL;
8025 +         oldpkg->conflicts_count = newpkg->conflicts_count;
8026 +         newpkg->conflicts_count = 0;
8027 +
8028 +         oldpkg->conflicts = newpkg->conflicts;
8029 +         newpkg->conflicts = NULL;
8030 +     }
8031 +
8032 +     if (!oldpkg->replaces_str) {
8033 +         oldpkg->replaces_str = newpkg->replaces_str;
8034 +         newpkg->replaces_str = NULL;
8035 +         oldpkg->replaces_count = newpkg->replaces_count;
8036 +         newpkg->replaces_count = 0;
8037 +
8038 +         oldpkg->replaces = newpkg->replaces;
8039 +         newpkg->replaces = NULL;
8040 +     }
8041 +
8042 +     if (!oldpkg->filename)
8043 +         oldpkg->filename = str_dup_safe(newpkg->filename);
8044 +     if (0)
8045 +     fprintf(stdout, "pkg=%s old local_filename=%s new local_filename=%s\n", 
8046 +            oldpkg->name, oldpkg->local_filename, newpkg->local_filename);
8047 +     if (!oldpkg->local_filename)
8048 +         oldpkg->local_filename = str_dup_safe(newpkg->local_filename);
8049 +     if (!oldpkg->tmp_unpack_dir)
8050 +         oldpkg->tmp_unpack_dir = str_dup_safe(newpkg->tmp_unpack_dir);
8051 +     if (!oldpkg->md5sum)
8052 +         oldpkg->md5sum = str_dup_safe(newpkg->md5sum);
8053 +     if (!oldpkg->size)
8054 +         oldpkg->size = str_dup_safe(newpkg->size);
8055 +     if (!oldpkg->installed_size)
8056 +         oldpkg->installed_size = str_dup_safe(newpkg->installed_size);
8057 +     if (!oldpkg->priority)
8058 +         oldpkg->priority = str_dup_safe(newpkg->priority);
8059 +     if (!oldpkg->source)
8060 +         oldpkg->source = str_dup_safe(newpkg->source);
8061 +     if (oldpkg->conffiles.head == NULL){
8062 +         oldpkg->conffiles = newpkg->conffiles;
8063 +         conffile_list_init(&newpkg->conffiles);
8064 +     }
8065 +     if (!oldpkg->installed_files){
8066 +         oldpkg->installed_files = newpkg->installed_files;
8067 +         oldpkg->installed_files_ref_cnt = newpkg->installed_files_ref_cnt;
8068 +         newpkg->installed_files = NULL;
8069 +     }
8070 +     if (!oldpkg->essential)
8071 +         oldpkg->essential = newpkg->essential;
8072 +
8073 +     return 0;
8074 +}
8075 +
8076 +abstract_pkg_t *abstract_pkg_new(void)
8077 +{
8078 +     abstract_pkg_t * ab_pkg;
8079 +
8080 +     ab_pkg = malloc(sizeof(abstract_pkg_t));
8081 +
8082 +     if (ab_pkg == NULL) {
8083 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8084 +         return NULL;
8085 +     }
8086 +
8087 +     if ( abstract_pkg_init(ab_pkg) < 0 ) 
8088 +        return NULL;
8089 +
8090 +     return ab_pkg;
8091 +}
8092 +
8093 +int abstract_pkg_init(abstract_pkg_t *ab_pkg)
8094 +{
8095 +     memset(ab_pkg, 0, sizeof(abstract_pkg_t));
8096 +
8097 +     ab_pkg->provided_by = abstract_pkg_vec_alloc();
8098 +     if (ab_pkg->provided_by==NULL){
8099 +        return -1;
8100 +     }
8101 +     ab_pkg->dependencies_checked = 0;
8102 +     ab_pkg->state_status = SS_NOT_INSTALLED;
8103 +
8104 +     return 0;
8105 +}
8106 +
8107 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg){
8108 +     char * temp_str;
8109 +     char **raw =NULL;
8110 +     char **raw_start=NULL; 
8111 +
8112 +     temp_str = (char *) malloc (strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
8113 +     if (temp_str == NULL ){
8114 +        ipkg_message(conf, IPKG_INFO, "Out of memory in  %s\n", __FUNCTION__);
8115 +        return;
8116 +     }
8117 +     sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
8118 +   
8119 +     raw = raw_start = read_raw_pkgs_from_file(temp_str);
8120 +     if (raw == NULL ){
8121 +        ipkg_message(conf, IPKG_ERROR, "Unable to open the control file in  %s\n", __FUNCTION__);
8122 +        return;
8123 +     }
8124 +
8125 +     while(*raw){
8126 +        if (!pkg_valorize_other_field(pkg, &raw ) == 0) {
8127 +            ipkg_message(conf, IPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
8128 +        }
8129 +     }
8130 +     raw = raw_start;
8131 +     while (*raw) {
8132 +        if (raw!=NULL)
8133 +          free(*raw++);
8134 +     }
8135 +
8136 +     free(raw_start); 
8137 +     free(temp_str);
8138 +
8139 +     return ;
8140 +
8141 +}
8142 +
8143 +char * pkg_formatted_info(pkg_t *pkg )
8144 +{
8145 +     char *line;
8146 +     char * buff;
8147 +
8148 +     buff = malloc(8192);
8149 +     if (buff == NULL) {
8150 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8151 +         return NULL;
8152 +     }
8153 +
8154 +     buff[0] = '\0';
8155 +
8156 +     line = pkg_formatted_field(pkg, "Package");
8157 +     strncat(buff ,line, strlen(line));
8158 +     free(line);
8159 +
8160 +     line = pkg_formatted_field(pkg, "Version");
8161 +     strncat(buff ,line, strlen(line));
8162 +     free(line);
8163 +
8164 +     line = pkg_formatted_field(pkg, "Depends");
8165 +     strncat(buff ,line, strlen(line));
8166 +     free(line);
8167 +     
8168 +     line = pkg_formatted_field(pkg, "Recommends");
8169 +     strncat(buff ,line, strlen(line));
8170 +     free(line);
8171 +
8172 +     line = pkg_formatted_field(pkg, "Suggests");
8173 +     strncat(buff ,line, strlen(line));
8174 +     free(line);
8175 +
8176 +     line = pkg_formatted_field(pkg, "Provides");
8177 +     strncat(buff ,line, strlen(line));
8178 +     free(line);
8179 +
8180 +     line = pkg_formatted_field(pkg, "Replaces");
8181 +     strncat(buff ,line, strlen(line));
8182 +     free(line);
8183 +
8184 +     line = pkg_formatted_field(pkg, "Conflicts");
8185 +     strncat(buff ,line, strlen(line));
8186 +     free(line);
8187 +
8188 +     line = pkg_formatted_field(pkg, "Status");
8189 +     strncat(buff ,line, strlen(line));
8190 +     free(line);
8191 +
8192 +     line = pkg_formatted_field(pkg, "Section");
8193 +     strncat(buff ,line, strlen(line));
8194 +     free(line);
8195 +
8196 +     line = pkg_formatted_field(pkg, "Essential"); /* @@@@ should be removed in future release. *//* I do not agree with this Pigi*/
8197 +     strncat(buff ,line, strlen(line));
8198 +     free(line);
8199 +
8200 +     line = pkg_formatted_field(pkg, "Architecture");
8201 +     strncat(buff ,line, strlen(line));
8202 +     free(line);
8203 +
8204 +     line = pkg_formatted_field(pkg, "Maintainer");
8205 +     strncat(buff ,line, strlen(line));
8206 +     free(line);
8207 +
8208 +     line = pkg_formatted_field(pkg, "MD5sum");
8209 +     strncat(buff ,line, strlen(line));
8210 +     free(line);
8211 +
8212 +     line = pkg_formatted_field(pkg, "Size");
8213 +     strncat(buff ,line, strlen(line));
8214 +     free(line);
8215 +
8216 +     line = pkg_formatted_field(pkg, "Filename");
8217 +     strncat(buff ,line, strlen(line));
8218 +     free(line);
8219 +
8220 +     line = pkg_formatted_field(pkg, "Conffiles");
8221 +     strncat(buff ,line, strlen(line));
8222 +     free(line);
8223 +
8224 +     line = pkg_formatted_field(pkg, "Source");
8225 +     strncat(buff ,line, strlen(line));
8226 +     free(line);
8227 +
8228 +     line = pkg_formatted_field(pkg, "Description");
8229 +     strncat(buff ,line, strlen(line));
8230 +     free(line);
8231 +
8232 +     line = pkg_formatted_field(pkg, "Installed-Time");
8233 +     strncat(buff ,line, strlen(line));
8234 +     free(line);
8235 +
8236 +     return buff;
8237 +}
8238 +
8239 +char * pkg_formatted_field(pkg_t *pkg, const char *field )
8240 +{
8241 +     static size_t LINE_LEN = 128;
8242 +     char * temp = (char *)malloc(1);
8243 +     int len = 0;
8244 +     int flag_provide_false = 0;
8245 +
8246 +/*
8247 +  Pigi: After some discussion with Florian we decided to modify the full procedure in 
8248 +        dynamic memory allocation. This should avoid any other segv in this area ( except for bugs )
8249 +*/
8250 +
8251 +     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8252 +         goto UNKNOWN_FMT_FIELD;
8253 +     }
8254 +
8255 +     temp[0]='\0'; 
8256 +
8257 +     switch (field[0])
8258 +     {
8259 +     case 'a':
8260 +     case 'A':
8261 +         if (strcasecmp(field, "Architecture") == 0) {
8262 +              /* Architecture */
8263 +              if (pkg->architecture) {
8264 +                   temp = (char *)realloc(temp,strlen(pkg->architecture)+17);
8265 +                   if ( temp == NULL ){
8266 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8267 +                     return NULL;
8268 +                   }
8269 +                   temp[0]='\0';
8270 +                   snprintf(temp, (strlen(pkg->architecture)+17), "Architecture: %s\n", pkg->architecture);
8271 +              }
8272 +         } else {
8273 +              goto UNKNOWN_FMT_FIELD;
8274 +         }
8275 +         break;
8276 +     case 'c':
8277 +     case 'C':
8278 +         if (strcasecmp(field, "Conffiles") == 0) {
8279 +              /* Conffiles */
8280 +              conffile_list_elt_t *iter;
8281 +               char confstr[LINE_LEN];
8282 +
8283 +              if (pkg->conffiles.head == NULL) {
8284 +                   return temp;
8285 +              }
8286 +
8287 +               len = 14 ;
8288 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8289 +                   if (iter->data->name && iter->data->value) {
8290 +                       len = len + (strlen(iter->data->name)+strlen(iter->data->value)+5);
8291 +                   }
8292 +              }
8293 +               temp = (char *)realloc(temp,len);
8294 +               if ( temp == NULL ){
8295 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8296 +                 return NULL;
8297 +               }
8298 +               temp[0]='\0';
8299 +               strncpy(temp, "Conffiles:\n", 12);
8300 +              for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8301 +                   if (iter->data->name && iter->data->value) {
8302 +                         snprintf(confstr, LINE_LEN, "%s %s\n", iter->data->name, iter->data->value);
8303 +                         strncat(temp, confstr, strlen(confstr));           
8304 +                   }
8305 +              }
8306 +         } else if (strcasecmp(field, "Conflicts") == 0) {
8307 +              int i;
8308 +
8309 +              if (pkg->conflicts_count) {
8310 +                    char conflictstr[LINE_LEN];
8311 +                    len = 14 ;
8312 +                   for(i = 0; i < pkg->conflicts_count; i++) {
8313 +                        len = len + (strlen(pkg->conflicts_str[i])+5);
8314 +                    }
8315 +                    temp = (char *)realloc(temp,len);
8316 +                    if ( temp == NULL ){
8317 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8318 +                      return NULL;
8319 +                    }
8320 +                    temp[0]='\0';
8321 +                    strncpy(temp, "Conflicts:", 11);
8322 +                   for(i = 0; i < pkg->conflicts_count; i++) {
8323 +                        snprintf(conflictstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->conflicts_str[i]);
8324 +                        strncat(temp, conflictstr, strlen(conflictstr));           
8325 +                    }
8326 +                    strncat(temp, "\n", strlen("\n")); 
8327 +              }
8328 +         } else {
8329 +              goto UNKNOWN_FMT_FIELD;
8330 +         }
8331 +         break;
8332 +     case 'd':
8333 +     case 'D':
8334 +         if (strcasecmp(field, "Depends") == 0) {
8335 +              /* Depends */
8336 +              int i;
8337 +
8338 +              if (pkg->depends_count) {
8339 +                    char depstr[LINE_LEN];
8340 +                    len = 14 ;
8341 +                   for(i = 0; i < pkg->depends_count; i++) {
8342 +                        len = len + (strlen(pkg->depends_str[i])+4);
8343 +                    }
8344 +                    temp = (char *)realloc(temp,len);
8345 +                    if ( temp == NULL ){
8346 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8347 +                      return NULL;
8348 +                    }
8349 +                    temp[0]='\0';
8350 +                    strncpy(temp, "Depends:", 10);
8351 +                   for(i = 0; i < pkg->depends_count; i++) {
8352 +                        snprintf(depstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
8353 +                        strncat(temp, depstr, strlen(depstr));           
8354 +                    }
8355 +                    strncat(temp, "\n", strlen("\n")); 
8356 +              }
8357 +         } else if (strcasecmp(field, "Description") == 0) {
8358 +              /* Description */
8359 +              if (pkg->description) {
8360 +                   temp = (char *)realloc(temp,strlen(pkg->description)+16);
8361 +                   if ( temp == NULL ){
8362 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8363 +                     return NULL;
8364 +                   }
8365 +                   temp[0]='\0';
8366 +                   snprintf(temp, (strlen(pkg->description)+16), "Description: %s\n", pkg->description);
8367 +              }
8368 +         } else {
8369 +              goto UNKNOWN_FMT_FIELD;
8370 +         }
8371 +      break;
8372 +     case 'e':
8373 +     case 'E': {
8374 +         /* Essential */
8375 +         if (pkg->essential) {
8376 +              temp = (char *)realloc(temp,16);
8377 +              if ( temp == NULL ){
8378 +                fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8379 +                return NULL;
8380 +              }
8381 +              temp[0]='\0';
8382 +              snprintf(temp, (16), "Essential: yes\n");
8383 +         }
8384 +     }
8385 +         break;
8386 +     case 'f':
8387 +     case 'F': {
8388 +         /* Filename */
8389 +         if (pkg->filename) {
8390 +              temp = (char *)realloc(temp,strlen(pkg->filename)+12);
8391 +              if ( temp == NULL ){
8392 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8393 +                 return NULL;
8394 +              }
8395 +              temp[0]='\0';
8396 +              snprintf(temp, (strlen(pkg->filename)+12), "Filename: %s\n", pkg->filename);
8397 +         }
8398 +     }
8399 +         break;
8400 +     case 'i':
8401 +     case 'I': {
8402 +         if (strcasecmp(field, "Installed-Size") == 0) {
8403 +              /* Installed-Size */
8404 +               temp = (char *)realloc(temp,strlen(pkg->installed_size)+17);
8405 +               if ( temp == NULL ){
8406 +                  fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8407 +                  return NULL;
8408 +               }
8409 +               temp[0]='\0';
8410 +               snprintf(temp, (strlen(pkg->installed_size)+17), "Installed-Size: %s\n", pkg->installed_size);
8411 +         } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) {
8412 +               temp = (char *)realloc(temp,29);
8413 +               if ( temp == NULL ){
8414 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8415 +                 return NULL;
8416 +               }
8417 +               temp[0]='\0';
8418 +               snprintf(temp, 29, "Installed-Time: %lu\n", pkg->installed_time);
8419 +         }
8420 +     }
8421 +         break;
8422 +     case 'm':
8423 +     case 'M': {
8424 +         /* Maintainer | MD5sum */
8425 +         if (strcasecmp(field, "Maintainer") == 0) {
8426 +              /* Maintainer */
8427 +              if (pkg->maintainer) {
8428 +                   temp = (char *)realloc(temp,strlen(pkg->maintainer)+14);
8429 +                   if ( temp == NULL ){
8430 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8431 +                     return NULL;
8432 +                   }
8433 +                   temp[0]='\0';
8434 +                   snprintf(temp, (strlen(pkg->maintainer)+14), "maintainer: %s\n", pkg->maintainer);
8435 +              }
8436 +         } else if (strcasecmp(field, "MD5sum") == 0) {
8437 +              /* MD5sum */
8438 +              if (pkg->md5sum) {
8439 +                   temp = (char *)realloc(temp,strlen(pkg->md5sum)+11);
8440 +                   if ( temp == NULL ){
8441 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8442 +                     return NULL;
8443 +                   }
8444 +                   temp[0]='\0';
8445 +                   snprintf(temp, (strlen(pkg->md5sum)+11), "MD5Sum: %s\n", pkg->md5sum);
8446 +              }
8447 +         } else {
8448 +              goto UNKNOWN_FMT_FIELD;
8449 +         }
8450 +     }
8451 +         break;
8452 +     case 'p':
8453 +     case 'P': {
8454 +         if (strcasecmp(field, "Package") == 0) {
8455 +              /* Package */
8456 +               temp = (char *)realloc(temp,strlen(pkg->name)+11);
8457 +               if ( temp == NULL ){
8458 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8459 +                 return NULL;
8460 +               }
8461 +               temp[0]='\0';
8462 +               snprintf(temp, (strlen(pkg->name)+11), "Package: %s\n", pkg->name);
8463 +         } else if (strcasecmp(field, "Priority") == 0) {
8464 +              /* Priority */
8465 +               temp = (char *)realloc(temp,strlen(pkg->priority)+12);
8466 +               if ( temp == NULL ){
8467 +                 fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8468 +                 return NULL;
8469 +               }
8470 +               temp[0]='\0';
8471 +               snprintf(temp, (strlen(pkg->priority)+12), "Priority: %s\n", pkg->priority);
8472 +         } else if (strcasecmp(field, "Provides") == 0) {
8473 +              /* Provides */
8474 +              int i;
8475 +
8476 +              if (pkg->provides_count) {
8477 +               /* Here we check if the ipkg_internal_use_only is used, and we discard it.*/
8478 +                  for ( i=0; i < pkg->provides_count; i++ ){
8479 +                     if (strstr(pkg->provides_str[i],"ipkg_internal_use_only")!=NULL) {
8480 +                         memset (pkg->provides_str[i],'\x0',strlen(pkg->provides_str[i])); /* Pigi clear my trick flag, just in case */
8481 +                         flag_provide_false = 1;
8482 +                      }
8483 +                  }
8484 +                  if ( !flag_provide_false ||                                             /* Pigi there is not my trick flag */
8485 +                     ((flag_provide_false) &&  (pkg->provides_count > 1))){             /* Pigi There is, but we also have others Provides */
8486 +                     char provstr[LINE_LEN];
8487 +                     len = 15;
8488 +                    for(i = 0; i < pkg->provides_count; i++) {
8489 +                         len = len + (strlen(pkg->provides_str[i])+5);
8490 +                     }
8491 +                     temp = (char *)realloc(temp,len);
8492 +                     if ( temp == NULL ){
8493 +                       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8494 +                       return NULL;
8495 +                     }
8496 +                     temp[0]='\0';
8497 +                     strncpy(temp, "Provides:", 12);
8498 +                    for(i = 0; i < pkg->provides_count; i++) {
8499 +                         if (strlen(pkg->provides_str[i])>0){;
8500 +                            snprintf(provstr, LINE_LEN, "%s %s", i == 1 ? "" : ",", pkg->provides_str[i]);
8501 +                            strncat(temp, provstr, strlen(provstr));           
8502 +                         }
8503 +                     }
8504 +                     strncat(temp, "\n", strlen("\n")); 
8505 +                  }
8506 +               }
8507 +         } else {
8508 +              goto UNKNOWN_FMT_FIELD;
8509 +         }
8510 +     }
8511 +         break;
8512 +     case 'r':
8513 +     case 'R': {
8514 +         int i;
8515 +         /* Replaces | Recommends*/
8516 +         if (strcasecmp (field, "Replaces") == 0) {
8517 +              if (pkg->replaces_count) {
8518 +                    char replstr[LINE_LEN];
8519 +                    len = 14;
8520 +                   for (i = 0; i < pkg->replaces_count; i++) {
8521 +                        len = len + (strlen(pkg->replaces_str[i])+5);
8522 +                    }
8523 +                    temp = (char *)realloc(temp,len);
8524 +                    if ( temp == NULL ){
8525 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8526 +                      return NULL;
8527 +                    }
8528 +                    temp[0]='\0';
8529 +                    strncpy(temp, "Replaces:", 12);
8530 +                   for (i = 0; i < pkg->replaces_count; i++) {
8531 +                        snprintf(replstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->replaces_str[i]);
8532 +                        strncat(temp, replstr, strlen(replstr));           
8533 +                    }
8534 +                    strncat(temp, "\n", strlen("\n")); 
8535 +              }
8536 +         } else if (strcasecmp (field, "Recommends") == 0) {
8537 +              if (pkg->recommends_count) {
8538 +                    char recstr[LINE_LEN];
8539 +                    len = 15;
8540 +                   for(i = 0; i < pkg->recommends_count; i++) {
8541 +                         len = len + (strlen( pkg->recommends_str[i])+5);
8542 +                    }
8543 +                    temp = (char *)realloc(temp,len);
8544 +                   if ( temp == NULL ){
8545 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8546 +                     return NULL;
8547 +                   }
8548 +                    temp[0]='\0';
8549 +                    strncpy(temp, "Recommends:", 13);
8550 +                   for(i = 0; i < pkg->recommends_count; i++) {
8551 +                        snprintf(recstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->recommends_str[i]);
8552 +                        strncat(temp, recstr, strlen(recstr));           
8553 +                    }
8554 +                    strncat(temp, "\n", strlen("\n")); 
8555 +              }
8556 +         } else {
8557 +              goto UNKNOWN_FMT_FIELD;
8558 +         }
8559 +     }
8560 +         break;
8561 +     case 's':
8562 +     case 'S': {
8563 +         /* Section | Size | Source | Status | Suggests */
8564 +         if (strcasecmp(field, "Section") == 0) {
8565 +              /* Section */
8566 +              if (pkg->section) {
8567 +                   temp = (char *)realloc(temp,strlen(pkg->section)+11);
8568 +                   if ( temp == NULL ){
8569 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8570 +                     return NULL;
8571 +                   }
8572 +                   temp[0]='\0';
8573 +                   snprintf(temp, (strlen(pkg->section)+11), "Section: %s\n", pkg->section);
8574 +              }
8575 +         } else if (strcasecmp(field, "Size") == 0) {
8576 +              /* Size */
8577 +              if (pkg->size) {
8578 +                   temp = (char *)realloc(temp,strlen(pkg->size)+8);
8579 +                   if ( temp == NULL ){
8580 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8581 +                     return NULL;
8582 +                   }
8583 +                   temp[0]='\0';
8584 +                   snprintf(temp, (strlen(pkg->size)+8), "Size: %s\n", pkg->size);
8585 +              }
8586 +         } else if (strcasecmp(field, "Source") == 0) {
8587 +              /* Source */
8588 +              if (pkg->source) {
8589 +                   temp = (char *)realloc(temp,strlen(pkg->source)+10);
8590 +                   if ( temp == NULL ){
8591 +                     fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8592 +                     return NULL;
8593 +                   }
8594 +                   temp[0]='\0';
8595 +                   snprintf(temp, (strlen(pkg->source)+10), "Source: %s\n", pkg->source);
8596 +               }
8597 +         } else if (strcasecmp(field, "Status") == 0) {
8598 +              /* Status */
8599 +               /* Benjamin Pineau note: we should avoid direct usage of 
8600 +                * strlen(arg) without keeping "arg" for later free()
8601 +                */
8602 +               char *pflag=pkg_state_flag_to_str(pkg->state_flag);
8603 +               char *pstat=pkg_state_status_to_str(pkg->state_status);
8604 +               char *pwant=pkg_state_want_to_str(pkg->state_want);
8605 +
8606 +               size_t sum_of_sizes = (size_t) ( strlen(pwant)+ strlen(pflag)+ strlen(pstat) + 12 );
8607 +               temp = (char *)realloc(temp,sum_of_sizes);
8608 +               if ( temp == NULL ){
8609 +                   fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8610 +                   return NULL;
8611 +                }
8612 +                temp[0]='\0';
8613 +                snprintf(temp, sum_of_sizes , "Status: %s %s %s\n", pwant, pflag, pstat);
8614 +                free(pflag);
8615 +                free(pwant);
8616 +               if(pstat) /* pfstat can be NULL if ENOMEM */
8617 +                   free(pstat);
8618 +         } else if (strcasecmp(field, "Suggests") == 0) {
8619 +              if (pkg->suggests_count) {
8620 +                   int i;
8621 +                    char sugstr[LINE_LEN];
8622 +                    len = 13;
8623 +                   for(i = 0; i < pkg->suggests_count; i++) {
8624 +                        len = len + (strlen(pkg->suggests_str[i])+5);
8625 +                    }
8626 +                    temp = (char *)realloc(temp,len);
8627 +                    if ( temp == NULL ){
8628 +                      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8629 +                      return NULL;
8630 +                    }
8631 +                    temp[0]='\0';
8632 +                    strncpy(temp, "Suggests:", 10);
8633 +                   for(i = 0; i < pkg->suggests_count; i++) {
8634 +                        snprintf(sugstr, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->suggests_str[i]);
8635 +                        strncat(temp, sugstr, strlen(sugstr));           
8636 +                    }
8637 +                    strncat(temp, "\n", strlen("\n")); 
8638 +              }
8639 +         } else {
8640 +              goto UNKNOWN_FMT_FIELD;
8641 +         }
8642 +     }
8643 +         break;
8644 +     case 'v':
8645 +     case 'V': {
8646 +         /* Version */
8647 +         char *version = pkg_version_str_alloc(pkg);
8648 +          temp = (char *)realloc(temp,strlen(version)+14);
8649 +          if ( temp == NULL ){
8650 +             fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8651 +             return NULL;
8652 +          }
8653 +          temp[0]='\0';
8654 +          snprintf(temp, (strlen(version)+12), "Version: %s\n", version);
8655 +         free(version);
8656 +     }
8657 +         break;
8658 +     default:
8659 +         goto UNKNOWN_FMT_FIELD;
8660 +     }
8661 +
8662 +     if ( strlen(temp)<2 ) {
8663 +          temp[0]='\0';
8664 +     }
8665 +     return temp;
8666 +
8667 + UNKNOWN_FMT_FIELD:
8668 +     fprintf(stderr, "%s: ERROR: Unknown field name: %s\n", __FUNCTION__, field);
8669 +     if ( strlen(temp)<2 ) {
8670 +          temp[0]='\0';
8671 +     }
8672 +
8673 +     return temp;
8674 +}
8675 +
8676 +void pkg_print_info(pkg_t *pkg, FILE *file)
8677 +{
8678 +     char * buff;
8679 +     if (pkg == NULL) {
8680 +       return;
8681 +     }
8682 +
8683 +     buff = pkg_formatted_info(pkg);
8684 +     if ( buff == NULL ) 
8685 +         return;
8686 +     if (strlen(buff)>2){
8687 +         fwrite(buff, 1, strlen(buff), file);
8688 +     } 
8689 +     free(buff);
8690 +}
8691 +
8692 +void pkg_print_status(pkg_t * pkg, FILE * file)
8693 +{
8694 +     if (pkg == NULL) {
8695 +         return;
8696 +     }
8697 +
8698 +     /* XXX: QUESTION: Do we actually want more fields here? The
8699 +       original idea was to save space by installing only what was
8700 +       needed for actual computation, (package, version, status,
8701 +       essential, conffiles). The assumption is that all other fields
8702 +       can be found in th available file.
8703 +
8704 +       But, someone proposed the idea to make it possible to
8705 +       reconstruct a .ipk from an installed package, (ie. for beaming
8706 +       from one handheld to another). So, maybe we actually want a few
8707 +       more fields here, (depends, suggests, etc.), so that that would
8708 +       be guaranteed to work even in the absence of more information
8709 +       from the available file.
8710 +
8711 +       28-MAR-03: kergoth and I discussed this yesterday.  We think
8712 +       the essential info needs to be here for all installed packages
8713 +       because they may not appear in the Packages files on various
8714 +       feeds.  Furthermore, one should be able to install from URL or
8715 +       local storage without requiring a Packages file from any feed.
8716 +       -Jamey
8717 +     */
8718 +     pkg_print_field(pkg, file, "Package");
8719 +     pkg_print_field(pkg, file, "Version");
8720 +     pkg_print_field(pkg, file, "Depends");
8721 +     pkg_print_field(pkg, file, "Recommends");
8722 +     pkg_print_field(pkg, file, "Suggests");
8723 +     pkg_print_field(pkg, file, "Provides");
8724 +     pkg_print_field(pkg, file, "Replaces");
8725 +     pkg_print_field(pkg, file, "Conflicts");
8726 +     pkg_print_field(pkg, file, "Status");
8727 +     pkg_print_field(pkg, file, "Essential"); /* @@@@ should be removed in future release. */
8728 +     pkg_print_field(pkg, file, "Architecture");
8729 +     pkg_print_field(pkg, file, "Conffiles");
8730 +     pkg_print_field(pkg, file, "Installed-Time");
8731 +     fputs("\n", file);
8732 +}
8733 +
8734 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field)
8735 +{
8736 +     char *buff;
8737 +     if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8738 +       fprintf(stderr, "%s: ERROR: Unknown field name: %s\n",
8739 +            __FUNCTION__, field);
8740 +     }
8741 +     buff = pkg_formatted_field(pkg, field);
8742 +     if (strlen(buff)>2) {
8743 +       fprintf(file, "%s", buff);
8744 +       fflush(file);
8745 +     }
8746 +     free(buff);
8747 +     return;
8748 +}
8749 +
8750 +/*
8751 + * libdpkg - Debian packaging suite library routines
8752 + * vercmp.c - comparison of version numbers
8753 + *
8754 + * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
8755 + */
8756 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg)
8757 +{
8758 +     int r;
8759 +
8760 +     if (pkg->epoch > ref_pkg->epoch) {
8761 +         return 1;
8762 +     }
8763 +
8764 +     if (pkg->epoch < ref_pkg->epoch) {
8765 +         return -1;
8766 +     }
8767 +
8768 +     r = verrevcmp(pkg->version, ref_pkg->version);
8769 +     if (r) {
8770 +         return r;
8771 +     }
8772 +
8773 +#ifdef USE_DEBVERSION
8774 +     r = verrevcmp(pkg->revision, ref_pkg->revision);
8775 +     if (r) {
8776 +         return r;
8777 +     }
8778 +
8779 +     r = verrevcmp(pkg->familiar_revision, ref_pkg->familiar_revision);
8780 +#endif
8781 +
8782 +     return r;
8783 +}
8784 +
8785 +int verrevcmp(const char *val, const char *ref)
8786 +{
8787 +     int vc, rc;
8788 +     long vl, rl;
8789 +     const char *vp, *rp;
8790 +     const char *vsep, *rsep;
8791 +    
8792 +     if (!val) val= "";
8793 +     if (!ref) ref= "";
8794 +     for (;;) {
8795 +         vp= val;  while (*vp && !isdigit(*vp)) vp++;
8796 +         rp= ref;  while (*rp && !isdigit(*rp)) rp++;
8797 +         for (;;) {
8798 +              vc= (val == vp) ? 0 : *val++;
8799 +              rc= (ref == rp) ? 0 : *ref++;
8800 +              if (!rc && !vc) break;
8801 +              if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
8802 +              if (rc && !isalpha(rc)) rc += 256;
8803 +              if (vc != rc) return vc - rc;
8804 +         }
8805 +         val= vp;
8806 +         ref= rp;
8807 +         vl=0;  if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
8808 +         rl=0;  if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
8809 +         if (vl != rl) return vl - rl;
8810 +
8811 +         vc = *val;
8812 +         rc = *ref;
8813 +         vsep = strchr(".-", vc);
8814 +         rsep = strchr(".-", rc);
8815 +         if (vsep && !rsep) return -1;
8816 +         if (!vsep && rsep) return +1;
8817 +
8818 +         if (!*val && !*ref) return 0;
8819 +         if (!*val) return -1;
8820 +         if (!*ref) return +1;
8821 +     }
8822 +}
8823 +
8824 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op)
8825 +{
8826 +     int r;
8827 +
8828 +     r = pkg_compare_versions(it, ref);
8829 +
8830 +     if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
8831 +         return r <= 0;
8832 +     }
8833 +
8834 +     if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
8835 +         return r >= 0;
8836 +     }
8837 +
8838 +     if (strcmp(op, "<<") == 0) {
8839 +         return r < 0;
8840 +     }
8841 +
8842 +     if (strcmp(op, ">>") == 0) {
8843 +         return r > 0;
8844 +     }
8845 +
8846 +     if (strcmp(op, "=") == 0) {
8847 +         return r == 0;
8848 +     }
8849 +
8850 +     fprintf(stderr, "unknown operator: %s", op);
8851 +     return 0;
8852 +}
8853 +
8854 +int pkg_name_version_and_architecture_compare(void *p1, void *p2)
8855 +{
8856 +     const pkg_t *a = *(const pkg_t **)p1;
8857 +     const pkg_t *b = *(const pkg_t **)p2;
8858 +     int namecmp;
8859 +     int vercmp;
8860 +     if (!a->name || !b->name) {
8861 +       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->name=%p b=%p b->name=%p\n",
8862 +              a, a->name, b, b->name);
8863 +       return 0;
8864 +     }
8865 +       
8866 +     namecmp = strcmp(a->name, b->name);
8867 +     if (namecmp)
8868 +         return namecmp;
8869 +     vercmp = pkg_compare_versions(a, b);
8870 +     if (vercmp)
8871 +         return vercmp;
8872 +     if (!a->arch_priority || !b->arch_priority) {
8873 +       fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->arch_priority=%i b=%p b->arch_priority=%i\n",
8874 +              a, a->arch_priority, b, b->arch_priority);
8875 +       return 0;
8876 +     }
8877 +     if (a->arch_priority > b->arch_priority)
8878 +         return 1;
8879 +     if (a->arch_priority < b->arch_priority)
8880 +         return -1;
8881 +     return 0;
8882 +}
8883 +
8884 +int abstract_pkg_name_compare(void *p1, void *p2)
8885 +{
8886 +     const abstract_pkg_t *a = *(const abstract_pkg_t **)p1;
8887 +     const abstract_pkg_t *b = *(const abstract_pkg_t **)p2;
8888 +     if (!a->name || !b->name) {
8889 +       fprintf(stderr, "abstract_pkg_name_compare: a=%p a->name=%p b=%p b->name=%p\n",
8890 +              a, a->name, b, b->name);
8891 +       return 0;
8892 +     }
8893 +     return strcmp(a->name, b->name);
8894 +}
8895 +
8896 +
8897 +char *pkg_version_str_alloc(pkg_t *pkg)
8898 +{
8899 +     char *complete_version;
8900 +     char *epoch_str;
8901 +#ifdef USE_DEBVERSION
8902 +     char *revision_str;
8903 +     char *familiar_revision_str;
8904 +#endif
8905 +
8906 +     if (pkg->epoch) {
8907 +         sprintf_alloc(&epoch_str, "%d:", pkg->epoch);
8908 +     } else {
8909 +         epoch_str = strdup("");
8910 +     }
8911 +
8912 +#ifdef USE_DEBVERSION
8913 +     if (pkg->revision && strlen(pkg->revision)) {
8914 +         sprintf_alloc(&revision_str, "-%s", pkg->revision);
8915 +     } else {
8916 +         revision_str = strdup("");
8917 +     }
8918 +
8919 +     if (pkg->familiar_revision && strlen(pkg->familiar_revision)) {
8920 +         sprintf_alloc(&familiar_revision_str, "-fam%s", pkg->familiar_revision);
8921 +     } else {
8922 +         familiar_revision_str = strdup("");
8923 +     }
8924 +#endif
8925 +
8926 +#ifdef USE_DEBVERSION
8927 +     sprintf_alloc(&complete_version, "%s%s%s%s",
8928 +                  epoch_str, pkg->version, revision_str, familiar_revision_str);
8929 +#else
8930 +     sprintf_alloc(&complete_version, "%s%s",
8931 +                  epoch_str, pkg->version);
8932 +#endif
8933 +
8934 +     free(epoch_str);
8935 +#ifdef USE_DEBVERSION
8936 +     free(revision_str);
8937 +     free(familiar_revision_str);
8938 +#endif
8939 +
8940 +     return complete_version;
8941 +}
8942 +
8943 +str_list_t *pkg_get_installed_files(pkg_t *pkg)
8944 +{
8945 +     int err;
8946 +     char *list_file_name = NULL;
8947 +     FILE *list_file = NULL;
8948 +     char *line;
8949 +     char *installed_file_name;
8950 +     int rootdirlen;
8951 +
8952 +     pkg->installed_files_ref_cnt++;
8953 +
8954 +     if (pkg->installed_files) {
8955 +         return pkg->installed_files;
8956 +     }
8957 +
8958 +     pkg->installed_files = str_list_alloc();
8959 +     if (pkg->installed_files == NULL) {
8960 +         fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8961 +         return NULL;
8962 +     }
8963 +
8964 +     /* For uninstalled packages, get the file list firectly from the package.
8965 +       For installed packages, look at the package.list file in the database.
8966 +     */
8967 +     if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
8968 +         if (pkg->local_filename == NULL) {
8969 +              return pkg->installed_files;
8970 +         }
8971 +         /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
8972 +            file. In other words, change deb_extract so that it can
8973 +            simply return the file list as a char *[] rather than
8974 +            insisting on writing in to a FILE * as it does now. */
8975 +         list_file = tmpfile();
8976 +         err = pkg_extract_data_file_names_to_stream(pkg, list_file);
8977 +         if (err) {
8978 +              fclose(list_file);
8979 +              fprintf(stderr, "%s: Error extracting file list from %s: %s\n",
8980 +                      __FUNCTION__, pkg->local_filename, strerror(err));
8981 +              return pkg->installed_files;
8982 +         }
8983 +         rewind(list_file);
8984 +     } else {
8985 +         sprintf_alloc(&list_file_name, "%s/%s.list",
8986 +                       pkg->dest->info_dir, pkg->name);
8987 +         if (! file_exists(list_file_name)) {
8988 +              free(list_file_name);
8989 +              return pkg->installed_files;
8990 +         }
8991 +
8992 +         list_file = fopen(list_file_name, "r");
8993 +         if (list_file == NULL) {
8994 +              fprintf(stderr, "WARNING: Cannot open %s: %s\n",
8995 +                      list_file_name, strerror(errno));
8996 +              free(list_file_name);
8997 +              return pkg->installed_files;
8998 +         }
8999 +         free(list_file_name);
9000 +     }
9001 +
9002 +     rootdirlen = strlen( pkg->dest->root_dir );
9003 +     while (1) {
9004 +         char *file_name;
9005 +       
9006 +         line = file_read_line_alloc(list_file);
9007 +         if (line == NULL) {
9008 +              break;
9009 +         }
9010 +         str_chomp(line);
9011 +         file_name = line;
9012 +
9013 +         /* Take pains to avoid uglies like "/./" in the middle of file_name. */
9014 +         if( strncmp( pkg->dest->root_dir, 
9015 +                      file_name, 
9016 +                      rootdirlen ) ) {
9017 +              if (*file_name == '.') {
9018 +                   file_name++;
9019 +              }
9020 +              if (*file_name == '/') {
9021 +                   file_name++;
9022 +              }
9023 +
9024 +              /* Freed in pkg_free_installed_files */
9025 +              sprintf_alloc(&installed_file_name, "%s%s", pkg->dest->root_dir, file_name);
9026 +         } else {
9027 +              // already contains root_dir as header -> ABSOLUTE
9028 +              sprintf_alloc(&installed_file_name, "%s", file_name);
9029 +         }
9030 +         str_list_append(pkg->installed_files, installed_file_name);
9031 +         free(line);
9032 +     }
9033 +
9034 +     fclose(list_file);
9035 +
9036 +     return pkg->installed_files;
9037 +}
9038 +
9039 +/* XXX: CLEANUP: This function and it's counterpart,
9040 +   (pkg_get_installed_files), do not match our init/deinit naming
9041 +   convention. Nor the alloc/free convention. But, then again, neither
9042 +   of these conventions currrently fit the way these two functions
9043 +   work. */
9044 +int pkg_free_installed_files(pkg_t *pkg)
9045 +{
9046 +     str_list_elt_t *iter;
9047 +
9048 +     pkg->installed_files_ref_cnt--;
9049 +     if (pkg->installed_files_ref_cnt > 0) {
9050 +         return 0;
9051 +     }
9052 +
9053 +     if (pkg->installed_files) {
9054 +
9055 +         for (iter = pkg->installed_files->head; iter; iter = iter->next) {
9056 +              /* malloced in pkg_get_installed_files */
9057 +              free (iter->data);
9058 +              iter->data = NULL;
9059 +         }
9060 +
9061 +         str_list_deinit(pkg->installed_files);
9062 +     }
9063 +
9064 +     pkg->installed_files = NULL;
9065 +
9066 +     return 0;
9067 +}
9068 +
9069 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg)
9070 +{
9071 +     int err;
9072 +     char *list_file_name;
9073 +
9074 +     //I don't think pkg_free_installed_files should be called here. Jamey
9075 +     //pkg_free_installed_files(pkg);
9076 +
9077 +     sprintf_alloc(&list_file_name, "%s/%s.list",
9078 +                  pkg->dest->info_dir, pkg->name);
9079 +     if (!conf->noaction) {
9080 +         err = unlink(list_file_name);
9081 +         free(list_file_name);
9082 +
9083 +         if (err) {
9084 +              return errno;
9085 +         }
9086 +     }
9087 +     return 0;
9088 +}
9089 +
9090 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name)
9091 +{
9092 +     conffile_list_elt_t *iter;
9093 +     conffile_t *conffile;
9094 +
9095 +     if (pkg == NULL) {
9096 +         return NULL;
9097 +     }
9098 +
9099 +     for (iter = pkg->conffiles.head; iter; iter = iter->next) {
9100 +         conffile = iter->data;
9101 +
9102 +         if (strcmp(conffile->name, file_name) == 0) {
9103 +              return conffile;
9104 +         }
9105 +     }
9106 +
9107 +     return NULL;
9108 +}
9109 +
9110 +int pkg_run_script(ipkg_conf_t *conf, pkg_t *pkg,
9111 +                  const char *script, const char *args)
9112 +{
9113 +     int err;
9114 +     char *path;
9115 +     char *cmd;
9116 +
9117 +     /* XXX: FEATURE: When conf->offline_root is set, we should run the
9118 +       maintainer script within a chroot environment. */
9119 +
9120 +     /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
9121 +       have scripts in pkg->tmp_unpack_dir. */
9122 +     if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
9123 +         if (pkg->dest == NULL) {
9124 +              fprintf(stderr, "%s: ERROR: installed package %s has a NULL dest\n",
9125 +                      __FUNCTION__, pkg->name);
9126 +              return EINVAL;
9127 +         }
9128 +         sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, script);
9129 +     } else {
9130 +         if (pkg->tmp_unpack_dir == NULL) {
9131 +              fprintf(stderr, "%s: ERROR: uninstalled package %s has a NULL tmp_unpack_dir\n",
9132 +                      __FUNCTION__, pkg->name);
9133 +              return EINVAL;
9134 +         }
9135 +         sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script);
9136 +     }
9137 +
9138 +     ipkg_message(conf, IPKG_INFO, "Running script %s\n", path);
9139 +     if (conf->noaction) return 0;
9140 +
9141 +     /* XXX: CLEANUP: There must be a better way to handle maintainer
9142 +       scripts when running with offline_root mode and/or a dest other
9143 +       than '/'. I've been playing around with some clever chroot
9144 +       tricks and I might come up with something workable. */
9145 +     if (conf->offline_root) {
9146 +         setenv("IPKG_OFFLINE_ROOT", conf->offline_root, 1);
9147 +     }
9148 +
9149 +     setenv("PKG_ROOT",
9150 +           pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, 1);
9151 +
9152 +     if (! file_exists(path)) {
9153 +         free(path);
9154 +         return 0;
9155 +     }
9156 +
9157 +     if (conf->offline_root) {
9158 +         fprintf(stderr, "(offline root mode: not running %s.%s)\n", pkg->name, script);
9159 +         free(path);
9160 +         return 0;
9161 +     }
9162 +
9163 +     sprintf_alloc(&cmd, "%s %s", path, args);
9164 +     free(path);
9165 +
9166 +     err = xsystem(cmd);
9167 +     free(cmd);
9168 +
9169 +     if (err) {
9170 +         fprintf(stderr, "%s script returned status %d\n", script, err);
9171 +         return err;
9172 +     }
9173 +
9174 +     return 0;
9175 +}
9176 +
9177 +char *pkg_state_want_to_str(pkg_state_want_t sw)
9178 +{
9179 +     int i;
9180 +
9181 +     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
9182 +         if (pkg_state_want_map[i].value == sw) {
9183 +              return strdup(pkg_state_want_map[i].str);
9184 +         }
9185 +     }
9186 +
9187 +     fprintf(stderr, "%s: ERROR: Illegal value for state_want: %d\n",
9188 +            __FUNCTION__, sw);
9189 +     return strdup("<STATE_WANT_UNKNOWN>");
9190 +}
9191 +
9192 +pkg_state_want_t pkg_state_want_from_str(char *str)
9193 +{
9194 +     int i;
9195 +
9196 +     for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
9197 +         if (strcmp(str, pkg_state_want_map[i].str) == 0) {
9198 +              return pkg_state_want_map[i].value;
9199 +         }
9200 +     }
9201 +
9202 +     fprintf(stderr, "%s: ERROR: Illegal value for state_want string: %s\n",
9203 +            __FUNCTION__, str);
9204 +     return SW_UNKNOWN;
9205 +}
9206 +
9207 +char *pkg_state_flag_to_str(pkg_state_flag_t sf)
9208 +{
9209 +     int i;
9210 +     int len = 3; /* ok\000 is minimum */
9211 +     char *str = NULL;
9212 +
9213 +     /* clear the temporary flags before converting to string */
9214 +     sf &= SF_NONVOLATILE_FLAGS;
9215 +
9216 +     if (sf == 0) {
9217 +         return strdup("ok");
9218 +     } else {
9219 +
9220 +         for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9221 +              if (sf & pkg_state_flag_map[i].value) {
9222 +                   len += strlen(pkg_state_flag_map[i].str) + 1;
9223 +              }
9224 +         }
9225 +         str = malloc(len);
9226 +          if ( str == NULL ) {
9227 +             fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9228 +              return NULL;
9229 +          }
9230 +         str[0] = 0;
9231 +         for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9232 +              if (sf & pkg_state_flag_map[i].value) {
9233 +                   strcat(str, pkg_state_flag_map[i].str);
9234 +                   strcat(str, ",");
9235 +              }
9236 +         }
9237 +         len = strlen(str);
9238 +         str[len-1] = 0; /* squash last comma */
9239 +         return str;
9240 +     }
9241 +}
9242 +
9243 +pkg_state_flag_t pkg_state_flag_from_str(char *str)
9244 +{
9245 +     int i;
9246 +     int sf = SF_OK;
9247 +
9248 +     if (strcmp(str, "ok") == 0) {
9249 +         return SF_OK;
9250 +     }
9251 +     for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9252 +         const char *sfname = pkg_state_flag_map[i].str;
9253 +         int sfname_len = strlen(sfname);
9254 +         if (strncmp(str, sfname, sfname_len) == 0) {
9255 +              sf |= pkg_state_flag_map[i].value;
9256 +              str += sfname_len;
9257 +              if (str[0] == ',') {
9258 +                   str++;
9259 +              } else {
9260 +                   break;
9261 +              }
9262 +         }
9263 +     }
9264 +
9265 +     return sf;
9266 +}
9267 +
9268 +char *pkg_state_status_to_str(pkg_state_status_t ss)
9269 +{
9270 +     int i;
9271 +
9272 +     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9273 +         if (pkg_state_status_map[i].value == ss) {
9274 +              return strdup(pkg_state_status_map[i].str);
9275 +         }
9276 +     }
9277 +
9278 +     fprintf(stderr, "%s: ERROR: Illegal value for state_status: %d\n",
9279 +            __FUNCTION__, ss);
9280 +     return strdup("<STATE_STATUS_UNKNOWN>");
9281 +}
9282 +
9283 +pkg_state_status_t pkg_state_status_from_str(char *str)
9284 +{
9285 +     int i;
9286 +
9287 +     for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9288 +         if (strcmp(str, pkg_state_status_map[i].str) == 0) {
9289 +              return pkg_state_status_map[i].value;
9290 +         }
9291 +     }
9292 +
9293 +     fprintf(stderr, "%s: ERROR: Illegal value for state_status string: %s\n",
9294 +            __FUNCTION__, str);
9295 +     return SS_NOT_INSTALLED;
9296 +}
9297 +
9298 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg)
9299 +{
9300 +     nv_pair_list_elt_t *l;
9301 +
9302 +     if (!pkg->architecture)
9303 +         return 1;
9304 +
9305 +     l = conf->arch_list.head;
9306 +
9307 +     while (l) {
9308 +         nv_pair_t *nv = l->data;
9309 +         if (strcmp(nv->name, pkg->architecture) == 0) {
9310 +              ipkg_message(conf, IPKG_DEBUG, "arch %s (priority %s) supported for pkg %s\n", nv->name, nv->value, pkg->name);
9311 +              return 1;
9312 +         }
9313 +         l = l->next;
9314 +     }
9315 +
9316 +     ipkg_message(conf, IPKG_DEBUG, "arch %s unsupported for pkg %s\n", pkg->architecture, pkg->name);
9317 +     return 0;
9318 +}
9319 +
9320 +int pkg_get_arch_priority(ipkg_conf_t *conf, const char *archname)
9321 +{
9322 +     nv_pair_list_elt_t *l;
9323 +
9324 +     l = conf->arch_list.head;
9325 +
9326 +     while (l) {
9327 +         nv_pair_t *nv = l->data;
9328 +         if (strcmp(nv->name, archname) == 0) {
9329 +              int priority = strtol(nv->value, NULL, 0);
9330 +              return priority;
9331 +         }
9332 +         l = l->next;
9333 +     }
9334 +     return 0;
9335 +}
9336 +
9337 +int pkg_info_preinstall_check(ipkg_conf_t *conf)
9338 +{
9339 +     int i;
9340 +     hash_table_t *pkg_hash = &conf->pkg_hash;
9341 +     pkg_vec_t *available_pkgs = pkg_vec_alloc();
9342 +     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9343 +
9344 +     ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: updating arch priority for each package\n");
9345 +     pkg_hash_fetch_available(pkg_hash, available_pkgs);
9346 +     /* update arch_priority for each package */
9347 +     for (i = 0; i < available_pkgs->len; i++) {
9348 +         pkg_t *pkg = available_pkgs->pkgs[i];
9349 +         int arch_priority = 1;
9350 +         if (!pkg)
9351 +              continue;
9352 +         // ipkg_message(conf, IPKG_DEBUG2, " package %s version=%s arch=%p:", pkg->name, pkg->version, pkg->architecture);
9353 +         if (pkg->architecture) 
9354 +              arch_priority = pkg_get_arch_priority(conf, pkg->architecture);
9355 +         else 
9356 +              ipkg_message(conf, IPKG_ERROR, "pkg_info_preinstall_check: no architecture for package %s\n", pkg->name);
9357 +         // ipkg_message(conf, IPKG_DEBUG2, "%s arch_priority=%d\n", pkg->architecture, arch_priority);
9358 +         pkg->arch_priority = arch_priority;
9359 +     }
9360 +
9361 +     for (i = 0; i < available_pkgs->len; i++) {
9362 +         pkg_t *pkg = available_pkgs->pkgs[i];
9363 +         if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
9364 +              /* clear flags and want for any uninstallable package */
9365 +              ipkg_message(conf, IPKG_NOTICE, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n", 
9366 +                           pkg->name, pkg->arch_priority, pkg->state_flag, pkg->state_want);
9367 +              pkg->state_want = SW_UNKNOWN;
9368 +              pkg->state_flag = 0;
9369 +         }
9370 +     }
9371 +     pkg_vec_free(available_pkgs);
9372 +
9373 +     /* update the file owner data structure */
9374 +     ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: update file owner list\n");
9375 +     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9376 +     for (i = 0; i < installed_pkgs->len; i++) {
9377 +         pkg_t *pkg = installed_pkgs->pkgs[i];
9378 +         str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
9379 +         str_list_elt_t *iter;
9380 +         if (installed_files == NULL) {
9381 +              ipkg_message(conf, IPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
9382 +              break;
9383 +         }
9384 +         for (iter = installed_files->head; iter; iter = iter->next) {
9385 +              char *installed_file = iter->data;
9386 +              // ipkg_message(conf, IPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
9387 +              file_hash_set_file_owner(conf, installed_file, pkg);
9388 +         }
9389 +     }
9390 +     pkg_vec_free(installed_pkgs);
9391 +
9392 +     return 0;
9393 +}
9394 +
9395 +struct pkg_write_filelist_data {
9396 +     ipkg_conf_t *conf;
9397 +     pkg_t *pkg;
9398 +     FILE *stream;
9399 +};
9400 +
9401 +void pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
9402 +{
9403 +     struct pkg_write_filelist_data *data = data_;
9404 +     pkg_t *entry = entry_;
9405 +     if (entry == data->pkg) {
9406 +         fprintf(data->stream, "%s\n", key);
9407 +     }
9408 +}
9409 +
9410 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg)
9411 +{
9412 +     struct pkg_write_filelist_data data;
9413 +     char *list_file_name = NULL;
9414 +     int err = 0;
9415 +
9416 +     if (!pkg) {
9417 +         ipkg_message(conf, IPKG_ERROR, "Null pkg\n");
9418 +         return -EINVAL;
9419 +     }
9420 +     ipkg_message(conf, IPKG_INFO,
9421 +                 "    creating %s.list file\n", pkg->name);
9422 +     sprintf_alloc(&list_file_name, "%s/%s.list", pkg->dest->info_dir, pkg->name);
9423 +     if (!list_file_name) {
9424 +         ipkg_message(conf, IPKG_ERROR, "Failed to alloc list_file_name\n");
9425 +         return -ENOMEM;
9426 +     }
9427 +     ipkg_message(conf, IPKG_INFO,
9428 +                 "    creating %s file for pkg %s\n", list_file_name, pkg->name);
9429 +     data.stream = fopen(list_file_name, "w");
9430 +     if (!data.stream) {
9431 +         ipkg_message(conf, IPKG_ERROR, "Could not open %s for writing: %s\n",
9432 +                      list_file_name, strerror(errno));
9433 +                      return errno;
9434 +     }
9435 +     data.pkg = pkg;
9436 +     data.conf = conf;
9437 +     hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
9438 +     fclose(data.stream);
9439 +     free(list_file_name);
9440 +
9441 +     return err;
9442 +}
9443 +
9444 +int pkg_write_changed_filelists(ipkg_conf_t *conf)
9445 +{
9446 +     pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9447 +     hash_table_t *pkg_hash = &conf->pkg_hash;
9448 +     int i;
9449 +     int err;
9450 +     if (conf->noaction)
9451 +         return 0;
9452 +
9453 +     ipkg_message(conf, IPKG_INFO, "%s: saving changed filelists\n", __FUNCTION__);
9454 +     pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9455 +     for (i = 0; i < installed_pkgs->len; i++) {
9456 +         pkg_t *pkg = installed_pkgs->pkgs[i];
9457 +         if (pkg->state_flag & SF_FILELIST_CHANGED) {
9458 +               ipkg_message(conf, IPKG_DEBUG, "Calling pkg_write_filelist for pkg=%s from %s\n", pkg->name, __FUNCTION__);
9459 +              err = pkg_write_filelist(conf, pkg);
9460 +              if (err)
9461 +                   ipkg_message(conf, IPKG_NOTICE, "pkg_write_filelist pkg=%s returned %d\n", pkg->name, err);
9462 +         }
9463 +     }
9464 +     return 0;
9465 +}
9466 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_depends.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.c
9467 --- busybox-1.2.0-orig/archival/libipkg/pkg_depends.c   1970-01-01 01:00:00.000000000 +0100
9468 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.c  2006-07-22 16:31:25.000000000 +0200
9469 @@ -0,0 +1,1033 @@
9470 +/* pkg_depends.c - the itsy package management system
9471 +
9472 +   Steven M. Ayer
9473 +   
9474 +   Copyright (C) 2002 Compaq Computer Corporation
9475 +
9476 +   This program is free software; you can redistribute it and/or
9477 +   modify it under the terms of the GNU General Public License as
9478 +   published by the Free Software Foundation; either version 2, or (at
9479 +   your option) any later version.
9480 +
9481 +   This program is distributed in the hope that it will be useful, but
9482 +   WITHOUT ANY WARRANTY; without even the implied warranty of
9483 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9484 +   General Public License for more details.
9485 +*/
9486 +
9487 +#include "ipkg.h"
9488 +#include <errno.h>
9489 +#include <ctype.h>
9490 +   
9491 +#include "pkg.h"
9492 +#include "ipkg_utils.h"
9493 +#include "pkg_hash.h"
9494 +#include "ipkg_message.h"
9495 +#include "pkg_parse.h"
9496 +#include "hash_table.h"
9497 +
9498 +static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str);
9499 +static depend_t * depend_init(void);
9500 +static void depend_deinit(depend_t *d);
9501 +static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
9502 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
9503 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
9504 +
9505 +static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
9506 +{
9507 +     depend_t *depend = (depend_t *)cdata;
9508 +     if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
9509 +         return 1;
9510 +     else
9511 +         return 0;
9512 +}
9513 +
9514 +static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
9515 +{
9516 +     depend_t *depend = (depend_t *)cdata;
9517 +#if 0
9518 +     pkg_t * temp = pkg_new();
9519 +     int comparison;
9520 +     parseVersion(temp, depend->version);
9521 +     comparison = pkg_compare_versions(pkg, temp);
9522 +     free(temp);
9523 +
9524 +     fprintf(stderr, "%s: pkg=%s pkg->version=%s constraint=%p type=%d version=%s comparison=%d satisfied=%d\n", 
9525 +            __FUNCTION__, pkg->name, pkg->version, 
9526 +            depend, depend->constraint, depend->version,
9527 +            comparison, version_constraints_satisfied(depend, pkg));
9528 +#endif
9529 +     if (version_constraints_satisfied(depend, pkg))
9530 +         return 1;
9531 +     else
9532 +         return 0;
9533 +}
9534 +
9535 +/* returns ndependences or negative error value */ 
9536 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, 
9537 +                                           pkg_vec_t *unsatisfied, char *** unresolved)
9538 +{
9539 +     pkg_t * satisfier_entry_pkg;
9540 +     register int i, j, k;
9541 +     int count, found;
9542 +     char ** the_lost;
9543 +     abstract_pkg_t * ab_pkg;
9544 +
9545 +     /* 
9546 +      * this is a setup to check for redundant/cyclic dependency checks, 
9547 +      * which are marked at the abstract_pkg level
9548 +      */
9549 +     if (!(ab_pkg = pkg->parent)) {
9550 +         fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
9551 +         *unresolved = NULL;
9552 +         return 0;
9553 +     }
9554 +     if (ab_pkg->dependencies_checked) {    /* avoid duplicate or cyclic checks */
9555 +         *unresolved = NULL;
9556 +         return 0;
9557 +     } else { 
9558 +         ab_pkg->dependencies_checked = 1;  /* mark it for subsequent visits */
9559 +     }
9560 +     /**/
9561 +
9562 +     count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
9563 +     if (!count){
9564 +         *unresolved = NULL;
9565 +         return 0;
9566 +     }
9567 +
9568 +     the_lost = NULL;
9569 +       
9570 +     /* foreach dependency */
9571 +     for (i = 0; i < count; i++) {
9572 +         compound_depend_t * compound_depend = &pkg->depends[i];
9573 +         depend_t ** possible_satisfiers = compound_depend->possibilities;;
9574 +         found = 0;
9575 +         satisfier_entry_pkg = NULL;
9576 +
9577 +         if (compound_depend->type == GREEDY_DEPEND) {
9578 +              /* foreach possible satisfier */
9579 +              for (j = 0; j < compound_depend->possibility_count; j++) {
9580 +                   /* foreach provided_by, which includes the abstract_pkg itself */
9581 +                   abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
9582 +                   abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
9583 +                   int nposs = ab_provider_vec->len;
9584 +                   abstract_pkg_t **ab_providers = ab_provider_vec->pkgs; 
9585 +                   int l;
9586 +                   for (l = 0; l < nposs; l++) {
9587 +                        pkg_vec_t *test_vec = ab_providers[l]->pkgs;
9588 +                        /* if no depends on this one, try the first package that Provides this one */
9589 +                        if (!test_vec){   /* no pkg_vec hooked up to the abstract_pkg!  (need another feed?) */
9590 +                             continue;
9591 +                        }
9592 +             
9593 +                        /* cruise this possiblity's pkg_vec looking for an installed version */
9594 +                        for (k = 0; k < test_vec->len; k++) {
9595 +                             pkg_t *pkg_scout = test_vec->pkgs[k];
9596 +                             /* not installed, and not already known about? */
9597 +                             if ((pkg_scout->state_want != SW_INSTALL)
9598 +                                 && !pkg_scout->parent->dependencies_checked
9599 +                                 && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
9600 +                                  char ** newstuff = NULL;
9601 +                                  int rc;
9602 +                                  pkg_vec_t *tmp_vec = pkg_vec_alloc ();
9603 +                                  /* check for not-already-installed dependencies */
9604 +                                  rc = pkg_hash_fetch_unsatisfied_dependencies(conf, 
9605 +                                                                               pkg_scout, 
9606 +                                                                               tmp_vec,
9607 +                                                                               &newstuff);
9608 +                                  if (newstuff == NULL) {
9609 +                                       int i;
9610 +                                       int ok = 1;
9611 +                                       for (i = 0; i < rc; i++) {
9612 +                                           pkg_t *p = tmp_vec->pkgs[i];
9613 +                                           if (p->state_want == SW_INSTALL)
9614 +                                               continue;
9615 +                                           ipkg_message(conf, IPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
9616 +                                           ok = 0;
9617 +                                           break;
9618 +                                       }
9619 +                                       pkg_vec_free (tmp_vec);
9620 +                                       if (ok) {
9621 +                                           /* mark this one for installation */
9622 +                                           ipkg_message(conf, IPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
9623 +                                           pkg_vec_insert(unsatisfied, pkg_scout);
9624 +                                       }
9625 +                                  } else  {
9626 +                                       ipkg_message(conf, IPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
9627 +                                       free (newstuff);
9628 +                                  }
9629 +                             }
9630 +                        }
9631 +                   }
9632 +              }
9633 +
9634 +              continue;
9635 +         }
9636 +
9637 +         /* foreach possible satisfier, look for installed package  */
9638 +         for (j = 0; j < compound_depend->possibility_count; j++) {
9639 +              /* foreach provided_by, which includes the abstract_pkg itself */
9640 +              depend_t *dependence_to_satisfy = possible_satisfiers[j];
9641 +              abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9642 +              pkg_t *satisfying_pkg = 
9643 +                   pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
9644 +                                                              pkg_installed_and_constraint_satisfied, 
9645 +                                                              dependence_to_satisfy, 1);
9646 +               /* Being that I can't test constraing in pkg_hash, I will test it here */
9647 +              if (satisfying_pkg != NULL) {
9648 +                  if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9649 +                     satisfying_pkg = NULL;
9650 +                  }
9651 +               }
9652 +              ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
9653 +              if (satisfying_pkg != NULL) {
9654 +                   found = 1;
9655 +                   break;
9656 +              }
9657 +
9658 +         }
9659 +         /* if nothing installed matches, then look for uninstalled satisfier */
9660 +         if (!found) {
9661 +              /* foreach possible satisfier, look for installed package  */
9662 +              for (j = 0; j < compound_depend->possibility_count; j++) {
9663 +                   /* foreach provided_by, which includes the abstract_pkg itself */
9664 +                   depend_t *dependence_to_satisfy = possible_satisfiers[j];
9665 +                   abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9666 +                   pkg_t *satisfying_pkg = 
9667 +                        pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg, 
9668 +                                                                   pkg_constraint_satisfied, 
9669 +                                                                   dependence_to_satisfy, 1);
9670 +                    /* Being that I can't test constraing in pkg_hash, I will test it here too */
9671 +                   if (satisfying_pkg != NULL) {
9672 +                         if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9673 +                            satisfying_pkg = NULL;
9674 +                         }
9675 +                    }
9676 +
9677 +                   /* user request overrides package recommendation */
9678 +                   if (satisfying_pkg != NULL
9679 +                       && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
9680 +                       && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
9681 +                        ipkg_message (conf, IPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
9682 +                                      pkg->name, satisfying_pkg->name);
9683 +                        continue;
9684 +                   }
9685 +
9686 +                   ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
9687 +                   if (satisfying_pkg != NULL) {
9688 +                        satisfier_entry_pkg = satisfying_pkg;
9689 +                        break;
9690 +                   }
9691 +              }
9692 +         }
9693 +
9694 +         /* we didn't find one, add something to the unsatisfied vector */
9695 +         if (!found) {
9696 +              if (!satisfier_entry_pkg) {
9697 +                   /* failure to meet recommendations is not an error */
9698 +                   if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
9699 +                        the_lost = add_unresolved_dep(pkg, the_lost, i);
9700 +                   else
9701 +                        ipkg_message (conf, IPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
9702 +                                      pkg->name, compound_depend->possibilities[0]->pkg->name);
9703 +              }
9704 +              else {
9705 +                   if (compound_depend->type == SUGGEST) {
9706 +                        /* just mention it politely */
9707 +                        ipkg_message (conf, IPKG_NOTICE, "package %s suggests installing %s\n",
9708 +                                      pkg->name, satisfier_entry_pkg->name);
9709 +                   } else {
9710 +                        char ** newstuff = NULL;
9711 +                        
9712 +                        if (satisfier_entry_pkg != pkg &&
9713 +                            !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
9714 +                             pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
9715 +                             pkg_hash_fetch_unsatisfied_dependencies(conf, 
9716 +                                                                     satisfier_entry_pkg, 
9717 +                                                                     unsatisfied,
9718 +                                                                     &newstuff);
9719 +                             the_lost = merge_unresolved(the_lost, newstuff);
9720 +                        }
9721 +                   }
9722 +              }
9723 +         }
9724 +     }
9725 +     *unresolved = the_lost;
9726 +
9727 +     return unsatisfied->len;
9728 +}
9729 +
9730 +/*checking for conflicts !in replaces 
9731 +  If a packages conflicts with another but is also replacing it, I should not consider it a 
9732 +  really conflicts 
9733 +  returns 0 if conflicts <> replaces or 1 if conflicts == replaces 
9734 +*/
9735 +int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
9736 +{
9737 +    int i ;
9738 +    int replaces_count = pkg->replaces_count;
9739 +    abstract_pkg_t **replaces;
9740 +
9741 +    if (pkg->replaces_count==0)    // No replaces, it's surely a conflict
9742 +        return 0;
9743 +
9744 +    replaces = pkg->replaces;
9745 +
9746 +    for (i = 0; i < replaces_count; i++) {
9747 +        if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) {      // Found
9748 +            ipkg_message(NULL, IPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
9749 +            return 1;
9750 +        }
9751 +    }
9752 +    return 0;
9753 +
9754 +}
9755 +
9756 +
9757 +/* Abhaya: added support for conflicts */
9758 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
9759 +{
9760 +    pkg_vec_t * installed_conflicts, * test_vec;
9761 +    compound_depend_t * conflicts;
9762 +    depend_t ** possible_satisfiers;
9763 +    depend_t * possible_satisfier;
9764 +    register int i, j, k;
9765 +    int count;
9766 +    abstract_pkg_t * ab_pkg;
9767 +    pkg_t **pkg_scouts; 
9768 +    pkg_t *pkg_scout; 
9769 +
9770 +    /* 
9771 +     * this is a setup to check for redundant/cyclic dependency checks, 
9772 +     * which are marked at the abstract_pkg level
9773 +     */
9774 +    if(!(ab_pkg = pkg->parent)){
9775 +       fprintf(stderr, "dependency check error.  pkg %s isn't in hash table\n", pkg->name);
9776 +       return (pkg_vec_t *)NULL;
9777 +    }
9778 +
9779 +    conflicts = pkg->conflicts;
9780 +    if(!conflicts){
9781 +       return (pkg_vec_t *)NULL;
9782 +    }
9783 +    installed_conflicts = pkg_vec_alloc();
9784 +
9785 +    count = pkg->conflicts_count;
9786 +
9787 +
9788 +
9789 +    /* foreach conflict */
9790 +    for(i = 0; i < pkg->conflicts_count; i++){
9791 +
9792 +       possible_satisfiers = conflicts->possibilities;
9793 +
9794 +       /* foreach possible satisfier */
9795 +       for(j = 0; j < conflicts->possibility_count; j++){
9796 +            possible_satisfier = possible_satisfiers[j];
9797 +            if (!possible_satisfier)
9798 +                fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
9799 +            if (!possible_satisfier->pkg)
9800 +                fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
9801 +           test_vec = possible_satisfier->pkg->pkgs;
9802 +           if (test_vec) {
9803 +                /* pkg_vec found, it is an actual package conflict
9804 +                * cruise this possiblity's pkg_vec looking for an installed version */
9805 +               pkg_scouts = test_vec->pkgs;
9806 +               for(k = 0; k < test_vec->len; k++){
9807 +                    pkg_scout = pkg_scouts[k];
9808 +                    if (!pkg_scout) {
9809 +                        fprintf(stderr,  "%s: null pkg scout\n", __FUNCTION__);
9810 +                        continue; 
9811 +                    }
9812 +                   if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
9813 +                      version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
9814 +                       if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
9815 +                           pkg_vec_insert(installed_conflicts, pkg_scout);
9816 +                       }
9817 +                   }
9818 +               }
9819 +           }
9820 +       }
9821 +       conflicts++;
9822 +    }
9823 +
9824 +    if (installed_conflicts->len)
9825 +           return installed_conflicts;
9826 +    pkg_vec_free(installed_conflicts);
9827 +       return (pkg_vec_t *)NULL;
9828 +}
9829 +
9830 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
9831 +{
9832 +    pkg_t * temp;
9833 +    int comparison;
9834 +
9835 +    if(depends->constraint == NONE)
9836 +       return 1;
9837 +
9838 +    temp = pkg_new();
9839 +
9840 +    parseVersion(temp, depends->version);
9841 +
9842 +    comparison = pkg_compare_versions(pkg, temp);
9843 +
9844 +    free(temp);
9845 +
9846 +    if((depends->constraint == EARLIER) && 
9847 +       (comparison < 0))
9848 +       return 1;
9849 +    else if((depends->constraint == LATER) && 
9850 +           (comparison > 0))
9851 +       return 1;
9852 +    else if(comparison == 0)
9853 +       return 1;
9854 +    else if((depends->constraint == LATER_EQUAL) && 
9855 +           (comparison >= 0))
9856 +       return 1;
9857 +    else if((depends->constraint == EARLIER_EQUAL) && 
9858 +           (comparison <= 0))
9859 +       return 1;
9860 +    
9861 +    return 0;
9862 +}
9863 +
9864 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend)
9865 +{
9866 +     abstract_pkg_t *apkg = depend->pkg;
9867 +     abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9868 +     int n_providers = provider_apkgs->len;
9869 +     abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9870 +     pkg_vec_t *pkg_vec;
9871 +     int n_pkgs ; 
9872 +     int i;
9873 +     int j;
9874 +
9875 +     for (i = 0; i < n_providers; i++) {
9876 +         abstract_pkg_t *papkg = apkgs[i];
9877 +         pkg_vec = papkg->pkgs;
9878 +         if (pkg_vec) {
9879 +              n_pkgs = pkg_vec->len;
9880 +              for (j = 0; j < n_pkgs; j++) {
9881 +                   pkg_t *pkg = pkg_vec->pkgs[j];
9882 +                   if (version_constraints_satisfied(depend, pkg)) {
9883 +                        return 1;
9884 +                   }
9885 +              }
9886 +         }
9887 +     }
9888 +     return 0;
9889 +}
9890 +
9891 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend)
9892 +{
9893 +     abstract_pkg_t *apkg = depend->pkg;
9894 +     abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9895 +     int n_providers = provider_apkgs->len;
9896 +     abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9897 +     int i;
9898 +     int n_pkgs;
9899 +     int j;
9900 +
9901 +     for (i = 0; i < n_providers; i++) {
9902 +         abstract_pkg_t *papkg = apkgs[i];
9903 +         pkg_vec_t *pkg_vec = papkg->pkgs;
9904 +         if (pkg_vec) {
9905 +              n_pkgs = pkg_vec->len;
9906 +              for (j = 0; j < n_pkgs; j++) {
9907 +                   pkg_t *pkg = pkg_vec->pkgs[j];
9908 +                   if (version_constraints_satisfied(depend, pkg)) {
9909 +                        if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
9910 +                             return 1;
9911 +                   }
9912 +              }
9913 +         }
9914 +     }
9915 +     return 0;
9916 +}
9917 +
9918 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
9919 +{
9920 +    register int i;
9921 +    pkg_t ** pkgs = vec->pkgs;
9922 +
9923 +    for(i = 0; i < vec->len; i++)
9924 +       if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
9925 +          && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
9926 +          && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
9927 +           return 1;
9928 +    return 0;
9929 +}
9930 +
9931 +
9932 +#ifdef DeadCode
9933 +/**
9934 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
9935 + * the same abstract package and 0 otherwise.
9936 + */
9937 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
9938 +{
9939 +     abstract_pkg_t **provides = pkg->provides;
9940 +     int provides_count = pkg->provides_count;
9941 +     abstract_pkg_t **replacee_provides = replacee->provides;
9942 +     int replacee_provides_count = replacee->provides_count;
9943 +     int i, j;
9944 +     for (i = 0; i < provides_count; i++) {
9945 +         abstract_pkg_t *apkg = provides[i];
9946 +         for (j = 0; j < replacee_provides_count; j++) {
9947 +              abstract_pkg_t *replacee_apkg = replacee_provides[i];
9948 +              if (apkg == replacee_apkg)
9949 +                   return 1;
9950 +         }
9951 +     }
9952 +     return 0;
9953 +}
9954 +#endif
9955 +
9956 +/**
9957 + * pkg_provides_abstract returns 1 if pkg->provides contains providee
9958 + * and 0 otherwise.
9959 + */
9960 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
9961 +{
9962 +     abstract_pkg_t **provides = pkg->provides;
9963 +     int provides_count = pkg->provides_count;
9964 +     int i;
9965 +     for (i = 0; i < provides_count; i++) {
9966 +         if (provides[i] == providee)
9967 +              return 1;
9968 +     }
9969 +     return 0;
9970 +}
9971 +
9972 +/**
9973 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
9974 + * otherwise.
9975 + */
9976 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
9977 +{
9978 +     abstract_pkg_t **replaces = pkg->replaces;
9979 +     int replaces_count = pkg->replaces_count;
9980 +     /* abstract_pkg_t **replacee_provides = pkg->provides;
9981 +     int replacee_provides_count = pkg->provides_count; */
9982 +     int i, j;
9983 +     for (i = 0; i < replaces_count; i++) {
9984 +         abstract_pkg_t *abstract_replacee = replaces[i];
9985 +         for (j = 0; j < replaces_count; j++) {
9986 +   /*            ipkg_message(NULL, IPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
9987 +                 pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
9988 +              if (replacee->provides[j] == abstract_replacee)
9989 +                   return 1;
9990 +         }
9991 +     }
9992 +     return 0;
9993 +}
9994 +
9995 +
9996 +/**
9997 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
9998 + * otherwise.
9999 + */
10000 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
10001 +{
10002 +     compound_depend_t *conflicts = pkg->conflicts;
10003 +     int conflicts_count = pkg->conflicts_count;
10004 +     int i, j;
10005 +     for (i = 0; i < conflicts_count; i++) {
10006 +         int possibility_count = conflicts[i].possibility_count;
10007 +         struct depend **possibilities = conflicts[i].possibilities;
10008 +         for (j = 0; j < possibility_count; j++) {
10009 +              if (possibilities[j]->pkg == conflictee) {
10010 +                   return 1;
10011 +              }
10012 +         }
10013 +     }
10014 +     return 0;
10015 +}
10016 +
10017 +/**
10018 + * pkg_conflicts returns 1 if pkg->conflicts contains one of
10019 + * conflictee's provides and 0 otherwise.
10020 + */
10021 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
10022 +{
10023 +     compound_depend_t *conflicts = pkg->conflicts;
10024 +     int conflicts_count = pkg->conflicts_count;
10025 +     abstract_pkg_t **conflictee_provides = conflictee->provides;
10026 +     int conflictee_provides_count = conflictee->provides_count;
10027 +     int i, j, k;
10028 +     int possibility_count;
10029 +     struct depend **possibilities;
10030 +     abstract_pkg_t *possibility ;
10031 +
10032 +     for (i = 0; i < conflicts_count; i++) {
10033 +         possibility_count = conflicts[i].possibility_count;
10034 +         possibilities = conflicts[i].possibilities;
10035 +         for (j = 0; j < possibility_count; j++) {
10036 +              possibility = possibilities[j]->pkg;
10037 +              for (k = 0; k < conflictee_provides_count; k++) {
10038 +                   if (possibility == conflictee_provides[k]) {
10039 +                        return 1;
10040 +                   }
10041 +              }
10042 +         }
10043 +     }
10044 +     return 0;
10045 +}
10046 +
10047 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
10048 +{
10049 +    int oldlen = 0, newlen = 0;
10050 +    char ** result;
10051 +    register int i, j;
10052 +
10053 +    if(!newstuff)
10054 +       return oldstuff;
10055 +    
10056 +    while(oldstuff && oldstuff[oldlen]) oldlen++;
10057 +    while(newstuff && newstuff[newlen]) newlen++;
10058 +    
10059 +    result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
10060 +    if (result == NULL) {
10061 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10062 +        return NULL;
10063 +    }
10064 +    
10065 +    for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
10066 +       *(result + i) = *(newstuff + j);
10067 +    
10068 +    *(result + i) = NULL;
10069 +
10070 +    return result;
10071 +}
10072 +    
10073 +/* 
10074 + * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
10075 + * this is null terminated, no count is carried around 
10076 + */
10077 +char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
10078 +{
10079 +    int count;
10080 +    char ** resized;
10081 +    char *depend_str = pkg_depend_str(pkg, ref_ndx);
10082 +
10083 +    count = 0;
10084 +    while(the_lost && the_lost[count]) count++;
10085 +
10086 +    count++;  /* need one to hold the null */
10087 +    resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
10088 +    if (resized == NULL) {
10089 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10090 +       return NULL;
10091 +    }
10092 +    resized[count - 1] = strdup(depend_str);
10093 +    resized[count] = NULL;
10094 +    
10095 +    return resized;
10096 +}
10097 +       
10098 +void printDepends(pkg_t * pkg)
10099 +{
10100 +    register int i, j;
10101 +    compound_depend_t * depend;
10102 +    int count;
10103 +    
10104 +    count = pkg->pre_depends_count + pkg->depends_count;
10105 +    
10106 +    depend = pkg->depends;
10107 +    if(!depend){
10108 +       fprintf(stderr, "Depends pointer is NULL\n");
10109 +       return;
10110 +    }
10111 +    for(i = 0; i < count; i++){
10112 +       fprintf(stderr, "%s has %d possibilities:\n", 
10113 +               (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
10114 +               depend->possibility_count);
10115 +       for(j = 0; j < depend->possibility_count; j++)
10116 +           fprintf(stderr, "\t%s version %s (%d)\n",
10117 +                   depend->possibilities[j]->pkg->name,
10118 +                   depend->possibilities[j]->version,
10119 +                   depend->possibilities[j]->constraint);
10120 +       depend++;
10121 +    }
10122 +}
10123 +
10124 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
10125 +{
10126 +    register int i, j;
10127 +
10128 +    /* every pkg provides itself */
10129 +    abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
10130 +
10131 +    if (!pkg->provides_count)
10132 +      return 0;
10133 +
10134 +    pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1));
10135 +    if (pkg->provides == NULL) {
10136 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10137 +       return -1 ;
10138 +    }
10139 +    pkg->provides[0] = ab_pkg;
10140 +
10141 +    // if (strcmp(ab_pkg->name, pkg->name))
10142 +    //     fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
10143 +
10144 +    for(i = 0; i < pkg->provides_count; i++){
10145 +        abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
10146 +
10147 +       pkg->provides[i+1] = provided_abpkg;
10148 +
10149 +       j = 0;
10150 +       abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
10151 +    }
10152 +    return 0;
10153 +}
10154 +
10155 +/* Abhaya: added conflicts support */
10156 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
10157 +{
10158 +    register int i;
10159 +    compound_depend_t * conflicts;
10160 +
10161 +    if (!pkg->conflicts_count)
10162 +       return 0;
10163 +
10164 +    conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) *
10165 +                                       pkg->conflicts_count);
10166 +    if (conflicts == NULL) {
10167 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10168 +       return  -1;
10169 +    }
10170 +    for (i = 0; i < pkg->conflicts_count; i++) {
10171 +        conflicts->type = CONFLICTS;
10172 +        parseDepends(conflicts, hash,
10173 +                     pkg->conflicts_str[i]);
10174 +#if 0
10175 +        for (j = 0; j < conflicts->possibility_count; j++) {
10176 +             depend_t *possibility = conflicts->possibilities[j];
10177 +             abstract_pkg_t *conflicting_apkg = possibility->pkg;
10178 +             pkg_add_conflict_pair(ab_pkg, conflicting_apkg);
10179 +        }
10180 +#endif
10181 +        conflicts++;
10182 +    }
10183 +    return 0;
10184 +}
10185 +
10186 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
10187 +{
10188 +     register int i, j;
10189 +
10190 +     if (!pkg->replaces_count)
10191 +         return 0;
10192 +
10193 +     pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count);
10194 +     if (pkg->replaces == NULL) {
10195 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10196 +        return  -1;
10197 +     }
10198 +
10199 +     // if (strcmp(ab_pkg->name, pkg->name))
10200 +     //     fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
10201 +
10202 +     for(i = 0; i < pkg->replaces_count; i++){
10203 +         abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
10204 +
10205 +         pkg->replaces[i] = old_abpkg;
10206 +
10207 +         j = 0;
10208 +         if (!old_abpkg->replaced_by)
10209 +              old_abpkg->replaced_by = abstract_pkg_vec_alloc();
10210 +               if ( old_abpkg->replaced_by == NULL ){
10211 +                  return -1;
10212 +               }
10213 +         /* if a package pkg both replaces and conflicts old_abpkg,
10214 +          * then add it to the replaced_by vector so that old_abpkg
10215 +          * will be upgraded to ab_pkg automatically */
10216 +         if (pkg_conflicts_abstract(pkg, old_abpkg))
10217 +              abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
10218 +     }
10219 +     return 0;
10220 +}
10221 +
10222 +int buildDepends(hash_table_t * hash, pkg_t * pkg)
10223 +{
10224 +     int count;
10225 +     register int i;
10226 +     compound_depend_t * depends;
10227 +
10228 +     if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
10229 +         return 0;
10230 +
10231 +     if (0 && pkg->pre_depends_count)
10232 +         fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n", 
10233 +                 pkg->name, pkg->pre_depends_count, pkg->depends_count);
10234 +     depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
10235 +     if (depends == NULL) {
10236 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10237 +        return  -1;
10238 +     }
10239 +     
10240 +
10241 +     for(i = 0; i < pkg->pre_depends_count; i++){
10242 +         parseDepends(depends, hash, pkg->pre_depends_str[i]);
10243 +         if (0 && pkg->pre_depends_count)
10244 +              fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n", 
10245 +                      pkg->pre_depends_str[i], depends, depends->possibility_count);
10246 +         depends->type = PREDEPEND;
10247 +         depends++;
10248 +     }
10249 +
10250 +     for(i = 0; i < pkg->recommends_count; i++){
10251 +         parseDepends(depends, hash, pkg->recommends_str[i]);
10252 +         if (0 && pkg->recommends_count)
10253 +              fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n", 
10254 +                      pkg->recommends_str[i], depends, depends->possibility_count);
10255 +         depends->type = RECOMMEND;
10256 +         depends++;
10257 +     }
10258 +
10259 +     for(i = 0; i < pkg->suggests_count; i++){
10260 +         parseDepends(depends, hash, pkg->suggests_str[i]);
10261 +         if (0 && pkg->suggests_count)
10262 +              fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n", 
10263 +                      pkg->suggests_str[i], depends, depends->possibility_count);
10264 +         depends->type = SUGGEST;
10265 +         depends++;
10266 +     }
10267 +
10268 +     for(i = 0; i < pkg->depends_count; i++){
10269 +         parseDepends(depends, hash, pkg->depends_str[i]);
10270 +         if (0 && pkg->depends_count)
10271 +              fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
10272 +                      pkg->depends_str[i], depends, depends->possibility_count);
10273 +         depends++;
10274 +     }
10275 +     return 0;
10276 +}    
10277 +
10278 +/*
10279 + * pkg_depend_string: returns the depends string specified by index.
10280 + *   All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
10281 + *   [0,npredepends) -> returns pre_depends_str[index]
10282 + *   [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
10283 + *   [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
10284 + *   [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
10285 + */
10286 +char *pkg_depend_str(pkg_t *pkg, int index)
10287 +{
10288 +     if (index < pkg->pre_depends_count) {
10289 +         return pkg->pre_depends_str[index];
10290 +     }
10291 +     index -= pkg->pre_depends_count;
10292 +
10293 +     if (index < pkg->recommends_count) {
10294 +         return pkg->recommends_str[index];
10295 +     }
10296 +     index -= pkg->recommends_count;
10297 +
10298 +     if (index < pkg->suggests_count) {
10299 +         return pkg->suggests_str[index];
10300 +     }
10301 +     index -= pkg->suggests_count;
10302 +
10303 +     if (index < pkg->depends_count) {
10304 +         return pkg->depends_str[index];
10305 +     }
10306 +     fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", index, pkg->name);
10307 +     return NULL;
10308 +}
10309 +
10310 +void freeDepends(pkg_t *pkg)
10311 +{
10312 +    int i;
10313 +
10314 +    if (pkg == NULL || pkg->depends == NULL) {
10315 +       return;
10316 +    }
10317 +
10318 +    fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
10319 +    for (i=0; i < pkg->depends->possibility_count; i++) {
10320 +       depend_deinit(pkg->depends->possibilities[i]);
10321 +    }
10322 +    free(pkg->depends->possibilities);
10323 +    free(pkg->depends);
10324 +    pkg->depends = NULL;
10325 +}
10326 +
10327 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
10328 +{
10329 +     compound_depend_t * depends;
10330 +     int count, othercount;
10331 +     register int i, j;
10332 +     abstract_pkg_t * ab_depend;
10333 +     abstract_pkg_t ** temp;
10334 +
10335 +     count = pkg->pre_depends_count + pkg->depends_count;
10336 +     depends = pkg->depends;
10337 +
10338 +         if (0 && pkg->pre_depends_count)
10339 +              fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
10340 +                      pkg->name, pkg->pre_depends_count, pkg->depends_count);
10341 +     for (i = 0; i < count; i++) {
10342 +         if (0 && pkg->pre_depends_count)
10343 +              fprintf(stderr, "  i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
10344 +         for (j = 0; j < depends->possibility_count; j++){
10345 +              ab_depend = depends->possibilities[j]->pkg;
10346 +              if(!ab_depend->depended_upon_by)
10347 +                   ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
10348 +
10349 +              temp = ab_depend->depended_upon_by;
10350 +              othercount = 1;
10351 +              while(*temp){
10352 +                   temp++;
10353 +                   othercount++;
10354 +              }
10355 +              *temp = ab_pkg;
10356 +
10357 +              ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by, 
10358 +                                                                       (othercount + 1) * sizeof(abstract_pkg_t *));
10359 +              /* the array may have moved */
10360 +              temp = ab_depend->depended_upon_by + othercount;
10361 +              *temp = NULL;
10362 +         }
10363 +         depends++;
10364 +     }
10365 +}
10366 +
10367 +static depend_t * depend_init(void)
10368 +{
10369 +    depend_t * d = (depend_t *)malloc(sizeof(depend_t));    
10370 +    if ( d==NULL ){
10371 +        fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10372 +        return NULL; 
10373 +     }
10374 +    d->constraint = NONE;
10375 +    d->version = NULL;
10376 +    d->pkg = NULL;
10377 +    
10378 +    return d;
10379 +}
10380 +
10381 +static void depend_deinit(depend_t *d)
10382 +{
10383 +    free(d);
10384 +}
10385 +
10386 +static int parseDepends(compound_depend_t *compound_depend, 
10387 +                       hash_table_t * hash, char * depend_str)
10388 +{
10389 +     char * pkg_name, buffer[2048];
10390 +     int num_of_ors = 0;
10391 +     register int i;
10392 +     register char * src, * dest;
10393 +     depend_t ** possibilities;
10394 +
10395 +     /* first count the number of ored possibilities for satisfying dependency */
10396 +     src = depend_str;
10397 +     while(*src)
10398 +         if(*src++ == '|')
10399 +              num_of_ors++;
10400 +
10401 +     compound_depend->type = DEPEND;
10402 +
10403 +     compound_depend->possibility_count = num_of_ors + 1;
10404 +     possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
10405 +     if (!possibilities)
10406 +         return -ENOMEM;
10407 +     compound_depend->possibilities = possibilities;
10408 +
10409 +     src = depend_str;
10410 +     for(i = 0; i < num_of_ors + 1; i++){
10411 +         possibilities[i] = depend_init();
10412 +          if (!possibilities[i])
10413 +              return -ENOMEM;
10414 +         /* gobble up just the name first */
10415 +         dest = buffer;
10416 +         while(*src &&
10417 +               !isspace(*src) &&
10418 +               (*src != '(') &&
10419 +               (*src != '*') &&
10420 +               (*src != '|'))
10421 +              *dest++ = *src++;
10422 +         *dest = '\0';
10423 +         pkg_name = trim_alloc(buffer);
10424 +          if (pkg_name == NULL )
10425 +              return -ENOMEM;
10426 +       
10427 +         /* now look at possible version info */
10428 +       
10429 +         /* skip to next chars */
10430 +         if(isspace(*src))
10431 +              while(*src && isspace(*src)) src++;
10432 +
10433 +         /* extract constraint and version */
10434 +         if(*src == '('){
10435 +              src++;
10436 +              if(!strncmp(src, "<<", 2)){
10437 +                   possibilities[i]->constraint = EARLIER;
10438 +                   src += 2;
10439 +              }
10440 +              else if(!strncmp(src, "<=", 2)){
10441 +                   possibilities[i]->constraint = EARLIER_EQUAL;
10442 +                   src += 2;
10443 +              }
10444 +              else if(!strncmp(src, ">=", 2)){
10445 +                   possibilities[i]->constraint = LATER_EQUAL;
10446 +                   src += 2;
10447 +              }
10448 +              else if(!strncmp(src, ">>", 2)){
10449 +                   possibilities[i]->constraint = LATER;
10450 +                   src += 2;
10451 +              }
10452 +              else if(!strncmp(src, "=", 1)){
10453 +                   possibilities[i]->constraint = EQUAL;
10454 +                   src++;
10455 +              }
10456 +              /* should these be here to support deprecated designations; dpkg does */
10457 +              else if(!strncmp(src, "<", 1)){
10458 +                   possibilities[i]->constraint = EARLIER_EQUAL;
10459 +                   src++;
10460 +              }
10461 +              else if(!strncmp(src, ">", 1)){
10462 +                   possibilities[i]->constraint = LATER_EQUAL;
10463 +                   src++; 
10464 +              }
10465 +
10466 +              /* now we have any constraint, pass space to version string */
10467 +              while(isspace(*src)) src++;
10468 +
10469 +              /* this would be the version string */
10470 +              dest = buffer;
10471 +              while(*src && *src != ')')
10472 +                   *dest++ = *src++;
10473 +              *dest = '\0';
10474 +           
10475 +              possibilities[i]->version = trim_alloc(buffer);
10476 +              /*                   fprintf(stderr, "let's print the depends version string:");
10477 +                                   fprintf(stderr, "version %s\n", possibilities[i]->version);*/
10478 +               if (possibilities[i]->version == NULL )
10479 +                   return -ENOMEM;
10480 +
10481 +        
10482 +         }
10483 +         /* hook up the dependency to its abstract pkg */
10484 +         possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
10485 +
10486 +         free(pkg_name);
10487 +       
10488 +         /* now get past the ) and any possible | chars */
10489 +         while(*src &&
10490 +               (isspace(*src) ||
10491 +                (*src == ')') ||
10492 +                (*src == '|')))
10493 +              src++;
10494 +         if (*src == '*')
10495 +         {
10496 +              compound_depend->type = GREEDY_DEPEND;
10497 +              src++;
10498 +         }
10499 +     }
10500 +
10501 +     return 0;
10502 +}
10503 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_depends.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.h
10504 --- busybox-1.2.0-orig/archival/libipkg/pkg_depends.h   1970-01-01 01:00:00.000000000 +0100
10505 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_depends.h  2006-07-22 16:31:25.000000000 +0200
10506 @@ -0,0 +1,105 @@
10507 +/* pkg_depends.h - the itsy package management system
10508 +
10509 +   Steven M. Ayer
10510 +   
10511 +   Copyright (C) 2002 Compaq Computer Corporation
10512 +
10513 +   This program is free software; you can redistribute it and/or
10514 +   modify it under the terms of the GNU General Public License as
10515 +   published by the Free Software Foundation; either version 2, or (at
10516 +   your option) any later version.
10517 +
10518 +   This program is distributed in the hope that it will be useful, but
10519 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10520 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10521 +   General Public License for more details.
10522 +*/
10523 +
10524 +#ifndef PKG_DEPENDS_H
10525 +#define PKG_DEPENDS_H
10526 +
10527 +#include "pkg.h"
10528 +#include "pkg_hash.h"
10529 +
10530 +enum depend_type {
10531 +    PREDEPEND,
10532 +    DEPEND,
10533 +    CONFLICTS,
10534 +    GREEDY_DEPEND,
10535 +    RECOMMEND,
10536 +    SUGGEST
10537 +};
10538 +typedef enum depend_type depend_type_t;
10539 +
10540 +enum version_constraint {
10541 +    NONE,
10542 +    EARLIER,
10543 +    EARLIER_EQUAL,
10544 +    EQUAL,
10545 +    LATER_EQUAL,
10546 +    LATER
10547 +};
10548 +typedef enum version_constraint version_constraint_t;
10549 +
10550 +struct depend{
10551 +    version_constraint_t constraint;
10552 +    char * version;
10553 +    abstract_pkg_t * pkg;
10554 +};
10555 +typedef struct depend depend_t;
10556 +    
10557 +struct compound_depend{
10558 +    depend_type_t type;
10559 +    int possibility_count;
10560 +    struct depend ** possibilities;
10561 +};
10562 +typedef struct compound_depend compound_depend_t;
10563 +
10564 +#include "hash_table.h"
10565 +
10566 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10567 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10568 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10569 +int buildDepends(hash_table_t * hash, pkg_t * pkg);
10570 +
10571 +/**
10572 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
10573 + * the same abstract package and 0 otherwise.
10574 + */
10575 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee);
10576 +
10577 +/**
10578 + * pkg_provides returns 1 if pkg->provides contains providee and 0
10579 + * otherwise.
10580 + */
10581 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee);
10582 +
10583 +/**
10584 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
10585 + * otherwise.
10586 + */
10587 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee);
10588 +
10589 +/**
10590 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee provides and 0
10591 + * otherwise.
10592 + */
10593 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflicts);
10594 +
10595 +/**
10596 + * pkg_conflicts returns 1 if pkg->conflicts contains one of conflictee's provides and 0
10597 + * otherwise.
10598 + */
10599 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflicts);
10600 +
10601 +char *pkg_depend_str(pkg_t *pkg, int index);
10602 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg);
10603 +void freeDepends(pkg_t *pkg);
10604 +void printDepends(pkg_t * pkg);
10605 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg);
10606 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, pkg_vec_t *depends, char *** unresolved);
10607 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg);
10608 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend);
10609 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend);
10610 +
10611 +#endif
10612 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.c
10613 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest.c      1970-01-01 01:00:00.000000000 +0100
10614 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.c     2006-07-22 16:31:25.000000000 +0200
10615 @@ -0,0 +1,92 @@
10616 +/* pkg_dest.c - the itsy package management system
10617 +
10618 +   Carl D. Worth
10619 +
10620 +   Copyright (C) 2001 University of Southern California
10621 +
10622 +   This program is free software; you can redistribute it and/or
10623 +   modify it under the terms of the GNU General Public License as
10624 +   published by the Free Software Foundation; either version 2, or (at
10625 +   your option) any later version.
10626 +
10627 +   This program is distributed in the hope that it will be useful, but
10628 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10629 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10630 +   General Public License for more details.
10631 +*/
10632 +
10633 +#include "ipkg.h"
10634 +
10635 +#include "pkg_dest.h"
10636 +#include "file_util.h"
10637 +#include "str_util.h"
10638 +#include "sprintf_alloc.h"
10639 +
10640 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir)
10641 +{
10642 +    dest->name = strdup(name);
10643 +
10644 +    /* Guarantee that dest->root_dir ends with a '/' */
10645 +    if (str_ends_with(root_dir, "/")) {
10646 +       dest->root_dir = strdup(root_dir);
10647 +    } else {
10648 +       sprintf_alloc(&dest->root_dir, "%s/", root_dir);
10649 +    }
10650 +    file_mkdir_hier(dest->root_dir, 0755);
10651 +
10652 +    sprintf_alloc(&dest->ipkg_dir, "%s%s",
10653 +                 dest->root_dir, IPKG_STATE_DIR_PREFIX);
10654 +    file_mkdir_hier(dest->ipkg_dir, 0755);
10655 +
10656 +    if (str_starts_with (lists_dir, "/")) 
10657 +        sprintf_alloc(&dest->lists_dir, "%s", lists_dir);
10658 +    else
10659 +        sprintf_alloc(&dest->lists_dir, "/%s", lists_dir);
10660 +
10661 +    file_mkdir_hier(dest->lists_dir, 0755);
10662 +
10663 +    sprintf_alloc(&dest->info_dir, "%s/%s",
10664 +                 dest->ipkg_dir, IPKG_INFO_DIR_SUFFIX);
10665 +    file_mkdir_hier(dest->info_dir, 0755);
10666 +
10667 +    sprintf_alloc(&dest->status_file_name, "%s/%s",
10668 +                 dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10669 +
10670 +    sprintf_alloc(&dest->status_file_tmp_name, "%s/%s.tmp",
10671 +                 dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10672 +
10673 +    dest->status_file = NULL;
10674 +
10675 +    return 0;
10676 +}
10677 +
10678 +void pkg_dest_deinit(pkg_dest_t *dest)
10679 +{
10680 +    free(dest->name);
10681 +    dest->name = NULL;
10682 +
10683 +    free(dest->root_dir);
10684 +    dest->root_dir = NULL;
10685 +
10686 +    free(dest->ipkg_dir);
10687 +    dest->ipkg_dir = NULL;
10688 +
10689 +    free(dest->lists_dir);
10690 +    dest->lists_dir = NULL;
10691 +
10692 +    free(dest->info_dir);
10693 +    dest->info_dir = NULL;
10694 +
10695 +    free(dest->status_file_name);
10696 +    dest->status_file_name = NULL;
10697 +
10698 +    free(dest->status_file_tmp_name);
10699 +    dest->status_file_tmp_name = NULL;
10700 +
10701 +    if (dest->status_file) {
10702 +       fclose(dest->status_file);
10703 +    }
10704 +    dest->status_file = NULL;
10705 +
10706 +    dest->root_dir = NULL;
10707 +}
10708 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.h
10709 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest.h      1970-01-01 01:00:00.000000000 +0100
10710 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest.h     2006-07-22 16:31:25.000000000 +0200
10711 @@ -0,0 +1,38 @@
10712 +/* pkg_dest.h - the itsy package management system
10713 +
10714 +   Carl D. Worth
10715 +
10716 +   Copyright (C) 2001 University of Southern California
10717 +
10718 +   This program is free software; you can redistribute it and/or
10719 +   modify it under the terms of the GNU General Public License as
10720 +   published by the Free Software Foundation; either version 2, or (at
10721 +   your option) any later version.
10722 +
10723 +   This program is distributed in the hope that it will be useful, but
10724 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10725 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10726 +   General Public License for more details.
10727 +*/
10728 +
10729 +#ifndef PKG_DEST_H
10730 +#define PKG_DEST_H
10731 +
10732 +typedef struct pkg_dest pkg_dest_t;
10733 +struct pkg_dest
10734 +{
10735 +    char *name;
10736 +    char *root_dir;
10737 +    char *ipkg_dir;
10738 +    char *lists_dir;
10739 +    char *info_dir;
10740 +    char *status_file_name;
10741 +    char *status_file_tmp_name;
10742 +    FILE *status_file;
10743 +};
10744 +
10745 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char *lists_dir);
10746 +void pkg_dest_deinit(pkg_dest_t *dest);
10747 +
10748 +#endif
10749 +
10750 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.c
10751 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.c 1970-01-01 01:00:00.000000000 +0100
10752 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.c        2006-07-22 16:31:25.000000000 +0200
10753 @@ -0,0 +1,85 @@
10754 +/* pkg_dest_list.c - the itsy package management system
10755 +
10756 +   Carl D. Worth
10757 +
10758 +   Copyright (C) 2001 University of Southern California
10759 +
10760 +   This program is free software; you can redistribute it and/or
10761 +   modify it under the terms of the GNU General Public License as
10762 +   published by the Free Software Foundation; either version 2, or (at
10763 +   your option) any later version.
10764 +
10765 +   This program is distributed in the hope that it will be useful, but
10766 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10767 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10768 +   General Public License for more details.
10769 +*/
10770 +
10771 +#include "ipkg.h"
10772 +
10773 +#include "pkg_dest.h"
10774 +#include "void_list.h"
10775 +#include "pkg_dest_list.h"
10776 +
10777 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data)
10778 +{
10779 +    return void_list_elt_init((void_list_elt_t *) elt, data);
10780 +}
10781 +
10782 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt)
10783 +{
10784 +    void_list_elt_deinit((void_list_elt_t *) elt);
10785 +}
10786 +
10787 +int pkg_dest_list_init(pkg_dest_list_t *list)
10788 +{
10789 +    return void_list_init((void_list_t *) list);
10790 +}
10791 +
10792 +void pkg_dest_list_deinit(pkg_dest_list_t *list)
10793 +{
10794 +    pkg_dest_list_elt_t *iter;
10795 +    pkg_dest_t *pkg_dest;
10796 +
10797 +    for (iter = list->head; iter; iter = iter->next) {
10798 +       pkg_dest = iter->data;
10799 +       pkg_dest_deinit(pkg_dest);
10800 +
10801 +       /* malloced in pkg_dest_list_append */
10802 +       free(pkg_dest);
10803 +       iter->data = NULL;
10804 +    }
10805 +    void_list_deinit((void_list_t *) list);
10806 +}
10807 +
10808 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10809 +                                const char *root_dir,const char *lists_dir)
10810 +{
10811 +    int err;
10812 +    pkg_dest_t *pkg_dest;
10813 +
10814 +    /* freed in plg_dest_list_deinit */
10815 +    pkg_dest = malloc(sizeof(pkg_dest_t));
10816 +    if (pkg_dest == NULL) {
10817 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10818 +       return NULL;
10819 +    }
10820 +
10821 +    pkg_dest_init(pkg_dest, name, root_dir,lists_dir);
10822 +    err = void_list_append((void_list_t *) list, pkg_dest);
10823 +    if (err) {
10824 +       return NULL;
10825 +    }
10826 +
10827 +    return pkg_dest;
10828 +}
10829 +
10830 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data)
10831 +{
10832 +    return void_list_push((void_list_t *) list, data);
10833 +}
10834 +
10835 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list)
10836 +{
10837 +    return (pkg_dest_list_elt_t *) void_list_pop((void_list_t *) list);
10838 +}
10839 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.h
10840 --- busybox-1.2.0-orig/archival/libipkg/pkg_dest_list.h 1970-01-01 01:00:00.000000000 +0100
10841 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_dest_list.h        2006-07-22 16:31:25.000000000 +0200
10842 @@ -0,0 +1,50 @@
10843 +/* pkg_dest_list.h - the itsy package management system
10844 +
10845 +   Carl D. Worth
10846 +
10847 +   Copyright (C) 2001 University of Southern California
10848 +
10849 +   This program is free software; you can redistribute it and/or
10850 +   modify it under the terms of the GNU General Public License as
10851 +   published by the Free Software Foundation; either version 2, or (at
10852 +   your option) any later version.
10853 +
10854 +   This program is distributed in the hope that it will be useful, but
10855 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10856 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10857 +   General Public License for more details.
10858 +*/
10859 +
10860 +#ifndef PKG_DEST_LIST_H
10861 +#define PKG_DEST_LIST_H
10862 +
10863 +#include "pkg_dest.h"
10864 +
10865 +typedef struct pkg_dest_list_elt pkg_dest_list_elt_t;
10866 +struct pkg_dest_list_elt
10867 +{
10868 +    pkg_dest_list_elt_t *next;
10869 +    pkg_dest_t *data;
10870 +};
10871 +
10872 +typedef struct pkg_dest_list pkg_dest_list_t;
10873 +struct pkg_dest_list
10874 +{
10875 +    pkg_dest_list_elt_t pre_head;
10876 +    pkg_dest_list_elt_t *head;
10877 +    pkg_dest_list_elt_t *tail;
10878 +};
10879 +
10880 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data);
10881 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt);
10882 +
10883 +int pkg_dest_list_init(pkg_dest_list_t *list);
10884 +void pkg_dest_list_deinit(pkg_dest_list_t *list);
10885 +
10886 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10887 +                                const char *root_dir,const char* lists_dir);
10888 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data);
10889 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list);
10890 +
10891 +#endif
10892 +
10893 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_extract.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.c
10894 --- busybox-1.2.0-orig/archival/libipkg/pkg_extract.c   1970-01-01 01:00:00.000000000 +0100
10895 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.c  2006-07-22 16:31:25.000000000 +0200
10896 @@ -0,0 +1,224 @@
10897 +/* pkg_extract.c - the itsy package management system
10898 +
10899 +   Carl D. Worth
10900 +
10901 +   Copyright (C) 2001 University of Southern California
10902 +
10903 +   This program is free software; you can redistribute it and/or
10904 +   modify it under the terms of the GNU General Public License as
10905 +   published by the Free Software Foundation; either version 2, or (at
10906 +   your option) any later version.
10907 +
10908 +   This program is distributed in the hope that it will be useful, but
10909 +   WITHOUT ANY WARRANTY; without even the implied warranty of
10910 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10911 +   General Public License for more details.
10912 +*/
10913 +
10914 +#include "ipkg.h"
10915 +#include <errno.h>
10916 +#include <fcntl.h>
10917 +#include <stdio.h>
10918 +
10919 +#include "pkg_extract.h"
10920 +
10921 +#include "libbb.h"
10922 +#include "file_util.h"
10923 +#include "sprintf_alloc.h"
10924 +#include "unarchive.h"
10925 +
10926 +#define IPKG_CONTROL_ARCHIVE  "control.tar.gz"
10927 +#define IPKG_DATA_ARCHIVE  "data.tar.gz"
10928 +#define IPKG_CONTROL_FILE  "control"
10929 +
10930 +static void extract_ipkg_file_to_dir(pkg_t *pkg, const char *dir, const char *filename)
10931 +{
10932 +       archive_handle_t *archive;
10933 +       char *path;
10934 +
10935 +       sprintf_alloc(&path, "%s/", dir);
10936 +       archive = init_handle();
10937 +       archive->src_fd = bb_xopen(pkg->local_filename, O_RDONLY);
10938 +       archive->filter = filter_accept_list;
10939 +       llist_add_to(&(archive->accept), (char *)filename);
10940 +       archive->buffer = path;
10941 +       archive->action_data = data_extract_all_prefix;
10942 +       archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
10943 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10944 +       close(archive->src_fd);
10945 +       free(archive->accept);
10946 +       free(archive);
10947 +       free(path);
10948 +}
10949 +
10950 +static void data_extract_file_name_to_buffer(archive_handle_t *archive)
10951 +{
10952 +       unsigned int size = strlen(archive->file_header->name) + 2;
10953 +
10954 +       if (archive->buffer == NULL) {
10955 +               archive->buffer = xmalloc(size);
10956 +               strcpy(archive->buffer, archive->file_header->name);
10957 +       } else {
10958 +               size += strlen(archive->buffer);
10959 +               archive->buffer = xrealloc(archive->buffer, size);
10960 +               strcat(archive->buffer, archive->file_header->name);
10961 +       } 
10962 +       strcat(archive->buffer, "\n");
10963 +       data_skip(archive);
10964 +}
10965 +
10966 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
10967 +{
10968 +       archive_handle_t *archive;
10969 +       char *name;
10970 +
10971 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE); 
10972 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10973 +       archive = init_handle();
10974 +       archive->src_fd = bb_xopen(name, O_RDONLY);
10975 +       archive->filter = filter_accept_list;
10976 +       llist_add_to(&(archive->accept), "./" IPKG_CONTROL_FILE);
10977 +       archive->action_data = data_extract_to_buffer;
10978 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10979 +       close(archive->src_fd);
10980 +       fputs(archive->buffer, stream);
10981 +       free(archive->buffer);
10982 +       free(archive->accept);
10983 +       free(archive);
10984 +       free(name);
10985 +
10986 +       return 0;
10987 +}
10988 +
10989 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
10990 +{
10991 +    return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
10992 +}
10993 +
10994 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir, const char *prefix)
10995 +{
10996 +       archive_handle_t *archive;
10997 +       char *name;
10998 +       char *path;
10999 +
11000 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE); 
11001 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
11002 +       sprintf_alloc(&path, "%s/%s", dir, prefix);
11003 +       archive = init_handle();
11004 +       archive->src_fd = bb_xopen(name, O_RDONLY);
11005 +       archive->filter = filter_accept_all;
11006 +       archive->buffer = path;
11007 +       archive->action_data = data_extract_all_prefix;
11008 +       archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
11009 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
11010 +       close(archive->src_fd);
11011 +       free(archive);
11012 +       free(path);
11013 +       free(name);
11014 +
11015 +       return 0;
11016 +}
11017 +
11018 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
11019 +{
11020 +       archive_handle_t *archive;
11021 +       char *name;
11022 +       char *path;
11023 +
11024 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
11025 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
11026 +       sprintf_alloc(&path, "%s/", dir);
11027 +       archive = init_handle();
11028 +       archive->src_fd = bb_xopen(name, O_RDONLY);
11029 +       archive->filter = filter_accept_all;
11030 +       archive->buffer = path;
11031 +       archive->action_data = data_extract_all_prefix;
11032 +       archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
11033 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
11034 +       close(archive->src_fd);
11035 +       free(archive);
11036 +       free(path);
11037 +       free(name);
11038 +
11039 +       return 0;
11040 +}
11041 +
11042 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
11043 +{
11044 +     int err=0;
11045 +     char *line, *data_file;
11046 +     FILE *file;
11047 +     FILE *tmp;
11048 +
11049 +     file = fopen(file_name, "w");
11050 +     if (file == NULL) {
11051 +         fprintf(stderr, "%s: ERROR: Failed to open %s for writing.\n",
11052 +                 __FUNCTION__, file_name);
11053 +         return EINVAL;
11054 +     }
11055 +
11056 +     tmp = tmpfile();
11057 +     if (pkg->installed_files) {
11058 +         str_list_elt_t *elt;
11059 +         for (elt = pkg->installed_files->head; elt; elt = elt->next) {
11060 +              fprintf(file, "%s\n", elt->data);
11061 +         }
11062 +     } else {
11063 +         err = pkg_extract_data_file_names_to_stream(pkg, tmp);
11064 +         if (err) {
11065 +              fclose(file);
11066 +              fclose(tmp);
11067 +              return err;
11068 +         }
11069 +
11070 +         /* Fixup data file names by removing the initial '.' */
11071 +         rewind(tmp);
11072 +         while (1) {
11073 +              line = file_read_line_alloc(tmp);
11074 +              if (line == NULL) {
11075 +                   break;
11076 +              }
11077 +
11078 +              data_file = line;
11079 +              if (*data_file == '.') {
11080 +                   data_file++;
11081 +              }
11082 +
11083 +              if (*data_file != '/') {
11084 +                   fputs("/", file);
11085 +              }
11086 +
11087 +              /* I have no idea why, but this is what dpkg does */
11088 +              if (strcmp(data_file, "/\n") == 0) {
11089 +                   fputs("/.\n", file);
11090 +              } else {
11091 +                   fputs(data_file, file);
11092 +              }
11093 +         }
11094 +     }
11095 +     fclose(tmp);
11096 +     fclose(file);
11097 +
11098 +     return err;
11099 +}
11100 +
11101 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
11102 +{
11103 +       archive_handle_t *archive;
11104 +       char *name;
11105 +       
11106 +       extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
11107 +       sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
11108 +       archive = init_handle();
11109 +       archive->src_fd = bb_xopen(name, O_RDONLY);
11110 +       archive->filter = filter_accept_all;
11111 +       archive->action_data = data_extract_file_name_to_buffer;
11112 +       while( get_header_tar_gz(archive) == EXIT_SUCCESS );
11113 +       close(archive->src_fd);
11114 +       fputs(archive->buffer, file);
11115 +       free(archive->buffer);
11116 +       free(archive);
11117 +       free(name);
11118 +       
11119 +       return 0;
11120 +}
11121 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_extract.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.h
11122 --- busybox-1.2.0-orig/archival/libipkg/pkg_extract.h   1970-01-01 01:00:00.000000000 +0100
11123 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_extract.h  2006-07-22 16:31:25.000000000 +0200
11124 @@ -0,0 +1,32 @@
11125 +/* pkg_extract.c - the itsy package management system
11126 +
11127 +   Carl D. Worth
11128 +
11129 +   Copyright (C) 2001 University of Southern California
11130 +
11131 +   This program is free software; you can redistribute it and/or
11132 +   modify it under the terms of the GNU General Public License as
11133 +   published by the Free Software Foundation; either version 2, or (at
11134 +   your option) any later version.
11135 +
11136 +   This program is distributed in the hope that it will be useful, but
11137 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11138 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11139 +   General Public License for more details.
11140 +*/
11141 +
11142 +#ifndef PKG_EXTRACT_H
11143 +#define PKG_EXTRACT_H
11144 +
11145 +#include "pkg.h"
11146 +
11147 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream);
11148 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir);
11149 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
11150 +                                                const char *dir,
11151 +                                                const char *prefix);
11152 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir);
11153 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name);
11154 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file);
11155 +
11156 +#endif
11157 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.h
11158 --- busybox-1.2.0-orig/archival/libipkg/pkg.h   1970-01-01 01:00:00.000000000 +0100
11159 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg.h  2006-07-22 16:31:25.000000000 +0200
11160 @@ -0,0 +1,232 @@
11161 +/* pkg.h - the itsy package management system
11162 +
11163 +   Carl D. Worth
11164 +
11165 +   Copyright (C) 2001 University of Southern California
11166 +
11167 +   This program is free software; you can redistribute it and/or
11168 +   modify it under the terms of the GNU General Public License as
11169 +   published by the Free Software Foundation; either version 2, or (at
11170 +   your option) any later version.
11171 +
11172 +   This program is distributed in the hope that it will be useful, but
11173 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11174 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11175 +   General Public License for more details.
11176 +*/
11177 +
11178 +#ifndef PKG_H
11179 +#define PKG_H
11180 +
11181 +#include <sys/types.h>
11182 +#include <sys/stat.h>
11183 +#include <unistd.h>
11184 +
11185 +#include "pkg_vec.h"
11186 +#include "str_list.h"
11187 +#include "pkg_src.h"
11188 +#include "pkg_dest.h"
11189 +#include "ipkg_conf.h"
11190 +#include "conffile_list.h"
11191 +
11192 +struct ipkg_conf;
11193 +
11194 +
11195 +#define ARRAY_SIZE(array) sizeof(array) / sizeof((array)[0])
11196 +
11197 +/* I think "Size" is currently the shortest field name */
11198 +#define PKG_MINIMUM_FIELD_NAME_LEN 4
11199 +
11200 +enum pkg_state_want
11201 +{
11202 +    SW_UNKNOWN = 1,
11203 +    SW_INSTALL,
11204 +    SW_DEINSTALL,
11205 +    SW_PURGE,
11206 +    SW_LAST_STATE_WANT
11207 +};
11208 +typedef enum pkg_state_want pkg_state_want_t;
11209 +
11210 +enum pkg_state_flag
11211 +{
11212 +    SF_OK        = 0,
11213 +    SF_REINSTREQ = 1,
11214 +    SF_HOLD      = 2,  /* do not upgrade version */
11215 +    SF_REPLACE   = 4,  /* replace this package */
11216 +    SF_NOPRUNE   = 8,  /* do not remove obsolete files */
11217 +    SF_PREFER    = 16, /* prefer this version */
11218 +    SF_OBSOLETE  = 32, /* old package in upgrade pair */
11219 +    SF_MARKED    = 64, /* temporary mark */
11220 +    SF_FILELIST_CHANGED = 128, /* needs filelist written */
11221 +    SF_USER      = 256,
11222 +    SF_LAST_STATE_FLAG
11223 +};
11224 +typedef enum pkg_state_flag pkg_state_flag_t;
11225 +#define SF_NONVOLATILE_FLAGS (SF_HOLD|SF_NOPRUNE|SF_PREFER|SF_OBSOLETE|SF_USER)
11226 +
11227 +enum pkg_state_status
11228 +{
11229 +    SS_NOT_INSTALLED = 1,
11230 +    SS_UNPACKED,
11231 +    SS_HALF_CONFIGURED,
11232 +    SS_INSTALLED,
11233 +    SS_HALF_INSTALLED,
11234 +    SS_CONFIG_FILES,
11235 +    SS_POST_INST_FAILED,
11236 +    SS_REMOVAL_FAILED,
11237 +    SS_LAST_STATE_STATUS
11238 +};
11239 +typedef enum pkg_state_status pkg_state_status_t;
11240 +
11241 +struct abstract_pkg{
11242 +    char * name;
11243 +    int dependencies_checked;
11244 +    pkg_vec_t * pkgs;
11245 +    pkg_state_status_t state_status;
11246 +    pkg_state_flag_t state_flag;
11247 +    struct abstract_pkg ** depended_upon_by; /* @@@@ this should be abstract_pkg_vec_t -Jamey */
11248 +    abstract_pkg_vec_t * provided_by;
11249 +    abstract_pkg_vec_t * replaced_by;
11250 +};
11251 +
11252 +#include "pkg_depends.h"
11253 +
11254 +/* XXX: CLEANUP: I'd like to clean up pkg_t in several ways:
11255 +
11256 +   The 3 version fields should go into a single version struct. (This
11257 +   is especially important since, currently, pkg->version can easily
11258 +   be mistaken for pkg_verson_str_alloc(pkg) although they are very
11259 +   distinct. This has been the source of multiple bugs.
11260 +
11261 +   The 3 state fields could possibly also go into their own struct.
11262 +
11263 +   All fields which deal with lists of packages, (Depends,
11264 +   Pre-Depends, Provides, Suggests, Recommends, Enhances), should each
11265 +   be handled by a single struct in pkg_t
11266 +
11267 +   All string fields for which there is a small set of possible
11268 +   values, (section, maintainer, architecture, maybe version?), that
11269 +   are reused among different packages -- for all such packages we
11270 +   should move from "char *"s to some atom datatype to share data
11271 +   storage and use less memory. We might even do reference counting,
11272 +   but probably not since most often we only create new pkg_t structs,
11273 +   we don't often free them.  */
11274 +struct pkg
11275 +{
11276 +     char *name;
11277 +     unsigned long epoch;
11278 +     char *version;
11279 +     char *revision;
11280 +     char *familiar_revision;
11281 +     pkg_src_t *src;
11282 +     pkg_dest_t *dest;
11283 +     char *architecture;
11284 +     char *section;
11285 +     char *maintainer;
11286 +     char *description;
11287 +     pkg_state_want_t state_want;
11288 +     pkg_state_flag_t state_flag;
11289 +     pkg_state_status_t state_status;
11290 +     char **depends_str;
11291 +     int depends_count;
11292 +     char **pre_depends_str;
11293 +     int pre_depends_count;
11294 +     char **recommends_str;
11295 +     int recommends_count;
11296 +     char **suggests_str;
11297 +     int suggests_count;
11298 +     compound_depend_t * depends;
11299 +
11300 +     /* Abhaya: new conflicts */
11301 +     char **conflicts_str;
11302 +     compound_depend_t * conflicts;
11303 +     int conflicts_count;
11304 +       
11305 +     char **replaces_str;
11306 +     int replaces_count;
11307 +     abstract_pkg_t ** replaces;
11308 +
11309 +     char **provides_str;
11310 +     int provides_count;
11311 +     abstract_pkg_t ** provides;
11312 +
11313 +     abstract_pkg_t *parent;
11314 +
11315 +     pkg_t *old_pkg; /* during upgrade, points from installee to previously installed */
11316 +
11317 +     char *filename;
11318 +     char *local_filename;
11319 +     char *url;
11320 +     char *tmp_unpack_dir;
11321 +     char *md5sum;
11322 +     char *size;
11323 +     char *installed_size;
11324 +     char *priority;
11325 +     char *source;
11326 +     conffile_list_t conffiles;
11327 +     time_t installed_time;
11328 +     /* As pointer for lazy evaluation */
11329 +     str_list_t *installed_files;
11330 +     /* XXX: CLEANUP: I'd like to perhaps come up with a better
11331 +       mechanism to avoid the problem here, (which is that the
11332 +       installed_files list was being freed from an inner loop while
11333 +       still being used within an outer loop. */
11334 +     int installed_files_ref_cnt;
11335 +     int essential;
11336 +     int arch_priority;
11337 +/* Adding this flag, to "force" ipkg to choose a "provided_by_hand" package, if there are multiple choice */
11338 +     int provided_by_hand;
11339 +};
11340 +
11341 +pkg_t *pkg_new(void);
11342 +int pkg_init(pkg_t *pkg);
11343 +void pkg_deinit(pkg_t *pkg);
11344 +int pkg_init_from_file(pkg_t *pkg, const char *filename);
11345 +abstract_pkg_t *abstract_pkg_new(void);
11346 +int abstract_pkg_init(abstract_pkg_t *ab_pkg);
11347 +
11348 +/* 
11349 + * merges fields from newpkg into oldpkg.
11350 + * Forcibly sets oldpkg state_status, state_want and state_flags if set_status is nonzero 
11351 + */
11352 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status);
11353 +
11354 +char *pkg_version_str_alloc(pkg_t *pkg);
11355 +
11356 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg);
11357 +int pkg_name_version_and_architecture_compare(void *a, void *b);
11358 +int abstract_pkg_name_compare(void *a, void *b);
11359 +
11360 +char * pkg_formatted_info(pkg_t *pkg );
11361 +char * pkg_formatted_field(pkg_t *pkg, const char *field );
11362 +
11363 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg);
11364 +
11365 +void pkg_print_info(pkg_t *pkg, FILE *file);
11366 +void pkg_print_status(pkg_t * pkg, FILE * file);
11367 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field);
11368 +str_list_t *pkg_get_installed_files(pkg_t *pkg);
11369 +int pkg_free_installed_files(pkg_t *pkg);
11370 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg);
11371 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name);
11372 +int pkg_run_script(struct ipkg_conf *conf, pkg_t *pkg,
11373 +                  const char *script, const char *args);
11374 +
11375 +/* enum mappings */
11376 +char *pkg_state_want_to_str(pkg_state_want_t sw);
11377 +pkg_state_want_t pkg_state_want_from_str(char *str);
11378 +char *pkg_state_flag_to_str(pkg_state_flag_t sf);
11379 +pkg_state_flag_t pkg_state_flag_from_str(char *str);
11380 +char *pkg_state_status_to_str(pkg_state_status_t ss);
11381 +pkg_state_status_t pkg_state_status_from_str(char *str);
11382 +
11383 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op);
11384 +
11385 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg);
11386 +int pkg_info_preinstall_check(ipkg_conf_t *conf);
11387 +int pkg_free_installed_files(pkg_t *pkg);
11388 +
11389 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg);
11390 +int pkg_write_changed_filelists(ipkg_conf_t *conf);
11391 +
11392 +#endif
11393 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_hash.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.c
11394 --- busybox-1.2.0-orig/archival/libipkg/pkg_hash.c      1970-01-01 01:00:00.000000000 +0100
11395 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.c     2006-07-22 16:31:25.000000000 +0200
11396 @@ -0,0 +1,617 @@
11397 +/* ipkg_hash.c - the itsy package management system
11398 +
11399 +   Steven M. Ayer
11400 +   
11401 +   Copyright (C) 2002 Compaq Computer Corporation
11402 +
11403 +   This program is free software; you can redistribute it and/or
11404 +   modify it under the terms of the GNU General Public License as
11405 +   published by the Free Software Foundation; either version 2, or (at
11406 +   your option) any later version.
11407 +
11408 +   This program is distributed in the hope that it will be useful, but
11409 +   WITHOUT ANY WARRANTY; without even the implied warranty of
11410 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11411 +   General Public License for more details.
11412 +*/
11413 +
11414 +#include "ipkg.h"
11415 +#include <errno.h>
11416 +#include <ctype.h>
11417 +#include <stdlib.h>
11418 +#include <string.h>
11419 +
11420 +#include "hash_table.h"
11421 +#include "pkg.h"
11422 +#include "ipkg_message.h"
11423 +#include "pkg_vec.h"
11424 +#include "pkg_hash.h"
11425 +#include "pkg_parse.h"
11426 +#include "ipkg_utils.h"
11427 +
11428 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
11429 +
11430 +/*
11431 + * this will talk to both feeds-lists files and installed status files
11432 + * example api:
11433 + *
11434 + * hash_table_t hash;
11435 + * pkg_hash_init(name, &hash, 1000);
11436 + * pkg_hash_add_from_file(<feed filename>);
11437 + *
11438 + * the query function is just there as a shell to prove to me that this
11439 + * sort of works, but isn't far from doing something useful
11440 + * 
11441 + * -sma, 12/21/01
11442 + * modified: CDW 3 Jan. 2002
11443 + */
11444 +
11445 +
11446 +\f
11447 +int pkg_hash_init(const char *name, hash_table_t *hash, int len)
11448 +{
11449 +  return hash_table_init(name, hash, len);
11450 +}
11451 +
11452 +void pkg_hash_deinit(hash_table_t *hash)
11453 +{
11454 +  hash_table_deinit(hash);
11455 +}
11456 +
11457 +
11458 +/* Find the default arch for a given package status file if none is given. */
11459 +static char *pkg_get_default_arch(ipkg_conf_t *conf)
11460 +{
11461 +     nv_pair_list_elt_t *l;
11462 +     char *def_arch = HOST_CPU_STR;            /* Default arch */
11463 +     int def_prio = 0;                         /* Other archs override this */
11464 +
11465 +     l = conf->arch_list.head;
11466 +
11467 +     while (l) {
11468 +         nv_pair_t *nv = l->data;
11469 +         int priority = strtol(nv->value, NULL, 0);
11470 +
11471 +         /* Check if this arch has higher priority, and is valid */
11472 +         if ((priority > def_prio) &&
11473 +             (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
11474 +              /* Our new default */
11475 +              def_prio = priority;
11476 +              def_arch = nv->name;
11477 +         }
11478 +         l = l->next;
11479 +     }
11480 +
11481 +     return strdup(def_arch);
11482 +}
11483 +
11484 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
11485 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
11486 +{
11487 +     hash_table_t *hash = &conf->pkg_hash;
11488 +     char **raw;
11489 +     char **raw_start;
11490 +     pkg_t *pkg;
11491 +    
11492 +     raw = raw_start = read_raw_pkgs_from_file(file_name);
11493 +     if (!raw)
11494 +        return -ENOMEM;
11495 +
11496 +     while(*raw){         /* don't worry, we'll increment raw in the parsing function */
11497 +         pkg = pkg_new();
11498 +         if (!pkg)
11499 +              return -ENOMEM;
11500 +
11501 +         if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
11502 +              if (!pkg->architecture) {
11503 +                   char *version_str = pkg_version_str_alloc(pkg);
11504 +                   pkg->architecture = pkg_get_default_arch(conf);
11505 +                   ipkg_message(conf, IPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
11506 +                                pkg->name, version_str, pkg->architecture);
11507 +                   free(version_str);
11508 +              }
11509 +              hash_insert_pkg(hash, pkg, is_status_file,conf);
11510 +         } else {
11511 +              free(pkg);
11512 +         }
11513 +     }
11514 +
11515 +     /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
11516 +       memory after read_raw_pkgs_from_file */
11517 +     raw = raw_start;
11518 +     while (*raw) {
11519 +         free(*raw++);
11520 +     }
11521 +     free(raw_start);
11522 +     return 0;
11523 +}
11524 +
11525 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
11526 +{
11527 +  return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
11528 +}
11529 +
11530 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name)
11531 +{
11532 +    abstract_pkg_t *apkg = abstract_pkg_fetch_by_name(hash, name);
11533 +    if (apkg)
11534 +       return NULL;
11535 +    return apkg->provided_by;
11536 +}
11537 +
11538 +
11539 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg, 
11540 +                                                 int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet)
11541 +{
11542 +     int i; 
11543 +     int nprovides = 0;
11544 +     int nmatching = 0;
11545 +     pkg_vec_t *matching_pkgs = pkg_vec_alloc();
11546 +     abstract_pkg_vec_t *matching_apkgs = abstract_pkg_vec_alloc();
11547 +     abstract_pkg_vec_t *provided_apkg_vec;
11548 +     abstract_pkg_t **provided_apkgs;
11549 +     abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
11550 +     pkg_t *latest_installed_parent = NULL;
11551 +     pkg_t *latest_matching = NULL;
11552 +     pkg_t *held_pkg = NULL;
11553 +     pkg_t *good_pkg_by_name = NULL;
11554 +
11555 +     if (matching_apkgs == NULL || providers == NULL || 
11556 +         apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
11557 +         return NULL;
11558 +
11559 +     ipkg_message(conf, IPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
11560 +
11561 +     provided_apkg_vec = apkg->provided_by;
11562 +     nprovides = provided_apkg_vec->len;
11563 +     provided_apkgs = provided_apkg_vec->pkgs;
11564 +     if (nprovides > 1)
11565 +         ipkg_message(conf, IPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
11566 +
11567 +     /* accumulate all the providers */
11568 +     for (i = 0; i < nprovides; i++) {
11569 +         abstract_pkg_t *provider_apkg = provided_apkgs[i];
11570 +         ipkg_message(conf, IPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
11571 +         abstract_pkg_vec_insert(providers, provider_apkg);
11572 +     }
11573 +     nprovides = providers->len;
11574 +
11575 +     for (i = 0; i < nprovides; i++) {
11576 +         abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
11577 +         abstract_pkg_t *replacement_apkg = NULL;
11578 +         pkg_vec_t *vec;
11579 +
11580 +         if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
11581 +              replacement_apkg = provider_apkg->replaced_by->pkgs[0];
11582 +              if (provider_apkg->replaced_by->len > 1) {
11583 +                   ipkg_message(conf, IPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n", 
11584 +                                provider_apkg->name, replacement_apkg->name);
11585 +              }
11586 +         }
11587 +
11588 +         if (replacement_apkg)
11589 +              ipkg_message(conf, IPKG_DEBUG, "   replacement_apkg=%s for provider_apkg=%s\n", 
11590 +                           replacement_apkg->name, provider_apkg->name);
11591 +
11592 +         if (replacement_apkg && (replacement_apkg != provider_apkg)) {
11593 +              if (abstract_pkg_vec_contains(providers, replacement_apkg))
11594 +                   continue;
11595 +              else
11596 +                   provider_apkg = replacement_apkg;
11597 +         }
11598 +
11599 +         if (!(vec = provider_apkg->pkgs)) {
11600 +              ipkg_message(conf, IPKG_DEBUG, "   no pkgs for provider_apkg %s\n", provider_apkg->name);
11601 +              continue;
11602 +         }
11603 +    
11604 +
11605 +         /* now check for supported architecture */
11606 +         {
11607 +              int max_count = 0;
11608 +              int i;
11609 +
11610 +              /* count packages matching max arch priority and keep track of last one */
11611 +              for (i = 0; i < vec->len; i++) {
11612 +                   pkg_t *maybe = vec->pkgs[i];
11613 +                   ipkg_message(conf, IPKG_DEBUG, "  %s arch=%s arch_priority=%d version=%s  \n",
11614 +                                maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
11615 +                   if (maybe->arch_priority > 0)  {
11616 +                        max_count++;
11617 +                        abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
11618 +                        pkg_vec_insert(matching_pkgs, maybe);
11619 +                   }
11620 +              }
11621 +         }
11622 +     }
11623 +
11624 +     if (matching_pkgs->len > 1)
11625 +         pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
11626 +     if (matching_apkgs->len > 1)
11627 +         abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
11628 +
11629 +/* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
11630 +   needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
11631 +/* The problem is what to do when there are more than a mathing package, with the same name and several version ?
11632 +   Until now I always got the latest, but that breaks the downgrade option.
11633 +   If I stop at the first one, I would probably miss the new ones 
11634 +   Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
11635 +   or from a Packages feed.
11636 +   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*/
11637 +/*Pigi*/
11638 +
11639 +     for (i = 0; i < matching_pkgs->len; i++) {
11640 +         pkg_t *matching = matching_pkgs->pkgs[i];
11641 +          if (constraint_fcn(matching, cdata)) {  /* We found it */
11642 +             ipkg_message(conf, IPKG_DEBUG, " Found a valid candidate for the install: %s %s  \n", matching->name, matching->version) ;
11643 +             good_pkg_by_name = matching;
11644 +             if ( matching->provided_by_hand == 1 )    /* It has been provided by hand, so it is what user want */
11645 +                break;                                 
11646 +          }
11647 +     }
11648 +
11649 +
11650 +     for (i = 0; i < matching_pkgs->len; i++) {
11651 +         pkg_t *matching = matching_pkgs->pkgs[i];
11652 +         latest_matching = matching;
11653 +         if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
11654 +              latest_installed_parent = matching;
11655 +         if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
11656 +              if (held_pkg)
11657 +                   ipkg_message(conf, IPKG_ERROR, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER.  Using latest.\n",
11658 +                                held_pkg->name, matching->name);
11659 +              held_pkg = matching;
11660 +         }
11661 +     }
11662 +
11663 +     if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
11664 +         ipkg_message(conf, IPKG_ERROR, "Package=%s, %d matching providers\n",
11665 +                      apkg->name, matching_apkgs->len);
11666 +         for (i = 0; i < matching_apkgs->len; i++) {
11667 +              abstract_pkg_t *matching = matching_apkgs->pkgs[i];
11668 +              ipkg_message(conf, IPKG_ERROR, "    %s\n", matching->name);
11669 +         }
11670 +         ipkg_message(conf, IPKG_ERROR, "Please select one with ipkg install or ipkg flag prefer\n");
11671 +     }
11672 +
11673 +     if (matching_apkgs->len > 1 && conf->verbosity > 1) {
11674 +         ipkg_message(conf, IPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
11675 +                      __FUNCTION__, apkg->name, matching_pkgs->len);
11676 +         for (i = 0; i < matching_pkgs->len; i++) {
11677 +              pkg_t *matching = matching_pkgs->pkgs[i];
11678 +              ipkg_message(conf, IPKG_INFO, "    %s %s %s\n",
11679 +                           matching->name, matching->version, matching->architecture);
11680 +         }
11681 +     }
11682 +
11683 +     nmatching = matching_apkgs->len;
11684 +
11685 +     pkg_vec_free(matching_pkgs);
11686 +     abstract_pkg_vec_free(matching_apkgs);
11687 +     abstract_pkg_vec_free(providers);
11688 +
11689 +     if (good_pkg_by_name) {   /* We found a good candidate, we will install it */ 
11690 +         return good_pkg_by_name;
11691 +     }
11692 +     if (held_pkg) {
11693 +         ipkg_message(conf, IPKG_INFO, "  using held package %s\n", held_pkg->name);
11694 +         return held_pkg;
11695 +     }
11696 +     if (latest_installed_parent) {
11697 +         ipkg_message(conf, IPKG_INFO, "  using latest version of installed package %s\n", latest_installed_parent->name);
11698 +         return latest_installed_parent;
11699 +     }
11700 +     if (nmatching > 1) {
11701 +         ipkg_message(conf, IPKG_INFO, "  no matching pkg out of matching_apkgs=%d\n", nmatching);
11702 +         return NULL;
11703 +     }
11704 +     if (latest_matching) {
11705 +         ipkg_message(conf, IPKG_INFO, "  using latest matching %s %s %s\n",
11706 +                      latest_matching->name, latest_matching->version, latest_matching->architecture);
11707 +         return latest_matching;
11708 +     }
11709 +     return NULL;
11710 +}
11711 +
11712 +static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
11713 +{
11714 +     const char *name = (const char *)cdata;
11715 +     if (strcmp(pkg->name, name) == 0)
11716 +         return 1;
11717 +     else
11718 +         return 0;   
11719 +}
11720 +
11721 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name)
11722 +{
11723 +     hash_table_t *hash = &conf->pkg_hash;
11724 +     abstract_pkg_t *apkg = NULL;
11725 +
11726 +     if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
11727 +         return NULL;
11728 +     
11729 +     return pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0);
11730 +}
11731 +
11732 +
11733 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
11734 +                                      const char *pkg_name,
11735 +                                      const char * version)
11736 +{
11737 +    pkg_vec_t * vec;
11738 +    register int i;
11739 +    char *version_str = NULL;
11740 +    
11741 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
11742 +       return NULL;
11743 +    
11744 +    for(i = 0; i < vec->len; i++) {
11745 +       version_str = pkg_version_str_alloc(vec->pkgs[i]);
11746 +       if(!strcmp(version_str, version)) {
11747 +           free(version_str);
11748 +           break;
11749 +       }
11750 +       free(version_str);
11751 +    }
11752 +       
11753 +    if(i == vec->len)
11754 +       return NULL;
11755 +    
11756 +    return vec->pkgs[i];
11757 +}
11758 +
11759 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
11760 +                                            const char *pkg_name,
11761 +                                            pkg_dest_t *dest)
11762 +{
11763 +    pkg_vec_t * vec;
11764 +    register int i;
11765 +
11766 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
11767 +       return NULL;
11768 +    }
11769 +    
11770 +    for(i = 0; i < vec->len; i++)
11771 +       if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
11772 +           return vec->pkgs[i];
11773 +        }
11774 +    return NULL;
11775 +}
11776 +
11777 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
11778 +                                       const char *pkg_name)
11779 +{
11780 +    pkg_vec_t * vec;
11781 +    register int i;
11782 +
11783 +    if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
11784 +       return NULL;
11785 +    } 
11786 +
11787 +    for(i = 0; i < vec->len; i++)
11788 +       if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
11789 +           return vec->pkgs[i];
11790 +        } 
11791 +    
11792 +    return NULL;
11793 +}
11794 +
11795 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
11796 +{
11797 +    abstract_pkg_t * ab_pkg;
11798 +
11799 +    if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
11800 +       return NULL;
11801 +    }
11802 +    
11803 +    if (ab_pkg->pkgs) {
11804 +      return ab_pkg->pkgs;
11805 +    } else if (ab_pkg->provided_by) {
11806 +      abstract_pkg_t *abpkg =  abstract_pkg_vec_get(ab_pkg->provided_by, 0);
11807 +      if (abpkg != NULL){
11808 +         return abpkg->pkgs;
11809 +      } else {
11810 +         return ab_pkg->pkgs;
11811 +      }
11812 +    } else {
11813 +      return NULL;
11814 +    }
11815 +}
11816 +
11817 +static int pkg_compare_names(const void *p1, const void *p2)
11818 +{
11819 +  const pkg_t *pkg1 = *(const pkg_t **)p1;
11820 +  const pkg_t *pkg2 = *(const pkg_t **)p2;
11821 +  if (pkg1->name == NULL)
11822 +    return 1;
11823 +  if (pkg2->name == NULL)
11824 +    return -1;
11825 +  return(strcmp(pkg1->name, pkg2->name));
11826 +}
11827 +
11828 +
11829 +static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
11830 +{
11831 +  int j;
11832 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11833 +  pkg_vec_t *all = (pkg_vec_t *)data;
11834 +  pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11835 +  if (pkg_vec) {
11836 +    for (j = 0; j < pkg_vec->len; j++) {
11837 +      pkg_t *pkg = pkg_vec->pkgs[j];
11838 +      pkg_vec_insert(all, pkg);
11839 +    }
11840 +  }
11841 +}
11842 +
11843 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
11844 +{
11845 +    hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
11846 +    qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
11847 +}
11848 +
11849 +static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
11850 +{
11851 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11852 +  pkg_vec_t *all = (pkg_vec_t *)data;
11853 +  pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11854 +  int j;
11855 +  if (pkg_vec) {
11856 +    for (j = 0; j < pkg_vec->len; j++) {
11857 +      pkg_t *pkg = pkg_vec->pkgs[j];
11858 +      if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
11859 +       pkg_vec_insert(all, pkg);
11860 +      }
11861 +    }
11862 +  }
11863 +}
11864 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
11865 +{
11866 +    hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
11867 +    qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
11868 +}
11869 +
11870 +static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
11871 +{
11872 +  int i;
11873 +  pkg_t *pkg;
11874 +  abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11875 +  ipkg_conf_t *conf = (ipkg_conf_t *)data;
11876 +  abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
11877 +  fprintf(stdout, "%s\n", ab_pkg->name);
11878 +  i = 0;
11879 +  if (dependents != NULL)
11880 +    while (dependents [i] != NULL)
11881 +      printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
11882 +  dependents = ab_pkg->provided_by->pkgs;
11883 +  i = 0;
11884 +  if (dependents != NULL)
11885 +    while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
11886 +      printf ("\tprovided by - %s\n", dependents [i ++]->name);
11887 +  pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name);
11888 +  if (pkg) {
11889 +    i = 0;
11890 +    while (i < pkg->depends_count)
11891 +      printf ("\tdepends on - %s\n", pkg->depends_str [i ++]); 
11892 +  }
11893 +}
11894 +void pkg_hash_dump(hash_table_t *hash, void *data)
11895 +{
11896 +
11897 +  printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
11898 +  hash_table_foreach(hash, pkg_hash_dump_helper, data);
11899 +  printf ("\n+=+%s+=+\n\n", __FUNCTION__);    
11900 +}
11901 +
11902 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11903 +{
11904 +  abstract_pkg_t * ab_pkg;
11905 +
11906 +  if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
11907 +    ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
11908 +
11909 +  return ab_pkg;
11910 +}
11911 +
11912 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
11913 +{
11914 +     abstract_pkg_t * ab_pkg;
11915 +     int arch_priority;
11916 +
11917 +     if(!pkg)
11918 +         return pkg;
11919 +
11920 +     arch_priority = pkg->arch_priority;
11921 +
11922 +     if (buildDepends(hash, pkg)<0){
11923 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11924 +        return NULL;
11925 +     }
11926 +     ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
11927 +
11928 +     if (set_status) {
11929 +         if (pkg->state_status == SS_INSTALLED) {
11930 +              ab_pkg->state_status = SS_INSTALLED;
11931 +         } else if (pkg->state_status == SS_UNPACKED) {
11932 +              ab_pkg->state_status = SS_UNPACKED;
11933 +         }
11934 +     }
11935 +
11936 +     if(!ab_pkg->pkgs)
11937 +         ab_pkg->pkgs = pkg_vec_alloc();
11938 +    
11939 +     /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
11940 +     pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
11941 +     pkg->parent = ab_pkg;
11942 +
11943 +     if (buildProvides(hash, ab_pkg, pkg)<0){
11944 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11945 +        return NULL;
11946 +     }
11947 +     /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
11948 +     if (buildConflicts(hash, ab_pkg, pkg)<0){
11949 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11950 +        return NULL;
11951 +     }
11952 +     if (buildReplaces(hash, ab_pkg, pkg)<0) {
11953 +        fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11954 +        return NULL;
11955 +     }
11956 +    
11957 +     buildDependedUponBy(pkg, ab_pkg);
11958 +     return pkg;
11959 +}
11960 +
11961 +/*
11962 + * this will assume that we've already determined that
11963 + * the abstract pkg doesn't exist, 'cause we should know these things...
11964 + */
11965 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11966 +{
11967 +  abstract_pkg_t * ab_pkg;
11968 +
11969 +  ab_pkg = abstract_pkg_new();
11970 +  if (ab_pkg == NULL) { return NULL; }
11971 +
11972 +  ab_pkg->name = strdup(pkg_name);
11973 +  hash_table_insert(hash, pkg_name, ab_pkg);
11974 +
11975 +  return ab_pkg;
11976 +}
11977 +
11978 +
11979 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name)
11980 +{
11981 +     hash_table_t *file_hash = &conf->file_hash;
11982 +
11983 +     return hash_table_get(file_hash, file_name); 
11984 +}
11985 +
11986 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
11987 +{
11988 +     hash_table_t *file_hash = &conf->file_hash;
11989 +     pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
11990 +     int file_name_len = strlen(file_name);
11991 +
11992 +     if (file_name[file_name_len -1] == '/')
11993 +         return 0;
11994 +
11995 +     if (conf->offline_root) {
11996 +         int len = strlen(conf->offline_root);
11997 +         if (strncmp(file_name, conf->offline_root, len) == 0) {
11998 +              file_name += len;
11999 +         }
12000 +     }
12001 +
12002 +     // ipkg_message(conf, IPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
12003 +     hash_table_insert(file_hash, file_name, owning_pkg); 
12004 +     if (old_owning_pkg) {
12005 +         str_list_remove_elt(old_owning_pkg->installed_files, file_name);
12006 +         /* mark this package to have its filelist written */
12007 +         old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
12008 +         owning_pkg->state_flag |= SF_FILELIST_CHANGED;
12009 +     }
12010 +     return 0;
12011 +}
12012 +
12013 +
12014 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_hash.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.h
12015 --- busybox-1.2.0-orig/archival/libipkg/pkg_hash.h      1970-01-01 01:00:00.000000000 +0100
12016 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_hash.h     2006-07-22 16:31:25.000000000 +0200
12017 @@ -0,0 +1,61 @@
12018 +/* pkg_hash.h - the itsy package management system
12019 +
12020 +   Steven M. Ayer
12021 +   
12022 +   Copyright (C) 2002 Compaq Computer Corporation
12023 +
12024 +   This program is free software; you can redistribute it and/or
12025 +   modify it under the terms of the GNU General Public License as
12026 +   published by the Free Software Foundation; either version 2, or (at
12027 +   your option) any later version.
12028 +
12029 +   This program is distributed in the hope that it will be useful, but
12030 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12031 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12032 +   General Public License for more details.
12033 +*/
12034 +
12035 +#ifndef PKG_HASH_H
12036 +#define PKG_HASH_H
12037 +
12038 +#include "pkg.h"
12039 +#include "pkg_vec.h"
12040 +#include "hash_table.h"
12041 +
12042 +
12043 +int pkg_hash_init(const char *name, hash_table_t *hash, int len);
12044 +void pkg_hash_deinit(hash_table_t *hash);
12045 +void pkg_hash_map(hash_table_t *hash, void (*f)(void *data, void *entry), void *data);
12046 +
12047 +void pkg_hash_dump(hash_table_t *hash, void *data);
12048 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *available);
12049 +
12050 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
12051 +                          pkg_src_t *src, pkg_dest_t *dest, int is_status_file);
12052 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf);
12053 +
12054 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
12055 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name);
12056 +pkg_vec_t *pkg_hash_fetch_by_name(hash_table_t *hash, const char *pkg_name);
12057 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *installed);
12058 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash, 
12059 +                                      const char *pkg_name,
12060 +                                      const char * version);
12061 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name);
12062 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg, 
12063 +                                                 int (*constraint_fcn)(pkg_t *pkg, void *data), void *cdata, int quiet);
12064 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name);
12065 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
12066 +                                       const char *pkg_name);
12067 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
12068 +                                            const char *pkg_name,
12069 +                                            pkg_dest_t *dest);
12070 +
12071 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name);
12072 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *pkg);
12073 +
12074 +/* XXX: shouldn't this go in pkg_vec.[ch]? */
12075 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name);
12076 +
12077 +#endif
12078 +
12079 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_parse.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.c
12080 --- busybox-1.2.0-orig/archival/libipkg/pkg_parse.c     1970-01-01 01:00:00.000000000 +0100
12081 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.c    2006-07-22 16:31:25.000000000 +0200
12082 @@ -0,0 +1,366 @@
12083 +/* pkg_parse.c - the itsy package management system
12084 +
12085 +   Steven M. Ayer
12086 +   
12087 +   Copyright (C) 2002 Compaq Computer Corporation
12088 +
12089 +   This program is free software; you can redistribute it and/or
12090 +   modify it under the terms of the GNU General Public License as
12091 +   published by the Free Software Foundation; either version 2, or (at
12092 +   your option) any later version.
12093 +
12094 +   This program is distributed in the hope that it will be useful, but
12095 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12096 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12097 +   General Public License for more details.
12098 +*/
12099 +
12100 +#include "ipkg.h"
12101 +#include <errno.h>
12102 +#include <ctype.h>
12103 +   
12104 +#include "pkg.h"
12105 +#include "ipkg_utils.h"
12106 +#include "pkg_parse.h"
12107 +
12108 +int isGenericFieldType(char * type, char * line)
12109 +{
12110 +    if(!strncmp(line, type, strlen(type)))
12111 +       return 1;
12112 +    return 0;
12113 +}
12114 +
12115 +char * parseGenericFieldType(char * type, char * raw)
12116 +{
12117 +    char * field_value = raw + (strlen(type) + 1);
12118 +    return trim_alloc(field_value);
12119 +}
12120 +
12121 +void parseStatus(pkg_t *pkg, char * raw)
12122 +{
12123 +    char sw_str[64], sf_str[64], ss_str[64];
12124 +
12125 +    sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
12126 +    pkg->state_want = pkg_state_want_from_str(sw_str);
12127 +    pkg->state_flag = pkg_state_flag_from_str(sf_str);
12128 +    pkg->state_status = pkg_state_status_from_str(ss_str);
12129 +}
12130 +
12131 +char ** parseDependsString(char * raw, int * depends_count)
12132 +{
12133 +    char ** depends = NULL;
12134 +    int line_count = 0;
12135 +    char buff[2048], * dest;
12136 +
12137 +    while(raw && *raw && !isspace(*raw)) {
12138 +       raw++;
12139 +    }
12140 +
12141 +    if(line_is_blank(raw)){
12142 +       *depends_count = line_count;
12143 +       return NULL;
12144 +    }
12145 +    while(raw && *raw){
12146 +       depends = (char **)realloc(depends, sizeof(char *) * (line_count + 1));
12147 +       
12148 +       while(isspace(*raw)) raw++;
12149 +
12150 +       dest = buff;
12151 +       while((*raw != ',') && *raw)
12152 +           *dest++ = *raw++;
12153 +
12154 +       *dest = '\0';
12155 +       depends[line_count] = trim_alloc(buff);
12156 +       if(depends[line_count] ==NULL)
12157 +          return NULL;
12158 +        line_count++;
12159 +       if(*raw == ',')
12160 +           raw++;
12161 +    }
12162 +    *depends_count = line_count;
12163 +    return depends;
12164 +}
12165 +
12166 +void parseConffiles(pkg_t * pkg, char * raw)
12167 +{
12168 +    char file_name[1048], md5sum[1048];  /* please tell me there aren't any longer that 1k */
12169 +
12170 +    if(!strncmp(raw, "Conffiles:", 10))
12171 +       raw += strlen("Conffiles:");
12172 +
12173 +    while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
12174 +       conffile_list_append(&pkg->conffiles, file_name, md5sum);
12175 +       /*      fprintf(stderr, "%s %s ", file_name, md5sum);*/
12176 +       while (*raw && isspace(*raw)) {
12177 +           raw++;
12178 +       }
12179 +       raw += strlen(file_name);
12180 +       while (*raw && isspace(*raw)) {
12181 +           raw++;
12182 +       }
12183 +       raw += strlen(md5sum);
12184 +    }
12185 +}    
12186 +
12187 +int parseVersion(pkg_t *pkg, char *raw)
12188 +{
12189 +  char *colon, *eepochcolon;
12190 +#ifdef USE_DEBVERSION
12191 +  char *hyphen;
12192 +#endif
12193 +  unsigned long epoch;
12194 +
12195 +  if (!*raw) {
12196 +      fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
12197 +      return EINVAL;
12198 +  }
12199 +
12200 +  if (strncmp(raw, "Version:", 8) == 0) {
12201 +      raw += 8;
12202 +  }
12203 +  while (*raw && isspace(*raw)) {
12204 +      raw++;
12205 +  }
12206 +  
12207 +  colon= strchr(raw,':');
12208 +  if (colon) {
12209 +    epoch= strtoul(raw,&eepochcolon,10);
12210 +    if (colon != eepochcolon) {
12211 +       fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
12212 +       return EINVAL;
12213 +    }
12214 +    if (!*++colon) {
12215 +       fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
12216 +       return EINVAL;
12217 +    }
12218 +    raw= colon;
12219 +    pkg->epoch= epoch;
12220 +  } else {
12221 +    pkg->epoch= 0;
12222 +  }
12223 +
12224 +  pkg->revision = "";
12225 +  pkg->familiar_revision = "";
12226 +
12227 +  pkg->version= malloc(strlen(raw)+1);
12228 +  if ( pkg->version == NULL ) {
12229 +     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12230 +     return ENOMEM;
12231 +  }
12232 +  strcpy(pkg->version, raw);
12233 +
12234 +#ifdef USE_DEBVERSION
12235 +  hyphen= strrchr(pkg->version,'-');
12236 +
12237 +  if (hyphen) {
12238 +    *hyphen++= 0;
12239 +    if (strncmp("fam", hyphen, 3) == 0) {
12240 +      pkg->familiar_revision=hyphen+3;
12241 +      hyphen= strrchr(pkg->version,'-');
12242 +      if (hyphen) {
12243 +       *hyphen++= 0;
12244 +       pkg->revision = hyphen;
12245 +      }
12246 +    } else {
12247 +      pkg->revision = hyphen;
12248 +    }
12249 +  }
12250 +#endif
12251 +
12252 +/*
12253 +  fprintf(stderr,"Parsed version: %lu, %s, %s, %s\n",
12254 +         pkg->epoch,
12255 +         pkg->version,
12256 +         pkg->revision,
12257 +         pkg->familiar_revision);
12258 +*/
12259 +         
12260 +  return 0;
12261 +}
12262 +
12263 +
12264 +/* This code is needed to insert in first position the keyword for the aligning bug */
12265 +
12266 +int alterProvidesLine(char *raw, char *temp)
12267 +{
12268 +
12269 +
12270 +  if (!*raw) {
12271 +      fprintf(stderr, "%s: ERROR: Provides string is empty", __FUNCTION__);
12272 +      return -EINVAL;
12273 +  }
12274 +
12275 +  if ( temp == NULL ) {
12276 +     fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12277 +     return -ENOMEM;
12278 +  }
12279 +
12280 +  if (strncmp(raw, "Provides:", 9) == 0) {
12281 +      raw += 9;
12282 +  }
12283 +  while (*raw && isspace(*raw)) {
12284 +      raw++;
12285 +  }      
12286 +  
12287 +  snprintf ( temp, 35, "Provides: ipkg_internal_use_only, ");           /* First part of the line */
12288 +  while (*raw) {
12289 +     strncat( temp, raw++, 1);
12290 +  }
12291 +  return 0;
12292
12293 +}
12294 +
12295 +/* Some random thoughts from Carl:
12296 +
12297 +   This function could be considerably simplified if we just kept
12298 +   an array of all the generic string-valued field names, and looped
12299 +   through those looking for a match. Also, these fields could perhaps
12300 +   be stored in the package as an array as well, (or, probably better,
12301 +   as an nv_pair_list_t).
12302 +
12303 +   Fields which require special parsing or storage, (such as Depends:
12304 +   and Status:) could be handled as they are now. 
12305 +*/
12306 +/* XXX: FEATURE: The Suggests: field needs to be changed from a string
12307 +   to a dependency list. And, since we already have
12308 +   Depends/Pre-Depends and need to add Conflicts, Recommends, and
12309 +   Enhances, perhaps we could generalize all of these and save some
12310 +   code duplication.
12311 +*/
12312 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
12313 +{
12314 +    int reading_conffiles, reading_description;
12315 +    int pkg_false_provides=1;
12316 +    char ** lines;
12317 +    char * provide=NULL;
12318 +
12319 +    pkg->src = src;
12320 +    pkg->dest = dest;
12321 +
12322 +    reading_conffiles = reading_description = 0;
12323 +
12324 +    for (lines = *raw; *lines; lines++) {
12325 +       /*      fprintf(stderr, "PARSING %s\n", *lines);*/
12326 +       if(isGenericFieldType("Package:", *lines)) 
12327 +           pkg->name = parseGenericFieldType("Package", *lines);
12328 +       else if(isGenericFieldType("Architecture:", *lines))
12329 +           pkg->architecture = parseGenericFieldType("Architecture", *lines);
12330 +       else if(isGenericFieldType("Filename:", *lines))
12331 +           pkg->filename = parseGenericFieldType("Filename", *lines);
12332 +       else if(isGenericFieldType("Section:", *lines))
12333 +           pkg->section = parseGenericFieldType("Section", *lines);
12334 +       else if(isGenericFieldType("MD5sum:", *lines))
12335 +           pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
12336 +       /* The old ipkg wrote out status files with the wrong case for MD5sum,
12337 +          let's parse it either way */
12338 +       else if(isGenericFieldType("MD5Sum:", *lines))
12339 +           pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
12340 +       else if(isGenericFieldType("Size:", *lines))
12341 +           pkg->size = parseGenericFieldType("Size", *lines);
12342 +       else if(isGenericFieldType("Source:", *lines))
12343 +           pkg->source = parseGenericFieldType("Source", *lines);
12344 +       else if(isGenericFieldType("Installed-Size:", *lines))
12345 +           pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
12346 +       else if(isGenericFieldType("Installed-Time:", *lines)) {
12347 +            char *time_str = parseGenericFieldType("Installed-Time", *lines);
12348 +            pkg->installed_time = strtoul(time_str, NULL, 0);
12349 +       } else if(isGenericFieldType("Priority:", *lines))
12350 +           pkg->priority = parseGenericFieldType("Priority", *lines);
12351 +       else if(isGenericFieldType("Essential:", *lines)) {
12352 +           char *essential_value;
12353 +           essential_value = parseGenericFieldType("Essential", *lines);
12354 +           if (strcmp(essential_value, "yes") == 0) {
12355 +               pkg->essential = 1;
12356 +           }
12357 +           free(essential_value);
12358 +       }
12359 +       else if(isGenericFieldType("Status", *lines))
12360 +           parseStatus(pkg, *lines);
12361 +       else if(isGenericFieldType("Version", *lines))
12362 +           parseVersion(pkg, *lines);
12363 +       else if(isGenericFieldType("Maintainer", *lines))
12364 +           pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
12365 +       else if(isGenericFieldType("Conffiles", *lines)){
12366 +           parseConffiles(pkg, *lines);
12367 +           reading_conffiles = 1;
12368 +       }
12369 +       else if(isGenericFieldType("Description", *lines)) {
12370 +           pkg->description = parseGenericFieldType("Description", *lines);
12371 +           reading_conffiles = 0;
12372 +           reading_description = 1;
12373 +       }
12374 +
12375 +       else if(isGenericFieldType("Provides", *lines)){
12376 +/* Here we add the internal_use to align the off by one problem between provides_str and provides */
12377 +            provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new ipkg_internal_use_only */
12378 +            if ( alterProvidesLine(*lines,provide) ){
12379 +               return EINVAL;
12380 +            }
12381 +           pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
12382 +/* Let's try to hack a bit here.
12383 +   The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
12384 +   in alot of other places. We will remove it before writing down the status database */
12385 +            pkg_false_provides=0;
12386 +            free(provide);
12387 +        } 
12388 +
12389 +       else if(isGenericFieldType("Depends", *lines))
12390 +           pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
12391 +       else if(isGenericFieldType("Pre-Depends", *lines))
12392 +           pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
12393 +       else if(isGenericFieldType("Recommends", *lines))
12394 +           pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
12395 +       else if(isGenericFieldType("Suggests", *lines))
12396 +           pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
12397 +       /* Abhaya: support for conflicts */
12398 +       else if(isGenericFieldType("Conflicts", *lines))
12399 +           pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
12400 +       else if(isGenericFieldType("Replaces", *lines))
12401 +           pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
12402 +       else if(line_is_blank(*lines)) {
12403 +           lines++;
12404 +           break;
12405 +       }
12406 +       else if(**lines == ' '){
12407 +           if(reading_description) {
12408 +               /* we already know it's not blank, so the rest of description */      
12409 +               pkg->description = realloc(pkg->description,
12410 +                                          strlen(pkg->description)
12411 +                                          + 1 + strlen(*lines) + 1);
12412 +               strcat(pkg->description, "\n");
12413 +               strcat(pkg->description, (*lines));
12414 +           }
12415 +           else if(reading_conffiles)
12416 +               parseConffiles(pkg, *lines);
12417 +       }
12418 +    }
12419 +    *raw = lines;
12420 +/* If the ipk has not a Provides line, we insert our false line */ 
12421 +    if ( pkg_false_provides==1)
12422 +       pkg->provides_str = parseDependsString ((char *)"Provides: ipkg_internal_use_only ", &pkg->provides_count);
12423 +
12424 +    if (pkg->name) {
12425 +       return 0;
12426 +    } else {
12427 +       return EINVAL;
12428 +    }
12429 +}
12430 +
12431 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
12432 +{
12433 +    char ** lines;
12434 +
12435 +    for (lines = *raw; *lines; lines++) {
12436 +       if(isGenericFieldType("Essential:", *lines)) {
12437 +           char *essential_value;
12438 +           essential_value = parseGenericFieldType("Essential", *lines);
12439 +           if (strcmp(essential_value, "yes") == 0) {
12440 +               pkg->essential = 1;
12441 +           }
12442 +           free(essential_value);
12443 +       }
12444 +    }
12445 +    *raw = lines;
12446 +
12447 +    return 0;
12448 +}
12449 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_parse.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.h
12450 --- busybox-1.2.0-orig/archival/libipkg/pkg_parse.h     1970-01-01 01:00:00.000000000 +0100
12451 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_parse.h    2006-07-22 16:31:25.000000000 +0200
12452 @@ -0,0 +1,31 @@
12453 +/* pkg_parse.h - the itsy package management system
12454 +
12455 +   Steven M. Ayer
12456 +   
12457 +   Copyright (C) 2002 Compaq Computer Corporation
12458 +
12459 +   This program is free software; you can redistribute it and/or
12460 +   modify it under the terms of the GNU General Public License as
12461 +   published by the Free Software Foundation; either version 2, or (at
12462 +   your option) any later version.
12463 +
12464 +   This program is distributed in the hope that it will be useful, but
12465 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12466 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12467 +   General Public License for more details.
12468 +*/
12469 +
12470 +#ifndef PKG_PARSE_H
12471 +#define PKG_PARSE_H
12472 +
12473 +int isGenericFieldType(char * type, char * line);
12474 +char * parseGenericFieldType(char * type, char * raw);
12475 +void parseStatus(pkg_t *pkg, char * raw);
12476 +int parseVersion(pkg_t *pkg, char *raw);
12477 +char ** parseDependsString(char * raw, int * depends_count);
12478 +int parseVersion(pkg_t *pkg, char *raw);
12479 +void parseConffiles(pkg_t * pkg, char * raw);
12480 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
12481 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
12482 +
12483 +#endif
12484 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.c
12485 --- busybox-1.2.0-orig/archival/libipkg/pkg_src.c       1970-01-01 01:00:00.000000000 +0100
12486 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.c      2006-07-22 16:31:25.000000000 +0200
12487 @@ -0,0 +1,43 @@
12488 +/* pkg_src.c - the itsy package management system
12489 +
12490 +   Carl D. Worth
12491 +
12492 +   Copyright (C) 2001 University of Southern California
12493 +
12494 +   This program is free software; you can redistribute it and/or
12495 +   modify it under the terms of the GNU General Public License as
12496 +   published by the Free Software Foundation; either version 2, or (at
12497 +   your option) any later version.
12498 +
12499 +   This program is distributed in the hope that it will be useful, but
12500 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12501 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12502 +   General Public License for more details.
12503 +*/
12504 +
12505 +#include "ipkg.h"
12506 +
12507 +#include "pkg_src.h"
12508 +#include "str_util.h"
12509 +
12510 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip)
12511 +{
12512 +    src->gzip = gzip;
12513 +    src->name = str_dup_safe (name);
12514 +    src->value = str_dup_safe (base_url);
12515 +    if (extra_data)
12516 +       src->extra_data = str_dup_safe (extra_data);
12517 +    else
12518 +       src->extra_data = NULL;
12519 +    return 0;
12520 +}
12521 +
12522 +void pkg_src_deinit(pkg_src_t *src)
12523 +{
12524 +    free (src->name);
12525 +    free (src->value);
12526 +    if (src->extra_data)
12527 +       free (src->extra_data);
12528 +}
12529 +
12530 +
12531 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.h
12532 --- busybox-1.2.0-orig/archival/libipkg/pkg_src.h       1970-01-01 01:00:00.000000000 +0100
12533 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src.h      2006-07-22 16:31:25.000000000 +0200
12534 @@ -0,0 +1,34 @@
12535 +/* pkg_src.h - the itsy package management system
12536 +
12537 +   Carl D. Worth
12538 +
12539 +   Copyright (C) 2001 University of Southern California
12540 +
12541 +   This program is free software; you can redistribute it and/or
12542 +   modify it under the terms of the GNU General Public License as
12543 +   published by the Free Software Foundation; either version 2, or (at
12544 +   your option) any later version.
12545 +
12546 +   This program is distributed in the hope that it will be useful, but
12547 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12548 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12549 +   General Public License for more details.
12550 +*/
12551 +
12552 +#ifndef PKG_SRC_H
12553 +#define PKG_SRC_H
12554 +
12555 +#include "nv_pair.h"
12556 +
12557 +typedef struct 
12558 +{
12559 +  char *name;
12560 +  char *value;
12561 +  char *extra_data;
12562 +  int gzip;
12563 +} pkg_src_t;
12564 +
12565 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip);
12566 +void pkg_src_deinit(pkg_src_t *src);
12567 +
12568 +#endif
12569 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.c
12570 --- busybox-1.2.0-orig/archival/libipkg/pkg_src_list.c  1970-01-01 01:00:00.000000000 +0100
12571 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.c 2006-07-22 16:31:25.000000000 +0200
12572 @@ -0,0 +1,75 @@
12573 +/* pkg_src_list.c - the itsy package management system
12574 +
12575 +   Carl D. Worth
12576 +
12577 +   Copyright (C) 2001 University of Southern California
12578 +
12579 +   This program is free software; you can redistribute it and/or
12580 +   modify it under the terms of the GNU General Public License as
12581 +   published by the Free Software Foundation; either version 2, or (at
12582 +   your option) any later version.
12583 +
12584 +   This program is distributed in the hope that it will be useful, but
12585 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12586 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12587 +   General Public License for more details.
12588 +*/
12589 +
12590 +#include "ipkg.h"
12591 +
12592 +#include "pkg_src_list.h"
12593 +#include "void_list.h"
12594 +
12595 +int pkg_src_list_init(pkg_src_list_t *list)
12596 +{
12597 +    return void_list_init((void_list_t *) list);
12598 +}
12599 +
12600 +void pkg_src_list_deinit(pkg_src_list_t *list)
12601 +{
12602 +    pkg_src_list_elt_t *iter;
12603 +    pkg_src_t *pkg_src;
12604 +
12605 +    for (iter = list->head; iter; iter = iter->next) {
12606 +      pkg_src = iter->data;
12607 +      pkg_src_deinit(pkg_src);
12608 +
12609 +      /* malloced in pkg_src_list_append */
12610 +      free(pkg_src);
12611 +      iter->data = NULL;
12612 +    }
12613 +    void_list_deinit((void_list_t *) list);
12614 +}
12615 +
12616 +pkg_src_t *pkg_src_list_append(pkg_src_list_t *list,
12617 +                              const char *name, const char *base_url, const char *extra_data,
12618 +                              int gzip)
12619 +{
12620 +    int err;
12621 +
12622 +    /* freed in pkg_src_list_deinit */
12623 +    pkg_src_t *pkg_src = malloc(sizeof(pkg_src_t));
12624 +
12625 +    if (pkg_src == NULL) {
12626 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12627 +       return NULL;
12628 +    }
12629 +    pkg_src_init(pkg_src, name, base_url, extra_data, gzip);
12630 +
12631 +    err = void_list_append((void_list_t *) list, pkg_src);
12632 +    if (err) {
12633 +       return NULL;
12634 +    }
12635 +
12636 +    return pkg_src;
12637 +}
12638 +
12639 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data)
12640 +{
12641 +    return void_list_push((void_list_t *) list, data);
12642 +}
12643 +
12644 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list)
12645 +{
12646 +    return (pkg_src_list_elt_t *) void_list_pop((void_list_t *) list);
12647 +}
12648 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_src_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.h
12649 --- busybox-1.2.0-orig/archival/libipkg/pkg_src_list.h  1970-01-01 01:00:00.000000000 +0100
12650 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_src_list.h 2006-07-22 16:31:25.000000000 +0200
12651 @@ -0,0 +1,57 @@
12652 +/* pkg_src_list.h - the itsy package management system
12653 +
12654 +   Carl D. Worth
12655 +
12656 +   Copyright (C) 2001 University of Southern California
12657 +
12658 +   This program is free software; you can redistribute it and/or
12659 +   modify it under the terms of the GNU General Public License as
12660 +   published by the Free Software Foundation; either version 2, or (at
12661 +   your option) any later version.
12662 +
12663 +   This program is distributed in the hope that it will be useful, but
12664 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12665 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12666 +   General Public License for more details.
12667 +*/
12668 +
12669 +#ifndef PKG_SRC_LIST_H
12670 +#define PKG_SRC_LIST_H
12671 +
12672 +#include "pkg_src.h"
12673 +
12674 +typedef struct pkg_src_list_elt pkg_src_list_elt_t;
12675 +struct pkg_src_list_elt
12676 +{
12677 +    pkg_src_list_elt_t *next;
12678 +    pkg_src_t *data;
12679 +};
12680 +
12681 +typedef struct pkg_src_list pkg_src_list_t;
12682 +struct pkg_src_list
12683 +{
12684 +    pkg_src_list_elt_t pre_head;
12685 +    pkg_src_list_elt_t *head;
12686 +    pkg_src_list_elt_t *tail;
12687 +};
12688 +
12689 +static inline int pkg_src_list_empty(pkg_src_list_t *list)
12690 +{
12691 +     if (list->head == NULL)
12692 +         return 1;
12693 +     else
12694 +         return 0;
12695 +}
12696 +
12697 +int pkg_src_list_elt_init(pkg_src_list_elt_t *elt, nv_pair_t *data);
12698 +void pkg_src_list_elt_deinit(pkg_src_list_elt_t *elt);
12699 +
12700 +int pkg_src_list_init(pkg_src_list_t *list);
12701 +void pkg_src_list_deinit(pkg_src_list_t *list);
12702 +
12703 +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);
12704 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data);
12705 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list);
12706 +
12707 +#endif
12708 +
12709 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_vec.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.c
12710 --- busybox-1.2.0-orig/archival/libipkg/pkg_vec.c       1970-01-01 01:00:00.000000000 +0100
12711 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.c      2006-07-22 16:31:26.000000000 +0200
12712 @@ -0,0 +1,230 @@
12713 +/* pkg_vec.c - the itsy package management system
12714 +
12715 +   Steven M. Ayer
12716 +   
12717 +   Copyright (C) 2002 Compaq Computer Corporation
12718 +
12719 +   This program is free software; you can redistribute it and/or
12720 +   modify it under the terms of the GNU General Public License as
12721 +   published by the Free Software Foundation; either version 2, or (at
12722 +   your option) any later version.
12723 +
12724 +   This program is distributed in the hope that it will be useful, but
12725 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12726 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12727 +   General Public License for more details.
12728 +*/
12729 +
12730 +#include <stdlib.h>
12731 +#include <fnmatch.h>
12732 +#include "xregex.h"
12733 +#include "ipkg.h"
12734 +#include "pkg.h"
12735 +
12736 +pkg_vec_t * pkg_vec_alloc(void)
12737 +{
12738 +    pkg_vec_t * vec = (pkg_vec_t *)malloc(sizeof(pkg_vec_t));
12739 +    if (!vec) {
12740 +      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12741 +      return NULL;
12742 +    }
12743 +    vec->pkgs = NULL;
12744 +    vec->len = 0;
12745 +
12746 +    return vec;
12747 +}
12748 +
12749 +void pkg_vec_free(pkg_vec_t *vec)
12750 +{
12751 +    free(vec->pkgs);
12752 +    free(vec);
12753 +}
12754 +
12755 +/*
12756 + * assumption: all names in a vector are identical
12757 + * assumption: all version strings are trimmed,
12758 + *             so identical versions have identical version strings,
12759 + *             implying identical packages; let's marry these
12760 + */
12761 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
12762 +{
12763 +     int i;
12764 +     int found = 0;
12765 +
12766 +     /* look for a duplicate pkg by name, version, and architecture */
12767 +     for (i = 0; i < vec->len; i++){
12768 +         ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found pkg=%s version=%s arch=%s cmp=%s version=%s arch=%s \n", 
12769 +                      __FUNCTION__, pkg->name, pkg->version, pkg->architecture, 
12770 +                       vec->pkgs[i]->name, vec->pkgs[i]->version,vec->pkgs[i]->architecture );
12771 +         if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12772 +             && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12773 +             && (strcmp(pkg->architecture, vec->pkgs[i]->architecture) == 0)) {
12774 +              found  = 1;
12775 +               ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found duplicate for pkg=%s version=%s arch=%s\n",
12776 +                             __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12777 +              break;
12778 +         }
12779 +     }
12780 +
12781 +     /* we didn't find one, add it */
12782 +     if (!found){   
12783 +         ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
12784 +                      __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12785 +
12786 +         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12787 +         vec->pkgs[vec->len] = pkg;
12788 +         vec->len++;
12789 +         return pkg;
12790 +     }
12791 +     /* update the one that we have */
12792 +     else {
12793 +          ipkg_message(conf, IPKG_DEBUG2, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
12794 +                        __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12795 +         if (set_status) {
12796 +              /* this is from the status file, so need to merge with existing database */
12797 +               ipkg_message(conf, IPKG_DEBUG2, " with set_status\n");
12798 +              pkg_merge(vec->pkgs[i], pkg, set_status);
12799 +              /* XXX: CLEANUP: It's not so polite to free something here
12800 +                 that was passed in from above. */
12801 +              pkg_deinit(pkg);
12802 +              free(pkg);
12803 +         } else {
12804 +               ipkg_message(conf, IPKG_DEBUG2, " WITHOUT set_status\n");
12805 +              /* just overwrite the old one */
12806 +              pkg_deinit(vec->pkgs[i]);
12807 +              free(vec->pkgs[i]);
12808 +              vec->pkgs[i] = pkg;
12809 +         }
12810 +         return vec->pkgs[i];
12811 +     }
12812 +}
12813 +
12814 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
12815 +{
12816 +     int i;
12817 +     int found = 0;
12818 +
12819 +     /* look for a duplicate pkg by name, version, and architecture */
12820 +     for (i = 0; i < vec->len; i++)
12821 +         if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12822 +             && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12823 +             && (strcmp(pkg->architecture, vec->pkgs[i]->name) == 0)) {
12824 +              found = 1;
12825 +              break;
12826 +         }
12827 +
12828 +     /* we didn't find one, add it */
12829 +     if(!found){   
12830 +         vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12831 +         *(const pkg_t **)&vec->pkgs[vec->len] = pkg;
12832 +         vec->len++;
12833 +     }
12834 +}
12835 +
12836 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
12837 +{
12838 +     int i;
12839 +     for (i = 0; i < vec->len; i++)
12840 +         if (vec->pkgs[i] == apkg)
12841 +              return 1;
12842 +     return 0;
12843 +}
12844 +
12845 +typedef int (*compare_fcn_t)(const void *, const void *);
12846 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *))
12847 +{
12848 +     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12849 +}
12850 +
12851 +int pkg_vec_clear_marks(pkg_vec_t *vec)
12852 +{
12853 +     int npkgs = vec->len;
12854 +     int i;
12855 +     for (i = 0; i < npkgs; i++) {
12856 +         pkg_t *pkg = vec->pkgs[i];
12857 +         pkg->state_flag &= ~SF_MARKED;
12858 +     }
12859 +     return 0;
12860 +}
12861 +
12862 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
12863 +{
12864 +     int matching_count = 0;
12865 +     pkg_t **pkgs = vec->pkgs;
12866 +     int npkgs = vec->len;
12867 +     int i;
12868 +     for (i = 0; i < npkgs; i++) {
12869 +         pkg_t *pkg = pkgs[i];
12870 +         if (fnmatch(pattern, pkg->name, 0)==0) {
12871 +              pkg->state_flag |= SF_MARKED;
12872 +              matching_count++;
12873 +         }
12874 +     }
12875 +     return matching_count;
12876 +}
12877 +
12878 +
12879 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
12880 +{
12881 +    abstract_pkg_vec_t * vec ; 
12882 +    vec = (abstract_pkg_vec_t *)malloc(sizeof(abstract_pkg_vec_t));
12883 +    if (!vec) {
12884 +      fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12885 +      return NULL;
12886 +    }
12887 +    vec->pkgs = NULL;
12888 +    vec->len = 0;
12889 +
12890 +    return vec;
12891 +}
12892 +
12893 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
12894 +{
12895 +    free(vec->pkgs);
12896 +    free(vec);
12897 +}
12898 +
12899 +/*
12900 + * assumption: all names in a vector are unique
12901 + */
12902 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
12903 +{
12904 +    int i;
12905 +
12906 +    /* look for a duplicate pkg by name */
12907 +    for(i = 0; i < vec->len; i++)
12908 +       if (strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12909 +           break;
12910 +
12911 +    /* we didn't find one, add it */
12912 +    if(i == vec->len){   
12913 +       vec->pkgs = 
12914 +         (abstract_pkg_t **)
12915 +           realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
12916 +       vec->pkgs[vec->len] = pkg;
12917 +       vec->len++;
12918 +    }
12919 +}
12920 +
12921 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
12922 +{
12923 +    if (vec->len > i) 
12924 +       return vec->pkgs[i];
12925 +    else
12926 +       return NULL;
12927 +}
12928 +
12929 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
12930 +{
12931 +     int i;
12932 +     for (i = 0; i < vec->len; i++)
12933 +         if (vec->pkgs[i] == apkg)
12934 +              return 1;
12935 +     return 0;
12936 +}
12937 +
12938 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *))
12939 +{
12940 +     qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12941 +}
12942 +
12943 diff -ruN busybox-1.2.0-orig/archival/libipkg/pkg_vec.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.h
12944 --- busybox-1.2.0-orig/archival/libipkg/pkg_vec.h       1970-01-01 01:00:00.000000000 +0100
12945 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/pkg_vec.h      2006-07-22 16:31:26.000000000 +0200
12946 @@ -0,0 +1,62 @@
12947 +/* pkg_vec.h - the itsy package management system
12948 +
12949 +   Steven M. Ayer
12950 +   
12951 +   Copyright (C) 2002 Compaq Computer Corporation
12952 +
12953 +   This program is free software; you can redistribute it and/or
12954 +   modify it under the terms of the GNU General Public License as
12955 +   published by the Free Software Foundation; either version 2, or (at
12956 +   your option) any later version.
12957 +
12958 +   This program is distributed in the hope that it will be useful, but
12959 +   WITHOUT ANY WARRANTY; without even the implied warranty of
12960 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12961 +   General Public License for more details.
12962 +*/
12963 +
12964 +#ifndef PKG_VEC_H
12965 +#define PKG_VEC_H
12966 +
12967 +typedef struct pkg pkg_t;
12968 +typedef struct abstract_pkg abstract_pkg_t;
12969 +
12970 +struct pkg_vec
12971 +{
12972 +    pkg_t **pkgs;
12973 +    int len;
12974 +};
12975 +typedef struct pkg_vec pkg_vec_t;
12976 +
12977 +struct abstract_pkg_vec
12978 +{
12979 +    abstract_pkg_t **pkgs;
12980 +    int len;
12981 +};
12982 +typedef struct abstract_pkg_vec abstract_pkg_vec_t;
12983 +
12984 +
12985 +pkg_vec_t * pkg_vec_alloc(void);
12986 +void pkg_vec_free(pkg_vec_t *vec);
12987 +void marry_two_packages(pkg_t * newpkg, pkg_t * oldpkg);
12988 +
12989 +void pkg_vec_add(pkg_vec_t *vec, pkg_t *pkg);
12990 +/* pkg_vec_insert_merge: might munge pkg.
12991 +*  returns the pkg that is in the pkg graph */
12992 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status, ipkg_conf_t *conf);
12993 +/* this one never munges pkg */
12994 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg);
12995 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg);
12996 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *));
12997 +
12998 +int pkg_vec_clear_marks(pkg_vec_t *vec);
12999 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern);
13000 +
13001 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void);
13002 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec);
13003 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg);
13004 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i);
13005 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg);
13006 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *));
13007 +#endif
13008 +
13009 diff -ruN busybox-1.2.0-orig/archival/libipkg/sprintf_alloc.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/sprintf_alloc.h
13010 --- busybox-1.2.0-orig/archival/libipkg/sprintf_alloc.h 1970-01-01 01:00:00.000000000 +0100
13011 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/sprintf_alloc.h        2006-07-22 16:31:26.000000000 +0200
13012 @@ -0,0 +1,25 @@
13013 +/* sprintf_alloca.c -- like sprintf with memory allocation
13014 +
13015 +   Carl D. Worth
13016 +
13017 +   Copyright (C) 2001 University of Southern California
13018 +
13019 +   This program is free software; you can redistribute it and/or modify
13020 +   it under the terms of the GNU General Public License as published by
13021 +   the Free Software Foundation; either version 2, or (at your option)
13022 +   any later version.
13023 +
13024 +   This program is distributed in the hope that it will be useful,
13025 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
13026 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13027 +   GNU General Public License for more details.
13028 +*/
13029 +
13030 +#ifndef SPRINTF_ALLOC_H
13031 +#define SPRINTF_ALLOC_H
13032 +
13033 +#include "libbb.h"
13034 +
13035 +#define sprintf_alloc(str, fmt, args...)  *str = bb_xasprintf(fmt, ## args)
13036 +
13037 +#endif
13038 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.c
13039 --- busybox-1.2.0-orig/archival/libipkg/str_list.c      1970-01-01 01:00:00.000000000 +0100
13040 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.c     2006-07-22 16:31:26.000000000 +0200
13041 @@ -0,0 +1,76 @@
13042 +/* str_list.c - the itsy package management system
13043 +
13044 +   Carl D. Worth
13045 +
13046 +   Copyright (C) 2001 University of Southern California
13047 +
13048 +   This program is free software; you can redistribute it and/or
13049 +   modify it under the terms of the GNU General Public License as
13050 +   published by the Free Software Foundation; either version 2, or (at
13051 +   your option) any later version.
13052 +
13053 +   This program is distributed in the hope that it will be useful, but
13054 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13055 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13056 +   General Public License for more details.
13057 +*/
13058 +
13059 +#include "ipkg.h"
13060 +
13061 +#include "str_list.h"
13062 +
13063 +int str_list_elt_init(str_list_elt_t *elt, char *data)
13064 +{
13065 +    return void_list_elt_init((void_list_elt_t *) elt, data);
13066 +}
13067 +
13068 +void str_list_elt_deinit(str_list_elt_t *elt)
13069 +{
13070 +    void_list_elt_deinit((void_list_elt_t *) elt);
13071 +}
13072 +
13073 +str_list_t *str_list_alloc()
13074 +{
13075 +     str_list_t *list = (str_list_t *)malloc(sizeof(str_list_t));
13076 +     if (list)
13077 +         str_list_init(list);
13078 +     return list;
13079 +}
13080 +
13081 +int str_list_init(str_list_t *list)
13082 +{
13083 +    return void_list_init((void_list_t *) list);
13084 +}
13085 +
13086 +void str_list_deinit(str_list_t *list)
13087 +{
13088 +    void_list_deinit((void_list_t *) list);
13089 +}
13090 +
13091 +int str_list_append(str_list_t *list, char *data)
13092 +{
13093 +    return void_list_append((void_list_t *) list, data);
13094 +}
13095 +
13096 +int str_list_push(str_list_t *list, char *data)
13097 +{
13098 +    return void_list_push((void_list_t *) list, data);
13099 +}
13100 +
13101 +str_list_elt_t *str_list_pop(str_list_t *list)
13102 +{
13103 +    return (str_list_elt_t *) void_list_pop((void_list_t *) list);
13104 +}
13105 +
13106 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter)
13107 +{
13108 +    return (str_list_elt_t *) void_list_remove((void_list_t *) list,
13109 +                                              (void_list_elt_t **) iter);
13110 +}
13111 +
13112 +char *str_list_remove_elt(str_list_t *list, const char *target_str)
13113 +{
13114 +     return (char *)void_list_remove_elt((void_list_t *) list,
13115 +                                        (void *)target_str,
13116 +                                        (void_list_cmp_t)strcmp);
13117 +}
13118 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.h
13119 --- busybox-1.2.0-orig/archival/libipkg/str_list.h      1970-01-01 01:00:00.000000000 +0100
13120 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_list.h     2006-07-22 16:31:26.000000000 +0200
13121 @@ -0,0 +1,51 @@
13122 +/* str_list.h - the itsy package management system
13123 +
13124 +   Carl D. Worth
13125 +
13126 +   Copyright (C) 2001 University of Southern California
13127 +
13128 +   This program is free software; you can redistribute it and/or
13129 +   modify it under the terms of the GNU General Public License as
13130 +   published by the Free Software Foundation; either version 2, or (at
13131 +   your option) any later version.
13132 +
13133 +   This program is distributed in the hope that it will be useful, but
13134 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13135 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13136 +   General Public License for more details.
13137 +*/
13138 +
13139 +#ifndef STR_LIST_H
13140 +#define STR_LIST_H
13141 +
13142 +#include "void_list.h"
13143 +
13144 +typedef struct str_list_elt str_list_elt_t;
13145 +struct str_list_elt
13146 +{
13147 +    str_list_elt_t *next;
13148 +    char *data;
13149 +};
13150 +
13151 +typedef struct xstr_list str_list_t;
13152 +struct xstr_list
13153 +{
13154 +    str_list_elt_t pre_head;
13155 +    str_list_elt_t *head;
13156 +    str_list_elt_t *tail;
13157 +};
13158 +
13159 +int str_list_elt_init(str_list_elt_t *elt, char *data);
13160 +void str_list_elt_deinit(str_list_elt_t *elt);
13161 +
13162 +str_list_t *str_list_alloc(void);
13163 +int str_list_init(str_list_t *list);
13164 +void str_list_deinit(str_list_t *list);
13165 +
13166 +int str_list_append(str_list_t *list, char *data);
13167 +int str_list_push(str_list_t *list, char *data);
13168 +str_list_elt_t *str_list_pop(str_list_t *list);
13169 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter);
13170 +char *str_list_remove_elt(str_list_t *list, const char *target_str);
13171 +
13172 +#endif
13173 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_util.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.c
13174 --- busybox-1.2.0-orig/archival/libipkg/str_util.c      1970-01-01 01:00:00.000000000 +0100
13175 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.c     2006-07-22 16:31:26.000000000 +0200
13176 @@ -0,0 +1,73 @@
13177 +/* str_utils.c - the itsy package management system
13178 +
13179 +   Carl D. Worth
13180 +
13181 +   Copyright (C) 2001 University of Southern California
13182 +
13183 +   This program is free software; you can redistribute it and/or
13184 +   modify it under the terms of the GNU General Public License as
13185 +   published by the Free Software Foundation; either version 2, or (at
13186 +   your option) any later version.
13187 +
13188 +   This program is distributed in the hope that it will be useful, but
13189 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13190 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13191 +   General Public License for more details.
13192 +*/
13193 +
13194 +#include "ipkg.h"
13195 +
13196 +int str_starts_with(const char *str, const char *prefix)
13197 +{
13198 +    return (strncmp(str, prefix, strlen(prefix)) == 0);
13199 +}
13200 +
13201 +int str_ends_with(const char *str, const char *suffix)
13202 +{
13203 +    int suffix_len;
13204 +    int str_len;
13205 +
13206 +    str_len = strlen(str);
13207 +    suffix_len = strlen(suffix);
13208 +
13209 +    if (str_len < suffix_len) {
13210 +       return 0;
13211 +    }
13212 +
13213 +    return (strcmp(str + str_len - suffix_len, suffix) == 0);
13214 +}
13215 +
13216 +int str_chomp(char *str)
13217 +{
13218 +    if (str[strlen(str) - 1] == '\n') {
13219 +       str[strlen(str) - 1] = '\0';
13220 +       return 1;
13221 +    }
13222 +    return 0;
13223 +}
13224 +
13225 +int str_tolower(char *str)
13226 +{
13227 +    while (*str) {
13228 +       *str = tolower(*str);
13229 +       str++;
13230 +    }
13231 +
13232 +    return 0;
13233 +}
13234 +
13235 +int str_toupper(char *str)
13236 +{
13237 +    while (*str) {
13238 +       *str = toupper(*str);
13239 +       str++;
13240 +    }
13241 +
13242 +    return 0;
13243 +}
13244 +
13245 +char *str_dup_safe(const char *str)
13246 +{
13247 +    return str ? strdup(str) : NULL;
13248 +}
13249 +
13250 diff -ruN busybox-1.2.0-orig/archival/libipkg/str_util.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.h
13251 --- busybox-1.2.0-orig/archival/libipkg/str_util.h      1970-01-01 01:00:00.000000000 +0100
13252 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/str_util.h     2006-07-22 16:31:26.000000000 +0200
13253 @@ -0,0 +1,28 @@
13254 +/* str_utils.h - the itsy package management system
13255 +
13256 +   Carl D. Worth
13257 +
13258 +   Copyright (C) 2001 University of Southern California
13259 +
13260 +   This program is free software; you can redistribute it and/or
13261 +   modify it under the terms of the GNU General Public License as
13262 +   published by the Free Software Foundation; either version 2, or (at
13263 +   your option) any later version.
13264 +
13265 +   This program is distributed in the hope that it will be useful, but
13266 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13267 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13268 +   General Public License for more details.
13269 +*/
13270 +
13271 +#ifndef STR_UTILS_H
13272 +#define STR_UTILS_H
13273 +
13274 +int str_starts_with(const char *str, const char *prefix);
13275 +int str_ends_with(const char *str, const char *suffix);
13276 +int str_chomp(char *str);
13277 +int str_tolower(char *str);
13278 +int str_toupper(char *str);
13279 +char *str_dup_safe(const char *str);
13280 +
13281 +#endif
13282 diff -ruN busybox-1.2.0-orig/archival/libipkg/user.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.c
13283 --- busybox-1.2.0-orig/archival/libipkg/user.c  1970-01-01 01:00:00.000000000 +0100
13284 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.c 2006-07-22 16:31:26.000000000 +0200
13285 @@ -0,0 +1,58 @@
13286 +/* user.c - the itsy package management system
13287 +
13288 +   Jamey Hicks
13289 +
13290 +   Copyright (C) 2002 Hewlett Packard Company
13291 +   Copyright (C) 2001 University of Southern California
13292 +
13293 +   This program is free software; you can redistribute it and/or
13294 +   modify it under the terms of the GNU General Public License as
13295 +   published by the Free Software Foundation; either version 2, or (at
13296 +   your option) any later version.
13297 +
13298 +   This program is distributed in the hope that it will be useful, but
13299 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13300 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13301 +   General Public License for more details.
13302 +*/
13303 +
13304 +#include <stdio.h>
13305 +#include <stdarg.h>
13306 +#include "file_util.h"
13307 +#include "str_util.h"
13308 +#ifdef IPKG_LIB        
13309 +#include "libipkg.h"
13310 +#endif 
13311 +
13312 +
13313 +#ifdef IPKG_LIB
13314 +static char *question = NULL;
13315 +static int question_len = 255;
13316 +#endif 
13317 +char *get_user_response(const char *format, ...)
13318 +{
13319 +     int len = question_len;
13320 +     va_list ap;
13321 +     char *response;
13322 +     va_start(ap, format);
13323 +
13324 +#ifndef IPKG_LIB
13325 +     vprintf(format, ap);
13326 +     do {
13327 +         response = file_read_line_alloc(stdin);
13328 +     } while (response == NULL);
13329 +#else
13330 +     do {
13331 +         if (question == NULL || len > question_len) {
13332 +              question = realloc(question, len + 1);
13333 +              question_len = len;
13334 +         }
13335 +         len = vsnprintf(question,question_len,format,ap);
13336 +     } while (len > question_len);
13337 +     response = strdup(ipkg_cb_response(question));
13338 +#endif
13339 +     str_chomp(response);
13340 +     str_tolower(response);
13341 +
13342 +     return response;
13343 +}
13344 diff -ruN busybox-1.2.0-orig/archival/libipkg/user.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.h
13345 --- busybox-1.2.0-orig/archival/libipkg/user.h  1970-01-01 01:00:00.000000000 +0100
13346 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/user.h 2006-07-22 16:31:26.000000000 +0200
13347 @@ -0,0 +1,23 @@
13348 +/* user.c - the itsy package management system
13349 +
13350 +   Jamey Hicks
13351 +
13352 +   Copyright (C) 2002 Hewlett Packard Company
13353 +   Copyright (C) 2001 University of Southern California
13354 +
13355 +   This program is free software; you can redistribute it and/or
13356 +   modify it under the terms of the GNU General Public License as
13357 +   published by the Free Software Foundation; either version 2, or (at
13358 +   your option) any later version.
13359 +
13360 +   This program is distributed in the hope that it will be useful, but
13361 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13362 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13363 +   General Public License for more details.
13364 +*/
13365 +
13366 +#include <stdio.h>
13367 +#include <stdarg.h>
13368 +
13369 +char *get_user_response(const char *format, ...);
13370 +
13371 diff -ruN busybox-1.2.0-orig/archival/libipkg/void_list.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.c
13372 --- busybox-1.2.0-orig/archival/libipkg/void_list.c     1970-01-01 01:00:00.000000000 +0100
13373 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.c    2006-07-22 16:31:26.000000000 +0200
13374 @@ -0,0 +1,194 @@
13375 +/* void_list.c - the itsy package management system
13376 +
13377 +   Carl D. Worth
13378 +
13379 +   Copyright (C) 2001 University of Southern California
13380 +
13381 +   This program is free software; you can redistribute it and/or
13382 +   modify it under the terms of the GNU General Public License as
13383 +   published by the Free Software Foundation; either version 2, or (at
13384 +   your option) any later version.
13385 +
13386 +   This program is distributed in the hope that it will be useful, but
13387 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13388 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13389 +   General Public License for more details.
13390 +*/
13391 +
13392 +#include "ipkg.h"
13393 +#include <errno.h>
13394 +
13395 +#include "void_list.h"
13396 +
13397 +int void_list_elt_init(void_list_elt_t *elt, void *data)
13398 +{
13399 +    elt->next = NULL;
13400 +    elt->data = data;
13401 +
13402 +    return 0;
13403 +}
13404 +
13405 +void void_list_elt_deinit(void_list_elt_t *elt)
13406 +{
13407 +    void_list_elt_init(elt, NULL);
13408 +}
13409 +
13410 +int void_list_init(void_list_t *list)
13411 +{
13412 +    void_list_elt_init(&list->pre_head, NULL);
13413 +    list->head = NULL;
13414 +    list->pre_head.next = list->head;
13415 +    list->tail = NULL;
13416 +
13417 +    return 0;
13418 +}
13419 +
13420 +void void_list_deinit(void_list_t *list)
13421 +{
13422 +    void_list_elt_t *elt;
13423 +
13424 +    while (list->head) {
13425 +       elt = void_list_pop(list);
13426 +       void_list_elt_deinit(elt);
13427 +       /* malloced in void_list_append */
13428 +       free(elt);
13429 +    }
13430 +}
13431 +
13432 +int void_list_append(void_list_t *list, void *data)
13433 +{
13434 +    void_list_elt_t *elt;
13435 +
13436 +    /* freed in void_list_deinit */
13437 +    elt = malloc(sizeof(void_list_elt_t));
13438 +    if (elt == NULL) {
13439 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13440 +       return ENOMEM;
13441 +    }
13442 +
13443 +    void_list_elt_init(elt, data);
13444 +
13445 +    if (list->tail) {
13446 +       list->tail->next = elt;
13447 +       list->tail = elt;
13448 +    } else {
13449 +       list->head = elt;
13450 +       list->pre_head.next = list->head;
13451 +       list->tail = elt;
13452 +    }
13453 +
13454 +    return 0;
13455 +}
13456 +
13457 +int void_list_push(void_list_t *list, void *data)
13458 +{
13459 +    void_list_elt_t *elt;
13460 +
13461 +    elt = malloc(sizeof(void_list_elt_t));
13462 +    if (elt == NULL) {
13463 +       fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13464 +       return ENOMEM;
13465 +    }
13466 +
13467 +    void_list_elt_init(elt, data);
13468 +
13469 +    elt->next = list->head;
13470 +    list->head->next = elt;
13471 +    if (list->tail == NULL) {
13472 +       list->tail = list->head;
13473 +    }
13474 +
13475 +    return 0;
13476 +}
13477 +
13478 +void_list_elt_t *void_list_pop(void_list_t *list)
13479 +{
13480 +    void_list_elt_t *elt;
13481 +
13482 +    elt = list->head;
13483 +
13484 +    if (list->head) {
13485 +       list->head = list->head->next;
13486 +       list->pre_head.next = list->head;
13487 +       if (list->head == NULL) {
13488 +           list->tail = NULL;
13489 +       }
13490 +    }
13491 +
13492 +    return elt;
13493 +}
13494 +
13495 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
13496 +{
13497 +    void_list_elt_t *prior;
13498 +    void_list_elt_t *old_elt;
13499 +    void *old_data;
13500 +
13501 +    old_elt = *iter;
13502 +    old_data = old_elt->data;
13503 +
13504 +    if (old_elt == list->head) {
13505 +       prior = &list->pre_head;
13506 +       void_list_pop(list);
13507 +    } else {
13508 +       for (prior = list->head; prior; prior = prior->next) {
13509 +           if (prior->next == old_elt) {
13510 +               break;
13511 +           }
13512 +       }
13513 +       if (prior == NULL || prior->next != old_elt) {
13514 +           fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
13515 +           return NULL;
13516 +       }
13517 +       prior->next = old_elt->next;
13518 +
13519 +       if (old_elt == list->tail) {
13520 +           list->tail = prior;
13521 +       }
13522 +    }
13523 +
13524 +    void_list_elt_deinit(old_elt);
13525 +    *iter = prior;
13526 +
13527 +    return old_data;
13528 +}
13529 +
13530 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13531 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
13532 +{
13533 +     void_list_elt_t *prior;
13534 +     void_list_elt_t *old_elt = NULL;
13535 +     void *old_data = NULL;
13536 +
13537 +     /* first element */
13538 +     if (list->head && list->head->data && (cmp(list->head->data, target_data) == 0)) {
13539 +         old_elt = list->head;
13540 +         old_data = list->head->data;
13541 +         void_list_pop(list);
13542 +     } else {
13543 +         int found = 0;
13544 +         for (prior = list->head; prior && prior->next; prior = prior->next) {
13545 +              if (prior->next->data && (cmp(prior->next->data, target_data) == 0)) {
13546 +                   old_elt = prior->next;
13547 +                   old_data = old_elt->data;
13548 +                   found = 1;
13549 +                   break;
13550 +              }
13551 +         }
13552 +         if (!found) {
13553 +              return NULL;
13554 +         }
13555 +         prior->next = old_elt->next;
13556 +
13557 +         if (old_elt == list->tail) {
13558 +              list->tail = prior;
13559 +         }
13560 +     }
13561 +     if (old_elt)
13562 +         void_list_elt_deinit(old_elt);
13563 +
13564 +     if (old_data)
13565 +         return old_data;
13566 +     else
13567 +         return NULL;
13568 +}
13569 diff -ruN busybox-1.2.0-orig/archival/libipkg/void_list.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.h
13570 --- busybox-1.2.0-orig/archival/libipkg/void_list.h     1970-01-01 01:00:00.000000000 +0100
13571 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/void_list.h    2006-07-22 16:31:26.000000000 +0200
13572 @@ -0,0 +1,59 @@
13573 +/* void_list.h - the itsy package management system
13574 +
13575 +   Carl D. Worth
13576 +
13577 +   Copyright (C) 2001 University of Southern California
13578 +
13579 +   This program is free software; you can redistribute it and/or
13580 +   modify it under the terms of the GNU General Public License as
13581 +   published by the Free Software Foundation; either version 2, or (at
13582 +   your option) any later version.
13583 +
13584 +   This program is distributed in the hope that it will be useful, but
13585 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13586 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13587 +   General Public License for more details.
13588 +*/
13589 +
13590 +#ifndef VOID_LIST_H
13591 +#define VOID_LIST_H
13592 +
13593 +typedef struct void_list_elt void_list_elt_t;
13594 +struct void_list_elt
13595 +{
13596 +    void_list_elt_t *next;
13597 +    void *data;
13598 +};
13599 +
13600 +typedef struct void_list void_list_t;
13601 +struct void_list
13602 +{
13603 +    void_list_elt_t pre_head;
13604 +    void_list_elt_t *head;
13605 +    void_list_elt_t *tail;
13606 +};
13607 +
13608 +static inline int void_list_empty(void_list_t *list)
13609 +{
13610 +     if (list->head == NULL)
13611 +         return 1;
13612 +     else
13613 +         return 0;
13614 +}
13615 +
13616 +int void_list_elt_init(void_list_elt_t *elt, void *data);
13617 +void void_list_elt_deinit(void_list_elt_t *elt);
13618 +
13619 +int void_list_init(void_list_t *list);
13620 +void void_list_deinit(void_list_t *list);
13621 +
13622 +int void_list_append(void_list_t *list, void *data);
13623 +int void_list_push(void_list_t *list, void *data);
13624 +void_list_elt_t *void_list_pop(void_list_t *list);
13625 +
13626 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter);
13627 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13628 +typedef int (*void_list_cmp_t)(const void *, const void *);
13629 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp);
13630 +
13631 +#endif
13632 diff -ruN busybox-1.2.0-orig/archival/libipkg/xsystem.c busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.c
13633 --- busybox-1.2.0-orig/archival/libipkg/xsystem.c       1970-01-01 01:00:00.000000000 +0100
13634 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.c      2006-07-22 16:31:26.000000000 +0200
13635 @@ -0,0 +1,64 @@
13636 +/* xsystem.c - system(3) with error messages
13637 +
13638 +   Carl D. Worth
13639 +
13640 +   Copyright (C) 2001 University of Southern California
13641 +
13642 +   This program is free software; you can redistribute it and/or
13643 +   modify it under the terms of the GNU General Public License as
13644 +   published by the Free Software Foundation; either version 2, or (at
13645 +   your option) any later version.
13646 +
13647 +   This program is distributed in the hope that it will be useful, but
13648 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13649 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13650 +   General Public License for more details.
13651 +*/
13652 +
13653 +#include "ipkg.h"
13654 +#include <sys/wait.h>
13655 +
13656 +#include "xsystem.h"
13657 +
13658 +/* XXX: FEATURE: I shouldn't actually use system(3) at all. I don't
13659 +   really need the /bin/sh invocation which takes resources and
13660 +   introduces security problems. I should switch all of this to a sort
13661 +   of execl() or execv() interface/implementation.
13662 +*/
13663 +
13664 +/* Like system(3), but with error messages printed if the fork fails
13665 +   or if the child process dies due to an uncaught signal. Also, the
13666 +   return value is a bit simpler:
13667 +
13668 +   -1 if there was any problem
13669 +   Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13670 +   as defined in <sys/wait.h>.
13671 +*/
13672 +int xsystem(const char *cmd)
13673 +{
13674 +    int err;
13675 +
13676 +    err = system(cmd);
13677 +
13678 +    if (err == -1) {
13679 +       fprintf(stderr, "%s: ERROR: fork failed before execution: `%s'\n",
13680 +               __FUNCTION__, cmd);
13681 +       return -1;
13682 +    }
13683 +
13684 +    if (WIFSIGNALED(err)) {
13685 +       fprintf(stderr, "%s: ERROR: Child process died due to signal %d: `%s'\n",
13686 +               __FUNCTION__, WTERMSIG(err), cmd);
13687 +       return -1;
13688 +    }
13689 +
13690 +    if (WIFEXITED(err)) {
13691 +       /* Normal child exit */
13692 +       return WEXITSTATUS(err);
13693 +    }
13694 +
13695 +    fprintf(stderr, "%s: ERROR: Received unintelligible return value from system: %d",
13696 +           __FUNCTION__, err);
13697 +    return -1;
13698 +}
13699 +        
13700 diff -ruN busybox-1.2.0-orig/archival/libipkg/xsystem.h busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.h
13701 --- busybox-1.2.0-orig/archival/libipkg/xsystem.h       1970-01-01 01:00:00.000000000 +0100
13702 +++ busybox-1.2.0+ipkg-0.99.162/archival/libipkg/xsystem.h      2006-07-22 16:31:26.000000000 +0200
13703 @@ -0,0 +1,34 @@
13704 +/* xsystem.h - system(3) with error messages
13705 +
13706 +   Carl D. Worth
13707 +
13708 +   Copyright (C) 2001 University of Southern California
13709 +
13710 +   This program is free software; you can redistribute it and/or
13711 +   modify it under the terms of the GNU General Public License as
13712 +   published by the Free Software Foundation; either version 2, or (at
13713 +   your option) any later version.
13714 +
13715 +   This program is distributed in the hope that it will be useful, but
13716 +   WITHOUT ANY WARRANTY; without even the implied warranty of
13717 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13718 +   General Public License for more details.
13719 +*/
13720 +
13721 +#ifndef XSYSTEM_H
13722 +#define XSYSTEM_H
13723 +
13724 +#include <stdlib.h>
13725 +
13726 +/* Like system(3), but with error messages printed if the fork fails
13727 +   or if the child process dies due to an uncaught signal. Also, the
13728 +   return value is a bit simpler:
13729 +
13730 +   -1 if there was any problem
13731 +   Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13732 +   as defined in <sys/wait.h>.
13733 +*/
13734 +int xsystem(const char *cmd);
13735 +
13736 +#endif
13737 +        
13738 diff -ruN busybox-1.2.0-orig/archival/libunarchive/data_extract_all.c busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/data_extract_all.c
13739 --- busybox-1.2.0-orig/archival/libunarchive/data_extract_all.c 2006-07-01 00:42:04.000000000 +0200
13740 +++ busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/data_extract_all.c        2006-07-22 16:31:26.000000000 +0200
13741 @@ -126,3 +126,17 @@
13742                 utime(file_header->name, &t);
13743         }
13744  }
13745 +
13746 +extern void data_extract_all_prefix(archive_handle_t *archive_handle)
13747 +{
13748 +       char *name_ptr = archive_handle->file_header->name;
13749 +
13750 +       name_ptr += strspn(name_ptr, "./");
13751 +       if (name_ptr[0] != '\0') {
13752 +               archive_handle->file_header->name = xmalloc(strlen(archive_handle->buffer) + 1 + strlen(name_ptr) + 1);
13753 +               strcpy(archive_handle->file_header->name, archive_handle->buffer);
13754 +               strcat(archive_handle->file_header->name, name_ptr);
13755 +               data_extract_all(archive_handle);
13756 +       }
13757 +}
13758 +
13759 diff -ruN busybox-1.2.0-orig/archival/libunarchive/Makefile.in busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/Makefile.in
13760 --- busybox-1.2.0-orig/archival/libunarchive/Makefile.in        2006-07-01 00:42:03.000000000 +0200
13761 +++ busybox-1.2.0+ipkg-0.99.162/archival/libunarchive/Makefile.in       2006-07-22 16:31:26.000000000 +0200
13762 @@ -58,6 +58,7 @@
13763  LIBUNARCHIVE-$(CONFIG_FEATURE_DEB_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o
13764  LIBUNARCHIVE-$(CONFIG_GUNZIP) += $(GUNZIP_FILES)
13765  LIBUNARCHIVE-$(CONFIG_FEATURE_GUNZIP_UNCOMPRESS) += decompress_uncompress.o
13766 +LIBUNARCHIVE-$(CONFIG_IPKG) += $(GUNZIP_FILES) get_header_tar.o get_header_tar_gz.o
13767  LIBUNARCHIVE-$(CONFIG_RPM2CPIO) += $(GUNZIP_FILES) get_header_cpio.o
13768  LIBUNARCHIVE-$(CONFIG_RPM) += $(GUNZIP_FILES) get_header_cpio.o
13769  LIBUNARCHIVE-$(CONFIG_TAR) += get_header_tar.o
13770 diff -ruN busybox-1.2.0-orig/archival/Makefile.in busybox-1.2.0+ipkg-0.99.162/archival/Makefile.in
13771 --- busybox-1.2.0-orig/archival/Makefile.in     2006-07-01 00:42:04.000000000 +0200
13772 +++ busybox-1.2.0+ipkg-0.99.162/archival/Makefile.in    2006-07-22 16:31:25.000000000 +0200
13773 @@ -20,6 +20,7 @@
13774  ARCHIVAL-$(CONFIG_DPKG_DEB)    += dpkg_deb.o
13775  ARCHIVAL-$(CONFIG_GUNZIP)      += gunzip.o
13776  ARCHIVAL-$(CONFIG_GZIP)                += gzip.o
13777 +ARCHIVAL-$(CONFIG_IPKG)                += ipkg.o
13778  ARCHIVAL-$(CONFIG_RPM2CPIO)    += rpm2cpio.o
13779  ARCHIVAL-$(CONFIG_RPM)         += rpm.o
13780  ARCHIVAL-$(CONFIG_TAR)         += tar.o
13781 diff -ruN busybox-1.2.0-orig/include/applets.h busybox-1.2.0+ipkg-0.99.162/include/applets.h
13782 --- busybox-1.2.0-orig/include/applets.h        2006-07-01 00:42:10.000000000 +0200
13783 +++ busybox-1.2.0+ipkg-0.99.162/include/applets.h       2006-07-22 16:35:35.000000000 +0200
13784 @@ -152,6 +152,7 @@
13785  USE_IPCALC(APPLET(ipcalc, _BB_DIR_BIN, _BB_SUID_NEVER))
13786  USE_IPCRM(APPLET(ipcrm, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13787  USE_IPCS(APPLET(ipcs, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13788 +USE_IPKG(APPLET(ipkg, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
13789  USE_IPLINK(APPLET(iplink, _BB_DIR_BIN, _BB_SUID_NEVER))
13790  USE_IPROUTE(APPLET(iproute, _BB_DIR_BIN, _BB_SUID_NEVER))
13791  USE_IPTUNNEL(APPLET(iptunnel, _BB_DIR_BIN, _BB_SUID_NEVER))
13792 diff -ruN busybox-1.2.0-orig/include/unarchive.h busybox-1.2.0+ipkg-0.99.162/include/unarchive.h
13793 --- busybox-1.2.0-orig/include/unarchive.h      2006-07-01 00:42:10.000000000 +0200
13794 +++ busybox-1.2.0+ipkg-0.99.162/include/unarchive.h     2006-07-22 16:31:26.000000000 +0200
13795 @@ -77,6 +77,7 @@
13796  
13797  extern void data_skip(archive_handle_t *archive_handle);
13798  extern void data_extract_all(archive_handle_t *archive_handle);
13799 +extern void data_extract_all_prefix(archive_handle_t *archive_handle);
13800  extern void data_extract_to_stdout(archive_handle_t *archive_handle);
13801  extern void data_extract_to_buffer(archive_handle_t *archive_handle);
13802  
13803 diff -ruN busybox-1.2.0-orig/include/usage.h busybox-1.2.0+ipkg-0.99.162/include/usage.h
13804 --- busybox-1.2.0-orig/include/usage.h  2006-07-01 00:42:10.000000000 +0200
13805 +++ busybox-1.2.0+ipkg-0.99.162/include/usage.h 2006-07-22 16:31:26.000000000 +0200
13806 @@ -982,6 +982,82 @@
13807         "$ ls -la /tmp/busybox*\n" \
13808         "-rw-rw-r--    1 andersen andersen   554058 Apr 14 17:49 /tmp/busybox.tar.gz\n"
13809  
13810 +#define ipkg_trivial_usage \
13811 +       "[options]... sub-command [arguments]..."
13812 +#define ipkg_full_usage \
13813 +       "ipkg is an utility to install, remove and manage .ipk packages.\n" \
13814 +       "\n" \
13815 +       "Sub-commands:\n" \
13816 +       "\nPackage Manipulation:\n" \
13817 +       "\tupdate               Update list of available packages\n" \
13818 +       "\tupgrade                      Upgrade all installed packages to latest version\n" \
13819 +       "\tinstall <pkg>                Download and install <pkg> (and dependencies)\n" \
13820 +       "\tinstall <file.ipk>   Install package <file.ipk>\n" \
13821 +       "\tconfigure [<pkg>]    Configure unpacked packages\n" \
13822 +       "\tremove <pkg|regexp>  Remove package <pkg|packages following regexp>\n" \
13823 +       "\tflag <flag> <pkg> ...        Flag package(s) <pkg>\n" \
13824 +       "\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation) \n" \
13825 +       "\n" \
13826 +       "Informational Commands:\n" \
13827 +       "\tlist                 List available packages and descriptions\n" \
13828 +       "\tlist_installed               List all and only the installed packages and description \n" \
13829 +       "\tfiles <pkg>          List all files belonging to <pkg>\n" \
13830 +       "\tsearch <file|regexp>         Search for a package providing <file>\n" \
13831 +       "\tinfo [pkg|regexp [<field>]]  Display all/some info fields for <pkg> or all\n" \
13832 +       "\tstatus [pkg|regexp [<field>]]        Display all/some status fields for <pkg> or all\n" \
13833 +       "\tdownload <pkg>               Download <pkg> to current directory.\n" \
13834 +       "\tcompare_versions <v1> <op> <v2>\n" \
13835 +       "\t                          compare versions using <= < > >= = << >>\n" \
13836 +       "\tprint_architecture      prints the architecture.\n" \
13837 +       "\tprint_installation_architecture\n" \
13838 +       "\twhatdepends [-A] [pkgname|pat]+\n" \
13839 +       "\twhatdependsrec [-A] [pkgname|pat]+\n" \
13840 +       "\twhatprovides [-A] [pkgname|pat]+\n" \
13841 +       "\twhatconflicts [-A] [pkgname|pat]+\n" \
13842 +       "\twhatreplaces [-A] [pkgname|pat]+\n" \
13843 +       "\t                        prints the installation architecture.\n" \
13844 +       "\n" \
13845 +       "\nOptions:\n" \
13846 +       "\t-A                      Query all packages with whatdepends, whatprovides, whatreplaces, whatconflicts\n" \
13847 +       "\t-V <level>               Set verbosity level to <level>. If no value is\n" \
13848 +       "\t--verbosity <level>      provided increase verbosity by one. Verbosity levels:\n" \
13849 +       "\t                         0 errors only\n" \
13850 +       "\t                         1 normal messages (default)\n" \
13851 +       "\t                         2 informative messages\n" \
13852 +       "\t                         3 debug output\n" \
13853 +       "\t-f <conf_file>               Use <conf_file> as the ipkg configuration file\n" \
13854 +       "\t-conf <conf_file>    Default configuration file location\n" \
13855 +       "                               is /etc/ipkg.conf\n" \
13856 +       "\t-d <dest_name>               Use <dest_name> as the the root directory for\n" \
13857 +       "\t-dest <dest_name>    package installation, removal, upgrading.\n" \
13858 +       "                               <dest_name> should be a defined dest name from\n" \
13859 +       "                               the configuration file, (but can also be a\n" \
13860 +       "                               directory name in a pinch).\n" \
13861 +       "\t-o <offline_root>    Use <offline_root> as the root directory for\n" \
13862 +       "\t-offline <offline_root>      offline installation of packages.\n" \
13863 +       "\t-verbose_wget                more wget messages\n" \
13864 +       "\n" \
13865 +       "Force Options (use when ipkg is too smart for its own good):\n" \
13866 +       "\t-force-depends               Make dependency checks warnings instead of errors\n" \
13867 +       "\t                             Install/remove package in spite of failed dependences\n" \
13868 +       "\t-force-defaults              Use default options for questions asked by ipkg.\n" \
13869 +       "                               (no prompts). Note that this will not prevent\n" \
13870 +       "                               package installation scripts from prompting.\n" \
13871 +       "\t-force-reinstall     Allow ipkg to reinstall a package.\n" \
13872 +       "\t-force-overwrite     Allow ipkg to overwrite files from another package during an install.\n" \
13873 +       "\t-force-downgrade     Allow ipkg to downgrade packages.\n" \
13874 +       "\t-force_space            Install even if there does not seem to be enough space.\n" \
13875 +       "\t-noaction               No action -- test only\n" \
13876 +       "\t-nodeps                 Do not follow dependences\n" \
13877 +       "\t-force-removal-of-dependent-packages\n" \
13878 +       "\t-recursive           Allow ipkg to remove package and all that depend on it.\n" \
13879 +       "\t-test                   No action -- test only\n" \
13880 +       "\t-t                   Specify tmp-dir.\n" \
13881 +       "\t--tmp-dir            Specify tmp-dir.\n" \
13882 +       "\n" \
13883 +       "\tregexp could be something like 'pkgname*' '*file*' or similar\n" \
13884 +       "\teg: ipkg info 'libstd*' or ipkg search '*libop*' or ipkg remove 'libncur*'\n"
13885 +
13886  #define halt_trivial_usage \
13887         "[-d<delay>] [-n<nosync>] [-f<force>]"
13888  #define halt_full_usage \
13889 diff -ruN busybox-1.2.0-orig/Makefile busybox-1.2.0+ipkg-0.99.162/Makefile
13890 --- busybox-1.2.0-orig/Makefile 2006-07-01 00:42:13.000000000 +0200
13891 +++ busybox-1.2.0+ipkg-0.99.162/Makefile        2006-07-22 16:31:25.000000000 +0200
13892 @@ -31,7 +31,7 @@
13893  export srctree=$(top_srcdir)
13894  vpath %/Config.in $(srctree)
13895  
13896 -DIRS:=applets archival archival/libunarchive coreutils console-tools \
13897 +DIRS:=applets archival archival/libipkg archival/libunarchive coreutils console-tools \
13898         debianutils editors findutils init miscutils modutils networking \
13899         networking/libiproute networking/udhcp procps loginutils shell \
13900         sysklogd util-linux e2fsprogs libpwdgrp coreutils/libcoreutils libbb