Implement suggestion from Adam Slattery, (don't default to killing closing bug #1190.
[oweals/busybox.git] / dpkg.c
diff --git a/dpkg.c b/dpkg.c
index 7b4c74d7dd8fa0e7ad85d827e5232b40e71415c4..01ddc7b7b77a867aa9de1d6cef97e161e4a935e2 100644 (file)
--- a/dpkg.c
+++ b/dpkg.c
@@ -1,3 +1,5 @@
+#include <dirent.h>
+#include <getopt.h>
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
 
 #include "busybox.h"
 
-//#define PACKAGE "udpkg"
-//#define VERSION "0.1"
+#define DEPENDSMAX     64      /* maximum number of depends we can handle */
 
-/*
- * Should we do full dependency checking?
- */
-#define DODEPENDS 1
+/* Should we do full dependency checking? */
+//#define DODEPENDS 0
 
-/*
- * Should we do debugging?
- */
-#define DODEBUG 0
+/* Should we do debugging? */
+//#define DODEBUG 0
 
 #ifdef DODEBUG
 #define SYSTEM(x) do_system(x)
 #define DPRINTF(fmt,args...) /* nothing */
 #endif
 
-#define BUFSIZE                4096
-#define DEPENDSMAX     64      /* maximum number of depends we can handle */
+/* from dpkg-deb.c */
 
 static const char statusfile[] = "/var/lib/dpkg/status.udeb";
 static const char new_statusfile[] = "/var/lib/dpkg/status.udeb.new";
 static const char bak_statusfile[] = "/var/lib/dpkg/status.udeb.bak";
 
-static const char dpkgcidir[] = "/var/lib/dpkg/tmp.ci/";
-static const char rm_dpkgcidir[] = "rm -rf /var/lib/dpkg/tmp.ci/";
-
 static const char infodir[] = "/var/lib/dpkg/info/";
 static const char udpkg_quiet[] = "UDPKG_QUIET";
 
-//static const int status_wantstart    = 0;
-//static const int status_wantunknown  = (1 << 0);
-static const int status_wantinstall    = (1 << 1);
-//static const int status_wanthold     = (1 << 2);
-//static const int status_wantdeinstall        = (1 << 3);
-//static const int status_wantpurge    = (1 << 4);
-static const int status_wantmask       = 31;
-
-//static const int status_flagstart    = 5;
-static const int status_flagok = (1 << 5);     /* 32 */
-//static const int status_flagreinstreq        = (1 << 6); 
-//static const int status_flaghold     = (1 << 7);
-//static const int status_flagholdreinstreq    = (1 << 8);
-static const int status_flagmask       = 480;
-
-//static const int status_statusstart  = 9;
-//static const int status_statusnoninstalled   = (1 << 9); /* 512 */
-static const int status_statusunpacked = (1 << 10);
-static const int status_statushalfconfigured   = (1 << 11); 
-static const int status_statusinstalled        = (1 << 12);
-static const int status_statushalfinstalled    = (1 << 13);
-//static const int status_statusconfigfiles    = (1 << 14);
-//static const int status_statuspostinstfailed = (1 << 15);
-//static const int status_statusremovalfailed  = (1 << 16);
-static const int status_statusmask =  130560; /* i assume status_statusinstalled is supposed to be included */
-
-static const char *statuswords[][10] = {
-       { (char *) 0, "unknown", "install", "hold", "deinstall", "purge", 0 },
-       { (char *) 5, "ok", "reinstreq", "hold", "hold-reinstreq", 0 },
-       { (char *) 9, "not-installed", "unpacked", "half-configured",
-               "installed", "half-installed", "config-files",
-               "post-inst-failed", "removal-failed", 0 }
-};
-
-const int color_white  = 0;
-const int color_grey   = 1;
-const int color_black  = 2;
-
-/* data structures */
+//static const int status_want_unknown = 1;
+static const int state_want_install    = 2;
+//static const int state_want_hold     = 3;
+//static const int state_want_deinstall        = 4;
+//static const int state_want_purge    = 5;
+
+static const int state_flag_ok = 1;
+//static const int state_flag_reinstreq = 2; 
+//static const int state_flag_hold     = 3;
+//static const int state_flag_holdreinstreq = 4;
+
+//static const int state_statusnoninstalled = 1;
+static const int state_status_unpacked = 2;
+static const int state_status_halfconfigured = 3; 
+static const int state_status_installed        = 4;
+static const int state_status_halfinstalled    = 5;
+//static const int state_statusconfigfiles     = 6;
+//static const int state_statuspostinstfailed  = 7;
+//static const int state_statusremovalfailed   = 8;
+
+static const char *state_words_want[] = { "unknown", "install", "hold", "deinstall", "purge", 0 };
+static const char *state_words_flag[] = { "ok", "reinstreq", "hold", "hold-reinstreq", 0 };
+static const char *state_words_status[] = { "not-installed", "unpacked", "half-configured", "installed", 
+               "half-installed", "config-files", "post-inst-failed", "removal-failed", 0 };
+
+static const int color_white   = 0;
+static const int color_grey    = 1;
+static const int color_black   = 2;
+
+/* data structures */ 
 typedef struct package_s {
-       char *file;
+       char *filename;
        char *package;
-       char *version;
+       unsigned char state_want;
+       unsigned char state_flag;
+       unsigned char state_status;
        char *depends;
        char *provides;
        char *description;
+       char *priority;
+       char *section;
+       char *installed_size;
+       char *maintainer;
+       char *source;
+       char *version;
+       char *pre_depends;
+       char *replaces;
+       char *recommends;
+       char *suggests;
+       char *conflicts;
+       char *conffiles;
+       char *long_description;
+       char *architecture;
+       char *md5sum;
        int installer_menu_item;
-       unsigned long status;
        char color; /* for topo-sort */
        struct package_s *requiredfor[DEPENDSMAX]; 
        unsigned short requiredcount;
@@ -129,7 +129,7 @@ static char **depends_split(const char *dependsstr)
                goto end;
        }
 
-       p = strdup(dependsstr);
+       p = xstrdup(dependsstr);
        while (*p != 0 && *p != '\n') {
                if (*p != ' ') {
                        if (*p == ',') {
@@ -177,7 +177,7 @@ static void depends_sort_visit(package_t **ordered, package_t *pkgs,
 
 #if 0
        /* add it to the list */
-       newnode = (struct package_t *)malloc(sizeof(struct package_t));
+       newnode = (struct package_t *)xmalloc(sizeof(struct package_t));
        /* make a shallow copy */
        *newnode = *pkg;
        newnode->next = *ordered;
@@ -237,10 +237,9 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
                while (dependsvec[i] != 0) {
                        /* Check for dependencies; first look for installed packages */
                        dependpkg.package = dependsvec[i];
-                       if ((found = tfind(&dependpkg, &status, package_compare)) == 0 ||
-                           ((chk = *(package_t **)found) &&
-                            (chk->status & (status_flagok | status_statusinstalled)) != 
-                             (status_flagok | status_statusinstalled))) {
+                       if (((found = tfind(&dependpkg, &status, package_compare)) == 0) ||
+                           ((chk = *(package_t **)found) && (chk->state_flag & state_flag_ok) &&
+                               (chk->state_status & state_status_installed))) {
 
                                /* if it fails, we look through the list of packages we are going to 
                                 * install */
@@ -248,7 +247,7 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
                                        if (strcmp(chk->package, dependsvec[i]) == 0 || (chk->provides && 
                                             strncmp(chk->provides, dependsvec[i], strlen(dependsvec[i])) == 0)) {
                                                if (chk->requiredcount >= DEPENDSMAX) {
-                                                       fprintf(stderr, "Too many dependencies for %s\n", chk->package);
+                                                       error_msg("Too many dependencies for %s", chk->package);
                                                        return 0;
                                                }
                                                if (chk != pkg) {
@@ -258,7 +257,7 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
                                        }
                                }
                                if (chk == 0) {
-                                       fprintf(stderr, "%s depends on %s, but it is not going to be installed\n", pkg->package, dependsvec[i]);
+                                       error_msg("%s depends on %s, but it is not going to be installed", pkg->package, dependsvec[i]);
                                        return 0;
                                }
                        }
@@ -284,96 +283,192 @@ static package_t *depends_resolve(package_t *pkgs, void *status)
  *    replacing any pre-existing entries. when a merge happens, status info 
  *    read using the status_read function is written back to the status file
  */
-static unsigned long status_parse(const char *line)
+static unsigned char status_parse(const char *line, const char **status_words)
 {
-       char *p;
-       int i, j;
-       unsigned long l = 0;
+       unsigned char status_num;
+       int i = 0;
 
-       for (i = 0; i < 3; i++) {
-               if ((p = strchr(line, ' ')) != NULL) {
-                       *p = 0;
+       while (status_words[i] != 0) {
+               if (strncmp(line, status_words[i], strlen(status_words[i])) == 0) {
+                       status_num = (char)i;
+                       return(status_num);
                }
-               j = 1;
-               while (statuswords[i][j] != 0) {
-                       if (strcmp(line, statuswords[i][j]) == 0) {
-                               l |= (1 << ((int)statuswords[i][0] + j - 1));
-                               break;
-                       }
-                       j++;
-               }
-               /* parse error */
-               if (statuswords[i][j] == 0) {
-                       return 0;
-               }
-               line = p+1;
+               i++;
        }
-
-       return l;
-}
-
-static const char *status_print(unsigned long flags)
-{
-       /* this function returns a static buffer... */
-       static char buf[256];
-       int i, j;
-
-       buf[0] = 0;
-       for (i = 0; i < 3; i++) {
-               j = 1;
-               while (statuswords[i][j] != 0) {
-                       if ((flags & (1 << ((int)statuswords[i][0] + j - 1))) != 0)     {
-                               strcat(buf, statuswords[i][j]);
-                               if (i < 2) strcat(buf, " ");
-                               break;
-                       }
-                       j++;
-               }
-               if (statuswords[i][j] == 0) {
-                       fprintf(stderr, "corrupted status flag!!\n");
-                       return NULL;
-               }
-       }
-
-       return buf;
+       /* parse error */
+       error_msg("Invalid status word");
+       return(0);
 }
 
 /*
- * Read a control file (or a stanza of a status file) and parse it,
+ * Read the buffered control file and parse it,
  * filling parsed fields into the package structure
  */
-static void control_read(FILE *file, package_t *p)
+static int fill_package_struct(package_t *package, const char *package_buffer)
 {
-       char *line;
+       char *field = NULL;
+       int field_start = 0;
+       int field_length = 0;
 
-       while ((line = get_line_from_file(file)) != NULL) {
-               line[strlen(line)] = 0;
+       while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
+               field_length = strlen(field);
+               field_start += (field_length + 1);
 
-               if (strlen(line) == 0) {
+               if (strlen(field) == 0) {
+                       printf("empty line: *this shouldnt happen i dont think*\n");
                        break;
-               } else
-                       if (strstr(line, "Package: ") == line) {
-                               p->package = strdup(line + 9);
-               } else
-                       if (strstr(line, "Status: ") == line) {
-                               p->status = status_parse(line + 8);
-               } else
-                       if (strstr(line, "Depends: ") == line) {
-                               p->depends = strdup(line + 9);
-               } else
-                       if (strstr(line, "Provides: ") == line) {
-                               p->provides = strdup(line + 10);
-               } else
-                       if (strstr(line, "Description: ") == line) {
-                               p->description = strdup(line + 13);
+               }
+
+               /* these are common to both installed and uninstalled packages */
+               if (strstr(field, "Package: ") == field) {
+                       package->package = strdup(field + 9);
+               }
+               else if (strstr(field, "Depends: ") == field) {
+                       package->depends = strdup(field + 9);
+               }
+               else if (strstr(field, "Provides: ") == field) {
+                       package->provides = strdup(field + 10);
+               }
                /* This is specific to the Debian Installer. Ifdef? */
-               } else
-                       if (strstr(line, "installer-menu-item: ") == line) {
-                               p->installer_menu_item = atoi(line + 21);
+               else if (strstr(field, "installer-menu-item: ") == field) {
+                       package->installer_menu_item = atoi(field + 21);
+               }
+               else if (strstr(field, "Description: ") == field) {
+                       package->description = strdup(field + 13);
+               }
+               else if (strstr(field, "Priority: ") == field) {
+                       package->priority = strdup(field + 10);
+               }
+               else if (strstr(field, "Section: ") == field) {
+                       package->section = strdup(field + 9);
+               }
+               else if (strstr(field, "Installed-Size: ") == field) {
+                       package->installed_size = strdup(field + 16);
+               }
+               else if (strstr(field, "Maintainer: ") == field) {
+                       package->maintainer = strdup(field + 12);
+               }
+               else if (strstr(field, "Version: ") == field) {
+                       package->version = strdup(field + 9);
+               }
+               else if (strstr(field, "Suggests: ") == field) {
+                       package->suggests = strdup(field + 10);
+               }
+               else if (strstr(field, "Recommends: ") == field) {
+                       package->recommends = strdup(field + 12);
+               }
+/*             else if (strstr(field, "Conffiles: ") == field) {
+                       package->conffiles = read_block(file);
+                               package->conffiles = xcalloc(1, 1);
+                               while ((field = strtok(NULL, "\n")) != NULL) {
+                                       package->long_description = xrealloc(package->conffiles, 
+                                       strlen(package->conffiles) + strlen(field) + 1);
+                                       strcat(package->conffiles, field);
+                               }
+                       }
+*/
+               /* These are only in available file */
+               else if (strstr(field, "Architecture: ") == field) {
+                       package->architecture = strdup(field + 14);
+               }
+               else if (strstr(field, "Filename: ") == field) {
+                       package->filename = strdup(field + 10);
                }
-               /* TODO: localized descriptions */
+               else if (strstr(field, "MD5sum ") == field) {
+                       package->md5sum = strdup(field + 7);
+               }
+
+               /* This is only needed for status file */
+               if (strstr(field, "Status: ") == field) {
+                       char *word_pointer;
+
+                       word_pointer = strchr(field, ' ') + 1;
+                       package->state_want = status_parse(word_pointer, state_words_want);
+                       word_pointer = strchr(word_pointer, ' ') + 1;
+                       package->state_flag = status_parse(word_pointer, state_words_flag);
+                       word_pointer = strchr(word_pointer, ' ') + 1;
+                       package->state_status = status_parse(word_pointer, state_words_status);
+               } else {
+                       package->state_want = status_parse("purge", state_words_want);
+                       package->state_flag = status_parse("ok", state_words_flag);
+                       package->state_status = status_parse("not-installed", state_words_status);
+               }
+
+               free(field);
        }
-       free(line);
+       return EXIT_SUCCESS;
+}
+
+extern void write_package(FILE *out_file, package_t *pkg)
+{
+       if (pkg->package) {
+               fprintf(out_file, "Package: %s\n", pkg->package);
+       }
+       if ((pkg->state_want != 0) || (pkg->state_flag != 0)|| (pkg->state_status != 0)) {
+               fprintf(out_file, "Status: %s %s %s\n", 
+                       state_words_want[pkg->state_want - 1],
+                       state_words_flag[pkg->state_flag - 1],
+                       state_words_status[pkg->state_status - 1]);
+       }
+       if (pkg->depends) {
+               fprintf(out_file, "Depends: %s\n", pkg->depends);
+       }
+       if (pkg->provides) {
+               fprintf(out_file, "Provides: %s\n", pkg->provides);
+       }
+       if (pkg->priority) {
+               fprintf(out_file, "Priority: %s\n", pkg->priority);
+       }
+       if (pkg->section) {
+               fprintf(out_file, "Section: %s\n", pkg->section);
+       }
+       if (pkg->section) {
+               fprintf(out_file, "Installed-Size: %s\n", pkg->installed_size);
+       }
+       if (pkg->maintainer) {
+               fprintf(out_file, "Maintainer: %s\n", pkg->maintainer);
+       }
+       if (pkg->source) {
+               fprintf(out_file, "Source: %s\n", pkg->source);
+       }
+       if (pkg->version) {
+               fprintf(out_file, "Version: %s\n", pkg->version);
+       }
+       if (pkg->pre_depends) {
+               fprintf(out_file, "Pre-depends: %s\n", pkg->pre_depends);
+       }
+       if (pkg->replaces) {
+               fprintf(out_file, "Replaces: %s\n", pkg->replaces);
+       }
+       if (pkg->recommends) {
+               fprintf(out_file, "Recommends: %s\n", pkg->recommends);
+       }
+       if (pkg->suggests) {
+               fprintf(out_file, "Suggests: %s\n", pkg->suggests);
+       }
+       if (pkg->conflicts) {
+               fprintf(out_file, "Conflicts: %s\n", pkg->conflicts);
+       }
+       if (pkg->conffiles) {
+               fprintf(out_file, "Conf-files: %s\n", pkg->conffiles);
+       }
+       if (pkg->architecture) {
+               fprintf(out_file, "Architecture: %s\n", pkg->architecture);
+       }
+       if (pkg->filename) {
+               fprintf(out_file, "Filename: %s\n", pkg->filename);
+       }
+       if (pkg->md5sum) {
+               fprintf(out_file, "MD5sum: %s\n", pkg->md5sum);
+       }
+       if (pkg->installer_menu_item) {
+               fprintf(out_file, "installer-main-menu %d\n", pkg->installer_menu_item);
+       }
+       if (pkg->description) {
+               fprintf(out_file, "Description: %s\n", pkg->description);
+       }
+       fputc('\n', out_file);
+       pkg = pkg->next;
 }
 
 static void *status_read(void)
@@ -381,20 +476,19 @@ static void *status_read(void)
        FILE *f;
        void *status = 0;
        package_t *m = 0, *p = 0, *t = 0;
-
-       if ((f = fopen(statusfile, "r")) == NULL) {
-               perror(statusfile);
-               return 0;
-       }
+       char *package_control_buffer = NULL;
 
        if (getenv(udpkg_quiet) == NULL) {
                printf("(Reading database...)\n");
        }
 
-       while (!feof(f)) {
-               m = (package_t *)xmalloc(sizeof(package_t));
-               memset(m, 0, sizeof(package_t));
-               control_read(f, m);
+       if ((f = wfopen(statusfile, "r")) == NULL) {
+               return(NULL);
+       }
+
+       while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) {
+               m = (package_t *)xcalloc(1, sizeof(package_t));
+               fill_package_struct(m, package_control_buffer);
                if (m->package) {
                        /*
                         * If there is an item in the tree by this name,
@@ -409,25 +503,24 @@ static void *status_read(void)
                                 * of a pseudo package into the status
                                 * binary-tree.
                                 */
-                               p = (package_t *)xmalloc(sizeof(package_t));
-                               memset(p, 0, sizeof(package_t));
-                               p->package = strdup(m->provides);
-
+                               p = (package_t *)xcalloc(1, sizeof(package_t));
+                               p->package = xstrdup(m->provides);
                                t = *(package_t **)tsearch(p, &status, package_compare);
                                if (t != p) {
                                        free(p->package);
                                        free(p);
-                               }
-                               else {
+                               } else {
                                        /*
                                         * Pseudo package status is the
                                         * same as the status of the
                                         * package providing it 
                                         * FIXME: (not quite right, if 2
                                         * packages of different statuses
-                                        * provide it).
-                                        */
-                                       t->status = m->status;
+                                        * provide it).
+                                        */
+                                       t->state_want = m->state_want;
+                                       t->state_flag = m->state_flag;
+                                       t->state_status = m->state_status;
                                }
                        }
                }
@@ -442,84 +535,83 @@ static void *status_read(void)
 static int status_merge(void *status, package_t *pkgs)
 {
        FILE *fin, *fout;
-       char *line;
+       char *line = NULL;
        package_t *pkg = 0, *statpkg = 0;
        package_t locpkg;
-       int r = 0;
 
-       if ((fin = fopen(statusfile, "r")) == NULL) {
-               perror(statusfile);
-               return 0;
-       }
-       if ((fout = fopen(new_statusfile, "w")) == NULL) {
-               perror(new_statusfile);
+       if ((fout = wfopen(new_statusfile, "w")) == NULL) {
                return 0;
        }
        if (getenv(udpkg_quiet) == NULL) {
                printf("(Updating database...)\n");
        }
 
-       while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
-               line[strlen(line)] = 0; /* trim newline */
-               /* If we see a package header, find out if it's a package
-                * that we have processed. if so, we skip that block for
-                * now (write it at the end).
-                *
-                * we also look at packages in the status cache and update
-                * their status fields
-                */
-               if (strstr(line, "Package: ") == line) {
-                       for (pkg = pkgs; pkg != 0 && strncmp(line + 9,
-                                       pkg->package, strlen(line) - 9) != 0;
-                            pkg = pkg->next) ;
-
-                       locpkg.package = line + 9;
-                       statpkg = tfind(&locpkg, &status, package_compare);
-                       
-                       /* note: statpkg should be non-zero, unless the status
-                        * file was changed while we are processing (no locking
-                        * is currently done...
-                        */
-                       if (statpkg != 0) {
-                               statpkg = *(package_t **)statpkg;
+       /*
+        * Dont use wfopen here, handle errors ourself
+        */
+       if ((fin = fopen(statusfile, "r")) != NULL) {
+               while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
+                       chomp(line); /* trim newline */
+                       /* If we see a package header, find out if it's a package
+                        * that we have processed. if so, we skip that block for
+                        * now (write it at the end).
+                        *
+                        * we also look at packages in the status cache and update
+                        * their status fields
+                        */
+                       if (strstr(line, "Package: ") == line) {
+                               for (pkg = pkgs; pkg != 0 && strcmp(line + 9,
+                                               pkg->package) != 0; pkg = pkg->next) ;
+
+                               locpkg.package = line + 9;
+                               statpkg = tfind(&locpkg, &status, package_compare);
+       
+                               /* note: statpkg should be non-zero, unless the status
+                                * file was changed while we are processing (no locking
+                                * is currently done...
+                                */
+                               if (statpkg != 0) {
+                                       statpkg = *(package_t **)statpkg;
+                               }
                        }
+                       if (pkg != 0) {
+                               continue;
+                       }
+                       if (strstr(line, "Status: ") == line && statpkg != 0) {
+                               snprintf(line, sizeof(line), "Status: %s %s %s",
+                                       state_words_want[statpkg->state_want - 1], 
+                                       state_words_flag[statpkg->state_flag - 1], 
+                                       state_words_status[statpkg->state_status - 1]);
+                       }
+                       fprintf(fout, "%s\n", line);
                }
-               if (pkg != 0) {
-                       continue;
-               }
-               if (strstr(line, "Status: ") == line && statpkg != 0) {
-                       snprintf(line, sizeof(line), "Status: %s",
-                               status_print(statpkg->status));
-               }
-               fputs(line, fout);
-               fputc('\n', fout);
+               fclose(fin);
        }
        free(line);
 
        // Print out packages we processed.
        for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
-               fprintf(fout, "Package: %s\nStatus: %s\n", 
-                       pkg->package, status_print(pkg->status));
-               if (pkg->depends)
-                       fprintf(fout, "Depends: %s\n", pkg->depends);
-               if (pkg->provides)
-                       fprintf(fout, "Provides: %s\n", pkg->provides);
-               if (pkg->installer_menu_item)
-                       fprintf(fout, "installer-menu-item: %i\n", pkg->installer_menu_item);
-               if (pkg->description)
-                       fprintf(fout, "Description: %s\n", pkg->description);
-               fputc('\n', fout);
+               write_package(fout, pkg);
        }
-       
-       fclose(fin);
        fclose(fout);
 
-       r = rename(statusfile, bak_statusfile);
-       if (r == 0) {
-               r = rename(new_statusfile, statusfile);
+       /*
+        * Its ok if renaming statusfile fails becasue it doesnt exist
+        */
+       if (rename(statusfile, bak_statusfile) == -1) {
+               struct stat stat_buf;   
+               if (stat(statusfile, &stat_buf) == 0) {
+                       error_msg("Couldnt create backup status file");
+                       return(EXIT_FAILURE);
+               }
+               error_msg("No status file found, creating new one");
        }
 
-       return 0;
+       if (rename(new_statusfile, statusfile) == -1) {
+               error_msg("Couldnt create status file");
+               return(EXIT_FAILURE);
+       }
+       return(EXIT_SUCCESS);
 }
 
 static int is_file(const char *fn)
@@ -539,175 +631,100 @@ static int dpkg_doconfigure(package_t *pkg)
        char buf[1024];
 
        DPRINTF("Configuring %s\n", pkg->package);
-       pkg->status &= status_statusmask;
+       pkg->state_status = 0;
        snprintf(postinst, sizeof(postinst), "%s%s.postinst", infodir, pkg->package);
 
        if (is_file(postinst)) {
                snprintf(buf, sizeof(buf), "%s configure", postinst);
                if ((r = do_system(buf)) != 0) {
-                       fprintf(stderr, "postinst exited with status %d\n", r);
-                       pkg->status |= status_statushalfconfigured;
+                       error_msg("postinst exited with status %d\n", r);
+                       pkg->state_status = state_status_halfconfigured;
                        return 1;
                }
        }
-       pkg->status |= status_statusinstalled;
+       pkg->state_status = state_status_installed;
        
        return 0;
 }
 
 static int dpkg_dounpack(package_t *pkg)
 {
-       int r = 0, i;
-       char *cwd, *p;
-       FILE *infp, *outfp;
-       char buf[1024], buf2[1024];
-       char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
-                       "conffiles", "md5sums", "shlibs", "templates" };
+       FILE *out_stream;
+       char *info_prefix;
+       int status = TRUE;
+       int r = 0;
 
        DPRINTF("Unpacking %s\n", pkg->package);
 
-       cwd = getcwd(0, 0);
-       chdir("/");
-       snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -xf -", pkg->file);
-       if (SYSTEM(buf) != 0) {
-               goto end;
-       }
-       /* Installs the package scripts into the info directory */
-       for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
-               snprintf(buf, sizeof(buf), "%s%s/%s",
-                       dpkgcidir, pkg->package, adminscripts[i]);
-               snprintf(buf2, sizeof(buf), "%s%s.%s", 
-                       infodir, pkg->package, adminscripts[i]);
-               if (copy_file(buf, buf2, TRUE, FALSE, FALSE) < 0) {
-                       fprintf(stderr, "Cannot copy %s to %s: %s\n", 
-                               buf, buf2, strerror(errno));
-                       r = 1;
-                       break;
-               }
-               /* ugly hack to create the list file; should
-                * probably do something more elegant
-                *
-                * why oh why does dpkg create the list file
-                * so oddly...
-                */
-               snprintf(buf, sizeof(buf), "ar -p %s data.tar.gz|zcat|tar -tf -", pkg->file);
-               snprintf(buf2, sizeof(buf2), "%s%s.list", infodir, pkg->package);
-               if ((infp = popen(buf, "r")) == NULL || (outfp = fopen(buf2, "w")) == NULL) {
-                       fprintf(stderr, "Cannot create %s\n", buf2);
-                       r = 1;
-                       break;
-               }
-               while (fgets(buf, sizeof(buf), infp) && !feof(infp)) {
-                       p = buf;
-                       if (*p == '.') {
-                               p++;
-                       }
-                       if ((*p == '/') && (*(p + 1) == '\n')) {
-                               *(p + 1) = '.';
-                               *(p + 2) = '\n';
-                               *(p + 3) = 0;
-                       }
-                       if (p[strlen(p) - 2] == '/') {
-                               p[strlen(p) - 2] = '\n';
-                               p[strlen(p) - 1] = 0;
-                       }
-                       fputs(p, outfp);
-               }
-               fclose(infp);
-               fclose(outfp);
-       }
-
-       pkg->status &= status_wantmask;
-       pkg->status |= status_wantinstall;
-       pkg->status &= status_flagmask;
-       pkg->status |= status_flagok;
-       pkg->status &= status_statusmask;
-
-       if (r == 0) {
-               pkg->status |= status_statusunpacked;
+       /* extract the data file */
+       deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", NULL);
+
+       /* extract the control files */
+       info_prefix = (char *) malloc(strlen(pkg->package) + strlen(infodir) + 2 + 5 + 1);
+       sprintf(info_prefix, "%s/%s.", infodir, pkg->package);
+       deb_extract(pkg->package, stdout, (extract_control_tar_gz | extract_all_to_fs), info_prefix, NULL);
+
+       /* Create the list file */
+       strcat(info_prefix, "list");
+       out_stream = wfopen(info_prefix, "w");                  
+       deb_extract(pkg->package, out_stream, (extract_data_tar_gz | extract_list), NULL, NULL);
+       fclose(out_stream);
+
+       pkg->state_want = state_want_install;
+       pkg->state_flag = state_flag_ok;
+       if (status == TRUE) {
+               pkg->state_status = state_status_unpacked;
        } else {
-               pkg->status |= status_statushalfinstalled;
+               pkg->state_status = state_status_halfinstalled;
        }
-end:
-       chdir(cwd);
-       return r;
-}
 
-static int dpkg_doinstall(package_t *pkg)
-{
-       DPRINTF("Installing %s\n", pkg->package);
-       return (dpkg_dounpack(pkg) || dpkg_doconfigure(pkg));
+       return r;
 }
 
-static int dpkg_unpackcontrol(package_t *pkg)
+/*
+ * Extract and parse the control file from control.tar.gz
+ */
+static int dpkg_read_control(package_t *pkg)
 {
-       int r = 1;
-       char *cwd = 0;
-       char *p;
-       char buf[1024];
-       FILE *f;
+       FILE *pkg_file;
+       char *control_buffer = NULL;
 
-       p = strrchr(pkg->file, '/');
-       if (p) p++; else p = pkg->file;
-       p = pkg->package = strdup(p);
-       while (*p != 0 && *p != '_' && *p != '.') {
-               p++;
+       if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
+               return EXIT_FAILURE;
        }
-       *p = 0;
-
-       cwd = getcwd(0, 0);
-       snprintf(buf, sizeof(buf), "%s%s", dpkgcidir, pkg->package);
-       DPRINTF("dir = %s\n", buf);
-
-       if (mkdir(buf, S_IRWXU) == 0 && chdir(buf) == 0) {
-               snprintf(buf, sizeof(buf), "ar -p %s control.tar.gz|zcat|tar -xf -", pkg->file);
-               if (SYSTEM(buf) == 0) {
-                       if ((f = fopen("control", "r")) != NULL) {
-                               control_read(f, pkg);
-                               r = 0;
-                       }
-               }
-       }
-       chdir(cwd);
-       free(cwd);
-
-       return r;
+       control_buffer = deb_extract(pkg->filename, stdout, (extract_control_tar_gz | extract_one_to_buffer), NULL, "./control");
+       fill_package_struct(pkg, control_buffer);
+       return EXIT_SUCCESS;
 }
 
-static int dpkg_unpack(package_t *pkgs)
+static int dpkg_unpack(package_t *pkgs, void *status)
 {
        int r = 0;
        package_t *pkg;
-       void *status = status_read();
 
-       if (SYSTEM(rm_dpkgcidir) != 0 ||
-           mkdir(dpkgcidir, S_IRWXU) != 0) {
-               perror("mkdir");
-               return 1;
-       }
        for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
-               dpkg_unpackcontrol(pkg);
-               r = dpkg_dounpack(pkg);
-               if (r != 0) break;
+               dpkg_read_control(pkg);
+               if ((r = dpkg_dounpack(pkg)) != 0 ) {
+                       break;
+               }
        }
        status_merge(status, pkgs);
-       SYSTEM(rm_dpkgcidir);
 
        return r;
 }
 
-static int dpkg_configure(package_t *pkgs)
+static int dpkg_configure(package_t *pkgs, void *status)
 {
        int r = 0;
        void *found;
        package_t *pkg;
-       void *status = status_read();
 
        for (pkg = pkgs; pkg != 0 && r == 0; pkg = pkg->next) {
                found = tfind(pkg, &status, package_compare);
 
                if (found == 0) {
-                       fprintf(stderr, "Trying to configure %s, but it is not installed\n", pkg->package);
+                       error_msg("Trying to configure %s, but it is not installed", pkg->package);
                        r = 1;
                } 
                /* configure the package listed in the status file;
@@ -722,25 +739,15 @@ static int dpkg_configure(package_t *pkgs)
        return r;
 }
 
-static int dpkg_install(package_t *pkgs)
+static int dpkg_install(package_t *pkgs, void *status)
 {
        package_t *p, *ordered = 0;
-       void *status = status_read();
 
-       if (SYSTEM(rm_dpkgcidir) != 0 ||
-           mkdir(dpkgcidir, S_IRWXU) != 0) {
-               perror("mkdir");
-               return 1;
-       }
-       
        /* Stage 1: parse all the control information */
-       for (p = pkgs; p != 0; p = p->next)
-               if (dpkg_unpackcontrol(p) != 0) {
-                       perror(p->file);
-                       /* force loop break, and prevents further ops */
-                       pkgs = 0;
-               }
-       
+       for (p = pkgs; p != 0; p = p->next) {
+               dpkg_read_control(p);
+       }
+
        /* Stage 2: resolve dependencies */
 #ifdef DODEPENDS
        ordered = depends_resolve(pkgs, status);
@@ -750,32 +757,36 @@ static int dpkg_install(package_t *pkgs)
        
        /* Stage 3: install */
        for (p = ordered; p != 0; p = p->next) {
-               p->status &= status_wantmask;
-               p->status |= status_wantinstall;
+               p->state_want = state_want_install;
 
                /* for now the flag is always set to ok... this is probably
                 * not what we want
                 */
-               p->status &= status_flagmask;
-               p->status |= status_flagok;
+               p->state_flag = state_flag_ok;
+
+               DPRINTF("Installing %s\n", p->package);
+               if (dpkg_dounpack(p) != 0) {
+                       perror_msg(p->filename);
+               }
 
-               if (dpkg_doinstall(p) != 0) {
-                       perror(p->file);
+               if (dpkg_doconfigure(p) != 0) {
+                       perror_msg(p->filename);
                }
        }
-       
+
        if (ordered != 0) {
                status_merge(status, pkgs);
        }
-       SYSTEM(rm_dpkgcidir);
 
        return 0;
 }
 
-static int dpkg_remove(package_t *pkgs)
+/*
+ * Not implemented yet
+ *
+static int dpkg_remove(package_t *pkgs, void *status)
 {
        package_t *p;
-       void *status = status_read();
 
        for (p = pkgs; p != 0; p = p->next)
        {
@@ -784,52 +795,60 @@ static int dpkg_remove(package_t *pkgs)
 
        return 0;
 }
+*/
 
 extern int dpkg_main(int argc, char **argv)
 {
-       char opt = 0;
-       char *s;
+       const int arg_install = 1;
+       const int arg_unpack = 2;
+       const int arg_configure = 4;
+
        package_t *p, *packages = NULL;
-       char *cwd = getcwd(0, 0);
-
-       while (*++argv) {
-               if (**argv == '-') {
-                       /* Nasty little hack to "parse" long options. */
-                       s = *argv;
-                       while (*s == '-') {
-                               s++;
-                       }
-                       opt=s[0];
+       void *status = NULL;
+       int opt = 0;
+       int optflag = 0;
+
+       while ((opt = getopt(argc, argv, "iruc")) != -1) {
+               switch (opt) {
+                       case 'i':
+                               optflag |= arg_install;
+                               break;
+                       case 'u':
+                               optflag |= arg_unpack;
+                               break;
+                       case 'c':
+                               optflag |= arg_configure;
+                               break;
+                       default:
+                               show_usage();
+               }
+       }
+
+       while (optind < argc) {
+               p = (package_t *) xcalloc(1, sizeof(package_t));
+               if (optflag & arg_configure) {
+                       p->package = xstrdup(argv[optind]);
                } else {
-                       p = (package_t *)xmalloc(sizeof(package_t));
-                       memset(p, 0, sizeof(package_t));
-
-                       if (**argv == '/') {
-                               p->file = *argv;
-                       } else
-                               if (opt != 'c') {
-                                       p->file = xmalloc(strlen(cwd) + strlen(*argv) + 2);
-                                       sprintf(p->file, "%s/%s", cwd, *argv);
-                       } else {
-                               p->package = strdup(*argv);
-                       }
+                       p->filename = xstrdup(argv[optind]);
+               }
+               p->next = packages;
+               packages = p;
+
+               optind++;
+       }
+
+       make_directory((char *)infodir, S_IRWXU, FILEUTILS_RECUR);
 
-                       p->next = packages;
-                       packages = p;
-               }               
-       }
-
-       switch (opt) {
-               case 'i':
-                       return dpkg_install(packages);
-               case 'r':
-                       return dpkg_remove(packages);
-               case 'u':
-                       return dpkg_unpack(packages);
-               case 'c':
-                       return dpkg_configure(packages);
-               default :       
-                       usage(dpkg_usage);
-                       return 0;
-       }
-}
\ No newline at end of file
+       status = status_read();
+
+       if (optflag & arg_install) {
+               return dpkg_install(packages, status);
+       }
+       else if (optflag & arg_unpack) {
+               return dpkg_unpack(packages, status);
+       }
+       else if (optflag & arg_configure) {
+               return dpkg_configure(packages, status);
+       }
+       return(EXIT_FAILURE);
+}