summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorluwenpeng <[email protected]>2024-04-30 15:47:45 +0800
committerluwenpeng <[email protected]>2024-04-30 15:47:45 +0800
commitbdf899cf01c9fb556639ef1e6b4c8a31caac58e3 (patch)
tree8a155436d2a78f910e17daf0b0d06e891f338a1e
parent95e0982fd18d548f6ceea8d5e86a185624557fc1 (diff)
Move code scanning directories to dumpfile io
-rw-r--r--src/CMakeLists.txt1
-rw-r--r--src/file/CMakeLists.txt3
-rw-r--r--src/file/file_scan.cpp69
-rw-r--r--src/file/file_scan.h21
-rw-r--r--src/packet_io/CMakeLists.txt2
-rw-r--r--src/packet_io/dumpfile_io.cpp78
6 files changed, 73 insertions, 101 deletions
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 <errno.h>
-#include <dirent.h>
-#include <unistd.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#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 <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 "dumpfile_io.h"
#include "packet_priv.h"
-#include "file_scan.h"
#include "lock_free_queue.h"
-#include "dumpfile_io.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)
{