Пример #1
0
LocFile* LocFileNew( char* path, unsigned int flags )
{
	FILE* fp = fopen( path, "rb" );
	if( fp == NULL )
	{
		INFO("Cannot open file %s (file does not exist?)..\n", path );
		return NULL;
	}
	
	struct stat st;
	if( stat( path, &st ) < 0 )
	{
		INFO( "Cannot stat file.\n" );
		fclose( fp );
		return NULL;
	}
	
	if( S_ISDIR( st.st_mode ) )
	{
		INFO( "Is a directory. Can not open.\n" );
		fclose( fp );
		return NULL;
	}
	
	LocFile* fo = (LocFile*) calloc( 1, sizeof(LocFile) );
	if( fo != NULL )
	{
		int len = strlen( path );
		fo->lf_Path = StringDuplicateN( path, len );
		fo->lf_Filename = GetFileNamePtr( path, len );
	
		fo->fp = fp;
		fseek( fp, 0L, SEEK_END );
		fo->filesize = ftell( fp );

		if( flags & FILE_READ_NOW )
		{
			LocFileRead( fo, 0, fo->filesize );
		}
	}
	else
	{
		ERROR("Cannot allocate memory for LocFile\n");
	}

	return fo;
}
Пример #2
0
/**
 * Reload content from file
 *
 * @param file pointer to LocFile structure 
 * @param path pointer to path from which data will be reloaded
 * @return 0 when success, otherwise error number
 */
int LocFileReload( LocFile *file, char *path )
{
	DEBUG("File %s will be reloaded\n", path );
	
	if( file->lf_Buffer )
	{
		FFree( file->lf_Buffer );
		file->lf_Buffer = NULL;
	}
	
	FILE* fp = fopen( path, "rb" );
	if( fp == NULL )
	{
		FERROR("Cannot open file %s (file does not exist?)..\n", path );
		return -1;
	}
	
	struct stat st;
	if( stat( path, &st ) < 0 )
	{
		FERROR("Cannot run stat on file: %s!\n", path);
		return -2;
	}
	memcpy(  &(file->lf_Info),  &st, sizeof(stat) );
	
	fseek( fp, 0, SEEK_END );
	long fsize = ftell( fp );
	fseek( fp, 0, SEEK_SET );  //same as rewind(f);
	file->lf_FileSize = fsize;

	LocFileRead( file, fp, 0, file->lf_FileSize );
	
	fclose( fp );
	
	return 0;
}
Пример #3
0
/**
 * Create new LocFile structure and read file from provided path
 *
 * @param path pointer to char table with path
 * @param flags additional flags used to open file
 * @return pointer to new LocFile when success, otherwise NULL
 */
LocFile* LocFileNew( char* path, unsigned int flags )
{
	if( path == NULL )
	{
		FERROR("File path is null\n");
		return NULL;
	}
	FILE* fp = fopen( path, "rb" );
	if( fp == NULL )
	{
		FERROR("Cannot open file %s (file does not exist?)..\n", path );
		return NULL;
	}
	
	struct stat st;
	if( stat( path, &st ) < 0 )
	{
		FERROR( "Cannot stat file: '%s'.\n", path );
		fclose( fp );
		return NULL;
	}
	
	if( S_ISDIR( st.st_mode ) )
	{
		FERROR( "'%s' is a directory. Can not open.\n", path );
		fclose( fp );
		return NULL;
	}
	DEBUG("Read local file %s\n", path );
	
	LocFile* fo = (LocFile*) FCalloc( 1, sizeof(LocFile) );
	if( fo != NULL )
	{
		fo->lf_PathLength = strlen( path );
		fo->lf_Path = StringDuplicateN( path, fo->lf_PathLength );
		fo->lf_Filename = StringDuplicate( GetFileNamePtr( path, fo->lf_PathLength ) );
		
		MURMURHASH3( fo->lf_Path, fo->lf_PathLength, fo->hash );
		
		DEBUG("PATH: %s\n", fo->lf_Path );
		
		memcpy(  &(fo->lf_Info),  &st, sizeof( struct stat) );

		fseek( fp, 0, SEEK_END );
		long fsize = ftell( fp );
		fseek( fp, 0, SEEK_SET );  //same as rewind(f);
		fo->lf_FileSize = fsize;// st.st_size; //ftell( fp );

		if( flags & FILE_READ_NOW )
		{
#if LOCFILE_USE_MMAP == 0
			LocFileRead( fo, fp, 0, fo->lf_FileSize );
#else

			fo->lf_Buffer = mmap(NULL/*address can be anywhere*/,
					fo->lf_FileSize/*map whole file*/,
					PROT_READ,
					MAP_SHARED | MAP_POPULATE,
					fileno(fp),
					0/*beginning of file*/);
			//DEBUG("***************** Mapping length: %d at %p\n", fo->lf_FileSize, fo->lf_Buffer);

#endif
		}
	}
	else
	{
		FERROR("Cannot allocate memory for LocFile\n");
	}
	
#if LOCFILE_USE_MMAP == 0
	fclose( fp );
#endif
	
	return fo;
}