Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | extern char get_header_tar_gz(archive_handle_t *archive_handle) |
| 27 | { |
| 28 | int fd_pipe[2]; |
| 29 | int pid; |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 30 | unsigned char magic[2]; |
Glenn L McGrath | e356883 | 2002-11-13 00:24:20 +0000 | [diff] [blame^] | 31 | |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 32 | /* Cant lseek over pipe's */ |
Glenn L McGrath | e356883 | 2002-11-13 00:24:20 +0000 | [diff] [blame^] | 33 | archive_handle->read = read; |
Glenn L McGrath | 237ae42 | 2002-11-03 14:05:15 +0000 | [diff] [blame] | 34 | archive_handle->seek = seek_by_char; |
| 35 | |
| 36 | archive_xread_all(archive_handle, &magic, 2); |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 37 | if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) { |
| 38 | error_msg_and_die("Invalid gzip magic"); |
| 39 | } |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 40 | |
| 41 | check_header_gzip(archive_handle->src_fd); |
| 42 | |
| 43 | if (pipe(fd_pipe) != 0) { |
Glenn L McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 44 | error_msg_and_die("Can't create pipe"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 45 | } |
| 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 McGrath | 2e41d0c | 2002-09-27 06:46:02 +0000 | [diff] [blame] | 67 | archive_handle->offset = 0; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 68 | 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 | |