~ubuntu-branches/debian/stretch/zsync/stretch

« back to all changes in this revision

Viewing changes to make.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Lemmen
  • Date: 2009-06-03 20:19:00 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090603201900-uq7n7ozqv862odzt
Tags: 0.6.1-1
New upstream release (closes: #444159, #469919)

Show diffs side-by-side

added added

removed removed

Lines of Context:
446
446
 
447
447
/* opt_str = guess_gzip_options(filename_str)
448
448
 * For the given (gzip) file, try to guess the options that were used with gzip
449
 
 * to create it. Returns the option string for gzip, or NULL */
 
449
 * to create it.
 
450
 * Returns a malloced string containing the options for gzip, or NULL */
450
451
static const char *const try_opts[] =
451
452
    { "--best", "", "--rsync", "--rsync --best", NULL };
452
453
#define SAMPLE 1024
453
454
 
454
 
const char *guess_gzip_options(const char *f) {
 
455
char *guess_gzip_options(const char *f) {
455
456
    char orig[SAMPLE];
456
457
    {   /* Read sample of the header of the compressed file */
457
458
        FILE *s = fopen(f, "r");
466
467
        int i;
467
468
        const char *o;
468
469
        char *enc_f = encode_filename(f);
 
470
        int has_mtime_fname;
 
471
 
 
472
        {
 
473
            int has_mtime = zhead_has_mtime(orig);
 
474
            int has_fname = zhead_has_fname(orig);
 
475
 
 
476
            if (has_mtime && !has_fname) {
 
477
                fprintf(stderr, "can't recompress, stream has mtime but no fname\n");
 
478
                return NULL;
 
479
            }
 
480
            else if (has_fname && !has_mtime) {
 
481
                fprintf(stderr, "can't recompress, stream has fname but no mtime\n");
 
482
                return NULL;
 
483
            }
 
484
            else {
 
485
                has_mtime_fname = has_fname; /* which = has_mtime */
 
486
            }
 
487
        }
469
488
 
470
489
        /* For each likely set of options, try recompressing the content with
471
490
         * those options */
472
491
        for (i = 0; (o = try_opts[i]) != NULL; i++) {
473
 
            char cmd[1024];
474
 
            snprintf(cmd, sizeof(cmd), "zcat %s | gzip -n %s 2> /dev/null",
475
 
                     enc_f, o);
 
492
            FILE *p;
 
493
            {   /* Compose command line */
 
494
                char cmd[1024];
 
495
                snprintf(cmd, sizeof(cmd), "zcat %s | gzip -n %s 2> /dev/null",
 
496
                        enc_f, o);
476
497
 
477
 
            {   /* Read the recompressed content */
478
 
                FILE *p = popen(cmd, "r");
479
 
                char samp[SAMPLE];
 
498
                /* And run it */
480
499
                if (verbose)
481
500
                    fprintf(stderr, "running %s to determine gzip options\n",
482
501
                            cmd);
 
502
                p = popen(cmd, "r");
483
503
                if (!p) {
484
504
                    perror(cmd);
485
505
                }
486
 
                else if (!read_sample_and_close(p, SAMPLE, samp)) {
487
 
                    ;
 
506
            }
 
507
 
 
508
            if (p) {   /* Read the recompressed content */
 
509
                char samp[SAMPLE];
 
510
                if (!read_sample_and_close(p, SAMPLE, samp)) {
 
511
                    ;       /* Read error - just fail this one and let the loop
 
512
                             * try another */
488
513
                }
489
514
                else {
490
 
 
491
515
                    /* We have the compressed version with these options.
492
516
                     * Compare with the original */
493
517
                    const char *a = skip_zhead(orig);
498
522
            }
499
523
        }
500
524
        free(enc_f);
501
 
        return o;
 
525
 
 
526
        if (!o) {
 
527
            return NULL;
 
528
        }
 
529
        else if (has_mtime_fname) {
 
530
            return strdup(o);
 
531
        }
 
532
        else {  /* Add --no-name to options to return */
 
533
            static const char noname[] = { "--no-name" };
 
534
            char* opts = malloc(strlen(o)+strlen(noname)+2);
 
535
            if (o[0]) {
 
536
                strcpy(opts, o);
 
537
                strcat(opts, " ");
 
538
            }
 
539
            else { opts[0] = 0; }
 
540
            strcat(opts, noname);
 
541
            return opts;
 
542
        }
502
543
    }
503
544
}
504
545
 
530
571
    int do_compress = 0;
531
572
    int do_recompress = -1;     // -1 means we decide for ourselves
532
573
    int do_exact = 0;
533
 
    const char *gzopts = NULL;
 
574
    char *gzopts = NULL;
534
575
    time_t mtime = -1;
535
576
 
536
577
    /* Open temporary file */
671
712
    read_stream_write_blocksums(instream, tf);
672
713
 
673
714
    {   /* Decide how long a rsum hash and checksum hash per block we need for this file */
674
 
        seq_matches = 2;
675
 
        rsum_len =
676
 
            (7.9 +
677
 
             ((log(len) + log(blocksize)) / log(2) - 8.6) / seq_matches) / 8;
 
715
        seq_matches = len > blocksize ? 2 : 1;
 
716
        rsum_len = ceil(((log(len) + log(blocksize)) / log(2) - 8.6) / seq_matches / 8);
678
717
 
679
718
        /* min and max lengths of rsums to store */
680
719
        if (rsum_len > 4) rsum_len = 4;
681
720
        if (rsum_len < 2) rsum_len = 2;
682
721
 
683
722
        /* Now the checksum length; min of two calculations */
684
 
        checksum_len =
685
 
            (7.9 +
686
 
             (20 +
687
 
              (log(len) +
688
 
               log(1 + len / blocksize)) / log(2)) / seq_matches) / 8;
 
723
        checksum_len = ceil(
 
724
                (20 + (log(len) + log(1 + len / blocksize)) / log(2))
 
725
                / seq_matches / 8);
689
726
        {
690
727
            int checksum_len2 =
691
728
                (7.9 + (20 + log(1 + len / blocksize) / log(2))) / 8;
835
872
 
836
873
    if (do_recompress)      /* Write Recompress header if wanted */
837
874
        fprintf(fout, "Recompress: %s %s\n", zhead, gzopts);
 
875
    if (gzopts)
 
876
        free(gzopts);
838
877
 
839
878
    /* If we have a zmap, write it, header first and then the map itself */
840
879
    if (zmapentries) {