]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
stat: add wrappers to isolate OS_WIN32 specifics
authorVictor Julien <vjulien@oisf.net>
Wed, 9 Aug 2023 06:00:09 +0000 (08:00 +0200)
committerVictor Julien <vjulien@oisf.net>
Fri, 11 Aug 2023 05:02:06 +0000 (07:02 +0200)
src/detect-engine.c
src/runmode-unix-socket.c
src/source-pcap-file-directory-helper.c
src/suricata.c
src/util-conf.c
src/util-path.h

index 06e21ec1bc34fcd9b981f54298bfb63b299a6358..012753c9a8a53ee51bc42cd0836488edf705b3f9 100644 (file)
@@ -77,6 +77,7 @@
 #include "util-spm.h"
 #include "util-device.h"
 #include "util-var-name.h"
+#include "util-path.h"
 #include "util-profiling.h"
 #include "util-validate.h"
 #include "util-hash-string.h"
@@ -3813,13 +3814,8 @@ static int DetectEngineMultiTenantLoadTenant(uint32_t tenant_id, const char *fil
 
     snprintf(prefix, sizeof(prefix), "multi-detect.%u", tenant_id);
 
-#ifdef OS_WIN32
-    struct _stat st;
-    if(_stat(filename, &st) != 0) {
-#else
-    struct stat st;
-    if(stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    SCStat st;
+    if (SCStatFn(filename, &st) != 0) {
         SCLogError("failed to stat file %s", filename);
         goto error;
     }
index 98b508fe6fbc8e18a49154a4d9093eb171923bd2..e1baa0656efe5eb0bf79ab4f8410fe249bf0dc9f 100644 (file)
@@ -28,6 +28,7 @@
 #include "util-cpu.h"
 #include "util-affinity.h"
 #include "util-var-name.h"
+#include "util-path.h"
 #include "unix-manager.h"
 
 #include "detect-engine.h"
@@ -323,11 +324,7 @@ static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer, void *data
     bool should_delete = false;
     time_t delay = 30;
     time_t poll_interval = 5;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
 
     json_t *jarg = json_object_get(cmd, "filename");
     if (!json_is_string(jarg)) {
@@ -337,11 +334,7 @@ static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer, void *data
         return TM_ECODE_FAILED;
     }
     filename = json_string_value(jarg);
-#ifdef OS_WIN32
-    if (_stat(filename, &st) != 0) {
-#else
-    if (stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(filename, &st) != 0) {
         json_object_set_new(answer, "message",
                             json_string("filename does not exist"));
         return TM_ECODE_FAILED;
@@ -365,11 +358,7 @@ static TmEcode UnixSocketAddPcapFileImpl(json_t *cmd, json_t* answer, void *data
         return TM_ECODE_FAILED;
     }
 
-#ifdef OS_WIN32
-    if (_stat(output_dir, &st) != 0) {
-#else
-    if (stat(output_dir, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(output_dir, &st) != 0) {
         json_object_set_new(answer, "message",
                             json_string("output-dir does not exist"));
         return TM_ECODE_FAILED;
@@ -1016,11 +1005,7 @@ TmEcode UnixSocketUnregisterTenantHandler(json_t *cmd, json_t* answer, void *dat
 TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data)
 {
     const char *filename;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
 
     if (!(DetectEngineMultiTenantEnabled())) {
         SCLogInfo("error: multi-tenant support not enabled");
@@ -1043,11 +1028,7 @@ TmEcode UnixSocketRegisterTenant(json_t *cmd, json_t* answer, void *data)
         return TM_ECODE_FAILED;
     }
     filename = json_string_value(jarg);
-#ifdef OS_WIN32
-    if (_stat(filename, &st) != 0) {
-#else
-    if (stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(filename, &st) != 0) {
         json_object_set_new(answer, "message", json_string("file does not exist"));
         return TM_ECODE_FAILED;
     }
@@ -1092,11 +1073,7 @@ static int reload_cnt = 1;
 TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data)
 {
     const char *filename;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
 
     if (!(DetectEngineMultiTenantEnabled())) {
         SCLogInfo("error: multi-tenant support not enabled");
@@ -1119,11 +1096,7 @@ TmEcode UnixSocketReloadTenant(json_t *cmd, json_t* answer, void *data)
         return TM_ECODE_FAILED;
     }
     filename = json_string_value(jarg);
-#ifdef OS_WIN32
-    if (_stat(filename, &st) != 0) {
-#else
-    if (stat(filename, &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCStatFn(filename, &st) != 0) {
         json_object_set_new(answer, "message", json_string("file does not exist"));
         return TM_ECODE_FAILED;
     }
index d701cc7405a24aee5fa17212de355da1ae2bf63e..17abd03a888af14a195478acdce3a85f506dbbab 100644 (file)
@@ -28,6 +28,7 @@
 #include "runmode-unix-socket.h"
 #include "util-mem.h"
 #include "util-time.h"
+#include "util-path.h"
 #include "source-pcap-file.h"
 
 static void GetTime(struct timespec *tm);
@@ -219,23 +220,14 @@ TmEcode PcapDetermineDirectoryOrFile(char *filename, DIR **directory)
 
 int PcapDirectoryGetModifiedTime(char const *file, struct timespec *out)
 {
-#ifdef OS_WIN32
-    struct _stat buf;
-#else
-    struct stat buf;
-#endif /* OS_WIN32 */
+    SCStat buf;
     int ret;
 
     if (file == NULL)
         return -1;
 
-#ifdef OS_WIN32
-    if((ret = _stat(file, &buf)) != 0)
-        return ret;
-#else
-    if ((ret = stat(file, &buf)) != 0)
+    if ((ret = SCStatFn(file, &buf)) != 0)
         return ret;
-#endif
 
 #ifdef OS_DARWIN
     out->tv_sec = buf.st_mtimespec.tv_sec;
index d436b0ba7da377f029c1c4625e08667d236517fe..1b504e23a3a11d54a344fd197c59888fd7052761 100644 (file)
 #include "util-macset.h"
 #include "util-misc.h"
 #include "util-mpm-hs.h"
+#include "util-path.h"
 #include "util-pidfile.h"
 #include "util-plugin.h"
 #include "util-privs.h"
@@ -502,11 +503,7 @@ static void SetBpfStringFromFile(char *filename)
     char *bpf_comment_tmp = NULL;
     char *bpf_comment_start =  NULL;
     uint32_t bpf_len = 0;
-#ifdef OS_WIN32
-    struct _stat st;
-#else
-    struct stat st;
-#endif /* OS_WIN32 */
+    SCStat st;
     FILE *fp = NULL;
     size_t nm = 0;
 
@@ -516,11 +513,7 @@ static void SetBpfStringFromFile(char *filename)
         exit(EXIT_FAILURE);
     }
 
-#ifdef OS_WIN32
-    if (_fstat(_fileno(fp), &st) != 0) {
-#else
-    if (fstat(fileno(fp), &st) != 0) {
-#endif /* OS_WIN32 */
+    if (SCFstatFn(fileno(fp), &st) != 0) {
         SCLogError("Failed to stat file %s", filename);
         exit(EXIT_FAILURE);
     }
@@ -1955,13 +1948,8 @@ static TmEcode ParseCommandLine(int argc, char** argv, SCInstance *suri)
                 PrintUsage(argv[0]);
                 return TM_ECODE_FAILED;
             }
-#ifdef OS_WIN32
-            struct _stat buf;
-            if(_stat(optarg, &buf) != 0) {
-#else
-            struct stat buf;
-            if (stat(optarg, &buf) != 0) {
-#endif /* OS_WIN32 */
+            SCStat buf;
+            if (SCStatFn(optarg, &buf) != 0) {
                 SCLogError("pcap file '%s': %s", optarg, strerror(errno));
                 return TM_ECODE_FAILED;
             }
index 0f01c7ead7b1c513810dc62c0cbde7177aa7deab..9bf5586bd970d1f106999fc99340238baf251b84 100644 (file)
@@ -28,6 +28,7 @@
 #include "runmodes.h"
 #include "util-conf.h"
 #include "util-debug.h"
+#include "util-path.h"
 
 TmEcode ConfigSetLogDirectory(const char *name)
 {
@@ -55,13 +56,8 @@ const char *ConfigGetLogDirectory(void)
 TmEcode ConfigCheckLogDirectoryExists(const char *log_dir)
 {
     SCEnter();
-#ifdef OS_WIN32
-    struct _stat buf;
-    if (_stat(log_dir, &buf) != 0) {
-#else
-    struct stat buf;
-    if (stat(log_dir, &buf) != 0) {
-#endif /* OS_WIN32 */
+    SCStat buf;
+    if (SCStatFn(log_dir, &buf) != 0) {
         SCReturnInt(TM_ECODE_FAILED);
     }
     SCReturnInt(TM_ECODE_OK);
@@ -103,13 +99,8 @@ const char *ConfigGetDataDirectory(void)
 TmEcode ConfigCheckDataDirectory(const char *data_dir)
 {
     SCEnter();
-#ifdef OS_WIN32
-    struct _stat buf;
-    if (_stat(data_dir, &buf) != 0) {
-#else
-    struct stat buf;
-    if (stat(data_dir, &buf) != 0) {
-#endif /* OS_WIN32 */
+    SCStat buf;
+    if (SCStatFn(data_dir, &buf) != 0) {
         SCReturnInt(TM_ECODE_FAILED);
     }
     SCReturnInt(TM_ECODE_OK);
index 6f788a8f2513b7880b7deb91668791d339a30b46..b8a5dd25939dc188e8c4563e86a7b14b4cc1703b 100644 (file)
 #ifndef __UTIL_PATH_H__
 #define __UTIL_PATH_H__
 
+#ifdef OS_WIN32
+typedef struct _stat SCStat;
+#define SCFstatFn(fd, statbuf)      _fstat((fd), (statbuf))
+#define SCStatFn(pathname, statbuf) _stat((pathname), (statbuf))
+#else
+typedef struct stat SCStat;
+#define SCFstatFn(fd, statbuf)      fstat((fd), (statbuf))
+#define SCStatFn(pathname, statbuf) stat((pathname), (statbuf))
+#endif
+
 #ifndef HAVE_NON_POSIX_MKDIR
     #define SCMkDir(a, b) mkdir(a, b)
 #else