diff --git a/src/util-logopenfile.c b/src/util-logopenfile.c index 12d230dafd..a322399e3e 100644 --- a/src/util-logopenfile.c +++ b/src/util-logopenfile.c @@ -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; diff --git a/src/util-path.c b/src/util-path.c index 87efc11774..d2989946b9 100644 --- a/src/util-path.c +++ b/src/util-path.c @@ -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; } diff --git a/src/util-path.h b/src/util-path.h index afb90c647b..f425092e64 100644 --- a/src/util-path.h +++ b/src/util-path.h @@ -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__ */