This repository has been archived by the owner on Feb 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ff.c
369 lines (320 loc) · 9.96 KB
/
ff.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#ifndef __cplusplus
#define _GNU_SOURCE
#endif
#include "dircolors.h"
#include "flagman.h"
#include "gitignore.h"
#include "message.h"
#include "options.h"
#include "regex.h"
// C standard library
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// POSIX C library
#include <dirent.h>
#include <fnmatch.h>
#include <pthread.h>
#include <sys/sysinfo.h>
#include <unistd.h>
typedef struct _shared_ptr shared_ptr;
struct _shared_ptr {
gitignore *ptr;
int *refcnt;
};
shared_ptr make_shared(gitignore *repo) {
int *refcnt = (int *)malloc(sizeof(int));
*refcnt = 1;
shared_ptr s;
s.ptr = repo;
s.refcnt = refcnt;
return s;
}
void free_shared(shared_ptr s) {
assert(s.refcnt != NULL);
if (__atomic_sub_fetch(s.refcnt, 1, __ATOMIC_SEQ_CST) == 0) {
gitignore_free(s.ptr);
free(s.refcnt);
s.refcnt = NULL;
}
}
shared_ptr make_shared_copy(shared_ptr s) {
__atomic_add_fetch(s.refcnt, 1, __ATOMIC_SEQ_CST);
return s;
}
typedef struct {
int depth;
size_t len;
char *str;
shared_ptr repo;
} message_body;
message_body *message_body_new(int depth, size_t len, const char *str,
shared_ptr repo) {
message_body *msg = (message_body *)malloc(sizeof(message_body));
msg->depth = depth;
msg->len = len;
msg->str = str ? strdup(str) : NULL;
msg->repo = repo;
return msg;
}
void message_body_free(void *ptr) {
message_body *msg = (message_body *)ptr;
free(msg->str);
free_shared(msg->repo);
free(msg);
}
void process_match(const char *real_path, const char *dir_name,
const char *base_name, const options *const opt) {
if (opt->colorize) {
printf(DIRCOLOR_DIR "%s/" DIRCOLOR_RESET "%s%s" DIRCOLOR_RESET "%c",
dir_name, dircolor(real_path), base_name, opt->delimiter);
} else {
printf("%s%c", real_path, opt->delimiter);
}
}
int cmp(const void *a, const void *b) {
return strcoll(*(char *const *)a, *(char *const *)b);
}
void walk(const char *parent, const size_t l_parent, const options *const opt,
const int depth,
// PCRE
regex *re, regex_storage *mem,
// GLOB
const char *glob_pattern, int glob_flags,
// GIT
shared_ptr repo) {
// If maximum depth is exceeded we stop
if (opt->max_depth > 0 && depth >= opt->max_depth) {
return;
}
// Traverse the directory
size_t cnt = 0, len_names = 16;
char **names = (char **)malloc(len_names * sizeof(char *));
for (struct dirent *_d = (struct dirent *)opendir(parent), *entry = NULL;
(DIR *)_d != NULL
&& ((entry = readdir((DIR *)_d)) != NULL || closedir((DIR *)_d));) {
const char *d_name = entry->d_name;
size_t d_namlen = strlen(d_name);
// Skip current and parent
if (strcmp(d_name, ".") == 0 || strcmp(d_name, "..") == 0) {
continue;
}
// Skip hidden
if (opt->skip_hidden
&& (d_name[0] == '.')) { // || d_name[d_namlen - 1] == '~')) {
continue;
}
// Assemble full filename
size_t l_current = l_parent + d_namlen + 1;
char *current = (char *)malloc((l_current + 1) * sizeof(char));
strncpy(current, parent, l_parent);
strncpy(current + l_parent, "/", 1);
strncpy(current + l_parent + 1, d_name, d_namlen);
current[l_current] = '\0';
// Filter by file extension (only files)
if (opt->ext && entry->d_type == DT_REG) {
const char *ext = strrchr(d_name, '.');
if (ext == NULL || strcmp(ext + 1, opt->ext) != 0) {
free(current);
continue;
}
}
// Check .gitignore
if (!opt->no_ignore && repo.ptr != NULL) {
if (gitignore_is_ignored(repo.ptr, current, l_current,
entry->d_type)) {
free(current);
continue;
}
}
// Perform the match
switch (opt->mode) {
case REGEX: {
if (regex_match(re, mem, d_name, d_namlen)) {
goto success;
}
break;
}
case GLOB:
if (fnmatch(glob_pattern, d_name, glob_flags) == 0) {
goto success;
}
break;
case NONE:
success:
if (!(opt->ext && entry->d_type == DT_DIR)
&& (opt->only_type == DT_UNKNOWN
|| opt->only_type == entry->d_type)) {
if (__builtin_expect(cnt == len_names, 0)) {
len_names *= 2;
names = (char **)realloc(names, len_names * sizeof(char *));
}
names[cnt++] = current;
}
break;
}
// If the current item is a directory itself, queue it for
// traversal
if (entry->d_type == DT_DIR) {
// Increment the flagman count
flagman_acquire(opt->flagman_lock);
// If this directory is a git repo, open it so we can scan
// .gitignore
shared_ptr currentrepo = make_shared(NULL);
if (!opt->no_ignore) {
if ((currentrepo.ptr = gitignore_new(current)) == NULL) {
// We failed, so we have to free the refcnt of NULL
free_shared(currentrepo);
// If it is not a git repo, duplicate the current handle
currentrepo = make_shared_copy(repo);
}
}
// Queue the new item
message *m = message_new(
message_body_new(depth + 1, l_current, current, currentrepo),
message_body_free);
queue_put(opt->q, m, depth + 1);
}
}
qsort(names, cnt, sizeof(char *), cmp);
for (size_t i = 0; i < cnt; ++i) {
const char *d_name = strrchr(names[i], '/') + 1;
process_match(names[i], parent, d_name, opt);
free(names[i]);
}
free(names);
}
static void *worker(void *arg) {
const options *const opt = (options *)arg;
// Assemble some thread-local storage, such as JIT stack for PCRE
// or options for globbing
regex *re = NULL;
regex_storage *mem = NULL;
const char *glob_pattern = NULL;
int glob_flags = 0;
switch (opt->mode) {
case REGEX:
re = opt->match.re;
mem = regex_storage_new(re);
break;
case GLOB:
glob_pattern = opt->match.pattern;
glob_flags = opt->icase ? FNM_CASEFOLD : 0;
break;
case NONE:
break;
}
// This is the main loop.
//
// Each message in the queue is a directory, so we fetch one
// message from the queue and walk the directory tree.
for (message *msg = NULL; (msg = queue_get(opt->q)) != NULL;
message_free(msg)) {
// Dissect the message
message_body *b = (message_body *)message_data(msg);
int depth = b->depth;
size_t l_parent = b->len;
const char *parent = b->str;
shared_ptr repo = b->repo;
// Walk the directory tree
walk(parent, l_parent, opt, depth, re, mem, glob_pattern, glob_flags,
repo);
// We are finished, so we can decrement the flagman count
flagman_release(opt->flagman_lock);
}
// Cleanup the thread-local state
switch (opt->mode) {
case REGEX:
regex_storage_free(mem);
break;
case GLOB:
break;
case NONE:
break;
}
return NULL;
}
int main(int argc, char *argv[]) {
options opt;
// Defaults
opt.mode = NONE;
opt.only_type = DT_UNKNOWN;
opt.skip_hidden = true;
opt.max_depth = -1;
opt.colorize = isatty(fileno(stdout));
opt.icase = false;
opt.no_ignore = false;
opt.nthreads = get_nprocs();
opt.ext = NULL;
opt.delimiter = '\n';
opt.absolute = false;
// Parse the command line
switch (ff_parse_options(argc, argv, &opt)) {
case OPTIONS_SUCCESS:
break;
case OPTIONS_FAILURE: // parsing error
return 1;
case OPTIONS_HELP: // --help
return 0;
}
gitignore_init_global();
// Open a new message queue
opt.q = queue_new();
// Acquire the flagman lock
opt.flagman_lock = flagman_new();
flagman_acquire(opt.flagman_lock);
// Start threads
pthread_t *thread = (pthread_t *)malloc(opt.nthreads * sizeof(pthread_t));
for (int i = 0; i < opt.nthreads; ++i) {
pthread_create(&thread[i], NULL, &worker, &opt);
}
// Send the inital job
if (opt.optind == argc) {
char *path = strdup(".");
if (opt.absolute) {
path = realpath(path, NULL);
}
shared_ptr repo = make_shared(NULL);
if (!opt.no_ignore) {
repo.ptr = gitignore_new(path);
}
message *msg = message_new(
message_body_new(0, strlen(path), path, repo), message_body_free);
queue_put_head(opt.q, msg);
free(path);
}
for (int arg = opt.optind; arg < argc; ++arg) {
char *path = argv[arg];
if (opt.absolute) {
path = realpath(path, NULL);
}
shared_ptr repo = make_shared(NULL);
if (!opt.no_ignore) {
repo.ptr = gitignore_new(path);
}
message *msg = message_new(
message_body_new(0, strlen(path), path, repo), message_body_free);
queue_put_head(opt.q, msg);
if (opt.absolute) {
free(path);
}
}
// Send termination signal
flagman_wait(opt.flagman_lock);
for (int i = 0; i < opt.nthreads; ++i) {
queue_put_tail(opt.q, NULL);
}
for (int i = 0; i < opt.nthreads; ++i) {
pthread_join(thread[i], NULL);
}
// Cleanup memory
if (opt.mode == REGEX) {
regex_free(opt.match.re);
}
free(thread);
flagman_free(opt.flagman_lock);
queue_free(opt.q);
gitignore_free_global();
return 0;
}