extractcontrol.c 3.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
/*  This file is part of "mirrorer" (TODO: find better title)
 *  Copyright (C) 2003 Bernhard R. Link
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include <config.h>

#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <string.h>
#include <malloc.h>
#include <zlib.h>
#include "error.h"
#include "md5sum.h"
#include "chunks.h"
#include "extractcontrol.h"

extern int verbose;

//**********************************************************************
//* This is simply a ugly prototype until I have time to get something
//* correct (perhaps borrowing code from dpkg2.0). Until then its just
//* a quick and dirty hack to make it running.
//**********************************************************************
//TODO: write this properly.


retvalue extractcontrol(char **control,const char *debfile) {
	int pipe1[2];
	int pipe2[2];
	int ret;
	pid_t ar,tar,pid;
	int status;
	gzFile f;

	retvalue result;

	result = RET_OK;

	ret = pipe(pipe1);
	if( ret < 0 ) {
		return RET_ERRNO(errno);
	}

	ret = pipe(pipe2);
	if( ret < 0 ) {
		close(pipe1[0]);close(pipe1[1]);
		return RET_ERRNO(errno);
	}
	
	ar = fork();
	if( ar < 0 ) {
		result = RET_ERRNO(errno);
		close(pipe1[0]);close(pipe1[1]);
		close(pipe2[0]);close(pipe2[1]);
		return result;
	}

	if( ar == 0 ) {
		/* calling ar */
		if( dup2(pipe1[1],1) < 0 )
			exit(255);
		close(pipe1[0]);close(pipe1[1]);
		close(pipe2[0]);close(pipe2[1]);
		//TODO without explicit path
		ret = execl("/usr/bin/ar","ar","p",debfile,"control.tar.gz",NULL);
		fprintf(stderr,"calling ar failed: %m\n");
		exit(254);
	}
//	fprintf(stderr,"ar is %d\n",ar);

	tar = fork();
	if( tar < 0 ) {
		result = RET_ERRNO(errno);
		close(pipe1[0]);close(pipe1[1]);
		close(pipe2[0]);close(pipe2[1]);
		tar = -1;
	} else if( tar == 0 ) {
		/* calling tar */
		if( dup2(pipe1[0],0) < 0 )
			exit(255);
		if( dup2(pipe2[1],1) < 0 )
			exit(255);
		close(pipe1[0]);close(pipe1[1]);
		close(pipe2[0]);close(pipe2[1]);
		//TODO without explicit path
		execl("/bin/tar","tar","-xOzf","-","./control",NULL);
		fprintf(stderr,"calling tar failed: %m\n");
		exit(254);
		
	}
//	fprintf(stderr,"tar is %d\n",tar);

	close(pipe1[0]);close(pipe1[1]);
	close(pipe2[1]);

114 115 116 117 118 119
	// read data:
	//TODO: check return values...
	f = gzdopen(pipe2[0],"r");
	*control = chunk_read(f);
	gzclose(f);

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	while( ar != -1 || tar != -1 ) {
		pid=wait(&status);
		if( pid < 0 ) {
			if( errno != EINTR )
				RET_UPDATE(result,RET_ERRNO(errno));
		} else {
			if( pid == ar ) {
				ar = -1;
				if( !WIFEXITED(status) || WEXITSTATUS(status)) {
					fprintf(stderr,"Error from ar: %d\n",WEXITSTATUS(status));
					result = RET_ERROR;
				}
			} else if( pid == tar ) {
				tar = -1;
				if( !WIFEXITED(status) || WEXITSTATUS(status)) {
					fprintf(stderr,"Error from tar: %d\n",WEXITSTATUS(status));
					result = RET_ERROR;
				}
			} else {
				// WTH?
				fprintf(stderr,"Who is %d, and why does this bother me?\n",pid);
			}
		}
		
	}
	return result;
}