zabbix-agent2/src/zabbix_agent/logfiles/logfiles.c
/******************************************************************************
 *                                                                            *
 * Function: files_have_same_blocks_md5                                       *
 *                                                                            *
 * Purpose: compare MD5 sums of first and last blocks between 2 files. If MD5 *
 *          sums have been calculated for blocks of different sizes or        *
 *          offsets then open the log file with larger size of blocks and get *
 *          MD5 for the smaller size                                          *
 *                                                                            *
 * Parameters:                                                                *
 *     log1 - [IN] log file 1 attributes                                      *
 *     log2 - [IN] log file 2 attributes                                      *
 *                                                                            *
 * Return value: SUCCEED or FAIL                                              *
 *                                                                            *
 * Comments: Use this function to compare log files from the 'new' file list. *
 *           DO NOT use it with a file which belongs to the 'old' list (the   *
 *           old file name now could point to different file)                 *
 *                                                                            *
 ******************************************************************************/
files_have_same_blocks_md5

Purpose: compare MD5 sums of first and last blocks between 2 files. If MD5
sums have been calculated for blocks of different sizes or
offsets then open the log file with larger size of blocks and get
MD5 for the smaller size

Parameters:
log1 - [IN] log file 1 attributes
log2 - [IN] log file 2 attributes

Return value: SUCCEED or FAIL

Comments: Use this function to compare log files from the 'new' file list.
DO NOT use it with a file which belongs to the 'old' list (the
old file name now could point to different file)
files_have_same_blocks_md5 Purpose : compare MD5 sums of first and last blocks between 2 files . If MD5 sums have been calculated for blocks of different sizes or offsets then open the log file with larger size of blocks and get MD5 for the smaller size Parameters : log1 - [ IN ] log file 1 attributes log2 - [ IN ] log file 2 attributes Return value : SUCCEED or FAIL Comments : Use this function to compare log files from the ' new ' file list . DO NOT use it with a file which belongs to the ' old ' list ( the old file name now could point to different file )
static int	files_have_same_blocks_md5(const struct st_logfile *log1, const struct st_logfile *log2)
{
	if (-1 == log1->md5_block_size || -1 == log2->md5_block_size)
		return FAIL;
	if (log1->md5_block_size == log2->md5_block_size)	
	{
		if (0 != memcmp(log1->first_block_md5, log2->first_block_md5, sizeof(log1->first_block_md5)))
			return FAIL;
		if (log1->last_block_offset == log2->last_block_offset)
		{
			if (0 != memcmp(log1->last_block_md5, log2->last_block_md5, sizeof(log1->last_block_md5)))
				return FAIL;
			return SUCCEED;
		}
	}
	if (0 < log1->md5_block_size && 0 < log2->md5_block_size)
	{
		const struct st_logfile	*file_smaller, *file_larger;
		int			fd, ret = FAIL;
		char			*err_msg = NULL;		
		md5_byte_t		md5tmp[MD5_DIGEST_SIZE];
		if (log1->md5_block_size < log2->md5_block_size)
		{
			file_smaller = log1;
			file_larger = log2;
		}
		else
		{
			file_smaller = log2;
			file_larger = log1;
		}
		if (-1 == (fd = zbx_open(file_larger->filename, O_RDONLY)))
			return FAIL;
		if (SUCCEED != file_part_md5(fd, 0, file_smaller->md5_block_size, md5tmp, "", &err_msg))
			goto clean;
		if (0 != memcmp(file_smaller->first_block_md5, md5tmp, sizeof(md5tmp)))
			goto clean;
		if (0 == file_smaller->last_block_offset)
		{
			ret = SUCCEED;
			goto clean;
		}
		if (SUCCEED != file_part_md5(fd, file_smaller->last_block_offset, file_smaller->md5_block_size, md5tmp,
				"", &err_msg))
		{
			goto clean;
		}
		if (0 == memcmp(file_smaller->last_block_md5, md5tmp, sizeof(md5tmp)))
			ret = SUCCEED;
clean:
		zbx_free(err_msg);
		close(fd);
		return ret;
	}
	return FAIL;
}
static int files_have_same_blocks_md5 ( const struct st_logfile * log1 , const struct st_logfile * log2 ) { if ( -1 == log1 -> md5_block_size || -1 == log2 -> md5_block_size ) return FAIL ; if ( log1 -> md5_block_size == log2 -> md5_block_size ) { if ( 0 != memcmp ( log1 -> first_block_md5 , log2 -> first_block_md5 , sizeof ( log1 -> first_block_md5 ) ) ) return FAIL ; if ( log1 -> last_block_offset == log2 -> last_block_offset ) { if ( 0 != memcmp ( log1 -> last_block_md5 , log2 -> last_block_md5 , sizeof ( log1 -> last_block_md5 ) ) ) return FAIL ; return SUCCEED ; } } if ( 0 < log1 -> md5_block_size && 0 < log2 -> md5_block_size ) { const struct st_logfile * file_smaller , * file_larger ; int fd , ret = FAIL ; char * err_msg = NULL ; md5_byte_t md5tmp [ MD5_DIGEST_SIZE ] ; if ( log1 -> md5_block_size < log2 -> md5_block_size ) { file_smaller = log1 ; file_larger = log2 ; } else { file_smaller = log2 ; file_larger = log1 ; } if ( -1 == ( fd = zbx_open ( file_larger -> filename , O_RDONLY ) ) ) return FAIL ; if ( SUCCEED != file_part_md5 ( fd , 0 , file_smaller -> md5_block_size , md5tmp , " " , & err_msg ) ) goto clean ; if ( 0 != memcmp ( file_smaller -> first_block_md5 , md5tmp , sizeof ( md5tmp ) ) ) goto clean ; if ( 0 == file_smaller -> last_block_offset ) { ret = SUCCEED ; goto clean ; } if ( SUCCEED != file_part_md5 ( fd , file_smaller -> last_block_offset , file_smaller -> md5_block_size , md5tmp , " " , & err_msg ) ) { goto clean ; } if ( 0 == memcmp ( file_smaller -> last_block_md5 , md5tmp , sizeof ( md5tmp ) ) ) ret = SUCCEED ; clean : zbx_free ( err_msg ) ; close ( fd ) ; return ret ; } return FAIL ; }
Function: files_have_same_blocks_md5                                       
                                                                            
 Purpose: compare MD5 sums of first and last blocks between 2 files.
Function : files_have_same_blocks_md5 Purpose : compare MD5 sums of first and last blocks between 2 files .
/* this works for empty files, too */ /* we have MD5 sums but they were calculated from blocks of different sizes or offsets */ /* required, but not used */