upgrade busybox to v1.11.1 and add current upstream fixes
[openwrt/staging/yousong.git] / package / busybox / patches / 500-ipkg.patch
1 --- a/archival/Config.in
2 +++ b/archival/Config.in
3 @@ -139,6 +139,15 @@
4 gzip is used to compress files.
5 It's probably the most widely used UNIX compression program.
6
7 +config IPKG
8 + bool "ipkg"
9 + default n
10 + select MD5SUM
11 + select WGET
12 + select DIFF
13 + help
14 + ipkg is the itsy package management system.
15 +
16 config RPM2CPIO
17 bool "rpm2cpio"
18 default n
19 --- a/archival/dpkg.c
20 +++ b/archival/dpkg.c
21 @@ -1482,6 +1482,10 @@
22 return ar_handle->sub_archive->buffer;
23 }
24
25 +/*
26 +
27 +// moved to data_extract_all.c
28 +
29 static void data_extract_all_prefix(archive_handle_t *archive_handle)
30 {
31 char *name_ptr = archive_handle->file_header->name;
32 @@ -1493,6 +1497,8 @@
33 }
34 }
35
36 +*/
37 +
38 static void unpack_package(deb_file_t *deb_file)
39 {
40 const char *package_name = name_hashtable[package_hashtable[deb_file->package]->name];
41 --- /dev/null
42 +++ b/archival/ipkg.c
43 @@ -0,0 +1,27 @@
44 +/* ipkg.c - the itsy package management system
45 +
46 + Florina Boor
47 +
48 + Copyright (C) 2003 kernel concepts
49 +
50 + This program is free software; you can redistribute it and/or
51 + modify it under the terms of the GNU General Public License as
52 + published by the Free Software Foundation; either version 2, or (at
53 + your option) any later version.
54 +
55 + This program is distributed in the hope that it will be useful, but
56 + WITHOUT ANY WARRANTY; without even the implied warranty of
57 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
58 + General Public License for more details.
59 +
60 + ipkg command line frontend using libipkg
61 +
62 +*/
63 +#include "libipkg/libipkg.h"
64 +#include "busybox.h"
65 +
66 +int ipkg_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
67 +int ipkg_main(int argc, char **argv)
68 +{
69 + return ipkg_op(argc, argv);
70 +}
71 --- a/archival/Kbuild
72 +++ b/archival/Kbuild
73 @@ -16,6 +16,7 @@
74 lib-$(CONFIG_DPKG_DEB) += dpkg_deb.o
75 lib-$(CONFIG_GUNZIP) += bbunzip.o
76 lib-$(CONFIG_GZIP) += gzip.o bbunzip.o
77 +lib-$(CONFIG_IPKG) += ipkg.o
78 lib-$(CONFIG_RPM2CPIO) += rpm2cpio.o
79 lib-$(CONFIG_RPM) += rpm.o
80 lib-$(CONFIG_TAR) += tar.o
81 --- /dev/null
82 +++ b/archival/libipkg/args.c
83 @@ -0,0 +1,242 @@
84 +/* args.c - parse command-line args
85 +
86 + Carl D. Worth
87 +
88 + Copyright 2001 University of Southern California
89 +
90 + This program is free software; you can redistribute it and/or modify
91 + it under the terms of the GNU General Public License as published by
92 + the Free Software Foundation; either version 2, or (at your option)
93 + any later version.
94 +
95 + This program is distributed in the hope that it will be useful,
96 + but WITHOUT ANY WARRANTY; without even the implied warranty of
97 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98 + GNU General Public License for more details.
99 + */
100 +
101 +#include <getopt.h>
102 +#include <stdlib.h>
103 +#include <string.h>
104 +#include <unistd.h>
105 +
106 +#include "ipkg.h"
107 +#include "ipkg_message.h"
108 +
109 +#include "args.h"
110 +#include "sprintf_alloc.h"
111 +
112 +#include "libbb.h"
113 +
114 +
115 +static void print_version(void);
116 +
117 +enum long_args_opt
118 +{
119 + ARGS_OPT_FORCE_DEFAULTS = 129,
120 + ARGS_OPT_FORCE_DEPENDS,
121 + ARGS_OPT_FORCE_OVERWRITE,
122 + ARGS_OPT_FORCE_DOWNGRADE,
123 + ARGS_OPT_FORCE_REINSTALL,
124 + ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES,
125 + ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES,
126 + ARGS_OPT_FORCE_SPACE,
127 + ARGS_OPT_NOACTION,
128 + ARGS_OPT_NODEPS,
129 + ARGS_OPT_VERBOSE_WGET,
130 + ARGS_OPT_VERBOSITY,
131 + ARGS_OPT_MULTIPLE_PROVIDERS
132 +};
133 +
134 +int args_init(args_t *args)
135 +{
136 + char *conf_file_dir;
137 +
138 + memset(args, 0, sizeof(args_t));
139 +
140 + args->dest = ARGS_DEFAULT_DEST;
141 +
142 + conf_file_dir = getenv("IPKG_CONF_DIR");
143 + if (conf_file_dir == NULL || conf_file_dir[0] == '\0') {
144 + conf_file_dir = ARGS_DEFAULT_CONF_FILE_DIR;
145 + }
146 + sprintf_alloc(&args->conf_file, "%s/%s", conf_file_dir,
147 + ARGS_DEFAULT_CONF_FILE_NAME);
148 +
149 + args->force_defaults = ARGS_DEFAULT_FORCE_DEFAULTS;
150 + args->force_depends = ARGS_DEFAULT_FORCE_DEPENDS;
151 + args->force_overwrite = ARGS_DEFAULT_FORCE_OVERWRITE;
152 + args->force_downgrade = ARGS_DEFAULT_FORCE_DOWNGRADE;
153 + args->force_reinstall = ARGS_DEFAULT_FORCE_REINSTALL;
154 + args->force_removal_of_dependent_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES;
155 + args->force_removal_of_essential_packages = ARGS_DEFAULT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES;
156 + args->noaction = ARGS_DEFAULT_NOACTION;
157 + args->nodeps = ARGS_DEFAULT_NODEPS;
158 + args->verbose_wget = ARGS_DEFAULT_VERBOSE_WGET;
159 + args->verbosity = ARGS_DEFAULT_VERBOSITY;
160 + args->offline_root = ARGS_DEFAULT_OFFLINE_ROOT;
161 + args->offline_root_pre_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_PRE_SCRIPT_CMD;
162 + args->offline_root_post_script_cmd = ARGS_DEFAULT_OFFLINE_ROOT_POST_SCRIPT_CMD;
163 + args->multiple_providers = 0;
164 + args->nocheckfordirorfile = 0;
165 + args->noreadfeedsfile = 0;
166 +
167 + return 1;
168 +}
169 +
170 +void args_deinit(args_t *args)
171 +{
172 + free(args->conf_file);
173 + args->conf_file = NULL;
174 +}
175 +
176 +int args_parse(args_t *args, int argc, char *argv[])
177 +{
178 + int c;
179 + int option_index = 0;
180 + int parse_err = 0;
181 + static struct option long_options[] = {
182 + {"query-all", 0, 0, 'A'},
183 + {"conf-file", 1, 0, 'f'},
184 + {"conf", 1, 0, 'f'},
185 + {"dest", 1, 0, 'd'},
186 + {"force-defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
187 + {"force_defaults", 0, 0, ARGS_OPT_FORCE_DEFAULTS},
188 + {"force-depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
189 + {"force_depends", 0, 0, ARGS_OPT_FORCE_DEPENDS},
190 + {"force-overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
191 + {"force_overwrite", 0, 0, ARGS_OPT_FORCE_OVERWRITE},
192 + {"force_downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
193 + {"force-downgrade", 0, 0, ARGS_OPT_FORCE_DOWNGRADE},
194 + {"force-reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
195 + {"force_reinstall", 0, 0, ARGS_OPT_FORCE_REINSTALL},
196 + {"force-space", 0, 0, ARGS_OPT_FORCE_SPACE},
197 + {"force_space", 0, 0, ARGS_OPT_FORCE_SPACE},
198 + {"recursive", 0, 0,
199 + ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
200 + {"force-removal-of-dependent-packages", 0, 0,
201 + ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
202 + {"force_removal_of_dependent_packages", 0, 0,
203 + ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES},
204 + {"force-removal-of-essential-packages", 0, 0,
205 + ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
206 + {"force_removal_of_essential_packages", 0, 0,
207 + ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES},
208 + {"multiple-providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
209 + {"multiple_providers", 0, 0, ARGS_OPT_MULTIPLE_PROVIDERS},
210 + {"noaction", 0, 0, ARGS_OPT_NOACTION},
211 + {"nodeps", 0, 0, ARGS_OPT_NODEPS},
212 + {"offline", 1, 0, 'o'},
213 + {"offline-root", 1, 0, 'o'},
214 + {"test", 0, 0, ARGS_OPT_NOACTION},
215 + {"tmp-dir", 1, 0, 't'},
216 + {"verbose-wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
217 + {"verbose_wget", 0, 0, ARGS_OPT_VERBOSE_WGET},
218 + {"verbosity", 2, 0, 'V'},
219 + {"version", 0, 0, 'v'},
220 + {0, 0, 0, 0}
221 + };
222 +
223 + while (1) {
224 + c = getopt_long_only(argc, argv, "Ad:f:no:t:vV:", long_options, &option_index);
225 + if (c == -1)
226 + break;
227 +
228 + switch (c) {
229 + case 'A':
230 + args->query_all = 1;
231 + break;
232 + case 'd':
233 + args->dest = optarg;
234 + break;
235 + case 'f':
236 + free(args->conf_file);
237 + args->conf_file = strdup(optarg);
238 + break;
239 + case 'o':
240 + args->offline_root = optarg;
241 + break;
242 + case 'n':
243 + args->noaction = 1;
244 + break;
245 + case 't':
246 + args->tmp_dir = strdup(optarg);
247 + break;
248 + case 'v':
249 + print_version();
250 + exit(0);
251 + case 'V':
252 + case ARGS_OPT_VERBOSITY:
253 + if (optarg)
254 + args->verbosity = atoi(optarg);
255 + else
256 + args->verbosity += 1;
257 + break;
258 + case ARGS_OPT_FORCE_DEFAULTS:
259 + args->force_defaults = 1;
260 + break;
261 + case ARGS_OPT_FORCE_DEPENDS:
262 + args->force_depends = 1;
263 + break;
264 + case ARGS_OPT_FORCE_OVERWRITE:
265 + args->force_overwrite = 1;
266 + break;
267 + case ARGS_OPT_FORCE_DOWNGRADE:
268 + args->force_downgrade = 1;
269 + break;
270 + case ARGS_OPT_FORCE_REINSTALL:
271 + args->force_reinstall = 1;
272 + break;
273 + case ARGS_OPT_FORCE_REMOVAL_OF_ESSENTIAL_PACKAGES:
274 + args->force_removal_of_essential_packages = 1;
275 + break;
276 + case ARGS_OPT_FORCE_REMOVAL_OF_DEPENDENT_PACKAGES:
277 + args->force_removal_of_dependent_packages = 1;
278 + break;
279 + case ARGS_OPT_FORCE_SPACE:
280 + args->force_space = 1;
281 + break;
282 + case ARGS_OPT_VERBOSE_WGET:
283 + args->verbose_wget = 1;
284 + break;
285 + case ARGS_OPT_MULTIPLE_PROVIDERS:
286 + args->multiple_providers = 1;
287 + break;
288 + case ARGS_OPT_NODEPS:
289 + args->nodeps = 1;
290 + break;
291 + case ARGS_OPT_NOACTION:
292 + args->noaction = 1;
293 + break;
294 + case ':':
295 + parse_err++;
296 + break;
297 + case '?':
298 + parse_err++;
299 + break;
300 + default:
301 + bb_error_msg("Confusion: getopt_long returned %d\n", c);
302 + }
303 + }
304 +
305 + if (parse_err) {
306 + return -parse_err;
307 + } else {
308 + return optind;
309 + }
310 +}
311 +
312 +void args_usage(char *complaint)
313 +{
314 + if (complaint) {
315 + bb_error_msg("%s\n", complaint);
316 + }
317 + print_version();
318 + bb_show_usage();
319 + exit(1);
320 +}
321 +
322 +static void print_version(void)
323 +{
324 + bb_error_msg("version %s\n", IPKG_VERSION);
325 +}
326 --- /dev/null
327 +++ b/archival/libipkg/args.h
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 --- /dev/null
402 +++ b/archival/libipkg/conffile.c
403 @@ -0,0 +1,64 @@
404 +/* conffile.c - the itsy package management system
405 +
406 + Carl D. Worth
407 +
408 + Copyright (C) 2001 University of Southern California
409 +
410 + This program is free software; you can redistribute it and/or
411 + modify it under the terms of the GNU General Public License as
412 + published by the Free Software Foundation; either version 2, or (at
413 + your option) any later version.
414 +
415 + This program is distributed in the hope that it will be useful, but
416 + WITHOUT ANY WARRANTY; without even the implied warranty of
417 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
418 + General Public License for more details.
419 +*/
420 +
421 +#include <string.h>
422 +#include <stdlib.h>
423 +
424 +#include "ipkg.h"
425 +#include "ipkg_message.h"
426 +
427 +#include "conffile.h"
428 +#include "file_util.h"
429 +#include "sprintf_alloc.h"
430 +
431 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum)
432 +{
433 + return nv_pair_init(conffile, file_name, md5sum);
434 +}
435 +
436 +void conffile_deinit(conffile_t *conffile)
437 +{
438 + nv_pair_deinit(conffile);
439 +}
440 +
441 +int conffile_has_been_modified(ipkg_conf_t *conf, conffile_t *conffile)
442 +{
443 + char *md5sum;
444 + char *filename = conffile->name;
445 + char *root_filename;
446 + int ret;
447 +
448 + if (conffile->value == NULL) {
449 + ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s has no md5sum\n", __FUNCTION__, conffile->name);
450 + return 1;
451 + }
452 +
453 + root_filename = root_filename_alloc(conf, filename);
454 +
455 + md5sum = file_md5sum_alloc(root_filename);
456 +
457 + ret = strcmp(md5sum, conffile->value);
458 + if (ret) {
459 + ipkg_message(conf, IPKG_NOTICE, "%s: conffile %s: \t\nold md5=%s \t\nnew md5=%s\n", __FUNCTION__,
460 + conffile->name, md5sum, conffile->value);
461 + }
462 +
463 + free(root_filename);
464 + free(md5sum);
465 +
466 + return ret;
467 +}
468 --- /dev/null
469 +++ b/archival/libipkg/conffile.h
470 @@ -0,0 +1,30 @@
471 +/* conffile.h - the itsy package management system
472 +
473 + Carl D. Worth
474 +
475 + Copyright (C) 2001 University of Southern California
476 +
477 + This program is free software; you can redistribute it and/or
478 + modify it under the terms of the GNU General Public License as
479 + published by the Free Software Foundation; either version 2, or (at
480 + your option) any later version.
481 +
482 + This program is distributed in the hope that it will be useful, but
483 + WITHOUT ANY WARRANTY; without even the implied warranty of
484 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
485 + General Public License for more details.
486 +*/
487 +
488 +#ifndef CONFFILE_H
489 +#define CONFFILE_H
490 +
491 +#include "nv_pair.h"
492 +
493 +typedef struct nv_pair conffile_t;
494 +
495 +int conffile_init(conffile_t *conffile, const char *file_name, const char *md5sum);
496 +void conffile_deinit(conffile_t *conffile);
497 +int conffile_has_been_modified(struct ipkg_conf *conf, conffile_t *conffile);
498 +
499 +#endif
500 +
501 --- /dev/null
502 +++ b/archival/libipkg/conffile_list.c
503 @@ -0,0 +1,47 @@
504 +/* conffile_list.c - the itsy package management system
505 +
506 + Carl D. Worth
507 +
508 + Copyright (C) 2001 University of Southern California
509 +
510 + This program is free software; you can redistribute it and/or
511 + modify it under the terms of the GNU General Public License as
512 + published by the Free Software Foundation; either version 2, or (at
513 + your option) any later version.
514 +
515 + This program is distributed in the hope that it will be useful, but
516 + WITHOUT ANY WARRANTY; without even the implied warranty of
517 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
518 + General Public License for more details.
519 +*/
520 +
521 +#include "ipkg.h"
522 +
523 +#include "conffile_list.h"
524 +
525 +int conffile_list_init(conffile_list_t *list)
526 +{
527 + return nv_pair_list_init(list);
528 +}
529 +
530 +void conffile_list_deinit(conffile_list_t *list)
531 +{
532 + nv_pair_list_deinit(list);
533 +}
534 +
535 +conffile_t *conffile_list_append(conffile_list_t *list, const char *file_name,
536 + const char *md5sum)
537 +{
538 + return nv_pair_list_append(list, file_name, md5sum);
539 +}
540 +
541 +int conffile_list_push(conffile_list_t *list, conffile_t *data)
542 +{
543 + return nv_pair_list_push(list, data);
544 +}
545 +
546 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list)
547 +{
548 + return nv_pair_list_pop(list);
549 +}
550 +
551 --- /dev/null
552 +++ b/archival/libipkg/conffile_list.h
553 @@ -0,0 +1,36 @@
554 +/* conffile_list.h - the itsy package management system
555 +
556 + Carl D. Worth
557 +
558 + Copyright (C) 2001 University of Southern California
559 +
560 + This program is free software; you can redistribute it and/or
561 + modify it under the terms of the GNU General Public License as
562 + published by the Free Software Foundation; either version 2, or (at
563 + your option) any later version.
564 +
565 + This program is distributed in the hope that it will be useful, but
566 + WITHOUT ANY WARRANTY; without even the implied warranty of
567 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
568 + General Public License for more details.
569 +*/
570 +
571 +#ifndef CONFFILE_LIST_H
572 +#define CONFFILE_LIST_H
573 +
574 +#include "conffile.h"
575 +#include "nv_pair_list.h"
576 +
577 +typedef struct nv_pair_list_elt conffile_list_elt_t;
578 +typedef struct nv_pair_list conffile_list_t;
579 +
580 +int conffile_list_init(conffile_list_t *list);
581 +void conffile_list_deinit(conffile_list_t *list);
582 +
583 +conffile_t *conffile_list_append(conffile_list_t *list, const char *name,
584 + const char *root_dir);
585 +int conffile_list_push(conffile_list_t *list, conffile_t *data);
586 +conffile_list_elt_t *conffile_list_pop(conffile_list_t *list);
587 +
588 +#endif
589 +
590 --- /dev/null
591 +++ b/archival/libipkg/file_util.c
592 @@ -0,0 +1,132 @@
593 +/* file_util.c - convenience routines for common stat operations
594 +
595 + Carl D. Worth
596 +
597 + Copyright (C) 2001 University of Southern California
598 +
599 + This program is free software; you can redistribute it and/or
600 + modify it under the terms of the GNU General Public License as
601 + published by the Free Software Foundation; either version 2, or (at
602 + your option) any later version.
603 +
604 + This program is distributed in the hope that it will be useful, but
605 + WITHOUT ANY WARRANTY; without even the implied warranty of
606 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
607 + General Public License for more details.
608 +*/
609 +
610 +#include "ipkg.h"
611 +#include <sys/types.h>
612 +#include <sys/stat.h>
613 +
614 +#include "sprintf_alloc.h"
615 +#include "file_util.h"
616 +#include "libbb.h"
617 +#undef strlen
618 +
619 +int file_exists(const char *file_name)
620 +{
621 + int err;
622 + struct stat stat_buf;
623 +
624 + err = stat(file_name, &stat_buf);
625 + if (err == 0) {
626 + return 1;
627 + } else {
628 + return 0;
629 + }
630 +}
631 +
632 +int file_is_dir(const char *file_name)
633 +{
634 + int err;
635 + struct stat stat_buf;
636 +
637 + err = stat(file_name, &stat_buf);
638 + if (err) {
639 + return 0;
640 + }
641 +
642 + return S_ISDIR(stat_buf.st_mode);
643 +}
644 +
645 +/* read a single line from a file, stopping at a newline or EOF.
646 + If a newline is read, it will appear in the resulting string.
647 + Return value is a malloc'ed char * which should be freed at
648 + some point by the caller.
649 +
650 + Return value is NULL if the file is at EOF when called.
651 +*/
652 +#define FILE_READ_LINE_BUF_SIZE 1024
653 +char *file_read_line_alloc(FILE *file)
654 +{
655 + char buf[FILE_READ_LINE_BUF_SIZE];
656 + int buf_len;
657 + char *line = NULL;
658 + int line_size = 0;
659 +
660 + memset(buf, 0, FILE_READ_LINE_BUF_SIZE);
661 + while (fgets(buf, FILE_READ_LINE_BUF_SIZE, file)) {
662 + buf_len = strlen(buf);
663 + if (line) {
664 + line_size += buf_len;
665 + line = realloc(line, line_size);
666 + if (line == NULL) {
667 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
668 + break;
669 + }
670 + strcat(line, buf);
671 + } else {
672 + line_size = buf_len + 1;
673 + line = strdup(buf);
674 + }
675 + if (buf[buf_len - 1] == '\n') {
676 + break;
677 + }
678 + }
679 +
680 + return line;
681 +}
682 +
683 +int file_move(const char *src, const char *dest)
684 +{
685 + int err;
686 +
687 + err = rename(src, dest);
688 +
689 + if (err && errno == EXDEV) {
690 + err = file_copy(src, dest);
691 + unlink(src);
692 + } else if (err) {
693 + fprintf(stderr, "%s: ERROR: failed to rename %s to %s: %s\n",
694 + __FUNCTION__, src, dest, strerror(errno));
695 + }
696 +
697 + return err;
698 +}
699 +
700 +/* I put these here to keep libbb dependencies from creeping all over
701 + the ipkg code */
702 +int file_copy(const char *src, const char *dest)
703 +{
704 + int err;
705 +
706 + err = copy_file(src, dest, FILEUTILS_FORCE | FILEUTILS_PRESERVE_STATUS);
707 + if (err) {
708 + fprintf(stderr, "%s: ERROR: failed to copy %s to %s\n",
709 + __FUNCTION__, src, dest);
710 + }
711 +
712 + return err;
713 +}
714 +
715 +int file_mkdir_hier(const char *path, long mode)
716 +{
717 + return bb_make_directory((char *)path, mode, FILEUTILS_RECUR);
718 +}
719 +
720 +char *file_md5sum_alloc(const char *file_name)
721 +{
722 + return hash_file(file_name, HASH_MD5);
723 +}
724 +
725 --- /dev/null
726 +++ b/archival/libipkg/file_util.h
727 @@ -0,0 +1,29 @@
728 +/* file_util.h - convenience routines for common file operations
729 +
730 + Carl D. Worth
731 +
732 + Copyright (C) 2001 University of Southern California
733 +
734 + This program is free software; you can redistribute it and/or
735 + modify it under the terms of the GNU General Public License as
736 + published by the Free Software Foundation; either version 2, or (at
737 + your option) any later version.
738 +
739 + This program is distributed in the hope that it will be useful, but
740 + WITHOUT ANY WARRANTY; without even the implied warranty of
741 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
742 + General Public License for more details.
743 +*/
744 +
745 +#ifndef FILE_UTIL_H
746 +#define FILE_UTIL_H
747 +
748 +int file_exists(const char *file_name);
749 +int file_is_dir(const char *file_name);
750 +char *file_read_line_alloc(FILE *file);
751 +int file_move(const char *src, const char *dest);
752 +int file_copy(const char *src, const char *dest);
753 +int file_mkdir_hier(const char *path, long mode);
754 +char *file_md5sum_alloc(const char *file_name);
755 +
756 +#endif
757 --- /dev/null
758 +++ b/archival/libipkg/hash_table.c
759 @@ -0,0 +1,155 @@
760 +/* hash.c - hash tables for ipkg
761 +
762 + Steven M. Ayer, Jamey Hicks
763 +
764 + Copyright (C) 2002 Compaq Computer Corporation
765 +
766 + This program is free software; you can redistribute it and/or
767 + modify it under the terms of the GNU General Public License as
768 + published by the Free Software Foundation; either version 2, or (at
769 + your option) any later version.
770 +
771 + This program is distributed in the hope that it will be useful, but
772 + WITHOUT ANY WARRANTY; without even the implied warranty of
773 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
774 + General Public License for more details.
775 +*/
776 +
777 +#include <errno.h>
778 +#include <stdio.h>
779 +#include <stdlib.h>
780 +#include <string.h>
781 +#include "hash_table.h"
782 +#include "ipkg_message.h"
783 +
784 +
785 +static int hash_index(hash_table_t *hash, const char *pkg_name);
786 +static int rotating(const char *key, int len, int prime);
787 +
788 +static int hash_index(hash_table_t *hash, const char *pkg_name)
789 +{
790 + return rotating(pkg_name, strlen(pkg_name), hash->n_entries);
791 +}
792 +
793 +static int rotating(const char *key, int len, int prime)
794 +{
795 + unsigned int hash, i;
796 + for (hash=len, i=0; i<len; ++i)
797 + hash = (hash<<4)^(hash>>28)^key[i];
798 + return (hash % prime);
799 +}
800 +
801 +
802 +/*
803 + * this is an open table keyed by strings
804 + */
805 +int hash_table_init(const char *name, hash_table_t *hash, int len)
806 +{
807 + static int primes_table[] = {
808 + 379, 761, 983, 1423, 2711, 3361, 3931, 4679, 5519, 6701, 9587,
809 + 19471, 23143, 33961, 46499, 49727, 99529, 0
810 + };
811 + int *picker;
812 +
813 + if (hash->entries != NULL) {
814 + /* we have been here already */
815 + return 0;
816 + }
817 +
818 + hash->name = name;
819 + hash->entries = NULL;
820 + hash->n_entries = 0;
821 + hash->hash_entry_key = NULL;
822 +
823 + picker = primes_table;
824 + while(*picker && (*picker++ < len));
825 + if(!*picker)
826 + fprintf(stderr, "%s: primes table might not be big enough (! << %d)\n", __FUNCTION__, len);
827 + --picker;
828 +
829 + hash->n_entries = *picker;
830 + hash->entries = (hash_entry_t *)calloc(hash->n_entries, sizeof(hash_entry_t));
831 + if (hash->entries == NULL) {
832 + fprintf(stderr, "%s: Out of memory.\n", __FUNCTION__);
833 + return ENOMEM;
834 + }
835 + return 0;
836 +}
837 +
838 +void hash_table_deinit(hash_table_t *hash)
839 +{
840 + free(hash->entries);
841 + hash->entries = NULL;
842 + hash->n_entries = 0;
843 +}
844 +
845 +void *hash_table_get(hash_table_t *hash, const char *key)
846 +{
847 + int ndx= hash_index(hash, key);
848 + hash_entry_t *hash_entry = hash->entries + ndx;
849 + while (hash_entry)
850 + {
851 + if (hash_entry->key)
852 + {
853 + if (strcmp(key, hash_entry->key) == 0) {
854 + // ipkg_message(NULL, IPKG_DEBUG, "Function: %s. Key found for '%s' \n", __FUNCTION__, key);
855 + return hash_entry->data;
856 + }
857 + }
858 + hash_entry = hash_entry->next;
859 + }
860 + return NULL;
861 +}
862 +
863 +int hash_table_insert(hash_table_t *hash, const char *key, void *value)
864 +{
865 + int ndx= hash_index(hash, key);
866 + hash_entry_t *hash_entry = hash->entries + ndx;
867 + if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Inserting in hash for '%s' \n", __FUNCTION__, key);
868 + if (hash_entry->key) {
869 + if (strcmp(hash_entry->key, key) == 0) {
870 + /* alread in table, update the value */
871 + if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash for '%s' \n", __FUNCTION__, key);
872 + hash_entry->data = value;
873 + return 0;
874 + } else {
875 + /*
876 + * if this is a collision, we have to go to the end of the ll,
877 + * then add a new entry
878 + * before we can hook up the value
879 + */
880 + if (0) ipkg_message(NULL, IPKG_DEBUG2, "Function: %s. Value already in hash by collision for '%s' \n", __FUNCTION__, key);
881 + while (hash_entry->next)
882 + hash_entry = hash_entry->next;
883 + hash_entry->next = (hash_entry_t *)malloc(sizeof(hash_entry_t));
884 + if (!hash_entry->next) {
885 + return -ENOMEM;
886 + }
887 + hash_entry = hash_entry->next;
888 + hash_entry->next = NULL;
889 + }
890 + }
891 + hash->n_elements++;
892 + hash_entry->key = strdup(key);
893 + hash_entry->data = value;
894 +
895 + return 0;
896 +}
897 +
898 +
899 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data)
900 +{
901 + int i;
902 + if (!hash || !f)
903 + return;
904 +
905 + for (i = 0; i < hash->n_entries; i++) {
906 + hash_entry_t *hash_entry = (hash->entries + i);
907 + do {
908 + if(hash_entry->key) {
909 + f(hash_entry->key, hash_entry->data, data);
910 + }
911 + } while((hash_entry = hash_entry->next));
912 + }
913 +}
914 +
915 --- /dev/null
916 +++ b/archival/libipkg/hash_table.h
917 @@ -0,0 +1,44 @@
918 +/* hash.h - hash tables for ipkg
919 +
920 + Steven M. Ayer, Jamey Hicks
921 +
922 + Copyright (C) 2002 Compaq Computer Corporation
923 +
924 + This program is free software; you can redistribute it and/or
925 + modify it under the terms of the GNU General Public License as
926 + published by the Free Software Foundation; either version 2, or (at
927 + your option) any later version.
928 +
929 + This program is distributed in the hope that it will be useful, but
930 + WITHOUT ANY WARRANTY; without even the implied warranty of
931 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
932 + General Public License for more details.
933 +*/
934 +
935 +#ifndef _HASH_TABLE_H_
936 +#define _HASH_TABLE_H_
937 +
938 +typedef struct hash_entry hash_entry_t;
939 +typedef struct hash_table hash_table_t;
940 +
941 +struct hash_entry {
942 + const char * key;
943 + void * data;
944 + struct hash_entry * next;
945 +};
946 +
947 +struct hash_table {
948 + const char *name;
949 + hash_entry_t * entries;
950 + int n_entries; /* number of buckets */
951 + int n_elements;
952 + const char * (*hash_entry_key)(void * data);
953 +};
954 +
955 +int hash_table_init(const char *name, hash_table_t *hash, int len);
956 +void hash_table_deinit(hash_table_t *hash);
957 +void *hash_table_get(hash_table_t *hash, const char *key);
958 +int hash_table_insert(hash_table_t *hash, const char *key, void *value);
959 +void hash_table_foreach(hash_table_t *hash, void (*f)(const char *key, void *entry, void *data), void *data);
960 +
961 +#endif /* _HASH_TABLE_H_ */
962 --- /dev/null
963 +++ b/archival/libipkg/ipkg_cmd.c
964 @@ -0,0 +1,1431 @@
965 +/* ipkg_cmd.c - the itsy package management system
966 +
967 + Carl D. Worth
968 +
969 + Copyright (C) 2001 University of Southern California
970 +
971 + This program is free software; you can redistribute it and/or
972 + modify it under the terms of the GNU General Public License as
973 + published by the Free Software Foundation; either version 2, or (at
974 + your option) any later version.
975 +
976 + This program is distributed in the hope that it will be useful, but
977 + WITHOUT ANY WARRANTY; without even the implied warranty of
978 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
979 + General Public License for more details.
980 +*/
981 +
982 +#include <string.h>
983 +
984 +#include "ipkg.h"
985 +#include <libgen.h>
986 +#include <glob.h>
987 +#include <errno.h>
988 +#include <stdlib.h>
989 +#include <unistd.h>
990 +#include <signal.h>
991 +#include <stdio.h>
992 +#include <dirent.h>
993 +
994 +#include "ipkg_conf.h"
995 +#include "ipkg_cmd.h"
996 +#include "ipkg_message.h"
997 +#include "pkg.h"
998 +#include "pkg_dest.h"
999 +#include "pkg_parse.h"
1000 +#include "sprintf_alloc.h"
1001 +#include "pkg.h"
1002 +#include "file_util.h"
1003 +#include "str_util.h"
1004 +#include "libbb.h"
1005 +#include "unarchive.h"
1006 +
1007 +#include <fnmatch.h>
1008 +
1009 +
1010 +#include "ipkg_download.h"
1011 +#include "ipkg_install.h"
1012 +#include "ipkg_upgrade.h"
1013 +#include "ipkg_remove.h"
1014 +#include "ipkg_configure.h"
1015 +#include "ipkg_message.h"
1016 +
1017 +#ifdef IPKG_LIB
1018 +#include "libipkg.h"
1019 +static void *p_userdata = NULL;
1020 +#endif
1021 +
1022 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv);
1023 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv);
1024 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv);
1025 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv);
1026 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv);
1027 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv);
1028 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv);
1029 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv);
1030 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv);
1031 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv);
1032 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv);
1033 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv);
1034 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv);
1035 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv);
1036 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1037 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1038 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv);
1039 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv);
1040 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv);
1041 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv);
1042 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv);
1043 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv);
1044 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv);
1045 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv);
1046 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv);
1047 +
1048 +/* XXX: CLEANUP: The usage strings should be incorporated into this
1049 + array for easier maintenance */
1050 +static ipkg_cmd_t cmds[] = {
1051 + {"update", 0, (ipkg_cmd_fun_t)ipkg_update_cmd},
1052 + {"upgrade", 0, (ipkg_cmd_fun_t)ipkg_upgrade_cmd},
1053 + {"list", 0, (ipkg_cmd_fun_t)ipkg_list_cmd},
1054 + {"list_installed", 0, (ipkg_cmd_fun_t)ipkg_list_installed_cmd},
1055 + {"info", 0, (ipkg_cmd_fun_t)ipkg_info_cmd},
1056 + {"flag", 1, (ipkg_cmd_fun_t)ipkg_flag_cmd},
1057 + {"status", 0, (ipkg_cmd_fun_t)ipkg_status_cmd},
1058 + {"install_pending", 0, (ipkg_cmd_fun_t)ipkg_install_pending_cmd},
1059 + {"install", 1, (ipkg_cmd_fun_t)ipkg_install_cmd},
1060 + {"remove", 1, (ipkg_cmd_fun_t)ipkg_remove_cmd},
1061 + {"purge", 1, (ipkg_cmd_fun_t)ipkg_purge_cmd},
1062 + {"configure", 0, (ipkg_cmd_fun_t)ipkg_configure_cmd},
1063 + {"files", 1, (ipkg_cmd_fun_t)ipkg_files_cmd},
1064 + {"search", 1, (ipkg_cmd_fun_t)ipkg_search_cmd},
1065 + {"download", 1, (ipkg_cmd_fun_t)ipkg_download_cmd},
1066 + {"compare_versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1067 + {"compare-versions", 1, (ipkg_cmd_fun_t)ipkg_compare_versions_cmd},
1068 + {"print-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1069 + {"print_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1070 + {"print-installation-architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1071 + {"print_installation_architecture", 0, (ipkg_cmd_fun_t)ipkg_print_architecture_cmd},
1072 + {"depends", 1, (ipkg_cmd_fun_t)ipkg_depends_cmd},
1073 + {"whatdepends", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_cmd},
1074 + {"whatdependsrec", 1, (ipkg_cmd_fun_t)ipkg_whatdepends_recursively_cmd},
1075 + {"whatrecommends", 1, (ipkg_cmd_fun_t)ipkg_whatrecommends_cmd},
1076 + {"whatsuggests", 1, (ipkg_cmd_fun_t)ipkg_whatsuggests_cmd},
1077 + {"whatprovides", 1, (ipkg_cmd_fun_t)ipkg_whatprovides_cmd},
1078 + {"whatreplaces", 1, (ipkg_cmd_fun_t)ipkg_whatreplaces_cmd},
1079 + {"whatconflicts", 1, (ipkg_cmd_fun_t)ipkg_whatconflicts_cmd},
1080 +};
1081 +
1082 +int ipkg_state_changed;
1083 +static void write_status_files_if_changed(ipkg_conf_t *conf)
1084 +{
1085 + if (ipkg_state_changed && !conf->noaction) {
1086 + ipkg_message(conf, IPKG_INFO,
1087 + " writing status file\n");
1088 + ipkg_conf_write_status_files(conf);
1089 + pkg_write_changed_filelists(conf);
1090 + } else {
1091 + ipkg_message(conf, IPKG_NOTICE, "Nothing to be done\n");
1092 + }
1093 +}
1094 +
1095 +
1096 +static int num_cmds = sizeof(cmds) / sizeof(ipkg_cmd_t);
1097 +
1098 +ipkg_cmd_t *ipkg_cmd_find(const char *name)
1099 +{
1100 + int i;
1101 + ipkg_cmd_t *cmd;
1102 +
1103 + for (i=0; i < num_cmds; i++) {
1104 + cmd = &cmds[i];
1105 + if (strcmp(name, cmd->name) == 0) {
1106 + return cmd;
1107 + }
1108 + }
1109 +
1110 + return NULL;
1111 +}
1112 +
1113 +#ifdef IPKG_LIB
1114 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv, void *userdata)
1115 +{
1116 + int result;
1117 + p_userdata = userdata;
1118 +
1119 +
1120 + result = (cmd->fun)(conf, argc, argv);
1121 + if ( result == 0 ) {
1122 + ipkg_message(conf, IPKG_NOTICE, "Done.\n");
1123 + } else {
1124 + ipkg_message(conf, IPKG_NOTICE, "An error ocurred, return value: %d.\n", result);
1125 +
1126 + }
1127 + if ( error_list ) {
1128 + reverse_error_list(&error_list);
1129 +
1130 + ipkg_message(conf, IPKG_NOTICE, "Collected errors:\n");
1131 + /* Here we print the errors collected and free the list */
1132 + while (error_list != NULL) {
1133 + ipkg_message(conf, IPKG_NOTICE, "%s",error_list->errmsg);
1134 + error_list = error_list->next;
1135 +
1136 + }
1137 + free_error_list(&error_list);
1138 +
1139 + }
1140 +
1141 + p_userdata = NULL;
1142 + return result;
1143 +}
1144 +#else
1145 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv)
1146 +{
1147 + return (cmd->fun)(conf, argc, argv);
1148 +}
1149 +#endif
1150 +
1151 +static int ipkg_update_cmd(ipkg_conf_t *conf, int argc, char **argv)
1152 +{
1153 + int err;
1154 + int failures;
1155 + char *lists_dir;
1156 + pkg_src_list_elt_t *iter;
1157 + pkg_src_t *src;
1158 +
1159 +
1160 + sprintf_alloc(&lists_dir, "%s", conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir);
1161 +
1162 + if (! file_is_dir(lists_dir)) {
1163 + if (file_exists(lists_dir)) {
1164 + ipkg_message(conf, IPKG_ERROR,
1165 + "%s: ERROR: %s exists, but is not a directory\n",
1166 + __FUNCTION__, lists_dir);
1167 + free(lists_dir);
1168 + return EINVAL;
1169 + }
1170 + err = file_mkdir_hier(lists_dir, 0755);
1171 + if (err) {
1172 + ipkg_message(conf, IPKG_ERROR,
1173 + "%s: ERROR: failed to make directory %s: %s\n",
1174 + __FUNCTION__, lists_dir, strerror(errno));
1175 + free(lists_dir);
1176 + return EINVAL;
1177 + }
1178 + }
1179 +
1180 + failures = 0;
1181 + for (iter = conf->pkg_src_list.head; iter; iter = iter->next) {
1182 + char *url, *list_file_name;
1183 +
1184 + src = iter->data;
1185 +
1186 + if (src->extra_data) /* debian style? */
1187 + sprintf_alloc(&url, "%s/%s/%s", src->value, src->extra_data,
1188 + src->gzip ? "Packages.gz" : "Packages");
1189 + else
1190 + sprintf_alloc(&url, "%s/%s", src->value, src->gzip ? "Packages.gz" : "Packages");
1191 +
1192 + sprintf_alloc(&list_file_name, "%s/%s", lists_dir, src->name);
1193 + if (src->gzip) {
1194 + char *tmp;
1195 + char *tmp_file_name;
1196 + FILE *in, *out;
1197 +
1198 + tmp = strdup ("/tmp/ipkg.XXXXXX");
1199 +
1200 + if (mkdtemp (tmp) == NULL) {
1201 + perror ("mkdtemp");
1202 + failures++;
1203 + continue;
1204 + }
1205 +
1206 + sprintf_alloc (&tmp_file_name, "%s/%s.gz", tmp, src->name);
1207 + err = ipkg_download(conf, url, tmp_file_name);
1208 + if (err == 0) {
1209 + ipkg_message (conf, IPKG_NOTICE, "Inflating %s\n", url);
1210 + in = fopen (tmp_file_name, "r");
1211 + out = fopen (list_file_name, "w");
1212 + if (in && out) {
1213 + inflate_unzip_result res;
1214 + inflate_unzip (&res, 0x8000, fileno(in), fileno(out));
1215 + } else
1216 + err = 1;
1217 + if (in)
1218 + fclose (in);
1219 + if (out)
1220 + fclose (out);
1221 + unlink (tmp_file_name);
1222 + rmdir (tmp);
1223 + free (tmp);
1224 + }
1225 + } else
1226 + err = ipkg_download(conf, url, list_file_name);
1227 + if (err) {
1228 + failures++;
1229 + } else {
1230 + ipkg_message(conf, IPKG_NOTICE,
1231 + "Updated list of available packages in %s\n",
1232 + list_file_name);
1233 + }
1234 + free(url);
1235 + free(list_file_name);
1236 + }
1237 + free(lists_dir);
1238 +
1239 +#ifdef CONFIG_CLEAR_SW_INSTALL_FLAG
1240 +#warning here
1241 + /* clear SW_INSTALL on any package where state is SS_NOT_INSTALLED.
1242 + * this is a hack to work around poor bookkeeping in old ipkg upgrade code
1243 + * -Jamey 3/1/03
1244 + */
1245 + {
1246 + int i;
1247 + int changed = 0;
1248 + pkg_vec_t *available = pkg_vec_alloc();
1249 + pkg_hash_fetch_available(&conf->pkg_hash, available);
1250 + ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL for SS_NOT_INSTALLED packages.\n");
1251 + for (i = 0; i < available->len; i++) {
1252 + pkg_t *pkg = available->pkgs[i];
1253 + if (pkg->state_want == SW_INSTALL && pkg->state_status == SS_NOT_INSTALLED) {
1254 + ipkg_message(conf, IPKG_DEBUG, "Clearing SW_INSTALL on package %s.\n", pkg->name);
1255 + pkg->state_want = SW_UNKNOWN;
1256 + changed = 1;
1257 + }
1258 + }
1259 + pkg_vec_free(available);
1260 + if (changed) {
1261 + write_status_files_if_changed(conf);
1262 + }
1263 + }
1264 +#endif
1265 +
1266 + return failures;
1267 +}
1268 +
1269 +
1270 +/* scan the args passed and cache the local filenames of the packages */
1271 +int ipkg_multiple_files_scan(ipkg_conf_t *conf, int argc, char **argv)
1272 +{
1273 + int i;
1274 + int err;
1275 +
1276 + /*
1277 + * First scan through package names/urls
1278 + * For any urls, download the packages and install in database.
1279 + * For any files, install package info in database.
1280 + */
1281 + for (i = 0; i < argc; i ++) {
1282 + char *filename = argv [i];
1283 + //char *tmp = basename (tmp);
1284 + //int tmplen = strlen (tmp);
1285 +
1286 + //if (strcmp (tmp + (tmplen - strlen (IPKG_PKG_EXTENSION)), IPKG_PKG_EXTENSION) != 0)
1287 + // continue;
1288 + //if (strcmp (tmp + (tmplen - strlen (DPKG_PKG_EXTENSION)), DPKG_PKG_EXTENSION) != 0)
1289 + // continue;
1290 +
1291 + ipkg_message(conf, IPKG_DEBUG2, "Debug mfs: %s \n",filename );
1292 +
1293 + err = ipkg_prepare_url_for_install(conf, filename, &argv[i]);
1294 + if (err)
1295 + return err;
1296 + }
1297 + return 0;
1298 +}
1299 +
1300 +struct ipkg_intercept
1301 +{
1302 + char *oldpath;
1303 + char *statedir;
1304 +};
1305 +
1306 +typedef struct ipkg_intercept *ipkg_intercept_t;
1307 +
1308 +ipkg_intercept_t ipkg_prep_intercepts(ipkg_conf_t *conf)
1309 +{
1310 + ipkg_intercept_t ctx;
1311 + char *newpath;
1312 + int gen;
1313 +
1314 + ctx = malloc (sizeof (*ctx));
1315 + ctx->oldpath = strdup (getenv ("PATH"));
1316 +
1317 + sprintf_alloc (&newpath, "%s/ipkg/intercept:%s", IPKGLIBDIR, ctx->oldpath);
1318 + setenv ("PATH", newpath, 1);
1319 + free (newpath);
1320 +
1321 + gen = 0;
1322 + retry:
1323 + sprintf_alloc (&ctx->statedir, "/tmp/ipkg-intercept-%d-%d", getpid (), gen);
1324 + if (mkdir (ctx->statedir, 0770) < 0) {
1325 + if (errno == EEXIST) {
1326 + free (ctx->statedir);
1327 + gen++;
1328 + goto retry;
1329 + }
1330 + perror (ctx->statedir);
1331 + return NULL;
1332 + }
1333 + setenv ("IPKG_INTERCEPT_DIR", ctx->statedir, 1);
1334 + return ctx;
1335 +}
1336 +
1337 +int ipkg_finalize_intercepts(ipkg_intercept_t ctx)
1338 +{
1339 + char *cmd;
1340 + DIR *dir;
1341 + int err = 0;
1342 +
1343 + setenv ("PATH", ctx->oldpath, 1);
1344 + free (ctx->oldpath);
1345 +
1346 + dir = opendir (ctx->statedir);
1347 + if (dir) {
1348 + struct dirent *de;
1349 + while (de = readdir (dir), de != NULL) {
1350 + char *path;
1351 +
1352 + if (de->d_name[0] == '.')
1353 + continue;
1354 +
1355 + sprintf_alloc (&path, "%s/%s", ctx->statedir, de->d_name);
1356 + if (access (path, X_OK) == 0) {
1357 + if (system (path)) {
1358 + err = errno;
1359 + perror (de->d_name);
1360 + }
1361 + }
1362 + free (path);
1363 + }
1364 + } else
1365 + perror (ctx->statedir);
1366 +
1367 + sprintf_alloc (&cmd, "rm -rf %s", ctx->statedir);
1368 + system (cmd);
1369 + free (cmd);
1370 +
1371 + free (ctx->statedir);
1372 + free (ctx);
1373 +
1374 + return err;
1375 +}
1376 +
1377 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name)
1378 +{
1379 + pkg_vec_t *all;
1380 + int i;
1381 + pkg_t *pkg;
1382 + ipkg_intercept_t ic;
1383 + int r, err = 0;
1384 +
1385 + ipkg_message(conf, IPKG_INFO,
1386 + "Configuring unpacked packages\n");
1387 + fflush( stdout );
1388 +
1389 + all = pkg_vec_alloc();
1390 + pkg_hash_fetch_available(&conf->pkg_hash, all);
1391 +
1392 + ic = ipkg_prep_intercepts (conf);
1393 +
1394 + for(i = 0; i < all->len; i++) {
1395 + pkg = all->pkgs[i];
1396 +
1397 + if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
1398 + continue;
1399 +
1400 + if (pkg->state_status == SS_UNPACKED) {
1401 + ipkg_message(conf, IPKG_NOTICE,
1402 + "Configuring %s\n", pkg->name);
1403 + fflush( stdout );
1404 + r = ipkg_configure(conf, pkg);
1405 + if (r == 0) {
1406 + pkg->state_status = SS_INSTALLED;
1407 + pkg->parent->state_status = SS_INSTALLED;
1408 + pkg->state_flag &= ~SF_PREFER;
1409 + } else {
1410 + if (!err)
1411 + err = r;
1412 + }
1413 + }
1414 + }
1415 +
1416 + r = ipkg_finalize_intercepts (ic);
1417 + if (r && !err)
1418 + err = r;
1419 +
1420 + pkg_vec_free(all);
1421 + return err;
1422 +}
1423 +
1424 +static void sigint_handler(int sig)
1425 +{
1426 + signal(sig, SIG_DFL);
1427 + ipkg_message(NULL, IPKG_NOTICE,
1428 + "ipkg: interrupted. writing out status database\n");
1429 + write_status_files_if_changed(global_conf);
1430 + exit(128 + sig);
1431 +}
1432 +
1433 +static int ipkg_install_cmd(ipkg_conf_t *conf, int argc, char **argv)
1434 +{
1435 + int i;
1436 + char *arg;
1437 + int err=0;
1438 +
1439 + global_conf = conf;
1440 + signal(SIGINT, sigint_handler);
1441 +
1442 + /*
1443 + * Now scan through package names and install
1444 + */
1445 + for (i=0; i < argc; i++) {
1446 + arg = argv[i];
1447 +
1448 + ipkg_message(conf, IPKG_DEBUG2, "Debug install_cmd: %s \n",arg );
1449 + err = ipkg_prepare_url_for_install(conf, arg, &argv[i]);
1450 + if (err != EINVAL && err != 0)
1451 + return err;
1452 + }
1453 + pkg_info_preinstall_check(conf);
1454 +
1455 + for (i=0; i < argc; i++) {
1456 + arg = argv[i];
1457 + if (conf->multiple_providers)
1458 + err = ipkg_install_multi_by_name(conf, arg);
1459 + else{
1460 + err = ipkg_install_by_name(conf, arg);
1461 + }
1462 + if (err == IPKG_PKG_HAS_NO_CANDIDATE) {
1463 + ipkg_message(conf, IPKG_ERROR,
1464 + "Cannot find package %s.\n"
1465 + "Check the spelling or perhaps run 'ipkg update'\n",
1466 + arg);
1467 + }
1468 + }
1469 +
1470 + /* recheck to verify that all dependences are satisfied */
1471 + if (0) ipkg_satisfy_all_dependences(conf);
1472 +
1473 + ipkg_configure_packages(conf, NULL);
1474 +
1475 + write_status_files_if_changed(conf);
1476 +
1477 + return err;
1478 +}
1479 +
1480 +static int ipkg_upgrade_cmd(ipkg_conf_t *conf, int argc, char **argv)
1481 +{
1482 + int i;
1483 + pkg_t *pkg;
1484 + int err;
1485 +
1486 + global_conf = conf;
1487 + signal(SIGINT, sigint_handler);
1488 +
1489 + if (argc) {
1490 + for (i=0; i < argc; i++) {
1491 + char *arg = argv[i];
1492 +
1493 + err = ipkg_prepare_url_for_install(conf, arg, &arg);
1494 + if (err != EINVAL && err != 0)
1495 + return err;
1496 + }
1497 + pkg_info_preinstall_check(conf);
1498 +
1499 + for (i=0; i < argc; i++) {
1500 + char *arg = argv[i];
1501 + if (conf->restrict_to_default_dest) {
1502 + pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1503 + argv[i],
1504 + conf->default_dest);
1505 + if (pkg == NULL) {
1506 + ipkg_message(conf, IPKG_NOTICE,
1507 + "Package %s not installed in %s\n",
1508 + argv[i], conf->default_dest->name);
1509 + continue;
1510 + }
1511 + } else {
1512 + pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
1513 + argv[i]);
1514 + }
1515 + if (pkg)
1516 + ipkg_upgrade_pkg(conf, pkg);
1517 + else {
1518 + ipkg_install_by_name(conf, arg);
1519 + }
1520 + }
1521 + } else {
1522 + pkg_vec_t *installed = pkg_vec_alloc();
1523 +
1524 + pkg_info_preinstall_check(conf);
1525 +
1526 + pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
1527 + for (i = 0; i < installed->len; i++) {
1528 + pkg = installed->pkgs[i];
1529 + ipkg_upgrade_pkg(conf, pkg);
1530 + }
1531 + pkg_vec_free(installed);
1532 + }
1533 +
1534 + /* recheck to verify that all dependences are satisfied */
1535 + if (0) ipkg_satisfy_all_dependences(conf);
1536 +
1537 + ipkg_configure_packages(conf, NULL);
1538 +
1539 + write_status_files_if_changed(conf);
1540 +
1541 + return 0;
1542 +}
1543 +
1544 +static int ipkg_download_cmd(ipkg_conf_t *conf, int argc, char **argv)
1545 +{
1546 + int i, err;
1547 + char *arg;
1548 + pkg_t *pkg;
1549 +
1550 + pkg_info_preinstall_check(conf);
1551 + for (i = 0; i < argc; i++) {
1552 + arg = argv[i];
1553 +
1554 + pkg = pkg_hash_fetch_best_installation_candidate_by_name(conf, arg);
1555 + if (pkg == NULL) {
1556 + ipkg_message(conf, IPKG_ERROR,
1557 + "Cannot find package %s.\n"
1558 + "Check the spelling or perhaps run 'ipkg update'\n",
1559 + arg);
1560 + continue;
1561 + }
1562 +
1563 + err = ipkg_download_pkg(conf, pkg, ".");
1564 +
1565 + if (err) {
1566 + ipkg_message(conf, IPKG_ERROR,
1567 + "Failed to download %s\n", pkg->name);
1568 + } else {
1569 + ipkg_message(conf, IPKG_NOTICE,
1570 + "Downloaded %s as %s\n",
1571 + pkg->name, pkg->local_filename);
1572 + }
1573 + }
1574 +
1575 + return 0;
1576 +}
1577 +
1578 +
1579 +static int ipkg_list_cmd(ipkg_conf_t *conf, int argc, char **argv)
1580 +{
1581 + int i ;
1582 + pkg_vec_t *available;
1583 + pkg_t *pkg;
1584 + char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1585 + char *newline;
1586 + char *pkg_name = NULL;
1587 + char *version_str;
1588 +
1589 + if (argc > 0) {
1590 + pkg_name = argv[0];
1591 + }
1592 + available = pkg_vec_alloc();
1593 + pkg_hash_fetch_available(&conf->pkg_hash, available);
1594 + for (i=0; i < available->len; i++) {
1595 + pkg = available->pkgs[i];
1596 + /* if we have package name or pattern and pkg does not match, then skip it */
1597 + if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
1598 + continue;
1599 + if (pkg->description) {
1600 + strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1601 + } else {
1602 + desc_short[0] = '\0';
1603 + }
1604 + desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1605 + newline = strchr(desc_short, '\n');
1606 + if (newline) {
1607 + *newline = '\0';
1608 + }
1609 +#ifndef IPKG_LIB
1610 + printf("%s - %s\n", pkg->name, desc_short);
1611 +#else
1612 + if (ipkg_cb_list) {
1613 + version_str = pkg_version_str_alloc(pkg);
1614 + ipkg_cb_list(pkg->name,desc_short,
1615 + version_str,
1616 + pkg->state_status,
1617 + p_userdata);
1618 + free(version_str);
1619 + }
1620 +#endif
1621 + }
1622 + pkg_vec_free(available);
1623 +
1624 + return 0;
1625 +}
1626 +
1627 +
1628 +static int ipkg_list_installed_cmd(ipkg_conf_t *conf, int argc, char **argv)
1629 +{
1630 + int i ;
1631 + pkg_vec_t *available;
1632 + pkg_t *pkg;
1633 + char desc_short[IPKG_LIST_DESCRIPTION_LENGTH];
1634 + char *newline;
1635 + char *pkg_name = NULL;
1636 + char *version_str;
1637 +
1638 + if (argc > 0) {
1639 + pkg_name = argv[0];
1640 + }
1641 + available = pkg_vec_alloc();
1642 + pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1643 + for (i=0; i < available->len; i++) {
1644 + pkg = available->pkgs[i];
1645 + /* if we have package name or pattern and pkg does not match, then skip it */
1646 + if (pkg_name && fnmatch(pkg_name, pkg->name, 0))
1647 + continue;
1648 + if (pkg->description) {
1649 + strncpy(desc_short, pkg->description, IPKG_LIST_DESCRIPTION_LENGTH);
1650 + } else {
1651 + desc_short[0] = '\0';
1652 + }
1653 + desc_short[IPKG_LIST_DESCRIPTION_LENGTH - 1] = '\0';
1654 + newline = strchr(desc_short, '\n');
1655 + if (newline) {
1656 + *newline = '\0';
1657 + }
1658 +#ifndef IPKG_LIB
1659 + printf("%s - %s\n", pkg->name, desc_short);
1660 +#else
1661 + if (ipkg_cb_list) {
1662 + version_str = pkg_version_str_alloc(pkg);
1663 + ipkg_cb_list(pkg->name,desc_short,
1664 + version_str,
1665 + pkg->state_status,
1666 + p_userdata);
1667 + free(version_str);
1668 + }
1669 +#endif
1670 + }
1671 +
1672 + return 0;
1673 +}
1674 +
1675 +static int ipkg_info_status_cmd(ipkg_conf_t *conf, int argc, char **argv, int installed_only)
1676 +{
1677 + int i;
1678 + pkg_vec_t *available;
1679 + pkg_t *pkg;
1680 + char *pkg_name = NULL;
1681 + char **pkg_fields = NULL;
1682 + int n_fields = 0;
1683 + char *buff ; // = (char *)malloc(1);
1684 +
1685 + if (argc > 0) {
1686 + pkg_name = argv[0];
1687 + }
1688 + if (argc > 1) {
1689 + pkg_fields = &argv[1];
1690 + n_fields = argc - 1;
1691 + }
1692 +
1693 + available = pkg_vec_alloc();
1694 + if (installed_only)
1695 + pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1696 + else
1697 + pkg_hash_fetch_available(&conf->pkg_hash, available);
1698 + for (i=0; i < available->len; i++) {
1699 + pkg = available->pkgs[i];
1700 + if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1701 + continue;
1702 + }
1703 +#ifndef IPKG_LIB
1704 + if (n_fields) {
1705 + for (j = 0; j < n_fields; j++)
1706 + pkg_print_field(pkg, stdout, pkg_fields[j]);
1707 + } else {
1708 + pkg_print_info(pkg, stdout);
1709 + }
1710 +#else
1711 +
1712 + buff = pkg_formatted_info(pkg);
1713 + if ( buff ) {
1714 + if (ipkg_cb_status) ipkg_cb_status(pkg->name,
1715 + pkg->state_status,
1716 + buff,
1717 + p_userdata);
1718 +/*
1719 + We should not forget that actually the pointer is allocated.
1720 + We need to free it :) ( Thanks florian for seeing the error )
1721 +*/
1722 + free(buff);
1723 + }
1724 +#endif
1725 + if (conf->verbosity > 1) {
1726 + conffile_list_elt_t *iter;
1727 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
1728 + conffile_t *cf = iter->data;
1729 + int modified = conffile_has_been_modified(conf, cf);
1730 + ipkg_message(conf, IPKG_NOTICE, "conffile=%s md5sum=%s modified=%d\n",
1731 + cf->name, cf->value, modified);
1732 + }
1733 + }
1734 + }
1735 +#ifndef IPKG_LIB
1736 + if (buff)
1737 + free(buff);
1738 +#endif
1739 + pkg_vec_free(available);
1740 +
1741 + return 0;
1742 +}
1743 +
1744 +static int ipkg_info_cmd(ipkg_conf_t *conf, int argc, char **argv)
1745 +{
1746 + return ipkg_info_status_cmd(conf, argc, argv, 0);
1747 +}
1748 +
1749 +static int ipkg_status_cmd(ipkg_conf_t *conf, int argc, char **argv)
1750 +{
1751 + return ipkg_info_status_cmd(conf, argc, argv, 1);
1752 +}
1753 +
1754 +static int ipkg_configure_cmd(ipkg_conf_t *conf, int argc, char **argv)
1755 +{
1756 +
1757 + int err;
1758 + if (argc > 0) {
1759 + char *pkg_name = NULL;
1760 +
1761 + pkg_name = argv[0];
1762 +
1763 + err = ipkg_configure_packages (conf, pkg_name);
1764 +
1765 + } else {
1766 + err = ipkg_configure_packages (conf, NULL);
1767 + }
1768 +
1769 + write_status_files_if_changed(conf);
1770 +
1771 + return err;
1772 +}
1773 +
1774 +static int ipkg_install_pending_cmd(ipkg_conf_t *conf, int argc, char **argv)
1775 +{
1776 + int i, err;
1777 + char *globpattern;
1778 + glob_t globbuf;
1779 +
1780 + sprintf_alloc(&globpattern, "%s/*" IPKG_PKG_EXTENSION, conf->pending_dir);
1781 + err = glob(globpattern, 0, NULL, &globbuf);
1782 + free(globpattern);
1783 + if (err) {
1784 + return 0;
1785 + }
1786 +
1787 + ipkg_message(conf, IPKG_NOTICE,
1788 + "The following packages in %s will now be installed.\n",
1789 + conf->pending_dir);
1790 + for (i = 0; i < globbuf.gl_pathc; i++) {
1791 + ipkg_message(conf, IPKG_NOTICE,
1792 + "%s%s", i == 0 ? "" : " ", globbuf.gl_pathv[i]);
1793 + }
1794 + ipkg_message(conf, IPKG_NOTICE, "\n");
1795 + for (i = 0; i < globbuf.gl_pathc; i++) {
1796 + err = ipkg_install_from_file(conf, globbuf.gl_pathv[i]);
1797 + if (err == 0) {
1798 + err = unlink(globbuf.gl_pathv[i]);
1799 + if (err) {
1800 + ipkg_message(conf, IPKG_ERROR,
1801 + "%s: ERROR: failed to unlink %s: %s\n",
1802 + __FUNCTION__, globbuf.gl_pathv[i], strerror(err));
1803 + return err;
1804 + }
1805 + }
1806 + }
1807 + globfree(&globbuf);
1808 +
1809 + return err;
1810 +}
1811 +
1812 +static int ipkg_remove_cmd(ipkg_conf_t *conf, int argc, char **argv)
1813 +{
1814 + int i,a,done;
1815 + pkg_t *pkg;
1816 + pkg_t *pkg_to_remove;
1817 + pkg_vec_t *available;
1818 + char *pkg_name = NULL;
1819 + global_conf = conf;
1820 + signal(SIGINT, sigint_handler);
1821 +
1822 +// ENH: Add the "no pkg removed" just in case.
1823 +
1824 + done = 0;
1825 +
1826 + available = pkg_vec_alloc();
1827 + pkg_info_preinstall_check(conf);
1828 + if ( argc > 0 ) {
1829 + pkg_hash_fetch_all_installed(&conf->pkg_hash, available);
1830 + for (i=0; i < argc; i++) {
1831 + pkg_name = malloc(strlen(argv[i])+2);
1832 + strcpy(pkg_name,argv[i]);
1833 + for (a=0; a < available->len; a++) {
1834 + pkg = available->pkgs[a];
1835 + if (pkg_name && fnmatch(pkg_name, pkg->name, 0)) {
1836 + continue;
1837 + }
1838 + if (conf->restrict_to_default_dest) {
1839 + pkg_to_remove = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1840 + pkg->name,
1841 + conf->default_dest);
1842 + } else {
1843 + pkg_to_remove = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name );
1844 + }
1845 +
1846 + if (pkg == NULL) {
1847 + ipkg_message(conf, IPKG_ERROR, "Package %s is not installed.\n", pkg->name);
1848 + continue;
1849 + }
1850 + if (pkg->state_status == SS_NOT_INSTALLED) { // Added the control, so every already removed package could be skipped
1851 + ipkg_message(conf, IPKG_ERROR, "Package seems to be %s not installed (STATUS = NOT_INSTALLED).\n", pkg->name);
1852 + continue;
1853 + }
1854 + ipkg_remove_pkg(conf, pkg_to_remove,0);
1855 + done = 1;
1856 + }
1857 + free (pkg_name);
1858 + }
1859 + pkg_vec_free(available);
1860 + } else {
1861 + pkg_vec_t *installed_pkgs = pkg_vec_alloc();
1862 + int flagged_pkg_count = 0;
1863 + int removed;
1864 +
1865 + pkg_hash_fetch_all_installed(&conf->pkg_hash, installed_pkgs);
1866 +
1867 + for (i = 0; i < installed_pkgs->len; i++) {
1868 + pkg = installed_pkgs->pkgs[i];
1869 + if (pkg->state_flag & SF_USER) {
1870 + flagged_pkg_count++;
1871 + } else {
1872 + if (!pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL))
1873 + ipkg_message(conf, IPKG_NOTICE, "Non-user leaf package: %s\n", pkg->name);
1874 + }
1875 + }
1876 + if (!flagged_pkg_count) {
1877 + ipkg_message(conf, IPKG_NOTICE, "No packages flagged as installed by user, \n"
1878 + "so refusing to uninstall unflagged non-leaf packages\n");
1879 + return 0;
1880 + }
1881 +
1882 + /* find packages not flagged SF_USER (i.e., installed to
1883 + * satisfy a dependence) and not having any dependents, and
1884 + * remove them */
1885 + do {
1886 + removed = 0;
1887 + for (i = 0; i < installed_pkgs->len; i++) {
1888 + pkg = installed_pkgs->pkgs[i];
1889 + if (!(pkg->state_flag & SF_USER)
1890 + && !pkg_has_installed_dependents(conf, pkg->parent, pkg, NULL)) {
1891 + removed++;
1892 + ipkg_message(conf, IPKG_NOTICE, "Removing non-user leaf package %s\n");
1893 + ipkg_remove_pkg(conf, pkg,0);
1894 + done = 1;
1895 + }
1896 + }
1897 + } while (removed);
1898 + pkg_vec_free(installed_pkgs);
1899 + }
1900 +
1901 + if ( done == 0 )
1902 + ipkg_message(conf, IPKG_NOTICE, "No packages removed.\n");
1903 +
1904 + write_status_files_if_changed(conf);
1905 + return 0;
1906 +}
1907 +
1908 +static int ipkg_purge_cmd(ipkg_conf_t *conf, int argc, char **argv)
1909 +{
1910 + int i;
1911 + pkg_t *pkg;
1912 +
1913 + global_conf = conf;
1914 + signal(SIGINT, sigint_handler);
1915 +
1916 + pkg_info_preinstall_check(conf);
1917 +
1918 + for (i=0; i < argc; i++) {
1919 + if (conf->restrict_to_default_dest) {
1920 + pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1921 + argv[i],
1922 + conf->default_dest);
1923 + } else {
1924 + pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1925 + }
1926 +
1927 + if (pkg == NULL) {
1928 + ipkg_message(conf, IPKG_ERROR,
1929 + "Package %s is not installed.\n", argv[i]);
1930 + continue;
1931 + }
1932 + ipkg_purge_pkg(conf, pkg);
1933 + }
1934 +
1935 + write_status_files_if_changed(conf);
1936 + return 0;
1937 +}
1938 +
1939 +static int ipkg_flag_cmd(ipkg_conf_t *conf, int argc, char **argv)
1940 +{
1941 + int i;
1942 + pkg_t *pkg;
1943 + char *flags = argv[0];
1944 +
1945 + global_conf = conf;
1946 + signal(SIGINT, sigint_handler);
1947 +
1948 + for (i=1; i < argc; i++) {
1949 + if (conf->restrict_to_default_dest) {
1950 + pkg = pkg_hash_fetch_installed_by_name_dest(&conf->pkg_hash,
1951 + argv[i],
1952 + conf->default_dest);
1953 + } else {
1954 + pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, argv[i]);
1955 + }
1956 +
1957 + if (pkg == NULL) {
1958 + ipkg_message(conf, IPKG_ERROR,
1959 + "Package %s is not installed.\n", argv[i]);
1960 + continue;
1961 + }
1962 + if (( strcmp(flags,"hold")==0)||( strcmp(flags,"noprune")==0)||
1963 + ( strcmp(flags,"user")==0)||( strcmp(flags,"ok")==0)) {
1964 + pkg->state_flag = pkg_state_flag_from_str(flags);
1965 + }
1966 +/* pb_ asked this feature 03292004 */
1967 +/* Actually I will use only this two, but this is an open for various status */
1968 + if (( strcmp(flags,"installed")==0)||( strcmp(flags,"unpacked")==0)){
1969 + pkg->state_status = pkg_state_status_from_str(flags);
1970 + }
1971 + ipkg_state_changed++;
1972 + ipkg_message(conf, IPKG_NOTICE,
1973 + "Setting flags for package %s to %s\n",
1974 + pkg->name, flags);
1975 + }
1976 +
1977 + write_status_files_if_changed(conf);
1978 + return 0;
1979 +}
1980 +
1981 +static int ipkg_files_cmd(ipkg_conf_t *conf, int argc, char **argv)
1982 +{
1983 + pkg_t *pkg;
1984 + str_list_t *installed_files;
1985 + str_list_elt_t *iter;
1986 + char *pkg_version;
1987 + size_t buff_len = 8192;
1988 + size_t used_len;
1989 + char *buff ;
1990 +
1991 + buff = (char *)malloc(buff_len);
1992 + if ( buff == NULL ) {
1993 + fprintf( stderr,"%s: Unable to allocate memory \n",__FUNCTION__);
1994 + return ENOMEM;
1995 + }
1996 +
1997 + if (argc < 1) {
1998 + return EINVAL;
1999 + }
2000 +
2001 + pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash,
2002 + argv[0]);
2003 + if (pkg == NULL) {
2004 + ipkg_message(conf, IPKG_ERROR,
2005 + "Package %s not installed.\n", argv[0]);
2006 + return 0;
2007 + }
2008 +
2009 + installed_files = pkg_get_installed_files(pkg);
2010 + pkg_version = pkg_version_str_alloc(pkg);
2011 +
2012 +#ifndef IPKG_LIB
2013 + printf("Package %s (%s) is installed on %s and has the following files:\n",
2014 + pkg->name, pkg_version, pkg->dest->name);
2015 + for (iter = installed_files->head; iter; iter = iter->next) {
2016 + puts(iter->data);
2017 + }
2018 +#else
2019 + if (buff) {
2020 + try_again:
2021 + used_len = snprintf(buff, buff_len, "Package %s (%s) is installed on %s and has the following files:\n",
2022 + pkg->name, pkg_version, pkg->dest->name) + 1;
2023 + if (used_len > buff_len) {
2024 + buff_len *= 2;
2025 + buff = realloc (buff, buff_len);
2026 + goto try_again;
2027 + }
2028 + for (iter = installed_files->head; iter; iter = iter->next) {
2029 + used_len += strlen (iter->data) + 1;
2030 + while (buff_len <= used_len) {
2031 + buff_len *= 2;
2032 + buff = realloc (buff, buff_len);
2033 + }
2034 + strncat(buff, iter->data, buff_len);
2035 + strncat(buff, "\n", buff_len);
2036 + }
2037 + if (ipkg_cb_list) ipkg_cb_list(pkg->name,
2038 + buff,
2039 + pkg_version_str_alloc(pkg),
2040 + pkg->state_status,
2041 + p_userdata);
2042 + free(buff);
2043 + }
2044 +#endif
2045 +
2046 + free(pkg_version);
2047 + pkg_free_installed_files(pkg);
2048 +
2049 + return 0;
2050 +}
2051 +
2052 +static int ipkg_depends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2053 +{
2054 +
2055 + if (argc > 0) {
2056 + pkg_vec_t *available_pkgs = pkg_vec_alloc();
2057 + const char *rel_str = "depends on";
2058 + int i;
2059 +
2060 + pkg_info_preinstall_check(conf);
2061 +
2062 + if (conf->query_all)
2063 + pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2064 + else
2065 + pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2066 + for (i = 0; i < argc; i++) {
2067 + const char *target = argv[i];
2068 + int j;
2069 +
2070 + ipkg_message(conf, IPKG_ERROR, "target=%s\n", target);
2071 +
2072 + for (j = 0; j < available_pkgs->len; j++) {
2073 + pkg_t *pkg = available_pkgs->pkgs[j];
2074 + if (fnmatch(target, pkg->name, 0) == 0) {
2075 + int k;
2076 + int count = pkg->depends_count + pkg->pre_depends_count;
2077 + ipkg_message(conf, IPKG_ERROR, "What %s (arch=%s) %s\n",
2078 + target, pkg->architecture, rel_str);
2079 + for (k = 0; k < count; k++) {
2080 + compound_depend_t *cdepend = &pkg->depends[k];
2081 + int l;
2082 + for (l = 0; l < cdepend->possibility_count; l++) {
2083 + depend_t *possibility = cdepend->possibilities[l];
2084 + ipkg_message(conf, IPKG_ERROR, " %s", possibility->pkg->name);
2085 + if (conf->verbosity > 0) {
2086 + // char *ver = abstract_pkg_version_str_alloc(possibility->pkg);
2087 + ipkg_message(conf, IPKG_NOTICE, " %s", possibility->version);
2088 + if (possibility->version) {
2089 + char *typestr = NULL;
2090 + switch (possibility->constraint) {
2091 + case NONE: typestr = "none"; break;
2092 + case EARLIER: typestr = "<"; break;
2093 + case EARLIER_EQUAL: typestr = "<="; break;
2094 + case EQUAL: typestr = "="; break;
2095 + case LATER_EQUAL: typestr = ">="; break;
2096 + case LATER: typestr = ">"; break;
2097 + }
2098 + ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2099 + }
2100 + // free(ver);
2101 + }
2102 + ipkg_message(conf, IPKG_ERROR, "\n");
2103 + }
2104 + }
2105 + }
2106 + }
2107 + }
2108 + pkg_vec_free(available_pkgs);
2109 + }
2110 + return 0;
2111 +}
2112 +
2113 +enum what_field_type {
2114 + WHATDEPENDS,
2115 + WHATCONFLICTS,
2116 + WHATPROVIDES,
2117 + WHATREPLACES,
2118 + WHATRECOMMENDS,
2119 + WHATSUGGESTS
2120 +};
2121 +
2122 +static int ipkg_what_depends_conflicts_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int recursive, int argc, char **argv)
2123 +{
2124 +
2125 + if (argc > 0) {
2126 + pkg_vec_t *available_pkgs = pkg_vec_alloc();
2127 + const char *rel_str = NULL;
2128 + int i;
2129 + int changed;
2130 +
2131 + switch (what_field_type) {
2132 + case WHATDEPENDS: rel_str = "depends on"; break;
2133 + case WHATCONFLICTS: rel_str = "conflicts with"; break;
2134 + case WHATSUGGESTS: rel_str = "suggests"; break;
2135 + case WHATRECOMMENDS: rel_str = "recommends"; break;
2136 + case WHATPROVIDES: rel_str = "provides"; break;
2137 + case WHATREPLACES: rel_str = "replaces"; break;
2138 + }
2139 +
2140 + if (conf->query_all)
2141 + pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2142 + else
2143 + pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2144 +
2145 + /* mark the root set */
2146 + pkg_vec_clear_marks(available_pkgs);
2147 + ipkg_message(conf, IPKG_NOTICE, "Root set:\n");
2148 + for (i = 0; i < argc; i++) {
2149 + const char *dependee_pattern = argv[i];
2150 + pkg_vec_mark_if_matches(available_pkgs, dependee_pattern);
2151 + }
2152 + for (i = 0; i < available_pkgs->len; i++) {
2153 + pkg_t *pkg = available_pkgs->pkgs[i];
2154 + if (pkg->state_flag & SF_MARKED) {
2155 + /* mark the parent (abstract) package */
2156 + pkg_mark_provides(pkg);
2157 + ipkg_message(conf, IPKG_NOTICE, " %s\n", pkg->name);
2158 + }
2159 + }
2160 +
2161 + ipkg_message(conf, IPKG_NOTICE, "What %s root set\n", rel_str);
2162 + do {
2163 + int j;
2164 + changed = 0;
2165 +
2166 + for (j = 0; j < available_pkgs->len; j++) {
2167 + pkg_t *pkg = available_pkgs->pkgs[j];
2168 + int k;
2169 + int count = ((what_field_type == WHATCONFLICTS)
2170 + ? pkg->conflicts_count
2171 + : pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count);
2172 + /* skip this package if it is already marked */
2173 + if (pkg->parent->state_flag & SF_MARKED) {
2174 + continue;
2175 + }
2176 + for (k = 0; k < count; k++) {
2177 + compound_depend_t *cdepend =
2178 + (what_field_type == WHATCONFLICTS) ? &pkg->conflicts[k] : &pkg->depends[k];
2179 + int l;
2180 + for (l = 0; l < cdepend->possibility_count; l++) {
2181 + depend_t *possibility = cdepend->possibilities[l];
2182 + if (possibility->pkg->state_flag & SF_MARKED) {
2183 + /* mark the depending package so we won't visit it again */
2184 + pkg->state_flag |= SF_MARKED;
2185 + pkg_mark_provides(pkg);
2186 + changed++;
2187 +
2188 + ipkg_message(conf, IPKG_NOTICE, " %s", pkg->name);
2189 + if (conf->verbosity > 0) {
2190 + char *ver = pkg_version_str_alloc(pkg);
2191 + ipkg_message(conf, IPKG_NOTICE, " %s", ver);
2192 + ipkg_message(conf, IPKG_NOTICE, "\t%s %s", rel_str, possibility->pkg->name);
2193 + if (possibility->version) {
2194 + char *typestr = NULL;
2195 + switch (possibility->constraint) {
2196 + case NONE: typestr = "none"; break;
2197 + case EARLIER: typestr = "<"; break;
2198 + case EARLIER_EQUAL: typestr = "<="; break;
2199 + case EQUAL: typestr = "="; break;
2200 + case LATER_EQUAL: typestr = ">="; break;
2201 + case LATER: typestr = ">"; break;
2202 + }
2203 + ipkg_message(conf, IPKG_NOTICE, " (%s %s)", typestr, possibility->version);
2204 + }
2205 + free(ver);
2206 + if (!pkg_dependence_satisfiable(conf, possibility))
2207 + ipkg_message(conf, IPKG_NOTICE, " unsatisfiable");
2208 + }
2209 + ipkg_message(conf, IPKG_NOTICE, "\n");
2210 + goto next_package;
2211 + }
2212 + }
2213 + }
2214 + next_package:
2215 + ;
2216 + }
2217 + } while (changed && recursive);
2218 + pkg_vec_free(available_pkgs);
2219 + }
2220 +
2221 + return 0;
2222 +}
2223 +
2224 +int pkg_mark_provides(pkg_t *pkg)
2225 +{
2226 + int provides_count = pkg->provides_count;
2227 + abstract_pkg_t **provides = pkg->provides;
2228 + int i;
2229 + pkg->parent->state_flag |= SF_MARKED;
2230 + for (i = 0; i < provides_count; i++) {
2231 + provides[i]->state_flag |= SF_MARKED;
2232 + }
2233 + return 0;
2234 +}
2235 +
2236 +static int ipkg_whatdepends_recursively_cmd(ipkg_conf_t *conf, int argc, char **argv)
2237 +{
2238 + return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 1, argc, argv);
2239 +}
2240 +static int ipkg_whatdepends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2241 +{
2242 + return ipkg_what_depends_conflicts_cmd(conf, WHATDEPENDS, 0, argc, argv);
2243 +}
2244 +
2245 +static int ipkg_whatsuggests_cmd(ipkg_conf_t *conf, int argc, char **argv)
2246 +{
2247 + return ipkg_what_depends_conflicts_cmd(conf, WHATSUGGESTS, 0, argc, argv);
2248 +}
2249 +
2250 +static int ipkg_whatrecommends_cmd(ipkg_conf_t *conf, int argc, char **argv)
2251 +{
2252 + return ipkg_what_depends_conflicts_cmd(conf, WHATRECOMMENDS, 0, argc, argv);
2253 +}
2254 +
2255 +static int ipkg_whatconflicts_cmd(ipkg_conf_t *conf, int argc, char **argv)
2256 +{
2257 + return ipkg_what_depends_conflicts_cmd(conf, WHATCONFLICTS, 0, argc, argv);
2258 +}
2259 +
2260 +static int ipkg_what_provides_replaces_cmd(ipkg_conf_t *conf, enum what_field_type what_field_type, int argc, char **argv)
2261 +{
2262 +
2263 + if (argc > 0) {
2264 + pkg_vec_t *available_pkgs = pkg_vec_alloc();
2265 + const char *rel_str = (what_field_type == WHATPROVIDES ? "provides" : "replaces");
2266 + int i;
2267 +
2268 + pkg_info_preinstall_check(conf);
2269 +
2270 + if (conf->query_all)
2271 + pkg_hash_fetch_available(&conf->pkg_hash, available_pkgs);
2272 + else
2273 + pkg_hash_fetch_all_installed(&conf->pkg_hash, available_pkgs);
2274 + for (i = 0; i < argc; i++) {
2275 + const char *target = argv[i];
2276 + int j;
2277 +
2278 + ipkg_message(conf, IPKG_ERROR, "What %s %s\n",
2279 + rel_str, target);
2280 + for (j = 0; j < available_pkgs->len; j++) {
2281 + pkg_t *pkg = available_pkgs->pkgs[j];
2282 + int k;
2283 + int count = (what_field_type == WHATPROVIDES) ? pkg->provides_count : pkg->replaces_count;
2284 + for (k = 0; k < count; k++) {
2285 + abstract_pkg_t *apkg =
2286 + ((what_field_type == WHATPROVIDES)
2287 + ? pkg->provides[k]
2288 + : pkg->replaces[k]);
2289 + if (fnmatch(target, apkg->name, 0) == 0) {
2290 + ipkg_message(conf, IPKG_ERROR, " %s", pkg->name);
2291 + if (strcmp(target, apkg->name) != 0)
2292 + ipkg_message(conf, IPKG_ERROR, "\t%s %s\n", rel_str, apkg->name);
2293 + ipkg_message(conf, IPKG_ERROR, "\n");
2294 + }
2295 + }
2296 + }
2297 + }
2298 + pkg_vec_free(available_pkgs);
2299 + }
2300 + return 0;
2301 +}
2302 +
2303 +static int ipkg_whatprovides_cmd(ipkg_conf_t *conf, int argc, char **argv)
2304 +{
2305 + return ipkg_what_provides_replaces_cmd(conf, WHATPROVIDES, argc, argv);
2306 +}
2307 +
2308 +static int ipkg_whatreplaces_cmd(ipkg_conf_t *conf, int argc, char **argv)
2309 +{
2310 + return ipkg_what_provides_replaces_cmd(conf, WHATREPLACES, argc, argv);
2311 +}
2312 +
2313 +static int ipkg_search_cmd(ipkg_conf_t *conf, int argc, char **argv)
2314 +{
2315 + int i;
2316 +
2317 + pkg_vec_t *installed;
2318 + pkg_t *pkg;
2319 + str_list_t *installed_files;
2320 + str_list_elt_t *iter;
2321 + char *installed_file;
2322 +
2323 + if (argc < 1) {
2324 + return EINVAL;
2325 + }
2326 +
2327 + installed = pkg_vec_alloc();
2328 + pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
2329 +
2330 + for (i=0; i < installed->len; i++) {
2331 + pkg = installed->pkgs[i];
2332 +
2333 + installed_files = pkg_get_installed_files(pkg);
2334 +
2335 + for (iter = installed_files->head; iter; iter = iter->next) {
2336 + installed_file = iter->data;
2337 + if (fnmatch(argv[0], installed_file, 0)==0) {
2338 +#ifndef IPKG_LIB
2339 + printf("%s: %s\n", pkg->name, installed_file);
2340 +#else
2341 + if (ipkg_cb_list) ipkg_cb_list(pkg->name,
2342 + installed_file,
2343 + pkg_version_str_alloc(pkg),
2344 + pkg->state_status, p_userdata);
2345 +#endif
2346 + }
2347 + }
2348 +
2349 + pkg_free_installed_files(pkg);
2350 + }
2351 +
2352 + /* XXX: CLEANUP: It's not obvious from the name of
2353 + pkg_hash_fetch_all_installed that we need to call
2354 + pkg_vec_free to avoid a memory leak. */
2355 + pkg_vec_free(installed);
2356 +
2357 + return 0;
2358 +}
2359 +
2360 +static int ipkg_compare_versions_cmd(ipkg_conf_t *conf, int argc, char **argv)
2361 +{
2362 + if (argc == 3) {
2363 + /* this is a bit gross */
2364 + struct pkg p1, p2;
2365 + parseVersion(&p1, argv[0]);
2366 + parseVersion(&p2, argv[2]);
2367 + return pkg_version_satisfied(&p1, &p2, argv[1]);
2368 + } else {
2369 + ipkg_message(conf, IPKG_ERROR,
2370 + "ipkg compare_versions <v1> <op> <v2>\n"
2371 + "<op> is one of <= >= << >> =\n");
2372 + return -1;
2373 + }
2374 +}
2375 +
2376 +#ifndef HOST_CPU_STR
2377 +#define HOST_CPU_STR__(X) #X
2378 +#define HOST_CPU_STR_(X) HOST_CPU_STR__(X)
2379 +#define HOST_CPU_STR HOST_CPU_STR_(HOST_CPU_FOO)
2380 +#endif
2381 +
2382 +static int ipkg_print_architecture_cmd(ipkg_conf_t *conf, int argc, char **argv)
2383 +{
2384 + nv_pair_list_elt_t *l;
2385 +
2386 + l = conf->arch_list.head;
2387 + while (l) {
2388 + nv_pair_t *nv = l->data;
2389 + printf("arch %s %s\n", nv->name, nv->value);
2390 + l = l->next;
2391 + }
2392 + return 0;
2393 +}
2394 +
2395 +
2396 --- /dev/null
2397 +++ b/archival/libipkg/ipkg_cmd.h
2398 @@ -0,0 +1,46 @@
2399 +/* ipkg_cmd.h - the itsy package management system
2400 +
2401 + Carl D. Worth
2402 +
2403 + Copyright (C) 2001 University of Southern California
2404 +
2405 + This program is free software; you can redistribute it and/or
2406 + modify it under the terms of the GNU General Public License as
2407 + published by the Free Software Foundation; either version 2, or (at
2408 + your option) any later version.
2409 +
2410 + This program is distributed in the hope that it will be useful, but
2411 + WITHOUT ANY WARRANTY; without even the implied warranty of
2412 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2413 + General Public License for more details.
2414 +*/
2415 +
2416 +#ifndef IPKG_CMD_H
2417 +#define IPKG_CMD_H
2418 +
2419 +typedef int (*ipkg_cmd_fun_t)(ipkg_conf_t *conf, int argc, const char **argv);
2420 +
2421 +struct ipkg_cmd
2422 +{
2423 + char *name;
2424 + int requires_args;
2425 + ipkg_cmd_fun_t fun;
2426 +};
2427 +typedef struct ipkg_cmd ipkg_cmd_t;
2428 +
2429 +ipkg_cmd_t *ipkg_cmd_find(const char *name);
2430 +#ifdef IPKG_LIB
2431 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc,
2432 + const char **argv, void *userdata);
2433 +#else
2434 +int ipkg_cmd_exec(ipkg_cmd_t *cmd, ipkg_conf_t *conf, int argc, const char **argv);
2435 +#endif
2436 +int ipkg_multiple_files_scan (ipkg_conf_t *conf, int argc, char *argv[]);
2437 +/* install any packges with state_want == SW_INSTALL */
2438 +int ipkg_install_wanted_packages(ipkg_conf_t *conf);
2439 +/* ensure that all dependences are satisfied */
2440 +int ipkg_configure_packages(ipkg_conf_t *conf, char *pkg_name);
2441 +
2442 +int pkg_mark_provides(pkg_t *pkg);
2443 +
2444 +#endif
2445 --- /dev/null
2446 +++ b/archival/libipkg/ipkg_conf.c
2447 @@ -0,0 +1,711 @@
2448 +/* ipkg_conf.c - the itsy package management system
2449 +
2450 + Carl D. Worth
2451 +
2452 + Copyright (C) 2001 University of Southern California
2453 +
2454 + This program is free software; you can redistribute it and/or
2455 + modify it under the terms of the GNU General Public License as
2456 + published by the Free Software Foundation; either version 2, or (at
2457 + your option) any later version.
2458 +
2459 + This program is distributed in the hope that it will be useful, but
2460 + WITHOUT ANY WARRANTY; without even the implied warranty of
2461 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2462 + General Public License for more details.
2463 +*/
2464 +
2465 +#include <glob.h>
2466 +
2467 +#include "ipkg.h"
2468 +#include "ipkg_conf.h"
2469 +
2470 +#include "xregex.h"
2471 +#include "sprintf_alloc.h"
2472 +#include "ipkg_conf.h"
2473 +#include "ipkg_message.h"
2474 +#include "file_util.h"
2475 +#include "str_util.h"
2476 +#include "xsystem.h"
2477 +
2478 +
2479 +ipkg_conf_t *global_conf;
2480 +
2481 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2482 + pkg_src_list_t *pkg_src_list,
2483 + nv_pair_list_t *tmp_dest_nv_pair_list,
2484 + char **tmp_lists_dir);
2485 +static int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options);
2486 +static int ipkg_conf_set_option(const ipkg_option_t *options,
2487 + const char *name, const char *value);
2488 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2489 + const char *default_dest_name);
2490 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf,
2491 + pkg_src_list_t *nv_pair_list);
2492 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf,
2493 + nv_pair_list_t *nv_pair_list, char * lists_dir);
2494 +
2495 +int ipkg_init_options_array(const ipkg_conf_t *conf, ipkg_option_t **options)
2496 +{
2497 + ipkg_option_t tmp[] = {
2498 + { "force_defaults", IPKG_OPT_TYPE_BOOL, &conf->force_defaults },
2499 + { "force_depends", IPKG_OPT_TYPE_BOOL, &conf->force_depends },
2500 + { "force_overwrite", IPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
2501 + { "force_downgrade", IPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
2502 + { "force_reinstall", IPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
2503 + { "force_space", IPKG_OPT_TYPE_BOOL, &conf->force_space },
2504 + { "ftp_proxy", IPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
2505 + { "http_proxy", IPKG_OPT_TYPE_STRING, &conf->http_proxy },
2506 + { "multiple_providers", IPKG_OPT_TYPE_BOOL, &conf->multiple_providers },
2507 + { "no_proxy", IPKG_OPT_TYPE_STRING, &conf->no_proxy },
2508 + { "test", IPKG_OPT_TYPE_INT, &conf->noaction },
2509 + { "noaction", IPKG_OPT_TYPE_INT, &conf->noaction },
2510 + { "nodeps", IPKG_OPT_TYPE_BOOL, &conf->nodeps },
2511 + { "offline_root", IPKG_OPT_TYPE_STRING, &conf->offline_root },
2512 + { "offline_root_post_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
2513 + { "offline_root_pre_script_cmd", IPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
2514 + { "proxy_passwd", IPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
2515 + { "proxy_user", IPKG_OPT_TYPE_STRING, &conf->proxy_user },
2516 + { "query-all", IPKG_OPT_TYPE_BOOL, &conf->query_all },
2517 + { "verbose-wget", IPKG_OPT_TYPE_BOOL, &conf->verbose_wget },
2518 + { "verbosity", IPKG_OPT_TYPE_BOOL, &conf->verbosity },
2519 + { NULL }
2520 + };
2521 +
2522 + *options = (ipkg_option_t *)malloc(sizeof(tmp));
2523 + if ( options == NULL ){
2524 + fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
2525 + return -1;
2526 + }
2527 +
2528 + memcpy(*options, tmp, sizeof(tmp));
2529 + return 0;
2530 +};
2531 +
2532 +static void ipkg_conf_override_string(char **conf_str, char *arg_str)
2533 +{
2534 + if (arg_str) {
2535 + if (*conf_str) {
2536 + free(*conf_str);
2537 + }
2538 + *conf_str = strdup(arg_str);
2539 + }
2540 +}
2541 +
2542 +static void ipkg_conf_free_string(char **conf_str)
2543 +{
2544 + if (*conf_str) {
2545 + free(*conf_str);
2546 + *conf_str = NULL;
2547 + }
2548 +}
2549 +
2550 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args)
2551 +{
2552 + int err;
2553 + char *tmp_dir_base;
2554 + nv_pair_list_t tmp_dest_nv_pair_list;
2555 + char * lists_dir =NULL;
2556 + glob_t globbuf;
2557 + char *etc_ipkg_conf_pattern = "/etc/ipkg/*.conf";
2558 + char *pending_dir =NULL;
2559 +
2560 + memset(conf, 0, sizeof(ipkg_conf_t));
2561 +
2562 + pkg_src_list_init(&conf->pkg_src_list);
2563 +
2564 + nv_pair_list_init(&tmp_dest_nv_pair_list);
2565 + pkg_dest_list_init(&conf->pkg_dest_list);
2566 +
2567 + nv_pair_list_init(&conf->arch_list);
2568 +
2569 + conf->restrict_to_default_dest = 0;
2570 + conf->default_dest = NULL;
2571 +
2572 +
2573 + if (args->tmp_dir)
2574 + tmp_dir_base = args->tmp_dir;
2575 + else
2576 + tmp_dir_base = getenv("TMPDIR");
2577 + sprintf_alloc(&conf->tmp_dir, "%s/%s",
2578 + tmp_dir_base ? tmp_dir_base : IPKG_CONF_DEFAULT_TMP_DIR_BASE,
2579 + IPKG_CONF_TMP_DIR_SUFFIX);
2580 + conf->tmp_dir = mkdtemp(conf->tmp_dir);
2581 + if (conf->tmp_dir == NULL) {
2582 + fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
2583 + __FUNCTION__, conf->tmp_dir, strerror(errno));
2584 + return errno;
2585 + }
2586 +
2587 + conf->force_depends = 0;
2588 + conf->force_defaults = 0;
2589 + conf->force_overwrite = 0;
2590 + conf->force_downgrade = 0;
2591 + conf->force_reinstall = 0;
2592 + conf->force_space = 0;
2593 + conf->force_removal_of_essential_packages = 0;
2594 + conf->force_removal_of_dependent_packages = 0;
2595 + conf->nodeps = 0;
2596 + conf->verbose_wget = 0;
2597 + conf->offline_root = NULL;
2598 + conf->offline_root_pre_script_cmd = NULL;
2599 + conf->offline_root_post_script_cmd = NULL;
2600 + conf->multiple_providers = 0;
2601 + conf->verbosity = 1;
2602 + conf->noaction = 0;
2603 +
2604 + conf->http_proxy = NULL;
2605 + conf->ftp_proxy = NULL;
2606 + conf->no_proxy = NULL;
2607 + conf->proxy_user = NULL;
2608 + conf->proxy_passwd = NULL;
2609 +
2610 + pkg_hash_init("pkg-hash", &conf->pkg_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2611 + hash_table_init("file-hash", &conf->file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2612 + hash_table_init("obs-file-hash", &conf->obs_file_hash, IPKG_CONF_DEFAULT_HASH_LEN);
2613 + lists_dir=(char *)malloc(1);
2614 + lists_dir[0]='\0';
2615 + if (args->conf_file) {
2616 + struct stat stat_buf;
2617 + err = stat(args->conf_file, &stat_buf);
2618 + if (err == 0)
2619 + if (ipkg_conf_parse_file(conf, args->conf_file,
2620 + &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2621 + /* Memory leakage from ipkg_conf_parse-file */
2622 + return -1;
2623 + }
2624 +
2625 + }
2626 +
2627 + /* if (!lists_dir ){*/
2628 + if (strlen(lists_dir)<=1 ){
2629 + lists_dir = realloc(lists_dir,strlen(IPKG_CONF_LISTS_DIR)+2);
2630 + sprintf (lists_dir,"%s",IPKG_CONF_LISTS_DIR);
2631 + }
2632 +
2633 + if (args->offline_root) {
2634 + char *tmp = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
2635 + sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
2636 + free(lists_dir);
2637 + lists_dir = tmp;
2638 + }
2639 +
2640 + pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
2641 + snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
2642 +
2643 + conf->lists_dir = strdup(lists_dir);
2644 + conf->pending_dir = strdup(pending_dir);
2645 +
2646 + if (args->offline_root)
2647 + sprintf_alloc(&etc_ipkg_conf_pattern, "%s/etc/ipkg/*.conf", args->offline_root);
2648 + memset(&globbuf, 0, sizeof(globbuf));
2649 + err = glob(etc_ipkg_conf_pattern, 0, NULL, &globbuf);
2650 + if (!err) {
2651 + int i;
2652 + for (i = 0; i < globbuf.gl_pathc; i++) {
2653 + if (globbuf.gl_pathv[i])
2654 + if ( ipkg_conf_parse_file(conf, globbuf.gl_pathv[i],
2655 + &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
2656 + /* Memory leakage from ipkg_conf_parse-file */
2657 + return -1;
2658 + }
2659 + }
2660 + }
2661 + globfree(&globbuf);
2662 +
2663 + /* if no architectures were defined, then default all, noarch, and host architecture */
2664 + if (nv_pair_list_empty(&conf->arch_list)) {
2665 + nv_pair_list_append(&conf->arch_list, "all", "1");
2666 + nv_pair_list_append(&conf->arch_list, "noarch", "1");
2667 + nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
2668 + }
2669 +
2670 + /* Even if there is no conf file, we'll need at least one dest. */
2671 + if (tmp_dest_nv_pair_list.head == NULL) {
2672 + nv_pair_list_append(&tmp_dest_nv_pair_list,
2673 + IPKG_CONF_DEFAULT_DEST_NAME,
2674 + IPKG_CONF_DEFAULT_DEST_ROOT_DIR);
2675 + }
2676 +
2677 + /* After parsing the file, set options from command-line, (so that
2678 + command-line arguments take precedence) */
2679 + /* XXX: CLEANUP: The interaction between args.c and ipkg_conf.c
2680 + really needs to be cleaned up. There is so much duplication
2681 + right now it is ridiculous. Maybe ipkg_conf_t should just save
2682 + a pointer to args_t (which could then not be freed), rather
2683 + than duplicating every field here? */
2684 + if (args->force_depends) {
2685 + conf->force_depends = 1;
2686 + }
2687 + if (args->force_defaults) {
2688 + conf->force_defaults = 1;
2689 + }
2690 + if (args->force_overwrite) {
2691 + conf->force_overwrite = 1;
2692 + }
2693 + if (args->force_downgrade) {
2694 + conf->force_downgrade = 1;
2695 + }
2696 + if (args->force_reinstall) {
2697 + conf->force_reinstall = 1;
2698 + }
2699 + if (args->force_removal_of_dependent_packages) {
2700 + conf->force_removal_of_dependent_packages = 1;
2701 + }
2702 + if (args->force_removal_of_essential_packages) {
2703 + conf->force_removal_of_essential_packages = 1;
2704 + }
2705 + if (args->nodeps) {
2706 + conf->nodeps = 1;
2707 + }
2708 + if (args->noaction) {
2709 + conf->noaction = 1;
2710 + }
2711 + if (args->query_all) {
2712 + conf->query_all = 1;
2713 + }
2714 + if (args->verbose_wget) {
2715 + conf->verbose_wget = 1;
2716 + }
2717 + if (args->multiple_providers) {
2718 + conf->multiple_providers = 1;
2719 + }
2720 + if (args->verbosity != conf->verbosity) {
2721 + conf->verbosity = args->verbosity;
2722 + }
2723 +
2724 + ipkg_conf_override_string(&conf->offline_root,
2725 + args->offline_root);
2726 + ipkg_conf_override_string(&conf->offline_root_pre_script_cmd,
2727 + args->offline_root_pre_script_cmd);
2728 + ipkg_conf_override_string(&conf->offline_root_post_script_cmd,
2729 + args->offline_root_post_script_cmd);
2730 +
2731 +/* Pigi: added a flag to disable the checking of structures if the command does not need to
2732 + read anything from there.
2733 +*/
2734 + if ( !(args->nocheckfordirorfile)){
2735 + /* need to run load the source list before dest list -Jamey */
2736 + if ( !(args->noreadfeedsfile))
2737 + set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
2738 +
2739 + /* Now that we have resolved conf->offline_root, we can commit to
2740 + the directory names for the dests and load in all the package
2741 + lists. */
2742 + set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
2743 +
2744 + if (args->dest) {
2745 + err = ipkg_conf_set_default_dest(conf, args->dest);
2746 + if (err) {
2747 + return err;
2748 + }
2749 + }
2750 + }
2751 + nv_pair_list_deinit(&tmp_dest_nv_pair_list);
2752 + free(lists_dir);
2753 + free(pending_dir);
2754 +
2755 + return 0;
2756 +}
2757 +
2758 +void ipkg_conf_deinit(ipkg_conf_t *conf)
2759 +{
2760 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
2761 +#error
2762 + fprintf(stderr, "%s: Not cleaning up %s since ipkg compiled "
2763 + "with IPKG_DEBUG_NO_TMP_CLEANUP\n",
2764 + __FUNCTION__, conf->tmp_dir);
2765 +#else
2766 + int err;
2767 +
2768 + err = rmdir(conf->tmp_dir);
2769 + if (err) {
2770 + if (errno == ENOTEMPTY) {
2771 + char *cmd;
2772 + sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
2773 + err = xsystem(cmd);
2774 + free(cmd);
2775 + }
2776 + if (err)
2777 + fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
2778 + }
2779 +#endif /* IPKG_DEBUG_NO_TMP_CLEANUP */
2780 +
2781 + free(conf->tmp_dir); /*XXX*/
2782 +
2783 + pkg_src_list_deinit(&conf->pkg_src_list);
2784 + pkg_dest_list_deinit(&conf->pkg_dest_list);
2785 + nv_pair_list_deinit(&conf->arch_list);
2786 + if (&conf->pkg_hash)
2787 + pkg_hash_deinit(&conf->pkg_hash);
2788 + if (&conf->file_hash)
2789 + hash_table_deinit(&conf->file_hash);
2790 + if (&conf->obs_file_hash)
2791 + hash_table_deinit(&conf->obs_file_hash);
2792 +
2793 + ipkg_conf_free_string(&conf->offline_root);
2794 + ipkg_conf_free_string(&conf->offline_root_pre_script_cmd);
2795 + ipkg_conf_free_string(&conf->offline_root_post_script_cmd);
2796 +
2797 + if (conf->verbosity > 1) {
2798 + int i;
2799 + hash_table_t *hashes[] = {
2800 + &conf->pkg_hash,
2801 + &conf->file_hash,
2802 + &conf->obs_file_hash };
2803 + for (i = 0; i < 3; i++) {
2804 + hash_table_t *hash = hashes[i];
2805 + int c = 0;
2806 + int n_conflicts = 0;
2807 + int j;
2808 + for (j = 0; j < hash->n_entries; j++) {
2809 + int len = 0;
2810 + hash_entry_t *e = &hash->entries[j];
2811 + if (e->next)
2812 + n_conflicts++;
2813 + while (e && e->key) {
2814 + len++;
2815 + e = e->next;
2816 + }
2817 + if (len > c)
2818 + c = len;
2819 + }
2820 + ipkg_message(conf, IPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n",
2821 + hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
2822 + hash_table_deinit(hash);
2823 + }
2824 + }
2825 +}
2826 +
2827 +static int ipkg_conf_set_default_dest(ipkg_conf_t *conf,
2828 + const char *default_dest_name)
2829 +{
2830 + pkg_dest_list_elt_t *iter;
2831 + pkg_dest_t *dest;
2832 +
2833 + for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
2834 + dest = iter->data;
2835 + if (strcmp(dest->name, default_dest_name) == 0) {
2836 + conf->default_dest = dest;
2837 + conf->restrict_to_default_dest = 1;
2838 + return 0;
2839 + }
2840 + }
2841 +
2842 + fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
2843 +
2844 + return 1;
2845 +}
2846 +
2847 +static int set_and_load_pkg_src_list(ipkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
2848 +{
2849 + pkg_src_list_elt_t *iter;
2850 + pkg_src_t *src;
2851 + char *list_file;
2852 +
2853 + for (iter = pkg_src_list->head; iter; iter = iter->next) {
2854 + src = iter->data;
2855 + if (src == NULL) {
2856 + continue;
2857 + }
2858 +
2859 + sprintf_alloc(&list_file, "%s/%s",
2860 + conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir,
2861 + src->name);
2862 +
2863 + if (file_exists(list_file)) {
2864 + pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
2865 + }
2866 + free(list_file);
2867 + }
2868 +
2869 + return 0;
2870 +}
2871 +
2872 +static int set_and_load_pkg_dest_list(ipkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
2873 +{
2874 + nv_pair_list_elt_t *iter;
2875 + nv_pair_t *nv_pair;
2876 + pkg_dest_t *dest;
2877 + char *root_dir;
2878 +
2879 + for (iter = nv_pair_list->head; iter; iter = iter->next) {
2880 + nv_pair = iter->data;
2881 +
2882 + if (conf->offline_root) {
2883 + sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
2884 + } else {
2885 + root_dir = strdup(nv_pair->value);
2886 + }
2887 + dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
2888 + free(root_dir);
2889 + if (dest == NULL) {
2890 + continue;
2891 + }
2892 + if (conf->default_dest == NULL) {
2893 + conf->default_dest = dest;
2894 + }
2895 + if (file_exists(dest->status_file_name)) {
2896 + pkg_hash_add_from_file(conf, dest->status_file_name,
2897 + NULL, dest, 1);
2898 + }
2899 + }
2900 +
2901 + return 0;
2902 +}
2903 +
2904 +static int ipkg_conf_parse_file(ipkg_conf_t *conf, const char *filename,
2905 + pkg_src_list_t *pkg_src_list,
2906 + nv_pair_list_t *tmp_dest_nv_pair_list,
2907 + char **lists_dir)
2908 +{
2909 + ipkg_option_t * options;
2910 + FILE *file = fopen(filename, "r");
2911 + regex_t valid_line_re, comment_re;
2912 +#define regmatch_size 12
2913 + regmatch_t regmatch[regmatch_size];
2914 +
2915 + if (ipkg_init_options_array(conf, &options)<0)
2916 + return ENOMEM;
2917 +
2918 + if (file == NULL) {
2919 + fprintf(stderr, "%s: failed to open %s: %s\n",
2920 + __FUNCTION__, filename, strerror(errno));
2921 + free(options);
2922 + return errno;
2923 + }
2924 + ipkg_message(conf, IPKG_NOTICE, "loading conf file %s\n", filename);
2925 +
2926 + xregcomp(&comment_re,
2927 + "^[[:space:]]*(#.*|[[:space:]]*)$",
2928 + REG_EXTENDED);
2929 + xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
2930 +
2931 + while(1) {
2932 + int line_num = 0;
2933 + char *line;
2934 + char *type, *name, *value, *extra;
2935 +
2936 + line = file_read_line_alloc(file);
2937 + line_num++;
2938 + if (line == NULL) {
2939 + break;
2940 + }
2941 +
2942 + str_chomp(line);
2943 +
2944 + if (regexec(&comment_re, line, 0, 0, 0) == 0) {
2945 + goto NEXT_LINE;
2946 + }
2947 +
2948 + if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
2949 + str_chomp(line);
2950 + fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
2951 + filename, line_num, line);
2952 + goto NEXT_LINE;
2953 + }
2954 +
2955 + /* This has to be so ugly to deal with optional quotation marks */
2956 + if (regmatch[2].rm_so > 0) {
2957 + type = strndup(line + regmatch[2].rm_so,
2958 + regmatch[2].rm_eo - regmatch[2].rm_so);
2959 + } else {
2960 + type = strndup(line + regmatch[3].rm_so,
2961 + regmatch[3].rm_eo - regmatch[3].rm_so);
2962 + }
2963 + if (regmatch[5].rm_so > 0) {
2964 + name = strndup(line + regmatch[5].rm_so,
2965 + regmatch[5].rm_eo - regmatch[5].rm_so);
2966 + } else {
2967 + name = strndup(line + regmatch[6].rm_so,
2968 + regmatch[6].rm_eo - regmatch[6].rm_so);
2969 + }
2970 + if (regmatch[8].rm_so > 0) {
2971 + value = strndup(line + regmatch[8].rm_so,
2972 + regmatch[8].rm_eo - regmatch[8].rm_so);
2973 + } else {
2974 + value = strndup(line + regmatch[9].rm_so,
2975 + regmatch[9].rm_eo - regmatch[9].rm_so);
2976 + }
2977 + extra = NULL;
2978 + if (regmatch[11].rm_so > 0) {
2979 + extra = strndup (line + regmatch[11].rm_so,
2980 + regmatch[11].rm_eo - regmatch[11].rm_so);
2981 + }
2982 +
2983 + /* We use the tmp_dest_nv_pair_list below instead of
2984 + conf->pkg_dest_list because we might encounter an
2985 + offline_root option later and that would invalidate the
2986 + directories we would have computed in
2987 + pkg_dest_list_init. (We do a similar thing with
2988 + tmp_src_nv_pair_list for sake of symmetry.) */
2989 + if (strcmp(type, "option") == 0) {
2990 + ipkg_conf_set_option(options, name, value);
2991 + } else if (strcmp(type, "src") == 0) {
2992 + if (!nv_pair_list_find((nv_pair_list_t *)pkg_src_list, name)) {
2993 + pkg_src_list_append (pkg_src_list, name, value, extra, 0);
2994 + } else {
2995 + ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
2996 + name, value);
2997 + }
2998 + } else if (strcmp(type, "src/gz") == 0) {
2999 + if (!nv_pair_list_find((nv_pair_list_t *)pkg_src_list, name)) {
3000 + pkg_src_list_append (pkg_src_list, name, value, extra, 1);
3001 + } else {
3002 + ipkg_message(conf, IPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
3003 + name, value);
3004 + }
3005 + } else if (strcmp(type, "dest") == 0) {
3006 + nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
3007 + } else if (strcmp(type, "lists_dir") == 0) {
3008 + *lists_dir = realloc(*lists_dir,strlen(value)+1);
3009 + if (*lists_dir == NULL) {
3010 + ipkg_message(conf, IPKG_ERROR, "ERROR: Not enough memory\n");
3011 + free(options);
3012 + return EINVAL;
3013 + }
3014 + sprintf (*lists_dir,"%s",value);
3015 + } else if (strcmp(type, "arch") == 0) {
3016 + ipkg_message(conf, IPKG_INFO, "supported arch %s priority (%s)\n", name, value);
3017 + if (!value) {
3018 + ipkg_message(conf, IPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
3019 + value = strdup("10");
3020 + }
3021 + nv_pair_list_append(&conf->arch_list, strdup(name), strdup(value));
3022 + } else {
3023 + fprintf(stderr, "WARNING: Ignoring unknown configuration "
3024 + "parameter: %s %s %s\n", type, name, value);
3025 + free(options);
3026 + return EINVAL;
3027 + }
3028 +
3029 + free(type);
3030 + free(name);
3031 + free(value);
3032 + if (extra)
3033 + free (extra);
3034 +
3035 + NEXT_LINE:
3036 + free(line);
3037 + }
3038 +
3039 + free(options);
3040 + regfree(&comment_re);
3041 + regfree(&valid_line_re);
3042 + fclose(file);
3043 +
3044 + return 0;
3045 +}
3046 +
3047 +static int ipkg_conf_set_option(const ipkg_option_t *options,
3048 + const char *name, const char *value)
3049 +{
3050 + int i = 0;
3051 + while (options[i].name) {
3052 + if (strcmp(options[i].name, name) == 0) {
3053 + switch (options[i].type) {
3054 + case IPKG_OPT_TYPE_BOOL:
3055 + *((int *)options[i].value) = 1;
3056 + return 0;
3057 + case IPKG_OPT_TYPE_INT:
3058 + if (value) {
3059 + *((int *)options[i].value) = atoi(value);
3060 + return 0;
3061 + } else {
3062 + printf("%s: Option %s need an argument\n",
3063 + __FUNCTION__, name);
3064 + return EINVAL;
3065 + }
3066 + case IPKG_OPT_TYPE_STRING:
3067 + if (value) {
3068 + *((char **)options[i].value) = strdup(value);
3069 + return 0;
3070 + } else {
3071 + printf("%s: Option %s need an argument\n",
3072 + __FUNCTION__, name);
3073 + return EINVAL;
3074 + }
3075 + }
3076 + }
3077 + i++;
3078 + }
3079 +
3080 + fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
3081 + __FUNCTION__, name, value);
3082 + return EINVAL;
3083 +}
3084 +
3085 +int ipkg_conf_write_status_files(ipkg_conf_t *conf)
3086 +{
3087 + pkg_dest_list_elt_t *iter;
3088 + pkg_dest_t *dest;
3089 + pkg_vec_t *all;
3090 + pkg_t *pkg;
3091 + register int i;
3092 + int err;
3093 +
3094 + if (conf->noaction)
3095 + return 0;
3096 + for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3097 + dest = iter->data;
3098 + dest->status_file = fopen(dest->status_file_tmp_name, "w");
3099 + if (dest->status_file == NULL) {
3100 + fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
3101 + __FUNCTION__, dest->status_file_name, strerror(errno));
3102 + }
3103 + }
3104 +
3105 + all = pkg_vec_alloc();
3106 + pkg_hash_fetch_available(&conf->pkg_hash, all);
3107 +
3108 + for(i = 0; i < all->len; i++) {
3109 + pkg = all->pkgs[i];
3110 + /* We don't need most uninstalled packages in the status file */
3111 + if (pkg->state_status == SS_NOT_INSTALLED
3112 + && (pkg->state_want == SW_UNKNOWN
3113 + || pkg->state_want == SW_DEINSTALL
3114 + || pkg->state_want == SW_PURGE)) {
3115 + continue;
3116 + }
3117 + if (!pkg) {
3118 + fprintf(stderr, "Null package\n");
3119 + }
3120 + if (pkg->dest == NULL) {
3121 + fprintf(stderr, "%s: ERROR: Can't write status for "
3122 + "package %s since it has a NULL dest\n",
3123 + __FUNCTION__, pkg->name);
3124 + continue;
3125 + }
3126 + if (pkg->dest->status_file) {
3127 + pkg_print_status(pkg, pkg->dest->status_file);
3128 + }
3129 + }
3130 +
3131 + pkg_vec_free(all);
3132 +
3133 + for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
3134 + dest = iter->data;
3135 + if (dest->status_file) {
3136 + err = ferror(dest->status_file);
3137 + fclose(dest->status_file);
3138 + dest->status_file = NULL;
3139 + if (!err) {
3140 + file_move(dest->status_file_tmp_name, dest->status_file_name);
3141 + } else {
3142 + fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
3143 + "retaining old %s\n", __FUNCTION__,
3144 + dest->status_file_tmp_name, dest->status_file_name);
3145 + }
3146 + }
3147 + }
3148 +
3149 + return 0;
3150 +}
3151 +
3152 +
3153 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename)
3154 +{
3155 + char *root_filename;
3156 + sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
3157 + return root_filename;
3158 +}
3159 --- /dev/null
3160 +++ b/archival/libipkg/ipkg_conf.h
3161 @@ -0,0 +1,107 @@
3162 +/* ipkg_conf.h - the itsy package management system
3163 +
3164 + Carl D. Worth
3165 +
3166 + Copyright (C) 2001 University of Southern California
3167 +
3168 + This program is free software; you can redistribute it and/or
3169 + modify it under the terms of the GNU General Public License as
3170 + published by the Free Software Foundation; either version 2, or (at
3171 + your option) any later version.
3172 +
3173 + This program is distributed in the hope that it will be useful, but
3174 + WITHOUT ANY WARRANTY; without even the implied warranty of
3175 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3176 + General Public License for more details.
3177 +*/
3178 +
3179 +#ifndef IPKG_CONF_H
3180 +#define IPKG_CONF_H
3181 +
3182 +typedef struct ipkg_conf ipkg_conf_t;
3183 +
3184 +#include "hash_table.h"
3185 +#include "ipkg.h"
3186 +#include "args.h"
3187 +#include "pkg.h"
3188 +#include "pkg_hash.h"
3189 +#include "pkg_src_list.h"
3190 +#include "pkg_dest_list.h"
3191 +#include "nv_pair_list.h"
3192 +
3193 +#define IPKG_CONF_DEFAULT_TMP_DIR_BASE "/tmp"
3194 +#define IPKG_CONF_TMP_DIR_SUFFIX "ipkg-XXXXXX"
3195 +#define IPKG_CONF_LISTS_DIR IPKG_STATE_DIR_PREFIX "/lists"
3196 +#define IPKG_CONF_PENDING_DIR IPKG_STATE_DIR_PREFIX "/pending"
3197 +
3198 +/* In case the config file defines no dest */
3199 +#define IPKG_CONF_DEFAULT_DEST_NAME "root"
3200 +#define IPKG_CONF_DEFAULT_DEST_ROOT_DIR "/"
3201 +
3202 +#define IPKG_CONF_DEFAULT_HASH_LEN 1024
3203 +
3204 +struct ipkg_conf
3205 +{
3206 + pkg_src_list_t pkg_src_list;
3207 + pkg_dest_list_t pkg_dest_list;
3208 + nv_pair_list_t arch_list;
3209 +
3210 + int restrict_to_default_dest;
3211 + pkg_dest_t *default_dest;
3212 +
3213 + char *tmp_dir;
3214 + const char *lists_dir;
3215 + const char *pending_dir;
3216 +
3217 + /* options */
3218 + int force_depends;
3219 + int force_defaults;
3220 + int force_overwrite;
3221 + int force_downgrade;
3222 + int force_reinstall;
3223 + int force_space;
3224 + int force_removal_of_dependent_packages;
3225 + int force_removal_of_essential_packages;
3226 + int nodeps; /* do not follow dependences */
3227 + int verbose_wget;
3228 + int multiple_providers;
3229 + char *offline_root;
3230 + char *offline_root_pre_script_cmd;
3231 + char *offline_root_post_script_cmd;
3232 + int query_all;
3233 + int verbosity;
3234 + int noaction;
3235 +
3236 + /* proxy options */
3237 + char *http_proxy;
3238 + char *ftp_proxy;
3239 + char *no_proxy;
3240 + char *proxy_user;
3241 + char *proxy_passwd;
3242 +
3243 + hash_table_t pkg_hash;
3244 + hash_table_t file_hash;
3245 + hash_table_t obs_file_hash;
3246 +};
3247 +
3248 +enum ipkg_option_type {
3249 + IPKG_OPT_TYPE_BOOL,
3250 + IPKG_OPT_TYPE_INT,
3251 + IPKG_OPT_TYPE_STRING
3252 +};
3253 +typedef enum ipkg_option_type ipkg_option_type_t;
3254 +
3255 +typedef struct ipkg_option ipkg_option_t;
3256 +struct ipkg_option {
3257 + const char *name;
3258 + const ipkg_option_type_t type;
3259 + const void *value;
3260 +};
3261 +
3262 +int ipkg_conf_init(ipkg_conf_t *conf, const args_t *args);
3263 +void ipkg_conf_deinit(ipkg_conf_t *conf);
3264 +
3265 +int ipkg_conf_write_status_files(ipkg_conf_t *conf);
3266 +char *root_filename_alloc(ipkg_conf_t *conf, char *filename);
3267 +
3268 +#endif
3269 --- /dev/null
3270 +++ b/archival/libipkg/ipkg_configure.c
3271 @@ -0,0 +1,40 @@
3272 +/* ipkg_configure.c - the itsy package management system
3273 +
3274 + Carl D. Worth
3275 +
3276 + Copyright (C) 2001 University of Southern California
3277 +
3278 + This program is free software; you can redistribute it and/or
3279 + modify it under the terms of the GNU General Public License as
3280 + published by the Free Software Foundation; either version 2, or (at
3281 + your option) any later version.
3282 +
3283 + This program is distributed in the hope that it will be useful, but
3284 + WITHOUT ANY WARRANTY; without even the implied warranty of
3285 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3286 + General Public License for more details.
3287 +*/
3288 +
3289 +#include "ipkg.h"
3290 +
3291 +#include "ipkg_configure.h"
3292 +
3293 +int ipkg_configure(ipkg_conf_t *conf, pkg_t *pkg)
3294 +{
3295 + int err;
3296 +
3297 + /* DPKG_INCOMPATIBILITY:
3298 + dpkg actually does some conffile handling here, rather than at the
3299 + end of ipkg_install(). Do we care? */
3300 + /* DPKG_INCOMPATIBILITY:
3301 + dpkg actually includes a version number to this script call */
3302 + err = pkg_run_script(conf, pkg, "postinst", "configure");
3303 + if (err) {
3304 + printf("ERROR: %s.postinst returned %d\n", pkg->name, err);
3305 + return err;
3306 + }
3307 +
3308 + ipkg_state_changed++;
3309 + return 0;
3310 +}
3311 +
3312 --- /dev/null
3313 +++ b/archival/libipkg/ipkg_configure.h
3314 @@ -0,0 +1,25 @@
3315 +/* ipkg_configure.h - the itsy package management system
3316 +
3317 + Carl D. Worth
3318 +
3319 + Copyright (C) 2001 University of Southern California
3320 +
3321 + This program is free software; you can redistribute it and/or
3322 + modify it under the terms of the GNU General Public License as
3323 + published by the Free Software Foundation; either version 2, or (at
3324 + your option) any later version.
3325 +
3326 + This program is distributed in the hope that it will be useful, but
3327 + WITHOUT ANY WARRANTY; without even the implied warranty of
3328 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3329 + General Public License for more details.
3330 +*/
3331 +
3332 +#ifndef IPKG_CONFIGURE_H
3333 +#define IPKG_CONFIGURE_H
3334 +
3335 +#include "ipkg_conf.h"
3336 +
3337 +int ipkg_configure(ipkg_conf_t *ipkg_conf, pkg_t *pkg);
3338 +
3339 +#endif
3340 --- /dev/null
3341 +++ b/archival/libipkg/ipkg_download.c
3342 @@ -0,0 +1,195 @@
3343 +/* ipkg_download.c - the itsy package management system
3344 +
3345 + Carl D. Worth
3346 +
3347 + Copyright (C) 2001 University of Southern California
3348 +
3349 + This program is free software; you can redistribute it and/or
3350 + modify it under the terms of the GNU General Public License as
3351 + published by the Free Software Foundation; either version 2, or (at
3352 + your option) any later version.
3353 +
3354 + This program is distributed in the hope that it will be useful, but
3355 + WITHOUT ANY WARRANTY; without even the implied warranty of
3356 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3357 + General Public License for more details.
3358 +*/
3359 +
3360 +#include "ipkg.h"
3361 +#include "ipkg_download.h"
3362 +#include "ipkg_message.h"
3363 +
3364 +#include "sprintf_alloc.h"
3365 +#include "xsystem.h"
3366 +#include "file_util.h"
3367 +#include "str_util.h"
3368 +
3369 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name)
3370 +{
3371 + int err = 0;
3372 +
3373 + char *src_basec = strdup(src);
3374 + char *src_base = basename(src_basec);
3375 + char *tmp_file_location;
3376 + char *cmd;
3377 +
3378 + ipkg_message(conf,IPKG_NOTICE,"Downloading %s\n", src);
3379 +
3380 + fflush(stdout);
3381 +
3382 + if (str_starts_with(src, "file:")) {
3383 + int ret;
3384 + const char *file_src = src + 5;
3385 + ipkg_message(conf,IPKG_INFO,"Copying %s to %s...", file_src, dest_file_name);
3386 + ret = file_copy(src + 5, dest_file_name);
3387 + ipkg_message(conf,IPKG_INFO,"Done.\n");
3388 + return ret;
3389 + }
3390 +
3391 + sprintf_alloc(&tmp_file_location, "%s/%s", conf->tmp_dir, src_base);
3392 + err = unlink(tmp_file_location);
3393 + if (err && errno != ENOENT) {
3394 + ipkg_message(conf,IPKG_ERROR, "%s: ERROR: failed to unlink %s: %s\n",
3395 + __FUNCTION__, tmp_file_location, strerror(errno));
3396 + free(tmp_file_location);
3397 + return errno;
3398 + }
3399 +
3400 + if (conf->http_proxy) {
3401 + ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: http_proxy = %s\n", conf->http_proxy);
3402 + setenv("http_proxy", conf->http_proxy, 1);
3403 + }
3404 + if (conf->ftp_proxy) {
3405 + ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: ftp_proxy = %s\n", conf->ftp_proxy);
3406 + setenv("ftp_proxy", conf->ftp_proxy, 1);
3407 + }
3408 + if (conf->no_proxy) {
3409 + ipkg_message(conf,IPKG_DEBUG,"Setting environment variable: no_proxy = %s\n", conf->no_proxy);
3410 + setenv("no_proxy", conf->no_proxy, 1);
3411 + }
3412 +
3413 + /* XXX: BUG rewrite to use execvp or else busybox's internal wget -Jamey 7/23/2002 */
3414 + sprintf_alloc(&cmd, "wget --passive-ftp %s %s%s %s%s %s -P %s %s",
3415 + (conf->http_proxy || conf->ftp_proxy) ? "--proxy=on" : "",
3416 + conf->proxy_user ? "--proxy-user=" : "",
3417 + conf->proxy_user ? conf->proxy_user : "",
3418 + conf->proxy_passwd ? "--proxy-passwd=" : "",
3419 + conf->proxy_passwd ? conf->proxy_passwd : "",
3420 + conf->verbose_wget ? "" : "-q",
3421 + conf->tmp_dir,
3422 + src);
3423 + err = xsystem(cmd);
3424 + if (err) {
3425 + if (err != -1) {
3426 + ipkg_message(conf,IPKG_ERROR, "%s: ERROR: Command failed with return value %d: `%s'\n",
3427 + __FUNCTION__, err, cmd);
3428 + }
3429 + unlink(tmp_file_location);
3430 + free(tmp_file_location);
3431 + free(src_basec);
3432 + free(cmd);
3433 + return EINVAL;
3434 + }
3435 + free(cmd);
3436 +
3437 + err = file_move(tmp_file_location, dest_file_name);
3438 +
3439 + free(tmp_file_location);
3440 + free(src_basec);
3441 +
3442 + if (err) {
3443 + return err;
3444 + }
3445 +
3446 + return 0;
3447 +}
3448 +
3449 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir)
3450 +{
3451 + int err;
3452 + char *url;
3453 +
3454 + if (pkg->src == NULL) {
3455 + ipkg_message(conf,IPKG_ERROR, "ERROR: Package %s (parent %s) is not available from any configured src.\n",
3456 + pkg->name, pkg->parent->name);
3457 + return -1;
3458 + }
3459 +
3460 + sprintf_alloc(&url, "%s/%s", pkg->src->value, pkg->filename);
3461 +
3462 + /* XXX: BUG: The pkg->filename might be something like
3463 + "../../foo.ipk". While this is correct, and exactly what we
3464 + want to use to construct url above, here we actually need to
3465 + use just the filename part, without any directory. */
3466 + sprintf_alloc(&pkg->local_filename, "%s/%s", dir, pkg->filename);
3467 +
3468 + err = ipkg_download(conf, url, pkg->local_filename);
3469 + free(url);
3470 +
3471 + return err;
3472 +}
3473 +
3474 +/*
3475 + * Downloads file from url, installs in package database, return package name.
3476 + */
3477 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep)
3478 +{
3479 + int err = 0;
3480 + pkg_t *pkg;
3481 + pkg = pkg_new();
3482 + if (pkg == NULL)
3483 + return ENOMEM;
3484 +
3485 + if (str_starts_with(url, "http://")
3486 + || str_starts_with(url, "ftp://")) {
3487 + char *tmp_file;
3488 + char *file_basec = strdup(url);
3489 + char *file_base = basename(file_basec);
3490 +
3491 + sprintf_alloc(&tmp_file, "%s/%s", conf->tmp_dir, file_base);
3492 + err = ipkg_download(conf, url, tmp_file);
3493 + if (err)
3494 + return err;
3495 +
3496 + err = pkg_init_from_file(pkg, tmp_file);
3497 + if (err)
3498 + return err;
3499 + pkg->local_filename = strdup(tmp_file);
3500 +
3501 + free(tmp_file);
3502 + free(file_basec);
3503 +
3504 + } else if (strcmp(&url[strlen(url) - 4], IPKG_PKG_EXTENSION) == 0
3505 + || strcmp(&url[strlen(url) - 4], DPKG_PKG_EXTENSION) == 0) {
3506 +
3507 + err = pkg_init_from_file(pkg, url);
3508 + if (err)
3509 + return err;
3510 + pkg->local_filename = strdup(url);
3511 + ipkg_message(conf, IPKG_DEBUG2, "Package %s provided by hand (%s).\n", pkg->name,pkg->local_filename);
3512 + pkg->provided_by_hand = 1;
3513 +
3514 + } else {
3515 + pkg_deinit(pkg);
3516 + free(pkg);
3517 + return 0;
3518 + }
3519 +
3520 + if (!pkg->architecture) {
3521 + ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3522 + return -EINVAL;
3523 + }
3524 +
3525 + pkg->dest = conf->default_dest;
3526 + pkg->state_want = SW_INSTALL;
3527 + pkg->state_flag |= SF_PREFER;
3528 + pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
3529 + if ( pkg == NULL ){
3530 + fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
3531 + return 0;
3532 + }
3533 + if (namep) {
3534 + *namep = strdup(pkg->name);
3535 + }
3536 + return 0;
3537 +}
3538 --- /dev/null
3539 +++ b/archival/libipkg/ipkg_download.h
3540 @@ -0,0 +1,30 @@
3541 +/* ipkg_download.h - the itsy package management system
3542 +
3543 + Carl D. Worth
3544 +
3545 + Copyright (C) 2001 University of Southern California
3546 +
3547 + This program is free software; you can redistribute it and/or
3548 + modify it under the terms of the GNU General Public License as
3549 + published by the Free Software Foundation; either version 2, or (at
3550 + your option) any later version.
3551 +
3552 + This program is distributed in the hope that it will be useful, but
3553 + WITHOUT ANY WARRANTY; without even the implied warranty of
3554 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3555 + General Public License for more details.
3556 +*/
3557 +
3558 +#ifndef IPKG_DOWNLOAD_H
3559 +#define IPKG_DOWNLOAD_H
3560 +
3561 +#include "ipkg_conf.h"
3562 +
3563 +int ipkg_download(ipkg_conf_t *conf, const char *src, const char *dest_file_name);
3564 +int ipkg_download_pkg(ipkg_conf_t *conf, pkg_t *pkg, const char *dir);
3565 +/*
3566 + * Downloads file from url, installs in package database, return package name.
3567 + */
3568 +int ipkg_prepare_url_for_install(ipkg_conf_t *conf, const char *url, char **namep);
3569 +
3570 +#endif
3571 --- /dev/null
3572 +++ b/archival/libipkg/ipkg.h
3573 @@ -0,0 +1,74 @@
3574 +/* ipkg.h - the itsy package management system
3575 +
3576 + Carl D. Worth
3577 +
3578 + Copyright (C) 2001 University of Southern California
3579 +
3580 + This program is free software; you can redistribute it and/or
3581 + modify it under the terms of the GNU General Public License as
3582 + published by the Free Software Foundation; either version 2, or (at
3583 + your option) any later version.
3584 +
3585 + This program is distributed in the hope that it will be useful, but
3586 + WITHOUT ANY WARRANTY; without even the implied warranty of
3587 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3588 + General Public License for more details.
3589 +*/
3590 +
3591 +#ifndef IPKG_H
3592 +#define IPKG_H
3593 +
3594 +/*
3595 +#ifdef HAVE_CONFIG_H
3596 +#include "config.h"
3597 +#endif
3598 +*/
3599 +
3600 +#if 0
3601 +#define IPKG_DEBUG_NO_TMP_CLEANUP
3602 +#endif
3603 +
3604 +#include "ipkg_includes.h"
3605 +#include "ipkg_conf.h"
3606 +#include "ipkg_message.h"
3607 +
3608 +#define IPKG_PKG_EXTENSION ".ipk"
3609 +#define DPKG_PKG_EXTENSION ".deb"
3610 +
3611 +#define IPKG_LEGAL_PKG_NAME_CHARS "abcdefghijklmnopqrstuvwxyz0123456789.+-"
3612 +#define IPKG_PKG_VERSION_SEP_CHAR '_'
3613 +
3614 +#define IPKG_STATE_DIR_PREFIX IPKGLIBDIR"/ipkg"
3615 +#define IPKG_LISTS_DIR_SUFFIX "lists"
3616 +#define IPKG_INFO_DIR_SUFFIX "info"
3617 +#define IPKG_STATUS_FILE_SUFFIX "status"
3618 +
3619 +#define IPKG_BACKUP_SUFFIX "-ipkg.backup"
3620 +
3621 +#define IPKG_LIST_DESCRIPTION_LENGTH 128
3622 +
3623 +#define IPKG_VERSION "0.99.162"
3624 +
3625 +
3626 +enum ipkg_error {
3627 + IPKG_SUCCESS = 0,
3628 + IPKG_PKG_DEPS_UNSATISFIED,
3629 + IPKG_PKG_IS_ESSENTIAL,
3630 + IPKG_PKG_HAS_DEPENDENTS,
3631 + IPKG_PKG_HAS_NO_CANDIDATE
3632 +};
3633 +typedef enum ipkg_error ipkg_error_t;
3634 +
3635 +extern int ipkg_state_changed;
3636 +
3637 +
3638 +struct errlist {
3639 + char * errmsg;
3640 + struct errlist * next;
3641 +} ;
3642 +
3643 +extern struct errlist* error_list;
3644 +
3645 +extern ipkg_conf_t *global_conf;
3646 +
3647 +#endif
3648 --- /dev/null
3649 +++ b/archival/libipkg/ipkg_includes.h
3650 @@ -0,0 +1,79 @@
3651 +#ifndef IPKG_INCLUDES_H
3652 +#define IPKG_INCLUDES_H
3653 +
3654 +/* Define to 1 if you have the <memory.h> header file. */
3655 +#define HAVE_MEMORY_H 1
3656 +
3657 +/* Define to 1 if you have the <regex.h> header file. */
3658 +#define HAVE_REGEX_H 1
3659 +
3660 +/* Define to 1 if you have the <stdlib.h> header file. */
3661 +#define HAVE_STDLIB_H 1
3662 +
3663 +/* Define to 1 if you have the <strings.h> header file. */
3664 +#define HAVE_STRINGS_H 1
3665 +
3666 +/* Define to 1 if you have the <string.h> header file. */
3667 +#define HAVE_STRING_H 1
3668 +
3669 +/* Define to 1 if you have the <sys/stat.h> header file. */
3670 +#define HAVE_SYS_STAT_H 1
3671 +
3672 +/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
3673 +#define HAVE_SYS_WAIT_H 1
3674 +
3675 +/* Define to 1 if you have the <unistd.h> header file. */
3676 +#define HAVE_UNISTD_H 1
3677 +
3678 +/* Define to 1 if you have the ANSI C header files. */
3679 +#define STDC_HEADERS 1
3680 +
3681 +
3682 +#include <stdio.h>
3683 +
3684 +#if STDC_HEADERS
3685 +# include <stdlib.h>
3686 +# include <stdarg.h>
3687 +# include <stddef.h>
3688 +# include <ctype.h>
3689 +# include <errno.h>
3690 +#else
3691 +# if HAVE_STDLIB_H
3692 +# include <stdlib.h>
3693 +# endif
3694 +#endif
3695 +
3696 +#if HAVE_REGEX_H
3697 +# include <regex.h>
3698 +#endif
3699 +
3700 +#if HAVE_STRING_H
3701 +# if !STDC_HEADERS && HAVE_MEMORY_H
3702 +# include <memory.h>
3703 +# endif
3704 +/* XXX: What's the right way to pick up GNU's strndup declaration? */
3705 +# if __GNUC__
3706 +# define __USE_GNU 1
3707 +# endif
3708 +# include <string.h>
3709 +# undef __USE_GNU
3710 +#endif
3711 +
3712 +#if HAVE_STRINGS_H
3713 +# include <strings.h>
3714 +#endif
3715 +
3716 +#if HAVE_SYS_STAT_H
3717 +# include <sys/stat.h>
3718 +#endif
3719 +
3720 +#if HAVE_SYS_WAIT_H
3721 +# include <sys/wait.h>
3722 +#endif
3723 +
3724 +#if HAVE_UNISTD_H
3725 +# include <sys/types.h>
3726 +# include <unistd.h>
3727 +#endif
3728 +
3729 +#endif /* IPKG_INCLUDES_H */
3730 --- /dev/null
3731 +++ b/archival/libipkg/ipkg_install.c
3732 @@ -0,0 +1,1942 @@
3733 +/* ipkg_install.c - the itsy package management system
3734 +
3735 + Carl D. Worth
3736 +
3737 + Copyright (C) 2001 University of Southern California
3738 +
3739 + This program is free software; you can redistribute it and/or
3740 + modify it under the terms of the GNU General Public License as
3741 + published by the Free Software Foundation; either version 2, or (at
3742 + your option) any later version.
3743 +
3744 + This program is distributed in the hope that it will be useful, but
3745 + WITHOUT ANY WARRANTY; without even the implied warranty of
3746 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3747 + General Public License for more details.
3748 +*/
3749 +
3750 +#include "ipkg.h"
3751 +#include <errno.h>
3752 +#include <dirent.h>
3753 +#include <glob.h>
3754 +#include <time.h>
3755 +#include <signal.h>
3756 +typedef void (*sighandler_t)(int);
3757 +
3758 +#include "pkg.h"
3759 +#include "pkg_hash.h"
3760 +#include "pkg_extract.h"
3761 +
3762 +#include "ipkg_install.h"
3763 +#include "ipkg_configure.h"
3764 +#include "ipkg_download.h"
3765 +#include "ipkg_remove.h"
3766 +
3767 +#include "ipkg_utils.h"
3768 +#include "ipkg_message.h"
3769 +
3770 +#include "sprintf_alloc.h"
3771 +#include "file_util.h"
3772 +#include "str_util.h"
3773 +#include "xsystem.h"
3774 +#include "user.h"
3775 +
3776 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
3777 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg);
3778 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg);
3779 +
3780 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3781 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3782 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3783 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors);
3784 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3785 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3786 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3787 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3788 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3789 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3790 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3791 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3792 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3793 +
3794 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3795 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg);
3796 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg);
3797 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg);
3798 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg);
3799 +
3800 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg);
3801 +
3802 +static int user_prefers_old_conffile(const char *file, const char *backup);
3803 +
3804 +static char *backup_filename_alloc(const char *file_name);
3805 +static int backup_make_backup(ipkg_conf_t *conf, const char *file_name);
3806 +static int backup_exists_for(const char *file_name);
3807 +static int backup_remove(const char *file_name);
3808 +
3809 +
3810 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename)
3811 +{
3812 + int err, cmp;
3813 + pkg_t *pkg, *old;
3814 + char *old_version, *new_version;
3815 +
3816 + pkg = pkg_new();
3817 + if (pkg == NULL) {
3818 + return ENOMEM;
3819 + }
3820 +
3821 + err = pkg_init_from_file(pkg, filename);
3822 + if (err) {
3823 + return err;
3824 + }
3825 +
3826 + if (!pkg->architecture) {
3827 + ipkg_message(conf, IPKG_ERROR, "Package %s has no Architecture defined.\n", pkg->name);
3828 + return -EINVAL;
3829 + }
3830 +
3831 + /* XXX: CLEANUP: hash_insert_pkg has a nasty side effect of possibly
3832 + freeing the pkg that we pass in. It might be nice to clean this up
3833 + if possible. */
3834 + pkg = hash_insert_pkg(&conf->pkg_hash, pkg, 1,conf);
3835 + old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
3836 +
3837 + pkg->local_filename = strdup(filename);
3838 +
3839 + if (old) {
3840 + old_version = pkg_version_str_alloc(old);
3841 + new_version = pkg_version_str_alloc(pkg);
3842 +
3843 + cmp = pkg_compare_versions(old, pkg);
3844 + if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
3845 + cmp = -1 ; /* then we force ipkg to downgrade */
3846 + /* We need to use a value < 0 because in the 0 case we are asking to */
3847 + /* reinstall, and some check could fail asking the "force-reinstall" option */
3848 + }
3849 + if (cmp > 0) {
3850 + ipkg_message(conf, IPKG_NOTICE,
3851 + "Not downgrading package %s on %s from %s to %s.\n",
3852 + old->name, old->dest->name, old_version, new_version);
3853 + pkg->state_want = SW_DEINSTALL;
3854 + pkg->state_flag |= SF_OBSOLETE;
3855 + free(old_version);
3856 + free(new_version);
3857 + return 0;
3858 + } else {
3859 + free(old_version);
3860 + free(new_version);
3861 + }
3862 + }
3863 +
3864 + ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
3865 + return ipkg_install_pkg(conf, pkg,0);
3866 +}
3867 +
3868 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name)
3869 +{
3870 + int cmp;
3871 + pkg_t *old, *new;
3872 + char *old_version, *new_version;
3873 +
3874 + ipkg_message(conf, IPKG_DEBUG2, " Getting old from pkg_hash_fetch \n" );
3875 + old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
3876 + if ( old )
3877 + ipkg_message(conf, IPKG_DEBUG2, " Old versions from pkg_hash_fetch %s \n", old->version );
3878 +
3879 + ipkg_message(conf, IPKG_DEBUG2, " Getting new from pkg_hash_fetch \n" );
3880 + new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
3881 + if ( new )
3882 + ipkg_message(conf, IPKG_DEBUG2, " New versions from pkg_hash_fetch %s \n", new->version );
3883 +
3884 +/* Pigi Basically here is broken the version stuff.
3885 + What's happening is that nothing provide the version to differents
3886 + functions, so the returned struct is always the latest.
3887 + That's why the install by name don't work.
3888 +*/
3889 + ipkg_message(conf, IPKG_DEBUG2, " Versions from pkg_hash_fetch in %s ", __FUNCTION__ );
3890 +
3891 + if ( old )
3892 + ipkg_message(conf, IPKG_DEBUG2, " old %s ", old->version );
3893 + if ( new )
3894 + ipkg_message(conf, IPKG_DEBUG2, " new %s ", new->version );
3895 + ipkg_message(conf, IPKG_DEBUG2, " \n");
3896 +
3897 + if (new == NULL) {
3898 + return IPKG_PKG_HAS_NO_CANDIDATE;
3899 + }
3900 +
3901 + new->state_flag |= SF_USER;
3902 + if (old) {
3903 + old_version = pkg_version_str_alloc(old);
3904 + new_version = pkg_version_str_alloc(new);
3905 +
3906 + cmp = pkg_compare_versions(old, new);
3907 + if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
3908 + ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade \n");
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 + ipkg_message(conf, IPKG_DEBUG,
3914 + "Comparing visible versions of pkg %s:"
3915 + "\n\t%s is installed "
3916 + "\n\t%s is available "
3917 + "\n\t%d was comparison result\n",
3918 + pkg_name, old_version, new_version, cmp);
3919 + if (cmp == 0 && !conf->force_reinstall) {
3920 + ipkg_message(conf, IPKG_NOTICE,
3921 + "Package %s (%s) installed in %s is up to date.\n",
3922 + old->name, old_version, old->dest->name);
3923 + free(old_version);
3924 + free(new_version);
3925 + return 0;
3926 + } else if (cmp > 0) {
3927 + ipkg_message(conf, IPKG_NOTICE,
3928 + "Not downgrading package %s on %s from %s to %s.\n",
3929 + old->name, old->dest->name, old_version, new_version);
3930 + free(old_version);
3931 + free(new_version);
3932 + return 0;
3933 + } else if (cmp < 0) {
3934 + new->dest = old->dest;
3935 + old->state_want = SW_DEINSTALL; /* Here probably the problem for bug 1277 */
3936 + }
3937 + }
3938 +
3939 + /* XXX: CLEANUP: The error code of ipkg_install_by_name is really
3940 + supposed to be an ipkg_error_t, but ipkg_install_pkg could
3941 + return any kind of integer, (might be errno from a syscall,
3942 + etc.). This is a real mess and will need to be cleaned up if
3943 + anyone ever wants to make a nice libipkg. */
3944 +
3945 + ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
3946 + return ipkg_install_pkg(conf, new,0);
3947 +}
3948 +
3949 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name)
3950 +{
3951 + abstract_pkg_vec_t *providers = pkg_hash_fetch_all_installation_candidates (&conf->pkg_hash, pkg_name);
3952 + int i;
3953 + ipkg_error_t err;
3954 + abstract_pkg_t *ppkg ;
3955 +
3956 + if (providers == NULL)
3957 + return IPKG_PKG_HAS_NO_CANDIDATE;
3958 +
3959 + for (i = 0; i < providers->len; i++) {
3960 + ppkg = abstract_pkg_vec_get(providers, i);
3961 + ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_by_name %d \n",__FUNCTION__, i);
3962 + err = ipkg_install_by_name(conf, ppkg->name);
3963 + if (err)
3964 + return err;
3965 +/* XXX Maybe ppkg should be freed ? */
3966 + }
3967 + return 0;
3968 +}
3969 +
3970 +/*
3971 + * Walk dependence graph starting with pkg, collect packages to be
3972 + * installed into pkgs_needed, in dependence order.
3973 + */
3974 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *pkgs_needed)
3975 +{
3976 + int i, err;
3977 + pkg_vec_t *depends = pkg_vec_alloc();
3978 + char **unresolved = NULL;
3979 + int ndepends;
3980 +
3981 + ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf,
3982 + pkg, depends,
3983 + &unresolved);
3984 +
3985 + if (unresolved) {
3986 + ipkg_message(conf, IPKG_ERROR,
3987 + "%s: Cannot satisfy the following dependencies for %s:\n\t",
3988 + conf->force_depends ? "Warning" : "ERROR", pkg->name);
3989 + while (*unresolved) {
3990 + ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
3991 + unresolved++;
3992 + }
3993 + ipkg_message(conf, IPKG_ERROR, "\n");
3994 + if (! conf->force_depends) {
3995 + ipkg_message(conf, IPKG_INFO,
3996 + "This could mean that your package list is out of date or that the packages\n"
3997 + "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
3998 + "of this problem try again with the '-force-depends' option.\n");
3999 + pkg_vec_free(depends);
4000 + return IPKG_PKG_DEPS_UNSATISFIED;
4001 + }
4002 + }
4003 +
4004 + if (ndepends <= 0) {
4005 + pkg_vec_free(depends);
4006 + return 0;
4007 + }
4008 +
4009 + for (i = 0; i < depends->len; i++) {
4010 + pkg_t *dep = depends->pkgs[i];
4011 + /* The package was uninstalled when we started, but another
4012 + dep earlier in this loop may have depended on it and pulled
4013 + it in, so check first. */
4014 + if ((dep->state_status != SS_INSTALLED)
4015 + && (dep->state_status != SS_UNPACKED)
4016 + && (dep->state_want != SW_INSTALL)) {
4017 +
4018 + /* Mark packages as to-be-installed */
4019 + dep->state_want = SW_INSTALL;
4020 +
4021 + /* Dependencies should be installed the same place as pkg */
4022 + if (dep->dest == NULL) {
4023 + dep->dest = pkg->dest;
4024 + }
4025 +
4026 + err = pkg_mark_dependencies_for_installation(conf, dep, pkgs_needed);
4027 + if (err) {
4028 + pkg_vec_free(depends);
4029 + return err;
4030 + }
4031 + }
4032 + }
4033 + if (pkgs_needed)
4034 + pkg_vec_insert(pkgs_needed, pkg);
4035 +
4036 + pkg_vec_free(depends);
4037 +
4038 + return 0;
4039 +}
4040 +
4041 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed)
4042 +{
4043 + int cmp;
4044 + pkg_t *old, *new;
4045 + char *old_version, *new_version;
4046 +
4047 + old = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg_name);
4048 +
4049 + new = pkg_hash_fetch_best_installation_candidate_by_name(conf, pkg_name);
4050 + if (new == NULL) {
4051 + return IPKG_PKG_HAS_NO_CANDIDATE;
4052 + }
4053 + if (old) {
4054 + old_version = pkg_version_str_alloc(old);
4055 + new_version = pkg_version_str_alloc(new);
4056 +
4057 + cmp = pkg_compare_versions(old, new);
4058 + if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
4059 + ipkg_message(conf, IPKG_DEBUG, " Forcing downgrade ");
4060 + cmp = -1 ; /* then we force ipkg to downgrade */
4061 + /* We need to use a value < 0 because in the 0 case we are asking to */
4062 + /* reinstall, and some check could fail asking the "force-reinstall" option */
4063 + }
4064 + ipkg_message(conf, IPKG_DEBUG,
4065 + "comparing visible versions of pkg %s:"
4066 + "\n\t%s is installed "
4067 + "\n\t%s is available "
4068 + "\n\t%d was comparison result\n",
4069 + pkg_name, old_version, new_version, cmp);
4070 + if (cmp == 0 && !conf->force_reinstall) {
4071 + ipkg_message(conf, IPKG_NOTICE,
4072 + "Package %s (%s) installed in %s is up to date.\n",
4073 + old->name, old_version, old->dest->name);
4074 + free(old_version);
4075 + free(new_version);
4076 + return 0;
4077 + } else if (cmp > 0) {
4078 + ipkg_message(conf, IPKG_NOTICE,
4079 + "Not downgrading package %s on %s from %s to %s.\n",
4080 + old->name, old->dest->name, old_version, new_version);
4081 + free(old_version);
4082 + free(new_version);
4083 + return 0;
4084 + } else if (cmp < 0) {
4085 + new->dest = old->dest;
4086 + old->state_want = SW_DEINSTALL;
4087 + old->state_flag |= SF_OBSOLETE;
4088 + }
4089 + }
4090 + return pkg_mark_dependencies_for_installation(conf, new, pkgs_needed);
4091 +}
4092 +
4093 +\f
4094 +
4095 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg)
4096 +{
4097 + int i, err;
4098 + pkg_vec_t *depends = pkg_vec_alloc();
4099 + pkg_t *dep;
4100 + char **unresolved = NULL;
4101 + int ndepends;
4102 +
4103 + ndepends = pkg_hash_fetch_unsatisfied_dependencies(conf,
4104 + pkg, depends,
4105 + &unresolved);
4106 +
4107 + if (unresolved) {
4108 + ipkg_message(conf, IPKG_ERROR,
4109 + "%s: Cannot satisfy the following dependencies for %s:\n\t",
4110 + conf->force_depends ? "Warning" : "ERROR", pkg->name);
4111 + while (*unresolved) {
4112 + ipkg_message(conf, IPKG_ERROR, " %s", *unresolved);
4113 + unresolved++;
4114 + }
4115 + ipkg_message(conf, IPKG_ERROR, "\n");
4116 + if (! conf->force_depends) {
4117 + ipkg_message(conf, IPKG_INFO,
4118 + "This could mean that your package list is out of date or that the packages\n"
4119 + "mentioned above do not yet exist (try 'ipkg update'). To proceed in spite\n"
4120 + "of this problem try again with the '-force-depends' option.\n");
4121 + pkg_vec_free(depends);
4122 + return IPKG_PKG_DEPS_UNSATISFIED;
4123 + }
4124 + }
4125 +
4126 + if (ndepends <= 0) {
4127 + return 0;
4128 + }
4129 +
4130 + /* Mark packages as to-be-installed */
4131 + for (i=0; i < depends->len; i++) {
4132 + /* Dependencies should be installed the same place as pkg */
4133 + if (depends->pkgs[i]->dest == NULL) {
4134 + depends->pkgs[i]->dest = pkg->dest;
4135 + }
4136 + depends->pkgs[i]->state_want = SW_INSTALL;
4137 + }
4138 +
4139 + for (i = 0; i < depends->len; i++) {
4140 + dep = depends->pkgs[i];
4141 + /* The package was uninstalled when we started, but another
4142 + dep earlier in this loop may have depended on it and pulled
4143 + it in, so check first. */
4144 + if ((dep->state_status != SS_INSTALLED)
4145 + && (dep->state_status != SS_UNPACKED)) {
4146 + ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4147 + err = ipkg_install_pkg(conf, dep,0);
4148 + if (err) {
4149 + pkg_vec_free(depends);
4150 + return err;
4151 + }
4152 + }
4153 + }
4154 +
4155 + pkg_vec_free(depends);
4156 +
4157 + return 0;
4158 +}
4159 +
4160 +
4161 +/* check all packages have their dependences satisfied, e.g., in case an upgraded package split */
4162 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf)
4163 +{
4164 + if (conf->nodeps == 0) {
4165 + int i;
4166 + pkg_vec_t *installed = pkg_vec_alloc();
4167 + pkg_hash_fetch_all_installed(&conf->pkg_hash, installed);
4168 + for (i = 0; i < installed->len; i++) {
4169 + pkg_t *pkg = installed->pkgs[i];
4170 + satisfy_dependencies_for(conf, pkg);
4171 + }
4172 + pkg_vec_free(installed);
4173 + }
4174 + return 0;
4175 +}
4176 +
4177 +\f
4178 +
4179 +static int check_conflicts_for(ipkg_conf_t *conf, pkg_t *pkg)
4180 +{
4181 + int i;
4182 + pkg_vec_t *conflicts = NULL;
4183 + int level;
4184 + const char *prefix;
4185 + if (conf->force_depends) {
4186 + level = IPKG_NOTICE;
4187 + prefix = "Warning";
4188 + } else {
4189 + level = IPKG_ERROR;
4190 + prefix = "ERROR";
4191 + }
4192 +
4193 + if (!conf->force_depends)
4194 + conflicts = (pkg_vec_t *)pkg_hash_fetch_conflicts(&conf->pkg_hash, pkg);
4195 +
4196 + if (conflicts) {
4197 + ipkg_message(conf, level,
4198 + "%s: The following packages conflict with %s:\n\t", prefix, pkg->name);
4199 + i = 0;
4200 + while (i < conflicts->len)
4201 + ipkg_message(conf, level, " %s", conflicts->pkgs[i++]->name);
4202 + ipkg_message(conf, level, "\n");
4203 + pkg_vec_free(conflicts);
4204 + return IPKG_PKG_DEPS_UNSATISFIED;
4205 + }
4206 + return 0;
4207 +}
4208 +
4209 +static int update_file_ownership(ipkg_conf_t *conf, pkg_t *new_pkg, pkg_t *old_pkg)
4210 +{
4211 + str_list_t *new_list = pkg_get_installed_files(new_pkg);
4212 + str_list_elt_t *iter;
4213 +
4214 + for (iter = new_list->head; iter; iter = iter->next) {
4215 + char *new_file = iter->data;
4216 + pkg_t *owner = file_hash_get_file_owner(conf, new_file);
4217 + if (!new_file)
4218 + ipkg_message(conf, IPKG_ERROR, "Null new_file for new_pkg=%s\n", new_pkg->name);
4219 + if (!owner || (owner == old_pkg))
4220 + file_hash_set_file_owner(conf, new_file, new_pkg);
4221 + }
4222 + if (old_pkg) {
4223 + str_list_t *old_list = pkg_get_installed_files(old_pkg);
4224 + for (iter = old_list->head; iter; iter = iter->next) {
4225 + char *old_file = iter->data;
4226 + pkg_t *owner = file_hash_get_file_owner(conf, old_file);
4227 + if (owner == old_pkg) {
4228 + /* obsolete */
4229 + hash_table_insert(&conf->obs_file_hash, old_file, old_pkg);
4230 + }
4231 + }
4232 + }
4233 + return 0;
4234 +}
4235 +
4236 +static int verify_pkg_installable(ipkg_conf_t *conf, pkg_t *pkg)
4237 +{
4238 + /* XXX: FEATURE: Anything else needed here? Maybe a check on free space? */
4239 +
4240 + /* sma 6.20.02: yup; here's the first bit */
4241 + /*
4242 + * XXX: BUG easy for cworth
4243 + * 1) please point the call below to the correct current root destination
4244 + * 2) we need to resolve how to check the required space for a pending pkg,
4245 + * my diddling with the .ipk file size below isn't going to cut it.
4246 + * 3) return a proper error code instead of 1
4247 + */
4248 + int comp_size, blocks_available;
4249 +
4250 + if (!conf->force_space && pkg->installed_size != NULL) {
4251 + blocks_available = get_available_blocks(conf->default_dest->root_dir);
4252 +
4253 + comp_size = strtoul(pkg->installed_size, NULL, 0);
4254 + /* round up a blocks count without doing fancy-but-slow casting jazz */
4255 + comp_size = (int)((comp_size + 1023) / 1024);
4256 +
4257 + if (comp_size >= blocks_available) {
4258 + ipkg_message(conf, IPKG_ERROR,
4259 + "Only have %d available blocks on filesystem %s, pkg %s needs %d\n",
4260 + blocks_available, conf->default_dest->root_dir, pkg->name, comp_size);
4261 + return ENOSPC;
4262 + }
4263 + }
4264 + return 0;
4265 +}
4266 +
4267 +static int unpack_pkg_control_files(ipkg_conf_t *conf, pkg_t *pkg)
4268 +{
4269 + int err;
4270 + char *conffiles_file_name;
4271 + char *root_dir;
4272 + FILE *conffiles_file;
4273 +
4274 + sprintf_alloc(&pkg->tmp_unpack_dir, "%s/%s-XXXXXX", conf->tmp_dir, pkg->name);
4275 +
4276 + pkg->tmp_unpack_dir = mkdtemp(pkg->tmp_unpack_dir);
4277 + if (pkg->tmp_unpack_dir == NULL) {
4278 + ipkg_message(conf, IPKG_ERROR,
4279 + "%s: Failed to create temporary directory '%s': %s\n",
4280 + __FUNCTION__, pkg->tmp_unpack_dir, strerror(errno));
4281 + return errno;
4282 + }
4283 +
4284 + err = pkg_extract_control_files_to_dir(pkg, pkg->tmp_unpack_dir);
4285 + if (err) {
4286 + return err;
4287 + }
4288 +
4289 + /* XXX: CLEANUP: There might be a cleaner place to read in the
4290 + conffiles. Seems like I should be able to get everything to go
4291 + through pkg_init_from_file. If so, maybe it would make sense to
4292 + move all of unpack_pkg_control_files to that function. */
4293 +
4294 + /* Don't need to re-read conffiles if we already have it */
4295 + if (pkg->conffiles.head) {
4296 + return 0;
4297 + }
4298 +
4299 + sprintf_alloc(&conffiles_file_name, "%s/conffiles", pkg->tmp_unpack_dir);
4300 + if (! file_exists(conffiles_file_name)) {
4301 + free(conffiles_file_name);
4302 + return 0;
4303 + }
4304 +
4305 + conffiles_file = fopen(conffiles_file_name, "r");
4306 + if (conffiles_file == NULL) {
4307 + fprintf(stderr, "%s: failed to open %s: %s\n",
4308 + __FUNCTION__, conffiles_file_name, strerror(errno));
4309 + free(conffiles_file_name);
4310 + return errno;
4311 + }
4312 + free(conffiles_file_name);
4313 +
4314 + while (1) {
4315 + char *cf_name;
4316 + char *cf_name_in_dest;
4317 +
4318 + cf_name = file_read_line_alloc(conffiles_file);
4319 + if (cf_name == NULL) {
4320 + break;
4321 + }
4322 + str_chomp(cf_name);
4323 + if (cf_name[0] == '\0') {
4324 + continue;
4325 + }
4326 +
4327 + /* Prepend dest->root_dir to conffile name.
4328 + Take pains to avoid multiple slashes. */
4329 + root_dir = pkg->dest->root_dir;
4330 + if (conf->offline_root)
4331 + /* skip the offline_root prefix */
4332 + root_dir = pkg->dest->root_dir + strlen(conf->offline_root);
4333 + sprintf_alloc(&cf_name_in_dest, "%s%s", root_dir,
4334 + cf_name[0] == '/' ? (cf_name + 1) : cf_name);
4335 +
4336 + /* Can't get an md5sum now, (file isn't extracted yet).
4337 + We'll wait until resolve_conffiles */
4338 + conffile_list_append(&pkg->conffiles, cf_name_in_dest, NULL);
4339 +
4340 + free(cf_name);
4341 + free(cf_name_in_dest);
4342 + }
4343 +
4344 + fclose(conffiles_file);
4345 +
4346 + return 0;
4347 +}
4348 +
4349 +/* returns number of installed replacees */
4350 +int pkg_get_installed_replacees(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *installed_replacees)
4351 +{
4352 + abstract_pkg_t **replaces = pkg->replaces;
4353 + int replaces_count = pkg->replaces_count;
4354 + int i, j;
4355 + for (i = 0; i < replaces_count; i++) {
4356 + abstract_pkg_t *ab_pkg = replaces[i];
4357 + pkg_vec_t *pkg_vec = ab_pkg->pkgs;
4358 + if (pkg_vec) {
4359 + for (j = 0; j < pkg_vec->len; j++) {
4360 + pkg_t *replacee = pkg_vec->pkgs[j];
4361 + if (!pkg_conflicts(pkg, replacee))
4362 + continue;
4363 + if (replacee->state_status == SS_INSTALLED) {
4364 + pkg_vec_insert(installed_replacees, replacee);
4365 + }
4366 + }
4367 + }
4368 + }
4369 + return installed_replacees->len;
4370 +}
4371 +
4372 +int pkg_remove_installed_replacees(ipkg_conf_t *conf, pkg_vec_t *replacees)
4373 +{
4374 + int i;
4375 + int replaces_count = replacees->len;
4376 + for (i = 0; i < replaces_count; i++) {
4377 + pkg_t *replacee = replacees->pkgs[i];
4378 + int err;
4379 + replacee->state_flag |= SF_REPLACE; /* flag it so remove won't complain */
4380 + err = ipkg_remove_pkg(conf, replacee,0);
4381 + if (err)
4382 + return err;
4383 + }
4384 + return 0;
4385 +}
4386 +
4387 +/* to unwind the removal: make sure they are installed */
4388 +int pkg_remove_installed_replacees_unwind(ipkg_conf_t *conf, pkg_vec_t *replacees)
4389 +{
4390 + int i, err;
4391 + int replaces_count = replacees->len;
4392 + for (i = 0; i < replaces_count; i++) {
4393 + pkg_t *replacee = replacees->pkgs[i];
4394 + if (replacee->state_status != SS_INSTALLED) {
4395 + ipkg_message(conf, IPKG_DEBUG2,"Function: %s calling ipkg_install_pkg \n",__FUNCTION__);
4396 + err = ipkg_install_pkg(conf, replacee,0);
4397 + if (err)
4398 + return err;
4399 + }
4400 + }
4401 + return 0;
4402 +}
4403 +
4404 +int caught_sigint = 0;
4405 +static void ipkg_install_pkg_sigint_handler(int sig)
4406 +{
4407 + caught_sigint = sig;
4408 +}
4409 +
4410 +/* compares versions of pkg and old_pkg, returns 0 if OK to proceed with installation of pkg, 1 otherwise */
4411 +static int ipkg_install_check_downgrade(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg, int message)
4412 +{
4413 + if (old_pkg) {
4414 + char message_out[15];
4415 + char *old_version = pkg_version_str_alloc(old_pkg);
4416 + char *new_version = pkg_version_str_alloc(pkg);
4417 + int cmp = pkg_compare_versions(old_pkg, pkg);
4418 + int rc = 0;
4419 +
4420 + memset(message_out,'\x0',15);
4421 + strncpy (message_out,"Upgrading ",strlen("Upgrading "));
4422 + if ( (conf->force_downgrade==1) && (cmp > 0) ){ /* We've been asked to allow downgrade and version is precedent */
4423 + cmp = -1 ; /* then we force ipkg to downgrade */
4424 + strncpy (message_out,"Downgrading ",strlen("Downgrading ")); /* We need to use a value < 0 because in the 0 case we are asking to */
4425 + /* reinstall, and some check could fail asking the "force-reinstall" option */
4426 + }
4427 +
4428 + if (cmp > 0) {
4429 + ipkg_message(conf, IPKG_NOTICE,
4430 + "Not downgrading package %s on %s from %s to %s.\n",
4431 + old_pkg->name, old_pkg->dest->name, old_version, new_version);
4432 + rc = 1;
4433 + } else if (cmp < 0) {
4434 + ipkg_message(conf, IPKG_NOTICE,
4435 + "%s%s on %s from %s to %s...\n",
4436 + message_out, pkg->name, old_pkg->dest->name, old_version, new_version);
4437 + pkg->dest = old_pkg->dest;
4438 + rc = 0;
4439 + } else /* cmp == 0 */ {
4440 + if (conf->force_reinstall) {
4441 + ipkg_message(conf, IPKG_NOTICE,
4442 + "Reinstalling %s (%s) on %s...\n",
4443 + pkg->name, new_version, old_pkg->dest->name);
4444 + pkg->dest = old_pkg->dest;
4445 + rc = 0;
4446 + } else {
4447 + ipkg_message(conf, IPKG_NOTICE,
4448 + "Not installing %s (%s) on %s -- already installed.\n",
4449 + pkg->name, new_version, old_pkg->dest->name);
4450 + rc = 1;
4451 + }
4452 + }
4453 + free(old_version);
4454 + free(new_version);
4455 + return rc;
4456 + } else {
4457 + char message_out[15], *version ;
4458 + memset(message_out,'\x0',15);
4459 + if ( message )
4460 + strncpy( message_out,"Upgrading ",strlen("Upgrading ") );
4461 + else
4462 + strncpy( message_out,"Installing ",strlen("Installing ") );
4463 + version = pkg_version_str_alloc(pkg);
4464 +
4465 + ipkg_message(conf, IPKG_NOTICE,
4466 + "%s%s (%s) to %s...\n", message_out,
4467 + pkg->name, version, pkg->dest->name);
4468 + free(version);
4469 + return 0;
4470 + }
4471 +}
4472 +
4473 +/* and now the meat... */
4474 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg, int from_upgrade)
4475 +{
4476 + int err = 0;
4477 + int message = 0;
4478 + pkg_t *old_pkg = NULL;
4479 + pkg_vec_t *replacees;
4480 + abstract_pkg_t *ab_pkg = NULL;
4481 + int old_state_flag;
4482 + char* file_md5;
4483 +
4484 +
4485 + if ( from_upgrade )
4486 + message = 1; /* Coming from an upgrade, and should change the output message */
4487 +
4488 + if (!pkg) {
4489 + ipkg_message(conf, IPKG_ERROR,
4490 + "INTERNAL ERROR: null pkg passed to ipkg_install_pkg\n");
4491 + return -EINVAL;
4492 + }
4493 +
4494 + ipkg_message(conf, IPKG_DEBUG2, "Function: %s calling pkg_arch_supported %s \n", __FUNCTION__, __FUNCTION__);
4495 +
4496 + if (!pkg_arch_supported(conf, pkg)) {
4497 + ipkg_message(conf, IPKG_ERROR, "INTERNAL ERROR: architecture %s for pkg %s is unsupported.\n",
4498 + pkg->architecture, pkg->name);
4499 + return -EINVAL;
4500 + }
4501 + if (pkg->state_status == SS_INSTALLED && conf->force_reinstall == 0 && conf->nodeps == 0) {
4502 + err = satisfy_dependencies_for(conf, pkg);
4503 + if (err) { return err; }
4504 +
4505 + ipkg_message(conf, IPKG_NOTICE,
4506 + "Package %s is already installed in %s.\n",
4507 + pkg->name, pkg->dest->name);
4508 + return 0;
4509 + }
4510 +
4511 + if (pkg->dest == NULL) {
4512 + pkg->dest = conf->default_dest;
4513 + }
4514 +
4515 + old_pkg = pkg_hash_fetch_installed_by_name(&conf->pkg_hash, pkg->name);
4516 +
4517 + err = ipkg_install_check_downgrade(conf, pkg, old_pkg, message);
4518 + if (err) { return err; }
4519 +
4520 + pkg->state_want = SW_INSTALL;
4521 + if (old_pkg){
4522 + old_pkg->state_want = SW_DEINSTALL; /* needed for check_data_file_clashes of dependences */
4523 + }
4524 +
4525 +
4526 + /* Abhaya: conflicts check */
4527 + err = check_conflicts_for(conf, pkg);
4528 + if (err) { return err; }
4529 +
4530 + /* this setup is to remove the upgrade scenario in the end when
4531 + installing pkg A, A deps B & B deps on A. So both B and A are
4532 + installed. Then A's installation is started resulting in an
4533 + uncecessary upgrade */
4534 + if (pkg->state_status == SS_INSTALLED
4535 + && conf->force_reinstall == 0) return 0;
4536 +
4537 + err = verify_pkg_installable(conf, pkg);
4538 + if (err) { return err; }
4539 +
4540 + if (pkg->local_filename == NULL) {
4541 + err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
4542 + if (err) {
4543 + ipkg_message(conf, IPKG_ERROR,
4544 + "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
4545 + pkg->name);
4546 + return err;
4547 + }
4548 + }
4549 +
4550 +/* Check for md5 values */
4551 + if (pkg->md5sum)
4552 + {
4553 + file_md5 = file_md5sum_alloc(pkg->local_filename);
4554 + if (strcmp(file_md5, pkg->md5sum))
4555 + {
4556 + ipkg_message(conf, IPKG_ERROR,
4557 + "Package %s md5sum mismatch. Either the ipkg or the package index are corrupt. Try 'ipkg update'.\n",
4558 + pkg->name);
4559 + free(file_md5);
4560 + return err;
4561 + }
4562 + free(file_md5);
4563 + }
4564 +
4565 + if (pkg->tmp_unpack_dir == NULL) {
4566 + unpack_pkg_control_files(conf, pkg);
4567 + }
4568 +
4569 + /* We should update the filelist here, so that upgrades of packages that split will not fail. -Jamey 27-MAR-03 */
4570 +/* Pigi: check if it will pass from here when replacing. It seems to fail */
4571 +/* That's rather strange that files don't change owner. Investigate !!!!!!*/
4572 + err = update_file_ownership(conf, pkg, old_pkg);
4573 + if (err) { return err; }
4574 +
4575 + if (conf->nodeps == 0) {
4576 + err = satisfy_dependencies_for(conf, pkg);
4577 + if (err) { return err; }
4578 + }
4579 +
4580 + replacees = pkg_vec_alloc();
4581 + pkg_get_installed_replacees(conf, pkg, replacees);
4582 +
4583 + /* this next section we do with SIGINT blocked to prevent inconsistency between ipkg database and filesystem */
4584 + {
4585 + sigset_t newset, oldset;
4586 + sighandler_t old_handler = NULL;
4587 + int use_signal = 0;
4588 + caught_sigint = 0;
4589 + if (use_signal) {
4590 + old_handler = signal(SIGINT, ipkg_install_pkg_sigint_handler);
4591 + } else {
4592 + sigemptyset(&newset);
4593 + sigaddset(&newset, SIGINT);
4594 + sigprocmask(SIG_BLOCK, &newset, &oldset);
4595 + }
4596 +
4597 + ipkg_state_changed++;
4598 + pkg->state_flag |= SF_FILELIST_CHANGED;
4599 +
4600 + /* XXX: BUG: we really should treat replacement more like an upgrade
4601 + * Instead, we're going to remove the replacees
4602 + */
4603 + err = pkg_remove_installed_replacees(conf, replacees);
4604 + if (err) goto UNWIND_REMOVE_INSTALLED_REPLACEES;
4605 +
4606 + err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
4607 + if (err) goto UNWIND_PRERM_UPGRADE_OLD_PKG;
4608 +
4609 + err = prerm_deconfigure_conflictors(conf, pkg, replacees);
4610 + if (err) goto UNWIND_PRERM_DECONFIGURE_CONFLICTORS;
4611 +
4612 + err = preinst_configure(conf, pkg, old_pkg);
4613 + if (err) goto UNWIND_PREINST_CONFIGURE;
4614 +
4615 + err = backup_modified_conffiles(conf, pkg, old_pkg);
4616 + if (err) goto UNWIND_BACKUP_MODIFIED_CONFFILES;
4617 +
4618 + err = check_data_file_clashes(conf, pkg, old_pkg);
4619 + if (err) goto UNWIND_CHECK_DATA_FILE_CLASHES;
4620 +
4621 + err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
4622 + if (err) goto UNWIND_POSTRM_UPGRADE_OLD_PKG;
4623 +
4624 + if (conf->noaction) return 0;
4625 +
4626 + /* point of no return: no unwinding after this */
4627 + if (old_pkg && !conf->force_reinstall) {
4628 + old_pkg->state_want = SW_DEINSTALL;
4629 +
4630 + if (old_pkg->state_flag & SF_NOPRUNE) {
4631 + ipkg_message(conf, IPKG_INFO,
4632 + " not removing obsolesced files because package marked noprune\n");
4633 + } else {
4634 + ipkg_message(conf, IPKG_INFO,
4635 + " removing obsolesced files\n");
4636 + remove_obsolesced_files(conf, pkg, old_pkg);
4637 + }
4638 + /* removing files from old package, to avoid ghost files */
4639 + remove_data_files_and_list(conf, old_pkg);
4640 +/* Pigi : It should be better to remove also maintainer and postrem scripts here, just in case*/
4641 + remove_maintainer_scripts_except_postrm(conf, old_pkg);
4642 + remove_postrm(conf, old_pkg);
4643 +/* Pigi */
4644 +
4645 + }
4646 +
4647 +
4648 + ipkg_message(conf, IPKG_INFO,
4649 + " installing maintainer scripts\n");
4650 + install_maintainer_scripts(conf, pkg, old_pkg);
4651 +
4652 + /* the following just returns 0 */
4653 + remove_disappeared(conf, pkg);
4654 +
4655 + ipkg_message(conf, IPKG_INFO,
4656 + " installing data files\n");
4657 + install_data_files(conf, pkg);
4658 +
4659 +/* read comments from function for detail but I will execute this here as all other tests are ok.*/
4660 + err = check_data_file_clashes_change(conf, pkg, old_pkg);
4661 +
4662 + ipkg_message(conf, IPKG_INFO,
4663 + " resolving conf files\n");
4664 + resolve_conffiles(conf, pkg);
4665 +
4666 + pkg->state_status = SS_UNPACKED;
4667 + old_state_flag = pkg->state_flag;
4668 + pkg->state_flag &= ~SF_PREFER;
4669 + ipkg_message(conf, IPKG_DEBUG, " pkg=%s old_state_flag=%x state_flag=%x\n", pkg->name, old_state_flag, pkg->state_flag);
4670 +
4671 + if (old_pkg && !conf->force_reinstall) {
4672 + old_pkg->state_status = SS_NOT_INSTALLED;
4673 + }
4674 +
4675 + time(&pkg->installed_time);
4676 +
4677 + ipkg_message(conf, IPKG_INFO,
4678 + " cleanup temp files\n");
4679 + cleanup_temporary_files(conf, pkg);
4680 +
4681 + ab_pkg = pkg->parent;
4682 + if (ab_pkg)
4683 + ab_pkg->state_status = pkg->state_status;
4684 +
4685 + ipkg_message(conf, IPKG_INFO, "Done.\n");
4686 +
4687 + if (use_signal)
4688 + signal(SIGINT, old_handler);
4689 + else
4690 + sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4691 +
4692 + return 0;
4693 +
4694 +
4695 + UNWIND_POSTRM_UPGRADE_OLD_PKG:
4696 + postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4697 + UNWIND_CHECK_DATA_FILE_CLASHES:
4698 + check_data_file_clashes_unwind(conf, pkg, old_pkg);
4699 + UNWIND_BACKUP_MODIFIED_CONFFILES:
4700 + backup_modified_conffiles_unwind(conf, pkg, old_pkg);
4701 + UNWIND_PREINST_CONFIGURE:
4702 + preinst_configure_unwind(conf, pkg, old_pkg);
4703 + UNWIND_PRERM_DECONFIGURE_CONFLICTORS:
4704 + prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
4705 + UNWIND_PRERM_UPGRADE_OLD_PKG:
4706 + prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
4707 + UNWIND_REMOVE_INSTALLED_REPLACEES:
4708 + pkg_remove_installed_replacees_unwind(conf, replacees);
4709 +
4710 + ipkg_message(conf, IPKG_INFO,
4711 + " cleanup temp files\n");
4712 + cleanup_temporary_files(conf, pkg);
4713 +
4714 + ipkg_message(conf, IPKG_INFO,
4715 + "Failed.\n");
4716 + if (use_signal)
4717 + signal(SIGINT, old_handler);
4718 + else
4719 + sigprocmask(SIG_UNBLOCK, &newset, &oldset);
4720 +
4721 + return err;
4722 + }
4723 +}
4724 +
4725 +static int prerm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4726 +{
4727 + /* DPKG_INCOMPATIBILITY:
4728 + dpkg does some things here that we don't do yet. Do we care?
4729 +
4730 + 1. If a version of the package is already installed, call
4731 + old-prerm upgrade new-version
4732 + 2. If the script runs but exits with a non-zero exit status
4733 + new-prerm failed-upgrade old-version
4734 + Error unwind, for both the above cases:
4735 + old-postinst abort-upgrade new-version
4736 + */
4737 + return 0;
4738 +}
4739 +
4740 +static int prerm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4741 +{
4742 + /* DPKG_INCOMPATIBILITY:
4743 + dpkg does some things here that we don't do yet. Do we care?
4744 + (See prerm_upgrade_old_package for details)
4745 + */
4746 + return 0;
4747 +}
4748 +
4749 +static int prerm_deconfigure_conflictors(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4750 +{
4751 + /* DPKG_INCOMPATIBILITY:
4752 + dpkg does some things here that we don't do yet. Do we care?
4753 + 2. If a 'conflicting' package is being removed at the same time:
4754 + 1. If any packages depended on that conflicting package and
4755 + --auto-deconfigure is specified, call, for each such package:
4756 + deconfigured's-prerm deconfigure \
4757 + in-favour package-being-installed version \
4758 + removing conflicting-package version
4759 + Error unwind:
4760 + deconfigured's-postinst abort-deconfigure \
4761 + in-favour package-being-installed-but-failed version \
4762 + removing conflicting-package version
4763 +
4764 + The deconfigured packages are marked as requiring
4765 + configuration, so that if --install is used they will be
4766 + configured again if possible.
4767 + 2. To prepare for removal of the conflicting package, call:
4768 + conflictor's-prerm remove in-favour package new-version
4769 + Error unwind:
4770 + conflictor's-postinst abort-remove in-favour package new-version
4771 + */
4772 + return 0;
4773 +}
4774 +
4775 +static int prerm_deconfigure_conflictors_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_vec_t *conflictors)
4776 +{
4777 + /* DPKG_INCOMPATIBILITY: dpkg does some things here that we don't
4778 + do yet. Do we care? (See prerm_deconfigure_conflictors for
4779 + details) */
4780 + return 0;
4781 +}
4782 +
4783 +static int preinst_configure(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4784 +{
4785 + int err;
4786 + char *preinst_args;
4787 +
4788 + if (old_pkg) {
4789 + char *old_version = pkg_version_str_alloc(old_pkg);
4790 + sprintf_alloc(&preinst_args, "upgrade %s", old_version);
4791 + free(old_version);
4792 + } else if (pkg->state_status == SS_CONFIG_FILES) {
4793 + char *pkg_version = pkg_version_str_alloc(pkg);
4794 + sprintf_alloc(&preinst_args, "install %s", pkg_version);
4795 + free(pkg_version);
4796 + } else {
4797 + preinst_args = strdup("install");
4798 + }
4799 +
4800 + err = pkg_run_script(conf, pkg, "preinst", preinst_args);
4801 + if (err) {
4802 + ipkg_message(conf, IPKG_ERROR,
4803 + "Aborting installation of %s\n", pkg->name);
4804 + return 1;
4805 + }
4806 +
4807 + free(preinst_args);
4808 +
4809 + return 0;
4810 +}
4811 +
4812 +static int preinst_configure_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4813 +{
4814 + /* DPKG_INCOMPATIBILITY:
4815 + dpkg does the following error unwind, should we?
4816 + pkg->postrm abort-upgrade old-version
4817 + OR pkg->postrm abort-install old-version
4818 + OR pkg->postrm abort-install
4819 + */
4820 + return 0;
4821 +}
4822 +
4823 +static int backup_modified_conffiles(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4824 +{
4825 + int err;
4826 + conffile_list_elt_t *iter;
4827 + conffile_t *cf;
4828 +
4829 + if (conf->noaction) return 0;
4830 +
4831 + /* Backup all modified conffiles */
4832 + if (old_pkg) {
4833 + for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4834 + char *cf_name;
4835 +
4836 + cf = iter->data;
4837 + cf_name = root_filename_alloc(conf, cf->name);
4838 +
4839 + /* Don't worry if the conffile is just plain gone */
4840 + if (file_exists(cf_name) && conffile_has_been_modified(conf, cf)) {
4841 + err = backup_make_backup(conf, cf_name);
4842 + if (err) {
4843 + return err;
4844 + }
4845 + }
4846 + free(cf_name);
4847 + }
4848 + }
4849 +
4850 + /* Backup all conffiles that were not conffiles in old_pkg */
4851 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4852 + char *cf_name;
4853 + cf = iter->data;
4854 + cf_name = root_filename_alloc(conf, cf->name);
4855 + /* Ignore if this was a conffile in old_pkg as well */
4856 + if (pkg_get_conffile(old_pkg, cf->name)) {
4857 + continue;
4858 + }
4859 +
4860 + if (file_exists(cf_name) && (! backup_exists_for(cf_name))) {
4861 + err = backup_make_backup(conf, cf_name);
4862 + if (err) {
4863 + return err;
4864 + }
4865 + }
4866 + free(cf_name);
4867 + }
4868 +
4869 + return 0;
4870 +}
4871 +
4872 +static int backup_modified_conffiles_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4873 +{
4874 + conffile_list_elt_t *iter;
4875 +
4876 + if (old_pkg) {
4877 + for (iter = old_pkg->conffiles.head; iter; iter = iter->next) {
4878 + backup_remove(iter->data->name);
4879 + }
4880 + }
4881 +
4882 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
4883 + backup_remove(iter->data->name);
4884 + }
4885 +
4886 + return 0;
4887 +}
4888 +
4889 +
4890 +static int check_data_file_clashes(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4891 +{
4892 + /* DPKG_INCOMPATIBILITY:
4893 + ipkg takes a slightly different approach than dpkg at this
4894 + point. dpkg installs each file in the new package while
4895 + creating a backup for any file that is replaced, (so that it
4896 + can unwind if necessary). To avoid complexity and redundant
4897 + storage, ipkg doesn't do any installation until later, (at the
4898 + point at which dpkg removes the backups.
4899 +
4900 + But, we do have to check for data file clashes, since after
4901 + installing a package with a file clash, removing either of the
4902 + packages involved in the clash has the potential to break the
4903 + other package.
4904 + */
4905 + str_list_t *files_list;
4906 + str_list_elt_t *iter;
4907 +
4908 + int clashes = 0;
4909 +
4910 + files_list = pkg_get_installed_files(pkg);
4911 + for (iter = files_list->head; iter; iter = iter->next) {
4912 + char *root_filename;
4913 + char *filename = iter->data;
4914 + root_filename = root_filename_alloc(conf, filename);
4915 + if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
4916 + pkg_t *owner;
4917 + pkg_t *obs;
4918 + /* Pre-existing conffiles are OK */
4919 + /* @@@@ should have way to check that it is a conffile -Jamey */
4920 + if (backup_exists_for(root_filename)) {
4921 + continue;
4922 + }
4923 +
4924 + /* Pre-existing files are OK if force-overwrite was asserted. */
4925 + if (conf->force_overwrite) {
4926 + /* but we need to change who owns this file */
4927 + file_hash_set_file_owner(conf, filename, pkg);
4928 + continue;
4929 + }
4930 +
4931 + owner = file_hash_get_file_owner(conf, filename);
4932 +
4933 + /* Pre-existing files are OK if owned by the pkg being upgraded. */
4934 + if (owner && old_pkg) {
4935 + if (strcmp(owner->name, old_pkg->name) == 0) {
4936 + continue;
4937 + }
4938 + }
4939 +
4940 + /* Pre-existing files are OK if owned by a package replaced by new pkg. */
4941 + if (owner) {
4942 + ipkg_message(conf, IPKG_DEBUG2, "Checking for replaces for %s in package %s\n", filename, owner->name);
4943 + if (pkg_replaces(pkg, owner)) {
4944 + continue;
4945 + }
4946 +/* If the file that would be installed is owned by the same package, ( as per a reinstall or similar )
4947 + then it's ok to overwrite. */
4948 + if (strcmp(owner->name,pkg->name)==0){
4949 + ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
4950 + continue;
4951 + }
4952 + }
4953 +
4954 + /* Pre-existing files are OK if they are obsolete */
4955 + obs = hash_table_get(&conf->obs_file_hash, filename);
4956 + if (obs) {
4957 + ipkg_message(conf, IPKG_INFO, "Pre-exiting file %s is obsolete. obs_pkg=%s\n", filename, obs->name);
4958 + continue;
4959 + }
4960 +
4961 + /* We have found a clash. */
4962 + ipkg_message(conf, IPKG_ERROR,
4963 + "Package %s wants to install file %s\n"
4964 + "\tBut that file is already provided by package ",
4965 + pkg->name, filename);
4966 + if (owner) {
4967 + ipkg_message(conf, IPKG_ERROR,
4968 + "%s\n", owner->name);
4969 + } else {
4970 + ipkg_message(conf, IPKG_ERROR,
4971 + "<no package>\nPlease move this file out of the way and try again.\n");
4972 + }
4973 + clashes++;
4974 + }
4975 + free(root_filename);
4976 + }
4977 + pkg_free_installed_files(pkg);
4978 +
4979 + return clashes;
4980 +}
4981 +
4982 +static int check_data_file_clashes_change(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
4983 +{
4984 + /* Basically that's the worst hack I could do to be able to change ownership of
4985 + file list, but, being that we have no way to unwind the mods, due to structure
4986 + of hash table, probably is the quickest hack too, whishing it would not slow-up thing too much.
4987 + What we do here is change the ownership of file in hash if a replace ( or similar events
4988 + happens )
4989 + Only the action that are needed to change name should be considered.
4990 + @@@ To change after 1.0 release.
4991 + */
4992 + str_list_t *files_list;
4993 + str_list_elt_t *iter;
4994 +
4995 + int clashes = 0;
4996 +
4997 + files_list = pkg_get_installed_files(pkg);
4998 + for (iter = files_list->head; iter; iter = iter->next) {
4999 + char *root_filename;
5000 + char *filename = iter->data;
5001 + root_filename = root_filename_alloc(conf, filename);
5002 + if (file_exists(root_filename) && (! file_is_dir(root_filename))) {
5003 + pkg_t *owner;
5004 +
5005 + if (conf->force_overwrite) {
5006 + /* but we need to change who owns this file */
5007 + file_hash_set_file_owner(conf, filename, pkg);
5008 + continue;
5009 + }
5010 +
5011 + owner = file_hash_get_file_owner(conf, filename);
5012 +
5013 + /* Pre-existing files are OK if owned by a package replaced by new pkg. */
5014 + if (owner) {
5015 + if (pkg_replaces(pkg, owner)) {
5016 +/* It's now time to change the owner of that file.
5017 + It has been "replaced" from the new "Replaces", then I need to inform lists file about that. */
5018 + ipkg_message(conf, IPKG_INFO, "Replacing pre-existing file %s owned by package %s\n", filename, owner->name);
5019 + file_hash_set_file_owner(conf, filename, pkg);
5020 + continue;
5021 + }
5022 + }
5023 +
5024 + }
5025 + free(root_filename);
5026 + }
5027 + pkg_free_installed_files(pkg);
5028 +
5029 + return clashes;
5030 +}
5031 +
5032 +static int check_data_file_clashes_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5033 +{
5034 + /* Nothing to do since check_data_file_clashes doesn't change state */
5035 + return 0;
5036 +}
5037 +
5038 +static int postrm_upgrade_old_pkg(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5039 +{
5040 + /* DPKG_INCOMPATIBILITY: dpkg does the following here, should we?
5041 + 1. If the package is being upgraded, call
5042 + old-postrm upgrade new-version
5043 + 2. If this fails, attempt:
5044 + new-postrm failed-upgrade old-version
5045 + Error unwind, for both cases:
5046 + old-preinst abort-upgrade new-version */
5047 + return 0;
5048 +}
5049 +
5050 +static int postrm_upgrade_old_pkg_unwind(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5051 +{
5052 + /* DPKG_INCOMPATIBILITY:
5053 + dpkg does some things here that we don't do yet. Do we care?
5054 + (See postrm_upgrade_old_pkg for details)
5055 + */
5056 + return 0;
5057 +}
5058 +
5059 +static int remove_obsolesced_files(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5060 +{
5061 + int err;
5062 + str_list_t *old_files;
5063 + str_list_elt_t *of;
5064 + str_list_t *new_files;
5065 + str_list_elt_t *nf;
5066 +
5067 + if (old_pkg == NULL) {
5068 + return 0;
5069 + }
5070 +
5071 + old_files = pkg_get_installed_files(old_pkg);
5072 + new_files = pkg_get_installed_files(pkg);
5073 +
5074 + for (of = old_files->head; of; of = of->next) {
5075 + pkg_t *owner;
5076 + char *old, *new;
5077 + old = of->data;
5078 + for (nf = new_files->head; nf; nf = nf->next) {
5079 + new = nf->data;
5080 + if (strcmp(old, new) == 0) {
5081 + goto NOT_OBSOLETE;
5082 + }
5083 + }
5084 + if (file_is_dir(old)) {
5085 + continue;
5086 + }
5087 + owner = file_hash_get_file_owner(conf, old);
5088 + if (owner != old_pkg) {
5089 + /* in case obsolete file no longer belongs to old_pkg */
5090 + continue;
5091 + }
5092 +
5093 + /* old file is obsolete */
5094 + ipkg_message(conf, IPKG_INFO,
5095 + " removing obsolete file %s\n", old);
5096 + if (!conf->noaction) {
5097 + err = unlink(old);
5098 + if (err) {
5099 + ipkg_message(conf, IPKG_ERROR, " Warning: remove %s failed: %s\n", old,
5100 + strerror(errno));
5101 + }
5102 + }
5103 +
5104 + NOT_OBSOLETE:
5105 + ;
5106 + }
5107 +
5108 + pkg_free_installed_files(old_pkg);
5109 + pkg_free_installed_files(pkg);
5110 +
5111 + return 0;
5112 +}
5113 +
5114 +static int remove_obsolete_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5115 +{
5116 + int i;
5117 + int err = 0;
5118 + char *globpattern;
5119 + glob_t globbuf;
5120 + if (0) {
5121 + if (!pkg->dest) {
5122 + ipkg_message(conf, IPKG_ERROR, "%s: no dest for package %s\n", __FUNCTION__, pkg->name);
5123 + return -1;
5124 + }
5125 + sprintf_alloc(&globpattern, "%s/%s.*", pkg->dest->info_dir, pkg->name);
5126 + err = glob(globpattern, 0, NULL, &globbuf);
5127 + free(globpattern);
5128 + if (err) {
5129 + return err;
5130 + }
5131 + /* XXXX this should perhaps only remove the ones that are not overwritten in new package. Jamey 11/11/2003 */
5132 + for (i = 0; i < globbuf.gl_pathc; i++) {
5133 + ipkg_message(conf, IPKG_DEBUG, "Removing control file %s from old_pkg %s\n",
5134 + globbuf.gl_pathv[i], old_pkg->name);
5135 + if (!conf->noaction)
5136 + unlink(globbuf.gl_pathv[i]);
5137 + }
5138 + globfree(&globbuf);
5139 + }
5140 + return err;
5141 +}
5142 +
5143 +static int install_maintainer_scripts(ipkg_conf_t *conf, pkg_t *pkg, pkg_t *old_pkg)
5144 +{
5145 + int ret;
5146 + char *prefix;
5147 +
5148 + if (old_pkg)
5149 + remove_obsolete_maintainer_scripts(conf, pkg, old_pkg);
5150 + sprintf_alloc(&prefix, "%s.", pkg->name);
5151 + ret = pkg_extract_control_files_to_dir_with_prefix(pkg,
5152 + pkg->dest->info_dir,
5153 + prefix);
5154 + free(prefix);
5155 + return ret;
5156 +}
5157 +
5158 +static int remove_disappeared(ipkg_conf_t *conf, pkg_t *pkg)
5159 +{
5160 + /* DPKG_INCOMPATIBILITY:
5161 + This is a fairly sophisticated dpkg operation. Shall we
5162 + skip it? */
5163 +
5164 + /* Any packages all of whose files have been overwritten during the
5165 + installation, and which aren't required for dependencies, are
5166 + considered to have been removed. For each such package
5167 + 1. disappearer's-postrm disappear overwriter overwriter-version
5168 + 2. The package's maintainer scripts are removed
5169 + 3. It is noted in the status database as being in a sane state,
5170 + namely not installed (any conffiles it may have are ignored,
5171 + rather than being removed by dpkg). Note that disappearing
5172 + packages do not have their prerm called, because dpkg doesn't
5173 + know in advance that the package is going to vanish.
5174 + */
5175 + return 0;
5176 +}
5177 +
5178 +static int install_data_files(ipkg_conf_t *conf, pkg_t *pkg)
5179 +{
5180 + int err;
5181 +
5182 + /* ipkg takes a slightly different approach to data file backups
5183 + than dpkg. Rather than removing backups at this point, we
5184 + actually do the data file installation now. See comments in
5185 + check_data_file_clashes() for more details. */
5186 +
5187 + ipkg_message(conf, IPKG_INFO,
5188 + " extracting data files to %s\n", pkg->dest->root_dir);
5189 + err = pkg_extract_data_files_to_dir(pkg, pkg->dest->root_dir);
5190 + if (err) {
5191 + return err;
5192 + }
5193 +
5194 + /* XXX: BUG or FEATURE : We are actually loosing the Essential flag,
5195 + so we can't save ourself from removing important packages
5196 + At this point we (should) have extracted the .control file, so it
5197 + would be a good idea to reload the data in it, and set the Essential
5198 + state in *pkg. From now on the Essential is back in status file and
5199 + we can protect again.
5200 + We should operate this way:
5201 + fopen the file ( pkg->dest->root_dir/pkg->name.control )
5202 + check for "Essential" in it
5203 + set the value in pkg->essential.
5204 + This new routine could be useful also for every other flag
5205 + Pigi: 16/03/2004 */
5206 + set_flags_from_control(conf, pkg) ;
5207 +
5208 + ipkg_message(conf, IPKG_DEBUG, " Calling pkg_write_filelist from %s\n", __FUNCTION__);
5209 + err = pkg_write_filelist(conf, pkg);
5210 + if (err)
5211 + return err;
5212 +
5213 + /* XXX: FEATURE: ipkg should identify any files which existed
5214 + before installation and which were overwritten, (see
5215 + check_data_file_clashes()). What it must do is remove any such
5216 + files from the filelist of the old package which provided the
5217 + file. Otherwise, if the old package were removed at some point
5218 + it would break the new package. Removing the new package will
5219 + also break the old one, but this cannot be helped since the old
5220 + package's file has already been deleted. This is the importance
5221 + of check_data_file_clashes(), and only allowing ipkg to install
5222 + a clashing package with a user force. */
5223 +
5224 + return 0;
5225 +}
5226 +
5227 +static int resolve_conffiles(ipkg_conf_t *conf, pkg_t *pkg)
5228 +{
5229 + conffile_list_elt_t *iter;
5230 + conffile_t *cf;
5231 + char *cf_backup;
5232 +
5233 + char *md5sum;
5234 +
5235 +
5236 + if (conf->noaction) return 0;
5237 +
5238 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
5239 + char *root_filename;
5240 + cf = iter->data;
5241 + root_filename = root_filename_alloc(conf, cf->name);
5242 +
5243 + /* Might need to initialize the md5sum for each conffile */
5244 + if (cf->value == NULL) {
5245 + cf->value = file_md5sum_alloc(root_filename);
5246 + }
5247 +
5248 + if (!file_exists(root_filename)) {
5249 + free(root_filename);
5250 + continue;
5251 + }
5252 +
5253 + cf_backup = backup_filename_alloc(root_filename);
5254 +
5255 +
5256 + if (file_exists(cf_backup)) {
5257 + /* Let's compute md5 to test if files are changed */
5258 + md5sum = file_md5sum_alloc(cf_backup);
5259 + if (strcmp( cf->value,md5sum) != 0 ) {
5260 + if (conf->force_defaults
5261 + || user_prefers_old_conffile(cf->name, cf_backup) ) {
5262 + rename(cf_backup, root_filename);
5263 + }
5264 + }
5265 + unlink(cf_backup);
5266 + free(md5sum);
5267 + }
5268 +
5269 + free(cf_backup);
5270 + free(root_filename);
5271 + }
5272 +
5273 + return 0;
5274 +}
5275 +
5276 +static int user_prefers_old_conffile(const char *file_name, const char *backup)
5277 +{
5278 + char *response;
5279 + const char *short_file_name;
5280 +
5281 + short_file_name = strrchr(file_name, '/');
5282 + if (short_file_name) {
5283 + short_file_name++;
5284 + } else {
5285 + short_file_name = file_name;
5286 + }
5287 +
5288 + while (1) {
5289 + response = get_user_response(" Configuration file '%s'\n"
5290 + " ==> File on system created by you or by a script.\n"
5291 + " ==> File also in package provided by package maintainer.\n"
5292 + " What would you like to do about it ? Your options are:\n"
5293 + " Y or I : install the package maintainer's version\n"
5294 + " N or O : keep your currently-installed version\n"
5295 + " D : show the differences between the versions (if diff is installed)\n"
5296 + " The default action is to keep your current version.\n"
5297 + " *** %s (Y/I/N/O/D) [default=N] ? ", file_name, short_file_name);
5298 + if (strcmp(response, "y") == 0
5299 + || strcmp(response, "i") == 0
5300 + || strcmp(response, "yes") == 0) {
5301 + free(response);
5302 + return 0;
5303 + }
5304 +
5305 + if (strcmp(response, "d") == 0) {
5306 + char *cmd;
5307 +
5308 + free(response);
5309 + /* XXX: BUG rewrite to use exec or busybox's internal diff */
5310 + sprintf_alloc(&cmd, "diff -u %s %s", backup, file_name);
5311 + xsystem(cmd);
5312 + free(cmd);
5313 + printf(" [Press ENTER to continue]\n");
5314 + response = file_read_line_alloc(stdin);
5315 + free(response);
5316 + continue;
5317 + }
5318 +
5319 + free(response);
5320 + return 1;
5321 + }
5322 +}
5323 +
5324 +/* XXX: CLEANUP: I'd like to move all of the code for
5325 + creating/cleaning pkg->tmp_unpack_dir directly into pkg.c. (Then,
5326 + it would make sense to cleanup pkg->tmp_unpack_dir directly from
5327 + pkg_deinit for example). */
5328 +static int cleanup_temporary_files(ipkg_conf_t *conf, pkg_t *pkg)
5329 +{
5330 + DIR *tmp_dir;
5331 + struct dirent *dirent;
5332 + char *tmp_file;
5333 +
5334 +#ifdef IPKG_DEBUG_NO_TMP_CLEANUP
5335 +#error
5336 + ipkg_message(conf, IPKG_DEBUG,
5337 + "%s: Not cleaning up %s since ipkg compiled with IPKG_DEBUG_NO_TMP_CLEANUP\n",
5338 + __FUNCTION__, pkg->tmp_unpack_dir);
5339 + return 0;
5340 +#endif
5341 +
5342 + if (pkg->tmp_unpack_dir && file_is_dir(pkg->tmp_unpack_dir)) {
5343 + tmp_dir = opendir(pkg->tmp_unpack_dir);
5344 + if (tmp_dir) {
5345 + while (1) {
5346 + dirent = readdir(tmp_dir);
5347 + if (dirent == NULL) {
5348 + break;
5349 + }
5350 + sprintf_alloc(&tmp_file, "%s/%s",
5351 + pkg->tmp_unpack_dir, dirent->d_name);
5352 + if (! file_is_dir(tmp_file)) {
5353 + unlink(tmp_file);
5354 + }
5355 + free(tmp_file);
5356 + }
5357 + closedir(tmp_dir);
5358 + rmdir(pkg->tmp_unpack_dir);
5359 + free(pkg->tmp_unpack_dir);
5360 + pkg->tmp_unpack_dir = NULL;
5361 + }
5362 + }
5363 +
5364 + ipkg_message(conf, IPKG_INFO, "cleanup_temporary_files: pkg=%s local_filename=%s tmp_dir=%s\n",
5365 + pkg->name, pkg->local_filename, conf->tmp_dir);
5366 + if (pkg->local_filename && strncmp(pkg->local_filename, conf->tmp_dir, strlen(conf->tmp_dir)) == 0) {
5367 + unlink(pkg->local_filename);
5368 + free(pkg->local_filename);
5369 + pkg->local_filename = NULL;
5370 + }
5371 +
5372 + return 0;
5373 +}
5374 +
5375 +static char *backup_filename_alloc(const char *file_name)
5376 +{
5377 + char *backup;
5378 +
5379 + sprintf_alloc(&backup, "%s%s", file_name, IPKG_BACKUP_SUFFIX);
5380 +
5381 + return backup;
5382 +}
5383 +
5384 +int backup_make_backup(ipkg_conf_t *conf, const char *file_name)
5385 +{
5386 + int err;
5387 + char *backup;
5388 +
5389 + backup = backup_filename_alloc(file_name);
5390 + err = file_copy(file_name, backup);
5391 + if (err) {
5392 + ipkg_message(conf, IPKG_ERROR,
5393 + "%s: Failed to copy %s to %s\n",
5394 + __FUNCTION__, file_name, backup);
5395 + }
5396 +
5397 + free(backup);
5398 +
5399 + return err;
5400 +}
5401 +
5402 +static int backup_exists_for(const char *file_name)
5403 +{
5404 + int ret;
5405 + char *backup;
5406 +
5407 + backup = backup_filename_alloc(file_name);
5408 +
5409 + ret = file_exists(backup);
5410 +
5411 + free(backup);
5412 +
5413 + return ret;
5414 +}
5415 +
5416 +static int backup_remove(const char *file_name)
5417 +{
5418 + char *backup;
5419 +
5420 + backup = backup_filename_alloc(file_name);
5421 + unlink(backup);
5422 + free(backup);
5423 +
5424 + return 0;
5425 +}
5426 +
5427 +\f
5428 +
5429 +#ifdef CONFIG_IPKG_PROCESS_ACTIONS
5430 +
5431 +int ipkg_remove_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove)
5432 +{
5433 + /* first, remove the packages that need removing */
5434 + for (i = 0 ; i < pkgs_to_remove->len; i++ ) {
5435 + pkg_t *pkg = pkgs_to_remove->pkgs[i];
5436 + err = ipkg_remove_pkg(conf, pkg,0);
5437 + if (err) return err;
5438 + }
5439 + return 0;
5440 +}
5441 +
5442 +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)
5443 +{
5444 + int i;
5445 + /* now one more pass checking on the ones that need to be installed */
5446 + for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5447 + pkg_t *pkg = pkgs_to_install->pkgs[i];
5448 + if (pkg->dest == NULL)
5449 + pkg->dest = conf->default_dest;
5450 +
5451 + pkg->state_want = SW_INSTALL;
5452 +
5453 + /* Abhaya: conflicts check */
5454 + err = check_conflicts_for(conf, pkg);
5455 + if (err) { return err; }
5456 + }
5457 + return 0;
5458 +}
5459 +
5460 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5461 +{
5462 + int i;
5463 + /* now one more pass checking on the ones that need to be installed */
5464 + for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5465 + pkg_t *pkg = pkgs_to_install->pkgs[i];
5466 +
5467 + /* XXX: FEATURE: Need to really support Provides/Replaces: here at some point */
5468 + pkg_vec_t *replacees = pkg_vec_alloc();
5469 + pkg_get_installed_replacees(conf, pkg, replacees);
5470 +
5471 + /* XXX: BUG: we really should treat replacement more like an upgrade
5472 + * Instead, we're going to remove the replacees
5473 + */
5474 + err = pkg_remove_installed_replacees(conf, replacees);
5475 + if (err) return err;
5476 + pkg->state_flag |= SF_REMOVED_REPLACEES;
5477 + }
5478 + return 0;
5479 +}
5480 +
5481 +int ipkg_process_actions_unpack_packages(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_remove, pkg_vec_t *pkgs_to_install)
5482 +{
5483 + int i;
5484 + /* now one more pass checking on the ones that need to be installed */
5485 + for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5486 + pkg_t *pkg = pkgs_to_install->pkgs[i];
5487 + if (pkg->local_filename == NULL) {
5488 + err = ipkg_download_pkg(conf, pkg, conf->tmp_dir);
5489 + if (err) {
5490 + ipkg_message(conf, IPKG_ERROR,
5491 + "Failed to download %s. Perhaps you need to run 'ipkg update'?\n",
5492 + pkg->name);
5493 + return err;
5494 + }
5495 + }
5496 + if (pkg->tmp_unpack_dir == NULL) {
5497 + err = unpack_pkg_control_files(conf, pkg);
5498 + if (err) return err;
5499 + }
5500 + }
5501 + return 0;
5502 +}
5503 +
5504 +int ipkg_process_actions_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5505 +{
5506 + int i;
5507 + /* now one more pass checking on the ones that need to be installed */
5508 + for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5509 + pkg_t *pkg = pkgs_to_install->pkgs[i];
5510 + pkg_t *old_pkg = pkg->old_pkg;
5511 +
5512 + err = prerm_upgrade_old_pkg(conf, pkg, old_pkg);
5513 + if (err) return err;
5514 +
5515 + err = prerm_deconfigure_conflictors(conf, pkg, replacees);
5516 + if (err) return err;
5517 +
5518 + err = preinst_configure(conf, pkg, old_pkg);
5519 + if (err) return err;
5520 +
5521 + err = backup_modified_conffiles(conf, pkg, old_pkg);
5522 + if (err) return err;
5523 +
5524 + err = postrm_upgrade_old_pkg(conf, pkg, old_pkg);
5525 + if (err) return err;
5526 + }
5527 + return 0;
5528 +}
5529 +
5530 +int ipkg_process_actions_install(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5531 +{
5532 + int i;
5533 + /* now one more pass checking on the ones that need to be installed */
5534 + for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5535 + pkg_t *pkg = pkgs_to_install->pkgs[i];
5536 + pkg_t *old_pkg = pkg->old_pkg;
5537 +
5538 + if (old_pkg) {
5539 + old_pkg->state_want = SW_DEINSTALL;
5540 +
5541 + if (old_pkg->state_flag & SF_NOPRUNE) {
5542 + ipkg_message(conf, IPKG_INFO,
5543 + " not removing obsolesced files because package marked noprune\n");
5544 + } else {
5545 + ipkg_message(conf, IPKG_INFO,
5546 + " removing obsolesced files\n");
5547 + remove_obsolesced_files(conf, pkg, old_pkg);
5548 + }
5549 + }
5550 +
5551 + ipkg_message(conf, IPKG_INFO,
5552 + " installing maintainer scripts\n");
5553 + install_maintainer_scripts(conf, pkg, old_pkg);
5554 +
5555 + /* the following just returns 0 */
5556 + remove_disappeared(conf, pkg);
5557 +
5558 + ipkg_message(conf, IPKG_INFO,
5559 + " installing data files\n");
5560 + install_data_files(conf, pkg);
5561 +
5562 + ipkg_message(conf, IPKG_INFO,
5563 + " resolving conf files\n");
5564 + resolve_conffiles(conf, pkg);
5565 +
5566 + pkg->state_status = SS_UNPACKED;
5567 +
5568 + if (old_pkg) {
5569 + old_pkg->state_status = SS_NOT_INSTALLED;
5570 + }
5571 +
5572 + time(&pkg->installed_time);
5573 +
5574 + ipkg_message(conf, IPKG_INFO,
5575 + " cleanup temp files\n");
5576 + cleanup_temporary_files(conf, pkg);
5577 +
5578 + if (pkg->parent)
5579 + pkg->parent->state_status = pkg->state_status;
5580 + }
5581 + return 0;
5582 +}
5583 +
5584 +int ipkg_process_actions_unwind_prerm(ipkg_conf_t *conf, pkg_vec_t *pkgs_to_install)
5585 +{
5586 + int i;
5587 + /* now one more pass checking on the ones that need to be installed */
5588 + for (i = 0 ; i < pkgs_to_install->len; i++ ) {
5589 + pkg_t *pkg = pkgs_to_install->pkgs[i];
5590 + pkg_t *old_pkg = pkg->old_pkg;
5591 +
5592 + if (old_pkg) {
5593 + if (old_pkg->state_flags & SF_POSTRM_UPGRADE)
5594 + postrm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5595 + if (old_pkg->state_flags & SF_CHECK_DATA_FILE_CLASHES)
5596 + check_data_file_clashes_unwind(conf, pkg, old_pkg);
5597 + if (old_pkg->state_flags & SF_BACKUP_MODIFIED_CONFFILES)
5598 + backup_modified_conffiles_unwind(conf, pkg, old_pkg);
5599 + if (old_pkg->state_flags & SF_PREINST_CONFIGURE)
5600 + preinst_configure_unwind(conf, pkg, old_pkg);
5601 + if (old_pkg->state_flags & SF_DECONFIGURE_CONFLICTORS)
5602 + prerm_deconfigure_conflictors_unwind(conf, pkg, replacees);
5603 + if (old_pkg->state_flags & SF_PRERM_UPGRADE)
5604 + prerm_upgrade_old_pkg_unwind(conf, pkg, old_pkg);
5605 +
5606 + if (old_pkg->state_flags & SF_REMOVED_REPLACEES)
5607 + remove_installed_replacees_unwind(conf, pkg, old_pkg);
5608 +
5609 + }
5610 + }
5611 + return 0;
5612 +}
5613 +
5614 +/*
5615 + * Perform all the actions.
5616 + *
5617 + * pkgs_to_remove are packages marked for removal.
5618 + * pkgs_superseded are the old packages being replaced by upgrades.
5619 + *
5620 + * Assumes pkgs_to_install includes all dependences, recursively, sorted in installable order.
5621 + */
5622 +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)
5623 +{
5624 + int err;
5625 + int i;
5626 +
5627 + err = ipkg_remove_packages(conf, pkgs_to_remove);
5628 + if (err) return err;
5629 +
5630 + err = ipkg_process_actions_sanity_check(conf, pkgs_superseded, pkgs_to_install);
5631 + if (err) return err;
5632 +
5633 + err = ipkg_process_actions_remove_replacees(conf, pkgs_to_install);
5634 + if (err) goto UNWIND;
5635 +
5636 + /* @@@@ look at ipkg_install_pkg for handling replacements */
5637 + err = ipkg_process_actions_unpack_packages(conf, pkgs_to_install);
5638 + if (err) goto UNWIND;
5639 +
5640 + /*
5641 + * Now that we have the packages unpacked, we can look for data
5642 + * file clashes. First, we mark the files from the superseded
5643 + * packages as obsolete. Then we scan the files in
5644 + * pkgs_to_install, and only complain about clashes with
5645 + * non-obsolete files.
5646 + */
5647 +
5648 + err = ipkg_process_actions_check_data_file_clashes(conf, pkgs_superseded, pkgs_to_install);
5649 + if (err) goto UNWIND;
5650 +
5651 + /* this was before checking data file clashes */
5652 + err = ipkg_process_actions_prerm(conf, pkgs_superseded, pkgs_to_install);
5653 + if (err) goto UNWIND;
5654 +
5655 + /* point of no return: no unwinding after this */
5656 + err = ipkg_process_actions_install(conf, pkgs_to_install);
5657 + if (err) return err;
5658 +
5659 + ipkg_message(conf, IPKG_INFO, "Done.\n");
5660 + return 0;
5661 +
5662 + UNWIND:
5663 + ipkg_process_actions_unwind(conf, pkgs_to_install);
5664 +
5665 + ipkg_message(conf, IPKG_INFO,
5666 + " cleanup temp files\n");
5667 + cleanup_temporary_files(conf, pkg);
5668 +
5669 + ipkg_message(conf, IPKG_INFO,
5670 + "Failed.\n");
5671 + return err;
5672 +}
5673 +
5674 +#endif
5675 --- /dev/null
5676 +++ b/archival/libipkg/ipkg_install.h
5677 @@ -0,0 +1,35 @@
5678 +/* ipkg_install.h - the itsy package management system
5679 +
5680 + Carl D. Worth
5681 +
5682 + Copyright (C) 2001 University of Southern California
5683 +
5684 + This program is free software; you can redistribute it and/or
5685 + modify it under the terms of the GNU General Public License as
5686 + published by the Free Software Foundation; either version 2, or (at
5687 + your option) any later version.
5688 +
5689 + This program is distributed in the hope that it will be useful, but
5690 + WITHOUT ANY WARRANTY; without even the implied warranty of
5691 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5692 + General Public License for more details.
5693 +*/
5694 +
5695 +#ifndef IPKG_INSTALL_H
5696 +#define IPKG_INSTALL_H
5697 +
5698 +#include "pkg.h"
5699 +#include "ipkg_conf.h"
5700 +
5701 +ipkg_error_t ipkg_install_by_name(ipkg_conf_t *conf, const char *pkg_name);
5702 +ipkg_error_t ipkg_install_multi_by_name(ipkg_conf_t *conf, const char *pkg_name);
5703 +int ipkg_install_from_file(ipkg_conf_t *conf, const char *filename);
5704 +int ipkg_install_pkg(ipkg_conf_t *conf, pkg_t *pkg,int from_upgrading);
5705 +int satisfy_dependencies_for(ipkg_conf_t *conf, pkg_t *pkg);
5706 +
5707 +int ipkg_satisfy_all_dependences(ipkg_conf_t *conf);
5708 +
5709 +int pkg_mark_dependencies_for_installation(ipkg_conf_t *conf, pkg_t *pkg_name, pkg_vec_t *pkgs_needed);
5710 +int name_mark_dependencies_for_installation(ipkg_conf_t *conf, const char *pkg_name, pkg_vec_t *pkgs_needed);
5711 +
5712 +#endif
5713 --- /dev/null
5714 +++ b/archival/libipkg/ipkg_message.c
5715 @@ -0,0 +1,61 @@
5716 +/* ipkg_message.c - the itsy package management system
5717 +
5718 + Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5719 +
5720 + This program is free software; you can redistribute it and/or
5721 + modify it under the terms of the GNU General Public License as
5722 + published by the Free Software Foundation; either version 2, or (at
5723 + your option) any later version.
5724 +
5725 + This program is distributed in the hope that it will be useful, but
5726 + WITHOUT ANY WARRANTY; without even the implied warranty of
5727 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5728 + General Public License for more details.
5729 +*/
5730 +
5731 +
5732 +#include "ipkg.h"
5733 +#include "ipkg_conf.h"
5734 +#include "ipkg_message.h"
5735 +
5736 +#ifndef IPKG_LIB
5737 +
5738 +void
5739 +ipkg_message (ipkg_conf_t * conf, message_level_t level, const char *fmt, ...)
5740 +{
5741 + va_list ap;
5742 +
5743 + if (conf && (conf->verbosity < level))
5744 + {
5745 + return;
5746 + }
5747 + else
5748 + {
5749 +
5750 + va_start (ap, fmt);
5751 + vprintf (fmt, ap);
5752 + va_end (ap);
5753 + }
5754 +}
5755 +
5756 +#else
5757 +
5758 +#include "libipkg.h"
5759 +
5760 +//#define ipkg_message(conf, level, fmt, arg...) ipkg_cb_message(conf, level, fmt, ## arg)
5761 +
5762 +void
5763 +ipkg_message (ipkg_conf_t * conf, message_level_t level, const char *fmt, ...)
5764 +{
5765 + va_list ap;
5766 + char ts[256];
5767 +
5768 + if (ipkg_cb_message)
5769 + {
5770 + va_start (ap, fmt);
5771 + vsnprintf (ts,256,fmt, ap);
5772 + va_end (ap);
5773 + ipkg_cb_message(conf,level,ts);
5774 + }
5775 +}
5776 +#endif
5777 --- /dev/null
5778 +++ b/archival/libipkg/ipkg_message.h
5779 @@ -0,0 +1,32 @@
5780 +/* ipkg_message.h - the itsy package management system
5781 +
5782 + Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5783 +
5784 + This program is free software; you can redistribute it and/or
5785 + modify it under the terms of the GNU General Public License as
5786 + published by the Free Software Foundation; either version 2, or (at
5787 + your option) any later version.
5788 +
5789 + This program is distributed in the hope that it will be useful, but
5790 + WITHOUT ANY WARRANTY; without even the implied warranty of
5791 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5792 + General Public License for more details.
5793 +*/
5794 +
5795 +#ifndef _IPKG_MESSAGE_H_
5796 +#define _IPKG_MESSAGE_H_
5797 +
5798 +#include "ipkg.h"
5799 +#include "ipkg_conf.h"
5800 +
5801 +typedef enum {
5802 + IPKG_ERROR, /* error conditions */
5803 + IPKG_NOTICE, /* normal but significant condition */
5804 + IPKG_INFO, /* informational message */
5805 + IPKG_DEBUG, /* debug level message */
5806 + IPKG_DEBUG2, /* more debug level message */
5807 +} message_level_t;
5808 +
5809 +extern void ipkg_message(ipkg_conf_t *conf, message_level_t level, const char *fmt, ...);
5810 +
5811 +#endif /* _IPKG_MESSAGE_H_ */
5812 --- /dev/null
5813 +++ b/archival/libipkg/ipkg_remove.c
5814 @@ -0,0 +1,383 @@
5815 +/* ipkg_remove.c - the itsy package management system
5816 +
5817 + Carl D. Worth
5818 +
5819 + Copyright (C) 2001 University of Southern California
5820 +
5821 + This program is free software; you can redistribute it and/or
5822 + modify it under the terms of the GNU General Public License as
5823 + published by the Free Software Foundation; either version 2, or (at
5824 + your option) any later version.
5825 +
5826 + This program is distributed in the hope that it will be useful, but
5827 + WITHOUT ANY WARRANTY; without even the implied warranty of
5828 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5829 + General Public License for more details.
5830 +*/
5831 +
5832 +#include "ipkg.h"
5833 +#include "ipkg_message.h"
5834 +
5835 +#include <glob.h>
5836 +
5837 +#include "ipkg_remove.h"
5838 +
5839 +#include "file_util.h"
5840 +#include "sprintf_alloc.h"
5841 +#include "str_util.h"
5842 +
5843 +#include "ipkg_cmd.h"
5844 +
5845 +/*
5846 + * Returns number of the number of packages depending on the packages provided by this package.
5847 + * Every package implicitly provides itself.
5848 + */
5849 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents)
5850 +{
5851 + int nprovides = pkg->provides_count;
5852 + abstract_pkg_t **provides = pkg->provides;
5853 + int n_installed_dependents = 0;
5854 + int i;
5855 + for (i = 0; i <= nprovides; i++) {
5856 + abstract_pkg_t *providee = provides[i];
5857 + abstract_pkg_t **dependers = providee->depended_upon_by;
5858 + abstract_pkg_t *dep_ab_pkg;
5859 + if (dependers == NULL)
5860 + continue;
5861 + while ((dep_ab_pkg = *dependers++) != NULL) {
5862 + if (dep_ab_pkg->state_status == SS_INSTALLED){
5863 + n_installed_dependents++;
5864 + }
5865 + }
5866 +
5867 + }
5868 + /* if caller requested the set of installed dependents */
5869 + if (pdependents) {
5870 + int p = 0;
5871 + abstract_pkg_t **dependents = (abstract_pkg_t **)malloc((n_installed_dependents+1)*sizeof(abstract_pkg_t *));
5872 +
5873 + if ( dependents == NULL ){
5874 + fprintf(stderr,"%s Unable to allocate memory. REPORT THIS BUG IN BUGZILLA PLEASE\n", __FUNCTION__);
5875 + return -1;
5876 + }
5877 +
5878 + *pdependents = dependents;
5879 + for (i = 0; i <= nprovides; i++) {
5880 + abstract_pkg_t *providee = provides[i];
5881 + abstract_pkg_t **dependers = providee->depended_upon_by;
5882 + abstract_pkg_t *dep_ab_pkg;
5883 + if (dependers == NULL)
5884 + continue;
5885 + while ((dep_ab_pkg = *dependers++) != NULL) {
5886 + if (dep_ab_pkg->state_status == SS_INSTALLED && !(dep_ab_pkg->state_flag & SF_MARKED)) {
5887 + dependents[p++] = dep_ab_pkg;
5888 + dep_ab_pkg->state_flag |= SF_MARKED;
5889 + }
5890 + }
5891 + }
5892 + dependents[p] = NULL;
5893 + /* now clear the marks */
5894 + for (i = 0; i < p; i++) {
5895 + abstract_pkg_t *dep_ab_pkg = dependents[i];
5896 + dep_ab_pkg->state_flag &= ~SF_MARKED;
5897 + }
5898 + }
5899 + return n_installed_dependents;
5900 +}
5901 +
5902 +int ipkg_remove_dependent_pkgs (ipkg_conf_t *conf, pkg_t *pkg, abstract_pkg_t **dependents)
5903 +{
5904 + int i;
5905 + int a;
5906 + int count;
5907 + pkg_vec_t *dependent_pkgs = pkg_vec_alloc();
5908 + abstract_pkg_t * ab_pkg;
5909 +
5910 + if((ab_pkg = pkg->parent) == NULL){
5911 + fprintf(stderr, "%s: unable to get dependent pkgs. pkg %s isn't in hash table\n",
5912 + __FUNCTION__, pkg->name);
5913 + return 0;
5914 + }
5915 +
5916 + if (dependents == NULL)
5917 + return 0;
5918 +
5919 + // here i am using the dependencies_checked
5920 + if (ab_pkg->dependencies_checked == 2) // variable to make out whether this package
5921 + return 0; // has already been encountered in the process
5922 + // of marking packages for removal - Karthik
5923 + ab_pkg->dependencies_checked = 2;
5924 +
5925 + i = 0;
5926 + count = 1;
5927 + while (dependents [i] != NULL) {
5928 + abstract_pkg_t *dep_ab_pkg = dependents[i];
5929 +
5930 + if (dep_ab_pkg->dependencies_checked == 2){
5931 + i++;
5932 + continue;
5933 + }
5934 + if (dep_ab_pkg->state_status == SS_INSTALLED) {
5935 + for (a = 0; a < dep_ab_pkg->pkgs->len; a++) {
5936 + pkg_t *dep_pkg = dep_ab_pkg->pkgs->pkgs[a];
5937 + if (dep_pkg->state_status == SS_INSTALLED) {
5938 + pkg_vec_insert(dependent_pkgs, dep_pkg);
5939 + count++;
5940 + }
5941 + }
5942 + }
5943 + i++;
5944 + /* 1 - to keep track of visited ab_pkgs when checking for possiblility of a broken removal of pkgs.
5945 + * 2 - to keep track of pkgs whose deps have been checked alrdy - Karthik */
5946 + }
5947 +
5948 + if (count == 1)
5949 + return 0;
5950 +
5951 +
5952 + for (i = 0; i < dependent_pkgs->len; i++) {
5953 + int err = ipkg_remove_pkg(conf, dependent_pkgs->pkgs[i],0);
5954 + if (err)
5955 + return err;
5956 + }
5957 + return 0;
5958 +}
5959 +
5960 +static int user_prefers_removing_dependents(ipkg_conf_t *conf, abstract_pkg_t *abpkg, pkg_t *pkg, abstract_pkg_t **dependents)
5961 +{
5962 + abstract_pkg_t *dep_ab_pkg;
5963 + ipkg_message(conf, IPKG_ERROR, "Package %s is depended upon by packages:\n", pkg->name);
5964 + while ((dep_ab_pkg = *dependents++) != NULL) {
5965 + if (dep_ab_pkg->state_status == SS_INSTALLED)
5966 + ipkg_message(conf, IPKG_ERROR, "\t%s\n", dep_ab_pkg->name);
5967 + }
5968 + ipkg_message(conf, IPKG_ERROR, "These might cease to work if package %s is removed.\n\n", pkg->name);
5969 + ipkg_message(conf, IPKG_ERROR, "");
5970 + ipkg_message(conf, IPKG_ERROR, "You can force removal of this package with -force-depends.\n");
5971 + ipkg_message(conf, IPKG_ERROR, "You can force removal of this package and its dependents\n");
5972 + ipkg_message(conf, IPKG_ERROR, "with -force-removal-of-dependent-packages or -recursive\n");
5973 + ipkg_message(conf, IPKG_ERROR, "or by setting option force_removal_of_dependent_packages\n");
5974 + ipkg_message(conf, IPKG_ERROR, "in ipkg.conf.\n");
5975 + return 0;
5976 +}
5977 +
5978 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message)
5979 +{
5980 +/* Actually, when "message == 1" I have been called from an upgrade, and not from a normal remove
5981 + thus I wan't check for essential, as I'm upgrading.
5982 + I hope it won't break anything :)
5983 +*/
5984 + int err;
5985 + abstract_pkg_t *parent_pkg = NULL;
5986 +
5987 + if (pkg->essential && !message) {
5988 + if (conf->force_removal_of_essential_packages) {
5989 + fprintf(stderr, "WARNING: Removing essential package %s under your coercion.\n"
5990 + "\tIf your system breaks, you get to keep both pieces\n",
5991 + pkg->name);
5992 + } else {
5993 + fprintf(stderr, "ERROR: Refusing to remove essential package %s.\n"
5994 + "\tRemoving an essential package may lead to an unusable system, but if\n"
5995 + "\tyou enjoy that kind of pain, you can force ipkg to proceed against\n"
5996 + "\tits will with the option: -force-removal-of-essential-packages\n",
5997 + pkg->name);
5998 + return IPKG_PKG_IS_ESSENTIAL;
5999 + }
6000 + }
6001 +
6002 + if ((parent_pkg = pkg->parent) == NULL)
6003 + return 0;
6004 +
6005 + /* only attempt to remove dependent installed packages if
6006 + * force_depends is not specified or the package is being
6007 + * replaced.
6008 + */
6009 + if (!conf->force_depends
6010 + && !(pkg->state_flag & SF_REPLACE)) {
6011 + abstract_pkg_t **dependents;
6012 + int has_installed_dependents =
6013 + pkg_has_installed_dependents(conf, parent_pkg, pkg, &dependents);
6014 +
6015 + if (has_installed_dependents) {
6016 + /*
6017 + * if this package is depended up by others, then either we should
6018 + * not remove it or we should remove it and all of its dependents
6019 + */
6020 +
6021 + if (!conf->force_removal_of_dependent_packages
6022 + && !user_prefers_removing_dependents(conf, parent_pkg, pkg, dependents)) {
6023 + return IPKG_PKG_HAS_DEPENDENTS;
6024 + }
6025 +
6026 + /* remove packages depending on this package - Karthik */
6027 + err = ipkg_remove_dependent_pkgs (conf, pkg, dependents);
6028 + free(dependents);
6029 + if (err) return err;
6030 + }
6031 + }
6032 +
6033 + if ( message==0 ){
6034 + printf("Removing package %s from %s...\n", pkg->name, pkg->dest->name);
6035 + fflush(stdout);
6036 + }
6037 + pkg->state_flag |= SF_FILELIST_CHANGED;
6038 +
6039 + pkg->state_want = SW_DEINSTALL;
6040 + ipkg_state_changed++;
6041 +
6042 + pkg_run_script(conf, pkg, "prerm", "remove");
6043 +
6044 + /* DPKG_INCOMPATIBILITY: dpkg is slightly different here. It
6045 + maintains an empty filelist rather than deleting it. That seems
6046 + like a big pain, and I don't see that that should make a big
6047 + difference, but for anyone who wants tighter compatibility,
6048 + feel free to fix this. */
6049 + remove_data_files_and_list(conf, pkg);
6050 +
6051 + pkg_run_script(conf, pkg, "postrm", "remove");
6052 +
6053 + remove_maintainer_scripts_except_postrm(conf, pkg);
6054 +
6055 + /* Aman Gupta - Since ipkg is made for handheld devices with limited
6056 + * space, it doesn't make sense to leave extra configurations, files,
6057 + * and maintainer scripts left around. So, we make remove like purge,
6058 + * and take out all the crap :) */
6059 +
6060 + remove_postrm(conf, pkg);
6061 + pkg->state_status = SS_NOT_INSTALLED;
6062 +
6063 + if (parent_pkg)
6064 + parent_pkg->state_status = SS_NOT_INSTALLED;
6065 +
6066 + return 0;
6067 +}
6068 +
6069 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg)
6070 +{
6071 + ipkg_remove_pkg(conf, pkg,0);
6072 + return 0;
6073 +}
6074 +
6075 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg)
6076 +{
6077 + str_list_t installed_dirs;
6078 + str_list_t *installed_files;
6079 + str_list_elt_t *iter;
6080 + char *file_name;
6081 + conffile_t *conffile;
6082 + int removed_a_dir;
6083 + pkg_t *owner;
6084 +
6085 + str_list_init(&installed_dirs);
6086 + installed_files = pkg_get_installed_files(pkg);
6087 +
6088 + for (iter = installed_files->head; iter; iter = iter->next) {
6089 + file_name = iter->data;
6090 +
6091 + if (file_is_dir(file_name)) {
6092 + str_list_append(&installed_dirs, strdup(file_name));
6093 + continue;
6094 + }
6095 +
6096 + conffile = pkg_get_conffile(pkg, file_name);
6097 + if (conffile) {
6098 + /* XXX: QUESTION: Is this right? I figure we only need to
6099 + save the conffile if it has been modified. Is that what
6100 + dpkg does? Or does dpkg preserve all conffiles? If so,
6101 + this seems like a better thing to do to conserve
6102 + space. */
6103 + if (conffile_has_been_modified(conf, conffile)) {
6104 + printf(" not deleting modified conffile %s\n", file_name);
6105 + fflush(stdout);
6106 + continue;
6107 + }
6108 + }
6109 +
6110 + ipkg_message(conf, IPKG_INFO, " deleting %s (noaction=%d)\n", file_name, conf->noaction);
6111 + if (!conf->noaction)
6112 + unlink(file_name);
6113 + }
6114 +
6115 + if (!conf->noaction) {
6116 + do {
6117 + removed_a_dir = 0;
6118 + for (iter = installed_dirs.head; iter; iter = iter->next) {
6119 + file_name = iter->data;
6120 +
6121 + if (rmdir(file_name) == 0) {
6122 + ipkg_message(conf, IPKG_INFO, " deleting %s\n", file_name);
6123 + removed_a_dir = 1;
6124 + str_list_remove(&installed_dirs, &iter);
6125 + }
6126 + }
6127 + } while (removed_a_dir);
6128 + }
6129 +
6130 + pkg_free_installed_files(pkg);
6131 + /* We have to remove the file list now, so that
6132 + find_pkg_owning_file does not always just report this package */
6133 + pkg_remove_installed_files_list(conf, pkg);
6134 +
6135 + /* Don't print warning for dirs that are provided by other packages */
6136 + for (iter = installed_dirs.head; iter; iter = iter->next) {
6137 + file_name = iter->data;
6138 +
6139 + owner = file_hash_get_file_owner(conf, file_name);
6140 + if (owner) {
6141 + free(iter->data);
6142 + iter->data = NULL;
6143 + str_list_remove(&installed_dirs, &iter);
6144 + }
6145 + }
6146 +
6147 + /* cleanup */
6148 + for (iter = installed_dirs.head; iter; iter = iter->next) {
6149 + free(iter->data);
6150 + iter->data = NULL;
6151 + }
6152 + str_list_deinit(&installed_dirs);
6153 +
6154 + return 0;
6155 +}
6156 +
6157 +int remove_maintainer_scripts_except_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6158 +{
6159 + int i, err;
6160 + char *globpattern;
6161 + glob_t globbuf;
6162 +
6163 + if (conf->noaction) return 0;
6164 +
6165 + sprintf_alloc(&globpattern, "%s/%s.*",
6166 + pkg->dest->info_dir, pkg->name);
6167 + err = glob(globpattern, 0, NULL, &globbuf);
6168 + free(globpattern);
6169 + if (err) {
6170 + return 0;
6171 + }
6172 +
6173 + for (i = 0; i < globbuf.gl_pathc; i++) {
6174 + if (str_ends_with(globbuf.gl_pathv[i], ".postrm")) {
6175 + continue;
6176 + }
6177 + ipkg_message(conf, IPKG_INFO, " deleting %s\n", globbuf.gl_pathv[i]);
6178 + unlink(globbuf.gl_pathv[i]);
6179 + }
6180 + globfree(&globbuf);
6181 +
6182 + return 0;
6183 +}
6184 +
6185 +int remove_postrm(ipkg_conf_t *conf, pkg_t *pkg)
6186 +{
6187 + char *postrm_file_name;
6188 +
6189 + if (conf->noaction) return 0;
6190 +
6191 + sprintf_alloc(&postrm_file_name, "%s/%s.postrm",
6192 + pkg->dest->info_dir, pkg->name);
6193 + unlink(postrm_file_name);
6194 + free(postrm_file_name);
6195 +
6196 + return 0;
6197 +}
6198 --- /dev/null
6199 +++ b/archival/libipkg/ipkg_remove.h
6200 @@ -0,0 +1,33 @@
6201 +/* ipkg_remove.h - the itsy package management system
6202 +
6203 + Carl D. Worth
6204 +
6205 + Copyright (C) 2001 University of Southern California
6206 +
6207 + This program is free software; you can redistribute it and/or
6208 + modify it under the terms of the GNU General Public License as
6209 + published by the Free Software Foundation; either version 2, or (at
6210 + your option) any later version.
6211 +
6212 + This program is distributed in the hope that it will be useful, but
6213 + WITHOUT ANY WARRANTY; without even the implied warranty of
6214 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6215 + General Public License for more details.
6216 +*/
6217 +
6218 +#ifndef IPKG_REMOVE_H
6219 +#define IPKG_REMOVE_H
6220 +
6221 +#include "pkg.h"
6222 +#include "ipkg_conf.h"
6223 +
6224 +int ipkg_remove_pkg(ipkg_conf_t *conf, pkg_t *pkg,int message);
6225 +int ipkg_purge_pkg(ipkg_conf_t *conf, pkg_t *pkg);
6226 +int possible_broken_removal_of_packages (ipkg_conf_t *conf, pkg_t *pkg);
6227 +int pkg_has_installed_dependents(ipkg_conf_t *conf, abstract_pkg_t *parent_apkg, pkg_t *pkg, abstract_pkg_t *** pdependents);
6228 +int remove_data_files_and_list(ipkg_conf_t *conf, pkg_t *pkg);
6229 +int remove_maintainer_scripts_except_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6230 +int remove_postrm (ipkg_conf_t *conf, pkg_t *pkg);
6231 +
6232 +
6233 +#endif
6234 --- /dev/null
6235 +++ b/archival/libipkg/ipkg_upgrade.c
6236 @@ -0,0 +1,77 @@
6237 +/* ipkg_upgrade.c - the itsy package management system
6238 +
6239 + Carl D. Worth
6240 + Copyright (C) 2001 University of Southern California
6241 +
6242 + Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6243 +
6244 + This program is free software; you can redistribute it and/or
6245 + modify it under the terms of the GNU General Public License as
6246 + published by the Free Software Foundation; either version 2, or (at
6247 + your option) any later version.
6248 +
6249 + This program is distributed in the hope that it will be useful, but
6250 + WITHOUT ANY WARRANTY; without even the implied warranty of
6251 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6252 + General Public License for more details.
6253 +*/
6254 +
6255 +#include "ipkg.h"
6256 +#include "ipkg_install.h"
6257 +#include "ipkg_message.h"
6258 +
6259 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old)
6260 +{
6261 + pkg_t *new;
6262 + int cmp;
6263 + char *old_version, *new_version;
6264 +
6265 + if (old->state_flag & SF_HOLD) {
6266 + ipkg_message(conf, IPKG_NOTICE,
6267 + "Not upgrading package %s which is marked "
6268 + "hold (flags=%#x)\n", old->name, old->state_flag);
6269 + return 0;
6270 + }
6271 +
6272 + new = pkg_hash_fetch_best_installation_candidate_by_name(conf, old->name);
6273 + if (new == NULL) {
6274 + old_version = pkg_version_str_alloc(old);
6275 + ipkg_message(conf, IPKG_NOTICE,
6276 + "Assuming locally installed package %s (%s) "
6277 + "is up to date.\n", old->name, old_version);
6278 + free(old_version);
6279 + return 0;
6280 + }
6281 +
6282 + old_version = pkg_version_str_alloc(old);
6283 + new_version = pkg_version_str_alloc(new);
6284 +
6285 + cmp = pkg_compare_versions(old, new);
6286 + ipkg_message(conf, IPKG_DEBUG,
6287 + "comparing visible versions of pkg %s:"
6288 + "\n\t%s is installed "
6289 + "\n\t%s is available "
6290 + "\n\t%d was comparison result\n",
6291 + old->name, old_version, new_version, cmp);
6292 + if (cmp == 0) {
6293 + ipkg_message(conf, IPKG_INFO,
6294 + "Package %s (%s) installed in %s is up to date.\n",
6295 + old->name, old_version, old->dest->name);
6296 + free(old_version);
6297 + free(new_version);
6298 + return 0;
6299 + } else if (cmp > 0) {
6300 + ipkg_message(conf, IPKG_NOTICE,
6301 + "Not downgrading package %s on %s from %s to %s.\n",
6302 + old->name, old->dest->name, old_version, new_version);
6303 + free(old_version);
6304 + free(new_version);
6305 + return 0;
6306 + } else if (cmp < 0) {
6307 + new->dest = old->dest;
6308 + old->state_want = SW_DEINSTALL;
6309 + }
6310 +
6311 + new->state_flag |= SF_USER;
6312 + return ipkg_install_pkg(conf, new,1);
6313 +}
6314 --- /dev/null
6315 +++ b/archival/libipkg/ipkg_upgrade.h
6316 @@ -0,0 +1,18 @@
6317 +/* ipkg_upgrade.c - the itsy package management system
6318 +
6319 + Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
6320 +
6321 + This program is free software; you can redistribute it and/or
6322 + modify it under the terms of the GNU General Public License as
6323 + published by the Free Software Foundation; either version 2, or (at
6324 + your option) any later version.
6325 +
6326 + This program is distributed in the hope that it will be useful, but
6327 + WITHOUT ANY WARRANTY; without even the implied warranty of
6328 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6329 + General Public License for more details.
6330 +*/
6331 +
6332 +#include "ipkg.h"
6333 +
6334 +int ipkg_upgrade_pkg(ipkg_conf_t *conf, pkg_t *old);
6335 --- /dev/null
6336 +++ b/archival/libipkg/ipkg_utils.c
6337 @@ -0,0 +1,181 @@
6338 +/* ipkg_utils.c - the itsy package management system
6339 +
6340 + Steven M. Ayer
6341 +
6342 + Copyright (C) 2002 Compaq Computer Corporation
6343 +
6344 + This program is free software; you can redistribute it and/or
6345 + modify it under the terms of the GNU General Public License as
6346 + published by the Free Software Foundation; either version 2, or (at
6347 + your option) any later version.
6348 +
6349 + This program is distributed in the hope that it will be useful, but
6350 + WITHOUT ANY WARRANTY; without even the implied warranty of
6351 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6352 + General Public License for more details.
6353 +*/
6354 +
6355 +#include "ipkg.h"
6356 +#include <errno.h>
6357 +#include <ctype.h>
6358 +#include <sys/vfs.h>
6359 +
6360 +#include "ipkg_utils.h"
6361 +#include "pkg.h"
6362 +#include "pkg_hash.h"
6363 +
6364 +struct errlist* error_list;
6365 +
6366 +int get_available_blocks(char * filesystem)
6367 +{
6368 + struct statfs sfs;
6369 +
6370 + if(statfs(filesystem, &sfs)){
6371 + fprintf(stderr, "bad statfs\n");
6372 + return 0;
6373 + }
6374 + /* fprintf(stderr, "reported fs type %x\n", sfs.f_type); */
6375 + return ((sfs.f_bavail * sfs.f_bsize) / 1024);
6376 +}
6377 +
6378 +char **read_raw_pkgs_from_file(const char *file_name)
6379 +{
6380 + FILE *fp;
6381 + char **ret;
6382 +
6383 + if(!(fp = fopen(file_name, "r"))){
6384 + fprintf(stderr, "can't get %s open for read\n", file_name);
6385 + return NULL;
6386 + }
6387 +
6388 + ret = read_raw_pkgs_from_stream(fp);
6389 +
6390 + fclose(fp);
6391 +
6392 + return ret;
6393 +}
6394 +
6395 +char **read_raw_pkgs_from_stream(FILE *fp)
6396 +{
6397 + char **raw = NULL, *buf, *scout;
6398 + int count = 0;
6399 + size_t size = 512;
6400 +
6401 + buf = malloc (size);
6402 +
6403 + while (fgets(buf, size, fp)) {
6404 + while (strlen (buf) == (size - 1)
6405 + && buf[size-2] != '\n') {
6406 + size_t o = size - 1;
6407 + size *= 2;
6408 + buf = realloc (buf, size);
6409 + if (fgets (buf + o, size - o, fp) == NULL)
6410 + break;
6411 + }
6412 +
6413 + if(!(count % 50))
6414 + raw = realloc(raw, (count + 50) * sizeof(char *));
6415 +
6416 + if((scout = strchr(buf, '\n')))
6417 + *scout = '\0';
6418 +
6419 + raw[count++] = strdup(buf);
6420 + }
6421 +
6422 + raw = realloc(raw, (count + 1) * sizeof(char *));
6423 + raw[count] = NULL;
6424 +
6425 + free (buf);
6426 +
6427 + return raw;
6428 +}
6429 +
6430 +/* something to remove whitespace, a hash pooper */
6431 +char *trim_alloc(char *line)
6432 +{
6433 + char *new;
6434 + char *dest, *src, *end;
6435 +
6436 + new = malloc(strlen(line) + 1);
6437 + if ( new == NULL ){
6438 + fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
6439 + return NULL;
6440 + }
6441 + dest = new, src = line, end = line + (strlen(line) - 1);
6442 +
6443 + /* remove it from the front */
6444 + while(src &&
6445 + isspace(*src) &&
6446 + *src)
6447 + src++;
6448 + /* and now from the back */
6449 + while((end > src) &&
6450 + isspace(*end))
6451 + end--;
6452 + end++;
6453 + *end = '\0';
6454 + strcpy(new, src);
6455 + /* this does from the first space
6456 + * blasting away any versions stuff in depends
6457 + while(src &&
6458 + !isspace(*src) &&
6459 + *src)
6460 + *dest++ = *src++;
6461 + *dest = '\0';
6462 + */
6463 +
6464 + return new;
6465 +}
6466 +
6467 +int line_is_blank(const char *line)
6468 +{
6469 + const char *s;
6470 +
6471 + for (s = line; *s; s++) {
6472 + if (!isspace(*s))
6473 + return 0;
6474 + }
6475 + return 1;
6476 +}
6477 +
6478 +void push_error_list(struct errlist ** errors, char * msg){
6479 + struct errlist *err_lst_tmp;
6480 +
6481 +
6482 + err_lst_tmp = malloc ( sizeof (err_lst_tmp) );
6483 + err_lst_tmp->errmsg=strdup(msg) ;
6484 + err_lst_tmp->next = *errors;
6485 + *errors = err_lst_tmp;
6486 +}
6487 +
6488 +
6489 +void reverse_error_list(struct errlist **errors){
6490 + struct errlist *result=NULL;
6491 + struct errlist *current= *errors;
6492 + struct errlist *next;
6493 +
6494 + while ( current != NULL ) {
6495 + next = current->next;
6496 + current->next=result;
6497 + result=current;
6498 + current=next;
6499 + }
6500 + *errors=result;
6501 +
6502 +}
6503 +
6504 +
6505 +void free_error_list(struct errlist **errors){
6506 + struct errlist *current = *errors;
6507 +
6508 + while (current != NULL) {
6509 + free(current->errmsg);
6510 + current = (*errors)->next;
6511 + free(*errors);
6512 + *errors = current;
6513 + }
6514 +
6515 +
6516 +}
6517 +
6518 +
6519 --- /dev/null
6520 +++ b/archival/libipkg/ipkg_utils.h
6521 @@ -0,0 +1,29 @@
6522 +/* ipkg_utils.h - the itsy package management system
6523 +
6524 + Steven M. Ayer
6525 +
6526 + Copyright (C) 2002 Compaq Computer Corporation
6527 +
6528 + This program is free software; you can redistribute it and/or
6529 + modify it under the terms of the GNU General Public License as
6530 + published by the Free Software Foundation; either version 2, or (at
6531 + your option) any later version.
6532 +
6533 + This program is distributed in the hope that it will be useful, but
6534 + WITHOUT ANY WARRANTY; without even the implied warranty of
6535 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6536 + General Public License for more details.
6537 +*/
6538 +
6539 +#ifndef IPKG_UTILS_H
6540 +#define IPKG_UTILS_H
6541 +
6542 +#include "pkg.h"
6543 +
6544 +int get_available_blocks(char * filesystem);
6545 +char **read_raw_pkgs_from_file(const char *file_name);
6546 +char **read_raw_pkgs_from_stream(FILE *fp);
6547 +char *trim_alloc(char * line);
6548 +int line_is_blank(const char *line);
6549 +
6550 +#endif
6551 --- /dev/null
6552 +++ b/archival/libipkg/Kbuild
6553 @@ -0,0 +1,60 @@
6554 +# Makefile for busybox
6555 +#
6556 +# Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6557 +# Copyright (C) 2006 OpenWrt.org
6558 +#
6559 +# Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6560 +
6561 +LIBIPKG_CORE_OBJS:= \
6562 + args.o \
6563 + libipkg.o \
6564 + user.o \
6565 +
6566 +LIBIPKG_CMD_OBJS:= \
6567 + ipkg_cmd.o \
6568 + ipkg_configure.o \
6569 + ipkg_download.o \
6570 + ipkg_install.o \
6571 + ipkg_remove.o \
6572 + ipkg_upgrade.o \
6573 +
6574 +LIBIPKG_DB_OBJS:= \
6575 + hash_table.o \
6576 + ipkg_conf.o \
6577 + ipkg_utils.o \
6578 + pkg.o \
6579 + pkg_depends.o \
6580 + pkg_extract.o \
6581 + pkg_hash.o \
6582 + pkg_parse.o \
6583 + pkg_vec.o \
6584 +
6585 +LIBIPKG_LIST_OBJS:= \
6586 + conffile.o \
6587 + conffile_list.o \
6588 + nv_pair.o \
6589 + nv_pair_list.o \
6590 + pkg_dest.o \
6591 + pkg_dest_list.o \
6592 + pkg_src.o \
6593 + pkg_src_list.o \
6594 + str_list.o \
6595 + void_list.o \
6596 +
6597 +LIBIPKG_UTIL_OBJS:= \
6598 + file_util.o \
6599 + ipkg_message.o \
6600 + str_util.o \
6601 + xsystem.o \
6602 +
6603 +lib-y :=
6604 +lib-$(CONFIG_IPKG) += $(LIBIPKG_CORE_OBJS)
6605 +lib-$(CONFIG_IPKG) += $(LIBIPKG_CMD_OBJS)
6606 +lib-$(CONFIG_IPKG) += $(LIBIPKG_DB_OBJS)
6607 +lib-$(CONFIG_IPKG) += $(LIBIPKG_LIST_OBJS)
6608 +lib-$(CONFIG_IPKG) += $(LIBIPKG_UTIL_OBJS)
6609 +
6610 +ifeq ($(strip $(IPKG_ARCH)),)
6611 +IPKG_ARCH:=$(TARGET_ARCH)
6612 +endif
6613 +CFLAGS += -DIPKG_LIB -DIPKGLIBDIR="\"/usr/lib\"" -DHOST_CPU_STR="\"$(IPKG_ARCH)\""
6614 --- /dev/null
6615 +++ b/archival/libipkg/libipkg.c
6616 @@ -0,0 +1,527 @@
6617 +/* ipkglib.c - the itsy package management system
6618 +
6619 + Florina Boor
6620 +
6621 + Copyright (C) 2003 kernel concepts
6622 +
6623 + This program is free software; you can redistribute it and/or
6624 + modify it under the terms of the GNU General Public License as
6625 + published by the Free Software Foundation; either version 2, or (at
6626 + your option) any later version.
6627 +
6628 + This program is distributed in the hope that it will be useful, but
6629 + WITHOUT ANY WARRANTY; without even the implied warranty of
6630 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6631 + General Public License for more details.
6632 +*/
6633 +
6634 +#ifdef IPKG_LIB
6635 +
6636 +#include "ipkg.h"
6637 +#include "ipkg_includes.h"
6638 +#include "libipkg.h"
6639 +
6640 +#include "args.h"
6641 +#include "ipkg_conf.h"
6642 +#include "ipkg_cmd.h"
6643 +#include "file_util.h"
6644 +
6645 +
6646 +
6647 +ipkg_message_callback ipkg_cb_message = NULL;
6648 +ipkg_response_callback ipkg_cb_response = NULL;
6649 +ipkg_status_callback ipkg_cb_status = NULL;
6650 +ipkg_list_callback ipkg_cb_list = NULL;
6651 +
6652 +
6653 +int
6654 +ipkg_init (ipkg_message_callback mcall,
6655 + ipkg_response_callback rcall,
6656 + args_t * args)
6657 +{
6658 + ipkg_cb_message = mcall;
6659 + ipkg_cb_response = rcall;
6660 +
6661 + args_init (args);
6662 +
6663 + return 0;
6664 +}
6665 +
6666 +
6667 +int
6668 +ipkg_deinit (args_t * args)
6669 +{
6670 + args_deinit (args);
6671 + ipkg_cb_message = NULL;
6672 + ipkg_cb_response = NULL;
6673 +
6674 + /* place other cleanup stuff here */
6675 +
6676 + return 0;
6677 +}
6678 +
6679 +
6680 +int
6681 +ipkg_packages_list(args_t *args,
6682 + const char *packages,
6683 + ipkg_list_callback cblist,
6684 + void *userdata)
6685 +{
6686 + ipkg_cmd_t *cmd;
6687 + ipkg_conf_t ipkg_conf;
6688 + int err;
6689 +
6690 + err = ipkg_conf_init (&ipkg_conf, args);
6691 + if (err)
6692 + {
6693 + return err;
6694 + }
6695 +
6696 + ipkg_cb_list = cblist;
6697 + /* we need to do this because of static declarations,
6698 + * maybe a good idea to change */
6699 + cmd = ipkg_cmd_find ("list");
6700 + if (packages)
6701 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6702 + else
6703 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6704 + ipkg_cb_list = NULL;
6705 + ipkg_conf_deinit (&ipkg_conf);
6706 + return (err);
6707 +}
6708 +
6709 +
6710 +int
6711 +ipkg_packages_status(args_t *args,
6712 + const char *packages,
6713 + ipkg_status_callback cbstatus,
6714 + void *userdata)
6715 +{
6716 + ipkg_cmd_t *cmd;
6717 + ipkg_conf_t ipkg_conf;
6718 + int err;
6719 +
6720 + err = ipkg_conf_init (&ipkg_conf, args);
6721 + if (err)
6722 + {
6723 + return err;
6724 + }
6725 +
6726 + ipkg_cb_status = cbstatus;
6727 +
6728 + /* we need to do this because of static declarations,
6729 + * maybe a good idea to change */
6730 + cmd = ipkg_cmd_find ("status");
6731 + if (packages)
6732 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6733 + else
6734 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6735 +
6736 + ipkg_cb_status = NULL;
6737 + ipkg_conf_deinit (&ipkg_conf);
6738 + return (err);
6739 +}
6740 +
6741 +
6742 +int
6743 +ipkg_packages_info(args_t *args,
6744 + const char *packages,
6745 + ipkg_status_callback cbstatus,
6746 + void *userdata)
6747 +{
6748 + ipkg_cmd_t *cmd;
6749 + ipkg_conf_t ipkg_conf;
6750 + int err;
6751 +
6752 + err = ipkg_conf_init (&ipkg_conf, args);
6753 + if (err)
6754 + {
6755 + return err;
6756 + }
6757 +
6758 + ipkg_cb_status = cbstatus;
6759 +
6760 + /* we need to do this because of static declarations,
6761 + * maybe a good idea to change */
6762 + cmd = ipkg_cmd_find ("info");
6763 + if (packages)
6764 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &packages, userdata);
6765 + else
6766 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, userdata);
6767 +
6768 + ipkg_cb_status = NULL;
6769 + ipkg_conf_deinit (&ipkg_conf);
6770 + return (err);
6771 +}
6772 +
6773 +
6774 +int
6775 +ipkg_packages_install (args_t * args, const char *name)
6776 +{
6777 + ipkg_cmd_t *cmd;
6778 + ipkg_conf_t ipkg_conf;
6779 + int err;
6780 +
6781 + /* this error should be handled in application */
6782 + if (!name || !strlen (name))
6783 + return (-1);
6784 +
6785 + err = ipkg_conf_init (&ipkg_conf, args);
6786 + if (err)
6787 + {
6788 + return err;
6789 + }
6790 +
6791 + /* we need to do this because of static declarations,
6792 + * maybe a good idea to change */
6793 + cmd = ipkg_cmd_find ("install");
6794 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6795 +
6796 + ipkg_conf_deinit(&ipkg_conf);
6797 + return (err);
6798 +}
6799 +
6800 +
6801 +int
6802 +ipkg_packages_remove(args_t *args, const char *name, int purge)
6803 +{
6804 + ipkg_cmd_t *cmd;
6805 + ipkg_conf_t ipkg_conf;
6806 + int err;
6807 +
6808 + /* this error should be handled in application */
6809 + if (!name || !strlen (name))
6810 + return (-1);
6811 +
6812 + err = ipkg_conf_init (&ipkg_conf, args);
6813 + if (err)
6814 + {
6815 + return err;
6816 + }
6817 +
6818 + /* we need to do this because of static declarations,
6819 + * maybe a good idea to change */
6820 + if (purge)
6821 + cmd = ipkg_cmd_find ("purge");
6822 + else
6823 + cmd = ipkg_cmd_find ("remove");
6824 +
6825 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6826 +
6827 + ipkg_conf_deinit(&ipkg_conf);
6828 + return (err);
6829 +}
6830 +
6831 +
6832 +int
6833 +ipkg_lists_update(args_t *args)
6834 +{
6835 + ipkg_cmd_t *cmd;
6836 + ipkg_conf_t ipkg_conf;
6837 + int err;
6838 +
6839 + err = ipkg_conf_init (&ipkg_conf, args);
6840 + if (err)
6841 + {
6842 + return err;
6843 + }
6844 +
6845 + /* we need to do this because of static declarations,
6846 + * maybe a good idea to change */
6847 + cmd = ipkg_cmd_find ("update");
6848 +
6849 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6850 +
6851 + ipkg_conf_deinit(&ipkg_conf);
6852 + return (err);
6853 +}
6854 +
6855 +
6856 +int
6857 +ipkg_packages_upgrade(args_t *args)
6858 +{
6859 + ipkg_cmd_t *cmd;
6860 + ipkg_conf_t ipkg_conf;
6861 + int err;
6862 +
6863 + err = ipkg_conf_init (&ipkg_conf, args);
6864 + if (err)
6865 + {
6866 + return err;
6867 + }
6868 +
6869 + /* we need to do this because of static declarations,
6870 + * maybe a good idea to change */
6871 + cmd = ipkg_cmd_find ("upgrade");
6872 +
6873 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 0, NULL, NULL);
6874 +
6875 + ipkg_conf_deinit(&ipkg_conf);
6876 + return (err);
6877 +}
6878 +
6879 +
6880 +int
6881 +ipkg_packages_download (args_t * args, const char *name)
6882 +{
6883 + ipkg_cmd_t *cmd;
6884 + ipkg_conf_t ipkg_conf;
6885 + int err;
6886 +
6887 + /* this error should be handled in application */
6888 + if (!name || !strlen (name))
6889 + return (-1);
6890 +
6891 + err = ipkg_conf_init (&ipkg_conf, args);
6892 + if (err)
6893 + {
6894 + return err;
6895 + }
6896 +
6897 + /* we need to do this because of static declarations,
6898 + * maybe a good idea to change */
6899 + cmd = ipkg_cmd_find ("download");
6900 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, NULL);
6901 +
6902 + ipkg_conf_deinit(&ipkg_conf);
6903 + return (err);
6904 +}
6905 +
6906 +
6907 +int
6908 +ipkg_package_files(args_t *args,
6909 + const char *name,
6910 + ipkg_list_callback cblist,
6911 + void *userdata)
6912 +{
6913 + ipkg_cmd_t *cmd;
6914 + ipkg_conf_t ipkg_conf;
6915 + int err;
6916 +
6917 + /* this error should be handled in application */
6918 + if (!name || !strlen (name))
6919 + return (-1);
6920 +
6921 + err = ipkg_conf_init (&ipkg_conf, args);
6922 + if (err)
6923 + {
6924 + return err;
6925 + }
6926 +
6927 + ipkg_cb_list = cblist;
6928 +
6929 + /* we need to do this because of static declarations,
6930 + * maybe a good idea to change */
6931 + cmd = ipkg_cmd_find ("files");
6932 +
6933 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &name, userdata);
6934 +
6935 + ipkg_cb_list = NULL;
6936 + ipkg_conf_deinit(&ipkg_conf);
6937 + return (err);
6938 +}
6939 +
6940 +
6941 +int
6942 +ipkg_file_search(args_t *args,
6943 + const char *file,
6944 + ipkg_list_callback cblist,
6945 + void *userdata)
6946 +{
6947 + ipkg_cmd_t *cmd;
6948 + ipkg_conf_t ipkg_conf;
6949 + int err;
6950 +
6951 + /* this error should be handled in application */
6952 + if (!file || !strlen (file))
6953 + return (-1);
6954 +
6955 + err = ipkg_conf_init (&ipkg_conf, args);
6956 + if (err)
6957 + {
6958 + return err;
6959 + }
6960 +
6961 + ipkg_cb_list = cblist;
6962 +
6963 + /* we need to do this because of static declarations,
6964 + * maybe a good idea to change */
6965 + cmd = ipkg_cmd_find ("search");
6966 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, userdata);
6967 +
6968 + ipkg_cb_list = NULL;
6969 + ipkg_conf_deinit(&ipkg_conf);
6970 + return(err);
6971 +}
6972 +
6973 +
6974 +int
6975 +ipkg_file_what(args_t *args, const char *file, const char* command)
6976 +{
6977 + ipkg_cmd_t *cmd;
6978 + ipkg_conf_t ipkg_conf;
6979 + int err;
6980 +
6981 + /* this error should be handled in application */
6982 + if (!file || !strlen (file))
6983 + return (-1);
6984 +
6985 + err = ipkg_conf_init (&ipkg_conf, args);
6986 + if (err)
6987 + {
6988 + return err;
6989 + }
6990 +
6991 + /* we need to do this because of static declarations,
6992 + * maybe a good idea to change */
6993 + cmd = ipkg_cmd_find (command);
6994 + err = ipkg_cmd_exec (cmd, &ipkg_conf, 1, &file, NULL);
6995 +
6996 + ipkg_conf_deinit(&ipkg_conf);
6997 + return(err);
6998 +}
6999 +
7000 +#define ipkg_package_whatdepends(args,file) ipkg_file_what(args,file,"whatdepends")
7001 +#define ipkg_package_whatrecommends(args, file) ipkg_file_what(args,file,"whatrecommends")
7002 +#define ipkg_package_whatprovides(args, file) ipkg_file_what(args,file,"whatprovides")
7003 +#define ipkg_package_whatconflicts(args, file) ipkg_file_what(args,file,"whatconflicts")
7004 +#define ipkg_package_whatreplaces(args, file) ipkg_file_what(args,file,"whatreplaces")
7005 +
7006 +
7007 +int default_ipkg_message_callback(ipkg_conf_t *conf, message_level_t level,
7008 + char *msg)
7009 +{
7010 + if (conf && (conf->verbosity < level)) {
7011 + return 0;
7012 + } else {
7013 +#ifdef IPKG_LIB
7014 + if ( level == IPKG_ERROR ){
7015 + push_error_list(&error_list, msg);
7016 +// printf(msg);
7017 + } else
7018 +#endif
7019 + printf(msg);
7020 + }
7021 + return 0;
7022 +}
7023 +
7024 +int default_ipkg_list_callback(char *name, char *desc, char *version,
7025 + pkg_state_status_t status, void *userdata)
7026 +{
7027 + if (desc)
7028 + printf("%s - %s - %s\n", name, version, desc);
7029 + else
7030 + printf("%s - %s\n", name, version);
7031 + return 0;
7032 +}
7033 +
7034 +int default_ipkg_files_callback(char *name, char *desc, char *version,
7035 + pkg_state_status_t status, void *userdata)
7036 +{
7037 + if (desc)
7038 + printf("%s\n", desc);
7039 + return 0;
7040 +}
7041 +
7042 +int default_ipkg_status_callback(char *name, int istatus, char *desc,
7043 + void *userdata)
7044 +{
7045 + printf("%s\n", desc);
7046 + return 0;
7047 +}
7048 +
7049 +char* default_ipkg_response_callback(char *question)
7050 +{
7051 + char *response = NULL;
7052 + printf(question);
7053 + fflush(stdout);
7054 + do {
7055 + response = (char *)file_read_line_alloc(stdin);
7056 + } while (response == NULL);
7057 + return response;
7058 +}
7059 +
7060 +/* This is used for backward compatibility */
7061 +int
7062 +ipkg_op (int argc, char *argv[])
7063 +{
7064 + int err, opt_index;
7065 + args_t args;
7066 + char *cmd_name;
7067 + ipkg_cmd_t *cmd;
7068 + ipkg_conf_t ipkg_conf;
7069 +
7070 + args_init (&args);
7071 +
7072 + opt_index = args_parse (&args, argc, argv);
7073 + if (opt_index == argc || opt_index < 0)
7074 + {
7075 + args_usage ("ipkg must have one sub-command argument");
7076 + }
7077 +
7078 + cmd_name = argv[opt_index++];
7079 +/* Pigi: added a flag to disable the checking of structures if the command does not need to
7080 + read anything from there.
7081 +*/
7082 + if ( !strcmp(cmd_name,"print-architecture") ||
7083 + !strcmp(cmd_name,"print_architecture") ||
7084 + !strcmp(cmd_name,"print-installation-architecture") ||
7085 + !strcmp(cmd_name,"print_installation_architecture") )
7086 + args.nocheckfordirorfile = 1;
7087 +
7088 +/* Pigi: added a flag to disable the reading of feed files if the command does not need to
7089 + read anything from there.
7090 +*/
7091 + if ( !strcmp(cmd_name,"flag") ||
7092 + !strcmp(cmd_name,"configure") ||
7093 + !strcmp(cmd_name,"remove") ||
7094 + !strcmp(cmd_name,"files") ||
7095 + !strcmp(cmd_name,"search") ||
7096 + !strcmp(cmd_name,"compare_versions") ||
7097 + !strcmp(cmd_name,"compare-versions") ||
7098 + !strcmp(cmd_name,"list_installed") ||
7099 + !strcmp(cmd_name,"list-installed") ||
7100 + !strcmp(cmd_name,"status") )
7101 + args.noreadfeedsfile = 1;
7102 +
7103 +
7104 + err = ipkg_conf_init (&ipkg_conf, &args);
7105 + if (err)
7106 + {
7107 + return err;
7108 + }
7109 +
7110 + args_deinit (&args);
7111 +
7112 + ipkg_cb_message = default_ipkg_message_callback;
7113 + ipkg_cb_response = default_ipkg_response_callback;
7114 + ipkg_cb_status = default_ipkg_status_callback;
7115 + if ( strcmp(cmd_name, "files")==0)
7116 + ipkg_cb_list = default_ipkg_files_callback;
7117 + else
7118 + ipkg_cb_list = default_ipkg_list_callback;
7119 +
7120 + cmd = ipkg_cmd_find (cmd_name);
7121 + if (cmd == NULL)
7122 + {
7123 + fprintf (stderr, "%s: unknown sub-command %s\n", argv[0],
7124 + cmd_name);
7125 + args_usage (NULL);
7126 + }
7127 +
7128 + if (cmd->requires_args && opt_index == argc)
7129 + {
7130 + fprintf (stderr,
7131 + "%s: the ``%s'' command requires at least one argument\n",
7132 + __FUNCTION__, cmd_name);
7133 + args_usage (NULL);
7134 + }
7135 +
7136 + err = ipkg_cmd_exec (cmd, &ipkg_conf, argc - opt_index, (const char **) (argv + opt_index), NULL);
7137 +
7138 + ipkg_conf_deinit (&ipkg_conf);
7139 +
7140 + return err;
7141 +}
7142 +
7143 +#endif /* IPKG_LIB */
7144 --- /dev/null
7145 +++ b/archival/libipkg/libipkg.h
7146 @@ -0,0 +1,88 @@
7147 +/* ipkglib.h - the itsy package management system
7148 +
7149 + Florian Boor <florian.boor@kernelconcepts.de>
7150 +
7151 + This program is free software; you can redistribute it and/or
7152 + modify it under the terms of the GNU General Public License as
7153 + published by the Free Software Foundation; either version 2, or (at
7154 + your option) any later version.
7155 +
7156 + This program is distributed in the hope that it will be useful, but
7157 + WITHOUT ANY WARRANTY; without even the implied warranty of
7158 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7159 + General Public License for more details.
7160 +*/
7161 +
7162 +#ifndef IPKGLIB_H
7163 +#define IPKGLIB_H
7164 +
7165 +#ifdef IPKG_LIB
7166 +
7167 +#include "ipkg_conf.h"
7168 +#include "ipkg_message.h"
7169 +
7170 +#include "libbb.h"
7171 +#include "args.h"
7172 +#include "pkg.h"
7173 +
7174 +typedef int (*ipkg_message_callback)(ipkg_conf_t *conf, message_level_t level,
7175 + char *msg);
7176 +typedef int (*ipkg_list_callback)(char *name, char *desc, char *version,
7177 + pkg_state_status_t status, void *userdata);
7178 +typedef int (*ipkg_status_callback)(char *name, int istatus, char *desc,
7179 + void *userdata);
7180 +typedef char* (*ipkg_response_callback)(char *question);
7181 +
7182 +extern int ipkg_op(int argc, char *argv[]); /* ipkglib.c */
7183 +extern int ipkg_init (ipkg_message_callback mcall,
7184 + ipkg_response_callback rcall,
7185 + args_t * args);
7186 +
7187 +extern int ipkg_deinit (args_t *args);
7188 +extern int ipkg_packages_list(args_t *args,
7189 + const char *packages,
7190 + ipkg_list_callback cblist,
7191 + void *userdata);
7192 +extern int ipkg_packages_status(args_t *args,
7193 + const char *packages,
7194 + ipkg_status_callback cbstatus,
7195 + void *userdata);
7196 +extern int ipkg_packages_info(args_t *args,
7197 + const char *packages,
7198 + ipkg_status_callback cbstatus,
7199 + void *userdata);
7200 +extern int ipkg_packages_install(args_t *args, const char *name);
7201 +extern int ipkg_packages_remove(args_t *args, const char *name, int purge);
7202 +extern int ipkg_lists_update(args_t *args);
7203 +extern int ipkg_packages_upgrade(args_t *args);
7204 +extern int ipkg_packages_download(args_t *args, const char *name);
7205 +extern int ipkg_package_files(args_t *args,
7206 + const char *name,
7207 + ipkg_list_callback cblist,
7208 + void *userdata);
7209 +extern int ipkg_file_search(args_t *args,
7210 + const char *file,
7211 + ipkg_list_callback cblist,
7212 + void *userdata);
7213 +extern int ipkg_package_whatdepends(args_t *args, const char *file);
7214 +extern int ipkg_package_whatrecommends(args_t *args, const char *file);
7215 +extern int ipkg_package_whatprovides(args_t *args, const char *file);
7216 +extern int ipkg_package_whatconflicts(args_t *args, const char *file);
7217 +extern int ipkg_package_whatreplaces(args_t *args, const char *file);
7218 +
7219 +extern ipkg_message_callback ipkg_cb_message; /* ipkglib.c */
7220 +extern ipkg_response_callback ipkg_cb_response;
7221 +extern ipkg_status_callback ipkg_cb_status;
7222 +extern ipkg_list_callback ipkg_cb_list;
7223 +extern void push_error_list(struct errlist **errors,char * msg);
7224 +extern void reverse_error_list(struct errlist **errors);
7225 +extern void free_error_list(struct errlist **errors);
7226 +
7227 +#else
7228 +
7229 +extern int ipkg_op(int argc, char *argv[]);
7230 +
7231 +#endif
7232 +
7233 +
7234 +#endif
7235 --- /dev/null
7236 +++ b/archival/libipkg/nv_pair.c
7237 @@ -0,0 +1,40 @@
7238 +/* nv_pair.c - the itsy package management system
7239 +
7240 + Carl D. Worth
7241 +
7242 + Copyright (C) 2001 University of Southern California
7243 +
7244 + This program is free software; you can redistribute it and/or
7245 + modify it under the terms of the GNU General Public License as
7246 + published by the Free Software Foundation; either version 2, or (at
7247 + your option) any later version.
7248 +
7249 + This program is distributed in the hope that it will be useful, but
7250 + WITHOUT ANY WARRANTY; without even the implied warranty of
7251 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7252 + General Public License for more details.
7253 +*/
7254 +
7255 +#include "ipkg.h"
7256 +
7257 +#include "nv_pair.h"
7258 +#include "str_util.h"
7259 +
7260 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value)
7261 +{
7262 + nv_pair->name = str_dup_safe(name);
7263 + nv_pair->value = str_dup_safe(value);
7264 +
7265 + return 0;
7266 +}
7267 +
7268 +void nv_pair_deinit(nv_pair_t *nv_pair)
7269 +{
7270 + free(nv_pair->name);
7271 + nv_pair->name = NULL;
7272 +
7273 + free(nv_pair->value);
7274 + nv_pair->value = NULL;
7275 +}
7276 +
7277 +
7278 --- /dev/null
7279 +++ b/archival/libipkg/nv_pair.h
7280 @@ -0,0 +1,32 @@
7281 +/* nv_pair.h - the itsy package management system
7282 +
7283 + Carl D. Worth
7284 +
7285 + Copyright (C) 2001 University of Southern California
7286 +
7287 + This program is free software; you can redistribute it and/or
7288 + modify it under the terms of the GNU General Public License as
7289 + published by the Free Software Foundation; either version 2, or (at
7290 + your option) any later version.
7291 +
7292 + This program is distributed in the hope that it will be useful, but
7293 + WITHOUT ANY WARRANTY; without even the implied warranty of
7294 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7295 + General Public License for more details.
7296 +*/
7297 +
7298 +#ifndef NV_PAIR_H
7299 +#define NV_PAIR_H
7300 +
7301 +typedef struct nv_pair nv_pair_t;
7302 +struct nv_pair
7303 +{
7304 + char *name;
7305 + char *value;
7306 +};
7307 +
7308 +int nv_pair_init(nv_pair_t *nv_pair, const char *name, const char *value);
7309 +void nv_pair_deinit(nv_pair_t *nv_pair);
7310 +
7311 +#endif
7312 +
7313 --- /dev/null
7314 +++ b/archival/libipkg/nv_pair_list.c
7315 @@ -0,0 +1,98 @@
7316 +/* nv_pair_list.c - the itsy package management system
7317 +
7318 + Carl D. Worth
7319 +
7320 + Copyright (C) 2001 University of Southern California
7321 +
7322 + This program is free software; you can redistribute it and/or
7323 + modify it under the terms of the GNU General Public License as
7324 + published by the Free Software Foundation; either version 2, or (at
7325 + your option) any later version.
7326 +
7327 + This program is distributed in the hope that it will be useful, but
7328 + WITHOUT ANY WARRANTY; without even the implied warranty of
7329 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7330 + General Public License for more details.
7331 +*/
7332 +
7333 +#include "ipkg.h"
7334 +
7335 +#include "nv_pair.h"
7336 +#include "void_list.h"
7337 +#include "nv_pair_list.h"
7338 +
7339 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data)
7340 +{
7341 + return void_list_elt_init((void_list_elt_t *) elt, data);
7342 +}
7343 +
7344 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt)
7345 +{
7346 + void_list_elt_deinit((void_list_elt_t *) elt);
7347 +}
7348 +
7349 +int nv_pair_list_init(nv_pair_list_t *list)
7350 +{
7351 + return void_list_init((void_list_t *) list);
7352 +}
7353 +
7354 +void nv_pair_list_deinit(nv_pair_list_t *list)
7355 +{
7356 + nv_pair_list_elt_t *iter;
7357 + nv_pair_t *nv_pair;
7358 +
7359 + for (iter = list->head; iter; iter = iter->next) {
7360 + nv_pair = iter->data;
7361 + nv_pair_deinit(nv_pair);
7362 +
7363 + /* malloced in nv_pair_list_append */
7364 + free(nv_pair);
7365 + iter->data = NULL;
7366 + }
7367 + void_list_deinit((void_list_t *) list);
7368 +}
7369 +
7370 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list, const char *name, const char *value)
7371 +{
7372 + int err;
7373 +
7374 + /* freed in nv_pair_list_deinit */
7375 + nv_pair_t *nv_pair = malloc(sizeof(nv_pair_t));
7376 +
7377 + if (nv_pair == NULL) {
7378 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7379 + return NULL;
7380 + }
7381 + nv_pair_init(nv_pair, name, value);
7382 +
7383 + err = void_list_append((void_list_t *) list, nv_pair);
7384 + if (err) {
7385 + return NULL;
7386 + }
7387 +
7388 + return nv_pair;
7389 +}
7390 +
7391 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data)
7392 +{
7393 + return void_list_push((void_list_t *) list, data);
7394 +}
7395 +
7396 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list)
7397 +{
7398 + return (nv_pair_list_elt_t *) void_list_pop((void_list_t *) list);
7399 +}
7400 +
7401 +char *nv_pair_list_find(nv_pair_list_t *list, char *name)
7402 +{
7403 + nv_pair_list_elt_t *iter;
7404 + nv_pair_t *nv_pair;
7405 +
7406 + for (iter = list->head; iter; iter = iter->next) {
7407 + nv_pair = iter->data;
7408 + if (strcmp(nv_pair->name, name) == 0) {
7409 + return nv_pair->value;
7410 + }
7411 + }
7412 + return NULL;
7413 +}
7414 --- /dev/null
7415 +++ b/archival/libipkg/nv_pair_list.h
7416 @@ -0,0 +1,60 @@
7417 +/* nv_pair_list.h - the itsy package management system
7418 +
7419 + Carl D. Worth
7420 +
7421 + Copyright (C) 2001 University of Southern California
7422 +
7423 + This program is free software; you can redistribute it and/or
7424 + modify it under the terms of the GNU General Public License as
7425 + published by the Free Software Foundation; either version 2, or (at
7426 + your option) any later version.
7427 +
7428 + This program is distributed in the hope that it will be useful, but
7429 + WITHOUT ANY WARRANTY; without even the implied warranty of
7430 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7431 + General Public License for more details.
7432 +*/
7433 +
7434 +#ifndef NV_PAIR_LIST_H
7435 +#define NV_PAIR_LIST_H
7436 +
7437 +#include "nv_pair.h"
7438 +#include "void_list.h"
7439 +
7440 +typedef struct nv_pair_list_elt nv_pair_list_elt_t;
7441 +struct nv_pair_list_elt
7442 +{
7443 + nv_pair_list_elt_t *next;
7444 + nv_pair_t *data;
7445 +};
7446 +
7447 +typedef struct nv_pair_list nv_pair_list_t;
7448 +struct nv_pair_list
7449 +{
7450 + nv_pair_list_elt_t pre_head;
7451 + nv_pair_list_elt_t *head;
7452 + nv_pair_list_elt_t *tail;
7453 +};
7454 +
7455 +static inline int nv_pair_list_empty(nv_pair_list_t *list)
7456 +{
7457 + if (list->head == NULL)
7458 + return 1;
7459 + else
7460 + return 0;
7461 +}
7462 +
7463 +int nv_pair_list_elt_init(nv_pair_list_elt_t *elt, nv_pair_t *data);
7464 +void nv_pair_list_elt_deinit(nv_pair_list_elt_t *elt);
7465 +
7466 +int nv_pair_list_init(nv_pair_list_t *list);
7467 +void nv_pair_list_deinit(nv_pair_list_t *list);
7468 +
7469 +nv_pair_t *nv_pair_list_append(nv_pair_list_t *list,
7470 + const char *name, const char *value);
7471 +int nv_pair_list_push(nv_pair_list_t *list, nv_pair_t *data);
7472 +nv_pair_list_elt_t *nv_pair_list_pop(nv_pair_list_t *list);
7473 +char *nv_pair_list_find(nv_pair_list_t *list, char *name);
7474 +
7475 +#endif
7476 +
7477 --- /dev/null
7478 +++ b/archival/libipkg/pkg.c
7479 @@ -0,0 +1,1747 @@
7480 +/* pkg.c - the itsy package management system
7481 +
7482 + Carl D. Worth
7483 +
7484 + Copyright (C) 2001 University of Southern California
7485 +
7486 + This program is free software; you can redistribute it and/or
7487 + modify it under the terms of the GNU General Public License as
7488 + published by the Free Software Foundation; either version 2, or (at
7489 + your option) any later version.
7490 +
7491 + This program is distributed in the hope that it will be useful, but
7492 + WITHOUT ANY WARRANTY; without even the implied warranty of
7493 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7494 + General Public License for more details.
7495 +*/
7496 +
7497 +#include "ipkg.h"
7498 +#include <ctype.h>
7499 +#include <string.h>
7500 +#include <errno.h>
7501 +
7502 +#include "pkg.h"
7503 +
7504 +#include "pkg_parse.h"
7505 +#include "pkg_extract.h"
7506 +#include "ipkg_message.h"
7507 +#include "ipkg_utils.h"
7508 +
7509 +#include "sprintf_alloc.h"
7510 +#include "file_util.h"
7511 +#include "str_util.h"
7512 +#include "xsystem.h"
7513 +#include "ipkg_conf.h"
7514 +
7515 +typedef struct enum_map enum_map_t;
7516 +struct enum_map
7517 +{
7518 + int value;
7519 + char *str;
7520 +};
7521 +
7522 +static const enum_map_t pkg_state_want_map[] = {
7523 + { SW_UNKNOWN, "unknown"},
7524 + { SW_INSTALL, "install"},
7525 + { SW_DEINSTALL, "deinstall"},
7526 + { SW_PURGE, "purge"}
7527 +};
7528 +
7529 +static const enum_map_t pkg_state_flag_map[] = {
7530 + { SF_OK, "ok"},
7531 + { SF_REINSTREQ, "reinstreq"},
7532 + { SF_HOLD, "hold"},
7533 + { SF_REPLACE, "replace"},
7534 + { SF_NOPRUNE, "noprune"},
7535 + { SF_PREFER, "prefer"},
7536 + { SF_OBSOLETE, "obsolete"},
7537 + { SF_USER, "user"},
7538 +};
7539 +
7540 +static const enum_map_t pkg_state_status_map[] = {
7541 + { SS_NOT_INSTALLED, "not-installed" },
7542 + { SS_UNPACKED, "unpacked" },
7543 + { SS_HALF_CONFIGURED, "half-configured" },
7544 + { SS_INSTALLED, "installed" },
7545 + { SS_HALF_INSTALLED, "half-installed" },
7546 + { SS_CONFIG_FILES, "config-files" },
7547 + { SS_POST_INST_FAILED, "post-inst-failed" },
7548 + { SS_REMOVAL_FAILED, "removal-failed" }
7549 +};
7550 +
7551 +static int verrevcmp(const char *val, const char *ref);
7552 +
7553 +
7554 +pkg_t *pkg_new(void)
7555 +{
7556 + pkg_t *pkg;
7557 +
7558 + pkg = malloc(sizeof(pkg_t));
7559 + if (pkg == NULL) {
7560 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7561 + return NULL;
7562 + }
7563 +
7564 + pkg_init(pkg);
7565 +
7566 + return pkg;
7567 +}
7568 +
7569 +int pkg_init(pkg_t *pkg)
7570 +{
7571 + memset(pkg, 0, sizeof(pkg_t));
7572 + pkg->name = NULL;
7573 + pkg->epoch = 0;
7574 + pkg->version = NULL;
7575 + pkg->revision = NULL;
7576 + pkg->familiar_revision = NULL;
7577 + pkg->dest = NULL;
7578 + pkg->src = NULL;
7579 + pkg->architecture = NULL;
7580 + pkg->maintainer = NULL;
7581 + pkg->section = NULL;
7582 + pkg->description = NULL;
7583 + pkg->state_want = SW_UNKNOWN;
7584 + pkg->state_flag = SF_OK;
7585 + pkg->state_status = SS_NOT_INSTALLED;
7586 + pkg->depends_str = NULL;
7587 + pkg->provides_str = NULL;
7588 + pkg->depends_count = 0;
7589 + pkg->depends = NULL;
7590 + pkg->suggests_str = NULL;
7591 + pkg->recommends_str = NULL;
7592 + pkg->suggests_count = 0;
7593 + pkg->recommends_count = 0;
7594 +
7595 + /* Abhaya: added init for conflicts fields */
7596 + pkg->conflicts = NULL;
7597 + pkg->conflicts_count = 0;
7598 +
7599 + /* added for replaces. Jamey 7/23/2002 */
7600 + pkg->replaces = NULL;
7601 + pkg->replaces_count = 0;
7602 +
7603 + pkg->pre_depends_count = 0;
7604 + pkg->pre_depends_str = NULL;
7605 + pkg->provides_count = 0;
7606 + pkg->provides = NULL;
7607 + pkg->filename = NULL;
7608 + pkg->local_filename = NULL;
7609 + pkg->tmp_unpack_dir = NULL;
7610 + pkg->md5sum = NULL;
7611 + pkg->size = NULL;
7612 + pkg->installed_size = NULL;
7613 + pkg->priority = NULL;
7614 + pkg->source = NULL;
7615 + conffile_list_init(&pkg->conffiles);
7616 + pkg->installed_files = NULL;
7617 + pkg->installed_files_ref_cnt = 0;
7618 + pkg->essential = 0;
7619 + pkg->provided_by_hand = 0;
7620 +
7621 + return 0;
7622 +}
7623 +
7624 +void pkg_deinit(pkg_t *pkg)
7625 +{
7626 + free(pkg->name);
7627 + pkg->name = NULL;
7628 + pkg->epoch = 0;
7629 + free(pkg->version);
7630 + pkg->version = NULL;
7631 + /* revision and familiar_revision share storage with version, so
7632 + don't free */
7633 + pkg->revision = NULL;
7634 + pkg->familiar_revision = NULL;
7635 + /* owned by ipkg_conf_t */
7636 + pkg->dest = NULL;
7637 + /* owned by ipkg_conf_t */
7638 + pkg->src = NULL;
7639 + free(pkg->architecture);
7640 + pkg->architecture = NULL;
7641 + free(pkg->maintainer);
7642 + pkg->maintainer = NULL;
7643 + free(pkg->section);
7644 + pkg->section = NULL;
7645 + free(pkg->description);
7646 + pkg->description = NULL;
7647 + pkg->state_want = SW_UNKNOWN;
7648 + pkg->state_flag = SF_OK;
7649 + pkg->state_status = SS_NOT_INSTALLED;
7650 + free(pkg->depends_str);
7651 + pkg->depends_str = NULL;
7652 + free(pkg->provides_str);
7653 + pkg->provides_str = NULL;
7654 + pkg->depends_count = 0;
7655 + /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->depends ? */
7656 + pkg->pre_depends_count = 0;
7657 + free(pkg->pre_depends_str);
7658 + pkg->pre_depends_str = NULL;
7659 + pkg->provides_count = 0;
7660 + /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->provides ? */
7661 + /* XXX: CLEANUP: MEMORY_LEAK: how to free up pkg->suggests ? */
7662 + free(pkg->filename);
7663 + pkg->filename = NULL;
7664 + free(pkg->local_filename);
7665 + pkg->local_filename = NULL;
7666 + /* CLEANUP: It'd be nice to pullin the cleanup function from
7667 + ipkg_install.c here. See comment in
7668 + ipkg_install.c:cleanup_temporary_files */
7669 + free(pkg->tmp_unpack_dir);
7670 + pkg->tmp_unpack_dir = NULL;
7671 + free(pkg->md5sum);
7672 + pkg->md5sum = NULL;
7673 + free(pkg->size);
7674 + pkg->size = NULL;
7675 + free(pkg->installed_size);
7676 + pkg->installed_size = NULL;
7677 + free(pkg->priority);
7678 + pkg->priority = NULL;
7679 + free(pkg->source);
7680 + pkg->source = NULL;
7681 + conffile_list_deinit(&pkg->conffiles);
7682 + /* XXX: QUESTION: Is forcing this to 1 correct? I suppose so,
7683 + since if they are calling deinit, they should know. Maybe do an
7684 + assertion here instead? */
7685 + pkg->installed_files_ref_cnt = 1;
7686 + pkg_free_installed_files(pkg);
7687 + pkg->essential = 0;
7688 +}
7689 +
7690 +int pkg_init_from_file(pkg_t *pkg, const char *filename)
7691 +{
7692 + int err;
7693 + char **raw;
7694 + FILE *control_file;
7695 +
7696 + err = pkg_init(pkg);
7697 + if (err) { return err; }
7698 +
7699 + pkg->local_filename = strdup(filename);
7700 +
7701 + control_file = tmpfile();
7702 + err = pkg_extract_control_file_to_stream(pkg, control_file);
7703 + if (err) { return err; }
7704 +
7705 + rewind(control_file);
7706 + raw = read_raw_pkgs_from_stream(control_file);
7707 + pkg_parse_raw(pkg, &raw, NULL, NULL);
7708 +
7709 + fclose(control_file);
7710 +
7711 + return 0;
7712 +}
7713 +
7714 +/* Merge any new information in newpkg into oldpkg */
7715 +/* XXX: CLEANUP: This function shouldn't actually modify anything in
7716 + newpkg, but should leave it usable. This rework is so that
7717 + pkg_hash_insert doesn't clobber the pkg that you pass into it. */
7718 +/*
7719 + * uh, i thought that i had originally written this so that it took
7720 + * two pkgs and returned a new one? we can do that again... -sma
7721 + */
7722 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status)
7723 +{
7724 + if (oldpkg == newpkg) {
7725 + return 0;
7726 + }
7727 +
7728 + if (!oldpkg->src)
7729 + oldpkg->src = newpkg->src;
7730 + if (!oldpkg->dest)
7731 + oldpkg->dest = newpkg->dest;
7732 + if (!oldpkg->architecture)
7733 + oldpkg->architecture = str_dup_safe(newpkg->architecture);
7734 + if (!oldpkg->arch_priority)
7735 + oldpkg->arch_priority = newpkg->arch_priority;
7736 + if (!oldpkg->section)
7737 + oldpkg->section = str_dup_safe(newpkg->section);
7738 + if(!oldpkg->maintainer)
7739 + oldpkg->maintainer = str_dup_safe(newpkg->maintainer);
7740 + if(!oldpkg->description)
7741 + oldpkg->description = str_dup_safe(newpkg->description);
7742 + if (set_status) {
7743 + /* merge the state_flags from the new package */
7744 + oldpkg->state_want = newpkg->state_want;
7745 + oldpkg->state_status = newpkg->state_status;
7746 + oldpkg->state_flag = newpkg->state_flag;
7747 + } else {
7748 + if (oldpkg->state_want == SW_UNKNOWN)
7749 + oldpkg->state_want = newpkg->state_want;
7750 + if (oldpkg->state_status == SS_NOT_INSTALLED)
7751 + oldpkg->state_status = newpkg->state_status;
7752 + oldpkg->state_flag |= newpkg->state_flag;
7753 + }
7754 +
7755 + if (!oldpkg->depends_str && !oldpkg->pre_depends_str && !oldpkg->recommends_str && !oldpkg->suggests_str) {
7756 + oldpkg->depends_str = newpkg->depends_str;
7757 + newpkg->depends_str = NULL;
7758 + oldpkg->depends_count = newpkg->depends_count;
7759 + newpkg->depends_count = 0;
7760 +
7761 + oldpkg->depends = newpkg->depends;
7762 + newpkg->depends = NULL;
7763 +
7764 + oldpkg->pre_depends_str = newpkg->pre_depends_str;
7765 + newpkg->pre_depends_str = NULL;
7766 + oldpkg->pre_depends_count = newpkg->pre_depends_count;
7767 + newpkg->pre_depends_count = 0;
7768 +
7769 + oldpkg->recommends_str = newpkg->recommends_str;
7770 + newpkg->recommends_str = NULL;
7771 + oldpkg->recommends_count = newpkg->recommends_count;
7772 + newpkg->recommends_count = 0;
7773 +
7774 + oldpkg->suggests_str = newpkg->suggests_str;
7775 + newpkg->suggests_str = NULL;
7776 + oldpkg->suggests_count = newpkg->suggests_count;
7777 + newpkg->suggests_count = 0;
7778 + }
7779 +
7780 + if (!oldpkg->provides_str) {
7781 + oldpkg->provides_str = newpkg->provides_str;
7782 + newpkg->provides_str = NULL;
7783 + oldpkg->provides_count = newpkg->provides_count;
7784 + newpkg->provides_count = 0;
7785 +
7786 + oldpkg->provides = newpkg->provides;
7787 + newpkg->provides = NULL;
7788 + }
7789 +
7790 + if (!oldpkg->conflicts_str) {
7791 + oldpkg->conflicts_str = newpkg->conflicts_str;
7792 + newpkg->conflicts_str = NULL;
7793 + oldpkg->conflicts_count = newpkg->conflicts_count;
7794 + newpkg->conflicts_count = 0;
7795 +
7796 + oldpkg->conflicts = newpkg->conflicts;
7797 + newpkg->conflicts = NULL;
7798 + }
7799 +
7800 + if (!oldpkg->replaces_str) {
7801 + oldpkg->replaces_str = newpkg->replaces_str;
7802 + newpkg->replaces_str = NULL;
7803 + oldpkg->replaces_count = newpkg->replaces_count;
7804 + newpkg->replaces_count = 0;
7805 +
7806 + oldpkg->replaces = newpkg->replaces;
7807 + newpkg->replaces = NULL;
7808 + }
7809 +
7810 + if (!oldpkg->filename)
7811 + oldpkg->filename = str_dup_safe(newpkg->filename);
7812 + if (0)
7813 + fprintf(stdout, "pkg=%s old local_filename=%s new local_filename=%s\n",
7814 + oldpkg->name, oldpkg->local_filename, newpkg->local_filename);
7815 + if (!oldpkg->local_filename)
7816 + oldpkg->local_filename = str_dup_safe(newpkg->local_filename);
7817 + if (!oldpkg->tmp_unpack_dir)
7818 + oldpkg->tmp_unpack_dir = str_dup_safe(newpkg->tmp_unpack_dir);
7819 + if (!oldpkg->md5sum)
7820 + oldpkg->md5sum = str_dup_safe(newpkg->md5sum);
7821 + if (!oldpkg->size)
7822 + oldpkg->size = str_dup_safe(newpkg->size);
7823 + if (!oldpkg->installed_size)
7824 + oldpkg->installed_size = str_dup_safe(newpkg->installed_size);
7825 + if (!oldpkg->priority)
7826 + oldpkg->priority = str_dup_safe(newpkg->priority);
7827 + if (!oldpkg->source)
7828 + oldpkg->source = str_dup_safe(newpkg->source);
7829 + if (oldpkg->conffiles.head == NULL){
7830 + oldpkg->conffiles = newpkg->conffiles;
7831 + conffile_list_init(&newpkg->conffiles);
7832 + }
7833 + if (!oldpkg->installed_files){
7834 + oldpkg->installed_files = newpkg->installed_files;
7835 + oldpkg->installed_files_ref_cnt = newpkg->installed_files_ref_cnt;
7836 + newpkg->installed_files = NULL;
7837 + }
7838 + if (!oldpkg->essential)
7839 + oldpkg->essential = newpkg->essential;
7840 +
7841 + oldpkg->provided_by_hand |= newpkg->provided_by_hand;
7842 +
7843 + return 0;
7844 +}
7845 +
7846 +abstract_pkg_t *abstract_pkg_new(void)
7847 +{
7848 + abstract_pkg_t * ab_pkg;
7849 +
7850 + ab_pkg = malloc(sizeof(abstract_pkg_t));
7851 +
7852 + if (ab_pkg == NULL) {
7853 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7854 + return NULL;
7855 + }
7856 +
7857 + if ( abstract_pkg_init(ab_pkg) < 0 )
7858 + return NULL;
7859 +
7860 + return ab_pkg;
7861 +}
7862 +
7863 +int abstract_pkg_init(abstract_pkg_t *ab_pkg)
7864 +{
7865 + memset(ab_pkg, 0, sizeof(abstract_pkg_t));
7866 +
7867 + ab_pkg->provided_by = abstract_pkg_vec_alloc();
7868 + if (ab_pkg->provided_by==NULL){
7869 + return -1;
7870 + }
7871 + ab_pkg->dependencies_checked = 0;
7872 + ab_pkg->state_status = SS_NOT_INSTALLED;
7873 +
7874 + return 0;
7875 +}
7876 +
7877 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg){
7878 + char * temp_str;
7879 + char **raw =NULL;
7880 + char **raw_start=NULL;
7881 +
7882 + temp_str = (char *) malloc (strlen(pkg->dest->info_dir)+strlen(pkg->name)+12);
7883 + if (temp_str == NULL ){
7884 + ipkg_message(conf, IPKG_INFO, "Out of memory in %s\n", __FUNCTION__);
7885 + return;
7886 + }
7887 + sprintf( temp_str,"%s/%s.control",pkg->dest->info_dir,pkg->name);
7888 +
7889 + raw = raw_start = read_raw_pkgs_from_file(temp_str);
7890 + if (raw == NULL ){
7891 + ipkg_message(conf, IPKG_ERROR, "Unable to open the control file in %s\n", __FUNCTION__);
7892 + return;
7893 + }
7894 +
7895 + while(*raw){
7896 + if (!pkg_valorize_other_field(pkg, &raw ) == 0) {
7897 + ipkg_message(conf, IPKG_DEBUG, "unable to read control file for %s. May be empty\n", pkg->name);
7898 + }
7899 + }
7900 + raw = raw_start;
7901 + while (*raw) {
7902 + if (raw!=NULL)
7903 + free(*raw++);
7904 + }
7905 +
7906 + free(raw_start);
7907 + free(temp_str);
7908 +
7909 + return ;
7910 +
7911 +}
7912 +
7913 +char * pkg_formatted_info(pkg_t *pkg )
7914 +{
7915 + char *line;
7916 + char * buff;
7917 +
7918 + buff = malloc(8192);
7919 + if (buff == NULL) {
7920 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
7921 + return NULL;
7922 + }
7923 +
7924 + buff[0] = '\0';
7925 +
7926 + line = pkg_formatted_field(pkg, "Package");
7927 + strncat(buff ,line, strlen(line));
7928 + free(line);
7929 +
7930 + line = pkg_formatted_field(pkg, "Version");
7931 + strncat(buff ,line, strlen(line));
7932 + free(line);
7933 +
7934 + line = pkg_formatted_field(pkg, "Depends");
7935 + strncat(buff ,line, strlen(line));
7936 + free(line);
7937 +
7938 + line = pkg_formatted_field(pkg, "Recommends");
7939 + strncat(buff ,line, strlen(line));
7940 + free(line);
7941 +
7942 + line = pkg_formatted_field(pkg, "Suggests");
7943 + strncat(buff ,line, strlen(line));
7944 + free(line);
7945 +
7946 + line = pkg_formatted_field(pkg, "Provides");
7947 + strncat(buff ,line, strlen(line));
7948 + free(line);
7949 +
7950 + line = pkg_formatted_field(pkg, "Replaces");
7951 + strncat(buff ,line, strlen(line));
7952 + free(line);
7953 +
7954 + line = pkg_formatted_field(pkg, "Conflicts");
7955 + strncat(buff ,line, strlen(line));
7956 + free(line);
7957 +
7958 + line = pkg_formatted_field(pkg, "Status");
7959 + strncat(buff ,line, strlen(line));
7960 + free(line);
7961 +
7962 + line = pkg_formatted_field(pkg, "Section");
7963 + strncat(buff ,line, strlen(line));
7964 + free(line);
7965 +
7966 + line = pkg_formatted_field(pkg, "Essential"); /* @@@@ should be removed in future release. *//* I do not agree with this Pigi*/
7967 + strncat(buff ,line, strlen(line));
7968 + free(line);
7969 +
7970 + line = pkg_formatted_field(pkg, "Architecture");
7971 + strncat(buff ,line, strlen(line));
7972 + free(line);
7973 +
7974 + line = pkg_formatted_field(pkg, "Maintainer");
7975 + strncat(buff ,line, strlen(line));
7976 + free(line);
7977 +
7978 + line = pkg_formatted_field(pkg, "MD5sum");
7979 + strncat(buff ,line, strlen(line));
7980 + free(line);
7981 +
7982 + line = pkg_formatted_field(pkg, "Size");
7983 + strncat(buff ,line, strlen(line));
7984 + free(line);
7985 +
7986 + line = pkg_formatted_field(pkg, "Filename");
7987 + strncat(buff ,line, strlen(line));
7988 + free(line);
7989 +
7990 + line = pkg_formatted_field(pkg, "Conffiles");
7991 + strncat(buff ,line, strlen(line));
7992 + free(line);
7993 +
7994 + line = pkg_formatted_field(pkg, "Source");
7995 + strncat(buff ,line, strlen(line));
7996 + free(line);
7997 +
7998 + line = pkg_formatted_field(pkg, "Description");
7999 + strncat(buff ,line, strlen(line));
8000 + free(line);
8001 +
8002 + line = pkg_formatted_field(pkg, "Installed-Time");
8003 + strncat(buff ,line, strlen(line));
8004 + free(line);
8005 +
8006 + return buff;
8007 +}
8008 +
8009 +char * pkg_formatted_field(pkg_t *pkg, const char *field )
8010 +{
8011 + static size_t LINE_LEN = 128;
8012 + char line_str[LINE_LEN];
8013 + char * temp = (char *)malloc(1);
8014 + int len = 0;
8015 + int flag_provide_false = 0;
8016 +
8017 +/*
8018 + Pigi: After some discussion with Florian we decided to modify the full procedure in
8019 + dynamic memory allocation. This should avoid any other segv in this area ( except for bugs )
8020 +*/
8021 +
8022 + if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8023 + goto UNKNOWN_FMT_FIELD;
8024 + }
8025 +
8026 + temp[0]='\0';
8027 +
8028 + switch (field[0])
8029 + {
8030 + case 'a':
8031 + case 'A':
8032 + if (strcasecmp(field, "Architecture") == 0) {
8033 + /* Architecture */
8034 + if (pkg->architecture) {
8035 + temp = (char *)realloc(temp,strlen(pkg->architecture)+17);
8036 + if ( temp == NULL ){
8037 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8038 + return NULL;
8039 + }
8040 + temp[0]='\0';
8041 + snprintf(temp, (strlen(pkg->architecture)+17), "Architecture: %s\n", pkg->architecture);
8042 + }
8043 + } else {
8044 + goto UNKNOWN_FMT_FIELD;
8045 + }
8046 + break;
8047 + case 'c':
8048 + case 'C':
8049 + if (strcasecmp(field, "Conffiles") == 0) {
8050 + /* Conffiles */
8051 + conffile_list_elt_t *iter;
8052 +
8053 + if (pkg->conffiles.head == NULL) {
8054 + return temp;
8055 + }
8056 +
8057 + len = 14 ;
8058 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8059 + if (iter->data->name && iter->data->value) {
8060 + len = len + (strlen(iter->data->name)+strlen(iter->data->value)+5);
8061 + }
8062 + }
8063 + temp = (char *)realloc(temp,len);
8064 + if ( temp == NULL ){
8065 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8066 + return NULL;
8067 + }
8068 + temp[0]='\0';
8069 + strncpy(temp, "Conffiles:\n", 12);
8070 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8071 + if (iter->data->name && iter->data->value) {
8072 + snprintf(line_str, LINE_LEN, "%s %s\n", iter->data->name, iter->data->value);
8073 + strncat(temp, line_str, strlen(line_str));
8074 + }
8075 + }
8076 + } else if (strcasecmp(field, "Conflicts") == 0) {
8077 + int i;
8078 +
8079 + if (pkg->conflicts_count) {
8080 + len = 14 ;
8081 + for(i = 0; i < pkg->conflicts_count; i++) {
8082 + len = len + (strlen(pkg->conflicts_str[i])+5);
8083 + }
8084 + temp = (char *)realloc(temp,len);
8085 + if ( temp == NULL ){
8086 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8087 + return NULL;
8088 + }
8089 + temp[0]='\0';
8090 + strncpy(temp, "Conflicts:", 11);
8091 + for(i = 0; i < pkg->conflicts_count; i++) {
8092 + snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->conflicts_str[i]);
8093 + strncat(temp, line_str, strlen(line_str));
8094 + }
8095 + strncat(temp, "\n", strlen("\n"));
8096 + }
8097 + } else {
8098 + goto UNKNOWN_FMT_FIELD;
8099 + }
8100 + break;
8101 + case 'd':
8102 + case 'D':
8103 + if (strcasecmp(field, "Depends") == 0) {
8104 + /* Depends */
8105 + int i;
8106 +
8107 + if (pkg->depends_count) {
8108 + len = 14 ;
8109 + for(i = 0; i < pkg->depends_count; i++) {
8110 + len = len + (strlen(pkg->depends_str[i])+4);
8111 + }
8112 + temp = (char *)realloc(temp,len);
8113 + if ( temp == NULL ){
8114 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8115 + return NULL;
8116 + }
8117 + temp[0]='\0';
8118 + strncpy(temp, "Depends:", 10);
8119 + for(i = 0; i < pkg->depends_count; i++) {
8120 + snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->depends_str[i]);
8121 + strncat(temp, line_str, strlen(line_str));
8122 + }
8123 + strncat(temp, "\n", strlen("\n"));
8124 + }
8125 + } else if (strcasecmp(field, "Description") == 0) {
8126 + /* Description */
8127 + if (pkg->description) {
8128 + temp = (char *)realloc(temp,strlen(pkg->description)+16);
8129 + if ( temp == NULL ){
8130 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8131 + return NULL;
8132 + }
8133 + temp[0]='\0';
8134 + snprintf(temp, (strlen(pkg->description)+16), "Description: %s\n", pkg->description);
8135 + }
8136 + } else {
8137 + goto UNKNOWN_FMT_FIELD;
8138 + }
8139 + break;
8140 + case 'e':
8141 + case 'E': {
8142 + /* Essential */
8143 + if (pkg->essential) {
8144 + temp = (char *)realloc(temp,16);
8145 + if ( temp == NULL ){
8146 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8147 + return NULL;
8148 + }
8149 + temp[0]='\0';
8150 + snprintf(temp, (16), "Essential: yes\n");
8151 + }
8152 + }
8153 + break;
8154 + case 'f':
8155 + case 'F': {
8156 + /* Filename */
8157 + if (pkg->filename) {
8158 + temp = (char *)realloc(temp,strlen(pkg->filename)+12);
8159 + if ( temp == NULL ){
8160 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8161 + return NULL;
8162 + }
8163 + temp[0]='\0';
8164 + snprintf(temp, (strlen(pkg->filename)+12), "Filename: %s\n", pkg->filename);
8165 + }
8166 + }
8167 + break;
8168 + case 'i':
8169 + case 'I': {
8170 + if (strcasecmp(field, "Installed-Size") == 0) {
8171 + /* Installed-Size */
8172 + temp = (char *)realloc(temp,strlen(pkg->installed_size)+17);
8173 + if ( temp == NULL ){
8174 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8175 + return NULL;
8176 + }
8177 + temp[0]='\0';
8178 + snprintf(temp, (strlen(pkg->installed_size)+17), "Installed-Size: %s\n", pkg->installed_size);
8179 + } else if (strcasecmp(field, "Installed-Time") == 0 && pkg->installed_time) {
8180 + temp = (char *)realloc(temp,29);
8181 + if ( temp == NULL ){
8182 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8183 + return NULL;
8184 + }
8185 + temp[0]='\0';
8186 + snprintf(temp, 29, "Installed-Time: %lu\n", pkg->installed_time);
8187 + }
8188 + }
8189 + break;
8190 + case 'm':
8191 + case 'M': {
8192 + /* Maintainer | MD5sum */
8193 + if (strcasecmp(field, "Maintainer") == 0) {
8194 + /* Maintainer */
8195 + if (pkg->maintainer) {
8196 + temp = (char *)realloc(temp,strlen(pkg->maintainer)+14);
8197 + if ( temp == NULL ){
8198 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8199 + return NULL;
8200 + }
8201 + temp[0]='\0';
8202 + snprintf(temp, (strlen(pkg->maintainer)+14), "maintainer: %s\n", pkg->maintainer);
8203 + }
8204 + } else if (strcasecmp(field, "MD5sum") == 0) {
8205 + /* MD5sum */
8206 + if (pkg->md5sum) {
8207 + temp = (char *)realloc(temp,strlen(pkg->md5sum)+11);
8208 + if ( temp == NULL ){
8209 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8210 + return NULL;
8211 + }
8212 + temp[0]='\0';
8213 + snprintf(temp, (strlen(pkg->md5sum)+11), "MD5Sum: %s\n", pkg->md5sum);
8214 + }
8215 + } else {
8216 + goto UNKNOWN_FMT_FIELD;
8217 + }
8218 + }
8219 + break;
8220 + case 'p':
8221 + case 'P': {
8222 + if (strcasecmp(field, "Package") == 0) {
8223 + /* Package */
8224 + temp = (char *)realloc(temp,strlen(pkg->name)+11);
8225 + if ( temp == NULL ){
8226 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8227 + return NULL;
8228 + }
8229 + temp[0]='\0';
8230 + snprintf(temp, (strlen(pkg->name)+11), "Package: %s\n", pkg->name);
8231 + } else if (strcasecmp(field, "Priority") == 0) {
8232 + /* Priority */
8233 + temp = (char *)realloc(temp,strlen(pkg->priority)+12);
8234 + if ( temp == NULL ){
8235 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8236 + return NULL;
8237 + }
8238 + temp[0]='\0';
8239 + snprintf(temp, (strlen(pkg->priority)+12), "Priority: %s\n", pkg->priority);
8240 + } else if (strcasecmp(field, "Provides") == 0) {
8241 + /* Provides */
8242 + int i;
8243 +
8244 + if (pkg->provides_count) {
8245 + /* Here we check if the ipkg_internal_use_only is used, and we discard it.*/
8246 + for ( i=0; i < pkg->provides_count; i++ ){
8247 + if (strstr(pkg->provides_str[i],"ipkg_internal_use_only")!=NULL) {
8248 + memset (pkg->provides_str[i],'\x0',strlen(pkg->provides_str[i])); /* Pigi clear my trick flag, just in case */
8249 + flag_provide_false = 1;
8250 + }
8251 + }
8252 + if ( !flag_provide_false || /* Pigi there is not my trick flag */
8253 + ((flag_provide_false) && (pkg->provides_count > 1))){ /* Pigi There is, but we also have others Provides */
8254 + char provstr[LINE_LEN];
8255 + len = 15;
8256 + for(i = 0; i < pkg->provides_count; i++) {
8257 + len = len + (strlen(pkg->provides_str[i])+5);
8258 + }
8259 + temp = (char *)realloc(temp,len);
8260 + if ( temp == NULL ){
8261 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8262 + return NULL;
8263 + }
8264 + temp[0]='\0';
8265 + strncpy(temp, "Provides:", 12);
8266 + for(i = 0; i < pkg->provides_count; i++) {
8267 + if (strlen(pkg->provides_str[i])>0){;
8268 + snprintf(provstr, LINE_LEN, "%s %s", i == 1 ? "" : ",", pkg->provides_str[i]);
8269 + strncat(temp, provstr, strlen(provstr));
8270 + }
8271 + }
8272 + strncat(temp, "\n", strlen("\n"));
8273 + }
8274 + }
8275 + } else {
8276 + goto UNKNOWN_FMT_FIELD;
8277 + }
8278 + }
8279 + break;
8280 + case 'r':
8281 + case 'R': {
8282 + int i;
8283 + /* Replaces | Recommends*/
8284 + if (strcasecmp (field, "Replaces") == 0) {
8285 + if (pkg->replaces_count) {
8286 + len = 14;
8287 + for (i = 0; i < pkg->replaces_count; i++) {
8288 + len = len + (strlen(pkg->replaces_str[i])+5);
8289 + }
8290 + temp = (char *)realloc(temp,len);
8291 + if ( temp == NULL ){
8292 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8293 + return NULL;
8294 + }
8295 + temp[0]='\0';
8296 + strncpy(temp, "Replaces:", 12);
8297 + for (i = 0; i < pkg->replaces_count; i++) {
8298 + snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->replaces_str[i]);
8299 + strncat(temp, line_str, strlen(line_str));
8300 + }
8301 + strncat(temp, "\n", strlen("\n"));
8302 + }
8303 + } else if (strcasecmp (field, "Recommends") == 0) {
8304 + if (pkg->recommends_count) {
8305 + len = 15;
8306 + for(i = 0; i < pkg->recommends_count; i++) {
8307 + len = len + (strlen( pkg->recommends_str[i])+5);
8308 + }
8309 + temp = (char *)realloc(temp,len);
8310 + if ( temp == NULL ){
8311 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8312 + return NULL;
8313 + }
8314 + temp[0]='\0';
8315 + strncpy(temp, "Recommends:", 13);
8316 + for(i = 0; i < pkg->recommends_count; i++) {
8317 + snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->recommends_str[i]);
8318 + strncat(temp, line_str, strlen(line_str));
8319 + }
8320 + strncat(temp, "\n", strlen("\n"));
8321 + }
8322 + } else {
8323 + goto UNKNOWN_FMT_FIELD;
8324 + }
8325 + }
8326 + break;
8327 + case 's':
8328 + case 'S': {
8329 + /* Section | Size | Source | Status | Suggests */
8330 + if (strcasecmp(field, "Section") == 0) {
8331 + /* Section */
8332 + if (pkg->section) {
8333 + temp = (char *)realloc(temp,strlen(pkg->section)+11);
8334 + if ( temp == NULL ){
8335 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8336 + return NULL;
8337 + }
8338 + temp[0]='\0';
8339 + snprintf(temp, (strlen(pkg->section)+11), "Section: %s\n", pkg->section);
8340 + }
8341 + } else if (strcasecmp(field, "Size") == 0) {
8342 + /* Size */
8343 + if (pkg->size) {
8344 + temp = (char *)realloc(temp,strlen(pkg->size)+8);
8345 + if ( temp == NULL ){
8346 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8347 + return NULL;
8348 + }
8349 + temp[0]='\0';
8350 + snprintf(temp, (strlen(pkg->size)+8), "Size: %s\n", pkg->size);
8351 + }
8352 + } else if (strcasecmp(field, "Source") == 0) {
8353 + /* Source */
8354 + if (pkg->source) {
8355 + temp = (char *)realloc(temp,strlen(pkg->source)+10);
8356 + if ( temp == NULL ){
8357 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8358 + return NULL;
8359 + }
8360 + temp[0]='\0';
8361 + snprintf(temp, (strlen(pkg->source)+10), "Source: %s\n", pkg->source);
8362 + }
8363 + } else if (strcasecmp(field, "Status") == 0) {
8364 + /* Status */
8365 + /* Benjamin Pineau note: we should avoid direct usage of
8366 + * strlen(arg) without keeping "arg" for later free()
8367 + */
8368 + char *pflag=pkg_state_flag_to_str(pkg->state_flag);
8369 + char *pstat=pkg_state_status_to_str(pkg->state_status);
8370 + char *pwant=pkg_state_want_to_str(pkg->state_want);
8371 +
8372 + size_t sum_of_sizes = (size_t) ( strlen(pwant)+ strlen(pflag)+ strlen(pstat) + 12 );
8373 + temp = (char *)realloc(temp,sum_of_sizes);
8374 + if ( temp == NULL ){
8375 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8376 + return NULL;
8377 + }
8378 + temp[0]='\0';
8379 + snprintf(temp, sum_of_sizes , "Status: %s %s %s\n", pwant, pflag, pstat);
8380 + free(pflag);
8381 + free(pwant);
8382 + if(pstat) /* pfstat can be NULL if ENOMEM */
8383 + free(pstat);
8384 + } else if (strcasecmp(field, "Suggests") == 0) {
8385 + if (pkg->suggests_count) {
8386 + int i;
8387 + len = 13;
8388 + for(i = 0; i < pkg->suggests_count; i++) {
8389 + len = len + (strlen(pkg->suggests_str[i])+5);
8390 + }
8391 + temp = (char *)realloc(temp,len);
8392 + if ( temp == NULL ){
8393 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8394 + return NULL;
8395 + }
8396 + temp[0]='\0';
8397 + strncpy(temp, "Suggests:", 10);
8398 + for(i = 0; i < pkg->suggests_count; i++) {
8399 + snprintf(line_str, LINE_LEN, "%s %s", i == 0 ? "" : ",", pkg->suggests_str[i]);
8400 + strncat(temp, line_str, strlen(line_str));
8401 + }
8402 + strncat(temp, "\n", strlen("\n"));
8403 + }
8404 + } else {
8405 + goto UNKNOWN_FMT_FIELD;
8406 + }
8407 + }
8408 + break;
8409 + case 'v':
8410 + case 'V': {
8411 + /* Version */
8412 + char *version = pkg_version_str_alloc(pkg);
8413 + temp = (char *)realloc(temp,strlen(version)+14);
8414 + if ( temp == NULL ){
8415 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8416 + return NULL;
8417 + }
8418 + temp[0]='\0';
8419 + snprintf(temp, (strlen(version)+12), "Version: %s\n", version);
8420 + free(version);
8421 + }
8422 + break;
8423 + default:
8424 + goto UNKNOWN_FMT_FIELD;
8425 + }
8426 +
8427 + if ( strlen(temp)<2 ) {
8428 + temp[0]='\0';
8429 + }
8430 + return temp;
8431 +
8432 + UNKNOWN_FMT_FIELD:
8433 + fprintf(stderr, "%s: ERROR: Unknown field name: %s\n", __FUNCTION__, field);
8434 + if ( strlen(temp)<2 ) {
8435 + temp[0]='\0';
8436 + }
8437 +
8438 + return temp;
8439 +}
8440 +
8441 +void pkg_print_info(pkg_t *pkg, FILE *file)
8442 +{
8443 + char * buff;
8444 + if (pkg == NULL) {
8445 + return;
8446 + }
8447 +
8448 + buff = pkg_formatted_info(pkg);
8449 + if ( buff == NULL )
8450 + return;
8451 + if (strlen(buff)>2){
8452 + fwrite(buff, 1, strlen(buff), file);
8453 + }
8454 + free(buff);
8455 +}
8456 +
8457 +void pkg_print_status(pkg_t * pkg, FILE * file)
8458 +{
8459 + if (pkg == NULL) {
8460 + return;
8461 + }
8462 +
8463 + /* XXX: QUESTION: Do we actually want more fields here? The
8464 + original idea was to save space by installing only what was
8465 + needed for actual computation, (package, version, status,
8466 + essential, conffiles). The assumption is that all other fields
8467 + can be found in th available file.
8468 +
8469 + But, someone proposed the idea to make it possible to
8470 + reconstruct a .ipk from an installed package, (ie. for beaming
8471 + from one handheld to another). So, maybe we actually want a few
8472 + more fields here, (depends, suggests, etc.), so that that would
8473 + be guaranteed to work even in the absence of more information
8474 + from the available file.
8475 +
8476 + 28-MAR-03: kergoth and I discussed this yesterday. We think
8477 + the essential info needs to be here for all installed packages
8478 + because they may not appear in the Packages files on various
8479 + feeds. Furthermore, one should be able to install from URL or
8480 + local storage without requiring a Packages file from any feed.
8481 + -Jamey
8482 + */
8483 + pkg_print_field(pkg, file, "Package");
8484 + pkg_print_field(pkg, file, "Version");
8485 + pkg_print_field(pkg, file, "Depends");
8486 + pkg_print_field(pkg, file, "Recommends");
8487 + pkg_print_field(pkg, file, "Suggests");
8488 + pkg_print_field(pkg, file, "Provides");
8489 + pkg_print_field(pkg, file, "Replaces");
8490 + pkg_print_field(pkg, file, "Conflicts");
8491 + pkg_print_field(pkg, file, "Status");
8492 + pkg_print_field(pkg, file, "Essential"); /* @@@@ should be removed in future release. */
8493 + pkg_print_field(pkg, file, "Architecture");
8494 + pkg_print_field(pkg, file, "Conffiles");
8495 + pkg_print_field(pkg, file, "Installed-Time");
8496 + fputs("\n", file);
8497 +}
8498 +
8499 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field)
8500 +{
8501 + char *buff;
8502 + if (strlen(field) < PKG_MINIMUM_FIELD_NAME_LEN) {
8503 + fprintf(stderr, "%s: ERROR: Unknown field name: %s\n",
8504 + __FUNCTION__, field);
8505 + }
8506 + buff = pkg_formatted_field(pkg, field);
8507 + if (strlen(buff)>2) {
8508 + fprintf(file, "%s", buff);
8509 + fflush(file);
8510 + }
8511 + free(buff);
8512 + return;
8513 +}
8514 +
8515 +/*
8516 + * libdpkg - Debian packaging suite library routines
8517 + * vercmp.c - comparison of version numbers
8518 + *
8519 + * Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
8520 + */
8521 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg)
8522 +{
8523 + int r;
8524 +
8525 + if (pkg->epoch > ref_pkg->epoch) {
8526 + return 1;
8527 + }
8528 +
8529 + if (pkg->epoch < ref_pkg->epoch) {
8530 + return -1;
8531 + }
8532 +
8533 + r = verrevcmp(pkg->version, ref_pkg->version);
8534 + if (r) {
8535 + return r;
8536 + }
8537 +
8538 +#ifdef USE_DEBVERSION
8539 + r = verrevcmp(pkg->revision, ref_pkg->revision);
8540 + if (r) {
8541 + return r;
8542 + }
8543 +
8544 + r = verrevcmp(pkg->familiar_revision, ref_pkg->familiar_revision);
8545 +#endif
8546 +
8547 + return r;
8548 +}
8549 +
8550 +int verrevcmp(const char *val, const char *ref)
8551 +{
8552 + int vc, rc;
8553 + long vl, rl;
8554 + const char *vp, *rp;
8555 + const char *vsep, *rsep;
8556 +
8557 + if (!val) val= "";
8558 + if (!ref) ref= "";
8559 + for (;;) {
8560 + vp= val; while (*vp && !isdigit(*vp)) vp++;
8561 + rp= ref; while (*rp && !isdigit(*rp)) rp++;
8562 + for (;;) {
8563 + vc= (val == vp) ? 0 : *val++;
8564 + rc= (ref == rp) ? 0 : *ref++;
8565 + if (!rc && !vc) break;
8566 + if (vc && !isalpha(vc)) vc += 256; /* assumes ASCII character set */
8567 + if (rc && !isalpha(rc)) rc += 256;
8568 + if (vc != rc) return vc - rc;
8569 + }
8570 + val= vp;
8571 + ref= rp;
8572 + vl=0; if (isdigit(*vp)) vl= strtol(val,(char**)&val,10);
8573 + rl=0; if (isdigit(*rp)) rl= strtol(ref,(char**)&ref,10);
8574 + if (vl != rl) return vl - rl;
8575 +
8576 + vc = *val;
8577 + rc = *ref;
8578 + vsep = strchr(".-", vc);
8579 + rsep = strchr(".-", rc);
8580 + if (vsep && !rsep) return -1;
8581 + if (!vsep && rsep) return +1;
8582 +
8583 + if (!*val && !*ref) return 0;
8584 + if (!*val) return -1;
8585 + if (!*ref) return +1;
8586 + }
8587 +}
8588 +
8589 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op)
8590 +{
8591 + int r;
8592 +
8593 + r = pkg_compare_versions(it, ref);
8594 +
8595 + if (strcmp(op, "<=") == 0 || strcmp(op, "<") == 0) {
8596 + return r <= 0;
8597 + }
8598 +
8599 + if (strcmp(op, ">=") == 0 || strcmp(op, ">") == 0) {
8600 + return r >= 0;
8601 + }
8602 +
8603 + if (strcmp(op, "<<") == 0) {
8604 + return r < 0;
8605 + }
8606 +
8607 + if (strcmp(op, ">>") == 0) {
8608 + return r > 0;
8609 + }
8610 +
8611 + if (strcmp(op, "=") == 0) {
8612 + return r == 0;
8613 + }
8614 +
8615 + fprintf(stderr, "unknown operator: %s", op);
8616 + return 0;
8617 +}
8618 +
8619 +int pkg_name_version_and_architecture_compare(pkg_t *a, pkg_t *b)
8620 +{
8621 + int namecmp;
8622 + int vercmp;
8623 + if (!a->name || !b->name) {
8624 + fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->name=%p b=%p b->name=%p\n",
8625 + a, a->name, b, b->name);
8626 + return 0;
8627 + }
8628 +
8629 + namecmp = strcmp(a->name, b->name);
8630 + if (namecmp)
8631 + return namecmp;
8632 + vercmp = pkg_compare_versions(a, b);
8633 + if (vercmp)
8634 + return vercmp;
8635 + if (!a->arch_priority || !b->arch_priority) {
8636 + fprintf(stderr, "pkg_name_version_and_architecture_compare: a=%p a->arch_priority=%i b=%p b->arch_priority=%i\n",
8637 + a, a->arch_priority, b, b->arch_priority);
8638 + return 0;
8639 + }
8640 + if (a->arch_priority > b->arch_priority)
8641 + return 1;
8642 + if (a->arch_priority < b->arch_priority)
8643 + return -1;
8644 + return 0;
8645 +}
8646 +
8647 +int abstract_pkg_name_compare(abstract_pkg_t *a, abstract_pkg_t *b)
8648 +{
8649 + if (!a->name || !b->name) {
8650 + fprintf(stderr, "abstract_pkg_name_compare: a=%p a->name=%p b=%p b->name=%p\n",
8651 + a, a->name, b, b->name);
8652 + return 0;
8653 + }
8654 + return strcmp(a->name, b->name);
8655 +}
8656 +
8657 +
8658 +char *pkg_version_str_alloc(pkg_t *pkg)
8659 +{
8660 + char *complete_version;
8661 + char *epoch_str;
8662 +#ifdef USE_DEBVERSION
8663 + char *revision_str;
8664 + char *familiar_revision_str;
8665 +#endif
8666 +
8667 + if (pkg->epoch) {
8668 + sprintf_alloc(&epoch_str, "%d:", (int)(pkg->epoch));
8669 + } else {
8670 + epoch_str = strdup("");
8671 + }
8672 +
8673 +#ifdef USE_DEBVERSION
8674 + if (pkg->revision && strlen(pkg->revision)) {
8675 + sprintf_alloc(&revision_str, "-%s", pkg->revision);
8676 + } else {
8677 + revision_str = strdup("");
8678 + }
8679 +
8680 + if (pkg->familiar_revision && strlen(pkg->familiar_revision)) {
8681 + sprintf_alloc(&familiar_revision_str, "-fam%s", pkg->familiar_revision);
8682 + } else {
8683 + familiar_revision_str = strdup("");
8684 + }
8685 +#endif
8686 +
8687 +#ifdef USE_DEBVERSION
8688 + sprintf_alloc(&complete_version, "%s%s%s%s",
8689 + epoch_str, pkg->version, revision_str, familiar_revision_str);
8690 +#else
8691 + sprintf_alloc(&complete_version, "%s%s",
8692 + epoch_str, pkg->version);
8693 +#endif
8694 +
8695 + free(epoch_str);
8696 +#ifdef USE_DEBVERSION
8697 + free(revision_str);
8698 + free(familiar_revision_str);
8699 +#endif
8700 +
8701 + return complete_version;
8702 +}
8703 +
8704 +str_list_t *pkg_get_installed_files(pkg_t *pkg)
8705 +{
8706 + int err;
8707 + char *list_file_name = NULL;
8708 + FILE *list_file = NULL;
8709 + char *line;
8710 + char *installed_file_name;
8711 + int rootdirlen;
8712 +
8713 + pkg->installed_files_ref_cnt++;
8714 +
8715 + if (pkg->installed_files) {
8716 + return pkg->installed_files;
8717 + }
8718 +
8719 + pkg->installed_files = str_list_alloc();
8720 + if (pkg->installed_files == NULL) {
8721 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8722 + return NULL;
8723 + }
8724 +
8725 + /* For uninstalled packages, get the file list firectly from the package.
8726 + For installed packages, look at the package.list file in the database.
8727 + */
8728 + if (pkg->state_status == SS_NOT_INSTALLED || pkg->dest == NULL) {
8729 + if (pkg->local_filename == NULL) {
8730 + return pkg->installed_files;
8731 + }
8732 + /* XXX: CLEANUP: Maybe rewrite this to avoid using a temporary
8733 + file. In other words, change deb_extract so that it can
8734 + simply return the file list as a char *[] rather than
8735 + insisting on writing in to a FILE * as it does now. */
8736 + list_file = tmpfile();
8737 + err = pkg_extract_data_file_names_to_stream(pkg, list_file);
8738 + if (err) {
8739 + fclose(list_file);
8740 + fprintf(stderr, "%s: Error extracting file list from %s: %s\n",
8741 + __FUNCTION__, pkg->local_filename, strerror(err));
8742 + return pkg->installed_files;
8743 + }
8744 + rewind(list_file);
8745 + } else {
8746 + sprintf_alloc(&list_file_name, "%s/%s.list",
8747 + pkg->dest->info_dir, pkg->name);
8748 + if (! file_exists(list_file_name)) {
8749 + free(list_file_name);
8750 + return pkg->installed_files;
8751 + }
8752 +
8753 + list_file = fopen(list_file_name, "r");
8754 + if (list_file == NULL) {
8755 + fprintf(stderr, "WARNING: Cannot open %s: %s\n",
8756 + list_file_name, strerror(errno));
8757 + free(list_file_name);
8758 + return pkg->installed_files;
8759 + }
8760 + free(list_file_name);
8761 + }
8762 +
8763 + rootdirlen = strlen( pkg->dest->root_dir );
8764 + while (1) {
8765 + char *file_name;
8766 +
8767 + line = file_read_line_alloc(list_file);
8768 + if (line == NULL) {
8769 + break;
8770 + }
8771 + str_chomp(line);
8772 + file_name = line;
8773 +
8774 + /* Take pains to avoid uglies like "/./" in the middle of file_name. */
8775 + if( strncmp( pkg->dest->root_dir,
8776 + file_name,
8777 + rootdirlen ) ) {
8778 + if (*file_name == '.') {
8779 + file_name++;
8780 + }
8781 + if (*file_name == '/') {
8782 + file_name++;
8783 + }
8784 +
8785 + /* Freed in pkg_free_installed_files */
8786 + sprintf_alloc(&installed_file_name, "%s%s", pkg->dest->root_dir, file_name);
8787 + } else {
8788 + // already contains root_dir as header -> ABSOLUTE
8789 + sprintf_alloc(&installed_file_name, "%s", file_name);
8790 + }
8791 + str_list_append(pkg->installed_files, installed_file_name);
8792 + free(line);
8793 + }
8794 +
8795 + fclose(list_file);
8796 +
8797 + return pkg->installed_files;
8798 +}
8799 +
8800 +/* XXX: CLEANUP: This function and it's counterpart,
8801 + (pkg_get_installed_files), do not match our init/deinit naming
8802 + convention. Nor the alloc/free convention. But, then again, neither
8803 + of these conventions currrently fit the way these two functions
8804 + work. */
8805 +int pkg_free_installed_files(pkg_t *pkg)
8806 +{
8807 + str_list_elt_t *iter;
8808 +
8809 + pkg->installed_files_ref_cnt--;
8810 + if (pkg->installed_files_ref_cnt > 0) {
8811 + return 0;
8812 + }
8813 +
8814 + if (pkg->installed_files) {
8815 +
8816 + for (iter = pkg->installed_files->head; iter; iter = iter->next) {
8817 + /* malloced in pkg_get_installed_files */
8818 + free (iter->data);
8819 + iter->data = NULL;
8820 + }
8821 +
8822 + str_list_deinit(pkg->installed_files);
8823 + }
8824 +
8825 + pkg->installed_files = NULL;
8826 +
8827 + return 0;
8828 +}
8829 +
8830 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg)
8831 +{
8832 + int err;
8833 + char *list_file_name;
8834 +
8835 + //I don't think pkg_free_installed_files should be called here. Jamey
8836 + //pkg_free_installed_files(pkg);
8837 +
8838 + sprintf_alloc(&list_file_name, "%s/%s.list",
8839 + pkg->dest->info_dir, pkg->name);
8840 + if (!conf->noaction) {
8841 + err = unlink(list_file_name);
8842 + free(list_file_name);
8843 +
8844 + if (err) {
8845 + return errno;
8846 + }
8847 + }
8848 + return 0;
8849 +}
8850 +
8851 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name)
8852 +{
8853 + conffile_list_elt_t *iter;
8854 + conffile_t *conffile;
8855 +
8856 + if (pkg == NULL) {
8857 + return NULL;
8858 + }
8859 +
8860 + for (iter = pkg->conffiles.head; iter; iter = iter->next) {
8861 + conffile = iter->data;
8862 +
8863 + if (strcmp(conffile->name, file_name) == 0) {
8864 + return conffile;
8865 + }
8866 + }
8867 +
8868 + return NULL;
8869 +}
8870 +
8871 +int pkg_run_script(ipkg_conf_t *conf, pkg_t *pkg,
8872 + const char *script, const char *args)
8873 +{
8874 + int err;
8875 + char *path;
8876 + char *cmd;
8877 +
8878 + /* XXX: FEATURE: When conf->offline_root is set, we should run the
8879 + maintainer script within a chroot environment. */
8880 +
8881 + /* Installed packages have scripts in pkg->dest->info_dir, uninstalled packages
8882 + have scripts in pkg->tmp_unpack_dir. */
8883 + if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
8884 + if (pkg->dest == NULL) {
8885 + fprintf(stderr, "%s: ERROR: installed package %s has a NULL dest\n",
8886 + __FUNCTION__, pkg->name);
8887 + return EINVAL;
8888 + }
8889 + sprintf_alloc(&path, "%s/%s.%s", pkg->dest->info_dir, pkg->name, script);
8890 + } else {
8891 + if (pkg->tmp_unpack_dir == NULL) {
8892 + fprintf(stderr, "%s: ERROR: uninstalled package %s has a NULL tmp_unpack_dir\n",
8893 + __FUNCTION__, pkg->name);
8894 + return EINVAL;
8895 + }
8896 + sprintf_alloc(&path, "%s/%s", pkg->tmp_unpack_dir, script);
8897 + }
8898 +
8899 + ipkg_message(conf, IPKG_INFO, "Running script %s\n", path);
8900 + if (conf->noaction) return 0;
8901 +
8902 + /* XXX: CLEANUP: There must be a better way to handle maintainer
8903 + scripts when running with offline_root mode and/or a dest other
8904 + than '/'. I've been playing around with some clever chroot
8905 + tricks and I might come up with something workable. */
8906 + if (conf->offline_root) {
8907 + setenv("IPKG_OFFLINE_ROOT", conf->offline_root, 1);
8908 + }
8909 +
8910 + setenv("PKG_ROOT",
8911 + pkg->dest ? pkg->dest->root_dir : conf->default_dest->root_dir, 1);
8912 +
8913 + if (! file_exists(path)) {
8914 + free(path);
8915 + return 0;
8916 + }
8917 +
8918 + if (conf->offline_root) {
8919 + fprintf(stderr, "(offline root mode: not running %s.%s)\n", pkg->name, script);
8920 + free(path);
8921 + return 0;
8922 + }
8923 +
8924 + sprintf_alloc(&cmd, "%s %s", path, args);
8925 + free(path);
8926 +
8927 + err = xsystem(cmd);
8928 + free(cmd);
8929 +
8930 + if (err) {
8931 + fprintf(stderr, "%s script returned status %d\n", script, err);
8932 + return err;
8933 + }
8934 +
8935 + return 0;
8936 +}
8937 +
8938 +char *pkg_state_want_to_str(pkg_state_want_t sw)
8939 +{
8940 + int i;
8941 +
8942 + for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
8943 + if (pkg_state_want_map[i].value == sw) {
8944 + return strdup(pkg_state_want_map[i].str);
8945 + }
8946 + }
8947 +
8948 + fprintf(stderr, "%s: ERROR: Illegal value for state_want: %d\n",
8949 + __FUNCTION__, sw);
8950 + return strdup("<STATE_WANT_UNKNOWN>");
8951 +}
8952 +
8953 +pkg_state_want_t pkg_state_want_from_str(char *str)
8954 +{
8955 + int i;
8956 +
8957 + for (i=0; i < ARRAY_SIZE(pkg_state_want_map); i++) {
8958 + if (strcmp(str, pkg_state_want_map[i].str) == 0) {
8959 + return pkg_state_want_map[i].value;
8960 + }
8961 + }
8962 +
8963 + fprintf(stderr, "%s: ERROR: Illegal value for state_want string: %s\n",
8964 + __FUNCTION__, str);
8965 + return SW_UNKNOWN;
8966 +}
8967 +
8968 +char *pkg_state_flag_to_str(pkg_state_flag_t sf)
8969 +{
8970 + int i;
8971 + int len = 3; /* ok\000 is minimum */
8972 + char *str = NULL;
8973 +
8974 + /* clear the temporary flags before converting to string */
8975 + sf &= SF_NONVOLATILE_FLAGS;
8976 +
8977 + if (sf == 0) {
8978 + return strdup("ok");
8979 + } else {
8980 +
8981 + for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
8982 + if (sf & pkg_state_flag_map[i].value) {
8983 + len += strlen(pkg_state_flag_map[i].str) + 1;
8984 + }
8985 + }
8986 + str = malloc(len);
8987 + if ( str == NULL ) {
8988 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
8989 + return NULL;
8990 + }
8991 + str[0] = 0;
8992 + for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
8993 + if (sf & pkg_state_flag_map[i].value) {
8994 + strcat(str, pkg_state_flag_map[i].str);
8995 + strcat(str, ",");
8996 + }
8997 + }
8998 + len = strlen(str);
8999 + str[len-1] = 0; /* squash last comma */
9000 + return str;
9001 + }
9002 +}
9003 +
9004 +pkg_state_flag_t pkg_state_flag_from_str(char *str)
9005 +{
9006 + int i;
9007 + int sf = SF_OK;
9008 +
9009 + if (strcmp(str, "ok") == 0) {
9010 + return SF_OK;
9011 + }
9012 + for (i=0; i < ARRAY_SIZE(pkg_state_flag_map); i++) {
9013 + const char *sfname = pkg_state_flag_map[i].str;
9014 + int sfname_len = strlen(sfname);
9015 + if (strncmp(str, sfname, sfname_len) == 0) {
9016 + sf |= pkg_state_flag_map[i].value;
9017 + str += sfname_len;
9018 + if (str[0] == ',') {
9019 + str++;
9020 + } else {
9021 + break;
9022 + }
9023 + }
9024 + }
9025 +
9026 + return sf;
9027 +}
9028 +
9029 +char *pkg_state_status_to_str(pkg_state_status_t ss)
9030 +{
9031 + int i;
9032 +
9033 + for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9034 + if (pkg_state_status_map[i].value == ss) {
9035 + return strdup(pkg_state_status_map[i].str);
9036 + }
9037 + }
9038 +
9039 + fprintf(stderr, "%s: ERROR: Illegal value for state_status: %d\n",
9040 + __FUNCTION__, ss);
9041 + return strdup("<STATE_STATUS_UNKNOWN>");
9042 +}
9043 +
9044 +pkg_state_status_t pkg_state_status_from_str(char *str)
9045 +{
9046 + int i;
9047 +
9048 + for (i=0; i < ARRAY_SIZE(pkg_state_status_map); i++) {
9049 + if (strcmp(str, pkg_state_status_map[i].str) == 0) {
9050 + return pkg_state_status_map[i].value;
9051 + }
9052 + }
9053 +
9054 + fprintf(stderr, "%s: ERROR: Illegal value for state_status string: %s\n",
9055 + __FUNCTION__, str);
9056 + return SS_NOT_INSTALLED;
9057 +}
9058 +
9059 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg)
9060 +{
9061 + nv_pair_list_elt_t *l;
9062 +
9063 + if (!pkg->architecture)
9064 + return 1;
9065 +
9066 + l = conf->arch_list.head;
9067 +
9068 + while (l) {
9069 + nv_pair_t *nv = l->data;
9070 + if (strcmp(nv->name, pkg->architecture) == 0) {
9071 + ipkg_message(conf, IPKG_DEBUG, "arch %s (priority %s) supported for pkg %s\n", nv->name, nv->value, pkg->name);
9072 + return 1;
9073 + }
9074 + l = l->next;
9075 + }
9076 +
9077 + ipkg_message(conf, IPKG_DEBUG, "arch %s unsupported for pkg %s\n", pkg->architecture, pkg->name);
9078 + return 0;
9079 +}
9080 +
9081 +int pkg_get_arch_priority(ipkg_conf_t *conf, const char *archname)
9082 +{
9083 + nv_pair_list_elt_t *l;
9084 +
9085 + l = conf->arch_list.head;
9086 +
9087 + while (l) {
9088 + nv_pair_t *nv = l->data;
9089 + if (strcmp(nv->name, archname) == 0) {
9090 + int priority = strtol(nv->value, NULL, 0);
9091 + return priority;
9092 + }
9093 + l = l->next;
9094 + }
9095 + return 0;
9096 +}
9097 +
9098 +int pkg_info_preinstall_check(ipkg_conf_t *conf)
9099 +{
9100 + int i;
9101 + hash_table_t *pkg_hash = &conf->pkg_hash;
9102 + pkg_vec_t *available_pkgs = pkg_vec_alloc();
9103 + pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9104 +
9105 + ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: updating arch priority for each package\n");
9106 + pkg_hash_fetch_available(pkg_hash, available_pkgs);
9107 + /* update arch_priority for each package */
9108 + for (i = 0; i < available_pkgs->len; i++) {
9109 + pkg_t *pkg = available_pkgs->pkgs[i];
9110 + int arch_priority = 1;
9111 + if (!pkg)
9112 + continue;
9113 + // ipkg_message(conf, IPKG_DEBUG2, " package %s version=%s arch=%p:", pkg->name, pkg->version, pkg->architecture);
9114 + if (pkg->architecture)
9115 + arch_priority = pkg_get_arch_priority(conf, pkg->architecture);
9116 + else
9117 + ipkg_message(conf, IPKG_ERROR, "pkg_info_preinstall_check: no architecture for package %s\n", pkg->name);
9118 + // ipkg_message(conf, IPKG_DEBUG2, "%s arch_priority=%d\n", pkg->architecture, arch_priority);
9119 + pkg->arch_priority = arch_priority;
9120 + }
9121 +
9122 + for (i = 0; i < available_pkgs->len; i++) {
9123 + pkg_t *pkg = available_pkgs->pkgs[i];
9124 + if (!pkg->arch_priority && (pkg->state_flag || (pkg->state_want != SW_UNKNOWN))) {
9125 + /* clear flags and want for any uninstallable package */
9126 + ipkg_message(conf, IPKG_NOTICE, "Clearing state_want and state_flag for pkg=%s (arch_priority=%d flag=%d want=%d)\n",
9127 + pkg->name, pkg->arch_priority, pkg->state_flag, pkg->state_want);
9128 + pkg->state_want = SW_UNKNOWN;
9129 + pkg->state_flag = 0;
9130 + }
9131 + }
9132 + pkg_vec_free(available_pkgs);
9133 +
9134 + /* update the file owner data structure */
9135 + ipkg_message(conf, IPKG_INFO, "pkg_info_preinstall_check: update file owner list\n");
9136 + pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9137 + for (i = 0; i < installed_pkgs->len; i++) {
9138 + pkg_t *pkg = installed_pkgs->pkgs[i];
9139 + str_list_t *installed_files = pkg_get_installed_files(pkg); /* this causes installed_files to be cached */
9140 + str_list_elt_t *iter;
9141 + if (installed_files == NULL) {
9142 + ipkg_message(conf, IPKG_ERROR, "No installed files for pkg %s\n", pkg->name);
9143 + break;
9144 + }
9145 + for (iter = installed_files->head; iter; iter = iter->next) {
9146 + char *installed_file = iter->data;
9147 + // ipkg_message(conf, IPKG_DEBUG2, "pkg %s: file=%s\n", pkg->name, installed_file);
9148 + file_hash_set_file_owner(conf, installed_file, pkg);
9149 + }
9150 + }
9151 + pkg_vec_free(installed_pkgs);
9152 +
9153 + return 0;
9154 +}
9155 +
9156 +struct pkg_write_filelist_data {
9157 + ipkg_conf_t *conf;
9158 + pkg_t *pkg;
9159 + FILE *stream;
9160 +};
9161 +
9162 +void pkg_write_filelist_helper(const char *key, void *entry_, void *data_)
9163 +{
9164 + struct pkg_write_filelist_data *data = data_;
9165 + pkg_t *entry = entry_;
9166 + if (entry == data->pkg) {
9167 + fprintf(data->stream, "%s\n", key);
9168 + }
9169 +}
9170 +
9171 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg)
9172 +{
9173 + struct pkg_write_filelist_data data;
9174 + char *list_file_name = NULL;
9175 + int err = 0;
9176 +
9177 + if (!pkg) {
9178 + ipkg_message(conf, IPKG_ERROR, "Null pkg\n");
9179 + return -EINVAL;
9180 + }
9181 + ipkg_message(conf, IPKG_INFO,
9182 + " creating %s.list file\n", pkg->name);
9183 + sprintf_alloc(&list_file_name, "%s/%s.list", pkg->dest->info_dir, pkg->name);
9184 + if (!list_file_name) {
9185 + ipkg_message(conf, IPKG_ERROR, "Failed to alloc list_file_name\n");
9186 + return -ENOMEM;
9187 + }
9188 + ipkg_message(conf, IPKG_INFO,
9189 + " creating %s file for pkg %s\n", list_file_name, pkg->name);
9190 + data.stream = fopen(list_file_name, "w");
9191 + if (!data.stream) {
9192 + ipkg_message(conf, IPKG_ERROR, "Could not open %s for writing: %s\n",
9193 + list_file_name, strerror(errno));
9194 + return errno;
9195 + }
9196 + data.pkg = pkg;
9197 + data.conf = conf;
9198 + hash_table_foreach(&conf->file_hash, pkg_write_filelist_helper, &data);
9199 + fclose(data.stream);
9200 + free(list_file_name);
9201 +
9202 + return err;
9203 +}
9204 +
9205 +int pkg_write_changed_filelists(ipkg_conf_t *conf)
9206 +{
9207 + pkg_vec_t *installed_pkgs = pkg_vec_alloc();
9208 + hash_table_t *pkg_hash = &conf->pkg_hash;
9209 + int i;
9210 + int err;
9211 + if (conf->noaction)
9212 + return 0;
9213 +
9214 + ipkg_message(conf, IPKG_INFO, "%s: saving changed filelists\n", __FUNCTION__);
9215 + pkg_hash_fetch_all_installed(pkg_hash, installed_pkgs);
9216 + for (i = 0; i < installed_pkgs->len; i++) {
9217 + pkg_t *pkg = installed_pkgs->pkgs[i];
9218 + if (pkg->state_flag & SF_FILELIST_CHANGED) {
9219 + ipkg_message(conf, IPKG_DEBUG, "Calling pkg_write_filelist for pkg=%s from %s\n", pkg->name, __FUNCTION__);
9220 + err = pkg_write_filelist(conf, pkg);
9221 + if (err)
9222 + ipkg_message(conf, IPKG_NOTICE, "pkg_write_filelist pkg=%s returned %d\n", pkg->name, err);
9223 + }
9224 + }
9225 + return 0;
9226 +}
9227 --- /dev/null
9228 +++ b/archival/libipkg/pkg_depends.c
9229 @@ -0,0 +1,1031 @@
9230 +/* pkg_depends.c - the itsy package management system
9231 +
9232 + Steven M. Ayer
9233 +
9234 + Copyright (C) 2002 Compaq Computer Corporation
9235 +
9236 + This program is free software; you can redistribute it and/or
9237 + modify it under the terms of the GNU General Public License as
9238 + published by the Free Software Foundation; either version 2, or (at
9239 + your option) any later version.
9240 +
9241 + This program is distributed in the hope that it will be useful, but
9242 + WITHOUT ANY WARRANTY; without even the implied warranty of
9243 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9244 + General Public License for more details.
9245 +*/
9246 +
9247 +#include "ipkg.h"
9248 +#include <errno.h>
9249 +#include <ctype.h>
9250 +
9251 +#include "pkg.h"
9252 +#include "ipkg_utils.h"
9253 +#include "pkg_hash.h"
9254 +#include "ipkg_message.h"
9255 +#include "pkg_parse.h"
9256 +#include "hash_table.h"
9257 +
9258 +static int parseDepends(compound_depend_t *compound_depend, hash_table_t * hash, char * depend_str);
9259 +static depend_t * depend_init(void);
9260 +static void depend_deinit(depend_t *d);
9261 +static char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx);
9262 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff);
9263 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg);
9264 +
9265 +static int pkg_installed_and_constraint_satisfied(pkg_t *pkg, void *cdata)
9266 +{
9267 + depend_t *depend = (depend_t *)cdata;
9268 + if ((pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) && version_constraints_satisfied(depend, pkg))
9269 + return 1;
9270 + else
9271 + return 0;
9272 +}
9273 +
9274 +static int pkg_constraint_satisfied(pkg_t *pkg, void *cdata)
9275 +{
9276 + depend_t *depend = (depend_t *)cdata;
9277 +#if 0
9278 + pkg_t * temp = pkg_new();
9279 + int comparison;
9280 + parseVersion(temp, depend->version);
9281 + comparison = pkg_compare_versions(pkg, temp);
9282 + free(temp);
9283 +
9284 + fprintf(stderr, "%s: pkg=%s pkg->version=%s constraint=%p type=%d version=%s comparison=%d satisfied=%d\n",
9285 + __FUNCTION__, pkg->name, pkg->version,
9286 + depend, depend->constraint, depend->version,
9287 + comparison, version_constraints_satisfied(depend, pkg));
9288 +#endif
9289 + if (version_constraints_satisfied(depend, pkg))
9290 + return 1;
9291 + else
9292 + return 0;
9293 +}
9294 +
9295 +/* returns ndependences or negative error value */
9296 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg,
9297 + pkg_vec_t *unsatisfied, char *** unresolved)
9298 +{
9299 + pkg_t * satisfier_entry_pkg;
9300 + register int i, j, k, l;
9301 + int count, found;
9302 + char ** the_lost;
9303 + abstract_pkg_t * ab_pkg;
9304 +
9305 + /*
9306 + * this is a setup to check for redundant/cyclic dependency checks,
9307 + * which are marked at the abstract_pkg level
9308 + */
9309 + if (!(ab_pkg = pkg->parent)) {
9310 + fprintf(stderr, "%s:%d: something terribly wrong with pkg %s\n", __FUNCTION__, __LINE__, pkg->name);
9311 + *unresolved = NULL;
9312 + return 0;
9313 + }
9314 + if (ab_pkg->dependencies_checked) { /* avoid duplicate or cyclic checks */
9315 + *unresolved = NULL;
9316 + return 0;
9317 + } else {
9318 + ab_pkg->dependencies_checked = 1; /* mark it for subsequent visits */
9319 + }
9320 + /**/
9321 +
9322 + count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count;
9323 + if (!count){
9324 + *unresolved = NULL;
9325 + return 0;
9326 + }
9327 +
9328 + the_lost = NULL;
9329 +
9330 + /* foreach dependency */
9331 + for (i = 0; i < count; i++) {
9332 + compound_depend_t * compound_depend = &pkg->depends[i];
9333 + depend_t ** possible_satisfiers = compound_depend->possibilities;;
9334 + found = 0;
9335 + satisfier_entry_pkg = NULL;
9336 +
9337 + if (compound_depend->type == GREEDY_DEPEND) {
9338 + /* foreach possible satisfier */
9339 + for (j = 0; j < compound_depend->possibility_count; j++) {
9340 + /* foreach provided_by, which includes the abstract_pkg itself */
9341 + abstract_pkg_t *abpkg = possible_satisfiers[j]->pkg;
9342 + abstract_pkg_vec_t *ab_provider_vec = abpkg->provided_by;
9343 + int nposs = ab_provider_vec->len;
9344 + abstract_pkg_t **ab_providers = ab_provider_vec->pkgs;
9345 + for (l = 0; l < nposs; l++) {
9346 + pkg_vec_t *test_vec = ab_providers[l]->pkgs;
9347 + /* if no depends on this one, try the first package that Provides this one */
9348 + if (!test_vec){ /* no pkg_vec hooked up to the abstract_pkg! (need another feed?) */
9349 + continue;
9350 + }
9351 +
9352 + /* cruise this possiblity's pkg_vec looking for an installed version */
9353 + for (k = 0; k < test_vec->len; k++) {
9354 + pkg_t *pkg_scout = test_vec->pkgs[k];
9355 + /* not installed, and not already known about? */
9356 + if ((pkg_scout->state_want != SW_INSTALL)
9357 + && !pkg_scout->parent->dependencies_checked
9358 + && !is_pkg_in_pkg_vec(unsatisfied, pkg_scout)) {
9359 + char ** newstuff = NULL;
9360 + int rc;
9361 + pkg_vec_t *tmp_vec = pkg_vec_alloc ();
9362 + /* check for not-already-installed dependencies */
9363 + rc = pkg_hash_fetch_unsatisfied_dependencies(conf,
9364 + pkg_scout,
9365 + tmp_vec,
9366 + &newstuff);
9367 + if (newstuff == NULL) {
9368 + int ok = 1;
9369 + for (l = 0; l < rc; l++) {
9370 + pkg_t *p = tmp_vec->pkgs[l];
9371 + if (p->state_want == SW_INSTALL)
9372 + continue;
9373 + ipkg_message(conf, IPKG_DEBUG, "not installing %s due to requirement for %s\n", pkg_scout->name, p->name);
9374 + ok = 0;
9375 + break;
9376 + }
9377 + pkg_vec_free (tmp_vec);
9378 + if (ok) {
9379 + /* mark this one for installation */
9380 + ipkg_message(conf, IPKG_NOTICE, "Adding satisfier for greedy dependence: %s\n", pkg_scout->name);
9381 + pkg_vec_insert(unsatisfied, pkg_scout);
9382 + }
9383 + } else {
9384 + ipkg_message(conf, IPKG_DEBUG, "not installing %s due to broken depends \n", pkg_scout->name);
9385 + free (newstuff);
9386 + }
9387 + }
9388 + }
9389 + }
9390 + }
9391 +
9392 + continue;
9393 + }
9394 +
9395 + /* foreach possible satisfier, look for installed package */
9396 + for (j = 0; j < compound_depend->possibility_count; j++) {
9397 + /* foreach provided_by, which includes the abstract_pkg itself */
9398 + depend_t *dependence_to_satisfy = possible_satisfiers[j];
9399 + abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9400 + pkg_t *satisfying_pkg =
9401 + pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg,
9402 + pkg_installed_and_constraint_satisfied,
9403 + dependence_to_satisfy, 1);
9404 + /* Being that I can't test constraing in pkg_hash, I will test it here */
9405 + if (satisfying_pkg != NULL) {
9406 + if (!pkg_installed_and_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9407 + satisfying_pkg = NULL;
9408 + }
9409 + }
9410 + ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p \n", __FILE__, __LINE__, satisfying_pkg);
9411 + if (satisfying_pkg != NULL) {
9412 + found = 1;
9413 + break;
9414 + }
9415 +
9416 + }
9417 + /* if nothing installed matches, then look for uninstalled satisfier */
9418 + if (!found) {
9419 + /* foreach possible satisfier, look for installed package */
9420 + for (j = 0; j < compound_depend->possibility_count; j++) {
9421 + /* foreach provided_by, which includes the abstract_pkg itself */
9422 + depend_t *dependence_to_satisfy = possible_satisfiers[j];
9423 + abstract_pkg_t *satisfying_apkg = possible_satisfiers[j]->pkg;
9424 + pkg_t *satisfying_pkg =
9425 + pkg_hash_fetch_best_installation_candidate(conf, satisfying_apkg,
9426 + pkg_constraint_satisfied,
9427 + dependence_to_satisfy, 1);
9428 + /* Being that I can't test constraing in pkg_hash, I will test it here too */
9429 + if (satisfying_pkg != NULL) {
9430 + if (!pkg_constraint_satisfied ( satisfying_pkg,dependence_to_satisfy)) {
9431 + satisfying_pkg = NULL;
9432 + }
9433 + }
9434 +
9435 + /* user request overrides package recommendation */
9436 + if (satisfying_pkg != NULL
9437 + && (compound_depend->type == RECOMMEND || compound_depend->type == SUGGEST)
9438 + && (satisfying_pkg->state_want == SW_DEINSTALL || satisfying_pkg->state_want == SW_PURGE)) {
9439 + ipkg_message (conf, IPKG_NOTICE, "%s: ignoring recommendation for %s at user request\n",
9440 + pkg->name, satisfying_pkg->name);
9441 + continue;
9442 + }
9443 +
9444 + ipkg_message(conf, IPKG_DEBUG, "%s:%d: satisfying_pkg=%p\n", __FILE__, __LINE__, satisfying_pkg);
9445 + if (satisfying_pkg != NULL) {
9446 + satisfier_entry_pkg = satisfying_pkg;
9447 + break;
9448 + }
9449 + }
9450 + }
9451 +
9452 + /* we didn't find one, add something to the unsatisfied vector */
9453 + if (!found) {
9454 + if (!satisfier_entry_pkg) {
9455 + /* failure to meet recommendations is not an error */
9456 + if (compound_depend->type != RECOMMEND && compound_depend->type != SUGGEST)
9457 + the_lost = add_unresolved_dep(pkg, the_lost, i);
9458 + else
9459 + ipkg_message (conf, IPKG_NOTICE, "%s: unsatisfied recommendation for %s\n",
9460 + pkg->name, compound_depend->possibilities[0]->pkg->name);
9461 + }
9462 + else {
9463 + if (compound_depend->type == SUGGEST) {
9464 + /* just mention it politely */
9465 + ipkg_message (conf, IPKG_NOTICE, "package %s suggests installing %s\n",
9466 + pkg->name, satisfier_entry_pkg->name);
9467 + } else {
9468 + char ** newstuff = NULL;
9469 +
9470 + if (satisfier_entry_pkg != pkg &&
9471 + !is_pkg_in_pkg_vec(unsatisfied, satisfier_entry_pkg)) {
9472 + pkg_vec_insert(unsatisfied, satisfier_entry_pkg);
9473 + pkg_hash_fetch_unsatisfied_dependencies(conf,
9474 + satisfier_entry_pkg,
9475 + unsatisfied,
9476 + &newstuff);
9477 + the_lost = merge_unresolved(the_lost, newstuff);
9478 + }
9479 + }
9480 + }
9481 + }
9482 + }
9483 + *unresolved = the_lost;
9484 +
9485 + return unsatisfied->len;
9486 +}
9487 +
9488 +/*checking for conflicts !in replaces
9489 + If a packages conflicts with another but is also replacing it, I should not consider it a
9490 + really conflicts
9491 + returns 0 if conflicts <> replaces or 1 if conflicts == replaces
9492 +*/
9493 +int is_pkg_a_replaces(pkg_t *pkg_scout,pkg_t *pkg)
9494 +{
9495 + int i ;
9496 + int replaces_count = pkg->replaces_count;
9497 + abstract_pkg_t **replaces;
9498 +
9499 + if (pkg->replaces_count==0) // No replaces, it's surely a conflict
9500 + return 0;
9501 +
9502 + replaces = pkg->replaces;
9503 +
9504 + for (i = 0; i < replaces_count; i++) {
9505 + if (strcmp(pkg_scout->name,pkg->replaces[i]->name)==0) { // Found
9506 + ipkg_message(NULL, IPKG_DEBUG2, "Seems I've found a replace %s %s \n",pkg_scout->name,pkg->replaces[i]->name);
9507 + return 1;
9508 + }
9509 + }
9510 + return 0;
9511 +
9512 +}
9513 +
9514 +
9515 +/* Abhaya: added support for conflicts */
9516 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg)
9517 +{
9518 + pkg_vec_t * installed_conflicts, * test_vec;
9519 + compound_depend_t * conflicts;
9520 + depend_t ** possible_satisfiers;
9521 + depend_t * possible_satisfier;
9522 + register int i, j, k;
9523 + int count;
9524 + abstract_pkg_t * ab_pkg;
9525 + pkg_t **pkg_scouts;
9526 + pkg_t *pkg_scout;
9527 +
9528 + /*
9529 + * this is a setup to check for redundant/cyclic dependency checks,
9530 + * which are marked at the abstract_pkg level
9531 + */
9532 + if(!(ab_pkg = pkg->parent)){
9533 + fprintf(stderr, "dependency check error. pkg %s isn't in hash table\n", pkg->name);
9534 + return (pkg_vec_t *)NULL;
9535 + }
9536 +
9537 + conflicts = pkg->conflicts;
9538 + if(!conflicts){
9539 + return (pkg_vec_t *)NULL;
9540 + }
9541 + installed_conflicts = pkg_vec_alloc();
9542 +
9543 + count = pkg->conflicts_count;
9544 +
9545 +
9546 +
9547 + /* foreach conflict */
9548 + for(i = 0; i < pkg->conflicts_count; i++){
9549 +
9550 + possible_satisfiers = conflicts->possibilities;
9551 +
9552 + /* foreach possible satisfier */
9553 + for(j = 0; j < conflicts->possibility_count; j++){
9554 + possible_satisfier = possible_satisfiers[j];
9555 + if (!possible_satisfier)
9556 + fprintf(stderr, "%s:%d: possible_satisfier is null\n", __FUNCTION__, __LINE__);
9557 + if (!possible_satisfier->pkg)
9558 + fprintf(stderr, "%s:%d: possible_satisfier->pkg is null\n", __FUNCTION__, __LINE__);
9559 + test_vec = possible_satisfier->pkg->pkgs;
9560 + if (test_vec) {
9561 + /* pkg_vec found, it is an actual package conflict
9562 + * cruise this possiblity's pkg_vec looking for an installed version */
9563 + pkg_scouts = test_vec->pkgs;
9564 + for(k = 0; k < test_vec->len; k++){
9565 + pkg_scout = pkg_scouts[k];
9566 + if (!pkg_scout) {
9567 + fprintf(stderr, "%s: null pkg scout\n", __FUNCTION__);
9568 + continue;
9569 + }
9570 + if ((pkg_scout->state_status == SS_INSTALLED || pkg_scout->state_want == SW_INSTALL) &&
9571 + version_constraints_satisfied(possible_satisfier, pkg_scout) && !is_pkg_a_replaces(pkg_scout,pkg)){
9572 + if (!is_pkg_in_pkg_vec(installed_conflicts, pkg_scout)){
9573 + pkg_vec_insert(installed_conflicts, pkg_scout);
9574 + }
9575 + }
9576 + }
9577 + }
9578 + }
9579 + conflicts++;
9580 + }
9581 +
9582 + if (installed_conflicts->len)
9583 + return installed_conflicts;
9584 + pkg_vec_free(installed_conflicts);
9585 + return (pkg_vec_t *)NULL;
9586 +}
9587 +
9588 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg)
9589 +{
9590 + pkg_t * temp;
9591 + int comparison;
9592 +
9593 + if(depends->constraint == NONE)
9594 + return 1;
9595 +
9596 + temp = pkg_new();
9597 +
9598 + parseVersion(temp, depends->version);
9599 +
9600 + comparison = pkg_compare_versions(pkg, temp);
9601 +
9602 + free(temp);
9603 +
9604 + if((depends->constraint == EARLIER) &&
9605 + (comparison < 0))
9606 + return 1;
9607 + else if((depends->constraint == LATER) &&
9608 + (comparison > 0))
9609 + return 1;
9610 + else if(comparison == 0)
9611 + return 1;
9612 + else if((depends->constraint == LATER_EQUAL) &&
9613 + (comparison >= 0))
9614 + return 1;
9615 + else if((depends->constraint == EARLIER_EQUAL) &&
9616 + (comparison <= 0))
9617 + return 1;
9618 +
9619 + return 0;
9620 +}
9621 +
9622 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend)
9623 +{
9624 + abstract_pkg_t *apkg = depend->pkg;
9625 + abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9626 + int n_providers = provider_apkgs->len;
9627 + abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9628 + pkg_vec_t *pkg_vec;
9629 + int n_pkgs ;
9630 + int i;
9631 + int j;
9632 +
9633 + for (i = 0; i < n_providers; i++) {
9634 + abstract_pkg_t *papkg = apkgs[i];
9635 + pkg_vec = papkg->pkgs;
9636 + if (pkg_vec) {
9637 + n_pkgs = pkg_vec->len;
9638 + for (j = 0; j < n_pkgs; j++) {
9639 + pkg_t *pkg = pkg_vec->pkgs[j];
9640 + if (version_constraints_satisfied(depend, pkg)) {
9641 + return 1;
9642 + }
9643 + }
9644 + }
9645 + }
9646 + return 0;
9647 +}
9648 +
9649 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend)
9650 +{
9651 + abstract_pkg_t *apkg = depend->pkg;
9652 + abstract_pkg_vec_t *provider_apkgs = apkg->provided_by;
9653 + int n_providers = provider_apkgs->len;
9654 + abstract_pkg_t **apkgs = provider_apkgs->pkgs;
9655 + int i;
9656 + int n_pkgs;
9657 + int j;
9658 +
9659 + for (i = 0; i < n_providers; i++) {
9660 + abstract_pkg_t *papkg = apkgs[i];
9661 + pkg_vec_t *pkg_vec = papkg->pkgs;
9662 + if (pkg_vec) {
9663 + n_pkgs = pkg_vec->len;
9664 + for (j = 0; j < n_pkgs; j++) {
9665 + pkg_t *pkg = pkg_vec->pkgs[j];
9666 + if (version_constraints_satisfied(depend, pkg)) {
9667 + if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED)
9668 + return 1;
9669 + }
9670 + }
9671 + }
9672 + }
9673 + return 0;
9674 +}
9675 +
9676 +static int is_pkg_in_pkg_vec(pkg_vec_t * vec, pkg_t * pkg)
9677 +{
9678 + register int i;
9679 + pkg_t ** pkgs = vec->pkgs;
9680 +
9681 + for(i = 0; i < vec->len; i++)
9682 + if((strcmp(pkg->name, (*(pkgs + i))->name) == 0)
9683 + && (pkg_compare_versions(pkg, *(pkgs + i)) == 0)
9684 + && (strcmp(pkg->architecture, (*(pkgs + i))->architecture) == 0))
9685 + return 1;
9686 + return 0;
9687 +}
9688 +
9689 +
9690 +#ifdef DeadCode
9691 +/**
9692 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
9693 + * the same abstract package and 0 otherwise.
9694 + */
9695 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee)
9696 +{
9697 + abstract_pkg_t **provides = pkg->provides;
9698 + int provides_count = pkg->provides_count;
9699 + abstract_pkg_t **replacee_provides = replacee->provides;
9700 + int replacee_provides_count = replacee->provides_count;
9701 + int i, j;
9702 + for (i = 0; i < provides_count; i++) {
9703 + abstract_pkg_t *apkg = provides[i];
9704 + for (j = 0; j < replacee_provides_count; j++) {
9705 + abstract_pkg_t *replacee_apkg = replacee_provides[i];
9706 + if (apkg == replacee_apkg)
9707 + return 1;
9708 + }
9709 + }
9710 + return 0;
9711 +}
9712 +#endif
9713 +
9714 +/**
9715 + * pkg_provides_abstract returns 1 if pkg->provides contains providee
9716 + * and 0 otherwise.
9717 + */
9718 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee)
9719 +{
9720 + abstract_pkg_t **provides = pkg->provides;
9721 + int provides_count = pkg->provides_count;
9722 + int i;
9723 + for (i = 0; i < provides_count; i++) {
9724 + if (provides[i] == providee)
9725 + return 1;
9726 + }
9727 + return 0;
9728 +}
9729 +
9730 +/**
9731 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
9732 + * otherwise.
9733 + */
9734 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee)
9735 +{
9736 + abstract_pkg_t **replaces = pkg->replaces;
9737 + int replaces_count = pkg->replaces_count;
9738 + /* abstract_pkg_t **replacee_provides = pkg->provides;
9739 + int replacee_provides_count = pkg->provides_count; */
9740 + int i, j;
9741 + for (i = 0; i < replaces_count; i++) {
9742 + abstract_pkg_t *abstract_replacee = replaces[i];
9743 + for (j = 0; j < replaces_count; j++) {
9744 + /* ipkg_message(NULL, IPKG_DEBUG2, "Searching pkg-name %s repprovname %s absrepname %s \n",
9745 + pkg->name,replacee->provides[j]->name, abstract_replacee->name); */
9746 + if (replacee->provides[j] == abstract_replacee)
9747 + return 1;
9748 + }
9749 + }
9750 + return 0;
9751 +}
9752 +
9753 +
9754 +/**
9755 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee and 0
9756 + * otherwise.
9757 + */
9758 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflictee)
9759 +{
9760 + compound_depend_t *conflicts = pkg->conflicts;
9761 + int conflicts_count = pkg->conflicts_count;
9762 + int i, j;
9763 + for (i = 0; i < conflicts_count; i++) {
9764 + int possibility_count = conflicts[i].possibility_count;
9765 + struct depend **possibilities = conflicts[i].possibilities;
9766 + for (j = 0; j < possibility_count; j++) {
9767 + if (possibilities[j]->pkg == conflictee) {
9768 + return 1;
9769 + }
9770 + }
9771 + }
9772 + return 0;
9773 +}
9774 +
9775 +/**
9776 + * pkg_conflicts returns 1 if pkg->conflicts contains one of
9777 + * conflictee's provides and 0 otherwise.
9778 + */
9779 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflictee)
9780 +{
9781 + compound_depend_t *conflicts = pkg->conflicts;
9782 + int conflicts_count = pkg->conflicts_count;
9783 + abstract_pkg_t **conflictee_provides = conflictee->provides;
9784 + int conflictee_provides_count = conflictee->provides_count;
9785 + int i, j, k;
9786 + int possibility_count;
9787 + struct depend **possibilities;
9788 + abstract_pkg_t *possibility ;
9789 +
9790 + for (i = 0; i < conflicts_count; i++) {
9791 + possibility_count = conflicts[i].possibility_count;
9792 + possibilities = conflicts[i].possibilities;
9793 + for (j = 0; j < possibility_count; j++) {
9794 + possibility = possibilities[j]->pkg;
9795 + for (k = 0; k < conflictee_provides_count; k++) {
9796 + if (possibility == conflictee_provides[k]) {
9797 + return 1;
9798 + }
9799 + }
9800 + }
9801 + }
9802 + return 0;
9803 +}
9804 +
9805 +static char ** merge_unresolved(char ** oldstuff, char ** newstuff)
9806 +{
9807 + int oldlen = 0, newlen = 0;
9808 + char ** result;
9809 + register int i, j;
9810 +
9811 + if(!newstuff)
9812 + return oldstuff;
9813 +
9814 + while(oldstuff && oldstuff[oldlen]) oldlen++;
9815 + while(newstuff && newstuff[newlen]) newlen++;
9816 +
9817 + result = (char **)realloc(oldstuff, sizeof(char *) * (oldlen + newlen + 1));
9818 + if (result == NULL) {
9819 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9820 + return NULL;
9821 + }
9822 +
9823 + for(i = oldlen, j = 0; i < (oldlen + newlen); i++, j++)
9824 + *(result + i) = *(newstuff + j);
9825 +
9826 + *(result + i) = NULL;
9827 +
9828 + return result;
9829 +}
9830 +
9831 +/*
9832 + * a kinda kludgy way to back out depends str from two different arrays (reg'l'r 'n pre)
9833 + * this is null terminated, no count is carried around
9834 + */
9835 +char ** add_unresolved_dep(pkg_t * pkg, char ** the_lost, int ref_ndx)
9836 +{
9837 + int count;
9838 + char ** resized;
9839 + char *depend_str = pkg_depend_str(pkg, ref_ndx);
9840 +
9841 + count = 0;
9842 + while(the_lost && the_lost[count]) count++;
9843 +
9844 + count++; /* need one to hold the null */
9845 + resized = (char **)realloc(the_lost, sizeof(char *) * (count + 1));
9846 + if (resized == NULL) {
9847 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9848 + return NULL;
9849 + }
9850 + resized[count - 1] = strdup(depend_str);
9851 + resized[count] = NULL;
9852 +
9853 + return resized;
9854 +}
9855 +
9856 +void printDepends(pkg_t * pkg)
9857 +{
9858 + register int i, j;
9859 + compound_depend_t * depend;
9860 + int count;
9861 +
9862 + count = pkg->pre_depends_count + pkg->depends_count;
9863 +
9864 + depend = pkg->depends;
9865 + if(!depend){
9866 + fprintf(stderr, "Depends pointer is NULL\n");
9867 + return;
9868 + }
9869 + for(i = 0; i < count; i++){
9870 + fprintf(stderr, "%s has %d possibilities:\n",
9871 + (depend->type == GREEDY_DEPEND) ? "Greedy-Depend" : ((depend->type == DEPEND) ? "Depend" : "Pre-Depend"),
9872 + depend->possibility_count);
9873 + for(j = 0; j < depend->possibility_count; j++)
9874 + fprintf(stderr, "\t%s version %s (%d)\n",
9875 + depend->possibilities[j]->pkg->name,
9876 + depend->possibilities[j]->version,
9877 + depend->possibilities[j]->constraint);
9878 + depend++;
9879 + }
9880 +}
9881 +
9882 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
9883 +{
9884 + register int i, j;
9885 +
9886 + /* every pkg provides itself */
9887 + abstract_pkg_vec_insert(ab_pkg->provided_by, ab_pkg);
9888 +
9889 + if (!pkg->provides_count)
9890 + return 0;
9891 +
9892 + pkg->provides = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * (pkg->provides_count + 1));
9893 + if (pkg->provides == NULL) {
9894 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9895 + return -1 ;
9896 + }
9897 + pkg->provides[0] = ab_pkg;
9898 +
9899 + // if (strcmp(ab_pkg->name, pkg->name))
9900 + // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
9901 +
9902 + for(i = 0; i < pkg->provides_count; i++){
9903 + abstract_pkg_t *provided_abpkg = ensure_abstract_pkg_by_name(hash, pkg->provides_str[i]);
9904 +
9905 + pkg->provides[i+1] = provided_abpkg;
9906 +
9907 + j = 0;
9908 + abstract_pkg_vec_insert(provided_abpkg->provided_by, ab_pkg);
9909 + }
9910 + return 0;
9911 +}
9912 +
9913 +/* Abhaya: added conflicts support */
9914 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
9915 +{
9916 + register int i;
9917 + compound_depend_t * conflicts;
9918 +
9919 + if (!pkg->conflicts_count)
9920 + return 0;
9921 +
9922 + conflicts = pkg->conflicts = malloc(sizeof(compound_depend_t) *
9923 + pkg->conflicts_count);
9924 + if (conflicts == NULL) {
9925 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9926 + return -1;
9927 + }
9928 + for (i = 0; i < pkg->conflicts_count; i++) {
9929 + conflicts->type = CONFLICTS;
9930 + parseDepends(conflicts, hash,
9931 + pkg->conflicts_str[i]);
9932 +#if 0
9933 + for (j = 0; j < conflicts->possibility_count; j++) {
9934 + depend_t *possibility = conflicts->possibilities[j];
9935 + abstract_pkg_t *conflicting_apkg = possibility->pkg;
9936 + pkg_add_conflict_pair(ab_pkg, conflicting_apkg);
9937 + }
9938 +#endif
9939 + conflicts++;
9940 + }
9941 + return 0;
9942 +}
9943 +
9944 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg)
9945 +{
9946 + register int i, j;
9947 +
9948 + if (!pkg->replaces_count)
9949 + return 0;
9950 +
9951 + pkg->replaces = (abstract_pkg_t **)malloc(sizeof(abstract_pkg_t *) * pkg->replaces_count);
9952 + if (pkg->replaces == NULL) {
9953 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9954 + return -1;
9955 + }
9956 +
9957 + // if (strcmp(ab_pkg->name, pkg->name))
9958 + // fprintf(stderr, __FUNCTION__ ": ab_pkg=%s pkg=%s\n", ab_pkg->name, pkg->name);
9959 +
9960 + for(i = 0; i < pkg->replaces_count; i++){
9961 + abstract_pkg_t *old_abpkg = ensure_abstract_pkg_by_name(hash, pkg->replaces_str[i]);
9962 +
9963 + pkg->replaces[i] = old_abpkg;
9964 +
9965 + j = 0;
9966 + if (!old_abpkg->replaced_by)
9967 + old_abpkg->replaced_by = abstract_pkg_vec_alloc();
9968 + if ( old_abpkg->replaced_by == NULL ){
9969 + return -1;
9970 + }
9971 + /* if a package pkg both replaces and conflicts old_abpkg,
9972 + * then add it to the replaced_by vector so that old_abpkg
9973 + * will be upgraded to ab_pkg automatically */
9974 + if (pkg_conflicts_abstract(pkg, old_abpkg))
9975 + abstract_pkg_vec_insert(old_abpkg->replaced_by, ab_pkg);
9976 + }
9977 + return 0;
9978 +}
9979 +
9980 +int buildDepends(hash_table_t * hash, pkg_t * pkg)
9981 +{
9982 + int count;
9983 + register int i;
9984 + compound_depend_t * depends;
9985 +
9986 + if(!(count = pkg->pre_depends_count + pkg->depends_count + pkg->recommends_count + pkg->suggests_count))
9987 + return 0;
9988 +
9989 + if (0 && pkg->pre_depends_count)
9990 + fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
9991 + pkg->name, pkg->pre_depends_count, pkg->depends_count);
9992 + depends = pkg->depends = malloc(sizeof(compound_depend_t) * count);
9993 + if (depends == NULL) {
9994 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
9995 + return -1;
9996 + }
9997 +
9998 +
9999 + for(i = 0; i < pkg->pre_depends_count; i++){
10000 + parseDepends(depends, hash, pkg->pre_depends_str[i]);
10001 + if (0 && pkg->pre_depends_count)
10002 + fprintf(stderr, " pre_depends_str=%s depends=%p possibility_count=%x\n",
10003 + pkg->pre_depends_str[i], depends, depends->possibility_count);
10004 + depends->type = PREDEPEND;
10005 + depends++;
10006 + }
10007 +
10008 + for(i = 0; i < pkg->recommends_count; i++){
10009 + parseDepends(depends, hash, pkg->recommends_str[i]);
10010 + if (0 && pkg->recommends_count)
10011 + fprintf(stderr, " recommends_str=%s depends=%p possibility_count=%x\n",
10012 + pkg->recommends_str[i], depends, depends->possibility_count);
10013 + depends->type = RECOMMEND;
10014 + depends++;
10015 + }
10016 +
10017 + for(i = 0; i < pkg->suggests_count; i++){
10018 + parseDepends(depends, hash, pkg->suggests_str[i]);
10019 + if (0 && pkg->suggests_count)
10020 + fprintf(stderr, " suggests_str=%s depends=%p possibility_count=%x\n",
10021 + pkg->suggests_str[i], depends, depends->possibility_count);
10022 + depends->type = SUGGEST;
10023 + depends++;
10024 + }
10025 +
10026 + for(i = 0; i < pkg->depends_count; i++){
10027 + parseDepends(depends, hash, pkg->depends_str[i]);
10028 + if (0 && pkg->depends_count)
10029 + fprintf(stderr, " depends_str=%s depends=%p possibility_count=%x\n",
10030 + pkg->depends_str[i], depends, depends->possibility_count);
10031 + depends++;
10032 + }
10033 + return 0;
10034 +}
10035 +
10036 +/*
10037 + * pkg_depend_string: returns the depends string specified by index.
10038 + * All 4 kinds of dependences: dependence, pre-dependence, recommend, and suggest are number starting from 0.
10039 + * [0,npredepends) -> returns pre_depends_str[index]
10040 + * [npredepends,npredepends+nrecommends) -> returns recommends_str[index]
10041 + * [npredepends+nrecommends,npredepends+nrecommends+nsuggests) -> returns recommends_str[index]
10042 + * [npredepends+nrecommends+nsuggests,npredepends+nrecommends+nsuggests+ndepends) -> returns depends_str[index]
10043 + */
10044 +char *pkg_depend_str(pkg_t *pkg, int pkg_index)
10045 +{
10046 + if (pkg_index < pkg->pre_depends_count) {
10047 + return pkg->pre_depends_str[pkg_index];
10048 + }
10049 + pkg_index -= pkg->pre_depends_count;
10050 +
10051 + if (pkg_index < pkg->recommends_count) {
10052 + return pkg->recommends_str[pkg_index];
10053 + }
10054 + pkg_index -= pkg->recommends_count;
10055 +
10056 + if (pkg_index < pkg->suggests_count) {
10057 + return pkg->suggests_str[pkg_index];
10058 + }
10059 + pkg_index -= pkg->suggests_count;
10060 +
10061 + if (pkg_index < pkg->depends_count) {
10062 + return pkg->depends_str[pkg_index];
10063 + }
10064 + fprintf(stderr, "pkg_depend_str: index %d out of range for pkg=%s\n", pkg_index, pkg->name);
10065 + return NULL;
10066 +}
10067 +
10068 +void freeDepends(pkg_t *pkg)
10069 +{
10070 + int i;
10071 +
10072 + if (pkg == NULL || pkg->depends == NULL) {
10073 + return;
10074 + }
10075 +
10076 + fprintf(stderr, "Freeing depends=%p\n", pkg->depends);
10077 + for (i=0; i < pkg->depends->possibility_count; i++) {
10078 + depend_deinit(pkg->depends->possibilities[i]);
10079 + }
10080 + free(pkg->depends->possibilities);
10081 + free(pkg->depends);
10082 + pkg->depends = NULL;
10083 +}
10084 +
10085 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg)
10086 +{
10087 + compound_depend_t * depends;
10088 + int count, othercount;
10089 + register int i, j;
10090 + abstract_pkg_t * ab_depend;
10091 + abstract_pkg_t ** temp;
10092 +
10093 + count = pkg->pre_depends_count + pkg->depends_count;
10094 + depends = pkg->depends;
10095 +
10096 + if (0 && pkg->pre_depends_count)
10097 + fprintf(stderr, "pkg=%s pre_depends_count=%d depends_count=%d\n",
10098 + pkg->name, pkg->pre_depends_count, pkg->depends_count);
10099 + for (i = 0; i < count; i++) {
10100 + if (0 && pkg->pre_depends_count)
10101 + fprintf(stderr, " i=%d possibility_count=%x depends=%p\n", i, depends->possibility_count, depends);
10102 + for (j = 0; j < depends->possibility_count; j++){
10103 + ab_depend = depends->possibilities[j]->pkg;
10104 + if(!ab_depend->depended_upon_by)
10105 + ab_depend->depended_upon_by = (abstract_pkg_t **)calloc(1, sizeof(abstract_pkg_t *));
10106 +
10107 + temp = ab_depend->depended_upon_by;
10108 + othercount = 1;
10109 + while(*temp){
10110 + temp++;
10111 + othercount++;
10112 + }
10113 + *temp = ab_pkg;
10114 +
10115 + ab_depend->depended_upon_by = (abstract_pkg_t **)realloc(ab_depend->depended_upon_by,
10116 + (othercount + 1) * sizeof(abstract_pkg_t *));
10117 + /* the array may have moved */
10118 + temp = ab_depend->depended_upon_by + othercount;
10119 + *temp = NULL;
10120 + }
10121 + depends++;
10122 + }
10123 +}
10124 +
10125 +static depend_t * depend_init(void)
10126 +{
10127 + depend_t * d = (depend_t *)malloc(sizeof(depend_t));
10128 + if ( d==NULL ){
10129 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10130 + return NULL;
10131 + }
10132 + d->constraint = NONE;
10133 + d->version = NULL;
10134 + d->pkg = NULL;
10135 +
10136 + return d;
10137 +}
10138 +
10139 +static void depend_deinit(depend_t *d)
10140 +{
10141 + free(d);
10142 +}
10143 +
10144 +static int parseDepends(compound_depend_t *compound_depend,
10145 + hash_table_t * hash, char * depend_str)
10146 +{
10147 + char * pkg_name, buffer[2048];
10148 + int num_of_ors = 0;
10149 + register int i;
10150 + register char * src, * dest;
10151 + depend_t ** possibilities;
10152 +
10153 + /* first count the number of ored possibilities for satisfying dependency */
10154 + src = depend_str;
10155 + while(*src)
10156 + if(*src++ == '|')
10157 + num_of_ors++;
10158 +
10159 + compound_depend->type = DEPEND;
10160 +
10161 + compound_depend->possibility_count = num_of_ors + 1;
10162 + possibilities = (depend_t **)malloc(sizeof(depend_t *) * (num_of_ors + 1));
10163 + if (!possibilities)
10164 + return -ENOMEM;
10165 + compound_depend->possibilities = possibilities;
10166 +
10167 + src = depend_str;
10168 + for(i = 0; i < num_of_ors + 1; i++){
10169 + possibilities[i] = depend_init();
10170 + if (!possibilities[i])
10171 + return -ENOMEM;
10172 + /* gobble up just the name first */
10173 + dest = buffer;
10174 + while(*src &&
10175 + !isspace(*src) &&
10176 + (*src != '(') &&
10177 + (*src != '*') &&
10178 + (*src != '|'))
10179 + *dest++ = *src++;
10180 + *dest = '\0';
10181 + pkg_name = trim_alloc(buffer);
10182 + if (pkg_name == NULL )
10183 + return -ENOMEM;
10184 +
10185 + /* now look at possible version info */
10186 +
10187 + /* skip to next chars */
10188 + if(isspace(*src))
10189 + while(*src && isspace(*src)) src++;
10190 +
10191 + /* extract constraint and version */
10192 + if(*src == '('){
10193 + src++;
10194 + if(!strncmp(src, "<<", 2)){
10195 + possibilities[i]->constraint = EARLIER;
10196 + src += 2;
10197 + }
10198 + else if(!strncmp(src, "<=", 2)){
10199 + possibilities[i]->constraint = EARLIER_EQUAL;
10200 + src += 2;
10201 + }
10202 + else if(!strncmp(src, ">=", 2)){
10203 + possibilities[i]->constraint = LATER_EQUAL;
10204 + src += 2;
10205 + }
10206 + else if(!strncmp(src, ">>", 2)){
10207 + possibilities[i]->constraint = LATER;
10208 + src += 2;
10209 + }
10210 + else if(!strncmp(src, "=", 1)){
10211 + possibilities[i]->constraint = EQUAL;
10212 + src++;
10213 + }
10214 + /* should these be here to support deprecated designations; dpkg does */
10215 + else if(!strncmp(src, "<", 1)){
10216 + possibilities[i]->constraint = EARLIER_EQUAL;
10217 + src++;
10218 + }
10219 + else if(!strncmp(src, ">", 1)){
10220 + possibilities[i]->constraint = LATER_EQUAL;
10221 + src++;
10222 + }
10223 +
10224 + /* now we have any constraint, pass space to version string */
10225 + while(isspace(*src)) src++;
10226 +
10227 + /* this would be the version string */
10228 + dest = buffer;
10229 + while(*src && *src != ')')
10230 + *dest++ = *src++;
10231 + *dest = '\0';
10232 +
10233 + possibilities[i]->version = trim_alloc(buffer);
10234 + /* fprintf(stderr, "let's print the depends version string:");
10235 + fprintf(stderr, "version %s\n", possibilities[i]->version);*/
10236 + if (possibilities[i]->version == NULL )
10237 + return -ENOMEM;
10238 +
10239 +
10240 + }
10241 + /* hook up the dependency to its abstract pkg */
10242 + possibilities[i]->pkg = ensure_abstract_pkg_by_name(hash, pkg_name);
10243 +
10244 + free(pkg_name);
10245 +
10246 + /* now get past the ) and any possible | chars */
10247 + while(*src &&
10248 + (isspace(*src) ||
10249 + (*src == ')') ||
10250 + (*src == '|')))
10251 + src++;
10252 + if (*src == '*')
10253 + {
10254 + compound_depend->type = GREEDY_DEPEND;
10255 + src++;
10256 + }
10257 + }
10258 +
10259 + return 0;
10260 +}
10261 --- /dev/null
10262 +++ b/archival/libipkg/pkg_depends.h
10263 @@ -0,0 +1,105 @@
10264 +/* pkg_depends.h - the itsy package management system
10265 +
10266 + Steven M. Ayer
10267 +
10268 + Copyright (C) 2002 Compaq Computer Corporation
10269 +
10270 + This program is free software; you can redistribute it and/or
10271 + modify it under the terms of the GNU General Public License as
10272 + published by the Free Software Foundation; either version 2, or (at
10273 + your option) any later version.
10274 +
10275 + This program is distributed in the hope that it will be useful, but
10276 + WITHOUT ANY WARRANTY; without even the implied warranty of
10277 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10278 + General Public License for more details.
10279 +*/
10280 +
10281 +#ifndef PKG_DEPENDS_H
10282 +#define PKG_DEPENDS_H
10283 +
10284 +#include "pkg.h"
10285 +#include "pkg_hash.h"
10286 +
10287 +enum depend_type {
10288 + PREDEPEND,
10289 + DEPEND,
10290 + CONFLICTS,
10291 + GREEDY_DEPEND,
10292 + RECOMMEND,
10293 + SUGGEST
10294 +};
10295 +typedef enum depend_type depend_type_t;
10296 +
10297 +enum version_constraint {
10298 + NONE,
10299 + EARLIER,
10300 + EARLIER_EQUAL,
10301 + EQUAL,
10302 + LATER_EQUAL,
10303 + LATER
10304 +};
10305 +typedef enum version_constraint version_constraint_t;
10306 +
10307 +struct depend{
10308 + version_constraint_t constraint;
10309 + char * version;
10310 + abstract_pkg_t * pkg;
10311 +};
10312 +typedef struct depend depend_t;
10313 +
10314 +struct compound_depend{
10315 + depend_type_t type;
10316 + int possibility_count;
10317 + struct depend ** possibilities;
10318 +};
10319 +typedef struct compound_depend compound_depend_t;
10320 +
10321 +#include "hash_table.h"
10322 +
10323 +int buildProvides(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10324 +int buildConflicts(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10325 +int buildReplaces(hash_table_t * hash, abstract_pkg_t * ab_pkg, pkg_t * pkg);
10326 +int buildDepends(hash_table_t * hash, pkg_t * pkg);
10327 +
10328 +/**
10329 + * pkg_has_common_provides returns 1 if pkg and replacee both provide
10330 + * the same abstract package and 0 otherwise.
10331 + */
10332 +int pkg_has_common_provides(pkg_t *pkg, pkg_t *replacee);
10333 +
10334 +/**
10335 + * pkg_provides returns 1 if pkg->provides contains providee and 0
10336 + * otherwise.
10337 + */
10338 +int pkg_provides_abstract(pkg_t *pkg, abstract_pkg_t *providee);
10339 +
10340 +/**
10341 + * pkg_replaces returns 1 if pkg->replaces contains one of replacee's provides and 0
10342 + * otherwise.
10343 + */
10344 +int pkg_replaces(pkg_t *pkg, pkg_t *replacee);
10345 +
10346 +/**
10347 + * pkg_conflicts_abstract returns 1 if pkg->conflicts contains conflictee provides and 0
10348 + * otherwise.
10349 + */
10350 +int pkg_conflicts_abstract(pkg_t *pkg, abstract_pkg_t *conflicts);
10351 +
10352 +/**
10353 + * pkg_conflicts returns 1 if pkg->conflicts contains one of conflictee's provides and 0
10354 + * otherwise.
10355 + */
10356 +int pkg_conflicts(pkg_t *pkg, pkg_t *conflicts);
10357 +
10358 +char *pkg_depend_str(pkg_t *pkg, int pkg_index);
10359 +void buildDependedUponBy(pkg_t * pkg, abstract_pkg_t * ab_pkg);
10360 +void freeDepends(pkg_t *pkg);
10361 +void printDepends(pkg_t * pkg);
10362 +int version_constraints_satisfied(depend_t * depends, pkg_t * pkg);
10363 +int pkg_hash_fetch_unsatisfied_dependencies(ipkg_conf_t *conf, pkg_t * pkg, pkg_vec_t *depends, char *** unresolved);
10364 +pkg_vec_t * pkg_hash_fetch_conflicts(hash_table_t * hash, pkg_t * pkg);
10365 +int pkg_dependence_satisfiable(ipkg_conf_t *conf, depend_t *depend);
10366 +int pkg_dependence_satisfied(ipkg_conf_t *conf, depend_t *depend);
10367 +
10368 +#endif
10369 --- /dev/null
10370 +++ b/archival/libipkg/pkg_dest.c
10371 @@ -0,0 +1,92 @@
10372 +/* pkg_dest.c - the itsy package management system
10373 +
10374 + Carl D. Worth
10375 +
10376 + Copyright (C) 2001 University of Southern California
10377 +
10378 + This program is free software; you can redistribute it and/or
10379 + modify it under the terms of the GNU General Public License as
10380 + published by the Free Software Foundation; either version 2, or (at
10381 + your option) any later version.
10382 +
10383 + This program is distributed in the hope that it will be useful, but
10384 + WITHOUT ANY WARRANTY; without even the implied warranty of
10385 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10386 + General Public License for more details.
10387 +*/
10388 +
10389 +#include "ipkg.h"
10390 +
10391 +#include "pkg_dest.h"
10392 +#include "file_util.h"
10393 +#include "str_util.h"
10394 +#include "sprintf_alloc.h"
10395 +
10396 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir)
10397 +{
10398 + dest->name = strdup(name);
10399 +
10400 + /* Guarantee that dest->root_dir ends with a '/' */
10401 + if (str_ends_with(root_dir, "/")) {
10402 + dest->root_dir = strdup(root_dir);
10403 + } else {
10404 + sprintf_alloc(&dest->root_dir, "%s/", root_dir);
10405 + }
10406 + file_mkdir_hier(dest->root_dir, 0755);
10407 +
10408 + sprintf_alloc(&dest->ipkg_dir, "%s%s",
10409 + dest->root_dir, IPKG_STATE_DIR_PREFIX);
10410 + file_mkdir_hier(dest->ipkg_dir, 0755);
10411 +
10412 + if (str_starts_with (lists_dir, "/"))
10413 + sprintf_alloc(&dest->lists_dir, "%s", lists_dir);
10414 + else
10415 + sprintf_alloc(&dest->lists_dir, "/%s", lists_dir);
10416 +
10417 + file_mkdir_hier(dest->lists_dir, 0755);
10418 +
10419 + sprintf_alloc(&dest->info_dir, "%s/%s",
10420 + dest->ipkg_dir, IPKG_INFO_DIR_SUFFIX);
10421 + file_mkdir_hier(dest->info_dir, 0755);
10422 +
10423 + sprintf_alloc(&dest->status_file_name, "%s/%s",
10424 + dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10425 +
10426 + sprintf_alloc(&dest->status_file_tmp_name, "%s/%s.tmp",
10427 + dest->ipkg_dir, IPKG_STATUS_FILE_SUFFIX);
10428 +
10429 + dest->status_file = NULL;
10430 +
10431 + return 0;
10432 +}
10433 +
10434 +void pkg_dest_deinit(pkg_dest_t *dest)
10435 +{
10436 + free(dest->name);
10437 + dest->name = NULL;
10438 +
10439 + free(dest->root_dir);
10440 + dest->root_dir = NULL;
10441 +
10442 + free(dest->ipkg_dir);
10443 + dest->ipkg_dir = NULL;
10444 +
10445 + free(dest->lists_dir);
10446 + dest->lists_dir = NULL;
10447 +
10448 + free(dest->info_dir);
10449 + dest->info_dir = NULL;
10450 +
10451 + free(dest->status_file_name);
10452 + dest->status_file_name = NULL;
10453 +
10454 + free(dest->status_file_tmp_name);
10455 + dest->status_file_tmp_name = NULL;
10456 +
10457 + if (dest->status_file) {
10458 + fclose(dest->status_file);
10459 + }
10460 + dest->status_file = NULL;
10461 +
10462 + dest->root_dir = NULL;
10463 +}
10464 --- /dev/null
10465 +++ b/archival/libipkg/pkg_dest.h
10466 @@ -0,0 +1,38 @@
10467 +/* pkg_dest.h - the itsy package management system
10468 +
10469 + Carl D. Worth
10470 +
10471 + Copyright (C) 2001 University of Southern California
10472 +
10473 + This program is free software; you can redistribute it and/or
10474 + modify it under the terms of the GNU General Public License as
10475 + published by the Free Software Foundation; either version 2, or (at
10476 + your option) any later version.
10477 +
10478 + This program is distributed in the hope that it will be useful, but
10479 + WITHOUT ANY WARRANTY; without even the implied warranty of
10480 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10481 + General Public License for more details.
10482 +*/
10483 +
10484 +#ifndef PKG_DEST_H
10485 +#define PKG_DEST_H
10486 +
10487 +typedef struct pkg_dest pkg_dest_t;
10488 +struct pkg_dest
10489 +{
10490 + char *name;
10491 + char *root_dir;
10492 + char *ipkg_dir;
10493 + char *lists_dir;
10494 + char *info_dir;
10495 + char *status_file_name;
10496 + char *status_file_tmp_name;
10497 + FILE *status_file;
10498 +};
10499 +
10500 +int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char *lists_dir);
10501 +void pkg_dest_deinit(pkg_dest_t *dest);
10502 +
10503 +#endif
10504 +
10505 --- /dev/null
10506 +++ b/archival/libipkg/pkg_dest_list.c
10507 @@ -0,0 +1,85 @@
10508 +/* pkg_dest_list.c - the itsy package management system
10509 +
10510 + Carl D. Worth
10511 +
10512 + Copyright (C) 2001 University of Southern California
10513 +
10514 + This program is free software; you can redistribute it and/or
10515 + modify it under the terms of the GNU General Public License as
10516 + published by the Free Software Foundation; either version 2, or (at
10517 + your option) any later version.
10518 +
10519 + This program is distributed in the hope that it will be useful, but
10520 + WITHOUT ANY WARRANTY; without even the implied warranty of
10521 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10522 + General Public License for more details.
10523 +*/
10524 +
10525 +#include "ipkg.h"
10526 +
10527 +#include "pkg_dest.h"
10528 +#include "void_list.h"
10529 +#include "pkg_dest_list.h"
10530 +
10531 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data)
10532 +{
10533 + return void_list_elt_init((void_list_elt_t *) elt, data);
10534 +}
10535 +
10536 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt)
10537 +{
10538 + void_list_elt_deinit((void_list_elt_t *) elt);
10539 +}
10540 +
10541 +int pkg_dest_list_init(pkg_dest_list_t *list)
10542 +{
10543 + return void_list_init((void_list_t *) list);
10544 +}
10545 +
10546 +void pkg_dest_list_deinit(pkg_dest_list_t *list)
10547 +{
10548 + pkg_dest_list_elt_t *iter;
10549 + pkg_dest_t *pkg_dest;
10550 +
10551 + for (iter = list->head; iter; iter = iter->next) {
10552 + pkg_dest = iter->data;
10553 + pkg_dest_deinit(pkg_dest);
10554 +
10555 + /* malloced in pkg_dest_list_append */
10556 + free(pkg_dest);
10557 + iter->data = NULL;
10558 + }
10559 + void_list_deinit((void_list_t *) list);
10560 +}
10561 +
10562 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10563 + const char *root_dir,const char *lists_dir)
10564 +{
10565 + int err;
10566 + pkg_dest_t *pkg_dest;
10567 +
10568 + /* freed in plg_dest_list_deinit */
10569 + pkg_dest = malloc(sizeof(pkg_dest_t));
10570 + if (pkg_dest == NULL) {
10571 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
10572 + return NULL;
10573 + }
10574 +
10575 + pkg_dest_init(pkg_dest, name, root_dir,lists_dir);
10576 + err = void_list_append((void_list_t *) list, pkg_dest);
10577 + if (err) {
10578 + return NULL;
10579 + }
10580 +
10581 + return pkg_dest;
10582 +}
10583 +
10584 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data)
10585 +{
10586 + return void_list_push((void_list_t *) list, data);
10587 +}
10588 +
10589 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list)
10590 +{
10591 + return (pkg_dest_list_elt_t *) void_list_pop((void_list_t *) list);
10592 +}
10593 --- /dev/null
10594 +++ b/archival/libipkg/pkg_dest_list.h
10595 @@ -0,0 +1,50 @@
10596 +/* pkg_dest_list.h - the itsy package management system
10597 +
10598 + Carl D. Worth
10599 +
10600 + Copyright (C) 2001 University of Southern California
10601 +
10602 + This program is free software; you can redistribute it and/or
10603 + modify it under the terms of the GNU General Public License as
10604 + published by the Free Software Foundation; either version 2, or (at
10605 + your option) any later version.
10606 +
10607 + This program is distributed in the hope that it will be useful, but
10608 + WITHOUT ANY WARRANTY; without even the implied warranty of
10609 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10610 + General Public License for more details.
10611 +*/
10612 +
10613 +#ifndef PKG_DEST_LIST_H
10614 +#define PKG_DEST_LIST_H
10615 +
10616 +#include "pkg_dest.h"
10617 +
10618 +typedef struct pkg_dest_list_elt pkg_dest_list_elt_t;
10619 +struct pkg_dest_list_elt
10620 +{
10621 + pkg_dest_list_elt_t *next;
10622 + pkg_dest_t *data;
10623 +};
10624 +
10625 +typedef struct pkg_dest_list pkg_dest_list_t;
10626 +struct pkg_dest_list
10627 +{
10628 + pkg_dest_list_elt_t pre_head;
10629 + pkg_dest_list_elt_t *head;
10630 + pkg_dest_list_elt_t *tail;
10631 +};
10632 +
10633 +int pkg_dest_list_elt_init(pkg_dest_list_elt_t *elt, pkg_dest_t *data);
10634 +void pkg_dest_list_elt_deinit(pkg_dest_list_elt_t *elt);
10635 +
10636 +int pkg_dest_list_init(pkg_dest_list_t *list);
10637 +void pkg_dest_list_deinit(pkg_dest_list_t *list);
10638 +
10639 +pkg_dest_t *pkg_dest_list_append(pkg_dest_list_t *list, const char *name,
10640 + const char *root_dir,const char* lists_dir);
10641 +int pkg_dest_list_push(pkg_dest_list_t *list, pkg_dest_t *data);
10642 +pkg_dest_list_elt_t *pkg_dest_list_pop(pkg_dest_list_t *list);
10643 +
10644 +#endif
10645 +
10646 --- /dev/null
10647 +++ b/archival/libipkg/pkg_extract.c
10648 @@ -0,0 +1,224 @@
10649 +/* pkg_extract.c - the itsy package management system
10650 +
10651 + Carl D. Worth
10652 +
10653 + Copyright (C) 2001 University of Southern California
10654 +
10655 + This program is free software; you can redistribute it and/or
10656 + modify it under the terms of the GNU General Public License as
10657 + published by the Free Software Foundation; either version 2, or (at
10658 + your option) any later version.
10659 +
10660 + This program is distributed in the hope that it will be useful, but
10661 + WITHOUT ANY WARRANTY; without even the implied warranty of
10662 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10663 + General Public License for more details.
10664 +*/
10665 +
10666 +#include "ipkg.h"
10667 +#include <errno.h>
10668 +#include <fcntl.h>
10669 +#include <stdio.h>
10670 +
10671 +#include "pkg_extract.h"
10672 +
10673 +#include "libbb.h"
10674 +#include "file_util.h"
10675 +#include "sprintf_alloc.h"
10676 +#include "unarchive.h"
10677 +
10678 +#define IPKG_CONTROL_ARCHIVE "control.tar.gz"
10679 +#define IPKG_DATA_ARCHIVE "data.tar.gz"
10680 +#define IPKG_CONTROL_FILE "control"
10681 +
10682 +static void extract_ipkg_file_to_dir(pkg_t *pkg, const char *dir, const char *filename)
10683 +{
10684 + archive_handle_t *archive;
10685 + char *path;
10686 +
10687 + sprintf_alloc(&path, "%s/", dir);
10688 + archive = init_handle();
10689 + archive->src_fd = xopen(pkg->local_filename, O_RDONLY);
10690 + archive->filter = filter_accept_list;
10691 + llist_add_to(&(archive->accept), (char *)filename);
10692 + archive->buffer = path;
10693 + archive->action_data = data_extract_all_prefix;
10694 + archive->flags |= ARCHIVE_EXTRACT_UNCONDITIONAL;
10695 + while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10696 + close(archive->src_fd);
10697 + free(archive->accept);
10698 + free(archive);
10699 + free(path);
10700 +}
10701 +
10702 +static void data_extract_file_name_to_buffer(archive_handle_t *archive)
10703 +{
10704 + unsigned int size = strlen(archive->file_header->name) + 2;
10705 +
10706 + if (archive->buffer == NULL) {
10707 + archive->buffer = xmalloc(size);
10708 + strcpy(archive->buffer, archive->file_header->name);
10709 + } else {
10710 + size += strlen(archive->buffer);
10711 + archive->buffer = xrealloc(archive->buffer, size);
10712 + strcat(archive->buffer, archive->file_header->name);
10713 + }
10714 + strcat(archive->buffer, "\n");
10715 + data_skip(archive);
10716 +}
10717 +
10718 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream)
10719 +{
10720 + archive_handle_t *archive;
10721 + char *name;
10722 +
10723 + extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE);
10724 + sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10725 + archive = init_handle();
10726 + archive->src_fd = xopen(name, O_RDONLY);
10727 + archive->filter = filter_accept_list;
10728 + llist_add_to(&(archive->accept), "./" IPKG_CONTROL_FILE);
10729 + archive->action_data = data_extract_to_buffer;
10730 + while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10731 + close(archive->src_fd);
10732 + fputs(archive->buffer, stream);
10733 + free(archive->buffer);
10734 + free(archive->accept);
10735 + free(archive);
10736 + free(name);
10737 +
10738 + return 0;
10739 +}
10740 +
10741 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir)
10742 +{
10743 + return pkg_extract_control_files_to_dir_with_prefix(pkg, dir, "");
10744 +}
10745 +
10746 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg, const char *dir, const char *prefix)
10747 +{
10748 + archive_handle_t *archive;
10749 + char *name;
10750 + char *path;
10751 +
10752 + extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_CONTROL_ARCHIVE);
10753 + sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_CONTROL_ARCHIVE);
10754 + sprintf_alloc(&path, "%s/%s", dir, prefix);
10755 + archive = init_handle();
10756 + archive->src_fd = xopen(name, O_RDONLY);
10757 + archive->filter = filter_accept_all;
10758 + archive->buffer = path;
10759 + archive->action_data = data_extract_all_prefix;
10760 + archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
10761 + while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10762 + close(archive->src_fd);
10763 + free(archive);
10764 + free(path);
10765 + free(name);
10766 +
10767 + return 0;
10768 +}
10769 +
10770 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir)
10771 +{
10772 + archive_handle_t *archive;
10773 + char *name;
10774 + char *path;
10775 +
10776 + extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
10777 + sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
10778 + sprintf_alloc(&path, "%s/", dir);
10779 + archive = init_handle();
10780 + archive->src_fd = xopen(name, O_RDONLY);
10781 + archive->filter = filter_accept_all;
10782 + archive->buffer = path;
10783 + archive->action_data = data_extract_all_prefix;
10784 + archive->flags |= ARCHIVE_CREATE_LEADING_DIRS | ARCHIVE_PRESERVE_DATE | ARCHIVE_EXTRACT_UNCONDITIONAL;
10785 + while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10786 + close(archive->src_fd);
10787 + free(archive);
10788 + free(path);
10789 + free(name);
10790 +
10791 + return 0;
10792 +}
10793 +
10794 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name)
10795 +{
10796 + int err=0;
10797 + char *line, *data_file;
10798 + FILE *file;
10799 + FILE *tmp;
10800 +
10801 + file = fopen(file_name, "w");
10802 + if (file == NULL) {
10803 + fprintf(stderr, "%s: ERROR: Failed to open %s for writing.\n",
10804 + __FUNCTION__, file_name);
10805 + return EINVAL;
10806 + }
10807 +
10808 + tmp = tmpfile();
10809 + if (pkg->installed_files) {
10810 + str_list_elt_t *elt;
10811 + for (elt = pkg->installed_files->head; elt; elt = elt->next) {
10812 + fprintf(file, "%s\n", elt->data);
10813 + }
10814 + } else {
10815 + err = pkg_extract_data_file_names_to_stream(pkg, tmp);
10816 + if (err) {
10817 + fclose(file);
10818 + fclose(tmp);
10819 + return err;
10820 + }
10821 +
10822 + /* Fixup data file names by removing the initial '.' */
10823 + rewind(tmp);
10824 + while (1) {
10825 + line = file_read_line_alloc(tmp);
10826 + if (line == NULL) {
10827 + break;
10828 + }
10829 +
10830 + data_file = line;
10831 + if (*data_file == '.') {
10832 + data_file++;
10833 + }
10834 +
10835 + if (*data_file != '/') {
10836 + fputs("/", file);
10837 + }
10838 +
10839 + /* I have no idea why, but this is what dpkg does */
10840 + if (strcmp(data_file, "/\n") == 0) {
10841 + fputs("/.\n", file);
10842 + } else {
10843 + fputs(data_file, file);
10844 + }
10845 + }
10846 + }
10847 + fclose(tmp);
10848 + fclose(file);
10849 +
10850 + return err;
10851 +}
10852 +
10853 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file)
10854 +{
10855 + archive_handle_t *archive;
10856 + char *name;
10857 +
10858 + extract_ipkg_file_to_dir(pkg, global_conf->tmp_dir, "./" IPKG_DATA_ARCHIVE);
10859 + sprintf_alloc(&name, "%s/%s", global_conf->tmp_dir, IPKG_DATA_ARCHIVE);
10860 + archive = init_handle();
10861 + archive->src_fd = xopen(name, O_RDONLY);
10862 + archive->filter = filter_accept_all;
10863 + archive->action_data = data_extract_file_name_to_buffer;
10864 + while( get_header_tar_gz(archive) == EXIT_SUCCESS );
10865 + close(archive->src_fd);
10866 + fputs(archive->buffer, file);
10867 + free(archive->buffer);
10868 + free(archive);
10869 + free(name);
10870 +
10871 + return 0;
10872 +}
10873 --- /dev/null
10874 +++ b/archival/libipkg/pkg_extract.h
10875 @@ -0,0 +1,32 @@
10876 +/* pkg_extract.c - the itsy package management system
10877 +
10878 + Carl D. Worth
10879 +
10880 + Copyright (C) 2001 University of Southern California
10881 +
10882 + This program is free software; you can redistribute it and/or
10883 + modify it under the terms of the GNU General Public License as
10884 + published by the Free Software Foundation; either version 2, or (at
10885 + your option) any later version.
10886 +
10887 + This program is distributed in the hope that it will be useful, but
10888 + WITHOUT ANY WARRANTY; without even the implied warranty of
10889 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10890 + General Public License for more details.
10891 +*/
10892 +
10893 +#ifndef PKG_EXTRACT_H
10894 +#define PKG_EXTRACT_H
10895 +
10896 +#include "pkg.h"
10897 +
10898 +int pkg_extract_control_file_to_stream(pkg_t *pkg, FILE *stream);
10899 +int pkg_extract_control_files_to_dir(pkg_t *pkg, const char *dir);
10900 +int pkg_extract_control_files_to_dir_with_prefix(pkg_t *pkg,
10901 + const char *dir,
10902 + const char *prefix);
10903 +int pkg_extract_data_files_to_dir(pkg_t *pkg, const char *dir);
10904 +int pkg_extract_data_file_names_to_file(pkg_t *pkg, const char *file_name);
10905 +int pkg_extract_data_file_names_to_stream(pkg_t *pkg, FILE *file);
10906 +
10907 +#endif
10908 --- /dev/null
10909 +++ b/archival/libipkg/pkg.h
10910 @@ -0,0 +1,229 @@
10911 +/* pkg.h - the itsy package management system
10912 +
10913 + Carl D. Worth
10914 +
10915 + Copyright (C) 2001 University of Southern California
10916 +
10917 + This program is free software; you can redistribute it and/or
10918 + modify it under the terms of the GNU General Public License as
10919 + published by the Free Software Foundation; either version 2, or (at
10920 + your option) any later version.
10921 +
10922 + This program is distributed in the hope that it will be useful, but
10923 + WITHOUT ANY WARRANTY; without even the implied warranty of
10924 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10925 + General Public License for more details.
10926 +*/
10927 +
10928 +#ifndef PKG_H
10929 +#define PKG_H
10930 +
10931 +#include <sys/types.h>
10932 +#include <sys/stat.h>
10933 +#include <unistd.h>
10934 +
10935 +#include "pkg_vec.h"
10936 +#include "str_list.h"
10937 +#include "pkg_src.h"
10938 +#include "pkg_dest.h"
10939 +#include "ipkg_conf.h"
10940 +#include "conffile_list.h"
10941 +
10942 +struct ipkg_conf;
10943 +
10944 +/* I think "Size" is currently the shortest field name */
10945 +#define PKG_MINIMUM_FIELD_NAME_LEN 4
10946 +
10947 +enum pkg_state_want
10948 +{
10949 + SW_UNKNOWN = 1,
10950 + SW_INSTALL,
10951 + SW_DEINSTALL,
10952 + SW_PURGE,
10953 + SW_LAST_STATE_WANT
10954 +};
10955 +typedef enum pkg_state_want pkg_state_want_t;
10956 +
10957 +enum pkg_state_flag
10958 +{
10959 + SF_OK = 0,
10960 + SF_REINSTREQ = 1,
10961 + SF_HOLD = 2, /* do not upgrade version */
10962 + SF_REPLACE = 4, /* replace this package */
10963 + SF_NOPRUNE = 8, /* do not remove obsolete files */
10964 + SF_PREFER = 16, /* prefer this version */
10965 + SF_OBSOLETE = 32, /* old package in upgrade pair */
10966 + SF_MARKED = 64, /* temporary mark */
10967 + SF_FILELIST_CHANGED = 128, /* needs filelist written */
10968 + SF_USER = 256,
10969 + SF_LAST_STATE_FLAG
10970 +};
10971 +typedef enum pkg_state_flag pkg_state_flag_t;
10972 +#define SF_NONVOLATILE_FLAGS (SF_HOLD|SF_NOPRUNE|SF_PREFER|SF_OBSOLETE|SF_USER)
10973 +
10974 +enum pkg_state_status
10975 +{
10976 + SS_NOT_INSTALLED = 1,
10977 + SS_UNPACKED,
10978 + SS_HALF_CONFIGURED,
10979 + SS_INSTALLED,
10980 + SS_HALF_INSTALLED,
10981 + SS_CONFIG_FILES,
10982 + SS_POST_INST_FAILED,
10983 + SS_REMOVAL_FAILED,
10984 + SS_LAST_STATE_STATUS
10985 +};
10986 +typedef enum pkg_state_status pkg_state_status_t;
10987 +
10988 +struct abstract_pkg{
10989 + char * name;
10990 + int dependencies_checked;
10991 + pkg_vec_t * pkgs;
10992 + pkg_state_status_t state_status;
10993 + pkg_state_flag_t state_flag;
10994 + struct abstract_pkg ** depended_upon_by; /* @@@@ this should be abstract_pkg_vec_t -Jamey */
10995 + abstract_pkg_vec_t * provided_by;
10996 + abstract_pkg_vec_t * replaced_by;
10997 +};
10998 +
10999 +#include "pkg_depends.h"
11000 +
11001 +/* XXX: CLEANUP: I'd like to clean up pkg_t in several ways:
11002 +
11003 + The 3 version fields should go into a single version struct. (This
11004 + is especially important since, currently, pkg->version can easily
11005 + be mistaken for pkg_verson_str_alloc(pkg) although they are very
11006 + distinct. This has been the source of multiple bugs.
11007 +
11008 + The 3 state fields could possibly also go into their own struct.
11009 +
11010 + All fields which deal with lists of packages, (Depends,
11011 + Pre-Depends, Provides, Suggests, Recommends, Enhances), should each
11012 + be handled by a single struct in pkg_t
11013 +
11014 + All string fields for which there is a small set of possible
11015 + values, (section, maintainer, architecture, maybe version?), that
11016 + are reused among different packages -- for all such packages we
11017 + should move from "char *"s to some atom datatype to share data
11018 + storage and use less memory. We might even do reference counting,
11019 + but probably not since most often we only create new pkg_t structs,
11020 + we don't often free them. */
11021 +struct pkg
11022 +{
11023 + char *name;
11024 + unsigned long epoch;
11025 + char *version;
11026 + char *revision;
11027 + char *familiar_revision;
11028 + pkg_src_t *src;
11029 + pkg_dest_t *dest;
11030 + char *architecture;
11031 + char *section;
11032 + char *maintainer;
11033 + char *description;
11034 + pkg_state_want_t state_want;
11035 + pkg_state_flag_t state_flag;
11036 + pkg_state_status_t state_status;
11037 + char **depends_str;
11038 + int depends_count;
11039 + char **pre_depends_str;
11040 + int pre_depends_count;
11041 + char **recommends_str;
11042 + int recommends_count;
11043 + char **suggests_str;
11044 + int suggests_count;
11045 + compound_depend_t * depends;
11046 +
11047 + /* Abhaya: new conflicts */
11048 + char **conflicts_str;
11049 + compound_depend_t * conflicts;
11050 + int conflicts_count;
11051 +
11052 + char **replaces_str;
11053 + int replaces_count;
11054 + abstract_pkg_t ** replaces;
11055 +
11056 + char **provides_str;
11057 + int provides_count;
11058 + abstract_pkg_t ** provides;
11059 +
11060 + abstract_pkg_t *parent;
11061 +
11062 + pkg_t *old_pkg; /* during upgrade, points from installee to previously installed */
11063 +
11064 + char *filename;
11065 + char *local_filename;
11066 + char *url;
11067 + char *tmp_unpack_dir;
11068 + char *md5sum;
11069 + char *size;
11070 + char *installed_size;
11071 + char *priority;
11072 + char *source;
11073 + conffile_list_t conffiles;
11074 + time_t installed_time;
11075 + /* As pointer for lazy evaluation */
11076 + str_list_t *installed_files;
11077 + /* XXX: CLEANUP: I'd like to perhaps come up with a better
11078 + mechanism to avoid the problem here, (which is that the
11079 + installed_files list was being freed from an inner loop while
11080 + still being used within an outer loop. */
11081 + int installed_files_ref_cnt;
11082 + int essential;
11083 + int arch_priority;
11084 +/* Adding this flag, to "force" ipkg to choose a "provided_by_hand" package, if there are multiple choice */
11085 + int provided_by_hand;
11086 +};
11087 +
11088 +pkg_t *pkg_new(void);
11089 +int pkg_init(pkg_t *pkg);
11090 +void pkg_deinit(pkg_t *pkg);
11091 +int pkg_init_from_file(pkg_t *pkg, const char *filename);
11092 +abstract_pkg_t *abstract_pkg_new(void);
11093 +int abstract_pkg_init(abstract_pkg_t *ab_pkg);
11094 +
11095 +/*
11096 + * merges fields from newpkg into oldpkg.
11097 + * Forcibly sets oldpkg state_status, state_want and state_flags if set_status is nonzero
11098 + */
11099 +int pkg_merge(pkg_t *oldpkg, pkg_t *newpkg, int set_status);
11100 +
11101 +char *pkg_version_str_alloc(pkg_t *pkg);
11102 +
11103 +int pkg_compare_versions(const pkg_t *pkg, const pkg_t *ref_pkg);
11104 +int pkg_name_version_and_architecture_compare(pkg_t *a, pkg_t *b);
11105 +int abstract_pkg_name_compare(abstract_pkg_t *a, abstract_pkg_t *b);
11106 +
11107 +char * pkg_formatted_info(pkg_t *pkg );
11108 +char * pkg_formatted_field(pkg_t *pkg, const char *field );
11109 +
11110 +void set_flags_from_control(ipkg_conf_t *conf, pkg_t *pkg);
11111 +
11112 +void pkg_print_info(pkg_t *pkg, FILE *file);
11113 +void pkg_print_status(pkg_t * pkg, FILE * file);
11114 +void pkg_print_field(pkg_t *pkg, FILE *file, const char *field);
11115 +str_list_t *pkg_get_installed_files(pkg_t *pkg);
11116 +int pkg_free_installed_files(pkg_t *pkg);
11117 +int pkg_remove_installed_files_list(ipkg_conf_t *conf, pkg_t *pkg);
11118 +conffile_t *pkg_get_conffile(pkg_t *pkg, const char *file_name);
11119 +int pkg_run_script(struct ipkg_conf *conf, pkg_t *pkg,
11120 + const char *script, const char *args);
11121 +
11122 +/* enum mappings */
11123 +char *pkg_state_want_to_str(pkg_state_want_t sw);
11124 +pkg_state_want_t pkg_state_want_from_str(char *str);
11125 +char *pkg_state_flag_to_str(pkg_state_flag_t sf);
11126 +pkg_state_flag_t pkg_state_flag_from_str(char *str);
11127 +char *pkg_state_status_to_str(pkg_state_status_t ss);
11128 +pkg_state_status_t pkg_state_status_from_str(char *str);
11129 +
11130 +int pkg_version_satisfied(pkg_t *it, pkg_t *ref, const char *op);
11131 +
11132 +int pkg_arch_supported(ipkg_conf_t *conf, pkg_t *pkg);
11133 +int pkg_info_preinstall_check(ipkg_conf_t *conf);
11134 +int pkg_free_installed_files(pkg_t *pkg);
11135 +
11136 +int pkg_write_filelist(ipkg_conf_t *conf, pkg_t *pkg);
11137 +int pkg_write_changed_filelists(ipkg_conf_t *conf);
11138 +
11139 +#endif
11140 --- /dev/null
11141 +++ b/archival/libipkg/pkg_hash.c
11142 @@ -0,0 +1,616 @@
11143 +/* ipkg_hash.c - the itsy package management system
11144 +
11145 + Steven M. Ayer
11146 +
11147 + Copyright (C) 2002 Compaq Computer Corporation
11148 +
11149 + This program is free software; you can redistribute it and/or
11150 + modify it under the terms of the GNU General Public License as
11151 + published by the Free Software Foundation; either version 2, or (at
11152 + your option) any later version.
11153 +
11154 + This program is distributed in the hope that it will be useful, but
11155 + WITHOUT ANY WARRANTY; without even the implied warranty of
11156 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11157 + General Public License for more details.
11158 +*/
11159 +
11160 +#include "ipkg.h"
11161 +#include <errno.h>
11162 +#include <ctype.h>
11163 +#include <stdlib.h>
11164 +#include <string.h>
11165 +
11166 +#include "hash_table.h"
11167 +#include "pkg.h"
11168 +#include "ipkg_message.h"
11169 +#include "pkg_vec.h"
11170 +#include "pkg_hash.h"
11171 +#include "pkg_parse.h"
11172 +#include "ipkg_utils.h"
11173 +
11174 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
11175 +
11176 +/*
11177 + * this will talk to both feeds-lists files and installed status files
11178 + * example api:
11179 + *
11180 + * hash_table_t hash;
11181 + * pkg_hash_init(name, &hash, 1000);
11182 + * pkg_hash_add_from_file(<feed filename>);
11183 + *
11184 + * the query function is just there as a shell to prove to me that this
11185 + * sort of works, but isn't far from doing something useful
11186 + *
11187 + * -sma, 12/21/01
11188 + * modified: CDW 3 Jan. 2002
11189 + */
11190 +
11191 +
11192 +\f
11193 +int pkg_hash_init(const char *name, hash_table_t *hash, int len)
11194 +{
11195 + return hash_table_init(name, hash, len);
11196 +}
11197 +
11198 +void pkg_hash_deinit(hash_table_t *hash)
11199 +{
11200 + hash_table_deinit(hash);
11201 +}
11202 +
11203 +
11204 +/* Find the default arch for a given package status file if none is given. */
11205 +static char *pkg_get_default_arch(ipkg_conf_t *conf)
11206 +{
11207 + nv_pair_list_elt_t *l;
11208 + char *def_arch = HOST_CPU_STR; /* Default arch */
11209 + int def_prio = 0; /* Other archs override this */
11210 +
11211 + l = conf->arch_list.head;
11212 +
11213 + while (l) {
11214 + nv_pair_t *nv = l->data;
11215 + int priority = strtol(nv->value, NULL, 0);
11216 +
11217 + /* Check if this arch has higher priority, and is valid */
11218 + if ((priority > def_prio) &&
11219 + (strcmp(nv->name, "all")) && (strcmp(nv->name, "noarch"))) {
11220 + /* Our new default */
11221 + def_prio = priority;
11222 + def_arch = nv->name;
11223 + }
11224 + l = l->next;
11225 + }
11226 +
11227 + return strdup(def_arch);
11228 +}
11229 +
11230 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
11231 + pkg_src_t *src, pkg_dest_t *dest, int is_status_file)
11232 +{
11233 + hash_table_t *hash = &conf->pkg_hash;
11234 + char **raw;
11235 + char **raw_start;
11236 + pkg_t *pkg;
11237 +
11238 + raw = raw_start = read_raw_pkgs_from_file(file_name);
11239 + if (!raw)
11240 + return -ENOMEM;
11241 +
11242 + while(*raw){ /* don't worry, we'll increment raw in the parsing function */
11243 + pkg = pkg_new();
11244 + if (!pkg)
11245 + return -ENOMEM;
11246 +
11247 + if (pkg_parse_raw(pkg, &raw, src, dest) == 0) {
11248 + if (!pkg->architecture) {
11249 + char *version_str = pkg_version_str_alloc(pkg);
11250 + pkg->architecture = pkg_get_default_arch(conf);
11251 + ipkg_message(conf, IPKG_ERROR, "Package %s version %s has no architecture specified, defaulting to %s.\n",
11252 + pkg->name, version_str, pkg->architecture);
11253 + free(version_str);
11254 + }
11255 + hash_insert_pkg(hash, pkg, is_status_file,conf);
11256 + } else {
11257 + free(pkg);
11258 + }
11259 + }
11260 +
11261 + /* XXX: CLEANUP: I'd like a cleaner interface for cleaning up
11262 + memory after read_raw_pkgs_from_file */
11263 + raw = raw_start;
11264 + while (*raw) {
11265 + free(*raw++);
11266 + }
11267 + free(raw_start);
11268 + return 0;
11269 +}
11270 +
11271 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name)
11272 +{
11273 + return (abstract_pkg_t *)hash_table_get(hash, pkg_name);
11274 +}
11275 +
11276 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name)
11277 +{
11278 + abstract_pkg_t *apkg = abstract_pkg_fetch_by_name(hash, name);
11279 + if (apkg)
11280 + return NULL;
11281 + return apkg->provided_by;
11282 +}
11283 +
11284 +
11285 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg,
11286 + int (*constraint_fcn)(pkg_t *pkg, void *cdata), void *cdata, int quiet)
11287 +{
11288 + int i, j;
11289 + int nprovides = 0;
11290 + int nmatching = 0;
11291 + pkg_vec_t *matching_pkgs = pkg_vec_alloc();
11292 + abstract_pkg_vec_t *matching_apkgs = abstract_pkg_vec_alloc();
11293 + abstract_pkg_vec_t *provided_apkg_vec;
11294 + abstract_pkg_t **provided_apkgs;
11295 + abstract_pkg_vec_t *providers = abstract_pkg_vec_alloc();
11296 + pkg_t *latest_installed_parent = NULL;
11297 + pkg_t *latest_matching = NULL;
11298 + pkg_t *held_pkg = NULL;
11299 + pkg_t *good_pkg_by_name = NULL;
11300 +
11301 + if (matching_apkgs == NULL || providers == NULL ||
11302 + apkg == NULL || apkg->provided_by == NULL || (apkg->provided_by->len == 0))
11303 + return NULL;
11304 +
11305 + ipkg_message(conf, IPKG_DEBUG, "best installation candidate for %s\n", apkg->name);
11306 +
11307 + provided_apkg_vec = apkg->provided_by;
11308 + nprovides = provided_apkg_vec->len;
11309 + provided_apkgs = provided_apkg_vec->pkgs;
11310 + if (nprovides > 1)
11311 + ipkg_message(conf, IPKG_DEBUG, " apkg=%s nprovides=%d\n", apkg->name, nprovides);
11312 +
11313 + /* accumulate all the providers */
11314 + for (i = 0; i < nprovides; i++) {
11315 + abstract_pkg_t *provider_apkg = provided_apkgs[i];
11316 + ipkg_message(conf, IPKG_DEBUG, " adding %s to providers\n", provider_apkg->name);
11317 + abstract_pkg_vec_insert(providers, provider_apkg);
11318 + }
11319 + nprovides = providers->len;
11320 +
11321 + for (i = 0; i < nprovides; i++) {
11322 + abstract_pkg_t *provider_apkg = abstract_pkg_vec_get(providers, i);
11323 + abstract_pkg_t *replacement_apkg = NULL;
11324 + pkg_vec_t *vec;
11325 +
11326 + if (provider_apkg->replaced_by && provider_apkg->replaced_by->len) {
11327 + replacement_apkg = provider_apkg->replaced_by->pkgs[0];
11328 + if (provider_apkg->replaced_by->len > 1) {
11329 + ipkg_message(conf, IPKG_NOTICE, "Multiple replacers for %s, using first one (%s)\n",
11330 + provider_apkg->name, replacement_apkg->name);
11331 + }
11332 + }
11333 +
11334 + if (replacement_apkg)
11335 + ipkg_message(conf, IPKG_DEBUG, " replacement_apkg=%s for provider_apkg=%s\n",
11336 + replacement_apkg->name, provider_apkg->name);
11337 +
11338 + if (replacement_apkg && (replacement_apkg != provider_apkg)) {
11339 + if (abstract_pkg_vec_contains(providers, replacement_apkg))
11340 + continue;
11341 + else
11342 + provider_apkg = replacement_apkg;
11343 + }
11344 +
11345 + if (!(vec = provider_apkg->pkgs)) {
11346 + ipkg_message(conf, IPKG_DEBUG, " no pkgs for provider_apkg %s\n", provider_apkg->name);
11347 + continue;
11348 + }
11349 +
11350 +
11351 + /* now check for supported architecture */
11352 + {
11353 + int max_count = 0;
11354 +
11355 + /* count packages matching max arch priority and keep track of last one */
11356 + for (j = 0; j < vec->len; j++) {
11357 + pkg_t *maybe = vec->pkgs[j];
11358 + ipkg_message(conf, IPKG_DEBUG, " %s arch=%s arch_priority=%d version=%s \n",
11359 + maybe->name, maybe->architecture, maybe->arch_priority, maybe->version);
11360 + if (maybe->arch_priority > 0) {
11361 + max_count++;
11362 + abstract_pkg_vec_insert(matching_apkgs, maybe->parent);
11363 + pkg_vec_insert(matching_pkgs, maybe);
11364 + }
11365 + }
11366 + }
11367 + }
11368 +
11369 + if (matching_pkgs->len > 1)
11370 + pkg_vec_sort(matching_pkgs, pkg_name_version_and_architecture_compare);
11371 + if (matching_apkgs->len > 1)
11372 + abstract_pkg_vec_sort(matching_pkgs, abstract_pkg_name_compare);
11373 +
11374 +/* Here it is usefull, if ( matching_apkgs->len > 1 ), to test if one of this matching packages has the same name of the
11375 + needed package. In this case, I would return it for install, otherwise I will continue with the procedure */
11376 +/* The problem is what to do when there are more than a mathing package, with the same name and several version ?
11377 + Until now I always got the latest, but that breaks the downgrade option.
11378 + If I stop at the first one, I would probably miss the new ones
11379 + Maybe the way is to have some kind of flag somewhere, to see if the package been asked to install is from a file,
11380 + or from a Packages feed.
11381 + 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*/
11382 +/*Pigi*/
11383 +
11384 + for (i = 0; i < matching_pkgs->len; i++) {
11385 + pkg_t *matching = matching_pkgs->pkgs[i];
11386 + if (constraint_fcn(matching, cdata)) { /* We found it */
11387 + ipkg_message(conf, IPKG_DEBUG, " Found a valid candidate for the install: %s %s \n", matching->name, matching->version) ;
11388 + good_pkg_by_name = matching;
11389 + if ( matching->provided_by_hand == 1 ) /* It has been provided by hand, so it is what user want */
11390 + break;
11391 + }
11392 + }
11393 +
11394 +
11395 + for (i = 0; i < matching_pkgs->len; i++) {
11396 + pkg_t *matching = matching_pkgs->pkgs[i];
11397 + latest_matching = matching;
11398 + if (matching->parent->state_status == SS_INSTALLED || matching->parent->state_status == SS_UNPACKED)
11399 + latest_installed_parent = matching;
11400 + if (matching->state_flag & (SF_HOLD|SF_PREFER)) {
11401 + if (held_pkg)
11402 + ipkg_message(conf, IPKG_ERROR, "Multiple packages (%s and %s) providing same name marked HOLD or PREFER. Using latest.\n",
11403 + held_pkg->name, matching->name);
11404 + held_pkg = matching;
11405 + }
11406 + }
11407 +
11408 + if (!good_pkg_by_name && !held_pkg && !latest_installed_parent && matching_apkgs->len > 1 && !quiet) {
11409 + ipkg_message(conf, IPKG_ERROR, "Package=%s, %d matching providers\n",
11410 + apkg->name, matching_apkgs->len);
11411 + for (i = 0; i < matching_apkgs->len; i++) {
11412 + abstract_pkg_t *matching = matching_apkgs->pkgs[i];
11413 + ipkg_message(conf, IPKG_ERROR, " %s\n", matching->name);
11414 + }
11415 + ipkg_message(conf, IPKG_ERROR, "Please select one with ipkg install or ipkg flag prefer\n");
11416 + }
11417 +
11418 + if (matching_apkgs->len > 1 && conf->verbosity > 1) {
11419 + ipkg_message(conf, IPKG_NOTICE, "%s: for apkg=%s, %d matching pkgs\n",
11420 + __FUNCTION__, apkg->name, matching_pkgs->len);
11421 + for (i = 0; i < matching_pkgs->len; i++) {
11422 + pkg_t *matching = matching_pkgs->pkgs[i];
11423 + ipkg_message(conf, IPKG_INFO, " %s %s %s\n",
11424 + matching->name, matching->version, matching->architecture);
11425 + }
11426 + }
11427 +
11428 + nmatching = matching_apkgs->len;
11429 +
11430 + pkg_vec_free(matching_pkgs);
11431 + abstract_pkg_vec_free(matching_apkgs);
11432 + abstract_pkg_vec_free(providers);
11433 +
11434 + if (good_pkg_by_name) { /* We found a good candidate, we will install it */
11435 + return good_pkg_by_name;
11436 + }
11437 + if (held_pkg) {
11438 + ipkg_message(conf, IPKG_INFO, " using held package %s\n", held_pkg->name);
11439 + return held_pkg;
11440 + }
11441 + if (latest_installed_parent) {
11442 + ipkg_message(conf, IPKG_INFO, " using latest version of installed package %s\n", latest_installed_parent->name);
11443 + return latest_installed_parent;
11444 + }
11445 + if (nmatching > 1) {
11446 + ipkg_message(conf, IPKG_INFO, " no matching pkg out of matching_apkgs=%d\n", nmatching);
11447 + return NULL;
11448 + }
11449 + if (latest_matching) {
11450 + ipkg_message(conf, IPKG_INFO, " using latest matching %s %s %s\n",
11451 + latest_matching->name, latest_matching->version, latest_matching->architecture);
11452 + return latest_matching;
11453 + }
11454 + return NULL;
11455 +}
11456 +
11457 +static int pkg_name_constraint_fcn(pkg_t *pkg, void *cdata)
11458 +{
11459 + const char *name = (const char *)cdata;
11460 + if (strcmp(pkg->name, name) == 0)
11461 + return 1;
11462 + else
11463 + return 0;
11464 +}
11465 +
11466 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name)
11467 +{
11468 + hash_table_t *hash = &conf->pkg_hash;
11469 + abstract_pkg_t *apkg = NULL;
11470 +
11471 + if (!(apkg = abstract_pkg_fetch_by_name(hash, name)))
11472 + return NULL;
11473 +
11474 + return pkg_hash_fetch_best_installation_candidate(conf, apkg, pkg_name_constraint_fcn, apkg->name, 0);
11475 +}
11476 +
11477 +
11478 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash,
11479 + const char *pkg_name,
11480 + const char * version)
11481 +{
11482 + pkg_vec_t * vec;
11483 + register int i;
11484 + char *version_str = NULL;
11485 +
11486 + if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name)))
11487 + return NULL;
11488 +
11489 + for(i = 0; i < vec->len; i++) {
11490 + version_str = pkg_version_str_alloc(vec->pkgs[i]);
11491 + if(!strcmp(version_str, version)) {
11492 + free(version_str);
11493 + break;
11494 + }
11495 + free(version_str);
11496 + }
11497 +
11498 + if(i == vec->len)
11499 + return NULL;
11500 +
11501 + return vec->pkgs[i];
11502 +}
11503 +
11504 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
11505 + const char *pkg_name,
11506 + pkg_dest_t *dest)
11507 +{
11508 + pkg_vec_t * vec;
11509 + register int i;
11510 +
11511 + if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))) {
11512 + return NULL;
11513 + }
11514 +
11515 + for(i = 0; i < vec->len; i++)
11516 + if((vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED) && vec->pkgs[i]->dest == dest) {
11517 + return vec->pkgs[i];
11518 + }
11519 + return NULL;
11520 +}
11521 +
11522 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
11523 + const char *pkg_name)
11524 +{
11525 + pkg_vec_t * vec;
11526 + register int i;
11527 +
11528 + if(!(vec = pkg_vec_fetch_by_name(hash, pkg_name))){
11529 + return NULL;
11530 + }
11531 +
11532 + for(i = 0; i < vec->len; i++)
11533 + if (vec->pkgs[i]->state_status == SS_INSTALLED || vec->pkgs[i]->state_status == SS_UNPACKED){
11534 + return vec->pkgs[i];
11535 + }
11536 +
11537 + return NULL;
11538 +}
11539 +
11540 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name)
11541 +{
11542 + abstract_pkg_t * ab_pkg;
11543 +
11544 + if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name))){
11545 + return NULL;
11546 + }
11547 +
11548 + if (ab_pkg->pkgs) {
11549 + return ab_pkg->pkgs;
11550 + } else if (ab_pkg->provided_by) {
11551 + abstract_pkg_t *abpkg = abstract_pkg_vec_get(ab_pkg->provided_by, 0);
11552 + if (abpkg != NULL){
11553 + return abpkg->pkgs;
11554 + } else {
11555 + return ab_pkg->pkgs;
11556 + }
11557 + } else {
11558 + return NULL;
11559 + }
11560 +}
11561 +
11562 +static int pkg_compare_names(const void *p1, const void *p2)
11563 +{
11564 + const pkg_t *pkg1 = *(const pkg_t **)p1;
11565 + const pkg_t *pkg2 = *(const pkg_t **)p2;
11566 + if (pkg1->name == NULL)
11567 + return 1;
11568 + if (pkg2->name == NULL)
11569 + return -1;
11570 + return(strcmp(pkg1->name, pkg2->name));
11571 +}
11572 +
11573 +
11574 +static void pkg_hash_fetch_available_helper(const char *pkg_name, void *entry, void *data)
11575 +{
11576 + int j;
11577 + abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11578 + pkg_vec_t *all = (pkg_vec_t *)data;
11579 + pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11580 + if (pkg_vec) {
11581 + for (j = 0; j < pkg_vec->len; j++) {
11582 + pkg_t *pkg = pkg_vec->pkgs[j];
11583 + pkg_vec_insert(all, pkg);
11584 + }
11585 + }
11586 +}
11587 +
11588 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *all)
11589 +{
11590 + hash_table_foreach(hash, pkg_hash_fetch_available_helper, all);
11591 + qsort(all->pkgs, all->len, sizeof(pkg_t *), pkg_compare_names);
11592 +}
11593 +
11594 +static void pkg_hash_fetch_all_installed_helper(const char *pkg_name, void *entry, void *data)
11595 +{
11596 + abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11597 + pkg_vec_t *all = (pkg_vec_t *)data;
11598 + pkg_vec_t *pkg_vec = ab_pkg->pkgs;
11599 + int j;
11600 + if (pkg_vec) {
11601 + for (j = 0; j < pkg_vec->len; j++) {
11602 + pkg_t *pkg = pkg_vec->pkgs[j];
11603 + if (pkg->state_status == SS_INSTALLED || pkg->state_status == SS_UNPACKED) {
11604 + pkg_vec_insert(all, pkg);
11605 + }
11606 + }
11607 + }
11608 +}
11609 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *all)
11610 +{
11611 + hash_table_foreach(hash, pkg_hash_fetch_all_installed_helper, all);
11612 + qsort(all->pkgs, all->len, sizeof(void*), pkg_compare_names);
11613 +}
11614 +
11615 +static void pkg_hash_dump_helper(const char *pkg_name, void *entry, void *data)
11616 +{
11617 + int i;
11618 + pkg_t *pkg;
11619 + abstract_pkg_t *ab_pkg = (abstract_pkg_t *)entry;
11620 + ipkg_conf_t *conf = (ipkg_conf_t *)data;
11621 + abstract_pkg_t ** dependents = ab_pkg->depended_upon_by;
11622 + fprintf(stdout, "%s\n", ab_pkg->name);
11623 + i = 0;
11624 + if (dependents != NULL)
11625 + while (dependents [i] != NULL)
11626 + printf ("\tdepended upon by - %s\n", dependents [i ++]->name);
11627 + dependents = ab_pkg->provided_by->pkgs;
11628 + i = 0;
11629 + if (dependents != NULL)
11630 + while (dependents [i] != NULL && i < ab_pkg->provided_by->len)
11631 + printf ("\tprovided by - %s\n", dependents [i ++]->name);
11632 + pkg = pkg_hash_fetch_best_installation_candidate_by_name (conf, ab_pkg->name);
11633 + if (pkg) {
11634 + i = 0;
11635 + while (i < pkg->depends_count)
11636 + printf ("\tdepends on - %s\n", pkg->depends_str [i ++]);
11637 + }
11638 +}
11639 +void pkg_hash_dump(hash_table_t *hash, void *data)
11640 +{
11641 +
11642 + printf ("\n\n+=+%s+=+\n\n", __FUNCTION__);
11643 + hash_table_foreach(hash, pkg_hash_dump_helper, data);
11644 + printf ("\n+=+%s+=+\n\n", __FUNCTION__);
11645 +}
11646 +
11647 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11648 +{
11649 + abstract_pkg_t * ab_pkg;
11650 +
11651 + if(!(ab_pkg = abstract_pkg_fetch_by_name(hash, pkg_name)))
11652 + ab_pkg = add_new_abstract_pkg_by_name(hash, pkg_name);
11653 +
11654 + return ab_pkg;
11655 +}
11656 +
11657 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
11658 +{
11659 + abstract_pkg_t * ab_pkg;
11660 + int arch_priority;
11661 +
11662 + if(!pkg)
11663 + return pkg;
11664 +
11665 + arch_priority = pkg->arch_priority;
11666 +
11667 + if (buildDepends(hash, pkg)<0){
11668 + fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11669 + return NULL;
11670 + }
11671 + ab_pkg = ensure_abstract_pkg_by_name(hash, pkg->name);
11672 +
11673 + if (set_status) {
11674 + if (pkg->state_status == SS_INSTALLED) {
11675 + ab_pkg->state_status = SS_INSTALLED;
11676 + } else if (pkg->state_status == SS_UNPACKED) {
11677 + ab_pkg->state_status = SS_UNPACKED;
11678 + }
11679 + }
11680 +
11681 + if(!ab_pkg->pkgs)
11682 + ab_pkg->pkgs = pkg_vec_alloc();
11683 +
11684 + /* pkg_vec_insert_merge might munge package, but it returns an unmunged pkg */
11685 + pkg = pkg_vec_insert_merge(ab_pkg->pkgs, pkg, set_status,conf );
11686 + pkg->parent = ab_pkg;
11687 +
11688 + if (buildProvides(hash, ab_pkg, pkg)<0){
11689 + fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11690 + return NULL;
11691 + }
11692 + /* need to build the conflicts graph before replaces for correct calculation of replaced_by relation */
11693 + if (buildConflicts(hash, ab_pkg, pkg)<0){
11694 + fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11695 + return NULL;
11696 + }
11697 + if (buildReplaces(hash, ab_pkg, pkg)<0) {
11698 + fprintf(stderr, "%s : This should never happen. Report this Bug in bugzilla please \n ",__FUNCTION__);
11699 + return NULL;
11700 + }
11701 +
11702 + buildDependedUponBy(pkg, ab_pkg);
11703 + return pkg;
11704 +}
11705 +
11706 +/*
11707 + * this will assume that we've already determined that
11708 + * the abstract pkg doesn't exist, 'cause we should know these things...
11709 + */
11710 +static abstract_pkg_t * add_new_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name)
11711 +{
11712 + abstract_pkg_t * ab_pkg;
11713 +
11714 + ab_pkg = abstract_pkg_new();
11715 + if (ab_pkg == NULL) { return NULL; }
11716 +
11717 + ab_pkg->name = strdup(pkg_name);
11718 + hash_table_insert(hash, pkg_name, ab_pkg);
11719 +
11720 + return ab_pkg;
11721 +}
11722 +
11723 +
11724 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name)
11725 +{
11726 + hash_table_t *file_hash = &conf->file_hash;
11727 +
11728 + return hash_table_get(file_hash, file_name);
11729 +}
11730 +
11731 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *owning_pkg)
11732 +{
11733 + hash_table_t *file_hash = &conf->file_hash;
11734 + pkg_t *old_owning_pkg = hash_table_get(file_hash, file_name);
11735 + int file_name_len = strlen(file_name);
11736 +
11737 + if (file_name[file_name_len -1] == '/')
11738 + return 0;
11739 +
11740 + if (conf->offline_root) {
11741 + int len = strlen(conf->offline_root);
11742 + if (strncmp(file_name, conf->offline_root, len) == 0) {
11743 + file_name += len;
11744 + }
11745 + }
11746 +
11747 + // ipkg_message(conf, IPKG_DEBUG2, "owning_pkg=%s filename=%s\n", owning_pkg->name, file_name);
11748 + hash_table_insert(file_hash, file_name, owning_pkg);
11749 + if (old_owning_pkg) {
11750 + str_list_remove_elt(old_owning_pkg->installed_files, file_name);
11751 + /* mark this package to have its filelist written */
11752 + old_owning_pkg->state_flag |= SF_FILELIST_CHANGED;
11753 + owning_pkg->state_flag |= SF_FILELIST_CHANGED;
11754 + }
11755 + return 0;
11756 +}
11757 +
11758 +
11759 --- /dev/null
11760 +++ b/archival/libipkg/pkg_hash.h
11761 @@ -0,0 +1,61 @@
11762 +/* pkg_hash.h - the itsy package management system
11763 +
11764 + Steven M. Ayer
11765 +
11766 + Copyright (C) 2002 Compaq Computer Corporation
11767 +
11768 + This program is free software; you can redistribute it and/or
11769 + modify it under the terms of the GNU General Public License as
11770 + published by the Free Software Foundation; either version 2, or (at
11771 + your option) any later version.
11772 +
11773 + This program is distributed in the hope that it will be useful, but
11774 + WITHOUT ANY WARRANTY; without even the implied warranty of
11775 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11776 + General Public License for more details.
11777 +*/
11778 +
11779 +#ifndef PKG_HASH_H
11780 +#define PKG_HASH_H
11781 +
11782 +#include "pkg.h"
11783 +#include "pkg_vec.h"
11784 +#include "hash_table.h"
11785 +
11786 +
11787 +int pkg_hash_init(const char *name, hash_table_t *hash, int len);
11788 +void pkg_hash_deinit(hash_table_t *hash);
11789 +void pkg_hash_map(hash_table_t *hash, void (*f)(void *data, void *entry), void *data);
11790 +
11791 +void pkg_hash_dump(hash_table_t *hash, void *data);
11792 +void pkg_hash_fetch_available(hash_table_t *hash, pkg_vec_t *available);
11793 +
11794 +int pkg_hash_add_from_file(ipkg_conf_t *conf, const char *file_name,
11795 + pkg_src_t *src, pkg_dest_t *dest, int is_status_file);
11796 +pkg_t *hash_insert_pkg(hash_table_t *hash, pkg_t *pkg, int set_status,ipkg_conf_t *conf);
11797 +
11798 +abstract_pkg_t * ensure_abstract_pkg_by_name(hash_table_t * hash, const char * pkg_name);
11799 +abstract_pkg_t * abstract_pkg_fetch_by_name(hash_table_t * hash, const char * pkg_name);
11800 +pkg_vec_t *pkg_hash_fetch_by_name(hash_table_t *hash, const char *pkg_name);
11801 +void pkg_hash_fetch_all_installed(hash_table_t *hash, pkg_vec_t *installed);
11802 +pkg_t * pkg_hash_fetch_by_name_version(hash_table_t *hash,
11803 + const char *pkg_name,
11804 + const char * version);
11805 +abstract_pkg_vec_t *pkg_hash_fetch_all_installation_candidates(hash_table_t *hash, const char *name);
11806 +pkg_t *pkg_hash_fetch_best_installation_candidate(ipkg_conf_t *conf, abstract_pkg_t *apkg,
11807 + int (*constraint_fcn)(pkg_t *pkg, void *data), void *cdata, int quiet);
11808 +pkg_t *pkg_hash_fetch_best_installation_candidate_by_name(ipkg_conf_t *conf, const char *name);
11809 +pkg_t *pkg_hash_fetch_installed_by_name(hash_table_t *hash,
11810 + const char *pkg_name);
11811 +pkg_t *pkg_hash_fetch_installed_by_name_dest(hash_table_t *hash,
11812 + const char *pkg_name,
11813 + pkg_dest_t *dest);
11814 +
11815 +pkg_t *file_hash_get_file_owner(ipkg_conf_t *conf, const char *file_name);
11816 +int file_hash_set_file_owner(ipkg_conf_t *conf, const char *file_name, pkg_t *pkg);
11817 +
11818 +/* XXX: shouldn't this go in pkg_vec.[ch]? */
11819 +pkg_vec_t *pkg_vec_fetch_by_name(hash_table_t *hash, const char *pkg_name);
11820 +
11821 +#endif
11822 +
11823 --- /dev/null
11824 +++ b/archival/libipkg/pkg_parse.c
11825 @@ -0,0 +1,366 @@
11826 +/* pkg_parse.c - the itsy package management system
11827 +
11828 + Steven M. Ayer
11829 +
11830 + Copyright (C) 2002 Compaq Computer Corporation
11831 +
11832 + This program is free software; you can redistribute it and/or
11833 + modify it under the terms of the GNU General Public License as
11834 + published by the Free Software Foundation; either version 2, or (at
11835 + your option) any later version.
11836 +
11837 + This program is distributed in the hope that it will be useful, but
11838 + WITHOUT ANY WARRANTY; without even the implied warranty of
11839 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11840 + General Public License for more details.
11841 +*/
11842 +
11843 +#include "ipkg.h"
11844 +#include <errno.h>
11845 +#include <ctype.h>
11846 +
11847 +#include "pkg.h"
11848 +#include "ipkg_utils.h"
11849 +#include "pkg_parse.h"
11850 +
11851 +int isGenericFieldType(char * type, char * line)
11852 +{
11853 + if(!strncmp(line, type, strlen(type)))
11854 + return 1;
11855 + return 0;
11856 +}
11857 +
11858 +char * parseGenericFieldType(char * type, char * raw)
11859 +{
11860 + char * field_value = raw + (strlen(type) + 1);
11861 + return trim_alloc(field_value);
11862 +}
11863 +
11864 +void parseStatus(pkg_t *pkg, char * raw)
11865 +{
11866 + char sw_str[64], sf_str[64], ss_str[64];
11867 +
11868 + sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
11869 + pkg->state_want = pkg_state_want_from_str(sw_str);
11870 + pkg->state_flag = pkg_state_flag_from_str(sf_str);
11871 + pkg->state_status = pkg_state_status_from_str(ss_str);
11872 +}
11873 +
11874 +char ** parseDependsString(char * raw, int * depends_count)
11875 +{
11876 + char ** depends = NULL;
11877 + int line_count = 0;
11878 + char buff[2048], * dest;
11879 +
11880 + while(raw && *raw && !isspace(*raw)) {
11881 + raw++;
11882 + }
11883 +
11884 + if(line_is_blank(raw)){
11885 + *depends_count = line_count;
11886 + return NULL;
11887 + }
11888 + while(raw && *raw){
11889 + depends = (char **)realloc(depends, sizeof(char *) * (line_count + 1));
11890 +
11891 + while(isspace(*raw)) raw++;
11892 +
11893 + dest = buff;
11894 + while((*raw != ',') && *raw)
11895 + *dest++ = *raw++;
11896 +
11897 + *dest = '\0';
11898 + depends[line_count] = trim_alloc(buff);
11899 + if(depends[line_count] ==NULL)
11900 + return NULL;
11901 + line_count++;
11902 + if(*raw == ',')
11903 + raw++;
11904 + }
11905 + *depends_count = line_count;
11906 + return depends;
11907 +}
11908 +
11909 +void parseConffiles(pkg_t * pkg, char * raw)
11910 +{
11911 + char file_name[1048], md5sum[1048]; /* please tell me there aren't any longer that 1k */
11912 +
11913 + if(!strncmp(raw, "Conffiles:", 10))
11914 + raw += strlen("Conffiles:");
11915 +
11916 + while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
11917 + conffile_list_append(&pkg->conffiles, file_name, md5sum);
11918 + /* fprintf(stderr, "%s %s ", file_name, md5sum);*/
11919 + while (*raw && isspace(*raw)) {
11920 + raw++;
11921 + }
11922 + raw += strlen(file_name);
11923 + while (*raw && isspace(*raw)) {
11924 + raw++;
11925 + }
11926 + raw += strlen(md5sum);
11927 + }
11928 +}
11929 +
11930 +int parseVersion(pkg_t *pkg, char *raw)
11931 +{
11932 + char *colon, *eepochcolon;
11933 +#ifdef USE_DEBVERSION
11934 + char *hyphen;
11935 +#endif
11936 + unsigned long epoch;
11937 +
11938 + if (!*raw) {
11939 + fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
11940 + return EINVAL;
11941 + }
11942 +
11943 + if (strncmp(raw, "Version:", 8) == 0) {
11944 + raw += 8;
11945 + }
11946 + while (*raw && isspace(*raw)) {
11947 + raw++;
11948 + }
11949 +
11950 + colon= strchr(raw,':');
11951 + if (colon) {
11952 + epoch= strtoul(raw,&eepochcolon,10);
11953 + if (colon != eepochcolon) {
11954 + fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
11955 + return EINVAL;
11956 + }
11957 + if (!*++colon) {
11958 + fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
11959 + return EINVAL;
11960 + }
11961 + raw= colon;
11962 + pkg->epoch= epoch;
11963 + } else {
11964 + pkg->epoch= 0;
11965 + }
11966 +
11967 + pkg->revision = "";
11968 + pkg->familiar_revision = "";
11969 +
11970 + pkg->version= malloc(strlen(raw)+1);
11971 + if ( pkg->version == NULL ) {
11972 + fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
11973 + return ENOMEM;
11974 + }
11975 + strcpy(pkg->version, raw);
11976 +
11977 +#ifdef USE_DEBVERSION
11978 + hyphen= strrchr(pkg->version,'-');
11979 +
11980 + if (hyphen) {
11981 + *hyphen++= 0;
11982 + if (strncmp("fam", hyphen, 3) == 0) {
11983 + pkg->familiar_revision=hyphen+3;
11984 + hyphen= strrchr(pkg->version,'-');
11985 + if (hyphen) {
11986 + *hyphen++= 0;
11987 + pkg->revision = hyphen;
11988 + }
11989 + } else {
11990 + pkg->revision = hyphen;
11991 + }
11992 + }
11993 +#endif
11994 +
11995 +/*
11996 + fprintf(stderr,"Parsed version: %lu, %s, %s, %s\n",
11997 + pkg->epoch,
11998 + pkg->version,
11999 + pkg->revision,
12000 + pkg->familiar_revision);
12001 +*/
12002 +
12003 + return 0;
12004 +}
12005 +
12006 +
12007 +/* This code is needed to insert in first position the keyword for the aligning bug */
12008 +
12009 +int alterProvidesLine(char *raw, char *temp)
12010 +{
12011 +
12012 +
12013 + if (!*raw) {
12014 + fprintf(stderr, "%s: ERROR: Provides string is empty", __FUNCTION__);
12015 + return -EINVAL;
12016 + }
12017 +
12018 + if ( temp == NULL ) {
12019 + fprintf(stderr, "%s: out of memory \n", __FUNCTION__);
12020 + return -ENOMEM;
12021 + }
12022 +
12023 + if (strncmp(raw, "Provides:", 9) == 0) {
12024 + raw += 9;
12025 + }
12026 + while (*raw && isspace(*raw)) {
12027 + raw++;
12028 + }
12029 +
12030 + snprintf ( temp, 35, "Provides: ipkg_internal_use_only, "); /* First part of the line */
12031 + while (*raw) {
12032 + strncat( temp, raw++, 1);
12033 + }
12034 + return 0;
12035 +
12036 +}
12037 +
12038 +/* Some random thoughts from Carl:
12039 +
12040 + This function could be considerably simplified if we just kept
12041 + an array of all the generic string-valued field names, and looped
12042 + through those looking for a match. Also, these fields could perhaps
12043 + be stored in the package as an array as well, (or, probably better,
12044 + as an nv_pair_list_t).
12045 +
12046 + Fields which require special parsing or storage, (such as Depends:
12047 + and Status:) could be handled as they are now.
12048 +*/
12049 +/* XXX: FEATURE: The Suggests: field needs to be changed from a string
12050 + to a dependency list. And, since we already have
12051 + Depends/Pre-Depends and need to add Conflicts, Recommends, and
12052 + Enhances, perhaps we could generalize all of these and save some
12053 + code duplication.
12054 +*/
12055 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest)
12056 +{
12057 + int reading_conffiles, reading_description;
12058 + int pkg_false_provides=1;
12059 + char ** lines;
12060 + char * provide=NULL;
12061 +
12062 + pkg->src = src;
12063 + pkg->dest = dest;
12064 +
12065 + reading_conffiles = reading_description = 0;
12066 +
12067 + for (lines = *raw; *lines; lines++) {
12068 + /* fprintf(stderr, "PARSING %s\n", *lines);*/
12069 + if(isGenericFieldType("Package:", *lines))
12070 + pkg->name = parseGenericFieldType("Package", *lines);
12071 + else if(isGenericFieldType("Architecture:", *lines))
12072 + pkg->architecture = parseGenericFieldType("Architecture", *lines);
12073 + else if(isGenericFieldType("Filename:", *lines))
12074 + pkg->filename = parseGenericFieldType("Filename", *lines);
12075 + else if(isGenericFieldType("Section:", *lines))
12076 + pkg->section = parseGenericFieldType("Section", *lines);
12077 + else if(isGenericFieldType("MD5sum:", *lines))
12078 + pkg->md5sum = parseGenericFieldType("MD5sum", *lines);
12079 + /* The old ipkg wrote out status files with the wrong case for MD5sum,
12080 + let's parse it either way */
12081 + else if(isGenericFieldType("MD5Sum:", *lines))
12082 + pkg->md5sum = parseGenericFieldType("MD5Sum", *lines);
12083 + else if(isGenericFieldType("Size:", *lines))
12084 + pkg->size = parseGenericFieldType("Size", *lines);
12085 + else if(isGenericFieldType("Source:", *lines))
12086 + pkg->source = parseGenericFieldType("Source", *lines);
12087 + else if(isGenericFieldType("Installed-Size:", *lines))
12088 + pkg->installed_size = parseGenericFieldType("Installed-Size", *lines);
12089 + else if(isGenericFieldType("Installed-Time:", *lines)) {
12090 + char *time_str = parseGenericFieldType("Installed-Time", *lines);
12091 + pkg->installed_time = strtoul(time_str, NULL, 0);
12092 + } else if(isGenericFieldType("Priority:", *lines))
12093 + pkg->priority = parseGenericFieldType("Priority", *lines);
12094 + else if(isGenericFieldType("Essential:", *lines)) {
12095 + char *essential_value;
12096 + essential_value = parseGenericFieldType("Essential", *lines);
12097 + if (strcmp(essential_value, "yes") == 0) {
12098 + pkg->essential = 1;
12099 + }
12100 + free(essential_value);
12101 + }
12102 + else if(isGenericFieldType("Status", *lines))
12103 + parseStatus(pkg, *lines);
12104 + else if(isGenericFieldType("Version", *lines))
12105 + parseVersion(pkg, *lines);
12106 + else if(isGenericFieldType("Maintainer", *lines))
12107 + pkg->maintainer = parseGenericFieldType("Maintainer", *lines);
12108 + else if(isGenericFieldType("Conffiles", *lines)){
12109 + parseConffiles(pkg, *lines);
12110 + reading_conffiles = 1;
12111 + }
12112 + else if(isGenericFieldType("Description", *lines)) {
12113 + pkg->description = parseGenericFieldType("Description", *lines);
12114 + reading_conffiles = 0;
12115 + reading_description = 1;
12116 + }
12117 +
12118 + else if(isGenericFieldType("Provides", *lines)){
12119 +/* Here we add the internal_use to align the off by one problem between provides_str and provides */
12120 + provide = (char * ) malloc(strlen(*lines)+ 35 ); /* Preparing the space for the new ipkg_internal_use_only */
12121 + if ( alterProvidesLine(*lines,provide) ){
12122 + return EINVAL;
12123 + }
12124 + pkg->provides_str = parseDependsString( provide, &pkg->provides_count);
12125 +/* Let's try to hack a bit here.
12126 + The idea is that if a package has no Provides, we would add one generic, to permit the check of dependencies
12127 + in alot of other places. We will remove it before writing down the status database */
12128 + pkg_false_provides=0;
12129 + free(provide);
12130 + }
12131 +
12132 + else if(isGenericFieldType("Depends", *lines))
12133 + pkg->depends_str = parseDependsString(*lines, &pkg->depends_count);
12134 + else if(isGenericFieldType("Pre-Depends", *lines))
12135 + pkg->pre_depends_str = parseDependsString(*lines, &pkg->pre_depends_count);
12136 + else if(isGenericFieldType("Recommends", *lines))
12137 + pkg->recommends_str = parseDependsString(*lines, &pkg->recommends_count);
12138 + else if(isGenericFieldType("Suggests", *lines))
12139 + pkg->suggests_str = parseDependsString(*lines, &pkg->suggests_count);
12140 + /* Abhaya: support for conflicts */
12141 + else if(isGenericFieldType("Conflicts", *lines))
12142 + pkg->conflicts_str = parseDependsString(*lines, &pkg->conflicts_count);
12143 + else if(isGenericFieldType("Replaces", *lines))
12144 + pkg->replaces_str = parseDependsString(*lines, &pkg->replaces_count);
12145 + else if(line_is_blank(*lines)) {
12146 + lines++;
12147 + break;
12148 + }
12149 + else if(**lines == ' '){
12150 + if(reading_description) {
12151 + /* we already know it's not blank, so the rest of description */
12152 + pkg->description = realloc(pkg->description,
12153 + strlen(pkg->description)
12154 + + 1 + strlen(*lines) + 1);
12155 + strcat(pkg->description, "\n");
12156 + strcat(pkg->description, (*lines));
12157 + }
12158 + else if(reading_conffiles)
12159 + parseConffiles(pkg, *lines);
12160 + }
12161 + }
12162 + *raw = lines;
12163 +/* If the ipk has not a Provides line, we insert our false line */
12164 + if ( pkg_false_provides==1)
12165 + pkg->provides_str = parseDependsString ((char *)"Provides: ipkg_internal_use_only ", &pkg->provides_count);
12166 +
12167 + if (pkg->name) {
12168 + return 0;
12169 + } else {
12170 + return EINVAL;
12171 + }
12172 +}
12173 +
12174 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw)
12175 +{
12176 + char ** lines;
12177 +
12178 + for (lines = *raw; *lines; lines++) {
12179 + if(isGenericFieldType("Essential:", *lines)) {
12180 + char *essential_value;
12181 + essential_value = parseGenericFieldType("Essential", *lines);
12182 + if (strcmp(essential_value, "yes") == 0) {
12183 + pkg->essential = 1;
12184 + }
12185 + free(essential_value);
12186 + }
12187 + }
12188 + *raw = lines;
12189 +
12190 + return 0;
12191 +}
12192 --- /dev/null
12193 +++ b/archival/libipkg/pkg_parse.h
12194 @@ -0,0 +1,31 @@
12195 +/* pkg_parse.h - the itsy package management system
12196 +
12197 + Steven M. Ayer
12198 +
12199 + Copyright (C) 2002 Compaq Computer Corporation
12200 +
12201 + This program is free software; you can redistribute it and/or
12202 + modify it under the terms of the GNU General Public License as
12203 + published by the Free Software Foundation; either version 2, or (at
12204 + your option) any later version.
12205 +
12206 + This program is distributed in the hope that it will be useful, but
12207 + WITHOUT ANY WARRANTY; without even the implied warranty of
12208 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12209 + General Public License for more details.
12210 +*/
12211 +
12212 +#ifndef PKG_PARSE_H
12213 +#define PKG_PARSE_H
12214 +
12215 +int isGenericFieldType(char * type, char * line);
12216 +char * parseGenericFieldType(char * type, char * raw);
12217 +void parseStatus(pkg_t *pkg, char * raw);
12218 +int parseVersion(pkg_t *pkg, char *raw);
12219 +char ** parseDependsString(char * raw, int * depends_count);
12220 +int parseVersion(pkg_t *pkg, char *raw);
12221 +void parseConffiles(pkg_t * pkg, char * raw);
12222 +int pkg_parse_raw(pkg_t *pkg, char ***raw, pkg_src_t *src, pkg_dest_t *dest);
12223 +int pkg_valorize_other_field(pkg_t *pkg, char ***raw);
12224 +
12225 +#endif
12226 --- /dev/null
12227 +++ b/archival/libipkg/pkg_src.c
12228 @@ -0,0 +1,43 @@
12229 +/* pkg_src.c - the itsy package management system
12230 +
12231 + Carl D. Worth
12232 +
12233 + Copyright (C) 2001 University of Southern California
12234 +
12235 + This program is free software; you can redistribute it and/or
12236 + modify it under the terms of the GNU General Public License as
12237 + published by the Free Software Foundation; either version 2, or (at
12238 + your option) any later version.
12239 +
12240 + This program is distributed in the hope that it will be useful, but
12241 + WITHOUT ANY WARRANTY; without even the implied warranty of
12242 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12243 + General Public License for more details.
12244 +*/
12245 +
12246 +#include "ipkg.h"
12247 +
12248 +#include "pkg_src.h"
12249 +#include "str_util.h"
12250 +
12251 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip)
12252 +{
12253 + src->gzip = gzip;
12254 + src->name = str_dup_safe (name);
12255 + src->value = str_dup_safe (base_url);
12256 + if (extra_data)
12257 + src->extra_data = str_dup_safe (extra_data);
12258 + else
12259 + src->extra_data = NULL;
12260 + return 0;
12261 +}
12262 +
12263 +void pkg_src_deinit(pkg_src_t *src)
12264 +{
12265 + free (src->name);
12266 + free (src->value);
12267 + if (src->extra_data)
12268 + free (src->extra_data);
12269 +}
12270 +
12271 +
12272 --- /dev/null
12273 +++ b/archival/libipkg/pkg_src.h
12274 @@ -0,0 +1,34 @@
12275 +/* pkg_src.h - the itsy package management system
12276 +
12277 + Carl D. Worth
12278 +
12279 + Copyright (C) 2001 University of Southern California
12280 +
12281 + This program is free software; you can redistribute it and/or
12282 + modify it under the terms of the GNU General Public License as
12283 + published by the Free Software Foundation; either version 2, or (at
12284 + your option) any later version.
12285 +
12286 + This program is distributed in the hope that it will be useful, but
12287 + WITHOUT ANY WARRANTY; without even the implied warranty of
12288 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12289 + General Public License for more details.
12290 +*/
12291 +
12292 +#ifndef PKG_SRC_H
12293 +#define PKG_SRC_H
12294 +
12295 +#include "nv_pair.h"
12296 +
12297 +typedef struct
12298 +{
12299 + char *name;
12300 + char *value;
12301 + char *extra_data;
12302 + int gzip;
12303 +} pkg_src_t;
12304 +
12305 +int pkg_src_init(pkg_src_t *src, const char *name, const char *base_url, const char *extra_data, int gzip);
12306 +void pkg_src_deinit(pkg_src_t *src);
12307 +
12308 +#endif
12309 --- /dev/null
12310 +++ b/archival/libipkg/pkg_src_list.c
12311 @@ -0,0 +1,75 @@
12312 +/* pkg_src_list.c - the itsy package management system
12313 +
12314 + Carl D. Worth
12315 +
12316 + Copyright (C) 2001 University of Southern California
12317 +
12318 + This program is free software; you can redistribute it and/or
12319 + modify it under the terms of the GNU General Public License as
12320 + published by the Free Software Foundation; either version 2, or (at
12321 + your option) any later version.
12322 +
12323 + This program is distributed in the hope that it will be useful, but
12324 + WITHOUT ANY WARRANTY; without even the implied warranty of
12325 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12326 + General Public License for more details.
12327 +*/
12328 +
12329 +#include "ipkg.h"
12330 +
12331 +#include "pkg_src_list.h"
12332 +#include "void_list.h"
12333 +
12334 +int pkg_src_list_init(pkg_src_list_t *list)
12335 +{
12336 + return void_list_init((void_list_t *) list);
12337 +}
12338 +
12339 +void pkg_src_list_deinit(pkg_src_list_t *list)
12340 +{
12341 + pkg_src_list_elt_t *iter;
12342 + pkg_src_t *pkg_src;
12343 +
12344 + for (iter = list->head; iter; iter = iter->next) {
12345 + pkg_src = iter->data;
12346 + pkg_src_deinit(pkg_src);
12347 +
12348 + /* malloced in pkg_src_list_append */
12349 + free(pkg_src);
12350 + iter->data = NULL;
12351 + }
12352 + void_list_deinit((void_list_t *) list);
12353 +}
12354 +
12355 +pkg_src_t *pkg_src_list_append(pkg_src_list_t *list,
12356 + const char *name, const char *base_url, const char *extra_data,
12357 + int gzip)
12358 +{
12359 + int err;
12360 +
12361 + /* freed in pkg_src_list_deinit */
12362 + pkg_src_t *pkg_src = malloc(sizeof(pkg_src_t));
12363 +
12364 + if (pkg_src == NULL) {
12365 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12366 + return NULL;
12367 + }
12368 + pkg_src_init(pkg_src, name, base_url, extra_data, gzip);
12369 +
12370 + err = void_list_append((void_list_t *) list, pkg_src);
12371 + if (err) {
12372 + return NULL;
12373 + }
12374 +
12375 + return pkg_src;
12376 +}
12377 +
12378 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data)
12379 +{
12380 + return void_list_push((void_list_t *) list, data);
12381 +}
12382 +
12383 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list)
12384 +{
12385 + return (pkg_src_list_elt_t *) void_list_pop((void_list_t *) list);
12386 +}
12387 --- /dev/null
12388 +++ b/archival/libipkg/pkg_src_list.h
12389 @@ -0,0 +1,57 @@
12390 +/* pkg_src_list.h - the itsy package management system
12391 +
12392 + Carl D. Worth
12393 +
12394 + Copyright (C) 2001 University of Southern California
12395 +
12396 + This program is free software; you can redistribute it and/or
12397 + modify it under the terms of the GNU General Public License as
12398 + published by the Free Software Foundation; either version 2, or (at
12399 + your option) any later version.
12400 +
12401 + This program is distributed in the hope that it will be useful, but
12402 + WITHOUT ANY WARRANTY; without even the implied warranty of
12403 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12404 + General Public License for more details.
12405 +*/
12406 +
12407 +#ifndef PKG_SRC_LIST_H
12408 +#define PKG_SRC_LIST_H
12409 +
12410 +#include "pkg_src.h"
12411 +
12412 +typedef struct pkg_src_list_elt pkg_src_list_elt_t;
12413 +struct pkg_src_list_elt
12414 +{
12415 + pkg_src_list_elt_t *next;
12416 + pkg_src_t *data;
12417 +};
12418 +
12419 +typedef struct pkg_src_list pkg_src_list_t;
12420 +struct pkg_src_list
12421 +{
12422 + pkg_src_list_elt_t pre_head;
12423 + pkg_src_list_elt_t *head;
12424 + pkg_src_list_elt_t *tail;
12425 +};
12426 +
12427 +static inline int pkg_src_list_empty(pkg_src_list_t *list)
12428 +{
12429 + if (list->head == NULL)
12430 + return 1;
12431 + else
12432 + return 0;
12433 +}
12434 +
12435 +int pkg_src_list_elt_init(pkg_src_list_elt_t *elt, nv_pair_t *data);
12436 +void pkg_src_list_elt_deinit(pkg_src_list_elt_t *elt);
12437 +
12438 +int pkg_src_list_init(pkg_src_list_t *list);
12439 +void pkg_src_list_deinit(pkg_src_list_t *list);
12440 +
12441 +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);
12442 +int pkg_src_list_push(pkg_src_list_t *list, pkg_src_t *data);
12443 +pkg_src_list_elt_t *pkg_src_list_pop(pkg_src_list_t *list);
12444 +
12445 +#endif
12446 +
12447 --- /dev/null
12448 +++ b/archival/libipkg/pkg_vec.c
12449 @@ -0,0 +1,230 @@
12450 +/* pkg_vec.c - the itsy package management system
12451 +
12452 + Steven M. Ayer
12453 +
12454 + Copyright (C) 2002 Compaq Computer Corporation
12455 +
12456 + This program is free software; you can redistribute it and/or
12457 + modify it under the terms of the GNU General Public License as
12458 + published by the Free Software Foundation; either version 2, or (at
12459 + your option) any later version.
12460 +
12461 + This program is distributed in the hope that it will be useful, but
12462 + WITHOUT ANY WARRANTY; without even the implied warranty of
12463 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12464 + General Public License for more details.
12465 +*/
12466 +
12467 +#include <stdlib.h>
12468 +#include <fnmatch.h>
12469 +#include "xregex.h"
12470 +#include "ipkg.h"
12471 +#include "pkg.h"
12472 +
12473 +pkg_vec_t * pkg_vec_alloc(void)
12474 +{
12475 + pkg_vec_t * vec = (pkg_vec_t *)malloc(sizeof(pkg_vec_t));
12476 + if (!vec) {
12477 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12478 + return NULL;
12479 + }
12480 + vec->pkgs = NULL;
12481 + vec->len = 0;
12482 +
12483 + return vec;
12484 +}
12485 +
12486 +void pkg_vec_free(pkg_vec_t *vec)
12487 +{
12488 + free(vec->pkgs);
12489 + free(vec);
12490 +}
12491 +
12492 +/*
12493 + * assumption: all names in a vector are identical
12494 + * assumption: all version strings are trimmed,
12495 + * so identical versions have identical version strings,
12496 + * implying identical packages; let's marry these
12497 + */
12498 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status,ipkg_conf_t *conf)
12499 +{
12500 + int i;
12501 + int found = 0;
12502 +
12503 + /* look for a duplicate pkg by name, version, and architecture */
12504 + for (i = 0; i < vec->len; i++){
12505 + ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found pkg=%s version=%s arch=%s cmp=%s version=%s arch=%s \n",
12506 + __FUNCTION__, pkg->name, pkg->version, pkg->architecture,
12507 + vec->pkgs[i]->name, vec->pkgs[i]->version,vec->pkgs[i]->architecture );
12508 + if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12509 + && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12510 + && (strcmp(pkg->architecture, vec->pkgs[i]->architecture) == 0)) {
12511 + found = 1;
12512 + ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Found duplicate for pkg=%s version=%s arch=%s\n",
12513 + __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12514 + break;
12515 + }
12516 + }
12517 +
12518 + /* we didn't find one, add it */
12519 + if (!found){
12520 + ipkg_message(conf, IPKG_DEBUG2, "Function: %s. Adding new pkg=%s version=%s arch=%s\n",
12521 + __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12522 +
12523 + vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12524 + vec->pkgs[vec->len] = pkg;
12525 + vec->len++;
12526 + return pkg;
12527 + }
12528 + /* update the one that we have */
12529 + else {
12530 + ipkg_message(conf, IPKG_DEBUG2, "Function: %s. calling pkg_merge for pkg=%s version=%s arch=%s",
12531 + __FUNCTION__, pkg->name, pkg->version, pkg->architecture);
12532 + if (set_status) {
12533 + /* this is from the status file, so need to merge with existing database */
12534 + ipkg_message(conf, IPKG_DEBUG2, " with set_status\n");
12535 + pkg_merge(vec->pkgs[i], pkg, set_status);
12536 + /* XXX: CLEANUP: It's not so polite to free something here
12537 + that was passed in from above. */
12538 + pkg_deinit(pkg);
12539 + free(pkg);
12540 + } else {
12541 + ipkg_message(conf, IPKG_DEBUG2, " WITHOUT set_status\n");
12542 + /* just overwrite the old one */
12543 + pkg_deinit(vec->pkgs[i]);
12544 + free(vec->pkgs[i]);
12545 + vec->pkgs[i] = pkg;
12546 + }
12547 + return vec->pkgs[i];
12548 + }
12549 +}
12550 +
12551 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg)
12552 +{
12553 + int i;
12554 + int found = 0;
12555 +
12556 + /* look for a duplicate pkg by name, version, and architecture */
12557 + for (i = 0; i < vec->len; i++)
12558 + if ((strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12559 + && (pkg_compare_versions(pkg, vec->pkgs[i]) == 0)
12560 + && (strcmp(pkg->architecture, vec->pkgs[i]->name) == 0)) {
12561 + found = 1;
12562 + break;
12563 + }
12564 +
12565 + /* we didn't find one, add it */
12566 + if(!found){
12567 + vec->pkgs = (pkg_t **)realloc(vec->pkgs, (vec->len + 1) * sizeof(pkg_t *));
12568 + *(const pkg_t **)&vec->pkgs[vec->len] = pkg;
12569 + vec->len++;
12570 + }
12571 +}
12572 +
12573 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg)
12574 +{
12575 + int i;
12576 + for (i = 0; i < vec->len; i++)
12577 + if (vec->pkgs[i] == apkg)
12578 + return 1;
12579 + return 0;
12580 +}
12581 +
12582 +typedef int (*compare_fcn_t)(const void *, const void *);
12583 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *))
12584 +{
12585 + qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12586 +}
12587 +
12588 +int pkg_vec_clear_marks(pkg_vec_t *vec)
12589 +{
12590 + int npkgs = vec->len;
12591 + int i;
12592 + for (i = 0; i < npkgs; i++) {
12593 + pkg_t *pkg = vec->pkgs[i];
12594 + pkg->state_flag &= ~SF_MARKED;
12595 + }
12596 + return 0;
12597 +}
12598 +
12599 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern)
12600 +{
12601 + int matching_count = 0;
12602 + pkg_t **pkgs = vec->pkgs;
12603 + int npkgs = vec->len;
12604 + int i;
12605 + for (i = 0; i < npkgs; i++) {
12606 + pkg_t *pkg = pkgs[i];
12607 + if (fnmatch(pattern, pkg->name, 0)==0) {
12608 + pkg->state_flag |= SF_MARKED;
12609 + matching_count++;
12610 + }
12611 + }
12612 + return matching_count;
12613 +}
12614 +
12615 +
12616 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void)
12617 +{
12618 + abstract_pkg_vec_t * vec ;
12619 + vec = (abstract_pkg_vec_t *)malloc(sizeof(abstract_pkg_vec_t));
12620 + if (!vec) {
12621 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
12622 + return NULL;
12623 + }
12624 + vec->pkgs = NULL;
12625 + vec->len = 0;
12626 +
12627 + return vec;
12628 +}
12629 +
12630 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec)
12631 +{
12632 + free(vec->pkgs);
12633 + free(vec);
12634 +}
12635 +
12636 +/*
12637 + * assumption: all names in a vector are unique
12638 + */
12639 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg)
12640 +{
12641 + int i;
12642 +
12643 + /* look for a duplicate pkg by name */
12644 + for(i = 0; i < vec->len; i++)
12645 + if (strcmp(pkg->name, vec->pkgs[i]->name) == 0)
12646 + break;
12647 +
12648 + /* we didn't find one, add it */
12649 + if(i == vec->len){
12650 + vec->pkgs =
12651 + (abstract_pkg_t **)
12652 + realloc(vec->pkgs, (vec->len + 1) * sizeof(abstract_pkg_t *));
12653 + vec->pkgs[vec->len] = pkg;
12654 + vec->len++;
12655 + }
12656 +}
12657 +
12658 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i)
12659 +{
12660 + if (vec->len > i)
12661 + return vec->pkgs[i];
12662 + else
12663 + return NULL;
12664 +}
12665 +
12666 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg)
12667 +{
12668 + int i;
12669 + for (i = 0; i < vec->len; i++)
12670 + if (vec->pkgs[i] == apkg)
12671 + return 1;
12672 + return 0;
12673 +}
12674 +
12675 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *))
12676 +{
12677 + qsort(vec->pkgs, vec->len, sizeof(pkg_t *), (compare_fcn_t)compar);
12678 +}
12679 +
12680 --- /dev/null
12681 +++ b/archival/libipkg/pkg_vec.h
12682 @@ -0,0 +1,64 @@
12683 +/* pkg_vec.h - the itsy package management system
12684 +
12685 + Steven M. Ayer
12686 +
12687 + Copyright (C) 2002 Compaq Computer Corporation
12688 +
12689 + This program is free software; you can redistribute it and/or
12690 + modify it under the terms of the GNU General Public License as
12691 + published by the Free Software Foundation; either version 2, or (at
12692 + your option) any later version.
12693 +
12694 + This program is distributed in the hope that it will be useful, but
12695 + WITHOUT ANY WARRANTY; without even the implied warranty of
12696 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12697 + General Public License for more details.
12698 +*/
12699 +
12700 +#ifndef PKG_VEC_H
12701 +#define PKG_VEC_H
12702 +
12703 +typedef struct pkg pkg_t;
12704 +typedef struct abstract_pkg abstract_pkg_t;
12705 +
12706 +struct pkg_vec
12707 +{
12708 + pkg_t **pkgs;
12709 + int len;
12710 +};
12711 +typedef struct pkg_vec pkg_vec_t;
12712 +
12713 +struct abstract_pkg_vec
12714 +{
12715 + abstract_pkg_t **pkgs;
12716 + int len;
12717 +};
12718 +typedef struct abstract_pkg_vec abstract_pkg_vec_t;
12719 +
12720 +typedef int (*pkg_compar_t)(pkg_t *, pkg_t *);
12721 +typedef int (*abstract_pkg_compar_t)(abstract_pkg_t *, abstract_pkg_t *);
12722 +
12723 +pkg_vec_t * pkg_vec_alloc(void);
12724 +void pkg_vec_free(pkg_vec_t *vec);
12725 +void marry_two_packages(pkg_t * newpkg, pkg_t * oldpkg);
12726 +
12727 +void pkg_vec_add(pkg_vec_t *vec, pkg_t *pkg);
12728 +/* pkg_vec_insert_merge: might munge pkg.
12729 +* returns the pkg that is in the pkg graph */
12730 +pkg_t *pkg_vec_insert_merge(pkg_vec_t *vec, pkg_t *pkg, int set_status, ipkg_conf_t *conf);
12731 +/* this one never munges pkg */
12732 +void pkg_vec_insert(pkg_vec_t *vec, const pkg_t *pkg);
12733 +int pkg_vec_contains(pkg_vec_t *vec, pkg_t *apkg);
12734 +void pkg_vec_sort(pkg_vec_t *vec, int (*compar)(pkg_t *, pkg_t *));
12735 +
12736 +int pkg_vec_clear_marks(pkg_vec_t *vec);
12737 +int pkg_vec_mark_if_matches(pkg_vec_t *vec, const char *pattern);
12738 +
12739 +abstract_pkg_vec_t * abstract_pkg_vec_alloc(void);
12740 +void abstract_pkg_vec_free(abstract_pkg_vec_t *vec);
12741 +void abstract_pkg_vec_insert(abstract_pkg_vec_t *vec, abstract_pkg_t *pkg);
12742 +abstract_pkg_t * abstract_pkg_vec_get(abstract_pkg_vec_t *vec, int i);
12743 +int abstract_pkg_vec_contains(abstract_pkg_vec_t *vec, abstract_pkg_t *apkg);
12744 +void abstract_pkg_vec_sort(pkg_vec_t *vec, int (*compar)(abstract_pkg_t *, abstract_pkg_t *));
12745 +#endif
12746 +
12747 --- /dev/null
12748 +++ b/archival/libipkg/sprintf_alloc.h
12749 @@ -0,0 +1,25 @@
12750 +/* sprintf_alloca.c -- like sprintf with memory allocation
12751 +
12752 + Carl D. Worth
12753 +
12754 + Copyright (C) 2001 University of Southern California
12755 +
12756 + This program is free software; you can redistribute it and/or modify
12757 + it under the terms of the GNU General Public License as published by
12758 + the Free Software Foundation; either version 2, or (at your option)
12759 + any later version.
12760 +
12761 + This program is distributed in the hope that it will be useful,
12762 + but WITHOUT ANY WARRANTY; without even the implied warranty of
12763 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12764 + GNU General Public License for more details.
12765 +*/
12766 +
12767 +#ifndef SPRINTF_ALLOC_H
12768 +#define SPRINTF_ALLOC_H
12769 +
12770 +#include "libbb.h"
12771 +
12772 +#define sprintf_alloc(str, fmt, args...) *str = xasprintf(fmt, ## args)
12773 +
12774 +#endif
12775 --- /dev/null
12776 +++ b/archival/libipkg/str_list.c
12777 @@ -0,0 +1,76 @@
12778 +/* str_list.c - the itsy package management system
12779 +
12780 + Carl D. Worth
12781 +
12782 + Copyright (C) 2001 University of Southern California
12783 +
12784 + This program is free software; you can redistribute it and/or
12785 + modify it under the terms of the GNU General Public License as
12786 + published by the Free Software Foundation; either version 2, or (at
12787 + your option) any later version.
12788 +
12789 + This program is distributed in the hope that it will be useful, but
12790 + WITHOUT ANY WARRANTY; without even the implied warranty of
12791 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12792 + General Public License for more details.
12793 +*/
12794 +
12795 +#include "ipkg.h"
12796 +
12797 +#include "str_list.h"
12798 +
12799 +int str_list_elt_init(str_list_elt_t *elt, char *data)
12800 +{
12801 + return void_list_elt_init((void_list_elt_t *) elt, data);
12802 +}
12803 +
12804 +void str_list_elt_deinit(str_list_elt_t *elt)
12805 +{
12806 + void_list_elt_deinit((void_list_elt_t *) elt);
12807 +}
12808 +
12809 +str_list_t *str_list_alloc()
12810 +{
12811 + str_list_t *list = (str_list_t *)malloc(sizeof(str_list_t));
12812 + if (list)
12813 + str_list_init(list);
12814 + return list;
12815 +}
12816 +
12817 +int str_list_init(str_list_t *list)
12818 +{
12819 + return void_list_init((void_list_t *) list);
12820 +}
12821 +
12822 +void str_list_deinit(str_list_t *list)
12823 +{
12824 + void_list_deinit((void_list_t *) list);
12825 +}
12826 +
12827 +int str_list_append(str_list_t *list, char *data)
12828 +{
12829 + return void_list_append((void_list_t *) list, data);
12830 +}
12831 +
12832 +int str_list_push(str_list_t *list, char *data)
12833 +{
12834 + return void_list_push((void_list_t *) list, data);
12835 +}
12836 +
12837 +str_list_elt_t *str_list_pop(str_list_t *list)
12838 +{
12839 + return (str_list_elt_t *) void_list_pop((void_list_t *) list);
12840 +}
12841 +
12842 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter)
12843 +{
12844 + return (str_list_elt_t *) void_list_remove((void_list_t *) list,
12845 + (void_list_elt_t **) iter);
12846 +}
12847 +
12848 +char *str_list_remove_elt(str_list_t *list, const char *target_str)
12849 +{
12850 + return (char *)void_list_remove_elt((void_list_t *) list,
12851 + (void *)target_str,
12852 + (void_list_cmp_t)strcmp);
12853 +}
12854 --- /dev/null
12855 +++ b/archival/libipkg/str_list.h
12856 @@ -0,0 +1,51 @@
12857 +/* str_list.h - the itsy package management system
12858 +
12859 + Carl D. Worth
12860 +
12861 + Copyright (C) 2001 University of Southern California
12862 +
12863 + This program is free software; you can redistribute it and/or
12864 + modify it under the terms of the GNU General Public License as
12865 + published by the Free Software Foundation; either version 2, or (at
12866 + your option) any later version.
12867 +
12868 + This program is distributed in the hope that it will be useful, but
12869 + WITHOUT ANY WARRANTY; without even the implied warranty of
12870 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12871 + General Public License for more details.
12872 +*/
12873 +
12874 +#ifndef STR_LIST_H
12875 +#define STR_LIST_H
12876 +
12877 +#include "void_list.h"
12878 +
12879 +typedef struct str_list_elt str_list_elt_t;
12880 +struct str_list_elt
12881 +{
12882 + str_list_elt_t *next;
12883 + char *data;
12884 +};
12885 +
12886 +typedef struct xstr_list str_list_t;
12887 +struct xstr_list
12888 +{
12889 + str_list_elt_t pre_head;
12890 + str_list_elt_t *head;
12891 + str_list_elt_t *tail;
12892 +};
12893 +
12894 +int str_list_elt_init(str_list_elt_t *elt, char *data);
12895 +void str_list_elt_deinit(str_list_elt_t *elt);
12896 +
12897 +str_list_t *str_list_alloc(void);
12898 +int str_list_init(str_list_t *list);
12899 +void str_list_deinit(str_list_t *list);
12900 +
12901 +int str_list_append(str_list_t *list, char *data);
12902 +int str_list_push(str_list_t *list, char *data);
12903 +str_list_elt_t *str_list_pop(str_list_t *list);
12904 +str_list_elt_t *str_list_remove(str_list_t *list, str_list_elt_t **iter);
12905 +char *str_list_remove_elt(str_list_t *list, const char *target_str);
12906 +
12907 +#endif
12908 --- /dev/null
12909 +++ b/archival/libipkg/str_util.c
12910 @@ -0,0 +1,63 @@
12911 +/* str_utils.c - the itsy package management system
12912 +
12913 + Carl D. Worth
12914 +
12915 + Copyright (C) 2001 University of Southern California
12916 +
12917 + This program is free software; you can redistribute it and/or
12918 + modify it under the terms of the GNU General Public License as
12919 + published by the Free Software Foundation; either version 2, or (at
12920 + your option) any later version.
12921 +
12922 + This program is distributed in the hope that it will be useful, but
12923 + WITHOUT ANY WARRANTY; without even the implied warranty of
12924 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12925 + General Public License for more details.
12926 +*/
12927 +
12928 +#include "ipkg.h"
12929 +
12930 +int str_starts_with(const char *str, const char *prefix)
12931 +{
12932 + return (strncmp(str, prefix, strlen(prefix)) == 0);
12933 +}
12934 +
12935 +int str_ends_with(const char *str, const char *suffix)
12936 +{
12937 + int suffix_len;
12938 + int str_len;
12939 +
12940 + str_len = strlen(str);
12941 + suffix_len = strlen(suffix);
12942 +
12943 + if (str_len < suffix_len) {
12944 + return 0;
12945 + }
12946 +
12947 + return (strcmp(str + str_len - suffix_len, suffix) == 0);
12948 +}
12949 +
12950 +int str_chomp(char *str)
12951 +{
12952 + if (str[strlen(str) - 1] == '\n') {
12953 + str[strlen(str) - 1] = '\0';
12954 + return 1;
12955 + }
12956 + return 0;
12957 +}
12958 +
12959 +int str_toupper(char *str)
12960 +{
12961 + while (*str) {
12962 + *str = toupper(*str);
12963 + str++;
12964 + }
12965 +
12966 + return 0;
12967 +}
12968 +
12969 +char *str_dup_safe(const char *str)
12970 +{
12971 + return str ? strdup(str) : NULL;
12972 +}
12973 +
12974 --- /dev/null
12975 +++ b/archival/libipkg/str_util.h
12976 @@ -0,0 +1,27 @@
12977 +/* str_utils.h - the itsy package management system
12978 +
12979 + Carl D. Worth
12980 +
12981 + Copyright (C) 2001 University of Southern California
12982 +
12983 + This program is free software; you can redistribute it and/or
12984 + modify it under the terms of the GNU General Public License as
12985 + published by the Free Software Foundation; either version 2, or (at
12986 + your option) any later version.
12987 +
12988 + This program is distributed in the hope that it will be useful, but
12989 + WITHOUT ANY WARRANTY; without even the implied warranty of
12990 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12991 + General Public License for more details.
12992 +*/
12993 +
12994 +#ifndef STR_UTILS_H
12995 +#define STR_UTILS_H
12996 +
12997 +int str_starts_with(const char *str, const char *prefix);
12998 +int str_ends_with(const char *str, const char *suffix);
12999 +int str_chomp(char *str);
13000 +int str_toupper(char *str);
13001 +char *str_dup_safe(const char *str);
13002 +
13003 +#endif
13004 --- /dev/null
13005 +++ b/archival/libipkg/user.c
13006 @@ -0,0 +1,58 @@
13007 +/* user.c - the itsy package management system
13008 +
13009 + Jamey Hicks
13010 +
13011 + Copyright (C) 2002 Hewlett Packard Company
13012 + Copyright (C) 2001 University of Southern California
13013 +
13014 + This program is free software; you can redistribute it and/or
13015 + modify it under the terms of the GNU General Public License as
13016 + published by the Free Software Foundation; either version 2, or (at
13017 + your option) any later version.
13018 +
13019 + This program is distributed in the hope that it will be useful, but
13020 + WITHOUT ANY WARRANTY; without even the implied warranty of
13021 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13022 + General Public License for more details.
13023 +*/
13024 +
13025 +#include <stdio.h>
13026 +#include <stdarg.h>
13027 +#include "file_util.h"
13028 +#include "str_util.h"
13029 +#ifdef IPKG_LIB
13030 +#include "libipkg.h"
13031 +#endif
13032 +
13033 +
13034 +#ifdef IPKG_LIB
13035 +static char *question = NULL;
13036 +static int question_len = 255;
13037 +#endif
13038 +char *get_user_response(const char *format, ...)
13039 +{
13040 + int len = question_len;
13041 + va_list ap;
13042 + char *response;
13043 + va_start(ap, format);
13044 +
13045 +#ifndef IPKG_LIB
13046 + vprintf(format, ap);
13047 + do {
13048 + response = file_read_line_alloc(stdin);
13049 + } while (response == NULL);
13050 +#else
13051 + do {
13052 + if (question == NULL || len > question_len) {
13053 + question = realloc(question, len + 1);
13054 + question_len = len;
13055 + }
13056 + len = vsnprintf(question,question_len,format,ap);
13057 + } while (len > question_len);
13058 + response = strdup(ipkg_cb_response(question));
13059 +#endif
13060 + str_chomp(response);
13061 + str_tolower(response);
13062 +
13063 + return response;
13064 +}
13065 --- /dev/null
13066 +++ b/archival/libipkg/user.h
13067 @@ -0,0 +1,23 @@
13068 +/* user.c - the itsy package management system
13069 +
13070 + Jamey Hicks
13071 +
13072 + Copyright (C) 2002 Hewlett Packard Company
13073 + Copyright (C) 2001 University of Southern California
13074 +
13075 + This program is free software; you can redistribute it and/or
13076 + modify it under the terms of the GNU General Public License as
13077 + published by the Free Software Foundation; either version 2, or (at
13078 + your option) any later version.
13079 +
13080 + This program is distributed in the hope that it will be useful, but
13081 + WITHOUT ANY WARRANTY; without even the implied warranty of
13082 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13083 + General Public License for more details.
13084 +*/
13085 +
13086 +#include <stdio.h>
13087 +#include <stdarg.h>
13088 +
13089 +char *get_user_response(const char *format, ...);
13090 +
13091 --- /dev/null
13092 +++ b/archival/libipkg/void_list.c
13093 @@ -0,0 +1,194 @@
13094 +/* void_list.c - the itsy package management system
13095 +
13096 + Carl D. Worth
13097 +
13098 + Copyright (C) 2001 University of Southern California
13099 +
13100 + This program is free software; you can redistribute it and/or
13101 + modify it under the terms of the GNU General Public License as
13102 + published by the Free Software Foundation; either version 2, or (at
13103 + your option) any later version.
13104 +
13105 + This program is distributed in the hope that it will be useful, but
13106 + WITHOUT ANY WARRANTY; without even the implied warranty of
13107 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13108 + General Public License for more details.
13109 +*/
13110 +
13111 +#include "ipkg.h"
13112 +#include <errno.h>
13113 +
13114 +#include "void_list.h"
13115 +
13116 +int void_list_elt_init(void_list_elt_t *elt, void *data)
13117 +{
13118 + elt->next = NULL;
13119 + elt->data = data;
13120 +
13121 + return 0;
13122 +}
13123 +
13124 +void void_list_elt_deinit(void_list_elt_t *elt)
13125 +{
13126 + void_list_elt_init(elt, NULL);
13127 +}
13128 +
13129 +int void_list_init(void_list_t *list)
13130 +{
13131 + void_list_elt_init(&list->pre_head, NULL);
13132 + list->head = NULL;
13133 + list->pre_head.next = list->head;
13134 + list->tail = NULL;
13135 +
13136 + return 0;
13137 +}
13138 +
13139 +void void_list_deinit(void_list_t *list)
13140 +{
13141 + void_list_elt_t *elt;
13142 +
13143 + while (list->head) {
13144 + elt = void_list_pop(list);
13145 + void_list_elt_deinit(elt);
13146 + /* malloced in void_list_append */
13147 + free(elt);
13148 + }
13149 +}
13150 +
13151 +int void_list_append(void_list_t *list, void *data)
13152 +{
13153 + void_list_elt_t *elt;
13154 +
13155 + /* freed in void_list_deinit */
13156 + elt = malloc(sizeof(void_list_elt_t));
13157 + if (elt == NULL) {
13158 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13159 + return ENOMEM;
13160 + }
13161 +
13162 + void_list_elt_init(elt, data);
13163 +
13164 + if (list->tail) {
13165 + list->tail->next = elt;
13166 + list->tail = elt;
13167 + } else {
13168 + list->head = elt;
13169 + list->pre_head.next = list->head;
13170 + list->tail = elt;
13171 + }
13172 +
13173 + return 0;
13174 +}
13175 +
13176 +int void_list_push(void_list_t *list, void *data)
13177 +{
13178 + void_list_elt_t *elt;
13179 +
13180 + elt = malloc(sizeof(void_list_elt_t));
13181 + if (elt == NULL) {
13182 + fprintf(stderr, "%s: out of memory\n", __FUNCTION__);
13183 + return ENOMEM;
13184 + }
13185 +
13186 + void_list_elt_init(elt, data);
13187 +
13188 + elt->next = list->head;
13189 + list->head->next = elt;
13190 + if (list->tail == NULL) {
13191 + list->tail = list->head;
13192 + }
13193 +
13194 + return 0;
13195 +}
13196 +
13197 +void_list_elt_t *void_list_pop(void_list_t *list)
13198 +{
13199 + void_list_elt_t *elt;
13200 +
13201 + elt = list->head;
13202 +
13203 + if (list->head) {
13204 + list->head = list->head->next;
13205 + list->pre_head.next = list->head;
13206 + if (list->head == NULL) {
13207 + list->tail = NULL;
13208 + }
13209 + }
13210 +
13211 + return elt;
13212 +}
13213 +
13214 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
13215 +{
13216 + void_list_elt_t *prior;
13217 + void_list_elt_t *old_elt;
13218 + void *old_data;
13219 +
13220 + old_elt = *iter;
13221 + old_data = old_elt->data;
13222 +
13223 + if (old_elt == list->head) {
13224 + prior = &list->pre_head;
13225 + void_list_pop(list);
13226 + } else {
13227 + for (prior = list->head; prior; prior = prior->next) {
13228 + if (prior->next == old_elt) {
13229 + break;
13230 + }
13231 + }
13232 + if (prior == NULL || prior->next != old_elt) {
13233 + fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
13234 + return NULL;
13235 + }
13236 + prior->next = old_elt->next;
13237 +
13238 + if (old_elt == list->tail) {
13239 + list->tail = prior;
13240 + }
13241 + }
13242 +
13243 + void_list_elt_deinit(old_elt);
13244 + *iter = prior;
13245 +
13246 + return old_data;
13247 +}
13248 +
13249 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13250 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
13251 +{
13252 + void_list_elt_t *prior;
13253 + void_list_elt_t *old_elt = NULL;
13254 + void *old_data = NULL;
13255 +
13256 + /* first element */
13257 + if (list->head && list->head->data && (cmp(list->head->data, target_data) == 0)) {
13258 + old_elt = list->head;
13259 + old_data = list->head->data;
13260 + void_list_pop(list);
13261 + } else {
13262 + int found = 0;
13263 + for (prior = list->head; prior && prior->next; prior = prior->next) {
13264 + if (prior->next->data && (cmp(prior->next->data, target_data) == 0)) {
13265 + old_elt = prior->next;
13266 + old_data = old_elt->data;
13267 + found = 1;
13268 + break;
13269 + }
13270 + }
13271 + if (!found) {
13272 + return NULL;
13273 + }
13274 + prior->next = old_elt->next;
13275 +
13276 + if (old_elt == list->tail) {
13277 + list->tail = prior;
13278 + }
13279 + }
13280 + if (old_elt)
13281 + void_list_elt_deinit(old_elt);
13282 +
13283 + if (old_data)
13284 + return old_data;
13285 + else
13286 + return NULL;
13287 +}
13288 --- /dev/null
13289 +++ b/archival/libipkg/void_list.h
13290 @@ -0,0 +1,59 @@
13291 +/* void_list.h - the itsy package management system
13292 +
13293 + Carl D. Worth
13294 +
13295 + Copyright (C) 2001 University of Southern California
13296 +
13297 + This program is free software; you can redistribute it and/or
13298 + modify it under the terms of the GNU General Public License as
13299 + published by the Free Software Foundation; either version 2, or (at
13300 + your option) any later version.
13301 +
13302 + This program is distributed in the hope that it will be useful, but
13303 + WITHOUT ANY WARRANTY; without even the implied warranty of
13304 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13305 + General Public License for more details.
13306 +*/
13307 +
13308 +#ifndef VOID_LIST_H
13309 +#define VOID_LIST_H
13310 +
13311 +typedef struct void_list_elt void_list_elt_t;
13312 +struct void_list_elt
13313 +{
13314 + void_list_elt_t *next;
13315 + void *data;
13316 +};
13317 +
13318 +typedef struct void_list void_list_t;
13319 +struct void_list
13320 +{
13321 + void_list_elt_t pre_head;
13322 + void_list_elt_t *head;
13323 + void_list_elt_t *tail;
13324 +};
13325 +
13326 +static inline int void_list_empty(void_list_t *list)
13327 +{
13328 + if (list->head == NULL)
13329 + return 1;
13330 + else
13331 + return 0;
13332 +}
13333 +
13334 +int void_list_elt_init(void_list_elt_t *elt, void *data);
13335 +void void_list_elt_deinit(void_list_elt_t *elt);
13336 +
13337 +int void_list_init(void_list_t *list);
13338 +void void_list_deinit(void_list_t *list);
13339 +
13340 +int void_list_append(void_list_t *list, void *data);
13341 +int void_list_push(void_list_t *list, void *data);
13342 +void_list_elt_t *void_list_pop(void_list_t *list);
13343 +
13344 +void *void_list_remove(void_list_t *list, void_list_elt_t **iter);
13345 +/* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
13346 +typedef int (*void_list_cmp_t)(const void *, const void *);
13347 +void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp);
13348 +
13349 +#endif
13350 --- /dev/null
13351 +++ b/archival/libipkg/xsystem.c
13352 @@ -0,0 +1,64 @@
13353 +/* xsystem.c - system(3) with error messages
13354 +
13355 + Carl D. Worth
13356 +
13357 + Copyright (C) 2001 University of Southern California
13358 +
13359 + This program is free software; you can redistribute it and/or
13360 + modify it under the terms of the GNU General Public License as
13361 + published by the Free Software Foundation; either version 2, or (at
13362 + your option) any later version.
13363 +
13364 + This program is distributed in the hope that it will be useful, but
13365 + WITHOUT ANY WARRANTY; without even the implied warranty of
13366 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13367 + General Public License for more details.
13368 +*/
13369 +
13370 +#include "ipkg.h"
13371 +#include <sys/wait.h>
13372 +
13373 +#include "xsystem.h"
13374 +
13375 +/* XXX: FEATURE: I shouldn't actually use system(3) at all. I don't
13376 + really need the /bin/sh invocation which takes resources and
13377 + introduces security problems. I should switch all of this to a sort
13378 + of execl() or execv() interface/implementation.
13379 +*/
13380 +
13381 +/* Like system(3), but with error messages printed if the fork fails
13382 + or if the child process dies due to an uncaught signal. Also, the
13383 + return value is a bit simpler:
13384 +
13385 + -1 if there was any problem
13386 + Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13387 + as defined in <sys/wait.h>.
13388 +*/
13389 +int xsystem(const char *cmd)
13390 +{
13391 + int err;
13392 +
13393 + err = system(cmd);
13394 +
13395 + if (err == -1) {
13396 + fprintf(stderr, "%s: ERROR: fork failed before execution: `%s'\n",
13397 + __FUNCTION__, cmd);
13398 + return -1;
13399 + }
13400 +
13401 + if (WIFSIGNALED(err)) {
13402 + fprintf(stderr, "%s: ERROR: Child process died due to signal %d: `%s'\n",
13403 + __FUNCTION__, WTERMSIG(err), cmd);
13404 + return -1;
13405 + }
13406 +
13407 + if (WIFEXITED(err)) {
13408 + /* Normal child exit */
13409 + return WEXITSTATUS(err);
13410 + }
13411 +
13412 + fprintf(stderr, "%s: ERROR: Received unintelligible return value from system: %d",
13413 + __FUNCTION__, err);
13414 + return -1;
13415 +}
13416 +
13417 --- /dev/null
13418 +++ b/archival/libipkg/xsystem.h
13419 @@ -0,0 +1,34 @@
13420 +/* xsystem.h - system(3) with error messages
13421 +
13422 + Carl D. Worth
13423 +
13424 + Copyright (C) 2001 University of Southern California
13425 +
13426 + This program is free software; you can redistribute it and/or
13427 + modify it under the terms of the GNU General Public License as
13428 + published by the Free Software Foundation; either version 2, or (at
13429 + your option) any later version.
13430 +
13431 + This program is distributed in the hope that it will be useful, but
13432 + WITHOUT ANY WARRANTY; without even the implied warranty of
13433 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13434 + General Public License for more details.
13435 +*/
13436 +
13437 +#ifndef XSYSTEM_H
13438 +#define XSYSTEM_H
13439 +
13440 +#include <stdlib.h>
13441 +
13442 +/* Like system(3), but with error messages printed if the fork fails
13443 + or if the child process dies due to an uncaught signal. Also, the
13444 + return value is a bit simpler:
13445 +
13446 + -1 if there was any problem
13447 + Otherwise, the 8-bit return value of the program ala WEXITSTATUS
13448 + as defined in <sys/wait.h>.
13449 +*/
13450 +int xsystem(const char *cmd);
13451 +
13452 +#endif
13453 +
13454 --- a/archival/libunarchive/data_extract_all.c
13455 +++ b/archival/libunarchive/data_extract_all.c
13456 @@ -144,3 +144,17 @@
13457 }
13458 }
13459 }
13460 +
13461 +extern void data_extract_all_prefix(archive_handle_t *archive_handle)
13462 +{
13463 + char *name_ptr = archive_handle->file_header->name;
13464 +
13465 + name_ptr += strspn(name_ptr, "./");
13466 + if (name_ptr[0] != '\0') {
13467 + archive_handle->file_header->name = xmalloc(strlen(archive_handle->buffer) + 1 + strlen(name_ptr) + 1);
13468 + strcpy(archive_handle->file_header->name, archive_handle->buffer);
13469 + strcat(archive_handle->file_header->name, name_ptr);
13470 + data_extract_all(archive_handle);
13471 + }
13472 +}
13473 +
13474 --- a/archival/libunarchive/Kbuild
13475 +++ b/archival/libunarchive/Kbuild
13476 @@ -54,6 +54,7 @@
13477 lib-$(CONFIG_FEATURE_DEB_TAR_LZMA) += decompress_unlzma.o get_header_tar_lzma.o
13478 lib-$(CONFIG_GUNZIP) += decompress_unzip.o
13479 lib-$(CONFIG_FEATURE_GUNZIP_UNCOMPRESS) += decompress_uncompress.o
13480 +lib-$(CONFIG_IPKG) += $(GUNZIP_FILES) get_header_tar.o get_header_tar_gz.o
13481 lib-$(CONFIG_RPM2CPIO) += decompress_unzip.o get_header_cpio.o
13482 lib-$(CONFIG_RPM) += decompress_unzip.o get_header_cpio.o
13483 lib-$(CONFIG_FEATURE_RPM_BZ2) += decompress_bunzip2.o
13484 --- a/include/applets.h
13485 +++ b/include/applets.h
13486 @@ -200,6 +200,7 @@
13487 USE_IPCALC(APPLET(ipcalc, _BB_DIR_BIN, _BB_SUID_NEVER))
13488 USE_IPCRM(APPLET(ipcrm, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13489 USE_IPCS(APPLET(ipcs, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS))
13490 +USE_IPKG(APPLET(ipkg, _BB_DIR_USR_BIN, _BB_SUID_NEVER))
13491 USE_IPLINK(APPLET(iplink, _BB_DIR_BIN, _BB_SUID_NEVER))
13492 USE_IPROUTE(APPLET(iproute, _BB_DIR_BIN, _BB_SUID_NEVER))
13493 USE_IPRULE(APPLET(iprule, _BB_DIR_BIN, _BB_SUID_NEVER))
13494 --- a/include/unarchive.h
13495 +++ b/include/unarchive.h
13496 @@ -82,6 +82,7 @@
13497
13498 extern void data_skip(archive_handle_t *archive_handle);
13499 extern void data_extract_all(archive_handle_t *archive_handle);
13500 +extern void data_extract_all_prefix(archive_handle_t *archive_handle);
13501 extern void data_extract_to_stdout(archive_handle_t *archive_handle);
13502 extern void data_extract_to_buffer(archive_handle_t *archive_handle);
13503
13504 --- a/include/usage.h
13505 +++ b/include/usage.h
13506 @@ -1425,6 +1425,82 @@
13507 "$ ls -la /tmp/busybox*\n" \
13508 "-rw-rw-r-- 1 andersen andersen 554058 Apr 14 17:49 /tmp/busybox.tar.gz\n"
13509
13510 +#define ipkg_trivial_usage \
13511 + "[options]... sub-command [arguments]..."
13512 +#define ipkg_full_usage \
13513 + "ipkg is an utility to install, remove and manage .ipk packages.\n" \
13514 + "\n" \
13515 + "Sub-commands:\n" \
13516 + "\nPackage Manipulation:\n" \
13517 + "\tupdate Update list of available packages\n" \
13518 + "\tupgrade Upgrade all installed packages to latest version\n" \
13519 + "\tinstall <pkg> Download and install <pkg> (and dependencies)\n" \
13520 + "\tinstall <file.ipk> Install package <file.ipk>\n" \
13521 + "\tconfigure [<pkg>] Configure unpacked packages\n" \
13522 + "\tremove <pkg|regexp> Remove package <pkg|packages following regexp>\n" \
13523 + "\tflag <flag> <pkg> ... Flag package(s) <pkg>\n" \
13524 + "\t <flag>=hold|noprune|user|ok|installed|unpacked (one per invocation) \n" \
13525 + "\n" \
13526 + "Informational Commands:\n" \
13527 + "\tlist List available packages and descriptions\n" \
13528 + "\tlist_installed List all and only the installed packages and description \n" \
13529 + "\tfiles <pkg> List all files belonging to <pkg>\n" \
13530 + "\tsearch <file|regexp> Search for a package providing <file>\n" \
13531 + "\tinfo [pkg|regexp [<field>]] Display all/some info fields for <pkg> or all\n" \
13532 + "\tstatus [pkg|regexp [<field>]] Display all/some status fields for <pkg> or all\n" \
13533 + "\tdownload <pkg> Download <pkg> to current directory.\n" \
13534 + "\tcompare_versions <v1> <op> <v2>\n" \
13535 + "\t compare versions using <= < > >= = << >>\n" \
13536 + "\tprint_architecture prints the architecture.\n" \
13537 + "\tprint_installation_architecture\n" \
13538 + "\twhatdepends [-A] [pkgname|pat]+\n" \
13539 + "\twhatdependsrec [-A] [pkgname|pat]+\n" \
13540 + "\twhatprovides [-A] [pkgname|pat]+\n" \
13541 + "\twhatconflicts [-A] [pkgname|pat]+\n" \
13542 + "\twhatreplaces [-A] [pkgname|pat]+\n" \
13543 + "\t prints the installation architecture.\n" \
13544 + "\n" \
13545 + "\nOptions:\n" \
13546 + "\t-A Query all packages with whatdepends, whatprovides, whatreplaces, whatconflicts\n" \
13547 + "\t-V <level> Set verbosity level to <level>. If no value is\n" \
13548 + "\t--verbosity <level> provided increase verbosity by one. Verbosity levels:\n" \
13549 + "\t 0 errors only\n" \
13550 + "\t 1 normal messages (default)\n" \
13551 + "\t 2 informative messages\n" \
13552 + "\t 3 debug output\n" \
13553 + "\t-f <conf_file> Use <conf_file> as the ipkg configuration file\n" \
13554 + "\t-conf <conf_file> Default configuration file location\n" \
13555 + " is /etc/ipkg.conf\n" \
13556 + "\t-d <dest_name> Use <dest_name> as the the root directory for\n" \
13557 + "\t-dest <dest_name> package installation, removal, upgrading.\n" \
13558 + " <dest_name> should be a defined dest name from\n" \
13559 + " the configuration file, (but can also be a\n" \
13560 + " directory name in a pinch).\n" \
13561 + "\t-o <offline_root> Use <offline_root> as the root directory for\n" \
13562 + "\t-offline <offline_root> offline installation of packages.\n" \
13563 + "\t-verbose_wget more wget messages\n" \
13564 + "\n" \
13565 + "Force Options (use when ipkg is too smart for its own good):\n" \
13566 + "\t-force-depends Make dependency checks warnings instead of errors\n" \
13567 + "\t Install/remove package in spite of failed dependences\n" \
13568 + "\t-force-defaults Use default options for questions asked by ipkg.\n" \
13569 + " (no prompts). Note that this will not prevent\n" \
13570 + " package installation scripts from prompting.\n" \
13571 + "\t-force-reinstall Allow ipkg to reinstall a package.\n" \
13572 + "\t-force-overwrite Allow ipkg to overwrite files from another package during an install.\n" \
13573 + "\t-force-downgrade Allow ipkg to downgrade packages.\n" \
13574 + "\t-force_space Install even if there does not seem to be enough space.\n" \
13575 + "\t-noaction No action -- test only\n" \
13576 + "\t-nodeps Do not follow dependences\n" \
13577 + "\t-force-removal-of-dependent-packages\n" \
13578 + "\t-recursive Allow ipkg to remove package and all that depend on it.\n" \
13579 + "\t-test No action -- test only\n" \
13580 + "\t-t Specify tmp-dir.\n" \
13581 + "\t--tmp-dir Specify tmp-dir.\n" \
13582 + "\n" \
13583 + "\tregexp could be something like 'pkgname*' '*file*' or similar\n" \
13584 + "\teg: ipkg info 'libstd*' or ipkg search '*libop*' or ipkg remove 'libncur*'\n"
13585 +
13586 #define halt_trivial_usage \
13587 "[-d delay] [-n] [-f]"
13588 #define halt_full_usage "\n\n" \
13589 --- a/Makefile
13590 +++ b/Makefile
13591 @@ -444,6 +444,7 @@
13592
13593 libs-y := \
13594 archival/ \
13595 + archival/libipkg/ \
13596 archival/libunarchive/ \
13597 console-tools/ \
13598 coreutils/ \