Ejemplo n.º 1
0
void
hcache_done()
{
    FILE	*f;
    HCACHEDATA  *c;
    int 	header_count = 0;
    char*	hcachename;
    int		maxage;
    
    if (!hcachehash)
	return;

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

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

    maxage = cache_maxage();

    /* print out the version */
    write_netstring(f, CACHE_FILE_VERSION);

    c = hcachelist;
    for (c = hcachelist; c; c = c->next) {
	LIST	*l;
	char time_str[30];
	char age_str[30];
	char includes_count_str[30];
	char hdrscan_count_str[30];

	if (maxage == 0)
	    c->age = 0;
	else if (c->age > maxage)
	    continue;

	sprintf(includes_count_str, "%lu", list_length(c->includes));
	sprintf(hdrscan_count_str, "%lu", list_length(c->hdrscan));
	sprintf(time_str, "%lu", c->time);
	sprintf(age_str, "%lu", c->age);

	write_netstring(f, CACHE_RECORD_HEADER);
	write_netstring(f, c->boundname);
	write_netstring(f, time_str);
	write_netstring(f, age_str);
	write_netstring(f, includes_count_str);
	for (l = c->includes; l; l = list_next(l)) {
	    write_netstring(f, l->string);
	}
	write_netstring(f, hdrscan_count_str);
	for (l = c->hdrscan; l; l = list_next(l)) {
	    write_netstring(f, l->string);
	}
	fputs("\n", f);
	header_count++;
    }
    write_netstring(f, CACHE_RECORD_END);

    if (DEBUG_HEADER) {
	printf("hcache written to %s.   %d dependencies, %.0f%% hit rate\n",
	       hcachename, header_count,
	       queries ? 100.0 * hits / queries : 0);
    }

    fclose (f);
}
Ejemplo n.º 2
0
void hcache_done()
{
    FILE       * f;
    HCACHEDATA * c;
    int          header_count = 0;
    const char * hcachename;
    int          maxage;

    if ( !hcachehash )
        return;

    if ( !( hcachename = cache_name() ) )
        goto cleanup;

    if ( !( f = fopen( hcachename, "wb" ) ) )
        goto cleanup;

    maxage = cache_maxage();

    /* Print out the version. */
    write_netstring( f, CACHE_FILE_VERSION );

    c = hcachelist;
    for ( c = hcachelist; c; c = c->next )
    {
        LISTITER iter, end;
        char   time_str[ 30 ];
        char   age_str[ 30 ];
        char   includes_count_str[ 30 ];
        char   hdrscan_count_str[ 30 ];

        if ( maxage == 0 )
            c->age = 0;
        else if ( c->age > maxage )
            continue;

        sprintf( includes_count_str, "%lu", (long unsigned) list_length( c->includes ) );
        sprintf( hdrscan_count_str, "%lu", (long unsigned) list_length( c->hdrscan ) );
        sprintf( time_str, "%lu", (long unsigned) c->time );
        sprintf( age_str, "%lu", (long unsigned) c->age );

        write_netstring( f, CACHE_RECORD_HEADER );
        write_netstring( f, object_str( c->boundname ) );
        write_netstring( f, time_str );
        write_netstring( f, age_str );
        write_netstring( f, includes_count_str );
        for ( iter = list_begin( c->includes ), end = list_end( c->includes );
            iter != end; iter = list_next( iter ) )
            write_netstring( f, object_str( list_item( iter ) ) );
        write_netstring( f, hdrscan_count_str );
        for ( iter = list_begin( c->hdrscan ), end = list_end( c->hdrscan );
            iter != end; iter = list_next( iter ) )
            write_netstring( f, object_str( list_item( iter ) ) );
        fputs( "\n", f );
        ++header_count;
    }
    write_netstring( f, CACHE_RECORD_END );

    if ( DEBUG_HEADER )
        printf( "hcache written to %s.   %d dependencies, %.0f%% hit rate\n",
            hcachename, header_count, queries ? 100.0 * hits / queries : 0 );

    fclose ( f );
    
cleanup:
    for ( c = hcachelist; c; c = c->next )
    {
        list_free( c->includes );
        list_free( c->hdrscan );
        object_free( c->boundname );
    }

    hcachelist = 0;
    if ( hcachehash )
        hashdone( hcachehash );
    hcachehash = 0;
}
Ejemplo n.º 3
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);
}
Ejemplo n.º 4
0
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 );
}