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