示例#1
0
/* Destructor */
char *zsync_end(struct zsync_state *zs) {
    int i;
    char *f = zsync_cur_filename(zs);

    /* Free rcksum object and zmap */
    if (zs->rs)
        rcksum_end(zs->rs);
    if (zs->zmap)
        zmap_free(zs->zmap);

    /* Clear download URLs */
    for (i = 0; i < zs->nurl; i++)
        free(zs->url[i]);
    for (i = 0; i < zs->nzurl; i++)
        free(zs->zurl[i]);

    /* And the rest. */
    free(zs->url);
    free(zs->zurl);
    free(zs->checksum);
    free(zs->filename);
    free(zs->zfilename);
    free(zs);
    return f;
}
示例#2
0
/* zsync_complete(self)
 * Finish a zsync download. Should be called once all blocks have been
 * retrieved successfully. This returns 0 if the file passes the final
 * whole-file checksum and if any recompression requested by the .zsync file is
 * done.
 * Returns -1 on error (and prints the error to stderr)
 *          0 if successful but no checksum verified
 *          1 if successful including checksum verified
 */
int zsync_complete(struct zsync_state *zs) {
    int rc = 0;

    /* We've finished with the rsync algorithm. Take over the local copy from
     * librcksum and free our rcksum state. */
    int fh = rcksum_filehandle(zs->rs);
    zsync_cur_filename(zs);
    rcksum_end(zs->rs);
    zs->rs = NULL;

    /* Truncate the file to the exact length (to remove any trailing NULs from
     * the last block); return to the start of the file ready to verify. */
    if (ftruncate(fh, zs->filelen) != 0) {
        perror("ftruncate");
        rc = -1;
    }
    if (lseek(fh, 0, SEEK_SET) != 0) {
        perror("lseek");
        rc = -1;
    }

    /* Do checksum check */
    if (rc == 0 && zs->checksum && !strcmp(zs->checksum_method, ckmeth_sha1)) {
        rc = zsync_sha1(zs, fh);
    }
    close(fh);

    /* Do any requested recompression */
    if (rc >= 0 && zs->gzhead && zs->gzopts) {
        if (zsync_recompress(zs) != 0) {
            return -1;
        }
    }
    return rc;
}
示例#3
0
文件: zsync.c 项目: baggiogo/zsync-1
/* zsync_rename_file(self, filename)
 * Tell libzsync to move the local copy of the target (or under construction
 * target) to the given filename. */
int zsync_rename_file(struct zsync_state *zs, const char *f) {
    char *rf = zsync_cur_filename(zs);

    int x = rename(rf, f);

    if (!x) {
        free(rf);
        zs->cur_filename = strdup(f);
    }
    else
        perror("rename");

    return x;
}