⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 func.c

📁 监控linux指定目录
💻 C
字号:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ftw.h>
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/file.h>
#include <fcntl.h>
#include <dirent.h>
#include <time.h>
#include <getopt.h>
#include <syslog.h>
#include "arg.h"
#include "func.h"

FILE *f_new;
FILE *f_old;
int g_interval = 0;
char g_path[128] = {0};
char g_suffix[128] = {0};
dirList *g_pHead = NULL;

void print_dir_list()
{
	dirList *p = NULL;

	if (g_pHead != NULL)
	{
		p = g_pHead;
	}	
	while (p != NULL)
	{
	//	printf("%s\n", p->dir);	
		p = p->next;
	}
}

void free_usb_list()
{
	dirList *p = g_pHead;
	while (p != NULL)
	{
		g_pHead = p->next;
		free(p);
		p = g_pHead;
	}
}

int init_dir_list()
{
	dirList *newItem = NULL;
	dirList *p1 = NULL;
	char *p_dir = NULL;
	char *tok = "#";	
	
	newItem = (dirList*)malloc(sizeof(dirList));
	strcpy(newItem->dir, strtok(g_path, tok));
	newItem->next = NULL;
	g_pHead = newItem;

	while (p_dir = strtok(NULL, tok))
	{
		newItem = (dirList*)malloc(sizeof(dirList));
		strcpy(newItem->dir, p_dir);
		newItem->next = NULL;
		p1 = (dirList*)malloc(sizeof(dirList));	
		p1 = g_pHead;
		while (p1->next != NULL)
		{
			p1 = p1->next;
		}
		p1->next = newItem;	
	}
}

int check_suffix_ok(const char *file)
{
	int ret = 0;
	char *p = NULL;
	
	//printf("here file is %s\n", file);
	//strcpy(file_bak, file);
	
	if (strncmp(g_suffix, "*", 1) == 0)
	{
		//fprintf(f_new, "f@%s@%s\n", file, str_time);
		ret = 1;
	}
	else
	{
		p = strstr(file, g_suffix);
		if (p != NULL && 0 == strcmp(p, g_suffix))
		{
			//fprintf(f_new, "f@%s@%s\n", file, str_time);
			ret = 1;
		}
	}
	//printf("here file is %s\n", file);
	return ret;
}


void get_str_mod_time(char *str_time, time_t modTime)
{
	struct tm *p;
	p = gmtime(&modTime);
	char month[3] = {0};
	char day[3] = {0};
	char hour[3] = {0};
	char minute[3] = {0};
	char second[3] = {0};
	
	if (p->tm_mon < 9)
	{
		sprintf(month, "0%d", 1+p->tm_mon);
	}
	else
	{
		sprintf(month, "%d", 1+p->tm_mon);
	}

	if (p->tm_mday < 10)
	{
		sprintf(day, "0%d", p->tm_mday);
	}
	else
	{
		sprintf(day, "%d", p->tm_mday);
	}
	
	if (p->tm_hour < 10)
	{
		sprintf(hour, "0%d", p->tm_hour);
	}
	else
	{
		sprintf(hour, "%d", p->tm_hour);
	}

	if (p->tm_min < 10)
	{
		sprintf(minute, "0%d", p->tm_min);
	}
	else
	{
		sprintf(minute, "%d", p->tm_min);
	}

	if (p->tm_sec < 10)
	{
		sprintf(second, "0%d", p->tm_sec);
	}
	else
	{
		sprintf(second, "%d", p->tm_sec);
	}

	sprintf(str_time, "%d-%s-%s %s:%s:%s", 1900+p->tm_year, month, day, hour, minute, second);
	//printf("modtime is %s\n", str_time);
}

void trim_end(char* str)
{
	assert(str);

	char* pEnd = str + strlen(str) - 1;

	while ((*pEnd == '\n') || (*pEnd == '\r') || (*pEnd == 0x20) || (*pEnd == 0x09))
	{
		*pEnd = 0;
		pEnd--;
	}
}

int check_add_mod_handler(const char *file, const struct stat *sb, int flag)
{
	char buf[SIZE] = {0};
	char *p_type = NULL;
	char *p_file = NULL;
	char *p_time = NULL;
	int newadd = 1;
	char str_time[SIZE] = {0};
	char log[SIZE] = {0};
	
	if (flag == FTW_F && !check_suffix_ok(file))
	{
		return 0;
	}
	get_str_mod_time(str_time, sb->st_mtime);
	rewind(f_old);
	
	while (fgets(buf, SIZE, f_old))
	{
		trim_end(buf);
		
		p_type = NULL;
		p_file = NULL;
		p_time = NULL;
		p_type = strtok(buf, "@");
		p_file = strtok(NULL, "@");
		p_time = strtok(NULL, "@");
		if (flag == FTW_D && strncmp(p_type, "d", 1) == 0 && strcmp(p_file, file) == 0)
		{
			//printf("file is %s\n", file);
			//printf("p_file is %s\n", p_file);
			newadd = 0;
			break;
		}
		
		else if (flag == FTW_F && strncmp(p_type, "f", 1) == 0 && strcmp(p_file, file) == 0)
		{
			//printf("file is %s\n", file);
			//printf("p_file is %s\n", p_file);
			newadd = 0;

			if (strcmp(str_time, p_time) > 0)		
			{				
				printf("file %s had been modified.\n", file);
				sprintf(log, "file %s had been modified.\n", file);
				syslog(LOG_INFO ,log); 
				return 1;
			}
			//printf("file is %s\tret is %d\n", file, check_suffix(file));
			break;
		}
		memset(buf, 0, SIZE);
	}
	if (newadd)
	{
		if (flag == FTW_F)
		{
			printf("file %s had been added.\n", file);
			sprintf(log, "file %s had been added.\n", file);
			syslog(LOG_INFO ,log); 
		}
		else if (flag == FTW_D)
		{
			printf("directory %s had been added.\n", file);
			sprintf(log, "directory %s had been added.\n", file);
			syslog(LOG_INFO ,log); 
		}
		
		return 1;
	}
	return 0;
}

int write_new_file(const char *file, const struct stat *sb, int flag)
{
	char str_time[SIZE] = {0};
	
	get_str_mod_time(str_time, sb->st_mtime);
	if (flag == FTW_F && check_suffix_ok(file))
	{
		fprintf(f_new, "f@%s@%s\n", file, str_time);
	}
	if (flag == FTW_D)
	{
		fprintf(f_new, "d@%s@%s\n", file, str_time);
	}
	return 0;
}

int check_removed_file()
{
	char buf[SIZE] = {0};
	FILE *f;
	DIR *dir;
	char *p_type = NULL;
	char *p_file = NULL;
	char *p_time = NULL;
	int ret = 0;
	char log[SIZE] = {0};
	f_old = fopen(FILE_OLD, "r");
	if (f_old == NULL)
	{
		//printf("while check removed file, open file_old error\n");
		return -1;
	}
	while (fgets(buf, SIZE, f_old))
	{
		trim_end(buf);
		p_type = strtok(buf, "@");
		p_file = strtok(NULL, "@");
		p_time = strtok(NULL, "@");
		if (0 == strncmp(p_type, "f", 1))
		{
			if (0 != access(p_file, F_OK))
			{
				printf("file %s had been removed.\n", p_file);
				sprintf(log, "file %s had been removed.\n", p_file);
				syslog(LOG_INFO ,log); 
				ret = 1;				
				break ;
			}
		}
		else if (0 == strncmp(p_type, "d", 1))
		{
			dir = opendir(p_file);
			if (dir == NULL)
			{
				printf("directory %s had been removed.\n", p_file);
				sprintf(log, "directory %s had been removed.\n", p_file);
				syslog(LOG_INFO ,log); 
				ret = 1;
				break ;
			}
			else
			{
				closedir(dir);
			}
		}	
		memset(buf, SIZE, 0);
	}
	fclose(f_old);
	return ret;
}

int check_added_modified_file()
{
	int ret;
	dirList *p = NULL;
	
	f_old = fopen(FILE_OLD, "r");
	if (f_old == NULL)
	{
		//printf("when check added file, open f_old error\n");
		return -1;
	}
	if (g_pHead != NULL)
	{
		p = g_pHead;
	}	
	while (p != NULL)
	{
		ret = ftw(p->dir, check_add_mod_handler, 500);
		if (ret == 1)
		{
			break;
		}
		else
		{
			p = p->next;
		}
	}
	fclose(f_old);
	
	return ret;	
}


int creat_new_file()
{
	f_new = fopen(FILE_NEW, "w+");
	dirList *p = NULL;
	int ret;

	if (f_new == NULL)
	{
		printf("creat new file failed\n");
		return -1;
	}
	if (g_pHead != NULL)
	{
		p = g_pHead;
	}
	else
	{
		printf("g_pHead is NULL\n");
	}
	while (p != NULL)
	{
		ret = ftw(p->dir, write_new_file, 500);
		p = p->next;
	}

	fclose(f_new);
	return ret;
}

void run(runconf *conf)
{
	int ret_add = 0;
	int ret_del = 0;
	
	g_interval = conf->interval;
	strcpy(g_path, conf->path);
	if (strncmp(conf->suffix, "*", 1) != 0)
	{
		sprintf(g_suffix, ".%s", conf->suffix);
	}
	else
	{
		strcpy(g_suffix, conf->suffix);
	}	
	printf("path is %s\n", g_path);
	printf("suffix is %s\n", g_suffix);
	printf("g_interval is %d\n", g_interval);

	//dirList *g_pHead = NULL;
	init_dir_list();
	print_dir_list();

	remove(FILE_OLD);
	while (1)
	{			
		if (ret_add || ret_del)
		{
			if (0 == creat_new_file())
			{
				rename(FILE_NEW, FILE_OLD);
			}
			else
			{
				printf("creat new file error.\n");
				return ;
			}
		}
		sleep(conf->interval);

		ret_add = check_added_modified_file();
		ret_del = check_removed_file();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -