suricata
util-path.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2012 Open Information Security Foundation
2  *
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
5  * Software Foundation.
6  *
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.
11  *
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
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  */
24 
25 #include "suricata-common.h"
26 #include "suricata.h"
27 #include "debug.h"
28 #include "util-debug.h"
29 #include "util-path.h"
30 
31 /**
32  * \brief Check if a path is absolute
33  *
34  * \param path string with the path
35  *
36  * \retval 1 absolute
37  * \retval 0 not absolute
38  */
39 int PathIsAbsolute(const char *path)
40 {
41  if (strlen(path) > 1 && path[0] == '/') {
42  return 1;
43  }
44 
45 #if (defined OS_WIN32 || defined __CYGWIN__)
46  if (strlen(path) > 2) {
47  if (isalpha((unsigned char)path[0]) && path[1] == ':') {
48  return 1;
49  }
50  }
51 #endif
52 
53  return 0;
54 }
55 
56 /**
57  * \brief Check if a path is relative
58  *
59  * \param path string with the path
60  *
61  * \retval 1 relative
62  * \retval 0 not relative
63  */
64 int PathIsRelative(const char *path)
65 {
66  return PathIsAbsolute(path) ? 0 : 1;
67 }
68 
69 /**
70  * \brief Wrapper around SCMkDir with default mode arguments.
71  */
72 int SCDefaultMkDir(const char *path)
73 {
74  return SCMkDir(path, S_IRWXU | S_IRGRP | S_IXGRP);
75 }
76 
77 /**
78  * \brief Recursively create a directory.
79  *
80  * \param path Path to create
81  * \param final true will create the final path component, false will not
82  *
83  * \retval 0 on success
84  * \retval -1 on error
85  */
86 int SCCreateDirectoryTree(const char *path, const bool final)
87 {
88  char pathbuf[PATH_MAX];
89  char *p;
90  size_t len = strlen(path);
91 
92  if (len > PATH_MAX - 1) {
93  return -1;
94  }
95 
96  strlcpy(pathbuf, path, sizeof(pathbuf));
97 
98  for (p = pathbuf + 1; *p; p++) {
99  if (*p == '/') {
100  /* Truncate, while creating directory */
101  *p = '\0';
102 
103  if (SCDefaultMkDir(pathbuf) != 0) {
104  if (errno != EEXIST) {
105  return -1;
106  }
107  }
108 
109  *p = '/';
110  }
111  }
112 
113  if (final) {
114  if (SCDefaultMkDir(pathbuf) != 0) {
115  if (errno != EEXIST) {
116  return -1;
117  }
118  }
119  }
120 
121  return 0;
122 }
123 
124 /**
125  * \brief Check if a path exists.
126  *
127  * \param Path to check for existence
128  *
129  * \retval true if path exists
130  * \retval false if path does not exist
131  */
132 bool SCPathExists(const char *path)
133 {
134  struct stat sb;
135  if (stat(path, &sb) == 0) {
136  return true;
137  }
138  return false;
139 }
int PathIsRelative(const char *path)
Check if a path is relative.
Definition: util-path.c:64
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
#define SCMkDir(a, b)
Definition: util-path.h:29
bool SCPathExists(const char *path)
Check if a path exists.
Definition: util-path.c:132
int PathIsAbsolute(const char *path)
Check if a path is absolute.
Definition: util-path.c:39
int SCCreateDirectoryTree(const char *path, const bool final)
Recursively create a directory.
Definition: util-path.c:86
uint8_t len
int SCDefaultMkDir(const char *path)
Wrapper around SCMkDir with default mode arguments.
Definition: util-path.c:72