library/src/filesystem/BP-Filesystem.cc
// Simple routine to return a SHA1 hash for a file.  The
// sha1_ret_buffer parameter must be a buffer at least 40 bytes
// long (preferably 41 bytes for null terminator).
Simple routine to return a SHA1 hash for a file.  The
sha1_ret_buffer parameter must be a buffer at least 40 bytes
long (preferably 41 bytes for null terminator).
Simple routine to return a SHA1 hash for a file . The sha1_ret_buffer parameter must be a buffer at least 40 bytes long ( preferably 41 bytes for null terminator ) .
BP_ERROR_T BP_FileSHA1Contents
(
	char *file_path,
	char *sha1_ret_buffer
)
{
	if(!BP_StringIsReasonableForUnixEXT4Filename(file_path, bpstrlen(file_path), BP_TRUE))
		return ERR_FAILURE;
	if(!sha1_ret_buffer)
		return ERR_FAILURE;
	char *file_content = BP_FileGetContents(file_path);
	if(!file_content)
		return ERR_FAILURE;
	size_t file_size = BP_FGetSize(file_path);
	if(!file_size)
	{
		bpfree(file_content);
		return ERR_FAILURE;
	}
	char *file_sha1 = BP_GenSHA1(file_content, file_size);
	if(!file_sha1)
	{
		bpfree(file_content);
		return ERR_FAILURE;
	}
	memcpy(sha1_ret_buffer, file_sha1, 40);
	bpfree(file_content);
	bpfree(file_sha1);
	return ERR_SUCCESS;
}
BP_ERROR_T BP_FileSHA1Contents ( char * file_path , char * sha1_ret_buffer ) { if ( ! BP_StringIsReasonableForUnixEXT4Filename ( file_path , bpstrlen ( file_path ) , BP_TRUE ) ) return ERR_FAILURE ; if ( ! sha1_ret_buffer ) return ERR_FAILURE ; char * file_content = BP_FileGetContents ( file_path ) ; if ( ! file_content ) return ERR_FAILURE ; size_t file_size = BP_FGetSize ( file_path ) ; if ( ! file_size ) { bpfree ( file_content ) ; return ERR_FAILURE ; } char * file_sha1 = BP_GenSHA1 ( file_content , file_size ) ; if ( ! file_sha1 ) { bpfree ( file_content ) ; return ERR_FAILURE ; } memcpy ( sha1_ret_buffer , file_sha1 , 40 ) ; bpfree ( file_content ) ; bpfree ( file_sha1 ) ; return ERR_SUCCESS ; }
Simple routine to return a SHA1 hash for a file.
Simple routine to return a SHA1 hash for a file .
// ensure the path is reasonable and that the file is there // get content // retrieve size // generate sha1 from content // copy in the hash (40 bytes always by standard) // free the file content // return indicating success