예제 #1
0
void
hcache_init()
{
    HCACHEDATA  cachedata, *c;
    FILE	*f;
    char	*version;
    int		header_count = 0;
    char*	hcachename;

    hcachehash = hashinit (sizeof (HCACHEDATA), "hcache");

    if (! (hcachename = cache_name()))
	return;

    if (! (f = fopen (hcachename, "rb" )))
	return;
    
    version = read_netstring(f);
    if (!version || strcmp(version, CACHE_FILE_VERSION)) {
	fclose(f);
	return;
    }

    while (1)
    {
	char* record_type;
	char *time_str;
	char *age_str;
	char *includes_count_str;
	char *hdrscan_count_str;
	int i, count;
	LIST *l;

	record_type = read_netstring(f);
	if (!record_type) {
	    fprintf(stderr, "invalid %s\n", hcachename);
	    goto bail;
	}
	if (!strcmp(record_type, CACHE_RECORD_END)) {
	    break;
	}
	if (strcmp(record_type, CACHE_RECORD_HEADER)) {
	    fprintf(stderr, "invalid %s with record separator <%s>\n",
		    hcachename, record_type ? record_type : "<null>");
	    goto bail;
	}
	
	c = &cachedata;
	    
	c->boundname = read_netstring(f);
	time_str = read_netstring(f);
	age_str = read_netstring(f);
	includes_count_str = read_netstring(f);
	
	if (!c->boundname || !time_str || !age_str
	    || !includes_count_str)
	{
	    fprintf(stderr, "invalid %s\n", hcachename);
	    goto bail;
	}

	c->time = atoi(time_str);
	c->age = atoi(age_str) + 1;

	count = atoi(includes_count_str);
	for (l = 0, i = 0; i < count; i++) {
	    char* s = read_netstring(f);
	    if (!s) {
		fprintf(stderr, "invalid %s\n", hcachename);
		goto bail;
	    }
	    l = list_new(l, s);
	}
	c->includes = l;

	hdrscan_count_str = read_netstring(f);
	if (!includes_count_str) {
	    list_free(c->includes);
	    fprintf(stderr, "invalid %s\n", hcachename);
	    goto bail;
	}

	count = atoi(hdrscan_count_str);
	for (l = 0, i = 0; i < count; i++) {
	    char* s = read_netstring(f);
	    if (!s) {
		fprintf(stderr, "invalid %s\n", hcachename);
		goto bail;
	    }
	    l = list_new(l, s);
	}
	c->hdrscan = l;

	if (!hashenter(hcachehash, (HASHDATA **)&c)) {
	    fprintf(stderr, "can't insert header cache item, bailing on %s\n",
		    hcachename);
	    goto bail;
	}

	c->next = hcachelist;
	hcachelist = c;

	header_count++;
    }

    if (DEBUG_HEADER) {
	printf("hcache read from file %s\n", hcachename);
    }
    
 bail:
    fclose(f);
}
예제 #2
0
파일: hcache.c 프로젝트: 0xDEC0DE8/mcsema
void hcache_init()
{
    FILE       * f;
    OBJECT     * version = 0;
    int          header_count = 0;
    const char * hcachename;

    if ( hcachehash )
        return;

    hcachehash = hashinit( sizeof( HCACHEDATA ), "hcache" );

    if ( !( hcachename = cache_name() ) )
        return;

    if ( !( f = fopen( hcachename, "rb" ) ) )
        return;

    version = read_netstring( f );

    if ( !version || strcmp( object_str( version ), CACHE_FILE_VERSION ) )
        goto bail;

    while ( 1 )
    {
        HCACHEDATA   cachedata;
        HCACHEDATA * c;
        OBJECT * record_type = 0;
        OBJECT * time_str = 0;
        OBJECT * age_str = 0;
        OBJECT * includes_count_str = 0;
        OBJECT * hdrscan_count_str = 0;
        int      i;
        int      count;
        LIST   * l;
        int      found;

        cachedata.boundname = 0;
        cachedata.includes = 0;
        cachedata.hdrscan = 0;

        record_type = read_netstring( f );
        if ( !record_type )
        {
            fprintf( stderr, "invalid %s\n", hcachename );
            goto cleanup;
        }
        if ( !strcmp( object_str( record_type ), CACHE_RECORD_END ) )
        {
            object_free( record_type );
            break;
        }
        if ( strcmp( object_str( record_type ), CACHE_RECORD_HEADER ) )
        {
            fprintf( stderr, "invalid %s with record separator <%s>\n",
                hcachename, record_type ? object_str( record_type ) : "<null>" );
            goto cleanup;
        }

        cachedata.boundname = read_netstring( f );
        time_str            = read_netstring( f );
        age_str             = read_netstring( f );
        includes_count_str  = read_netstring( f );

        if ( !cachedata.boundname || !time_str || !age_str || !includes_count_str )
        {
            fprintf( stderr, "invalid %s\n", hcachename );
            goto cleanup;
        }

        cachedata.time = atoi( object_str( time_str ) );
        cachedata.age = atoi( object_str( age_str ) ) + 1;

        count = atoi( object_str( includes_count_str ) );
        for ( l = L0, i = 0; i < count; ++i )
        {
            OBJECT * s = read_netstring( f );
            if ( !s )
            {
                fprintf( stderr, "invalid %s\n", hcachename );
                list_free( l );
                goto cleanup;
            }
            l = list_push_back( l, s );
        }
        cachedata.includes = l;

        hdrscan_count_str = read_netstring( f );
        if ( !hdrscan_count_str )
        {
            fprintf( stderr, "invalid %s\n", hcachename );
            goto cleanup;
        }

        count = atoi( object_str( hdrscan_count_str ) );
        for ( l = L0, i = 0; i < count; ++i )
        {
            OBJECT * s = read_netstring( f );
            if ( !s )
            {
                fprintf( stderr, "invalid %s\n", hcachename );
                list_free( l );
                goto cleanup;
            }
            l = list_push_back( l, s );
        }
        cachedata.hdrscan = l;

        c = (HCACHEDATA *)hash_insert( hcachehash, cachedata.boundname, &found );
        if ( !found )
        {
            c->boundname = cachedata.boundname;
            c->time      = cachedata.time;
            c->includes  = cachedata.includes;
            c->hdrscan   = cachedata.hdrscan;
            c->age       = cachedata.age;
        }
        else
        {
            fprintf( stderr, "can't insert header cache item, bailing on %s\n",
                hcachename );
            goto cleanup;
        }

        c->next = hcachelist;
        hcachelist = c;

        ++header_count;
        
        object_free( record_type );
        object_free( time_str );
        object_free( age_str );
        object_free( includes_count_str );
        object_free( hdrscan_count_str );
        continue;

cleanup:

        if ( record_type ) object_free( record_type );
        if ( time_str ) object_free( time_str );
        if ( age_str ) object_free( age_str );
        if ( includes_count_str ) object_free( includes_count_str );
        if ( hdrscan_count_str ) object_free( hdrscan_count_str );

        if ( cachedata.boundname ) object_free( cachedata.boundname );
        if ( cachedata.includes ) list_free( cachedata.includes );
        if ( cachedata.hdrscan ) list_free( cachedata.hdrscan );

        goto bail;
    }

    if ( DEBUG_HEADER )
        printf( "hcache read from file %s\n", hcachename );

bail:
    if ( version )
        object_free( version );
    fclose( f );
}