]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
create directory: final arg to control full path or prefix
authorJason Ish <ish@unx.ca>
Thu, 4 Jan 2018 17:07:50 +0000 (11:07 -0600)
committerJason Ish <ish@unx.ca>
Thu, 18 Jan 2018 11:57:26 +0000 (05:57 -0600)
Give SCCreateDirectoryTree a new argument, final. If true the
full path will be created as a directory. If false, the last
component will not be created as a directory (current
behaviour).

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

index 12d230dafd1c3a3b283774a03e2d995b008eddb8..a322399e3e225d5c17a45183e58de16f301cc182 100644 (file)
@@ -267,7 +267,7 @@ SCLogOpenFileFp(const char *path, const char *append_setting, uint32_t mode)
         return NULL;
     }
 
-    int rc = SCCreateDirectoryTree(filename);
+    int rc = SCCreateDirectoryTree(filename, false);
     if (rc < 0) {
         SCFree(filename);
         return NULL;
index 87efc11774adbd7a8bdde02dcef586ecb9c6d040..d2989946b95c4209b0c421fa22b2e6184966d6b2 100644 (file)
@@ -66,12 +66,24 @@ 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
+/**
+ * \brief Wrapper around SCMkDir with default mode arguments.
+ */
+int SCDefaultMkDir(const char *path)
+{
+    return SCMkDir(path, S_IRWXU | S_IRGRP | S_IXGRP);
+}
+
+/**
+ * \brief Recursively create a directory.
+ *
+ * \param path Path to create
+ * \param final true will create the final path component, false will not
+ *
+ * \retval 0 on success
+ * \retval -1 on error
  */
-int SCCreateDirectoryTree(const char *path)
+int SCCreateDirectoryTree(const char *path, const bool final)
 {
     char pathbuf[PATH_MAX];
     char *p;
@@ -88,7 +100,7 @@ int SCCreateDirectoryTree(const char *path)
             /* Truncate, while creating directory */
             *p = '\0';
 
-            if (SCMkDir(pathbuf, S_IRWXU | S_IRGRP | S_IXGRP) != 0) {
+            if (SCDefaultMkDir(pathbuf) != 0) {
                 if (errno != EEXIST) {
                     return -1;
                 }
@@ -98,5 +110,13 @@ int SCCreateDirectoryTree(const char *path)
         }
     }
 
+    if (final) {
+        if (SCDefaultMkDir(pathbuf) != 0) {
+            if (errno != EEXIST) {
+                return -1;
+            }
+        }
+    }
+
     return 0;
 }
index afb90c647b3799430af3085886e1eae9df49ab84..f425092e64753e74406e8319ad72bff4afb37d01 100644 (file)
@@ -33,6 +33,7 @@
 
 int PathIsAbsolute(const char *);
 int PathIsRelative(const char *);
-int SCCreateDirectoryTree(const char *path);
+int SCDefaultMkDir(const char *path);
+int SCCreateDirectoryTree(const char *path, const bool final);
 
 #endif /* __UTIL_PATH_H__ */