-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind.c
284 lines (214 loc) · 5.31 KB
/
find.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
/*
* Find files.
*/
#define _GNU_SOURCE
#define _DEFAULT_SOURCE
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include "log.h"
#include "mem.h"
#include "util.h"
#include "find.h"
struct file_table_entry {
struct hash_table_entry hte;
struct file_data file_data;
};
static struct file_table_entry *hte_to_fte(struct hash_table_entry *hte)
{
return container_of(hte, struct file_table_entry, hte);
}
void file_table_entry_clean(struct hash_table_entry *hte)
{
struct file_table_entry *fte = hte_to_fte(hte);
list_remove(&hte->list_entry);
mem_free(fte);
}
static struct hash_table_entry *ht_entry_init(const char *file_name,
unsigned long file_size, struct list *list)
{
struct file_table_entry *fte;
unsigned int len = strlen(file_name);
fte = mem_alloc_zero(sizeof(*fte) + len + 1);
fte->file_data.name_len = len;
memcpy(fte->file_data.name, file_name, len);
digest_init(&fte->file_data.digest);
//debug("%p: '%s'\n", list, file_name);
hash_table_entry_init(&fte->hte, list, file_size, &fte->file_data);
return &fte->hte;
}
static long int __attribute__((unused)) get_file_size_ftell(const char *file)
{
FILE *fp;
long int size;
int result;
fp = fopen(file, "r");
if (!fp) {
log("ERROR: fopen '%s' failed: %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
result = fseek(fp, 0, SEEK_END);
if (result) {
log("ERROR: fseek '%s' failed: %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
size = ftell(fp);
if (size == -1) {
log("ERROR: ftell '%s' failed: %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
fclose(fp);
return size;
}
static off_t get_file_size_stat64(const char *file)
{
struct stat64 st;
int result;
result = stat64(file, &st);
if (result) {
log("ERROR: stat '%s' failed: %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
if (st.st_size < 0) {
log("ERROR: stat '%s' failed: %s\n", file, strerror(errno));
exit(EXIT_FAILURE);
}
return st.st_size;
}
static void process_file(const char *file, struct hash_table *ht)
{
struct hash_table_entry *hte;
off_t size;
size = get_file_size_stat64(file);
if (size) {
unsigned int index = hash_table_index(ht, (long int)size);
hte = ht_entry_init(file, (long int)size, &ht->array[index]);
hash_table_insert(ht, index, hte);
//debug("index-%u: size = %ld, %s\n", index, (long int)size, file);
} else {
hte = ht_entry_init(file, (long int)size, &ht->extras);
hash_table_insert_extra(ht, hte);
}
}
struct find_files_cb_data {
struct work_queue *wq;
struct hash_table *ht;
bool (*check_for_signals)(void);
char *sub_path;
};
static int find_files_cb(struct work_item *wi)
{
struct find_files_cb_data *cbd = wi->cb_data;
int result;
//char *dup;
//dup = strdup(cbd->sub_path);
//debug("> '%s'\n", cbd->sub_path);
result = find_files(cbd->wq, cbd->ht, cbd->check_for_signals,
cbd->sub_path);
assert(wi->list_entry.in_use);
list_remove(&wi->list_entry);
mem_free(wi);
//debug("< '%s'\n", dup);
//free(dup);
return result;
}
static void find_files_queue_work(unsigned int id, struct work_queue *wq,
struct hash_table *ht, bool (*check_for_signals)(void),
const char *sub_path)
{
struct find_files_cb_data *cbd;
struct work_item *wi;
unsigned int sub_path_len = strlen(sub_path) + 1;
wi = mem_alloc_zero(sizeof(*wi) + sizeof(*cbd) + sub_path_len);
cbd = (void*)(wi + 1);
cbd->sub_path = (void*)(cbd + 1);
cbd->wq = wq;
cbd->ht = ht;
cbd->check_for_signals = check_for_signals;
memcpy(cbd->sub_path, sub_path, sub_path_len);
wi->id = id;
wi->cb = find_files_cb;
wi->cb_data = cbd;
work_queue_add_item(wq, wi);
}
int find_files(struct work_queue *wq, struct hash_table *ht,
bool (*check_for_signals)(void), const char *parent_path)
{
unsigned int parent_len = 0;
struct dirent *de;
unsigned int id;
int result = 0;
DIR *dp;
//debug("> '%s'\n", parent_path);
dp = opendir(parent_path);
if (!dp) {
log("ERROR: opendir '%s' failed: %s\n", parent_path,
strerror(errno));
exit(EXIT_FAILURE);
}
for (id = 0; ; id++) {
if (check_for_signals()) {
//debug("exit on signal\n");
result = -1;
goto exit;
}
de = readdir(dp);
if (!de) {
//debug("done: '%s'\n", parent_path);
break;
}
//debug("d_name = '%s'\n", de->d_name);
switch (de->d_type) {
case DT_DIR: {
char *sub_path;
if (test_for_dots(de->d_name)) {
continue;
}
//debug("DIR: '%s'\n", de->d_name);
if (!parent_len) {
parent_len = strlen(parent_path);
}
sub_path = make_sub_path(parent_path, parent_len,
de->d_name);
//debug("DT_DIR: %s\n", sub_path);
find_files_queue_work(id, wq, ht, check_for_signals,
sub_path);
mem_free(sub_path);
break;
}
case DT_REG: {
char *sub_path;
if (!parent_len) {
parent_len = strlen(parent_path);
}
sub_path = make_sub_path(parent_path, parent_len,
de->d_name);
//debug("DT_REG: %s\n", sub_path);
process_file(sub_path, ht);
mem_free(sub_path);
break;
}
default:
break;
}
}
exit:
closedir(dp);
//debug("< '%s'\n", parent_path);
return result;
}
unsigned long file_count(struct hash_table *ht)
{
unsigned long count;
unsigned int i;
count = list_item_count(&ht->extras);
for (i = 0; i < ht->count; i++) {
count += list_item_count(&(ht->array[i]));
}
return count;
}