-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathutil.c
More file actions
271 lines (238 loc) · 7.23 KB
/
pathutil.c
File metadata and controls
271 lines (238 loc) · 7.23 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "grepper.h"
#include <_strings.h>
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#define MATCH_FILE 1
#define MATCH_SUFFIX_FILE 9
#define MATCH_FULL_FILE 2
#define MATCH_DIR 3
#define MATCH_SUFFIX_DIR 4
#define MATCH_FULL_DIR 5
static bool ends_with_rule(const char *a, const char *b)
{
size_t al = strlen(a);
size_t bl = (*(uint32_t *)b) & 0xFFFFFF;
b += 4;
return al >= bl && strcmp(a + al - bl, b) == 0;
}
const char *is_glob_path(const char *p, const char *end)
{
const char *slash = NULL;
for (const char *i = p; i < end; i++) {
if (*i == '/') {
slash = i;
} else if ((*i == '*' || *i == '?' || *i == '[' || *i == ']') && (i == p || *(i - 1) != '\\')) {
return slash ? slash + 1 : p;
}
}
return NULL;
}
const char *rel_path(const char *a, const char *b)
{
int al = strlen(a), bl = strlen(b);
if (!al || !bl)
return b;
al -= a[al - 1] == '/' ? 1 : 0;
bl -= b[bl - 1] == '/' ? 1 : 0;
if (bl < al)
return b;
if (strncmp(a, b, al) != 0)
return b;
if (al == bl)
return ".";
return b + al + 1;
}
static bool _matcher_wildmatch(const char *pattern, const char *name, bool is_dir)
{
unsigned flag = (*(uint32_t *)pattern) >> 24;
if (flag == MATCH_SUFFIX_DIR)
return is_dir && ends_with_rule(name, pattern);
if (flag == MATCH_SUFFIX_FILE)
return ends_with_rule(name, pattern);
pattern += 4;
if (flag == MATCH_FULL_FILE)
return strcmp(name, pattern) == 0;
if (flag == MATCH_FULL_DIR)
return is_dir && strcmp(name, pattern) == 0;
if (flag == MATCH_DIR)
return is_dir && wildmatch(pattern, name, 0) == WM_MATCH;
// if (flag == MATCH_FILE)
return wildmatch(pattern, name, 0) == WM_MATCH;
}
static bool _matcher_negate_match(struct matcher *m, const char *name, bool is_dir)
{
for (size_t i = 0; i < m->negate_excludes.len; i++) {
if (_matcher_wildmatch(m->negate_excludes.data[i], name, is_dir))
return true;
}
if (m->parent)
return _matcher_negate_match(m->parent, name, is_dir);
return false;
}
bool matcher_match(struct matcher *m, const char *name, bool is_dir)
{
const char *orig = name;
if (m->root) {
if (strstr(name, m->root) == name)
name = name + strlen(m->root);
}
bool incl = is_dir || m->top->includes.len == 0;
for (size_t i = 0; i < m->top->includes.len; i++) {
if (_matcher_wildmatch(m->top->includes.data[i], name, is_dir)) {
incl = true;
break;
}
}
if (!incl) {
DBG("[IGNORE] %s not included\n", name);
return false;
}
for (size_t i = 0; i < m->excludes.len; i++) {
const char *v = m->excludes.data[i];
if (_matcher_wildmatch(v, name, is_dir)) {
if (!_matcher_negate_match(m, name, is_dir)) {
unsigned flag = (*(uint32_t *)v) >> 24;
const char *rule = "";
if (flag == MATCH_SUFFIX_DIR) rule = "MATCH_SUFFIX_DIR";
if (flag == MATCH_SUFFIX_FILE) rule = "MATCH_SUFFIX_FILE";
if (flag == MATCH_FULL_FILE) rule = "MATCH_FULL_FILE";
if (flag == MATCH_FULL_DIR) rule = "MATCH_FULL_DIR";
if (flag == MATCH_DIR) rule = "MATCH_DIR";
if (flag == MATCH_FILE) rule = "MATCH_FILE";
DBG("[IGNORE] %s by %s/%s, %s(%s)\n", name, m->root, m->file, rule, v + 4);
return false;
}
}
}
return m->parent ? matcher_match(m->parent, orig, is_dir) : true;
}
void matcher_free(void *p)
{
struct matcher *m = (struct matcher *)p;
if (m->root)
free(m->root);
strings_free(&m->includes);
strings_free(&m->excludes);
strings_free(&m->negate_excludes);
free(m);
}
bool matcher_add_rule(struct matcher *m, const char *l, const char *end, bool incl)
{
struct strings *ss = &m->excludes;
while (isspace(*(end - 1)))
end--;
if (end <= l || l[0] == '#')
return false;
if (l[0] == '!') {
ss = &m->negate_excludes;
if (++l >= end)
return false;
}
if (*l == '/' && l + 1 == end)
return false;
bool simple = !is_glob_path(l, end);
char *buf = (char *)malloc(end - l + 10);
int off = 0;
uint32_t *op = (uint32_t *)buf;
if (*(end - 1) == '/') {
if (simple)
*op = *l == '/' ? MATCH_FULL_DIR : (off = 1, MATCH_SUFFIX_DIR);
else
*op = MATCH_DIR;
end--;
} else if (*l == '/') {
*op = simple ? MATCH_FULL_FILE : MATCH_FILE;
} else {
*op = simple ? (off = 1, MATCH_SUFFIX_FILE) : MATCH_FILE;
}
*op = (*op << 24) | (uint32_t)(end - l + off);
if (off)
buf[4] = '/';
memcpy(buf + 4 + off, l, end - l);
buf[4 + off + end - l] = 0;
strings_push(incl ? &m->includes : ss, buf);
return true;
}
static struct matcher *_matcher_load_raw(const char *dir, const char *f)
{
JOIN_PATH_TMP(path, dir, f);
int fd = open(path, O_RDONLY);
if (fd < 0) {
ERR("open ignore file %s", path);
return NULL;
}
size_t size = lseek(fd, 0, SEEK_END);
char *buf = (char *)malloc(size + 64);
v_pread(fd, buf, size, 0);
struct matcher *m = (struct matcher *)calloc(1, sizeof(struct matcher));
for (size_t i = 0; i < size;) {
const char *next = indexbyte(buf + i, buf + size, '\n');
if (!next)
next = buf + size;
matcher_add_rule(m, buf + i, next, false);
i = next - buf + 1;
}
close(fd);
free(buf);
m->root = strdup(dir);
m->file = f;
if (m->excludes.len + m->negate_excludes.len > 0)
return m;
matcher_free(m);
return NULL;
}
static bool file_exists(const char *dir, const char *name)
{
JOIN_PATH_TMP(tmp, dir, name);
struct stat ss;
return stat(tmp, &ss) == 0;
}
struct matcher *matcher_load_ignore_file(const char *dir, struct matcher *parent, struct stack *matchers)
{
struct matcher *m = NULL;
bool enter_repo = false;
if (file_exists(dir, ".git")) {
enter_repo = true;
m = _matcher_load_raw(dir, ".git/info/exclude");
if (m) {
m->parent = parent->top;
m->top = parent->top;
stack_push(0, matchers, (struct stacknode *)m);
parent = m;
}
}
if (file_exists(dir, ".gitignore")) {
m = _matcher_load_raw(dir, ".gitignore");
if (m) {
m->parent = enter_repo ? parent->top : parent;
m->top = parent->top;
stack_push(0, matchers, (struct stacknode *)m);
}
}
return m;
}
bool is_repo_bin(const char *dir, const char *name)
{
return false;
const char *dot = strrchr(dir, '.');
if (!dot)
return false;
if (strcmp(dot, ".git") == 0 && strcmp(name, "objects") == 0)
return true;
if (strcmp(dot, ".hg") == 0 && strcmp(name, "store") == 0)
return true;
return false;
}
bool is_dir(const char *name, bool follow_link)
{
struct stat ss;
#ifdef _WIN32
stat(name, &ss);
#else
follow_link ? stat(name, &ss) : lstat(name, &ss);
#endif
return (ss.st_mode & S_IFMT) == S_IFDIR;
}