blob: f77b775d32ea5a50b6e44db455913f53a712003b [file] [log] [blame]
Glenn L McGrath7ca04f32002-09-25 02:47:48 +00001/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 */
16
17#include <sys/types.h>
18#include <signal.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23#include "libbb.h"
24#include "unarchive.h"
25
26extern char get_header_tar_gz(archive_handle_t *archive_handle)
27{
28 int fd_pipe[2];
29 int pid;
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000030 unsigned char magic[2];
Glenn L McGrathe3568832002-11-13 00:24:20 +000031
Glenn L McGrath237ae422002-11-03 14:05:15 +000032 /* Cant lseek over pipe's */
Glenn L McGrathe3568832002-11-13 00:24:20 +000033 archive_handle->read = read;
Glenn L McGrath237ae422002-11-03 14:05:15 +000034 archive_handle->seek = seek_by_char;
35
36 archive_xread_all(archive_handle, &magic, 2);
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000037 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
38 error_msg_and_die("Invalid gzip magic");
39 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000040
41 check_header_gzip(archive_handle->src_fd);
42
43 if (pipe(fd_pipe) != 0) {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000044 error_msg_and_die("Can't create pipe");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000045 }
46
47 pid = fork();
48 if (pid == -1) {
49 error_msg_and_die("Fork failed\n");
50 }
51
52 if (pid == 0) {
53 /* child process */
54 close(fd_pipe[0]); /* We don't wan't to read from the pipe */
55 inflate(archive_handle->src_fd, fd_pipe[1]);
56 check_trailer_gzip(archive_handle->src_fd);
57 close(fd_pipe[1]); /* Send EOF */
58 exit(0);
59 /* notreached */
60 }
61 /* parent process */
62 close(fd_pipe[1]); /* Don't want to write down the pipe */
63 close(archive_handle->src_fd);
64
65 archive_handle->src_fd = fd_pipe[0];
66
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000067 archive_handle->offset = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000068 while (get_header_tar(archive_handle) == EXIT_SUCCESS);
69
70 if (kill(pid, SIGTERM) == -1) {
71 error_msg_and_die("Couldnt kill gunzip process");
72 }
73
74 /* I dont think this is needed */
75#if 0
76 if (waitpid(pid, NULL, 0) == -1) {
77 error_msg("Couldnt wait ?");
78 }
79#endif
80
81 /* Can only do one file at a time */
82 return(EXIT_FAILURE);
83}
84