From: James Dutrisac Date: Tue, 28 Jul 2020 17:08:29 +0000 (-0400) Subject: path: introduce path handling util funcs X-Git-Tag: suricata-6.0.0-beta1~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5a92d0a7041533d50581ba1f7780fdb534826e98;p=thirdparty%2Fsuricata.git path: introduce path handling util funcs This commit provides changes to util-path.c and util-path.h to support the recursive reading of directories. It adds 4 functions. - SCIsRegularFile to provide OS independent file info. - SCIsRegularDirectory to provide OS independent directory info. - SCRealPath is an OS independent wrapper for realpath. - PathJoin to manage path resolution logic. --- diff --git a/src/util-path.c b/src/util-path.c index f4ba183264..26a87af563 100644 --- a/src/util-path.c +++ b/src/util-path.c @@ -66,6 +66,43 @@ int PathIsRelative(const char *path) return PathIsAbsolute(path) ? 0 : 1; } +/** + * \brief Wrapper to join a directory and filename and resolve using realpath + * _fullpath is used for WIN32 + * + * \param out_buf output buffer. Up to PATH_MAX will be written. Unchanged on exit failure. + * \param buf_len length of output buffer + * \param dir the directory + * \param fname the filename + * + * \retval TM_ECODE_OK on success + * \retval TM_ECODE_FAILED on failure + */ +TmEcode PathJoin (char *out_buf, uint16_t buf_len, const char *const dir, const char *const fname) +{ + SCEnter(); +#ifdef OS_WIN32 +#define DIRECTORY_SEPARATOR '\\' +#else +#define DIRECTORY_SEPARATOR '/' +#endif + uint16_t max_path_len = MAX(buf_len, PATH_MAX); + int bytes_written = snprintf(out_buf, max_path_len, "%s%c%s", dir, DIRECTORY_SEPARATOR, fname); + if (bytes_written <= 0) { + SCLogError(SC_ERR_SPRINTF, "Could not join filename to path"); + SCReturnInt(TM_ECODE_FAILED); + } + char *tmp_buf = SCRealPath(out_buf, NULL); + if (tmp_buf == NULL) { + SCLogError(SC_ERR_SPRINTF, "Error resolving path: %s", strerror(errno)); + SCReturnInt(TM_ECODE_FAILED); + } + memset(out_buf, 0, buf_len); + strlcpy(out_buf, tmp_buf, max_path_len); + free(tmp_buf); + SCReturnInt(TM_ECODE_OK); +} + /** * \brief Wrapper around SCMkDir with default mode arguments. */ @@ -137,3 +174,54 @@ bool SCPathExists(const char *path) } return false; } + +/** + * \brief OS independent wrapper for directory check + * + * \param dir_entry object to check + * + * \retval True if the object is a regular directory, otherwise false. This directory + * and parent directory will return false. + */ +bool SCIsRegularDirectory(const struct dirent *const dir_entry) +{ +#ifndef OS_WIN32 + if ((dir_entry->d_type == DT_DIR) && + (strcmp(dir_entry->d_name, ".") != 0) && + (strcmp(dir_entry->d_name, "..") != 0)) { + return true; + } +#endif + return false; +} +/** + * \brief OS independent to check for regular file + * + * \param dir_entry object to check + * + * \retval True if the object is a regular file. Otherwise false. + */ +bool SCIsRegularFile(const struct dirent *const dir_entry) +{ +#ifndef OS_WIN32 + return dir_entry->d_type == DT_REG; +#endif + return false; +} + +/** + * \brief OS independent wrapper for realpath + * + * \param path the path to resolve + * \param resolved_path the resolved path; if null, a buffer will be allocated + * + * \retval the resolved_path; or a pointer to a new resolved_path buffer + */ +char *SCRealPath(const char *path, char *resolved_path) +{ +#ifdef OS_WIN32 + return _fullpath(resolved_path, path, PATH_MAX); +#else + return realpath(path, resolved_path); +#endif +} diff --git a/src/util-path.h b/src/util-path.h index aeffe2c787..9108f1829d 100644 --- a/src/util-path.h +++ b/src/util-path.h @@ -33,8 +33,12 @@ int PathIsAbsolute(const char *); int PathIsRelative(const char *); +TmEcode PathJoin (char *out_buf, uint16_t buf_len, const char *const dir, const char *const fname); int SCDefaultMkDir(const char *path); int SCCreateDirectoryTree(const char *path, const bool final); bool SCPathExists(const char *path); +bool SCIsRegularDirectory(const struct dirent *const dir_entry); +bool SCIsRegularFile(const struct dirent *const dir_entry); +char *SCRealPath(const char *path, char *resolved_path); #endif /* __UTIL_PATH_H__ */