zabbix-agent2/src/zabbix_agent/logfiles/logfiles.c
/******************************************************************************
 *                                                                            *
 * Function: pick_logfiles                                                    *
 *                                                                            *
 * Purpose: find logfiles in a directory and put them into a list             *
 *                                                                            *
 * Parameters:                                                                *
 *     directory      - [IN] directory where the logfiles reside              *
 *     mtime          - [IN] selection criterion "logfile modification time"  *
 *                      The logfile will be selected if modified not before   *
 *                      'mtime'.                                              *
 *     re             - [IN] selection criterion "regexp describing filename  *
 *                      pattern"                                              *
 *     use_ino        - [OUT] how to use inodes in is_same_file()             *
 *     logfiles       - [IN/OUT] pointer to the list of logfiles              *
 *     logfiles_alloc - [IN/OUT] number of logfiles memory was allocated for  *
 *     logfiles_num   - [IN/OUT] number of already inserted logfiles          *
 *     err_msg        - [IN/OUT] error message why an item became             *
 *                      NOTSUPPORTED                                          *
 *                                                                            *
 * Return value: SUCCEED or FAIL                                              *
 *                                                                            *
 * Comments: This is a helper function for make_logfile_list()                *
 *                                                                            *
 * Comments: Thead-safety - readdir() is a gray area, supposed to work on     *
 *           modern implementations when the directory stream is not shared   *
 *           between threads.                                                 *
 *                                                                            *
 ******************************************************************************/
pick_logfiles

Purpose: find logfiles in a directory and put them into a list

Parameters:
directory      - [IN] directory where the logfiles reside
mtime          - [IN] selection criterion "logfile modification time"
The logfile will be selected if modified not before
'mtime'.
pick_logfiles Purpose : find logfiles in a directory and put them into a list Parameters : directory - [ IN ] directory where the logfiles reside mtime - [ IN ] selection criterion " logfile modification time " The logfile will be selected if modified not before ' mtime ' .
static int	pick_logfiles(const char *directory, int mtime, const zbx_regexp_t *re, int *use_ino,
		struct st_logfile **logfiles, int *logfiles_alloc, int *logfiles_num, char **err_msg)
{
#if defined(_WINDOWS) || defined(__MINGW32__)
	int			ret = FAIL;
	intptr_t		find_handle;
	struct _wfinddata_t	find_data;
	char	*find_path = zbx_dsprintf(NULL, "%s*", directory);
	wchar_t	*find_wpath = zbx_utf8_to_unicode(find_path);
	if (-1 == (find_handle = _wfindfirst(find_wpath, &find_data)))
	{
		*err_msg = zbx_dsprintf(*err_msg, "Cannot open directory \"%s\" for reading: %s", directory,
				zbx_strerror(errno));
		zbx_free(find_wpath);
		zbx_free(find_path);
		return FAIL;
	}
	if (SUCCEED != set_use_ino_by_fs_type(find_path, use_ino, err_msg))
		goto clean;
	do
	{
		char	*file_name_utf8 = zbx_unicode_to_utf8(find_data.name);
		pick_logfile(directory, file_name_utf8, mtime, re, logfiles, logfiles_alloc, logfiles_num);
		zbx_free(file_name_utf8);
	}
	while (0 == _wfindnext(find_handle, &find_data));
	ret = SUCCEED;
clean:
	if (-1 == _findclose(find_handle))
	{
		*err_msg = zbx_dsprintf(*err_msg, "Cannot close directory \"%s\": %s", directory, zbx_strerror(errno));
		ret = FAIL;
	}
	zbx_free(find_wpath);
	zbx_free(find_path);
	return ret;
#else
	DIR		*dir = NULL;
	struct dirent	*d_ent = NULL;
	if (NULL == (dir = opendir(directory)))
	{
		*err_msg = zbx_dsprintf(*err_msg, "Cannot open directory \"%s\" for reading: %s", directory,
				zbx_strerror(errno));
		return FAIL;
	}
	*use_ino = 1;
	while (NULL != (d_ent = readdir(dir)))
		pick_logfile(directory, d_ent->d_name, mtime, re, logfiles, logfiles_alloc, logfiles_num);
	if (-1 == closedir(dir))
	{
		*err_msg = zbx_dsprintf(*err_msg, "Cannot close directory \"%s\": %s", directory, zbx_strerror(errno));
		return FAIL;
	}
	return SUCCEED;
#endif
}
static int pick_logfiles ( const char * directory , int mtime , const zbx_regexp_t * re , int * use_ino , struct st_logfile * * logfiles , int * logfiles_alloc , int * logfiles_num , char * * err_msg ) { #if defined ( _WINDOWS ) || defined ( __MINGW32__ ) int ret = FAIL ; intptr_t find_handle ; struct _wfinddata_t find_data ; char * find_path = zbx_dsprintf ( NULL , " " , directory ) ; wchar_t * find_wpath = zbx_utf8_to_unicode ( find_path ) ; if ( -1 == ( find_handle = _wfindfirst ( find_wpath , & find_data ) ) ) { * err_msg = zbx_dsprintf ( * err_msg , " \" \" " , directory , zbx_strerror ( errno ) ) ; zbx_free ( find_wpath ) ; zbx_free ( find_path ) ; return FAIL ; } if ( SUCCEED != set_use_ino_by_fs_type ( find_path , use_ino , err_msg ) ) goto clean ; do { char * file_name_utf8 = zbx_unicode_to_utf8 ( find_data . name ) ; pick_logfile ( directory , file_name_utf8 , mtime , re , logfiles , logfiles_alloc , logfiles_num ) ; zbx_free ( file_name_utf8 ) ; } while ( 0 == _wfindnext ( find_handle , & find_data ) ) ; ret = SUCCEED ; clean : if ( -1 == _findclose ( find_handle ) ) { * err_msg = zbx_dsprintf ( * err_msg , " \" \" " , directory , zbx_strerror ( errno ) ) ; ret = FAIL ; } zbx_free ( find_wpath ) ; zbx_free ( find_path ) ; return ret ; #else DIR * dir = NULL ; struct dirent * d_ent = NULL ; if ( NULL == ( dir = opendir ( directory ) ) ) { * err_msg = zbx_dsprintf ( * err_msg , " \" \" " , directory , zbx_strerror ( errno ) ) ; return FAIL ; } * use_ino = 1 ; while ( NULL != ( d_ent = readdir ( dir ) ) ) pick_logfile ( directory , d_ent -> d_name , mtime , re , logfiles , logfiles_alloc , logfiles_num ) ; if ( -1 == closedir ( dir ) ) { * err_msg = zbx_dsprintf ( * err_msg , " \" \" " , directory , zbx_strerror ( errno ) ) ; return FAIL ; } return SUCCEED ; #endif }
Function: pick_logfiles                                                    
                                                                            
 Purpose: find logfiles in a directory and put them into a list             
                                                                            
 Parameters:                                                                
     directory      - [IN] directory where the logfiles reside              
     mtime          - [IN] selection criterion "logfile modification time"  
                      The logfile will be selected if modified not before   
                      'mtime'.
Function : pick_logfiles Purpose : find logfiles in a directory and put them into a list Parameters : directory - [ IN ] directory where the logfiles reside mtime - [ IN ] selection criterion " logfile modification time " The logfile will be selected if modified not before ' mtime ' .
/* "open" Windows directory */ /* on UNIX file systems we always assume that inodes can be used to identify files */