]>
git.ipfire.org Git - people/ms/suricata.git/blob - src/util-path.c
1 /* Copyright (C) 2007-2012 Open Information Security Foundation
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 * \author Victor Julien <victor@inliniac.net>
25 #include "suricata-common.h"
28 #include "util-debug.h"
29 #include "util-path.h"
32 #define DIRECTORY_SEPARATOR '\\'
34 #define DIRECTORY_SEPARATOR '/'
38 * \brief Check if a path is absolute
40 * \param path string with the path
43 * \retval 0 not absolute
45 int PathIsAbsolute(const char *path
)
47 if (strlen(path
) > 1 && path
[0] == '/') {
51 #if (defined OS_WIN32 || defined __CYGWIN__)
52 if (strlen(path
) > 2) {
53 if (isalpha((unsigned char)path
[0]) && path
[1] == ':') {
63 * \brief Check if a path is relative
65 * \param path string with the path
68 * \retval 0 not relative
70 int PathIsRelative(const char *path
)
72 return PathIsAbsolute(path
) ? 0 : 1;
76 * \brief Wrapper to join a directory and filename and resolve using realpath
77 * _fullpath is used for WIN32
79 * \param out_buf output buffer. Up to PATH_MAX will be written. Unchanged on exit failure.
80 * \param buf_len length of output buffer
81 * \param dir the directory
82 * \param fname the filename
84 * \retval TM_ECODE_OK on success
85 * \retval TM_ECODE_FAILED on failure
87 TmEcode
PathJoin (char *out_buf
, uint16_t buf_len
, const char *const dir
, const char *const fname
)
90 uint16_t max_path_len
= MAX(buf_len
, PATH_MAX
);
91 int bytes_written
= snprintf(out_buf
, max_path_len
, "%s%c%s", dir
, DIRECTORY_SEPARATOR
, fname
);
92 if (bytes_written
<= 0) {
93 SCLogError(SC_ERR_SPRINTF
, "Could not join filename to path");
94 SCReturnInt(TM_ECODE_FAILED
);
96 char *tmp_buf
= SCRealPath(out_buf
, NULL
);
97 if (tmp_buf
== NULL
) {
98 SCLogError(SC_ERR_SPRINTF
, "Error resolving path: %s", strerror(errno
));
99 SCReturnInt(TM_ECODE_FAILED
);
101 memset(out_buf
, 0, buf_len
);
102 strlcpy(out_buf
, tmp_buf
, max_path_len
);
104 SCReturnInt(TM_ECODE_OK
);
108 * \brief Wrapper around SCMkDir with default mode arguments.
110 int SCDefaultMkDir(const char *path
)
112 return SCMkDir(path
, S_IRWXU
| S_IRGRP
| S_IXGRP
);
116 * \brief Recursively create a directory.
118 * \param path Path to create
119 * \param final true will create the final path component, false will not
121 * \retval 0 on success
122 * \retval -1 on error
124 int SCCreateDirectoryTree(const char *path
, const bool final
)
126 char pathbuf
[PATH_MAX
];
128 size_t len
= strlen(path
);
130 if (len
> PATH_MAX
- 1) {
134 strlcpy(pathbuf
, path
, sizeof(pathbuf
));
136 for (p
= pathbuf
+ 1; *p
; p
++) {
138 /* Truncate, while creating directory */
141 if (SCDefaultMkDir(pathbuf
) != 0) {
142 if (errno
!= EEXIST
) {
152 if (SCDefaultMkDir(pathbuf
) != 0) {
153 if (errno
!= EEXIST
) {
163 * \brief Check if a path exists.
165 * \param Path to check for existence
167 * \retval true if path exists
168 * \retval false if path does not exist
170 bool SCPathExists(const char *path
)
173 if (stat(path
, &sb
) == 0) {
180 * \brief OS independent wrapper for directory check
182 * \param dir_entry object to check
184 * \retval True if the object is a regular directory, otherwise false. This directory
185 * and parent directory will return false.
187 bool SCIsRegularDirectory(const struct dirent
*const dir_entry
)
190 if ((dir_entry
->d_type
== DT_DIR
) &&
191 (strcmp(dir_entry
->d_name
, ".") != 0) &&
192 (strcmp(dir_entry
->d_name
, "..") != 0)) {
199 * \brief OS independent to check for regular file
201 * \param dir_entry object to check
203 * \retval True if the object is a regular file. Otherwise false.
205 bool SCIsRegularFile(const struct dirent
*const dir_entry
)
208 return dir_entry
->d_type
== DT_REG
;
214 * \brief OS independent wrapper for realpath
216 * \param path the path to resolve
217 * \param resolved_path the resolved path; if null, a buffer will be allocated
219 * \retval the resolved_path; or a pointer to a new resolved_path buffer
221 char *SCRealPath(const char *path
, char *resolved_path
)
224 return _fullpath(resolved_path
, path
, PATH_MAX
);
226 return realpath(path
, resolved_path
);
231 * \brief Return the basename of the provided path.
232 * \param path The path on which to compute the basename
234 * \retval the basename of the path or NULL if the path lacks a non-leaf
236 const char *SCBasename(const char *path
)
238 if (!path
|| strlen(path
) == 0)
241 char *final
= strrchr(path
, DIRECTORY_SEPARATOR
);
245 if (*(final
+ 1) == '\0')