Move code scanning directories to dumpfile io

This commit is contained in:
luwenpeng
2024-04-30 15:47:45 +08:00
parent 95e0982fd1
commit bdf899cf01
6 changed files with 74 additions and 102 deletions

View File

@@ -4,12 +4,15 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "macro.h"
#include "packet_priv.h"
#include "file_scan.h"
#include "lock_free_queue.h"
#include "dumpfile_io.h"
#include "packet_priv.h"
#include "lock_free_queue.h"
#define MAX_PACKET_QUEUE_SIZE (4096 * 1000)
@@ -35,7 +38,70 @@ struct pcap_pkt
* Private API
******************************************************************************/
static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
typedef int file_handle(const char *file, void *arg);
static int scan_directory(const char *dir, file_handle *handler, void *arg)
{
struct stat statbuf;
struct dirent *entry;
DIR *dp = opendir(dir);
if (NULL == dp)
{
PACKET_IO_LOG_ERROR("opendir %s failed, %s", dir, strerror(errno));
return -1;
}
if (chdir(dir) == -1)
{
PACKET_IO_LOG_ERROR("chdir %s failed, %s", dir, strerror(errno));
goto error_out;
}
while ((entry = readdir(dp)))
{
if (lstat(entry->d_name, &statbuf) == -1)
{
PACKET_IO_LOG_ERROR("lstat %s failed, %s", entry->d_name, strerror(errno));
goto error_out;
}
if (S_IFDIR & statbuf.st_mode)
{
if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
{
continue;
}
if (scan_directory(entry->d_name, handler, arg) == -1)
{
goto error_out;
}
}
else
{
if (handler(entry->d_name, arg) == -1)
{
goto error_out;
}
}
}
if (chdir("..") == -1)
{
PACKET_IO_LOG_ERROR("chdir .. failed, %s", strerror(errno));
goto error_out;
}
closedir(dp);
return 0;
error_out:
closedir(dp);
return -1;
}
static void pcap_packet_handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
{
struct dumpfile_io *handle = (struct dumpfile_io *)user;
@@ -76,7 +142,7 @@ static void pcap_handle(u_char *user, const struct pcap_pkthdr *h, const u_char
}
}
static int dumpfile_handle(const char *file, void *arg)
static int dumpfile_handler(const char *file, void *arg)
{
char resolved_path[256];
char pcap_errbuf[PCAP_ERRBUF_SIZE];
@@ -91,7 +157,7 @@ static int dumpfile_handle(const char *file, void *arg)
PACKET_IO_LOG_ERROR("unable to open pcap file: %s, %s", resolved_path, pcap_errbuf);
return -1;
}
pcap_loop(handle->pcap, -1, pcap_handle, (u_char *)handle);
pcap_loop(handle->pcap, -1, pcap_packet_handler, (u_char *)handle);
pcap_close(handle->pcap);
PACKET_IO_LOG_STATE("dumpfile %s processed", resolved_path)
@@ -106,7 +172,7 @@ static void *dumpfile_thread(void *arg)
ATOMIC_SET(&handle->io_thread_is_runing, 1);
PACKET_IO_LOG_STATE("dumpfile io thread is running");
file_scan(handle->directory, dumpfile_handle, arg);
scan_directory(handle->directory, dumpfile_handler, arg);
while (ATOMIC_READ(&handle->io_thread_need_exit) == 0)
{