Go to the documentation of this file.
31 #define DIRECTORY_SEPARATOR '\\'
33 #define DIRECTORY_SEPARATOR '/'
46 if (strlen(path) > 1 && path[0] ==
'/') {
50 #if (defined OS_WIN32 || defined __CYGWIN__)
51 if (strlen(path) > 2) {
52 if (isalpha((
unsigned char)path[0]) && path[1] ==
':') {
74 int PathMerge(
char *out_buf,
size_t buf_size,
const char *
const dir,
const char *
const fname)
77 if (dir == NULL || strlen(dir) == 0)
80 size_t r =
strlcpy(path, dir,
sizeof(path));
81 if (r >=
sizeof(path)) {
85 #if defined OS_WIN32 || defined __CYGWIN__
86 if (path[strlen(path) - 1] !=
'\\')
87 r =
strlcat(path,
"\\\\",
sizeof(path));
89 if (path[strlen(path) - 1] !=
'/')
90 r =
strlcat(path,
"/",
sizeof(path));
92 if (r >=
sizeof(path)) {
95 r =
strlcat(path, fname,
sizeof(path));
96 if (r >=
sizeof(path)) {
99 r =
strlcpy(out_buf, path, buf_size);
110 if (
PathMerge(path,
sizeof(path), dir, fname) != 0)
132 int PathJoin(
char *out_buf,
size_t buf_size,
const char *
const dir,
const char *
const fname)
135 if (buf_size != PATH_MAX) {
138 if (
PathMerge(out_buf, buf_size, dir, fname) != 0) {
139 SCLogError(
"Could not join filename to path");
143 if (tmp_buf == NULL) {
144 SCLogError(
"Error resolving path: %s", strerror(errno));
147 memset(out_buf, 0, buf_size);
148 size_t ret =
strlcpy(out_buf, tmp_buf, buf_size);
150 if (ret >= buf_size) {
161 return SCMkDir(path, S_IRWXU | S_IRGRP | S_IXGRP);
175 char pathbuf[PATH_MAX];
177 size_t len = strlen(path);
179 if (
len > PATH_MAX - 1) {
183 strlcpy(pathbuf, path,
sizeof(pathbuf));
185 for (p = pathbuf + 1; *p; p++) {
191 if (errno != EEXIST) {
202 if (errno != EEXIST) {
222 if (stat(path, &sb) == 0) {
239 if ((dir_entry->d_type == DT_DIR) &&
240 (strcmp(dir_entry->d_name,
".") != 0) &&
241 (strcmp(dir_entry->d_name,
"..") != 0)) {
257 return dir_entry->d_type == DT_REG;
273 return _fullpath(resolved_path, path, PATH_MAX);
275 return realpath(path, resolved_path);
287 if (!path || strlen(path) == 0)
294 if (*(
final + 1) ==
'\0')
310 const char *pattern =
"..\\";
312 const char *pattern =
"../";
314 return strstr(path, pattern) != NULL;
char * PathMergeAlloc(const char *const dir, const char *const fname)
int PathMerge(char *out_buf, size_t buf_size, const char *const dir, const char *const fname)
int SCDefaultMkDir(const char *path)
Wrapper around SCMkDir with default mode arguments.
size_t strlcpy(char *dst, const char *src, size_t siz)
bool SCPathContainsTraversal(const char *path)
Check for directory traversal.
bool SCIsRegularDirectory(const struct dirent *const dir_entry)
OS independent wrapper for directory check.
size_t strlcat(char *, const char *src, size_t siz)
#define DIRECTORY_SEPARATOR
const char * SCBasename(const char *path)
bool SCPathExists(const char *path)
Check if a path exists.
int PathIsAbsolute(const char *path)
Check if a path is absolute.
#define SCLogError(...)
Macro used to log ERROR messages.
char * SCRealPath(const char *path, char *resolved_path)
OS independent wrapper for realpath.
int PathIsRelative(const char *path)
Check if a path is relative.
bool SCIsRegularFile(const struct dirent *const dir_entry)
OS independent to check for regular file.
int PathJoin(char *out_buf, size_t buf_size, const char *const dir, const char *const fname)
Wrapper to join a directory and filename and resolve using realpath _fullpath is used for WIN32.
int SCCreateDirectoryTree(const char *path, const bool final)
Recursively create a directory.