Fix #define syntax
[oweals/busybox.git] / archival / dpkg_deb.c
index d088828857b86730f1ebf9233374300f93a9a8fc..06a810cc1cf6f820feb907f0bd53a3d295930186 100644 (file)
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
-
+#include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <getopt.h>
+#include "unarchive.h"
 #include "busybox.h"
 
 extern int dpkg_deb_main(int argc, char **argv)
 {
-       char *argument = NULL;
-       char *output_buffer = NULL;
+       archive_handle_t *ar_archive;
+       archive_handle_t *tar_gz_archive;
        int opt = 0;
-       int optflag = 0;        
+#ifndef CONFIG_FEATURE_DPKG_DEB_EXTRACT_ONLY
+       const llist_t *control_tar_gz_llist = add_to_list(NULL, "control.tar.gz");
+#endif
+#ifndef CONFIG_AR
+       char magic[7];
+#endif
+       
+       /* a .deb file is an ar archive that contain three files,
+        * data.tar.gz, control.tar.gz and debian
+        */
        
-       while ((opt = getopt(argc, argv, "ceftXxI")) != -1) {
+       /* Setup the tar archive handle */
+       tar_gz_archive = init_handle();
+
+       /* Setup an ar archive handle that refers to the gzip sub archive */    
+       ar_archive = init_handle();
+       ar_archive->action_data_subarchive = get_header_tar_gz;
+       ar_archive->sub_archive = tar_gz_archive;
+       ar_archive->filter = filter_accept_list;
+       ar_archive->accept = add_to_list(NULL, "data.tar.gz");
+
+#ifndef CONFIG_FEATURE_DPKG_DEB_EXTRACT_ONLY
+       while ((opt = getopt(argc, argv, "cefXx")) != -1) {
+#else
+       while ((opt = getopt(argc, argv, "x")) != -1) {
+#endif
                switch (opt) {
+#ifndef CONFIG_FEATURE_DPKG_DEB_EXTRACT_ONLY
                        case 'c':
-                               optflag |= extract_contents;
+                               tar_gz_archive->action_header = header_verbose_list;
                                break;
                        case 'e':
-                               optflag |= extract_control;
+                               ar_archive->accept = control_tar_gz_llist;
+                               tar_gz_archive->action_data = data_extract_all;
                                break;
                        case 'f':
-                               optflag |= extract_field;
-                               break;
-                       case 't':
-                               optflag |= extract_fsys_tarfile;
+                               /* Print the entire control file
+                                * it should accept a second argument which specifies a 
+                                * specific field to print */
+                               ar_archive->accept = control_tar_gz_llist;
+                               tar_gz_archive->accept = add_to_list(NULL, "./control");;
+                               tar_gz_archive->filter = filter_accept_list;
+                               tar_gz_archive->action_data = data_extract_to_stdout;
                                break;
                        case 'X':
-                               optflag |= extract_verbose_extract;
-                               break;
+                               tar_gz_archive->action_header = header_list;
+#endif
                        case 'x':
-                               optflag |= extract_extract;
-                               break;
-                       case 'I':
-                               optflag |= extract_info;
+                               tar_gz_archive->action_data = data_extract_all;
                                break;
                        default:
                                show_usage();
                }
        }
 
-       if (optind == argc)  {
+       if (optind + 2 < argc)  {
                show_usage();
        }
 
-       switch (optflag) {
-               case (extract_control):
-               case (extract_extract):
-               case (extract_verbose_extract):
-                       /* argument is a dir name */
-                       if ( (optind + 1) == argc ) {
-                               argument = xstrdup("DEBIAN");
-                       } else {
-                               argument = xstrdup(argv[optind + 1]);
-                       }
-                       break;
-               case (extract_field):
-                       /* argument is a control field name */
-                       if ((optind + 1) != argc) {
-                               argument = xstrdup(argv[optind + 1]);                           
-                       }
-                       break;
-               case (extract_info):
-                       /* argument is a control field name */
-                       if ((optind + 1) != argc) {
-                               argument = xstrdup(argv[optind + 1]);
-                               break;
-                       } else {
-                               error_msg("-I currently requires a filename to be specifies");
-                               return(EXIT_FAILURE);
-                       }
-                       /* argument is a filename */
-               default:
-       }
+       tar_gz_archive->src_fd = ar_archive->src_fd = xopen(argv[optind++], O_RDONLY);
 
-       output_buffer = deb_extract(argv[optind], optflag, argument, NULL);
+       /* Workout where to extract the files */
+       /* 2nd argument is a dir name */
+       mkdir(argv[optind], 0777);
+       chdir(argv[optind]);
 
-       if (optflag & extract_field) {
-               char *field = NULL;
-               int field_length = 0;
-               int field_start = 0;
-
-               while ((field = read_package_field(&output_buffer[field_start])) != NULL) {
-                       field_length = strlen(field);
-                       field_start += (field_length + 1);
-                       if (strstr(field, argument) == field) {
-                               printf("%s\n", field + strlen(argument) + 2);
-                       }
-                       free(field);
-               }
+#ifdef CONFIG_AR
+       unpack_ar_archive(ar_archive);
+#else
+       xread_all(ar_archive->src_fd, magic, 7);
+       if (strncmp(magic, "!<arch>", 7) != 0) {
+               error_msg_and_die("Invalid ar magic");
        }
+       ar_archive->offset += 7;
+
+       while (get_header_ar(ar_archive) == EXIT_SUCCESS);
+#endif
+
+       /* Cleanup */
+       close (ar_archive->src_fd);
 
        return(EXIT_SUCCESS);
 }
+