Esempio n. 1
0
struct mkuz_blk_info *
mkuz_blkcache_regblock(int fd, const struct mkuz_blk *bp)
{
    struct mkuz_blkcache_itm *bcep;
    int rval;
    unsigned char h;

#if defined(MKUZ_DEBUG)
    assert((unsigned)lseek(fd, 0, SEEK_CUR) == bp->info.offset);
#endif
    h = digest_fold(bp->info.digest);
    if (blkcache.first[h].hit.len == 0) {
        bcep = &blkcache.first[h];
    } else {
        for (bcep = &blkcache.first[h]; bcep != NULL; bcep = bcep->next) {
            if (bcep->hit.len != bp->info.len)
                continue;
            if (memcmp(bp->info.digest, bcep->hit.digest,
              sizeof(bp->info.digest)) == 0) {
                break;
            }
        }
        if (bcep != NULL) {
            rval = verify_match(fd, bp, bcep);
            if (rval == 1) {
#if defined(MKUZ_DEBUG)
                fprintf(stderr, "cache hit %jd, %jd, %jd, %jd\n",
                  I2J(bcep->hit.blkno), I2J(bcep->hit.offset),
                  I2J(bp->info.offset), I2J(bp->info.len));
#endif
                return (&bcep->hit);
            }
            if (rval == 0) {
#if defined(MKUZ_DEBUG)
                fprintf(stderr, "block MD5 collision, you should try lottery, "
                  "man!\n");
#endif
                return (NULL);
            }
            warn("verify_match");
            return (NULL);
        }
        bcep = malloc(sizeof(struct mkuz_blkcache_itm));
        if (bcep == NULL)
            return (NULL);
        memset(bcep, '\0', sizeof(struct mkuz_blkcache_itm));
        bcep->next = blkcache.first[h].next;
        blkcache.first[h].next = bcep;
    }
    bcep->hit = bp->info;
    return (NULL);
}
Esempio n. 2
0
/**
 * Main function
 */
int main(int argc, char **argv) {
    /* The line read, the filename and the position in the file */
    char *line = malloc(1024 * sizeof(char));
    char *file_name = malloc(1024 * sizeof(char));
    unsigned long long position;

    /* The search pattern */
    char *pattern;
    int pattern_size;

    /* Read the pattern */
    if(argc < 2) {
        printf("Usage: %s <pattern file>\n", argv[0]);
        return 1;
    }

    /* Read in the pattern */
    pattern = read_entire_file(argv[1], &pattern_size);

    fgets(line, 1024, stdin);
    while(!feof(stdin)) {
        /* Read and parse */
        parse_line(line, file_name, &position);
        verify_match(pattern, pattern_size, file_name, position);

        fgets(line, 1024, stdin);
    }
    parse_line(line, file_name, &position);

    /* Free up */
    free(line);
    free(file_name);
    free(pattern);

    return 0;
}