示例#1
0
文件: hcache.c 项目: larus/jamplus
void
hcache_readfile(HCACHEFILE *file)
{
	HCACHEDATA	cachedata, *c, *last = 0;
	FILE	*f;
	int		bad_cache = 1, ch;
	const char	*version;
	BUFFER	buff;
	long	buffsize;

/*    if( ! (hcachename = hcache_filename()) )
	return;*/

	if( ! (f = fopen( file->cachefilename, "rb" )) )
		return;

	fseek( f, 0, SEEK_END );
	buffsize = ftell( f );
	fseek( f, 0, SEEK_SET );
	buffer_init( &buff );
	buffer_resize( &buff, buffsize + 1 );
	if ( fread( buffer_ptr( &buff ), buffsize, 1, f ) != 1 )
	{
		fclose( f );
		goto bail;
	}
	buffer_ptr( &buff )[buffsize] = 0;
	fclose( f );

	version = read_string( &buff );
	ch = buffer_getchar( &buff );
	if (!version || strcmp( version, CACHE_FILE_VERSION ) || ch != '\n' ) {
		goto bail;
	}

	for(;;) {
		int i, count, ch;
		LIST *l;

		c = &cachedata;

		c->boundname = read_string( &buff );
		if( !c->boundname ) /* Test for eof */
			break;

		c->time = read_int( &buff );
		c->age = read_int( &buff ) + 1; /* we're getting older... */

#ifdef OPT_BUILTIN_MD5CACHE_EXT
		c->mtime = read_int( &buff );
		read_md5sum( &buff, c->rulemd5sum );
		memcpy( &c->currentrulemd5sum, &c->rulemd5sum, MD5_SUMSIZE );
		read_md5sum( &buff, c->contentmd5sum );
		memcpy( &c->currentcontentmd5sum, &c->contentmd5sum, MD5_SUMSIZE );
#endif

		if( !c->boundname )
			goto bail;

		/* headers */
		count = read_int( &buff );
		for( l = 0, i = 0; i < count; ++i ) {
			const char *s = read_string( &buff );
			if( !s )
				goto bail;
			l = list_new( l, s, 0 );
		}
		c->includes = l;

		/* hdrscan */
		count = read_int( &buff );
		for( l = 0, i = 0; i < count; ++i ) {
			const char *s = read_string( &buff );
			if( !s )
				goto bail;
			l = list_new( l, s, 0 );
		}
		c->hdrscan = l;

		/* Read the newline */
		ch = skip_spaces( &buff );
		if( ch != '!' )
			goto bail;
		ch = skip_spaces( &buff );
		if( ch != '\n' )
			goto bail;

		if( !hashenter( file->hcachehash, (HASHDATA **)&c ) ) {
			printf( "jam: can't insert header cache item, bailing on %s\n",
				file->cachefilename );
			goto bail;
		}

		c->next = 0;
		if( last )
			last->next = c;
		else
			file->hcachelist = c;
		last = c;
	}

	bad_cache = 0;

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

bail:
	/* If its bad, no worries, it'll be overwritten in hcache_done() */
	if( bad_cache )
		printf( "jam: warning: the cache was invalid: %s\n", file->cachefilename );
	buffer_free( &buff );
}
示例#2
0
static void checksums_readfile() {
	CHECKSUMDATA *last;
	FILE *f;
	int bad_cache, ch;
	const char	*version;
	BUFFER buff;
	long buffsize;

	if ( checksumsfileread ) {
		return;
	}

	checksumsfileread = 1;

	checksumhash = hashinit( sizeof( CHECKSUMDATA ), "checksums" );

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

	fseek( f, 0, SEEK_END );
	buffsize = ftell( f );
	fseek( f, 0, SEEK_SET );
	buffer_init( &buff );
	buffer_resize( &buff, buffsize + 1 );
	if ( fread( buffer_ptr( &buff ), buffsize, 1, f ) != 1 )
	{
		fclose( f );
		goto bail;
	}
	buffer_ptr( &buff )[buffsize] = 0;
	fclose( f );

	version = read_string( &buff );
	ch = buffer_getchar( &buff );
	if ( !version  ||  strcmp( version, CHECKSUM_FILE_VERSION ) || ch != '\n' ) {
		goto bail;
	}

	last = 0;
	bad_cache = 1;

	for (;;) {
		CHECKSUMDATA	checksumdata, *c = &checksumdata;
		int ch;

		c->boundname = read_string( &buff );
		if ( !c->boundname ) { /* Test for eof */
			break;
		}

		c->age = read_int( &buff ) + 1; /* we're getting older... */

		c->mtime = read_int( &buff );
		read_md5sum( &buff, c->contentmd5sum );
		c->contentmd5sum_changed = 0;
		c->contentmd5sum_calculated = 0;

		/* Read the newline */
		ch = skip_spaces( &buff );
		if ( ch != '!' ) {
			goto bail;
		}
		ch = skip_spaces( &buff );
		if ( ch != '\n' ) {
			goto bail;
		}

		if ( !hashcheck( checksumhash, (HASHDATA **) &c ) ) {
			if ( !hashenter( checksumhash, (HASHDATA **)&c ) ) {
				printf( "jam: can't insert checksum cache item, bailing...\n" );
				goto bail;
			}
		}

		c->next = 0;
		if ( last ) {
			last->next = c;
		} else {
			checksumdatalist = c;
		}
		last = c;
	}

	bad_cache = 0;

	if ( DEBUG_HEADER ) {
		printf( "checksums read from file %s\n", checksums_filename() );
	}

bail:
	/* If its bad, no worries, it'll be overwritten in hcache_done() */
	if ( bad_cache )
		printf( "jam: warning: the checksum cache was invalid: %s\n", checksums_filename() );
	buffer_free( &buff );
}