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