Remove debugging statement.
[oweals/busybox.git] / archival / dpkg.c
1 #include <ctype.h>
2 #include <dirent.h>
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <search.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <utime.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14
15 #include "busybox.h"
16
17 #define DEPENDSMAX      64      /* maximum number of depends we can handle */
18
19 /* Should we do full dependency checking? */
20 //#define DODEPENDS 0
21
22 /* Should we do debugging? */
23 //#define DODEBUG 0
24
25 #ifdef DODEBUG
26 #define SYSTEM(x) do_system(x)
27 #define DPRINTF(fmt,args...) fprintf(stderr, fmt, ##args)
28 #else
29 #define SYSTEM(x) system(x)
30 #define DPRINTF(fmt,args...) /* nothing */
31 #endif
32
33 /* from dpkg-deb.c */
34
35 static const char statusfile[] = "/var/lib/dpkg/status.udeb";
36 static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
37 static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
38
39 static const char infodir[] = "/var/lib/dpkg/info/";
40 static const char udpkg_quiet[] = "UDPKG_QUIET";
41
42 //static const int status_want_unknown  = 1;
43 static const int state_want_install     = 2;
44 //static const int state_want_hold      = 3;
45 //static const int state_want_deinstall = 4;
46 //static const int state_want_purge     = 5;
47
48 static const int state_flag_ok  = 1;
49 //static const int state_flag_reinstreq = 2; 
50 //static const int state_flag_hold      = 3;
51 //static const int state_flag_holdreinstreq = 4;
52
53 //static const int state_statusnoninstalled = 1;
54 static const int state_status_unpacked  = 2;
55 static const int state_status_halfconfigured = 3; 
56 static const int state_status_installed = 4;
57 static const int state_status_halfinstalled     = 5;
58 //static const int state_statusconfigfiles      = 6;
59 //static const int state_statuspostinstfailed   = 7;
60 //static const int state_statusremovalfailed    = 8;
61
62 static const char *state_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 };
63 static const char *state_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 };
64 static const char *state_words_status[] = { "not-installed", "unpacked", "half-configured", "installed", 
65                 "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 };
66
67 static const int color_white    = 0;
68 static const int color_grey     = 1;
69 static const int color_black    = 2;
70
71 /* data structures */ 
72 typedef struct package_s {
73         char *filename;
74         char *package;
75         unsigned char state_want;
76         unsigned char state_flag;
77         unsigned char state_status;
78         char *depends;
79         char *provides;
80         char *description;
81         char *priority;
82         char *section;
83         char *installed_size;
84         char *maintainer;
85         char *source;
86         char *version;
87         char *pre_depends;
88         char *replaces;
89         char *recommends;
90         char *suggests;
91         char *conflicts;
92         char *conffiles;
93         char *long_description;
94         char *architecture;
95         char *md5sum;
96         int installer_menu_item;
97         char color; /* for topo-sort */
98         struct package_s *requiredfor[DEPENDSMAX]; 
99         unsigned short requiredcount;
100         struct package_s *next;
101 } package_t;
102
103 #ifdef DODEBUG
104 static int do_system(const char *cmd)
105 {
106         DPRINTF("cmd is %s\n", cmd);
107         return system(cmd);
108 }
109 #else
110 #define do_system(cmd) system(cmd)
111 #endif
112
113 static int package_compare(const void *p1, const void *p2)
114 {
115         return strcmp(((package_t *)p1)->package, 
116                 ((package_t *)p2)->package);
117 }
118
119 #ifdef DODEPENDS
120
121 static char **depends_split(const char *dependsstr)
122 {
123         static char *dependsvec[DEPENDSMAX];
124         char *p;
125         int i = 0;
126
127         dependsvec[0] = 0;
128         if (dependsstr == 0) {
129                 goto end;
130         }
131
132         p = xstrdup(dependsstr);
133         while (*p != 0 && *p != '\n') {
134                 if (*p != ' ') {
135                         if (*p == ',') {
136                                 *p = 0;
137                                 dependsvec[++i] = 0;
138                         } else {
139                                 if (dependsvec[i] == 0) {
140                                         dependsvec[i] = p;
141                                 }
142                         }
143                 } else {
144                         *p = 0; /* eat the space... */
145                 }
146                 p++;
147         }
148         *p = 0;
149
150 end:
151         dependsvec[i+1] = 0;
152         return dependsvec;
153 }
154
155 /* Topological sort algorithm:
156  * ordered is the output list, pkgs is the dependency graph, pkg is 
157  * the current node
158  *
159  * recursively add all the adjacent nodes to the ordered list, marking
160  * each one as visited along the way
161  *
162  * yes, this algorithm looks a bit odd when all the params have the
163  * same type :-)
164  */
165 static void depends_sort_visit(package_t **ordered, package_t *pkgs,
166                 package_t *pkg)
167 {
168         unsigned short i;
169
170         /* mark node as processing */
171         pkg->color = color_grey;
172
173         /* visit each not-yet-visited node */
174         for (i = 0; i < pkg->requiredcount; i++)
175                 if (pkg->requiredfor[i]->color == color_white)
176                         depends_sort_visit(ordered, pkgs, pkg->requiredfor[i]);
177
178 #if 0
179         /* add it to the list */
180         newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
181         /* make a shallow copy */
182         *newnode = *pkg;
183         newnode->next = *ordered;
184         *ordered = newnode;
185 #endif
186
187         pkg->next = *ordered;
188         *ordered = pkg;
189
190         /* mark node as done */
191         pkg->color = color_black;
192 }
193
194 static package_t *depends_sort(package_t *pkgs)
195 {
196         /* TODO: it needs to break cycles in the to-be-installed package 
197          * graph... */
198         package_t *ordered = NULL;
199         package_t *pkg;
200
201         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
202                 pkg->color = color_white;
203         }
204         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
205                 if (pkg->color == color_white) {
206                         depends_sort_visit(&ordered, pkgs, pkg);
207                 }
208         }
209
210         /* Leaks the old list... return the new one... */
211         return ordered;
212 }
213
214
215 /* resolve package dependencies -- 
216  * for each package in the list of packages to be installed, we parse its 
217  * dependency info to determine if the dependent packages are either 
218  * already installed, or are scheduled to be installed. If both tests fail
219  * than bail.
220  *
221  * The algorithm here is O(n^2*m) where n = number of packages to be 
222  * installed and m is the # of dependencies per package. Not a terribly
223  * efficient algorithm, but given that at any one time you are unlikely
224  * to install a very large number of packages it doesn't really matter
225  */
226 static package_t *depends_resolve(package_t *pkgs, void *status)
227 {
228         package_t *pkg, *chk;
229         package_t dependpkg;
230         char **dependsvec;
231         int i;
232         void *found;
233
234         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
235                 dependsvec = depends_split(pkg->depends);
236                 i = 0;
237                 while (dependsvec[i] != 0) {
238                         /* Check for dependencies; first look for installed packages */
239                         dependpkg.package = dependsvec[i];
240                         if (((found = tfind(&dependpkg, &status, package_compare)) == 0) ||
241                             ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) &&
242                                 (chk->state_status & state_status_installed))) {
243
244                                 /* if it fails, we look through the list of packages we are going to 
245                                  * install */
246                                 for (chk = pkgs; chk != 0; chk = chk->next) {
247                                         if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && 
248                                              strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
249                                                 if (chk->requiredcount >= DEPENDSMAX) {
250                                                         error_msg("Too many dependencies for %s", chk->package);
251                                                         return 0;
252                                                 }
253                                                 if (chk != pkg) {
254                                                         chk->requiredfor[chk->requiredcount++] = pkg;
255                                                 }
256                                                 break;
257                                         }
258                                 }
259                                 if (chk == 0) {
260                                         error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
261                                         return 0;
262                                 }
263                         }
264                         i++;
265                 }
266         }
267
268         return depends_sort(pkgs);
269 }
270 #endif
271
272 /* Status file handling routines
273  * 
274  * This is a fairly minimalistic implementation. there are two main functions 
275  * that are supported:
276  * 
277  * 1) reading the entire status file:
278  *    the status file is read into memory as a binary-tree, with just the 
279  *    package and status info preserved
280  *
281  * 2) merging the status file
282  *    control info from (new) packages is merged into the status file, 
283  *    replacing any pre-existing entries. when a merge happens, status info 
284  *    read using the status_read function is written back to the status file
285  */
286 static unsigned char status_parse(const char *line, const char **status_words)
287 {
288         unsigned char status_num;
289         int i = 0;
290
291         while (status_words[i] != 0) {
292                 if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) {
293                         status_num = (char)i;
294                         return(status_num);
295                 }
296                 i++;
297         }
298         /* parse error */
299         error_msg("Invalid status word");
300         return(0);
301 }
302
303 /*
304  * Read the buffered control file and parse it,
305  * filling parsed fields into the package structure
306  */
307 static int fill_package_struct(package_t *package, const char *package_buffer)
308 {
309         char *field = NULL;
310         int field_start = 0;
311         int field_length = 0;
312
313         while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
314                 field_length = strlen(field);
315                 field_start += (field_length + 1);
316
317                 if (strlen(field) == 0) {
318                         printf("empty line: *this shouldnt happen i dont think*\n");
319                         break;
320                 }
321
322                 /* these are common to both installed and uninstalled packages */
323                 if (strstr(field, "Package: ") == field) {
324                         package->package = strdup(field + 9);
325                 }
326                 else if (strstr(field, "Depends: ") == field) {
327                         package->depends = strdup(field + 9);
328                 }
329                 else if (strstr(field, "Provides: ") == field) {
330                         package->provides = strdup(field + 10);
331                 }
332                 /* This is specific to the Debian Installer. Ifdef? */
333                 else if (strstr(field, "installer-menu-item: ") == field) {
334                         package->installer_menu_item = atoi(field + 21);
335                 }
336                 else if (strstr(field, "Description: ") == field) {
337                         package->description = strdup(field + 13);
338                 }
339                 else if (strstr(field, "Priority: ") == field) {
340                         package->priority = strdup(field + 10);
341                 }
342                 else if (strstr(field, "Section: ") == field) {
343                         package->section = strdup(field + 9);
344                 }
345                 else if (strstr(field, "Installed-Size: ") == field) {
346                         package->installed_size = strdup(field + 16);
347                 }
348                 else if (strstr(field, "Maintainer: ") == field) {
349                         package->maintainer = strdup(field + 12);
350                 }
351                 else if (strstr(field, "Version: ") == field) {
352                         package->version = strdup(field + 9);
353                 }
354                 else if (strstr(field, "Suggests: ") == field) {
355                         package->suggests = strdup(field + 10);
356                 }
357                 else if (strstr(field, "Recommends: ") == field) {
358                         package->recommends = strdup(field + 12);
359                 }
360 /*              else if (strstr(field, "Conffiles: ") == field) {
361                         package->conffiles = read_block(file);
362                                 package->conffiles = xcalloc(1, 1);
363                                 while ((field = strtok(NULL, "\n")) != NULL) {
364                                         package->long_description = xrealloc(package->conffiles, 
365                                         strlen(package->conffiles) + strlen(field) + 1);
366                                         strcat(package->conffiles, field);
367                                 }
368                         }
369 */
370                 /* These are only in available file */
371                 else if (strstr(field, "Architecture: ") == field) {
372                         package->architecture = strdup(field + 14);
373                 }
374                 else if (strstr(field, "Filename: ") == field) {
375                         package->filename = strdup(field + 10);
376                 }
377                 else if (strstr(field, "MD5sum ") == field) {
378                         package->md5sum = strdup(field + 7);
379                 }
380
381                 /* This is only needed for status file */
382                 if (strstr(field, "Status: ") == field) {
383                         char *word_pointer;
384
385                         word_pointer = strchr(field, ' ') + 1;
386                         package->state_want = status_parse(word_pointer, state_words_want);
387                         word_pointer = strchr(word_pointer, ' ') + 1;
388                         package->state_flag = status_parse(word_pointer, state_words_flag);
389                         word_pointer = strchr(word_pointer, ' ') + 1;
390                         package->state_status = status_parse(word_pointer, state_words_status);
391                 } else {
392                         package->state_want = status_parse("purge", state_words_want);
393                         package->state_flag = status_parse("ok", state_words_flag);
394                         package->state_status = status_parse("not-installed", state_words_status);
395                 }
396
397                 free(field);
398         }
399         return EXIT_SUCCESS;
400 }
401
402 extern void write_package(FILE *out_file, package_t *pkg)
403 {
404         if (pkg->package) {
405                 fprintf(out_file, "Package: %s\n", pkg->package);
406         }
407         if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) {
408                 fprintf(out_file, "Status: %s %s %s\n", 
409                         state_words_want[pkg->state_want - 1],
410                         state_words_flag[pkg->state_flag - 1],
411                         state_words_status[pkg->state_status - 1]);
412         }
413         if (pkg->depends) {
414                 fprintf(out_file, "Depends: %s\n", pkg->depends);
415         }
416         if (pkg->provides) {
417                 fprintf(out_file, "Provides: %s\n", pkg->provides);
418         }
419         if (pkg->priority) {
420                 fprintf(out_file, "Priority: %s\n", pkg->priority);
421         }
422         if (pkg->section) {
423                 fprintf(out_file, "Section: %s\n", pkg->section);
424         }
425         if (pkg->section) {
426                 fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size);
427         }
428         if (pkg->maintainer) {
429                 fprintf(out_file, "Maintainer: %s\n", pkg->maintainer);
430         }
431         if (pkg->source) {
432                 fprintf(out_file, "Source: %s\n", pkg->source);
433         }
434         if (pkg->version) {
435                 fprintf(out_file, "Version: %s\n", pkg->version);
436         }
437         if (pkg->pre_depends) {
438                 fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends);
439         }
440         if (pkg->replaces) {
441                 fprintf(out_file, "Replaces: %s\n", pkg->replaces);
442         }
443         if (pkg->recommends) {
444                 fprintf(out_file, "Recommends: %s\n", pkg->recommends);
445         }
446         if (pkg->suggests) {
447                 fprintf(out_file, "Suggests: %s\n", pkg->suggests);
448         }
449         if (pkg->conflicts) {
450                 fprintf(out_file, "Conflicts: %s\n", pkg->conflicts);
451         }
452         if (pkg->conffiles) {
453                 fprintf(out_file, "Conf-files: %s\n", pkg->conffiles);
454         }
455         if (pkg->architecture) {
456                 fprintf(out_file, "Architecture: %s\n", pkg->architecture);
457         }
458         if (pkg->filename) {
459                 fprintf(out_file, "Filename: %s\n", pkg->filename);
460         }
461         if (pkg->md5sum) {
462                 fprintf(out_file, "MD5sum: %s\n", pkg->md5sum);
463         }
464         if (pkg->installer_menu_item) {
465                 fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item);
466         }
467         if (pkg->description) {
468                 fprintf(out_file, "Description: %s\n", pkg->description);
469         }
470         fputc('\n', out_file);
471         pkg = pkg->next;
472 }
473
474 static void *status_read(void)
475 {
476         FILE *f;
477         void *status = 0;
478         package_t *m = 0, *p = 0, *t = 0;
479         char *package_control_buffer = NULL;
480
481         if (getenv(udpkg_quiet) == NULL) {
482                 printf("(Reading database...)\n");
483         }
484
485         if ((f = wfopen(statusfile, "r")) == NULL) {
486                 return(NULL);
487         }
488
489         while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) {
490                 m = (package_t *)xcalloc(1, sizeof(package_t));
491                 fill_package_struct(m, package_control_buffer);
492                 if (m->package) {
493                         /*
494                          * If there is an item in the tree by this name,
495                          * it must be a virtual package; insert real
496                          * package in preference.
497                          */
498                         tdelete(m, &status, package_compare);
499                         tsearch(m, &status, package_compare);
500                         if (m->provides) {
501                                 /* 
502                                  * A "Provides" triggers the insertion
503                                  * of a pseudo package into the status
504                                  * binary-tree.
505                                  */
506                                 p = (package_t *)xcalloc(1, sizeof(package_t));
507                                 p->package = xstrdup(m->provides);
508                                 t = *(package_t **)tsearch(p, &status, package_compare);
509                                 if (t != p) {
510                                         free(p->package);
511                                         free(p);
512                                 } else {
513                                         /*
514                                          * Pseudo package status is the
515                                          * same as the status of the
516                                          * package providing it 
517                                          * FIXME: (not quite right, if 2
518                                          * packages of different statuses
519                                          * provide it).
520                                          */
521                                         t->state_want = m->state_want;
522                                         t->state_flag = m->state_flag;
523                                         t->state_status = m->state_status;
524                                 }
525                         }
526                 }
527                 else {
528                         free(m);
529                 }
530         }
531         fclose(f);
532         return status;
533 }
534
535 static int status_merge(void *status, package_t *pkgs)
536 {
537         FILE *fin, *fout;
538         char *line = NULL;
539         package_t *pkg = 0, *statpkg = 0;
540         package_t locpkg;
541
542         if ((fout = wfopen(new_statusfile, "w")) == NULL) {
543                 return 0;
544         }
545         if (getenv(udpkg_quiet) == NULL) {
546                 printf("(Updating database...)\n");
547         }
548
549         /*
550          * Dont use wfopen here, handle errors ourself
551          */
552         if ((fin = fopen(statusfile, "r")) != NULL) {
553                 while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
554                         chomp(line); /* trim newline */
555                         /* If we see a package header, find out if it's a package
556                          * that we have processed. if so, we skip that block for
557                          * now (write it at the end).
558                          *
559                          * we also look at packages in the status cache and update
560                          * their status fields
561                          */
562                         if (strstr(line, "Package: ") == line) {
563                                 for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
564                                                 pkg->package) != 0; pkg = pkg->next) ;
565
566                                 locpkg.package = line + 9;
567                                 statpkg = tfind(&locpkg, &status, package_compare);
568         
569                                 /* note: statpkg should be non-zero, unless the status
570                                  * file was changed while we are processing (no locking
571                                  * is currently done...
572                                  */
573                                 if (statpkg != 0) {
574                                         statpkg = *(package_t **)statpkg;
575                                 }
576                         }
577                         if (pkg != 0) {
578                                 continue;
579                         }
580                         if (strstr(line, "Status: ") == line && statpkg != 0) {
581                                 snprintf(line, sizeof(line), "Status: %s %s %s",
582                                         state_words_want[statpkg->state_want - 1], 
583                                         state_words_flag[statpkg->state_flag - 1], 
584                                         state_words_status[statpkg->state_status - 1]);
585                         }
586                         fprintf(fout, "%s\n", line);
587                 }
588                 fclose(fin);
589         }
590         free(line);
591
592         // Print out packages we processed.
593         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
594                 write_package(fout, pkg);
595         }
596         fclose(fout);
597
598         /*
599          * Its ok if renaming statusfile fails becasue it doesnt exist
600          */
601         if (rename(statusfile, bak_statusfile) == -1) {
602                 struct stat stat_buf;   
603                 if (stat(statusfile, &stat_buf) == 0) {
604                         error_msg("Couldnt create backup status file");
605                         return(EXIT_FAILURE);
606                 }
607                 error_msg("No status file found, creating new one");
608         }
609
610         if (rename(new_statusfile, statusfile) == -1) {
611                 error_msg("Couldnt create status file");
612                 return(EXIT_FAILURE);
613         }
614         return(EXIT_SUCCESS);
615 }
616
617 static int is_file(const char *fn)
618 {
619         struct stat statbuf;
620
621         if (stat(fn, &statbuf) < 0) {
622                 return 0;
623         }
624         return S_ISREG(statbuf.st_mode);
625 }
626
627 static int dpkg_doconfigure(package_t *pkg)
628 {
629         int r;
630         char postinst[1024];
631         char buf[1024];
632
633         DPRINTF("Configuring %s\n", pkg->package);
634         pkg->state_status = 0;
635         snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
636
637         if (is_file(postinst)) {
638                 snprintf(buf, sizeof(buf), "%s configure", postinst);
639                 if ((r = do_system(buf)) != 0) {
640                         error_msg("postinst exited with status %d\n", r);
641                         pkg->state_status = state_status_halfconfigured;
642                         return 1;
643                 }
644         }
645         pkg->state_status = state_status_installed;
646         
647         return 0;
648 }
649
650 static int dpkg_dounpack(package_t *pkg)
651 {
652         FILE *out_stream;
653         char *info_prefix;
654         int status = TRUE;
655         int r = 0;
656
657         DPRINTF("Unpacking %s\n", pkg->package);
658
659         /* extract the data file */
660         deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", NULL);
661
662         /* extract the control files */
663         info_prefix = (char *) malloc(strlen(pkg->package) + strlen(infodir) + 2 + 5 + 1);
664         sprintf(info_prefix, "%s/%s.", infodir, pkg->package);
665         deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL);
666
667         /* Create the list file */
668         strcat(info_prefix, "list");
669         out_stream = wfopen(info_prefix, "w");                  
670         deb_extract(pkg->filename, out_stream, (extract_data_tar_gz | extract_list), NULL, NULL);
671         fclose(out_stream);
672
673         pkg->state_want = state_want_install;
674         pkg->state_flag = state_flag_ok;
675  
676         if (status == TRUE) {
677                 pkg->state_status = state_status_unpacked;
678         } else {
679                 pkg->state_status = state_status_halfinstalled;
680         }
681
682         return r;
683 }
684
685 /*
686  * Extract and parse the control file from control.tar.gz
687  */
688 static int dpkg_read_control(package_t *pkg)
689 {
690         FILE *pkg_file;
691         char *control_buffer = NULL;
692
693         if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
694                 return EXIT_FAILURE;
695         }
696         control_buffer = deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control");
697         fill_package_struct(pkg, control_buffer);
698         return EXIT_SUCCESS;
699 }
700
701 static int dpkg_unpack(package_t *pkgs, void *status)
702 {
703         int r = 0;
704         package_t *pkg;
705
706         for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
707                 dpkg_read_control(pkg);
708                 if ((r = dpkg_dounpack(pkg)) != 0 ) {
709                         break;
710                 }
711         }
712         status_merge(status, pkgs);
713
714         return r;
715 }
716
717 static int dpkg_configure(package_t *pkgs, void *status)
718 {
719         int r = 0;
720         void *found;
721         package_t *pkg;
722
723         for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
724                 found = tfind(pkg, &status, package_compare);
725
726                 if (found == 0) {
727                         error_msg("Trying to configure %s, but it is not installed", pkg->package);
728                         r = 1;
729                 } 
730                 /* configure the package listed in the status file;
731                  * not pkg, as we have info only for the latter
732                  */
733                 else {
734                         r = dpkg_doconfigure(*(package_t **)found);
735                 }
736         }
737         status_merge(status, 0);
738
739         return r;
740 }
741
742 static int dpkg_install(package_t *pkgs, void *status)
743 {
744         package_t *p, *ordered = 0;
745
746         /* Stage 1: parse all the control information */
747         for (p = pkgs; p != 0; p = p->next) {
748                 dpkg_read_control(p);
749         }
750
751         /* Stage 2: resolve dependencies */
752 #ifdef DODEPENDS
753         ordered = depends_resolve(pkgs, status);
754 #else
755         ordered = pkgs;
756 #endif
757         
758         /* Stage 3: install */
759         for (p = ordered; p != 0; p = p->next) {
760                 p->state_want = state_want_install;
761
762                 /* for now the flag is always set to ok... this is probably
763                  * not what we want
764                  */
765                 p->state_flag = state_flag_ok;
766
767                 DPRINTF("Installing %s\n", p->package);
768                 if (dpkg_dounpack(p) != 0) {
769                         perror_msg(p->filename);
770                 }
771
772                 if (dpkg_doconfigure(p) != 0) {
773                         perror_msg(p->filename);
774                 }
775         }
776
777         if (ordered != 0) {
778                 status_merge(status, pkgs);
779         }
780
781         return 0;
782 }
783
784 /*
785  * Not implemented yet
786  *
787 static int dpkg_remove(package_t *pkgs, void *status)
788 {
789         package_t *p;
790
791         for (p = pkgs; p != 0; p = p->next)
792         {
793         }
794         status_merge(status, 0);
795
796         return 0;
797 }
798 */
799
800 extern int dpkg_main(int argc, char **argv)
801 {
802         const int arg_install = 1;
803         const int arg_unpack = 2;
804         const int arg_configure = 4;
805
806         package_t *p, *packages = NULL;
807         void *status = NULL;
808         int opt = 0;
809         int optflag = 0;
810
811         while ((opt = getopt(argc, argv, "iruc")) != -1) {
812                 switch (opt) {
813                         case 'i':
814                                 optflag |= arg_install;
815                                 break;
816                         case 'u':
817                                 optflag |= arg_unpack;
818                                 break;
819                         case 'c':
820                                 optflag |= arg_configure;
821                                 break;
822                         default:
823                                 show_usage();
824                 }
825         }
826
827         while (optind < argc) {
828                 p = (package_t *) xcalloc(1, sizeof(package_t));
829                 if (optflag & arg_configure) {
830                         p->package = xstrdup(argv[optind]);
831                 } else {
832                         p->filename = xstrdup(argv[optind]);
833                 }
834                 p->next = packages;
835                 packages = p;
836
837                 optind++;
838         }
839
840         make_directory((char *)infodir, S_IRWXU, FILEUTILS_RECUR);
841
842         status = status_read();
843
844         if (optflag & arg_install) {
845                 return dpkg_install(packages, status);
846         }
847         else if (optflag & arg_unpack) {
848                 return dpkg_unpack(packages, status);
849         }
850         else if (optflag & arg_configure) {
851                 return dpkg_configure(packages, status);
852         }
853         return(EXIT_FAILURE);
854 }