forked from jcnelson/vdev
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatch.cpp
More file actions
Latest commit
158 lines (113 loc) · 3.99 KB
/
Copy pathmatch.cpp
File metadata and controls
158 lines (113 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
vdev: a virtual device manager for *nix
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include"match.h"
// parse a regex
intvdev_match_regex_init( regex_t* regex, charconst* str ) {
int rc = 0;
rc = regcomp( regex, str, REG_EXTENDED | REG_NEWLINE );
if( rc != 0 ) {
return -EINVAL;
}
else {
return0;
}
}
// parse a regex and append it to a list of regexes and strings
// return 0 on success
// return -EINVAL if the regex is invalid
// return -ENOMEM if we're out of memory
intvdev_match_regex_append( char*** strings, regex_t** regexes, size_t* len, charconst* next ) {
// verify that this is a valid regex
regex_t reg;
int rc = 0;
memset( ®, 0, sizeof(regex_t) );
rc = regcomp( ®, next, REG_EXTENDED | REG_NEWLINE | REG_NOSUB );
if( rc != 0 ) {
vdev_error("regcomp(%s) rc = %d\n", next, rc );
return -EINVAL;
}
char** new_strings = (char**)realloc( *strings, sizeof(char**) * (*len + 2) );
regex_t* new_regexes = (regex_t*)realloc( *regexes, sizeof(regex_t) * (*len + 1) );
if( new_strings == NULL || new_regexes == NULL ) {
return -ENOMEM;
}
new_strings[*len] = vdev_strdup_or_null( next );
new_strings[*len + 1] = NULL;
new_regexes[*len] = reg;
*strings = new_strings;
*regexes = new_regexes;
*len = *len + 1;
return0;
}
// free a list of regexes and their associated paths
intvdev_match_regexes_free( char** regex_strs, regex_t* regexes, size_t len ) {
if( regex_strs != NULL || regexes != NULL ) {
for( unsignedint i = 0; i < len; i++ ) {
if( regex_strs != NULL && regex_strs[i] != NULL ) {
free( regex_strs[i] );
regex_strs[i] = NULL;
}
if( regexes != NULL ) {
regfree( ®exes[i] );
}
}
if( regex_strs != NULL ) {
free( regex_strs );
}
if( regexes != NULL ) {
free( regexes );
}
}
return0;
}
// does a path match a regex?
// return 1 if so, 0 if not, negative on error
intvdev_match_regex( charconst* path, regex_t* regex ) {
int rc = 0;
rc = regexec( regex, path, 0, NULL, 0 );
if( rc != 0 ) {
if( rc == REG_NOMATCH ) {
// no match
return0;
}
else {
vdev_error("regexec(%s) rc = %d\n", path, rc );
return -abs(rc);
}
}
// match!
return1;
}
// does a path match any regexes in a list?
// return the index of the match if so (>= 0)
// return the size if not
// return negative on error
intvdev_match_first_regex( charconst* path, regex_t* regexes, size_t num_regexes ) {
int matched = 0;
for( unsignedint i = 0; i < num_regexes; i++ ) {
matched = vdev_match_regex( path, ®exes[i] );
if( matched > 0 ) {
return i;
}
elseif( matched < 0 ) {
vdev_error("vdev_acl_regex_match(%s) rc = %d\n", path, matched );
return matched;
}
}
return num_regexes;
}