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 0d2b2dc3c514264855b26b0fd5835ead8ae389fa..01ddc7b7b77a867aa9de1d6cef97e161e4a935e2 100644 (file)
--- a/dpkg.c
+++ b/dpkg.c
 
 #include "busybox.h"
 
-
 #define DEPENDSMAX     64      /* maximum number of depends we can handle */
 
 /* Should we do full dependency checking? */
-#define DODEPENDS 1
+//#define DODEPENDS 0
 
 /* Should we do debugging? */
-#define DODEBUG 0
+//#define DODEBUG 0
 
 #ifdef DODEBUG
 #define SYSTEM(x) do_system(x)
@@ -116,37 +115,6 @@ static int package_compare(const void *p1, const void *p2)
                ((package_t *)p2)->package);
 }
 
-/*
- * NOTE: this was handled by a "rm -rf" shell command
- * Maybe theis behaviour should be integrated into the rm applet
- * (i dont appreciate the rm applets recursive action fn)-bug1
- */
-static int remove_dir(const char *dirname)
-{
-       struct dirent *fp;
-       DIR *dp = opendir(dirname);
-       while ((fp = readdir(dp)) != NULL) {
-               struct stat statbuf;
-               char *filename;
-
-               filename = (char *) xcalloc(1, strlen(dirname) + strlen(fp->d_name) + 2);
-               strcpy(filename, dirname);
-               strcat(filename, fp->d_name);
-               lstat(filename, &statbuf);
-
-               if ((strcmp(fp->d_name, ".") != 0) && (strcmp(fp->d_name, "..") != 0)) {
-                       if (S_ISDIR(statbuf.st_mode)) {
-                               remove_dir(strcat(filename, "/"));
-                       }
-                       else if (remove(filename) == -1) {
-                               perror_msg(filename);
-                       }
-               }
-       }
-       remove(dirname);
-       return EXIT_SUCCESS;
-}
-
 #ifdef DODEPENDS
 #include <ctype.h>
 
@@ -341,6 +309,7 @@ static int fill_package_struct(package_t *package, const char *package_buffer)
        char *field = NULL;
        int field_start = 0;
        int field_length = 0;
+
        while ((field = read_package_field(&package_buffer[field_start])) != NULL) {
                field_length = strlen(field);
                field_start += (field_length + 1);
@@ -430,6 +399,78 @@ static int fill_package_struct(package_t *package, const char *package_buffer)
        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)
 {
        FILE *f;
@@ -445,11 +486,9 @@ static void *status_read(void)
                return(NULL);
        }
 
-       while ( (package_control_buffer = read_text_file_to_buffer(f)) != NULL) {
+       while ( (package_control_buffer = fgets_str(f, "\n\n")) != NULL) {
                m = (package_t *)xcalloc(1, sizeof(package_t));
-               printf("read buffer [%s]\n", package_control_buffer);
                fill_package_struct(m, package_control_buffer);
-               printf("package is [%s]\n", m->package);
                if (m->package) {
                        /*
                         * If there is an item in the tree by this name,
@@ -489,7 +528,6 @@ static void *status_read(void)
                        free(m);
                }
        }
-       printf("done\n");
        fclose(f);
        return status;
 }
@@ -513,7 +551,7 @@ static int status_merge(void *status, package_t *pkgs)
         */
        if ((fin = fopen(statusfile, "r")) != NULL) {
                while (((line = get_line_from_file(fin)) != NULL) && !feof(fin)) { 
-                       line[strlen(line) - 1] = '\0'; /* trim newline */
+                       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).
@@ -553,20 +591,7 @@ static int status_merge(void *status, package_t *pkgs)
 
        // Print out packages we processed.
        for (pkg = pkgs; pkg != 0; pkg = pkg->next) {
-               fprintf(fout, "Package: %s\nStatus: %s %s %s\n", 
-                       pkg->package, 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(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(fout);
 
@@ -624,24 +649,26 @@ static int dpkg_doconfigure(package_t *pkg)
 
 static int dpkg_dounpack(package_t *pkg)
 {
-       int r = 0;
+       FILE *out_stream;
+       char *info_prefix;
        int status = TRUE;
-       char *lst_path;
+       int r = 0;
 
        DPRINTF("Unpacking %s\n", pkg->package);
 
        /* extract the data file */
-       deb_extract(pkg->filename, extract_extract, "/", NULL);
+       deb_extract(pkg->filename, stdout, (extract_data_tar_gz | extract_all_to_fs), "/", NULL);
 
        /* extract the control files */
-       deb_extract(pkg->filename, extract_control, infodir, pkg->package);
+       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 */
-       lst_path = xmalloc(strlen(infodir) + strlen(pkg->package) + 6);
-       strcpy(lst_path, infodir);
-       strcat(lst_path, pkg->package);
-       strcat(lst_path, ".list");
-       deb_extract(pkg->filename, extract_contents_to_file, lst_path, NULL);
+       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;
@@ -666,7 +693,7 @@ static int dpkg_read_control(package_t *pkg)
        if ((pkg_file = wfopen(pkg->filename, "r")) == NULL) {
                return EXIT_FAILURE;
        }
-       control_buffer = deb_extract(pkg->filename, extract_field, NULL, NULL);
+       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;
 }
@@ -778,7 +805,7 @@ extern int dpkg_main(int argc, char **argv)
 
        package_t *p, *packages = NULL;
        void *status = NULL;
-       char opt = 0;
+       int opt = 0;
        int optflag = 0;
 
        while ((opt = getopt(argc, argv, "iruc")) != -1) {
@@ -810,7 +837,7 @@ extern int dpkg_main(int argc, char **argv)
                optind++;
        }
 
-       create_path(infodir, S_IRWXU);
+       make_directory((char *)infodir, S_IRWXU, FILEUTILS_RECUR);
 
        status = status_read();