checkindeb.c 11.5 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
/*  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>
29
#include <ctype.h>
30
#include <zlib.h>
31
#include <db.h>
32 33
#include "error.h"
#include "md5sum.h"
34
#include "names.h"
35 36
#include "chunks.h"
#include "checkindeb.h"
37 38
#include "reference.h"
#include "packages.h"
39
#include "binaries.h"
40 41
#include "files.h"
#include "extractcontrol.h"
42 43 44 45

extern int verbose;

// This file shall include the code to include binaries, i.e.
46
// create  the chunk of the Packages.gz-file and 
47 48
// putting it in the various databases.

49
static retvalue deb_addtodist(const char *dbpath,DB *references,struct distribution *distribution,const char *architecture,struct debpackage *package,const struct strlist *filekeys) {
50
	retvalue result,r;
51
	char *identifier,*oldversion;
52
	DB *packages;
53
	struct strlist oldfilekeys,*o;
54

55
	identifier = calc_identifier(distribution->codename,package->component,architecture);
56 57 58 59 60 61 62 63 64
	if( ! identifier )
		return RET_ERROR_OOM;

	packages = packages_initialize(dbpath,identifier);
	if( ! packages ) {
		free(identifier);
		return RET_ERROR;
	}

65
	r = binaries_lookforolder(packages,package->package,package->version,&oldversion,&oldfilekeys);
66 67 68 69 70
	if( RET_WAS_ERROR(r) ) {
		(void)packages_done(packages);
		free(identifier);
		return r;
	}
71 72 73 74
	if( RET_IS_OK(r) )
		o = &oldfilekeys;
	else
		o = NULL;
75

76
	if( RET_IS_OK(r) && oldversion ) {
77 78 79 80
		fprintf(stderr,"Version '%s' already in the archive, skipping '%s'\n",oldversion,package->version);
		free(oldversion);
		result = RET_NOTHING;
	} else
81
		result = packages_insert(identifier,references,packages,
82
			package->package, package->control,
83
			filekeys, o);
84 85 86 87 88

	r = packages_done(packages);
	RET_ENDUPDATE(result,r);

	free(identifier);
89 90
	if( o )
		strlist_done(&oldfilekeys);
91 92 93
	return result;
}

94
/* things to do with .deb's checkin by hand: (by comparison with apt-ftparchive)
95
- extract the control file (that's the hard part -> extractcontrol.c )
96 97 98 99 100 101
- check for Package, Version, Architecture, Maintainer, Description
- apply overwrite if neccesary (section,priority and perhaps maintainer).
- add Size, MD5sum, Filename, Priority, Section
- remove Status (warning if existant?)
- check for Optional-field and reject then..
*/
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

static inline retvalue getvalue(const char *filename,const char *chunk,const char *field,char **value) {
	retvalue r;

	r = chunk_getvalue(chunk,field,value);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Cannot find %s-header in control file of %s!\n",field,filename);
		r = RET_ERROR;
	}
	return r;
}

static inline retvalue checkvalue(const char *filename,const char *chunk,const char *field) {
	retvalue r;

	r = chunk_checkfield(chunk,field);
	if( r == RET_NOTHING ) {
		fprintf(stderr,"Cannot find %s-header in control file of %s!\n",field,filename);
		r = RET_ERROR;
	}
	return r;
}

static inline retvalue getvalue_d(const char *defaul,const char *chunk,const char *field,char **value) {
	retvalue r;

	r = chunk_getvalue(chunk,field,value);
	if( r == RET_NOTHING ) {
		*value = strdup(defaul);
		if( *value == NULL )
			r = RET_ERROR_OOM;
	}
	return r;
}

137 138 139 140 141 142 143 144 145 146
static inline retvalue getvalue_n(const char *chunk,const char *field,char **value) {
	retvalue r;

	r = chunk_getvalue(chunk,field,value);
	if( r == RET_NOTHING ) {
		*value = NULL;
	}
	return r;
}

147 148 149
void deb_free(struct debpackage *pkg) {
	if( pkg ) {
		free(pkg->package);free(pkg->version);
150
		free(pkg->source);free(pkg->architecture);
151
		free(pkg->basename);free(pkg->control);
152 153 154 155
	}
	free(pkg);
}

156
retvalue deb_read(struct debpackage **pkg, const char *filename) {
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
	retvalue r;
	struct debpackage *deb;


	deb = calloc(1,sizeof(struct debpackage));

	r = extractcontrol(&deb->control,filename);
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}

	/* first look for fields that should be there */

	r = getvalue(filename,deb->control,"Package",&deb->package);
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}
	r = checkvalue(filename,deb->control,"Maintainer");
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}
	r = checkvalue(filename,deb->control,"Description");
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}
	r = getvalue(filename,deb->control,"Version",&deb->version);
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}
191
	r = getvalue(filename,deb->control,"Architecture",&deb->architecture);
192 193 194 195 196 197 198 199 200 201 202 203
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}

	/* can be there, otherwise we also know what it is */
	r = getvalue_d(deb->package,deb->control,"Source",&deb->source);
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}

204
	deb->basename = calc_package_basename(deb->package,deb->version,deb->architecture);
205 206 207 208 209
	if( deb->basename == NULL ) {
		deb_free(deb);
		return r;
	}

210 211 212 213 214
	r = checkvalue(filename,deb->control,"Priority");
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}
215
	r = getvalue_n(deb->control,"Section",&deb->section);
216 217 218 219
	if( RET_WAS_ERROR(r) ) {
		deb_free(deb);
		return r;
	}
220
	*pkg = deb;
221

222 223
	return RET_OK;
}
224

225 226 227
retvalue deb_complete(struct debpackage *pkg, const char *filekey, const char *md5andsize) {
	const char *size;
	struct fieldtoadd *file;
Bernhard Link's avatar
Bernhard Link committed
228
	char *newchunk;
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246

	size = md5andsize;
	while( !isblank(*size) && *size )
		size++;
	file = addfield_newn("MD5Sum",md5andsize, size-md5andsize,NULL);
	if( !file )
		return RET_ERROR_OOM;
	while( *size && isblank(*size) )
		size++;
	file = addfield_new("Size",size,file);
	if( !file )
		return RET_ERROR_OOM;
	file = addfield_new("Filename",filekey,file);
	if( !file )
		return RET_ERROR_OOM;

	// TODO: add overwriting of other fields here, (before the rest)
	
Bernhard Link's avatar
Bernhard Link committed
247
	newchunk  = chunk_replacefields(pkg->control,file,"Description");
248
	addfield_free(file);
Bernhard Link's avatar
Bernhard Link committed
249 250
	if( newchunk == NULL ) {
		return RET_ERROR_OOM;
251 252
	}

Bernhard Link's avatar
Bernhard Link committed
253 254 255
	free(pkg->control);
	pkg->control = newchunk;

256 257
	return RET_OK;
}
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

/* Guess which component to use:
 * - if the user gave one, use that one.
 * - if the section is a componentname, use this one
 * - if the section starts with a componentname/, use this one
 * - if the section ends with a /componentname, use this one
 * - if the section/ is the start of a componentname, use this one
 * - use the first component in the list
 */

retvalue guess_component(struct distribution *distribution, 
			 struct debpackage *package,
			 const char *givencomponent) {
	int i;
	const char *section;
	size_t section_len;

#define RETURNTHIS(comp) { \
		char *c = strdup(comp); \
		if( !c ) \
			return RET_ERROR_OOM; \
		free(package->component); \
		package->component = c; \
		return RET_OK; \
	}
	
	if( givencomponent ) {
		if( givencomponent && !strlist_in(&distribution->components,givencomponent) ) {
			fprintf(stderr,"Could not find '%s' in components of '%s': ",
					givencomponent,distribution->codename);
			strlist_fprint(stderr,&distribution->components);
			fputs("'\n",stderr);
			return RET_ERROR;
		}

		RETURNTHIS(givencomponent);
	}
	section = package->section;
	if( section == NULL ) {
		fprintf(stderr,"Found no section for '%s', so I cannot guess the component to put it in!\n",package->package);
		return RET_ERROR;
	}
	if( distribution->components.count <= 0 ) {
		fprintf(stderr,"I do not find any components in '%s', so there is no chance I cannot even take one by guessing!\n",distribution->codename);
		return RET_ERROR;
	}
	section_len = strlen(section);

	for( i = 0 ; i < distribution->components.count ; i++ ) {
		const char *component = distribution->components.values[i];

		if( strcmp(section,component) == 0 )
			RETURNTHIS(component);
	}
	for( i = 0 ; i < distribution->components.count ; i++ ) {
		const char *component = distribution->components.values[i];
		size_t len = strlen(component);

		if( len<section_len && section[len] == '/' && strncmp(section,component,len) == 0 )
			RETURNTHIS(component);
	}
	for( i = 0 ; i < distribution->components.count ; i++ ) {
		const char *component = distribution->components.values[i];
		size_t len = strlen(component);

		if( len<section_len && section[section_len-len-1] == '/' && 
				strncmp(section+section_len-len,component,len) == 0 )
			RETURNTHIS(component);
	}
	for( i = 0 ; i < distribution->components.count ; i++ ) {
		const char *component = distribution->components.values[i];

		if( strncmp(section,component,section_len) == 0 && component[section_len] == '/' )
			RETURNTHIS(component);
	}
	RETURNTHIS(distribution->components.values[0]);
#undef RETURNTHIS
}

338
/* insert the given .deb into the mirror in <component> in the <distribution>
339
 * putting things with architecture of "all" into <d->architectures> (and also
340
 * causing error, if it is not one of them otherwise)
341
 * if component is NULL, guessing it from the section. */
342

343
retvalue deb_add(const char *dbdir,DB *references,DB *filesdb,const char *mirrordir,const char *forcecomponent,struct distribution *distribution,const char *debfilename,int force){
344 345
	retvalue r,result;
	struct debpackage *pkg;
346
	char *md5andsize;
347
	struct strlist filekeys;
348 349 350 351 352 353 354 355 356 357 358 359
	int i;

	/* First taking a closer look to the file: */

	r = deb_read(&pkg,debfilename);
	if( RET_WAS_ERROR(r) ) {
		return r;
	}

	/* look for overwrites */

	// TODO: look for overwrites and things like this here...
360
	// TODO: set pkg->section to new value if doing so.
361
	
362 363
	/* decide where it has to go */

364 365 366 367
	r = guess_component(distribution,pkg,forcecomponent);
	if( RET_WAS_ERROR(r) ) {
		deb_free(pkg);
		return r;
368
	}
369 370 371 372 373
	if( verbose > 0 && forcecomponent == NULL ) {
		fprintf(stderr,"%s: component guessed as '%s'\n",debfilename,pkg->component);
	}
	
	/* some sanity checks: */
374

375
	if( strcmp(pkg->architecture,"all") != 0 &&
376
	    !strlist_in( &distribution->architectures, pkg->architecture )) {
377 378
		fprintf(stderr,"While checking in '%s': '%s' is not listed in '",
				debfilename,pkg->architecture);
379
		strlist_fprint(stderr,&distribution->architectures);
380 381 382 383 384 385 386
		fputs("'\n",stderr);
		if( force <= 0 ) {
			deb_free(pkg);
			return RET_ERROR;
		}
	} 
	
387
	/* calculate it's filekey */
388
	binaries_calcfilekeys(pkg->component,pkg->source,pkg->basename,&filekeys);
389 390 391 392
	if( RET_WAS_ERROR(r) ) {
		deb_free(pkg);
		return r;
	}
393 394 395

	/* then looking if we already have this, or copy it in */

396
	r = files_checkin(filesdb,mirrordir,filekeys.values[0],debfilename,&md5andsize);
397
	if( RET_WAS_ERROR(r) ) {
398
		strlist_done(&filekeys);
399 400 401 402
		deb_free(pkg);
		return r;
	} 

403 404
	r = deb_complete(pkg,filekeys.values[0],md5andsize);
	free(md5andsize);
405
	if( RET_WAS_ERROR(r) ) {
406
		strlist_done(&filekeys);
407 408 409 410 411 412 413 414 415
		deb_free(pkg);
		return r;
	} 
	
	/* finaly put it into one or more distributions */

	result = RET_NOTHING;

	if( strcmp(pkg->architecture,"all") != 0 ) {
416
		r = deb_addtodist(dbdir,references,distribution,pkg->architecture,pkg,&filekeys);
417
		RET_UPDATE(result,r);
418
	} else for( i = 0 ; i < distribution->architectures.count ; i++ ) {
419
		r = deb_addtodist(dbdir,references,distribution,distribution->architectures.values[i],pkg,&filekeys);
420 421 422
		RET_UPDATE(result,r);
	}

423
	strlist_done(&filekeys);
424 425 426 427
	deb_free(pkg);

	return result;
}