]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
util: move SCCreateDirectoryTree to util-path
authorJason Ish <ish@unx.ca>
Thu, 4 Jan 2018 16:06:31 +0000 (10:06 -0600)
committerJason Ish <ish@unx.ca>
Thu, 18 Jan 2018 11:57:26 +0000 (05:57 -0600)
Renames SCLogCreateDirectoryTree to SCCreateDirectoryTree
and move into a util module for re-use.

Also moves SCMkDir from suricata-common.h to the more
appropriately names util-path.h.

I would have prefered to use util-file for file related options
but that is already used by file store utilities. util-path
is close enough for file related operations.

src/suricata-common.h
src/util-logopenfile.c
src/util-path.c
src/util-path.h

index e43cfc9518dcf471365f73580a2b6a6abd10c7f1..410b1d500d3e773f077bdfae5744161a619d4074 100644 (file)
 
 #define WARN_UNUSED __attribute__((warn_unused_result))
 
-#ifndef HAVE_NON_POSIX_MKDIR
-    #define SCMkDir(a, b) mkdir(a, b)
-#else
-    #define SCMkDir(a, b) mkdir(a)
-#endif
-
 #define SCNtohl(x) (uint32_t)ntohl((x))
 #define SCNtohs(x) (uint16_t)ntohs((x))
 
index 92540812b561012615a7f842e88d9b5c6f0d5639..12d230dafd1c3a3b283774a03e2d995b008eddb8 100644 (file)
@@ -29,6 +29,7 @@
 #include "conf.h"            /* ConfNode, etc. */
 #include "output.h"          /* DEFAULT_LOG_* */
 #include "util-byte.h"
+#include "util-path.h"
 #include "util-logopenfile.h"
 #include "util-logopenfile-tile.h"
 
@@ -243,41 +244,6 @@ static char *SCLogFilenameFromPattern(const char *pattern)
     return filename;
 }
 
-/** \brief recursively create missing log directories
- *  \param path path to log file
- *  \retval 0 on success
- *  \retval -1 on error
- */
-static int SCLogCreateDirectoryTree(const char *filepath)
-{
-    char pathbuf[PATH_MAX];
-    char *p;
-    size_t len = strlen(filepath);
-
-    if (len > PATH_MAX - 1) {
-        return -1;
-    }
-
-    strlcpy(pathbuf, filepath, len);
-
-    for (p = pathbuf + 1; *p; p++) {
-        if (*p == '/') {
-            /* Truncate, while creating directory */
-            *p = '\0';
-
-            if (SCMkDir(pathbuf, S_IRWXU | S_IRGRP | S_IXGRP) != 0) {
-                if (errno != EEXIST) {
-                    return -1;
-                }
-            }
-
-            *p = '/';
-        }
-    }
-
-    return 0;
-}
-
 static void SCLogFileClose(LogFileCtx *log_ctx)
 {
     if (log_ctx->fp)
@@ -301,7 +267,7 @@ SCLogOpenFileFp(const char *path, const char *append_setting, uint32_t mode)
         return NULL;
     }
 
-    int rc = SCLogCreateDirectoryTree(filename);
+    int rc = SCCreateDirectoryTree(filename);
     if (rc < 0) {
         SCFree(filename);
         return NULL;
index d1b812f028773efb0db30f9239b08f99cda08588..5af172fb619894a7b162fa7bf1ed55ac8496898e 100644 (file)
@@ -26,6 +26,7 @@
 #include "suricata.h"
 #include "debug.h"
 #include "util-debug.h"
+#include "util-path.h"
 
 /**
  *  \brief Check if a path is absolute
@@ -64,3 +65,38 @@ int PathIsRelative(const char *path)
 {
     return PathIsAbsolute(path) ? 0 : 1;
 }
+
+/** \brief Recursively create missing log directories.
+ *  \param path path to log file
+ *  \retval 0 on success
+ *  \retval -1 on error
+ */
+int SCCreateDirectoryTree(const char *path)
+{
+    char pathbuf[PATH_MAX];
+    char *p;
+    size_t len = strlen(path);
+
+    if (len > PATH_MAX - 1) {
+        return -1;
+    }
+
+    strlcpy(pathbuf, path, len);
+
+    for (p = pathbuf + 1; *p; p++) {
+        if (*p == '/') {
+            /* Truncate, while creating directory */
+            *p = '\0';
+
+            if (SCMkDir(pathbuf, S_IRWXU | S_IRGRP | S_IXGRP) != 0) {
+                if (errno != EEXIST) {
+                    return -1;
+                }
+            }
+
+            *p = '/';
+        }
+    }
+
+    return 0;
+}
index 2243722a59e7749838e1562b082d35c255f10729..afb90c647b3799430af3085886e1eae9df49ab84 100644 (file)
 #ifndef __UTIL_PATH_H__
 #define __UTIL_PATH_H__
 
+#ifndef HAVE_NON_POSIX_MKDIR
+    #define SCMkDir(a, b) mkdir(a, b)
+#else
+    #define SCMkDir(a, b) mkdir(a)
+#endif
+
 int PathIsAbsolute(const char *);
 int PathIsRelative(const char *);
+int SCCreateDirectoryTree(const char *path);
 
 #endif /* __UTIL_PATH_H__ */