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
|
/*
arfnet2-search: Fast file indexer and search
Copyright (C) 2025 arf20 (Ángel Ruiz Fernandez)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
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.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
index.c: Efficient fast file index
*/
#define _GNU_SOURCE
#include "index.h"
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <magic.h>
#include "config.h"
/* closed addressing map */
typedef struct map_s {
struct node_s *map;
size_t count, size;
} map_t;
struct node_s {
node_data_t *data;
struct node_s *next;
map_t *child;
};
static magic_t magic_cookie = NULL;
size_t
hash(const char *s, int mod)
{
size_t hash = 0;
if (!s)
return 0;
while (*s)
hash = hash * 31 + *s++;
return hash % mod;
}
map_t *
map_new(size_t size)
{
map_t *map = malloc(sizeof(map_t));
map->map = malloc(sizeof(struct node_s) * size);
memset(map->map, 0, sizeof(struct node_s) * size);
map->size = size;
map->count = 0;
return map;
}
void
map_insert(map_t *map, const char *key, node_data_t *data, map_t *child)
{
struct node_s *node = &map->map[hash(key, map->size)];
if (node->data) {
for (; node->next; node = node->next);
node->next = malloc(sizeof(struct node_s));
node->next->data = data;
node->next->child = child;
node->next->next = NULL;
} else {
node->data = data;
node->child = child;
}
map->count++;
}
results_t *
results_new()
{
results_t *r = malloc(sizeof(results_t));
r->capacity = INIT_VEC_CAPACITY;
r->results = malloc(sizeof(node_data_t*) * r->capacity);
memset(r->results, 0, sizeof(node_data_t*) * r->capacity);
r->size = 0;
return r;
}
void
results_insert(results_t *results, const node_data_t *result)
{
if (results->size + 1 >= results->capacity) {
results->capacity *= 2;
results->results = realloc(results->results, sizeof(node_data_t*) *
results->capacity);
}
results->results[results->size++] = result;
}
static int
cmp_results(const void *_r1, const void *_r2, void *arg)
{
const node_data_t *r1 = *(node_data_t**)_r1, *r2 = *(node_data_t**)_r2;
sort_type_t sort_type = ((int*)arg)[0];
int desc = ((int*)arg)[1];
int cmp = 0;
switch (sort_type) {
case SORT_NAME:
cmp = strcmp(r1->name, r2->name);
break;
case SORT_PATH:
cmp = strcmp(r1->path, r2->path);
break;
case SORT_MIME:
if (!r1->mime)
return 0;
cmp = strcmp(r1->mime, r2->mime);
break;
case SORT_SIZE:
cmp = r1->stat.st_size - r2->stat.st_size;
break;
case SORT_TIME:
cmp = r1->stat.st_mtime - r2->stat.st_mtime;
break;
}
return !desc ? cmp : -cmp;
}
void
results_sort(results_t *results, sort_type_t sort_type, int desc)
{
int arg[2] = { sort_type, desc };
qsort_r(results->results, results->size, sizeof(node_data_t*), cmp_results,
&arg);
}
results_t *
results_filter(results_t *results, const filter_t *filter)
{
results_t *filtered = results_new();
for (size_t i = 0; i < results->size; i++) {
const node_data_t *n = results->results[i];
if (filter->time_low && (n->stat.st_mtime < filter->time_low))
continue;
if (filter->time_high && (n->stat.st_mtime > filter->time_high))
continue;
if (filter->size_low && (n->stat.st_size < filter->size_low))
continue;
if (filter->size_high && (n->stat.st_size > filter->size_high))
continue;
results_insert(filtered, n);
}
results_destroy(results);
return filtered;
}
void
results_destroy(results_t *results)
{
free(results->results);
free(results);
}
int
index_init()
{
magic_cookie = magic_open(MAGIC_MIME);
if (!magic_cookie) {
fprintf(stderr, "[index] error magic_open()\n");
return -1;
}
if (magic_load(magic_cookie, NULL) < 0) {
fprintf(stderr, "[index] error magic_load(): %s\n",
magic_error(magic_cookie));
return -1;
}
return 0;
}
void
index_deinit()
{
magic_close(magic_cookie);
}
map_t *
index_recurse(size_t size, const char *dir, int examine, size_t rootlen)
{
DIR *dirp = opendir(dir);
if (!dirp) {
fprintf(stderr, "[index] error opening directory %s: %s\n", dir,
strerror(errno));
return NULL;
}
map_t *map = map_new(size);
char path[4096];
struct dirent *de = NULL;
while ((de = readdir(dirp))) {
if (de->d_name[0] == '.') {
if (de->d_name[1] == '\0')
continue;
else if (de->d_name[1] == '.')
if (de->d_name[2] == '\0')
continue;
}
snprintf(path, 4096, "%s/%s", dir, de->d_name);
/* stat it */
node_data_t *data = malloc(sizeof(node_data_t));
memset(data, 0, sizeof(node_data_t));
data->name = strdup(de->d_name);
data->path = strdup(&path[rootlen]);
if (stat(path, &data->stat) < 0) {
fprintf(stderr, "[index] error stat() %s: %s\n", path,
strerror(errno));
free(data);
data = NULL;
}
/* examine */
if (examine) {
const char *mime = magic_file(magic_cookie, path);
if (!mime) {
fprintf(stderr, "[index] error magic_file() %s: %s\n", path,
magic_error(magic_cookie));
} else
data->mime = strdup(mime);
}
/* recurse */
map_t *child = NULL;
if (de->d_type == DT_DIR) {
child = index_recurse(size, path, examine, rootlen);
}
map_insert(map, de->d_name, data, child);
}
closedir(dirp);
return map;
}
map_t *
index_new(size_t size, const char *dir, int examine) {
return index_recurse(size, dir, examine, strlen(dir) + 1);
}
void
index_lookup_substr(map_t *index, const char *query,
results_t *results)
{
for (size_t i = 0; i < index->size; i++) {
if (!index->map[i].data)
continue;
for (struct node_s *node = &index->map[i]; node; node = node->next) {
if (strstr(node->data->name, query))
results_insert(results, node->data);
if (node->child)
index_lookup_substr(node->child, query, results);
}
}
}
void
index_lookup_substr_caseinsensitive(map_t *index, const char *query,
results_t *results)
{
for (size_t i = 0; i < index->size; i++) {
if (!index->map[i].data)
continue;
for (struct node_s *node = &index->map[i]; node; node = node->next) {
if (strcasestr(node->data->name, query))
results_insert(results, node->data);
if (node->child)
index_lookup_substr_caseinsensitive(node->child, query, results);
}
}
}
void
index_lookup_exact(map_t *index, const char *query, results_t *results)
{
for (size_t i = 0; i < index->size; i++) {
if (!index->map[i].data)
continue;
for (struct node_s *node = &index->map[i]; node; node = node->next) {
if (strcmp(node->data->name, query) == 0)
results_insert(results, node->data);
if (node->child)
index_lookup_exact(node->child, query, results);
}
}
}
void
index_lookup_regex(map_t *index, const char *query,
results_t *results)
{
}
results_t *
index_lookup(map_t *index, lookup_type_t type, const char *query)
{
results_t *results = results_new();
switch (type) {
case LOOKUP_SUBSTR:
index_lookup_substr(index, query, results);
break;
case LOOKUP_SUBSTR_CASEINSENSITIVE:
index_lookup_substr_caseinsensitive(index, query, results);
break;
case LOOKUP_EXACT:
index_lookup_exact(index, query, results);
break;
case LOOKUP_REGEX:
index_lookup_regex(index, query, results);
break;
}
return results;
}
void
index_destroy(index_t index)
{
}
|