diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 90605b8..73b8e80 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,4 @@ add_subdirectory(log) -add_subdirectory(file) add_subdirectory(timestamp) add_subdirectory(tuple) add_subdirectory(packet) diff --git a/src/file/CMakeLists.txt b/src/file/CMakeLists.txt deleted file mode 100644 index 099fec6..0000000 --- a/src/file/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_library(file file_scan.cpp) -target_include_directories(file PUBLIC ${CMAKE_CURRENT_LIST_DIR}) -target_link_libraries(file log) \ No newline at end of file diff --git a/src/file/file_scan.cpp b/src/file/file_scan.cpp deleted file mode 100644 index f32e36a..0000000 --- a/src/file/file_scan.cpp +++ /dev/null @@ -1,69 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "file_scan.h" - -int file_scan(const char *dir, file_handle *cb, void *arg) -{ - struct stat statbuf; - struct dirent *entry; - - DIR *dp = opendir(dir); - if (NULL == dp) - { - FILE_SCAN_LOG_ERROR("opendir %s failed, %s", dir, strerror(errno)); - return -1; - } - - if (chdir(dir) == -1) - { - FILE_SCAN_LOG_ERROR("chdir %s failed, %s", dir, strerror(errno)); - goto error_out; - } - - while ((entry = readdir(dp))) - { - if (lstat(entry->d_name, &statbuf) == -1) - { - FILE_SCAN_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 (file_scan(entry->d_name, cb, arg) == -1) - { - goto error_out; - } - } - else - { - if (cb(entry->d_name, arg) == -1) - { - goto error_out; - } - } - } - - if (chdir("..") == -1) - { - FILE_SCAN_LOG_ERROR("chdir .. failed, %s", strerror(errno)); - goto error_out; - } - - closedir(dp); - return 0; - -error_out: - closedir(dp); - return -1; -} diff --git a/src/file/file_scan.h b/src/file/file_scan.h deleted file mode 100644 index c057b00..0000000 --- a/src/file/file_scan.h +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef _FILE_SCAN_H -#define _FILE_SCAN_H - -#ifdef __cplusplus -extern "C" -{ -#endif - -#include "log.h" - -#define FILE_SCAN_LOG_ERROR(format, ...) LOG_ERROR("file scan", format, ##__VA_ARGS__) -#define FILE_SCAN_LOG_DEBUG(format, ...) LOG_DEBUG("file scan", format, ##__VA_ARGS__) - -typedef int file_handle(const char *file, void *arg); -int file_scan(const char *dir, file_handle *cb, void *arg); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/packet_io/CMakeLists.txt b/src/packet_io/CMakeLists.txt index b305b53..a4a183a 100644 --- a/src/packet_io/CMakeLists.txt +++ b/src/packet_io/CMakeLists.txt @@ -1,4 +1,4 @@ add_library(packet_io dumpfile_io.cpp marsio_io.cpp lock_free_queue.cpp packet_io.cpp) target_include_directories(packet_io PUBLIC ${CMAKE_CURRENT_LIST_DIR}) target_include_directories(packet_io PUBLIC ${CMAKE_SOURCE_DIR}/src/stellar) -target_link_libraries(packet_io mrzcpd pcap packet file) \ No newline at end of file +target_link_libraries(packet_io mrzcpd pcap packet) \ No newline at end of file diff --git a/src/packet_io/dumpfile_io.cpp b/src/packet_io/dumpfile_io.cpp index e1eb3a7..a096745 100644 --- a/src/packet_io/dumpfile_io.cpp +++ b/src/packet_io/dumpfile_io.cpp @@ -4,12 +4,15 @@ #include #include #include +#include +#include +#include +#include #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) {