blob: f0d4b13593ac7133c4e491a319998ff20394364b [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];
31
Glenn L McGrath237ae422002-11-03 14:05:15 +000032 /* Cant lseek over pipe's */
33 archive_handle->seek = seek_by_char;
34
35 archive_xread_all(archive_handle, &magic, 2);
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000036 if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
37 error_msg_and_die("Invalid gzip magic");
38 }
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000039
40 check_header_gzip(archive_handle->src_fd);
41
42 if (pipe(fd_pipe) != 0) {
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000043 error_msg_and_die("Can't create pipe");
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000044 }
45
46 pid = fork();
47 if (pid == -1) {
48 error_msg_and_die("Fork failed\n");
49 }
50
51 if (pid == 0) {
52 /* child process */
53 close(fd_pipe[0]); /* We don't wan't to read from the pipe */
54 inflate(archive_handle->src_fd, fd_pipe[1]);
55 check_trailer_gzip(archive_handle->src_fd);
56 close(fd_pipe[1]); /* Send EOF */
57 exit(0);
58 /* notreached */
59 }
60 /* parent process */
61 close(fd_pipe[1]); /* Don't want to write down the pipe */
62 close(archive_handle->src_fd);
63
64 archive_handle->src_fd = fd_pipe[0];
65
Glenn L McGrath2e41d0c2002-09-27 06:46:02 +000066 archive_handle->offset = 0;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +000067 while (get_header_tar(archive_handle) == EXIT_SUCCESS);
68
69 if (kill(pid, SIGTERM) == -1) {
70 error_msg_and_die("Couldnt kill gunzip process");
71 }
72
73 /* I dont think this is needed */
74#if 0
75 if (waitpid(pid, NULL, 0) == -1) {
76 error_msg("Couldnt wait ?");
77 }
78#endif
79
80 /* Can only do one file at a time */
81 return(EXIT_FAILURE);
82}
83