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 114 115 116 117 118 119 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 147 148 149 150 151 152 153 154 155 156 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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 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 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 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 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 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 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 545 546 547 548 549 550 551 552 553 554 555 556 557 558
|
/* This file is part of "reprepro"
* Copyright (C) 2003,2004,2005,2006 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 version 2 as
* published by the Free Software Foundation.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02111-1301 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 <ctype.h>
#include <zlib.h>
#include "error.h"
#include "ignore.h"
#include "strlist.h"
#include "md5sum.h"
#include "names.h"
#include "chunks.h"
#include "checkindeb.h"
#include "reference.h"
#include "binaries.h"
#include "files.h"
#include "debfile.h"
#include "guesscomponent.h"
#include "tracking.h"
extern int verbose;
/* This file includes the code to include binaries, i.e.
to create the chunk for the Packages.gz-file and
to put it in the various databases.
Things to do with .deb's checkin by hand: (by comparison with apt-ftparchive)
- extract the control file (that's the hard part -> extractcontrol.c )
- 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..
*/
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_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;
}
struct debpackage {
/* things to be set by deb_read: */
char *package,*version,*source,*architecture;
char *control;
/* only used when tracking */
/*@null@*/char *sourceversion;
/* things that might still be NULL then: */
char *section;
char *priority;
/* things that will still be NULL then: */
char *component; //This might be const, too and save some strdups, but...
/* with deb_calclocations: */
const char *filekey;
struct strlist filekeys;
/* with deb_copyfiles or deb_checkfiles: */
char *md5sum;
};
void deb_free(/*@only@*/struct debpackage *pkg) {
if( pkg != NULL ) {
free(pkg->package);free(pkg->version);
free(pkg->source);free(pkg->sourceversion);
free(pkg->architecture);
free(pkg->control);
free(pkg->section);free(pkg->component);
free(pkg->priority);
if( pkg->filekey != NULL )
strlist_done(&pkg->filekeys);
free(pkg->md5sum);
}
free(pkg);
}
/* read the data from a .deb, make some checks and extract some data */
static retvalue deb_read(/*@out@*/struct debpackage **pkg, const char *filename, bool_t needssourceversion) {
retvalue r;
struct debpackage *deb;
//TODO: this smells like code-duplication with binaries.c
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 = chunk_getname(deb->control,"Package",&deb->package,FALSE);
if( r == RET_NOTHING ) {
fprintf(stderr,"Missing 'Package' field in %s!\n",filename);
r = RET_ERROR;
}
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;
}
r = getvalue(filename,deb->control,"Architecture",&deb->architecture);
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
return r;
}
/* can be there, otherwise we also know what it is */
if( needssourceversion )
r = chunk_getnameandversion(deb->control,"Source",&deb->source,&deb->sourceversion);
else
r = chunk_getname(deb->control,"Source",&deb->source,TRUE);
if( RET_IS_OK(r) ) {
// As package-check is strict subset of source-check
// this has only be done, if it is not also sourcename:
r = properpackagename(deb->package);
if( RET_IS_OK(r) )
r = propersourcename(deb->source);
} else if( r == RET_NOTHING ) {
deb->source = strdup(deb->package);
if( deb->source == NULL )
r = RET_ERROR_OOM;
else
r = propersourcename(deb->package);
}
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
return r;
}
if( needssourceversion ) {
if( deb->sourceversion == NULL ) {
deb->sourceversion = strdup(deb->version);
if( deb->sourceversion == NULL )
r = RET_ERROR_OOM;
else
r = RET_OK;
} else
r = properversion(deb->sourceversion);
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
return r;
}
}
r = getvalue_n(deb->control,PRIORITY_FIELDNAME,&deb->priority);
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
return r;
}
r = getvalue_n(deb->control,SECTION_FIELDNAME,&deb->section);
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
return r;
}
r = properversion(deb->version);
if( !RET_WAS_ERROR(r) )
r = properfilenamepart(deb->architecture);
if( RET_WAS_ERROR(r) ) {
deb_free(deb);
return r;
}
*pkg = deb;
return RET_OK;
}
/* do overwrites, add Filename, Size and md5sum to the control-item */
static retvalue deb_complete(struct debpackage *pkg,const struct overrideinfo *override) {
const char *size;
struct fieldtoadd *replace;
char *newchunk;
assert( pkg->section != NULL && pkg->priority != NULL);
size = pkg->md5sum;
while( !xisblank(*size) && *size != '\0' )
size++;
replace = addfield_newn("MD5sum",pkg->md5sum, size-pkg->md5sum,NULL);
if( replace == NULL )
return RET_ERROR_OOM;
while( *size != '\0' && xisblank(*size) )
size++;
replace = addfield_new("Size",size,replace);
if( replace == NULL )
return RET_ERROR_OOM;
replace = addfield_new("Filename",pkg->filekey,replace);
if( replace == NULL )
return RET_ERROR_OOM;
replace = addfield_new(SECTION_FIELDNAME,pkg->section ,replace);
if( replace == NULL )
return RET_ERROR_OOM;
replace = addfield_new(PRIORITY_FIELDNAME,pkg->priority, replace);
if( replace == NULL )
return RET_ERROR_OOM;
replace = override_addreplacefields(override,replace);
if( replace == NULL )
return RET_ERROR_OOM;
newchunk = chunk_replacefields(pkg->control,replace,"Description");
addfield_free(replace);
if( newchunk == NULL ) {
return RET_ERROR_OOM;
}
free(pkg->control);
pkg->control = newchunk;
return RET_OK;
}
static retvalue deb_calclocations(struct debpackage *pkg,/*@null@*/const char *givenfilekey,const char *packagetype) {
retvalue r;
char *basename;
basename = calc_binary_basename(pkg->package,pkg->version,pkg->architecture,packagetype);
if( basename == NULL )
return RET_ERROR_OOM;
r = binaries_calcfilekeys(pkg->component,pkg->source,basename,&pkg->filekeys);
if( RET_WAS_ERROR(r) ) {
free(basename);
return r;
}
pkg->filekey = pkg->filekeys.values[0];
free(basename);
if( givenfilekey!=NULL && strcmp(givenfilekey,pkg->filekey) != 0 ) {
fprintf(stderr,"Name mismatch, .changes indicates '%s', but the file itself says '%s'!\n",givenfilekey,pkg->filekey);
return RET_ERROR;
}
return r;
}
retvalue deb_prepare(/*@out@*/struct debpackage **deb,filesdb filesdb,const char * const forcecomponent,const char * const forcearchitecture,const char *forcesection,const char *forcepriority,const char * const packagetype,struct distribution *distribution,const char *debfilename,const char * const givenfilekey,const char * const givenmd5sum,const struct overrideinfo *binoverride,int delete,bool_t needsourceversion,const struct strlist *allowed_binaries,const char *expectedsourcepackage,const char *expectedsourceversion){
retvalue r;
struct debpackage *pkg;
const struct overrideinfo *oinfo;
const struct strlist *components;
assert( (givenmd5sum!=NULL && givenfilekey!=NULL ) ||
(givenmd5sum==NULL && givenfilekey==NULL ) );
/* First taking a closer look to the file: */
r = deb_read(&pkg,debfilename,
needsourceversion || expectedsourceversion != NULL);
if( RET_WAS_ERROR(r) ) {
return r;
}
if( allowed_binaries != NULL &&
!strlist_in(allowed_binaries, pkg->package) &&
!IGNORING_(surprisingbinary,
"'%s' has packagename '%s' not listed in the .changes file!\n",
debfilename, pkg->package)) {
deb_free(pkg);
return RET_ERROR;
}
if( expectedsourcepackage != NULL &&
strcmp(pkg->source, expectedsourcepackage) != 0 ) {
/* this cannot ne ignored easily, as it determines
* the directory this file is stored into */
fprintf(stderr,
"'%s' lists source package '%s', but .changes says it is '%s'!\n",
debfilename, pkg->source,
expectedsourcepackage);
deb_free(pkg);
return RET_ERROR;
}
if( expectedsourceversion != NULL &&
strcmp(pkg->sourceversion, expectedsourceversion) != 0 &&
!IGNORING_(wrongsourceversion,
"'%s' lists source version '%s', but .changes says it is '%s'!\n",
debfilename, pkg->sourceversion,
expectedsourceversion)) {
deb_free(pkg);
return RET_ERROR;
}
oinfo = override_search(binoverride,pkg->package);
if( forcesection == NULL ) {
forcesection = override_get(oinfo,SECTION_FIELDNAME);
}
if( forcepriority == NULL ) {
forcepriority = override_get(oinfo,PRIORITY_FIELDNAME);
}
if( forcesection != NULL ) {
free(pkg->section);
pkg->section = strdup(forcesection);
if( pkg->section == NULL ) {
deb_free(pkg);
return RET_ERROR_OOM;
}
}
if( forcepriority != NULL ) {
free(pkg->priority);
pkg->priority = strdup(forcepriority);
if( pkg->priority == NULL ) {
deb_free(pkg);
return RET_ERROR_OOM;
}
}
if( pkg->section == NULL ) {
fprintf(stderr,"No section was given for '%s', skipping.\n",pkg->package);
deb_free(pkg);
return RET_ERROR;
}
if( pkg->priority == NULL ) {
fprintf(stderr,"No priority was given for '%s', skipping.\n",pkg->package);
deb_free(pkg);
return RET_ERROR;
}
/* decide where it has to go */
if( strcmp(packagetype,"udeb") == 0 )
components = &distribution->udebcomponents;
else
components = &distribution->components;
r = guess_component(distribution->codename,components,
pkg->package,pkg->section,forcecomponent,&pkg->component);
if( RET_WAS_ERROR(r) ) {
deb_free(pkg);
return r;
}
if( verbose > 0 && forcecomponent == NULL ) {
fprintf(stderr,"%s: component guessed as '%s'\n",debfilename,pkg->component);
}
/* some sanity checks: */
if( forcearchitecture != NULL && strcmp(forcearchitecture,"all") != 0 &&
strcmp(pkg->architecture,forcearchitecture) != 0 &&
strcmp(pkg->architecture,"all") != 0 ) {
fprintf(stderr,"Cannot checking in '%s' into architecture '%s', as it is '%s'!",
debfilename,forcearchitecture,pkg->architecture);
deb_free(pkg);
/* TODO: this should be moved upwards...
if( delete >= D_DELETE ) {
if( verbose >= 0 )
fprintf(stderr,"Deleting '%s' as requested!\n",debfilename);
if( unlink(debfilename) != 0 ) {
fprintf(stderr,"Error deleting '%s': %m\n",debfilename);
}
}
*/
return RET_ERROR;
} else if( strcmp(pkg->architecture,"all") != 0 &&
!strlist_in( &distribution->architectures, pkg->architecture )) {
(void)fprintf(stderr,"While checking in '%s': '%s' is not listed in '",
debfilename,pkg->architecture);
(void)strlist_fprint(stderr,&distribution->architectures);
(void)fputs("'\n",stderr);
deb_free(pkg);
return RET_ERROR;
}
if( !strlist_in(components,pkg->component) ) {
fprintf(stderr,"While checking in '%s': Would put in component '%s', but that is not available!\n",debfilename,pkg->component);
/* this cannot be ignored as there is not data structure available*/
return RET_ERROR;
}
r = deb_calclocations(pkg,givenfilekey,packagetype);
if( RET_WAS_ERROR(r) ) {
deb_free(pkg);
return r;
}
/* then looking if we already have this, or copy it in */
if( givenfilekey != NULL ) {
assert(givenmd5sum != NULL);
pkg->md5sum = strdup(givenmd5sum);
if( pkg->md5sum == NULL ) {
deb_free(pkg);
return RET_ERROR_OOM;
}
} else {
assert(givenmd5sum == NULL);
r = files_include(filesdb,debfilename,pkg->filekey,NULL,&pkg->md5sum,delete);
if( RET_WAS_ERROR(r) ) {
deb_free(pkg);
return r;
}
}
assert( pkg->md5sum != NULL );
/* Prepare everything that can be prepared beforehand */
r = deb_complete(pkg,oinfo);
if( RET_WAS_ERROR(r) ) {
deb_free(pkg);
return r;
}
*deb = pkg;
return RET_OK;
}
retvalue deb_addprepared(const struct debpackage *pkg, const char *dbdir,references refs,const char *forcearchitecture,const char *packagetype,struct distribution *distribution,struct strlist *dereferencedfilekeys,struct trackingdata *trackingdata){
retvalue r,result;
int i;
/* finally put it into one or more architectures of the distribution */
result = RET_NOTHING;
if( strcmp(pkg->architecture,"all") != 0 ) {
struct target *t = distribution_getpart(distribution,pkg->component,pkg->architecture,packagetype);
r = target_initpackagesdb(t,dbdir);
if( !RET_WAS_ERROR(r) ) {
retvalue r2;
if( interrupted() )
r = RET_ERROR_INTERUPTED;
else
r = target_addpackage(t,refs,pkg->package,pkg->version,pkg->control,&pkg->filekeys,FALSE,dereferencedfilekeys,trackingdata,ft_ARCH_BINARY);
r2 = target_closepackagesdb(t);
RET_ENDUPDATE(r,r2);
}
RET_UPDATE(result,r);
} else if( forcearchitecture != NULL && strcmp(forcearchitecture,"all") != 0 ) {
struct target *t = distribution_getpart(distribution,pkg->component,forcearchitecture,packagetype);
r = target_initpackagesdb(t,dbdir);
if( !RET_WAS_ERROR(r) ) {
retvalue r2;
if( interrupted() )
r = RET_ERROR_INTERUPTED;
else
r = target_addpackage(t,refs,pkg->package,pkg->version,pkg->control,&pkg->filekeys,FALSE,dereferencedfilekeys,trackingdata,ft_ALL_BINARY);
r2 = target_closepackagesdb(t);
RET_ENDUPDATE(r,r2);
}
RET_UPDATE(result,r);
} else for( i = 0 ; i < distribution->architectures.count ; i++ ) {
/*@dependent@*/struct target *t;
if( strcmp(distribution->architectures.values[i],"source") == 0 )
continue;
t = distribution_getpart(distribution,pkg->component,distribution->architectures.values[i],packagetype);
r = target_initpackagesdb(t,dbdir);
if( !RET_WAS_ERROR(r) ) {
retvalue r2;
if( interrupted() )
r = RET_ERROR_INTERUPTED;
else
r = target_addpackage(t,refs,pkg->package,pkg->version,pkg->control,&pkg->filekeys,FALSE,dereferencedfilekeys,trackingdata,ft_ALL_BINARY);
r2 = target_closepackagesdb(t);
RET_ENDUPDATE(r,r2);
}
RET_UPDATE(result,r);
}
RET_UPDATE(distribution->status, result);
return result;
}
/* insert the given .deb into the mirror in <component> in the <distribution>
* putting things with architecture of "all" into <d->architectures> (and also
* causing error, if it is not one of them otherwise)
* if component is NULL, guessing it from the section. */
retvalue deb_add(const char *dbdir,references refs,filesdb filesdb,const char *forcecomponent,const char *forcearchitecture,const char *forcesection,const char *forcepriority,const char *packagetype,struct distribution *distribution,const char *debfilename,const char *givenfilekey,const char *givenmd5sum,const struct overrideinfo *binoverride,int delete,struct strlist *dereferencedfilekeys,/*@null@*/trackingdb tracks){
struct debpackage *pkg;
retvalue r;
struct trackingdata trackingdata;
if( givenfilekey != NULL && givenmd5sum != NULL ) {
assert( delete == D_INPLACE );
}
r = deb_prepare(&pkg,filesdb,forcecomponent,forcearchitecture,forcesection,forcepriority,packagetype,distribution,debfilename,givenfilekey,givenmd5sum,binoverride,delete,tracks!=NULL,NULL,NULL,NULL);
if( RET_WAS_ERROR(r) )
return r;
if( tracks != NULL ) {
assert(pkg->sourceversion != NULL);
r = trackingdata_summon(tracks,pkg->source,pkg->sourceversion,&trackingdata);
if( RET_WAS_ERROR(r) ) {
deb_free(pkg);
return r;
}
}
r = deb_addprepared(pkg,dbdir,refs,forcearchitecture,packagetype,distribution,dereferencedfilekeys,(tracks!=NULL)?&trackingdata:NULL);
deb_free(pkg);
if( tracks != NULL ) {
retvalue r2;
r2 = trackingdata_finish(tracks, &trackingdata, refs, dereferencedfilekeys);
RET_ENDUPDATE(r,r2);
}
return r;
}
|