-
Notifications
You must be signed in to change notification settings - Fork 0
/
each_file.c
313 lines (294 loc) · 8.67 KB
/
each_file.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
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <libgen.h>
#include <dirent.h>
#include <sys/stat.h>
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
#ifdef WIN32
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#endif
#include "each_file.h"
static int each_file_dir(const char *path, struct file_type_filter *filters, int flags) {
DIR *d = opendir(path);
if(!d) return errno;
struct dirent *de;
while((de = readdir(d))) {
if(de->d_name[0] == '.' && de->d_name[1] == 0) continue;
if(de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == 0) continue;
char rpath[PATH_MAX + 1 + sizeof(de->d_name) + 1]; // FIXME: maybe allocate new string instead?
if(path[0])
snprintf(rpath, sizeof(rpath) / sizeof(rpath[0]), "%s/%s", path, de->d_name);
else
snprintf(rpath, sizeof(rpath) / sizeof(rpath[0]), "%s", de->d_name);
each_file(rpath, filters, flags);
}
if(closedir(d)) return errno;
return 0;
}
#ifdef WIN32
static int each_file_dirw(const wchar_t *path, struct file_type_filterw *filters, int flags) {
WIN32_FIND_DATAW fdata;
HANDLE h = FindFirstFileW(path, &fdata);
if(h == INVALID_HANDLE_VALUE) return -1;
do {
wchar_t rpath[MAX_PATH + 2];
if(path[0])
swprintf(rpath, MAX_PATH, L"%s\\%s", path, fdata.cFileName);
else
swprintf(rpath, MAX_PATH, L"%s", fdata.cFileName);
int r = each_filew(rpath, filters, flags);
if(r) {
FindClose(h);
return r;
}
} while (FindNextFileW(h, &fdata) != 0);
FindClose(h);
return 0;
}
#endif
#define FILL_PATH_INFO(path) \
p.file_name = (char *)path; \
char *file_dirname_str = strdup(path); \
p.file_dirname = dirname(file_dirname_str); \
char *file_basename_str = strdup(path); \
p.file_basename = basename(file_basename_str); \
char *file_ext_str = strdup(p.file_basename); \
char *file_ext = strrchr(file_ext_str, '.'); \
if(file_ext) { \
*file_ext = 0; \
p.file_ext = file_ext[1] ? file_ext + 1 : 0; \
} \
p.file_base = file_ext_str;
#define FILL_PATH_INFOW(path) \
p.file_name = (wchar_t *)path; \
wchar_t *file_dirname_str = _wcsdup(path); \
p.file_dirname = _wdirname(file_dirname_str); \
wchar_t *file_basename_str = _wcsdup(path); \
p.file_basename = _wbasename(file_basename_str); \
wchar_t *file_ext_str = _wcsdup(p.file_basename); \
wchar_t *file_ext = wcsrchr(file_ext_str, '.'); \
if(file_ext) { \
*file_ext = 0; \
p.file_ext = file_ext[1] ? file_ext + 1 : 0; \
} \
p.file_base = file_ext_str;
#define FREE_PATH_INFO() \
free(file_ext_str); \
free(file_basename_str); \
free(file_dirname_str);
#ifdef HAVE_LIBZIP
static int each_file_zip(const char *path, struct file_type_filter *filters, int flags) {
(void)flags;
int err;
zip_t *z = zip_open(path, ZIP_RDONLY, &err);
if(!z) return err;
int num_entries = zip_get_num_entries(z, 0);
if(num_entries < 0) {
zip_close(z);
return -1;
}
struct path_info p;
p.zip_file_name = (char *)path;
char *zip_dirname_str = strdup(path);
p.zip_file_dirname = dirname(zip_dirname_str);
char *zip_basename_str = strdup(path);
p.zip_file_base = basename(zip_basename_str);
char *zip_ext = strrchr(p.zip_file_base, '.');
if(zip_ext) *zip_ext = 0;
for(int j = 0; j < num_entries; j++) {
zip_stat_t st;
zip_stat_index(z, j, ZIP_STAT_NAME | ZIP_STAT_SIZE, &st);
const char *ext = strrchr(st.name, '.');
if(!ext || !ext[1]) continue;
for(struct file_type_filter *f = filters; f->ext; f++) {
if(strcasecmp(ext, f->ext)) continue;
struct zip_file_stream s;
#ifdef HAVE_GZIP
int r = zip_file_stream_init_index(&s, z, j, (flags & EF_TRANSPARENT_GZIP) ? STREAM_TRANSPARENT_GZIP : 0);
#else
int r = zip_file_stream_init_index(&s, z, j, 0);
#endif
if(r) return r;
FILL_PATH_INFO(st.name);
r = f->file_cb(&p, (struct stream *)&s, f->user_data);
FREE_PATH_INFO();
stream_close((struct stream *)&s);
if(r) return r;
break;
}
}
free(zip_basename_str);
free(zip_dirname_str);
zip_close(z);
return 0;
}
#ifdef WIN32
wchar_t *_wbasename(wchar_t* path) {
wchar_t* base = wcspbrk(path, L"\\/");
return base ? base + 1 : path;
}
wchar_t* _wdirname(wchar_t* path) {
wchar_t* last_backslash = wcspbrk(path, L"\\/");
if(last_backslash != NULL) {
if(last_backslash == path)
*(last_backslash + 1) = L'\0';
else
*last_backslash = L'\0';
} else wcscpy(path, L".");
return path;
}
static int each_file_zipw(const wchar_t *path, struct file_type_filterw *filters, int flags) {
(void)flags;
int fd = _wopen(path, _O_RDONLY | _O_BINARY);
if(!fd) return errno;
int err;
zip_t *z = zip_fdopen(fd, 0, &err);
if(!z) return err;
int num_entries = zip_get_num_entries(z, 0);
if(num_entries < 0) {
zip_close(z);
return -1;
}
struct path_infow p;
p.zip_file_name = (wchar_t *)path;
wchar_t *zip_dirname_str = _wcsdup(path);
p.zip_file_dirname = _wdirname(zip_dirname_str);
wchar_t *zip_basename_str = _wcsdup(path);
p.zip_file_base = _wbasename(zip_basename_str);
wchar_t *zip_ext = wcsrchr(p.zip_file_base, L'.');
if(zip_ext) *zip_ext = 0;
for(int j = 0; j < num_entries; j++) {
zip_stat_t st;
zip_stat_index(z, j, ZIP_STAT_NAME | ZIP_STAT_SIZE, &st);
int l = strlen(st.name);
wchar_t stname[MAX_PATH];
if(l >= MAX_PATH) l = MAX_PATH-1;
for(int k = 0; k < l; k++)
stname[k] = st.name[k];
const wchar_t *ext = wcsrchr(stname, L'.');
if(!ext || !ext[1]) continue;
for(struct file_type_filterw *f = filters; f->ext; f++) {
if(_wcsicmp(ext, f->ext)) continue;
struct zip_file_stream s;
#ifdef HAVE_GZIP
int r = zip_file_stream_init_index(&s, z, j, (flags & EF_TRANSPARENT_GZIP) ? STREAM_TRANSPARENT_GZIP : 0);
#else
int r = zip_file_stream_init_index(&s, z, j, 0);
#endif
if(r) return r;
FILL_PATH_INFOW(stname);
r = f->file_cb(&p, (struct stream *)&s, f->user_data);
FREE_PATH_INFO();
stream_close((struct stream *)&s);
if(r) return r;
break;
}
}
free(zip_basename_str);
free(zip_dirname_str);
zip_close(z);
return 0;
}
#endif /* WIN32 */
#endif /* HAVE_LIBZIP */
static int each_file_file(const char *path, const char *ext, struct file_type_filter *filters, int flags) {
for(struct file_type_filter *f = filters; f->ext; f++) {
if(strcasecmp(ext, f->ext)) continue;
struct path_info p;
#ifdef HAVE_LIBZIP
p.zip_file_name = p.zip_file_base = p.zip_file_dirname = 0;
#endif
if(flags & EF_OPEN_STREAM) {
struct file_stream s;
#ifdef HAVE_GZIP
int r = file_stream_init(&s, path, "rb", (flags & EF_TRANSPARENT_GZIP) ? STREAM_TRANSPARENT_GZIP : 0);
#else
int r = file_stream_init(&s, path, "rb", 0);
#endif
if(r) return r;
FILL_PATH_INFO(path);
r = f->file_cb(&p, (struct stream *)&s, f->user_data);
FREE_PATH_INFO();
stream_close((struct stream *)&s);
if(r) return r;
} else {
FILL_PATH_INFO(path);
f->file_cb(&p, 0, f->user_data);
FREE_PATH_INFO();
}
return 0;
}
return 1;
}
#ifdef WIN32
static int each_file_filew(const wchar_t *path, const wchar_t *ext, struct file_type_filterw *filters, int flags) {
for(struct file_type_filterw *f = filters; f->ext; f++) {
if(_wcsicmp(ext, f->ext)) continue;
struct path_infow p;
p.zip_file_name = p.zip_file_base = p.zip_file_dirname = 0;
if(flags & EF_OPEN_STREAM) {
struct file_stream s;
#ifdef HAVE_GZIP
int r = file_stream_initw(&s, path, L"rb", (flags & EF_TRANSPARENT_GZIP) ? STREAM_TRANSPARENT_GZIP : 0);
#else
int r = file_stream_initw(&s, path, L"rb", 0);
#endif
if(r) return r;
FILL_PATH_INFOW(path);
r = f->file_cb(&p, (struct stream *)&s, f->user_data);
FREE_PATH_INFO();
stream_close((struct stream *)&s);
if(r) return r;
} else {
FILL_PATH_INFOW(path);
f->file_cb(&p, 0, f->user_data);
FREE_PATH_INFO();
}
return 0;
}
return 1;
}
#endif
int each_file(const char *path, struct file_type_filter *filters, int flags) {
struct stat st;
int r = stat(path, &st);
if(r < 0) return errno;
if(S_ISDIR(st.st_mode) && (flags & EF_RECURSE_DIRS)) {
return each_file_dir(path, filters, flags);
} else {
const char *ext = strrchr(path, '.');
if(ext) {
#ifdef HAVE_LIBZIP
if(!strcasecmp(ext, ".zip") && (flags & EF_RECURSE_ARCHIVES) && (flags & EF_OPEN_STREAM))
return each_file_zip(path, filters, flags);
#endif
return each_file_file(path, ext, filters, flags);
}
}
return 0;
}
#ifdef WIN32
int each_filew(const wchar_t *path, struct file_type_filterw *filters, int flags) {
struct _stat st;
int r = _wstat(path, &st);
if(r < 0) return errno;
if(S_ISDIR(st.st_mode) && (flags & EF_RECURSE_DIRS)) {
return each_file_dirw(path, filters, flags);
} else {
const wchar_t *ext = wcsrchr(path, L'.');
if(ext) {
#ifdef HAVE_LIBZIP
if(!_wcsicmp(ext, L".zip") && (flags & EF_RECURSE_ARCHIVES) && (flags & EF_OPEN_STREAM))
return each_file_zipw(path, filters, flags);
#endif
return each_file_filew(path, ext, filters, flags);
}
}
return 0;
}
#endif