Commit 33ffca3c authored by Bernhard Link's avatar Bernhard Link

Added a extractcontrol prototype to extract the control file from a .deb

parent 9bbdcf8b
......@@ -3,8 +3,8 @@ bin_PROGRAMS = mirrorer
CPPFLAGS = @CPPFLAGS@ -D_GNU_SOURCE=1 -DPKGDATADIR=\"$(pkgdatadir)\" -Wall
mirrorer_SOURCES = main.c files.c md5.c md5sum.c dirs.c chunks.c reference.c packages.c binaries.c sources.c names.c dpkgversions.c release.c mprintf.c download.c updates.c strlist.c signature.c distribution.c
mirrorer_SOURCES = main.c files.c md5.c md5sum.c dirs.c chunks.c reference.c packages.c binaries.c sources.c names.c dpkgversions.c release.c mprintf.c download.c updates.c strlist.c signature.c distribution.c extractcontrol.c
mirrorer_LDFLAGS += -ldb3
noinst_HEADERS = md5.h md5sum.h dirs.h files.h chunks.h reference.h packages.h binaries.h sources.h names.h release.h error.h mprintf.h download.h updates.h strlist.h signature.h distribution.h
noinst_HEADERS = md5.h md5sum.h dirs.h files.h chunks.h reference.h packages.h binaries.h sources.h names.h release.h error.h mprintf.h download.h updates.h strlist.h signature.h distribution.h extractcontrol.h
MAINTAINERCLEANFILES = Makefile.in configure install-sh stamp-h.in aclocal.m4 config.h.in mkinstalldirs
/* 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 "mprintf.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]);
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);
}
}
}
if( result == RET_OK ) {
// read data;
//TODO: check return values...
f = gzdopen(pipe2[0],"r");
*control = chunk_read(f);
gzclose(f);
} else
close(pipe2[0]);
return result;
}
#ifndef __MIRRORER_EXTRACTCONTROL_H
#define __MIRRORER_EXTRACTCONTROL_H
#ifndef __MIRRORER_ERROR_H
#include "error.h"
#warning "What's hapening here?"
#endif
/* Read the control information of <debfile< and save it's only chunk
* into <control> */
retvalue extractcontrol(char **control,const char *debfile);
#endif
......@@ -38,6 +38,7 @@
#include "download.h"
#include "updates.h"
#include "signature.h"
#include "extractcontrol.h"
#ifndef STD_BASE_DIR
#define STD_BASE_DIR "/var/spool/mirrorer"
......@@ -65,6 +66,22 @@ static int printargs(int argc,char *argv[]) {
}
return 0;
}
static int extract_control(int argc,char *argv[]) {
retvalue result;
char *control;
if( argc != 2 ) {
fprintf(stderr,"mirrorer __extractcontrol <.deb-file>\n");
return 1;
}
result = extractcontrol(&control,argv[1]);
if( RET_IS_OK(result) )
printf("%s\n",control);
return EXIT_RET(result);
}
static int addmd5sums(int argc,char *argv[]) {
char buffer[2000],*c,*m;
......@@ -1351,6 +1368,7 @@ static struct action {
{"_removereferences", removereferences},
{"_addmd5sums",addmd5sums},
{"update",update},
{"__extractcontrol",extract_control},
{NULL,NULL}
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment