pkgdetails.c 15.5 KB
Newer Older
1 2 3 4
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
5 6
#include <stdarg.h>
#include <errno.h>
7 8 9 10

#define MAX_LINE 1000
#define MAX_PKGS 100

11 12
char *checksum_field=NULL;

13 14 15 16 17 18 19 20 21 22
/* exclude_field is a comma-separated list of dependencies that you want to exclude, for example:
   Package: init
   Pre-Depends: systemd-sysv | sysvinit-core | upstart

   This will always give systemd-sysv with GETDEPS, unless you use
   DEBOOTSTRAP_EXCLUDE_FIELD=systemd-sysv,sysvinit-core pkgdetails Packages init
 */
char *exclude_field=NULL;
char **exclude_array=NULL;

23 24
static void oom_die(void)
{
25
    fputs("Out of memory!\n", stderr);
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
    exit(1);
}

static char *xvasprintf(const char *fmt, va_list ap) {
    char *ret;

    if (vasprintf (&ret, fmt, ap) < 0) {
        if (errno == ENOMEM)
            oom_die();
        return NULL;
    }
    return ret;
}

static char *xasprintf(const char *fmt, ...) {
    va_list ap;
    char *ret;

    va_start(ap, fmt);
    ret = xvasprintf(fmt, ap);
    va_end(ap);
    return ret;
}

50 51 52 53 54
static void parse_exclude_field(char *str) {
    char *workptr, *saveptr = NULL, *tok = NULL;
    workptr = strdup(str);
    int nf=0,f;
    tok=workptr;
55
    while(*tok==',') tok++;
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
    do {
      saveptr=strchr(tok,',');
      tok++;
      if (saveptr) tok=saveptr+1;
      nf++;
    } while(saveptr);

    fprintf(stderr,"DBG parse_exclude_field(%s), %d fields\n",str, nf);

    exclude_array = (char **) malloc((nf+1) * sizeof(char *));
    if (exclude_array==NULL){
      oom_die();
    }
    tok = strtok_r(workptr, ",", &saveptr);
    for(f=0;f<nf;f++){
      exclude_array[f] = strdup(tok);
      fprintf(stderr,"DBG exclude_array[%d] = \"%s\"\n", f, exclude_array[f]);
      tok = strtok_r(NULL, ",", &saveptr);
    }
    exclude_array[nf+1-1]=NULL;
    free(workptr);
}

static void free_exclude_array(void) {
    int f;
    if (exclude_array==NULL) return;
    f=0;
    while(exclude_array[f]!=NULL){
      free(exclude_array[f]);
      exclude_array[f]=NULL;
      f++;
    }
    free(exclude_array);
    exclude_array=NULL;
}

static int exclude_dep(char *dep, char *versionclause) {
    int f;
    if (exclude_array==NULL) return(0);

    f=0;
    while(exclude_array[f]){
      if (!strcmp(exclude_array[f], dep)){
        return(1);
      }
      f++;
    }
    return(0);
}

106 107 108 109 110 111 112 113 114
static char *fieldcpy(char *dst, char *fld) {
    while (*fld && *fld != ':') 
        fld++;
    if (!*(fld++)) 
        return NULL;
    while (isspace(*fld)) fld++;
    return strcpy(dst, fld);
}

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
static void outputdeps(char *deps) {
    char *pch = deps;

    while (1) {
        while (isspace(*pch)) pch++;
        if (!*pch) break;

        while (*pch && *pch != '(' && *pch != '|' && *pch != ','
               && !isspace(*pch))
        {
            fputc(*pch++, stdout);
        }
        fputc('\n', stdout);
        while (*pch && *pch++ != ',') (void)NULL;
    }
}

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
typedef struct deps_proto {
  char *cur_pkg;
  int nd;
#define DEPCHUNK	16
  char ** deps;
} deps_t, *deps_p;

deps_p deps_new(void) {
  deps_p n = (deps_p) malloc(sizeof(deps_t));
  if (n==NULL) oom_die();
  memset(n, 0x00, sizeof(deps_t));
  n->cur_pkg = NULL;
  n->nd = 0;
  n->deps = NULL;
  return(n);
}

void deps_free(deps_p d) {
  int i;
  if (d==NULL) return;
  for(i=0;i<d->nd;i++){
    if (d->deps[i]) {
      free(d->deps[i]);
      d->deps[i] = NULL;
    }
  }
  d->nd = 0;
  free(d);
}

void deps_setpkgname(deps_p d, char *cur_pkg) {
  if (d->cur_pkg) free(d->cur_pkg);
  d->cur_pkg = strdup(cur_pkg);
}

void deps_add_4(deps_p d, char *dep) {
  int di;

  /* Typically, a package depends on 0-10 other ones. I can't be
     bothered to write a sort function for that. Slowsort it is. */
  for (di=0;di<d->nd;di++){
    if (!strcmp(d->deps[di], dep)) return;
  }

  /* extend if necessary */
  if ((d->nd % DEPCHUNK) == 0) {
    d->deps = (char **) realloc(d->deps, (d->nd + DEPCHUNK) * sizeof(char *));
    if (d->deps == NULL) oom_die();
    for(di=0;di < DEPCHUNK;di++) d->deps[d->nd + di] = NULL;
  }

  /* add it */
  d->deps[d->nd] = strdup(dep);
  fprintf(stderr,"DBG %s (pre-)depends on %s\n", d->cur_pkg, d->deps[d->nd]);
  d->nd++;
}

int deps_add_3(deps_p d, char *versioneddep) {
  char *workptr, *tok3, *saveptr3, *versionclause;
  int used;
  fprintf(stderr,"DBG deps_add_3(cur_pkg=%s , %s )\n",d->cur_pkg, versioneddep);
  workptr = strdup(versioneddep);
  tok3 = strtok_r(workptr, " (", &saveptr3);

  versionclause = strtok_r(NULL, " (", &saveptr3);
  /* versionclause is not used, but keep lint happy */
  if (versionclause) {
    while(*versionclause == ' ') versionclause++;
  }

  used = !exclude_dep(tok3, versionclause);

  if (!used) {
    fprintf(stderr,"W: pkgdetails: skip %s dependency on %s\n", d->cur_pkg, tok3);
  } else {
    /* add it to the list */
    deps_add_4(d, tok3);
  }

  free(workptr);

  return(used);
}

void deps_add_2(deps_p d, char *depsaltlist) {
  char *workptr, *tok2, *saveptr2;
  int used, chose_alternative;
  fprintf(stderr,"DBG deps_add_2(cur_pkg=%s , %s )\n",d->cur_pkg, depsaltlist);
  workptr = strdup(depsaltlist);
  tok2 = strtok_r(workptr, " |", &saveptr2);
  chose_alternative = 0;
  used = 0;
  while(tok2){
    used = deps_add_3(d, tok2);
    if (used) break;  // we want only the first alternative from the list
    tok2 = strtok_r(NULL, " |", &saveptr2);
    fprintf(stderr,"I: pkgdetails: consider %s dependency on %s\n", d->cur_pkg, tok2);
    chose_alternative = 1;
  }

  if (! used) {
    fprintf(stderr,"E: pkgdetails: none of the %s dependencies chosen from alternatives %s\n", d->cur_pkg, depsaltlist);
  }

  if (chose_alternative) {
    fprintf(stderr,"I: pkgdetails: used %s dependency on %s\n", d->cur_pkg, tok2);
  }
  free(workptr);
}

void deps_add(deps_p d, char *depslist) {
  char *workptr, *tok, *saveptr;
  fprintf(stderr,"DBG cur_pkg=%s doing (Pre-)depends on %s\n",d->cur_pkg, depslist);

  /* skip initial space */
  while ((depslist[0])&&(isspace(depslist[0]))) depslist++;
  workptr = strdup(depslist);
  tok = strtok_r(workptr, ",", &saveptr);
  while(tok){
    deps_add_2(d, tok);
    tok = strtok_r(NULL, ",", &saveptr);
  }
  free(workptr);
}

void deps_output(FILE *f, deps_p d) {
  int i;
  if ((f==NULL)||(d==NULL)||(d->nd==0)) return;

  for(i=0;i<d->nd;i++){
    fputs(d->deps[i], f);
    fputs("\n", f);
  }
}

/* The syntax of a Debian Depends: or Pre-Depends: line is as follows:

   Depends: deps
   Pre-Depends: deps
   deps = dep | deps "," dep
   dep = versioneddep | altdep
   altdep = versioneddep "|" versioneddep | versioneddep "|" altdep
   versioneddep = simpledep | simpledep "(" compareclause ")"
   simpledep = packagename

   It needs to be parsed accurately to properly do alternatives, if we
   don't like the first choice of an altdep list (hint: init)
 */
280 281 282 283
static void dogetdeps(char *pkgsfile, char **in_pkgs, int pkgc) {
    char buf[MAX_LINE];
    char cur_pkg[MAX_LINE];
    char cur_deps[MAX_LINE];
284 285
    char cur_predeps[MAX_LINE];
    char prev_pkg[MAX_LINE];
286 287 288 289
    char *pkgs[MAX_PKGS];
    int i;
    int skip;
    FILE *f;
290
    int output_pkg = -1;
291
    deps_p curdeps = NULL;
292

293
    cur_pkg[0] = cur_deps[0] = cur_predeps[0] = prev_pkg[0] = '\0';
294 295 296 297 298 299 300 301 302

    for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];

    f = fopen(pkgsfile, "r");
    if (f == NULL) {
        perror(pkgsfile);
        exit(1);
    }

303 304
    curdeps = deps_new();

305 306 307 308 309 310 311
    skip = 1;
    while (fgets(buf, sizeof(buf), f)) {
        if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
        if (strncasecmp(buf, "Package:", 8) == 0) {
            int any = 0;
            skip = 1;
            fieldcpy(cur_pkg, buf);
312
            deps_setpkgname(curdeps, cur_pkg);
313 314 315 316 317 318 319 320 321 322 323
            if (strcmp(cur_pkg, prev_pkg) != 0) {
                if (output_pkg != -1)
                    pkgs[output_pkg] = NULL;
                if (cur_deps[0])
                    outputdeps(cur_deps);
                if (cur_predeps[0])
                    outputdeps(cur_predeps);
                strcpy(prev_pkg, cur_pkg);
            }
            cur_deps[0] = cur_predeps[0] = '\0';
            output_pkg = -1;
324 325 326 327 328
	    for (i = 0; i < pkgc; i++) {
		if (!pkgs[i]) continue;
		any = 1;
                if (strcmp(cur_pkg, pkgs[i]) == 0) {
                    skip = 0;
329
                    output_pkg = i;
330 331 332 333
                    break;
                }
            }
            if (!any) break;
334
        } else if (!skip && strncasecmp(buf, "Depends:", 8) == 0)
335 336 337
{
		deps_add(curdeps, &buf[8]);
}
338
        else if (!skip && strncasecmp(buf, "Pre-Depends:", 12) == 0)
339 340 341
{
		deps_add(curdeps, &buf[12]);
}
342
    }
343 344 345

    deps_output(stdout, curdeps);

346
    fclose(f);
347 348

    deps_free(curdeps);
349 350 351 352 353 354 355 356 357 358 359
}

static void dopkgmirrorpkgs(int uniq, char *mirror, char *pkgsfile, 
        char *fieldname, char **in_pkgs, int pkgc) 
{
    char buf[MAX_LINE];
    char cur_field[MAX_LINE];
    char cur_pkg[MAX_LINE];
    char cur_ver[MAX_LINE];
    char cur_arch[MAX_LINE];
    char cur_size[MAX_LINE];
360
    char cur_checksum[MAX_LINE];
361
    char cur_filename[MAX_LINE];
362
    char prev_pkg[MAX_LINE];
363 364 365
    char *pkgs[MAX_PKGS];
    int i;
    FILE *f;
366 367
    char *output = NULL;
    int output_pkg = -1;
368

369
    cur_field[0] = cur_pkg[0] = cur_ver[0] = cur_arch[0] = cur_filename[0] = prev_pkg[0] = '\0';
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384

    for (i = 0; i < pkgc; i++) pkgs[i] = in_pkgs[i];

    f = fopen(pkgsfile, "r");
    if (f == NULL) {
        perror(pkgsfile);
        exit(1);
    }
    while (fgets(buf, sizeof(buf), f)) {
        if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
        if (strncasecmp(buf, fieldname, strlen(fieldname)) == 0) {
            fieldcpy(cur_field, buf);
	}
        if (strncasecmp(buf, "Package:", 8) == 0) {
            fieldcpy(cur_pkg, buf);
385 386 387 388 389 390 391 392 393 394
            if (strcmp(cur_pkg, prev_pkg) != 0) {
                if (output)
                    fputs(output, stdout);
                if (uniq && output_pkg != -1)
                    pkgs[output_pkg] = NULL;
                strcpy(prev_pkg, cur_pkg);
            }
            free(output);
            output = NULL;
            output_pkg = -1;
395 396 397 398 399 400
        } else if (strncasecmp(buf, "Version:", 8) == 0) {
            fieldcpy(cur_ver, buf);
        } else if (strncasecmp(buf, "Architecture:", 13) == 0) {
            fieldcpy(cur_arch, buf);
        } else if (strncasecmp(buf, "Size:", 5) == 0) {
            fieldcpy(cur_size, buf);
401 402 403
        } else if (strncasecmp(buf, checksum_field, strlen(checksum_field)) == 0
	           && buf[strlen(checksum_field)] == ':') {
            fieldcpy(cur_checksum, buf);
404 405 406 407 408 409 410 411
        } else if (strncasecmp(buf, "Filename:", 9) == 0) {
            fieldcpy(cur_filename, buf);
        } else if (!*buf) {
	    int any = 0;
	    for (i = 0; i < pkgc; i++) {
		if (!pkgs[i]) continue;
		any = 1;
                if (strcmp(cur_field, pkgs[i]) == 0) {
412 413 414
                    free(output);
                    output = xasprintf("%s %s %s %s %s %s %s\n", cur_pkg, cur_ver, cur_arch, mirror, cur_filename, cur_checksum, cur_size);
                    output_pkg = i;
415 416 417 418
		    break;
		}
            }
	    if (!any) break;
419
            cur_field[0] = '\0';
420 421
        }
    }
422 423 424 425
    if (output)
        fputs(output, stdout);
    if (uniq && output_pkg != -1)
        pkgs[output_pkg] = NULL;
426 427 428 429 430 431 432 433 434 435
    fclose(f);

    /* any that weren't found are returned as "pkg -" */
    if (uniq) {
        for (i = 0; i < pkgc; i++) {
            if (pkgs[i]) {
                printf("%s -\n", pkgs[i]);
            }
        }
    }
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
static void dopkgstanzas(char *pkgsfile, char **pkgs, int pkgc)
{
    char buf[MAX_LINE];
    char *accum;
    size_t accum_size = 0, accum_alloc = MAX_LINE * 2;
    char cur_pkg[MAX_LINE];
    FILE *f;

    accum = malloc(accum_alloc);
    if (!accum)
        oom_die();

    f = fopen(pkgsfile, "r");
    if (f == NULL) {
        perror(pkgsfile);
        free(accum);
        exit(1);
    }
    while (fgets(buf, sizeof(buf), f)) {
        if (*buf) {
	    size_t len = strlen(buf);
            if (accum_size + len + 1 > accum_alloc) {
                accum_alloc = (accum_size + len + 1) * 2;
                accum = realloc(accum, accum_alloc);
                if (!accum)
                    oom_die();
            }
            strcpy(accum + accum_size, buf);
	    accum_size += len;
        }
        if (*buf && buf[strlen(buf)-1] == '\n') buf[strlen(buf)-1] = '\0';
        if (strncasecmp(buf, "Package:", 8) == 0) {
            fieldcpy(cur_pkg, buf);
        } else if (!*buf) {
            int i;
            for (i = 0; i < pkgc; i++) {
                if (!pkgs[i]) continue;
                if (strcmp(cur_pkg, pkgs[i]) == 0) {
                    fputs(accum, stdout);
		    if (accum[accum_size - 1] != '\n')
			fputs("\n\n", stdout);
		    else if (accum[accum_size - 2] != '\n')
			fputc('\n', stdout);
                    break;
                }
            }
            *accum = '\0';
            accum_size = 0;
        }
    }
    fclose(f);

    free(accum);
}

494 495 496
static int dotranslatewgetpercent(int low, int high, int end, char *str) {
    int ch;
    int val, lastval;
497
    int allow_percentage;
498 499 500 501 502

    /* print out anything that looks like a % on its own line, appropriately
     * scaled */

    lastval = val = 0;
503
    allow_percentage = 0;
504
    while ( (ch = getchar()) != EOF ) {
505 506 507
	if (isspace(ch)) {
	    allow_percentage = 1;
	} else if (allow_percentage && isdigit(ch)) {
508
	    val *= 10; val += ch - '0';
509
	} else if (allow_percentage && ch == '%') {
510 511 512 513 514 515 516 517 518
	    float f = (float) val / 100.0 * (high - low) + low;
	    if (str) {
	    	printf("P: %d %d %s\n", (int) f, end, str);
	    } else {
	    	printf("P: %d %d\n", (int) f, end);
	    }
	    lastval = val;
	} else {
	    val = 0;
519
	    allow_percentage = 0;
520 521 522 523 524 525
	}
    }
    return lastval == 100;
}

int main(int argc, char *argv[]) {
526 527 528 529 530
    checksum_field=getenv("DEBOOTSTRAP_CHECKSUM_FIELD");
    if (checksum_field == NULL) {
        checksum_field="MD5sum";
    }

531 532 533 534 535 536 537
    exclude_field=getenv("DEBOOTSTRAP_EXCLUDE_FIELD");
    if (exclude_field != NULL) {
       parse_exclude_field(exclude_field);
    } else {
       exclude_array = NULL;
    }

538 539 540 541 542 543 544 545 546 547 548 549 550 551
    if ((argc == 6 || argc == 5) && strcmp(argv[1], "WGET%") == 0) {
	if (dotranslatewgetpercent(atoi(argv[2]), atoi(argv[3]), 
	                           atoi(argv[4]), argc == 6 ? argv[5] : NULL))
	{
	    exit(0);
	} else {
	    exit(1);
	}
    } else if (argc >= 4 && strcmp(argv[1], "GETDEPS") == 0) {
        int i;
        for (i = 3; argc - i > MAX_PKGS; i += MAX_PKGS) {
	    dogetdeps(argv[2], argv+i, MAX_PKGS);
	}
	dogetdeps(argv[2], argv+i, argc-i);
552 553
	printf("crash_here\n");
        free_exclude_array();
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
	exit(0);
    } else if (argc >= 5 && strcmp(argv[1], "PKGS") == 0) {
        int i;
        for (i = 4; argc - i > MAX_PKGS; i += MAX_PKGS) {
	    dopkgmirrorpkgs(1, argv[2], argv[3], "Package:", argv+i, MAX_PKGS);
	}
	dopkgmirrorpkgs(1, argv[2], argv[3], "Package:", argv+i, argc-i);
	exit(0);
    } else if (argc >= 6 && strcmp(argv[1], "FIELD") == 0) {
        int i;
        for (i = 5; argc - i > MAX_PKGS; i += MAX_PKGS) {
	    dopkgmirrorpkgs(0, argv[3], argv[4], argv[2], argv+i, MAX_PKGS);
	}
	dopkgmirrorpkgs(0, argv[3], argv[4], argv[2], argv+i, argc-i);
	exit(0);
569 570 571 572 573 574 575
    } else if (argc >= 4 && strcmp(argv[1], "STANZAS") == 0) {
	int i;
	for (i = 3; argc - i > MAX_PKGS; i += MAX_PKGS) {
	    dopkgstanzas(argv[2], argv+i, MAX_PKGS);
	}
	dopkgstanzas(argv[2], argv+i, argc-i);
	exit(0);
576 577 578 579 580
    } else {
        fprintf(stderr, "usage: %s PKGS mirror packagesfile pkgs..\n", argv[0]);
        fprintf(stderr, "   or: %s FIELD field mirror packagesfile pkgs..\n", 
                argv[0]);
        fprintf(stderr, "   or: %s GETDEPS packagesfile pkgs..\n", argv[0]);
581
        fprintf(stderr, "   or: %s STANZAS packagesfile pkgs..\n", argv[0]);
582 583 584 585
	fprintf(stderr, "   or: %s WGET%% low high end reason\n", argv[0]);
        exit(1);
    }
}