aptmethod.c 24.7 KB
Newer Older
Bernhard Link's avatar
Bernhard Link committed
1
/*  This file is part of "reprepro"
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *  Copyright (C) 2004 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 <sys/types.h>
Bernhard Link's avatar
Bernhard Link committed
21
#include <sys/wait.h>
22 23 24 25 26 27 28
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>
29
#include <ctype.h>
30 31 32 33 34 35 36
#include "error.h"
#include "mprintf.h"
#include "strlist.h"
#include "names.h"
#include "dirs.h"
#include "chunks.h"
#include "md5sum.h"
37
#include "files.h"
38
#include "aptmethod.h"
39 40 41 42 43 44 45 46

extern int verbose;


struct aptmethod {
	struct aptmethod *next;
	char *name;
	char *baseuri;
47
	char *config;
48 49 50 51 52
	int stdin,stdout;
	pid_t child;

	enum { ams_waitforcapabilities=0, ams_ok, ams_failed } status;
	
53 54
	struct tobedone *tobedone,*lasttobedone,*nexttosend;
	/* what is currently read: */
55
	char *inputbuffer;
56 57 58 59
	size_t input_size,alreadyread;
	/* What is currently written: */
	char *command;
	size_t alreadywritten,output_length;
60 61 62 63 64 65
};

struct aptmethodrun {
	struct aptmethod *methods;
};

66
static void aptmethod_free(struct aptmethod *method) {
67 68 69 70 71
	if( method == NULL )
		return;
	free(method->name);
	free(method->baseuri);
	free(method->inputbuffer);
72
	free(method->command);
73 74 75 76 77 78 79 80 81 82

	while( method->tobedone ) {
		struct tobedone *todo;

		todo = method->tobedone;
		method->tobedone = todo->next;

		free(todo->uri);
		free(todo->filename);
		free(todo->md5sum);
83
		free(todo->filekey);
84 85 86 87
		free(todo);
	}
}

88 89
retvalue aptmethod_shutdown(struct aptmethodrun *run) {
	struct aptmethod *method,*lastmethod;
90

91 92
	/* first get rid of everything not running: */
	method = run->methods; lastmethod = NULL;
93
	while( method ) {
94 95 96
		struct aptmethod *h;

		if( method->child > 0 ) {
97 98
			if( verbose > 5 )
				fprintf(stderr,"Still waiting for %d\n",method->child);
99 100 101 102 103 104 105 106 107 108 109 110
			lastmethod = method;
			method = method->next;
			continue;
		} else {
			h = method->next;
			if( lastmethod )
				lastmethod->next = h;
			else
				run->methods = h;
			aptmethod_free(method);
			method = h;
		}
111
	}
112 113 114

	/* finally get rid of all the processes: */
	for( method = run->methods ; method ; method = method->next ) {
115
		if( method->stdin >= 0 ) {
116
			(void)close(method->stdin);
117 118 119
			if( verbose > 30 )
				fprintf(stderr,"Closing stdin of %d\n",method->child);
		}
120
		method->stdin = -1;
121
		if( method->stdout >= 0 ) {
122
			(void)close(method->stdout);
123 124 125
			if( verbose > 30 )
				fprintf(stderr,"Closing stdout %d\n",method->child);
		}
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
		method->stdout = -1;
	}
	while( run->methods ) {
		pid_t pid;int status;
		
		pid = wait(&status);
		lastmethod = NULL; method = run->methods;
		while( method ) {
			if( method->child == pid ) {
				struct aptmethod *next = method->next;

				if( lastmethod ) {
					lastmethod->next = next;
				} else
					run->methods = next;

				aptmethod_free(method);
				method = next;
			} else {
				lastmethod = method;
				method = method->next;
			}
		}
	}
	return RET_OK;
151 152 153 154 155
}

/******************Initialize the data structures***********************/

retvalue aptmethod_initialize_run(struct aptmethodrun **run) {
156
	struct aptmethodrun *r;
157

158
	r = calloc(1,sizeof(struct aptmethodrun));
159 160 161 162 163 164 165 166
	if( r == NULL )
		return RET_ERROR_OOM;
	else {
		*run = r;
		return RET_OK;
	}
}

167
retvalue aptmethod_newmethod(struct aptmethodrun *run,const char *uri,const char *config,struct aptmethod **m) {
168 169 170
	struct aptmethod *method;
	const char *p;

171
	method = calloc(1,sizeof(struct aptmethod));
172 173 174 175 176 177 178 179 180 181 182 183 184
	if( method == NULL )
		return RET_ERROR_OOM;
	method->stdin = -1;
	method->stdout = -1;
	method->child = -1;
	method->status = ams_waitforcapabilities;
	p = uri;
	while( *p && ( *p == '_' || *p == '-' ||
		(*p>='a' && *p<='z') || (*p>='A' && *p<='Z') ||
		(*p>='0' && *p<='9') ) ) {
		p++;
	}
	if( *p == '\0' ) {
185
		fprintf(stderr,"Did not find colon in method-URI '%s'!\n",uri);
186 187 188 189
		free(method);
		return RET_ERROR;
	}
	if( *p != ':' ) {
190
		fprintf(stderr,"Unexpected character '%c' in method-URI '%s'!\n",*p,uri);
191 192 193 194
		free(method);
		return RET_ERROR;
	}
	if( p == uri ) {
195
		fprintf(stderr,"Zero-length name in method-URI '%s'!\n",uri);
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		free(method);
		return RET_ERROR;
	}

	method->name = strndup(uri,p-uri);
	if( method->name == NULL ) {
		free(method);
		return RET_ERROR_OOM;
	}
	method->baseuri = strdup(uri);
	if( method->baseuri == NULL ) {
		free(method->name);
		free(method);
		return RET_ERROR_OOM;
	}
211 212 213 214 215 216 217 218 219 220 221
	if( config ) {
		method->config = strdup(config);
		if( method->config == NULL ) {
			free(method->baseuri);
			free(method->name);
			free(method);
			return RET_ERROR_OOM;
		}
	} else {
		method->config = NULL;
	}
222 223
	method->next = run->methods;
	run->methods = method;
224 225 226 227
	*m = method;
	return RET_OK;
}

228 229
/**************************Fire up a method*****************************/

230
inline static retvalue aptmethod_startup(struct aptmethodrun *run,struct aptmethod *method,const char *methoddir) {
231 232 233 234 235
	pid_t f;
	int stdin[2];
	int stdout[2];
	int r;

236 237 238 239 240 241
	/* When there is nothing to get, there is no reason to startup
	 * the method. (And whoever adds methods only to execute commands
	 * will have the tough time they deserve) */
	if( method->tobedone == NULL ) {
		return RET_NOTHING;
	}
Bernhard Link's avatar
Bernhard Link committed
242
	/* when we are already running, we are already ready...*/
243
	if( method->child > 0 ) {
Bernhard Link's avatar
Bernhard Link committed
244
		return RET_OK;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	}

	method->status = ams_waitforcapabilities;

	r = pipe(stdin);	
	if( r < 0 ) {
		int err = errno;
		fprintf(stderr,"Error while creating pipe: %d=%m\n",err);
		return RET_ERRNO(err);
	}
	r = pipe(stdout);	
	if( r < 0 ) {
		int err = errno;
		close(stdin[0]);close(stdin[1]);
		fprintf(stderr,"Error while pipe: %d=%m\n",err);
		return RET_ERRNO(err);
	}

	f = fork();
	if( f < 0 ) {
		int err = errno;
		close(stdin[0]);close(stdin[1]);
		close(stdout[0]);close(stdout[1]);
		fprintf(stderr,"Error while forking: %d=%m\n",err);
		return RET_ERRNO(err);
	}
	if( f == 0 ) {
272
		long maxopen;
273 274 275 276 277 278 279 280 281 282 283 284
		char *methodname;
		/* child: */
		close(stdin[1]);
		close(stdout[0]);
		if( dup2(stdin[0],0) < 0 ) {
			fprintf(stderr,"Error while setting stdin: %d=%m\n",errno);
			exit(255);
		}
		if( dup2(stdout[1],1) < 0 ) {
			fprintf(stderr,"Error while setting stdin: %d=%m\n",errno);
			exit(255);
		}
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
		/* Try to close all open fd but 0,1,2 */
		maxopen = sysconf(_SC_OPEN_MAX);
		if( maxopen > 0 ) {
			int fd;
			for( fd = 3 ; fd < maxopen ; fd++ )
				close(fd);
		} else {
			/* closeat least the ones definitly causing problems*/
			const struct aptmethod *m;
			for( m = run->methods; m ; m = m->next ) {
				if( m != method ) {
					close(m->stdin);
					close(m->stdout);
				}
			}
		}
301
	
302 303 304 305 306 307 308
		methodname = calc_dirconcat(methoddir,method->name);

		if( methodname == NULL )
			exit(255);

		execl(methodname,methodname,NULL);
		
309 310 311 312 313
		fprintf(stderr,"Error while executing '%s': %d=%m\n",methodname,errno);
		exit(255);
	}
	/* the main program continues... */
	method->child = f;
314 315
	if( verbose > 5 )
		fprintf(stderr,"Method '%s' started as %d\n",method->baseuri,f);
316 317 318 319 320
	close(stdin[0]);
	close(stdout[1]);
	method->stdin = stdin[1];
	method->stdout = stdout[0];
	method->inputbuffer = NULL;
321 322 323 324 325
	method->input_size = 0;
	method->alreadyread = 0;
	method->command = NULL;
	method->output_length = 0;
	method->alreadywritten = 0;
326 327 328 329 330 331
	return RET_OK;
}

/************************Sending Configuration**************************/

static inline retvalue sendconfig(struct aptmethod *method) {
332 333 334 335 336 337
#define CONF601 "601 Configuration"
#define CONFITEM "\nConfig-Item: "
	size_t l;
	const char *p;
	char *c;
	int wasnewline;
338

339
	assert(method->command == NULL);
340

341
	method->alreadywritten = 0;
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359

	if( method->config == NULL ) {
		method->command = mprintf(CONF601 "Config-Item: Dir=/\n\n");
		if( method->command == NULL ) {
			return RET_ERROR_OOM;
		}
		method->output_length = strlen(method->command);
		return RET_OK;
	}

	l = sizeof(CONF601)+sizeof(CONFITEM)+1;
	for( p = method->config; *p ; p++ ) {
		if( *p == '\n' )
			l += sizeof(CONFITEM)-1;
		else 
			l++;
	}
	c = method->command = malloc(l);
360
	if( method->command == NULL ) {
361 362 363
		return RET_ERROR_OOM;
	}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
	strcpy(c,CONF601 CONFITEM);
	c += sizeof(CONF601)+sizeof(CONFITEM)-2;
	wasnewline = 1;
	for( p = method->config; *p ; p++ ) {
		if( *p != '\n' ) {
			if( !wasnewline || !isspace(*p) )
				*(c++) = *p;
			wasnewline = 0;
		} else {
			strcpy(c,CONFITEM);
			c += sizeof(CONFITEM)-1;
			wasnewline = 1;
		}
	}
	*(c++) = '\n';
	*(c++) = '\n';
	*c = '\0';

	if( verbose > 10 ) {
		fprintf(stderr,"Sending config: '%s'\n",method->command);
	}

	method->output_length = strlen(method->command);
387 388 389 390 391
	return RET_OK;
}

/**************************how to add files*****************************/

392
static inline struct tobedone *newtodo(const char *baseuri,const char *origfile,const char *destfile,const char *md5sum,const char *filekey) {
393 394 395 396 397 398 399 400 401
	struct tobedone *todo;

	todo = malloc(sizeof(struct tobedone));
	if( todo == NULL )
		return NULL;

	todo->next = NULL;
	todo->uri = calc_dirconcat(baseuri,origfile);
	todo->filename = strdup(destfile);
402 403 404 405
	if( filekey )
		todo->filekey = strdup(filekey);
	else
		todo->filekey = NULL;
406 407
	if( md5sum ) {
		todo->md5sum = strdup(md5sum);
408 409
	} else
		todo->md5sum = NULL;
410
	if( todo->uri == NULL || todo->filename == NULL ||
411 412 413
			(md5sum != NULL && todo->md5sum == NULL) ||
			(filekey != NULL && todo->filekey == NULL) ) {
		free(todo->md5sum);
414 415 416 417 418 419 420 421
		free(todo->uri);
		free(todo->filename);
		free(todo);
		return NULL;
	}
	return todo;
}

422
retvalue aptmethod_queuefile(struct aptmethod *method,const char *origfile,const char *destfile,const char *md5sum,const char *filekey,struct tobedone **t) {
423 424
	struct tobedone *todo;

425
	todo = newtodo(method->baseuri,origfile,destfile,md5sum,filekey);
426
	if( todo == NULL ) {
427 428 429
		return RET_ERROR_OOM;
	}

430
	if( method->lasttobedone == NULL )
431
		method->nexttosend = method->lasttobedone = method->tobedone = todo;
432 433 434 435
	else {
		method->lasttobedone->next = todo;
		method->lasttobedone = todo;
	}
436 437
	if( t )
		*t = todo;
438 439 440 441
	return RET_OK;
	
}

442 443 444 445 446 447
retvalue aptmethod_queueindexfile(struct aptmethod *method,const char *origfile,const char *destfile) {
	if( origfile == NULL || destfile == NULL )
		return RET_ERROR_OOM;
	return aptmethod_queuefile(method,origfile,destfile,NULL,NULL,NULL);
}

448 449 450
/*****************what to do with received files************************/

/* process a received file, possibly copying it around... */
451
static inline retvalue todo_done(struct aptmethod *method,const struct tobedone *todo,const char *filename,const char *md5sum,filesdb filesdb) {
452 453 454 455 456
	char *calculatedmd5;

	/* if the file is somewhere else, copy it: */
	if( strcmp(filename,todo->filename) != 0 ) {
		retvalue r;
457 458
		if( verbose > 1 ) {
			fprintf(stderr,"Linking file '%s' to '%s'...\n",filename,todo->filename);
459
		}
460
		r = md5sum_place(filename,todo->filename,&calculatedmd5);
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
		if( r == RET_NOTHING ) {
			fprintf(stderr,"Cannot open '%s', which was given by method.\n",filename);
			r = RET_ERROR_MISSING;
		}
		if( RET_WAS_ERROR(r) )
			return r;
		/* this we trust more */
		// todo: reimplement the pure copyfile without md5sum?
		md5sum = calculatedmd5;
	} else {
		retvalue r;
	/* if it should be in place, calculate its md5sum, if needed */
		if( todo->md5sum && !md5sum ) {
			r = md5sum_read(filename,&calculatedmd5);
			if( r == RET_NOTHING ) {
				fprintf(stderr,"Cannot open '%s', which was given by method.\n",filename);
				r = RET_ERROR_MISSING;
			}
			if( RET_WAS_ERROR(r) )
				return r;
			md5sum = calculatedmd5;
		} else
			calculatedmd5 = NULL;
	}
	
	/* if we know what it should be, check it: */
	if( todo->md5sum ) {
		if( md5sum == NULL || strcmp(md5sum,todo->md5sum) != 0) {
			fprintf(stderr,"Receiving '%s' wrong md5sum: got '%s' expected '%s'!\n",todo->uri,md5sum,todo->md5sum);
			return RET_ERROR_WRONG_MD5;
		}
	}

494 495 496 497 498 499 500 501 502 503 504
	if( todo->filekey ) {
		retvalue r;

		assert(filesdb);
		assert(todo->md5sum);
		
		r = files_add(filesdb,todo->filekey,todo->md5sum);
		if( RET_WAS_ERROR(r) )
			return r;
	}

505 506 507 508
	  
	return RET_OK;
}

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
/* look which file could not be received and remove it: */
static retvalue urierror(struct aptmethod *method,const char *uri) {
	struct tobedone *todo,*lasttodo;

	lasttodo = NULL; todo = method->tobedone;
	while( todo ) {
		if( strcmp(todo->uri,uri) == 0)  {

			/* remove item: */
			if( lasttodo == NULL )
				method->tobedone = todo->next;
			else
				lasttodo->next = todo->next;
			if( method->nexttosend == todo ) {
				/* just in case some method received
				 * files before we request them ;-) */
				method->nexttosend = todo->next;
			}
			if( method->lasttobedone == todo ) {
				method->lasttobedone = todo->next;
			}
			free(todo->uri);
			free(todo->filename);
			free(todo->md5sum);
			free(todo->filekey);
			free(todo);
			return RET_OK;
		}
		lasttodo = todo;
		todo = todo->next;
	}
	/* huh? */
	fprintf(stderr,"Error with unexpected file '%s'!",uri);
	return RET_ERROR;
}

545
/* look where a received file has to go to: */
546
static retvalue uridone(struct aptmethod *method,const char *uri,const char *filename, const char *md5sum,filesdb filesdb) {
547 548 549 550 551 552
	struct tobedone *todo,*lasttodo;

	lasttodo = NULL; todo = method->tobedone;
	while( todo ) {
		if( strcmp(todo->uri,uri) == 0)  {
			retvalue r;
553
			r = todo_done(method,todo,filename,md5sum,filesdb);
554 555 556 557 558 559

			/* remove item: */
			if( lasttodo == NULL )
				method->tobedone = todo->next;
			else
				lasttodo->next = todo->next;
560 561 562 563 564
			if( method->nexttosend == todo ) {
				/* just in case some method received
				 * files before we request them ;-) */
				method->nexttosend = todo->next;
			}
Bernhard Link's avatar
Bernhard Link committed
565 566 567
			if( method->lasttobedone == todo ) {
				method->lasttobedone = todo->next;
			}
568 569 570
			free(todo->uri);
			free(todo->filename);
			free(todo->md5sum);
571
			free(todo->filekey);
572 573 574 575 576 577 578 579 580 581 582 583
			free(todo);
			return r;
		}
		lasttodo = todo;
		todo = todo->next;
	}
	/* huh? */
	fprintf(stderr,"Received unexpected file '%s' at '%s'!",uri,filename);
	return RET_ERROR;
}

/***************************Input and Output****************************/
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
static retvalue logmessage(const struct aptmethod *method,const char *chunk,const char *type) {
	retvalue r;
	char *message;

	r = chunk_getvalue(chunk,"Message",&message);
	if( RET_WAS_ERROR(r) )
		return r;
	if( RET_IS_OK(r) ) {
		fprintf(stderr,"aptmethod '%s': '%s'\n",method->baseuri,message);
		free(message);
		return RET_OK;
	}
	r = chunk_getvalue(chunk,"URI",&message);
	if( RET_WAS_ERROR(r) )
		return r;
	if( RET_IS_OK(r) ) {
		fprintf(stderr,"aptmethod %s '%s'\n",type,message);
		free(message);
		return RET_OK;
	}
	fprintf(stderr,"aptmethod '%s': '%s'\n",method->baseuri,type);
	return RET_OK;
}
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static inline retvalue gotcapabilities(struct aptmethod *method,const char *chunk) {
	retvalue r;

	r = chunk_gettruth(chunk,"Single-Instance");
	if( RET_WAS_ERROR(r) )
		return r;
	if( r != RET_NOTHING ) {
		fprintf(stderr,"WARNING: Single-Instance not yet supported!\n");
	}
	r = chunk_gettruth(chunk,"Send-Config");
	if( RET_WAS_ERROR(r) )
		return r;
	if( r != RET_NOTHING ) {
		r = sendconfig(method);
		if( RET_WAS_ERROR(r) )
			return r;
	}
	method->status = ams_ok;
	return RET_OK;
}

628
static inline retvalue goturidone(struct aptmethod *method,const char *chunk,filesdb filesdb) {
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	retvalue r;
	char *uri,*filename,*md5,*size,*md5sum;

	//TODO: is it worth the mess to make this in-situ?
	
	r = chunk_getvalue(chunk,"URI",&uri);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Missing URI-header in uridone got from method!\n");
		r = RET_ERROR;
	}
	if( RET_WAS_ERROR(r) )
		return r;

	r = chunk_getvalue(chunk,"Filename",&filename);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Missing Filename-header in uridone got from method!\n");
		r = RET_ERROR;
	}
	if( RET_WAS_ERROR(r) ) {
		free(uri);
		return r;
	}
	
	md5sum = NULL;
	r = chunk_getvalue(chunk,"MD5-Hash",&md5);
	if( RET_IS_OK(r) ) {
		r = chunk_getvalue(chunk,"Size",&size);
		if( RET_IS_OK(r) ) {
			md5sum = calc_concatmd5andsize(md5,size);
			if( md5sum == NULL )
				r = RET_ERROR_OOM;
			free(size);
		}
		free(md5);
	}
	if( RET_WAS_ERROR(r) ) {
		free(uri);
		free(filename);
		return r;
	}

670
 	r = uridone(method,uri,filename,md5sum,filesdb);
671 672 673 674 675
	free(uri);
	free(filename);
	free(md5sum);
	return r;
}
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

static inline retvalue goturierror(struct aptmethod *method,const char *chunk) {
	retvalue r;
	char *uri,*message;

	r = chunk_getvalue(chunk,"URI",&uri);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Missing URI-header in urierror got from method!\n");
		r = RET_ERROR;
	}
	if( RET_WAS_ERROR(r) )
		return r;

	r = chunk_getvalue(chunk,"Message",&message);
	if( r == RET_NOTHING ) {
		message = NULL;
	}
	if( RET_WAS_ERROR(r) ) {
		free(uri);
		return r;
	}

	fprintf(stderr,"aptmethod error receiving '%s':\n'%s'\n",uri,message);
	
 	r = urierror(method,uri);
	free(uri);
	free(message);
	return r;
}
705
	
706
static inline retvalue parsereceivedblock(struct aptmethod *method,const char *input,filesdb filesdb) {
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
	const char *p;
#define OVERLINE {while( *p && *p != '\n') p++; if(*p == '\n') p++; }

	while( *input == '\n' || *input == '\r' )
		input++;
	if( *input == '\0' ) {
		fprintf(stderr,"Unexpected many newlines from methdod!\n");
		return RET_NOTHING;
	}
	p = input;
	switch( (*(input+1)=='0')?*input:'\0' ) {
		case '1':
			switch( *(input+2) ) {
				/* 100 Capabilities */
				case '0':
					fprintf(stderr,"Got '%s'\n",input);
					OVERLINE;
724
					return gotcapabilities(method,input);
725 726
				/* 101 Log */
				case '1':
727 728 729 730
					if( verbose > 10 ) {
						OVERLINE;
						return logmessage(method,p,"101");
					}
731 732 733
					return RET_OK;
				/* 102 Status */
				case '2':
734 735 736 737
					if( verbose > 2 ) {
						OVERLINE;
						return logmessage(method,p,"102");
					}
738 739 740 741 742 743
					return RET_OK;
			}
		case '2':
			switch( *(input+2) ) {
				/* 200 URI Start */
				case '0':
744 745 746 747
					if( verbose > 0 ) {
						OVERLINE;
						return logmessage(method,p,"start");
					}
748 749 750 751
					return RET_OK;
				/* 201 URI Done */
				case '1':
					OVERLINE;
752 753
					if( verbose >= 0 )
						logmessage(method,p,"got");
754
					return goturidone(method,p,filesdb);
755 756 757
			}

		case '4':
758 759 760 761 762 763 764 765 766 767 768 769 770 771
			switch( *(input+2) ) {
				case '0':
					OVERLINE;
					goturierror(method,p);
					break;
				case '1':
					OVERLINE;
					logmessage(method,p,"general error");
					method->status = ams_failed;
					break;
				default:
					fprintf(stderr,"Got error or unsupported mesage: '%s'\n",input);
			}
			/* even a sucessfully handled error is a error */
772 773 774 775 776 777 778
			return RET_ERROR;
		default:
			fprintf(stderr,"unexpected data from method: '%s'\n",input);
			return RET_ERROR;
	}
}

779
static retvalue receivedata(struct aptmethod *method,filesdb filesdb) {
780 781 782 783 784 785
	retvalue result;
	size_t r;
	char *p;
	int consecutivenewlines;

	/* First look if we have enough room to read.. */
786
	if( method->alreadyread + 1024 >= method->input_size ) {
787 788
		char *newptr;
		
789
		if( method->input_size >= 128000 ) {
790 791 792 793 794
			fprintf(stderr,"Ridiculous long answer from method!\n");
			method->status = ams_failed;
			return RET_ERROR;
		}
		
795
		newptr = realloc(method->inputbuffer,method->alreadyread+1024);
796 797 798 799
		if( newptr == NULL ) {
			return RET_ERROR_OOM;
		}
		method->inputbuffer = newptr;
800
		method->input_size = method->alreadyread + 1024;
801 802 803
	}
	/* then read as much as the pipe is able to fill of our buffer */

804
	r = read(method->stdout,method->inputbuffer+method->alreadyread,method->input_size-method->alreadyread-1);
805 806 807 808 809 810 811

	if( r < 0 ) {
		int err = errno;
		fprintf(stderr,"Error reading pipe from aptmethod: %d=%m\n",err);
		method->status = ams_failed;
		return RET_ERRNO(err);
	}
812
	method->alreadyread += r;
813

814 815 816 817
	result = RET_NOTHING;
	while(1) {
		retvalue res;

818
		r = method->alreadyread; 
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
		p = method->inputbuffer;
		consecutivenewlines = 0;

		while( r > 0 ) {
			if( *p == '\0' ) {
				fprintf(stderr,"Zeros in output from method!\n");
				method->status = ams_failed;
				return RET_ERROR;
			} else if( *p == '\n' ) {
				consecutivenewlines++;
				if( consecutivenewlines >= 2 )
					break;
			} else if( *p != '\r' ) {
				consecutivenewlines = 0;
			}
			p++; r--;
835
		}
836
		if( r <= 0 ) {
837
			return result;
838
		}
839
		*p ='\0'; p++; r--;
840
		res = parsereceivedblock(method,method->inputbuffer,filesdb);
841 842
		if( r > 0 )
			memmove(method->inputbuffer,p,r);
843
		method->alreadyread = r;
844
		RET_UPDATE(result,res);
845 846 847 848
	}
}

static retvalue senddata(struct aptmethod *method) {
849
	size_t r,l;
850

851
	assert(method->status == ams_ok);
852 853 854
	if( method->status != ams_ok )
		return RET_NOTHING;

855 856 857 858 859
	if( method->command == NULL ) {
		/* nothing queued to send, nothing to be queued...*/
		if( method->nexttosend == NULL ) {
			return RET_OK;
		}
860

861 862 863 864 865 866 867 868 869 870
		method->alreadywritten = 0;
		method->command = mprintf(
			 "600 URI Acquire\nURI: %s\nFilename: %s\n\n",
			 method->nexttosend->uri,method->nexttosend->filename);
		if( method->command == NULL ) {
			return RET_ERROR_OOM;
		}
		method->output_length = strlen(method->command);
		method->nexttosend = method->nexttosend->next;
	}
871 872


873
	l = method->output_length - method->alreadywritten;
874

875 876 877 878 879 880 881 882 883 884 885 886 887
	r = write(method->stdin,method->command+method->alreadywritten,l);
	if( r < 0 ) {
		int err;

		err = errno;
		fprintf(stderr,"Error writing to pipe: %d=%m\n",err);
		//TODO: disable the whole method??
		method->status = ams_failed;
		return RET_ERRNO(err);
	}
	if( r < l ) {
		method->alreadywritten += r;
		return RET_OK;
888
	}
889 890 891

	free(method->command);
	method->command = NULL;
892 893 894
	return RET_OK;
}

895
static retvalue checkchilds(struct aptmethodrun *run) {
Bernhard Link's avatar
Bernhard Link committed
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	pid_t child;int status; 
	retvalue result = RET_OK;

	while( (child = waitpid(-1,&status,WNOHANG)) > 0 ) {
		struct aptmethod *method,*lastmethod;

		lastmethod = NULL; method = run->methods;
		while( method ) {
			if( method->child == child )
				break;
			lastmethod = method;
			method = method->next;
		}
		if( method == NULL ) {
			fprintf(stderr,"Unexpected child died: %d\n",(int)child);
			continue;
		}
		/* remove this child out of the list: */
		if( lastmethod ) {
			lastmethod->next = method->next;
		} else
			run->methods = method->next;

		/* say something if it exited unnormal: */
		if( WIFEXITED(status) ) {
			int exitcode;

			exitcode = WEXITSTATUS(status);
			if( exitcode != 0 ) {
				fprintf(stderr,"Method %s://%s exited with non-zero exit-code %d!\n",method->name,method->baseuri,exitcode);
				result = RET_ERROR;
			}
		} else {
			fprintf(stderr,"Method %s://%s exited unnormally!\n",method->name,method->baseuri);
				result = RET_ERROR;
		}

		/* free the data... */
		aptmethod_free(method);
	}
	return result;
}

939
static retvalue readwrite(struct aptmethodrun *run,int *activity,filesdb filesdb) {
940 941 942 943 944 945 946 947 948 949 950
	int maxfd,v;
	fd_set readfds,writefds;
	struct aptmethod *method;
	retvalue result,r;

	/* First calculate what to look at: */
	FD_ZERO(&readfds);
	FD_ZERO(&writefds);
	maxfd = 0;
	*activity = 0;
	for( method = run->methods ; method ; method = method->next ) {
951
		if( method->status == ams_ok && ( method->command || method->nexttosend)) {
952
			FD_SET(method->stdin,&writefds);
953 954 955
			if( method->stdin > maxfd )
				maxfd = method->stdin;
			(*activity)++;
Bernhard Link's avatar
Bernhard Link committed
956 957
			if( verbose > 19 )
				fprintf(stderr,"want to write to '%s'\n",method->baseuri);
958 959 960 961 962 963 964
		}
		if( method->status == ams_waitforcapabilities ||
				method->tobedone ) {
			FD_SET(method->stdout,&readfds);
			if( method->stdout > maxfd )
				maxfd = method->stdout;
			(*activity)++;
Bernhard Link's avatar
Bernhard Link committed
965 966
			if( verbose > 19 )
				fprintf(stderr,"want to read from '%s'\n",method->baseuri);
967 968 969
		}
	}

970 971 972
	if( *activity == 0 )
		return RET_NOTHING;

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
	// TODO: think about a timeout...
	v = select(maxfd+1,&readfds,&writefds,NULL,NULL);
	if( v < 0 ) {
		int err = errno;
		fprintf(stderr,"Select returned error: %d=%m\n",err);
		*activity = -1;
		// TODO: what to do here?
		return RET_ERRNO(errno);
	}

	result = RET_NOTHING;

	maxfd = 0;
	for( method = run->methods ; method ; method = method->next ) {
		if( FD_ISSET(method->stdout,&readfds) ) {
988
			r = receivedata(method,filesdb);
989 990 991 992 993 994 995 996 997 998
			RET_UPDATE(result,r);
		}
		if( FD_ISSET(method->stdin,&writefds) ) {
			r = senddata(method);
			RET_UPDATE(result,r);
		}
	}
	return result;
}

999
retvalue aptmethod_download(struct aptmethodrun *run,const char *methoddir,filesdb filesdb) {
1000 1001 1002 1003 1004 1005 1006 1007 1008
	struct aptmethod *method,*lastmethod;
	retvalue result,r;
	int activity;

	result = RET_NOTHING;

	/* fire up all methods, removing those that do not work: */
	lastmethod = NULL; method = run->methods;
	while( method ) {
1009
		r = aptmethod_startup(run,method,methoddir);
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
		if( !RET_IS_OK(r) ) {
			struct aptmethod *next = method->next;

			if( lastmethod ) {
				lastmethod->next = next;
			} else
				run->methods = next;

			aptmethod_free(method);
			method = next;
		} else {
			lastmethod = method;
			method = method->next;
		}
		RET_UPDATE(result,r);
	}
	/* waiting for them to finish: */
	do {
1028
	  r = checkchilds(run);
Bernhard Link's avatar
Bernhard Link committed
1029
	  RET_UPDATE(result,r);
1030
	  r = readwrite(run,&activity,filesdb);
1031 1032 1033 1034 1035 1036
	  RET_UPDATE(result,r);
	} while( activity > 0 );

	return result;
}