Debian Codegraph @

src/file.c

raw
1 /**
2 * This file is part of the Detox package.
3 *
4 * Copyright (c) Doug Harple <[email protected]>
5 *
6 * For the full copyright and license information, please view the LICENSE
7 * file that was distributed with this source code.
8 */
9
10 #include "config.h"
11
12 #include <sys/stat.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <dirent.h>
16 #include <string.h>
17 #include <errno.h>
18
19 #include "clean_utf_8.h"
20 #include "file.h"
21 #include "filelist.h"
22 #include "sequence.h"
23 #include "wrapped.h"
24
25 /**
26 * Determines if the file should be ignored
27 *
28 * @param filename The file to check
29 * @param options The main options
30 *
31 * @return bool
32 */
33 static int ignore_file(const char *filename, const options_t *options)
34 {
35 char *file_walk;
36
37 if (filename[0] == '.') {
38 return 1;
39 }
40
41 while ((file_walk = filelist_get(options->files_to_ignore))) {
42 if (strcmp(filename, file_walk) == 0) {
43 return 1;
44 }
45 }
46
47 return 0;
48 }
49
50 /**
51 * Whether or not the file is "." or ".."
52 *
53 * @param filename The file to check
54 *
55 * @return bool
56 */
57 static int is_protected(const char *filename)
58 {
59 if (filename == NULL) {
60 return 0;
61 }
62
63 return (filename[0] == '.' && (filename[1] == '\0' || (filename[1] == '.' && filename[2] == '\0')));
64 }
65
66 /*
67 * Renames file to a safe filename.
68 */
69 char *parse_file(char *filename, options_t *options)
70 {
71 char *old_filename, *old_filename_ptr, *new_filename;
72 char *work, *hold;
73
74 struct stat stat_info_old;
75 struct stat stat_info_new;
76 int err;
77 size_t len;
78
79 len = strlen(filename) + 1;
80 old_filename = wrapped_malloc(len);
81
82 memcpy(old_filename, filename, len);
83
84 old_filename_ptr = strrchr(old_filename, '/');
85 if (old_filename_ptr != NULL) {
86 old_filename_ptr++;
87 } else {
88 old_filename_ptr = old_filename;
89 }
90
91 /*
92 * Do the actual filename cleaning
93 */
94
95 work = wrapped_strdup(old_filename_ptr);
96
97 if (is_protected(work)) {
98 work = NULL;
99 }
100
101 hold = sequence_run_filters(options->sequence_to_use, work);
102 if (work != NULL) {
103 free(work);
104 }
105 work = hold;
106
107 if (work == NULL) {
108 return old_filename;
109 }
110
111 /* check to see if nothing changed */
112 if (strcmp(old_filename_ptr, work) == 0) {
113 return old_filename;
114 }
115
116 len = (old_filename_ptr - old_filename);
117 new_filename = wrapped_malloc(len + strlen(work) + 1);
118
119 strncpy(new_filename, old_filename, len);
120 strcpy(new_filename + len, work);
121
122 free(work);
123
124 err = lstat(old_filename, &stat_info_old);
125 if (err == -1) {
126 free(new_filename);
127 return old_filename;
128 }
129
130 err = lstat(new_filename, &stat_info_new);
131 if (err != -1) { // New file exists
132 if (stat_info_old.st_dev != stat_info_new.st_dev || // Different device
133 stat_info_old.st_ino != stat_info_new.st_ino || // Different inode
134 stat_info_old.st_nlink > 1) { // More than one hard link
135 fprintf(stderr, "Cannot rename %s to %s: file already exists\n", old_filename, new_filename);
136
137 free(new_filename);
138 return old_filename;
139 }
140 }
141
142 if (options->verbose || options->dry_run) {
143 printf("%s -> %s\n", old_filename, new_filename);
144 }
145
146 if (options->dry_run) {
147 free(new_filename);
148 return old_filename;
149 }
150
151 err = rename(old_filename, new_filename);
152 if (err == -1) {
153 fprintf(stderr, "Cannot rename %s to %s: %s\n", old_filename, new_filename, strerror(errno));
154 free(new_filename);
155 return old_filename;
156 }
157
158 free(old_filename);
159
160 return new_filename;
161 }
162
163 /*
164 * Handles directory.
165 */
166 void parse_dir(char *filename, options_t *options)
167 {
168 char *new_file, *work;
169 DIR *dir_handle;
170 struct dirent *dir_entry;
171 struct stat stat_info;
172 int check_file;
173 int err;
174 size_t new_file_length;
175
176 err = lstat(filename, &stat_info);
177 if (err == -1) {
178 return;
179 }
180
181 if (!S_ISDIR(stat_info.st_mode)) {
182 return;
183 }
184
185 new_file_length = strlen(filename) + 1024;
186 new_file = wrapped_malloc(new_file_length);
187
188 /*
189 * Parse directory
190 */
191
192 dir_handle = opendir(filename);
193 if (dir_handle == NULL) {
194 fprintf(stderr, "unable to parse: %s\n", strerror(errno));
195 free(new_file);
196
197 // too many open files
198 if (errno == EMFILE) {
199 exit(EXIT_FAILURE);
200 }
201
202 return;
203 }
204
205 dir_entry = readdir(dir_handle);
206
207 while (dir_entry != NULL) {
208
209 /*
210 * Check for files that need to be ignored
211 */
212 check_file = !ignore_file(dir_entry->d_name, options);
213
214 if (check_file) {
215 snprintf(new_file, new_file_length, "%s/%s", filename, dir_entry->d_name);
216
217 lstat(new_file, &stat_info);
218 if (S_ISDIR(stat_info.st_mode)) {
219 work = parse_file(new_file, options);
220 if (options->recurse) {
221 parse_dir(work, options);
222 }
223 free(work);
224 } else if (S_ISREG(stat_info.st_mode) || options->special) {
225 work = parse_file(new_file, options);
226 free(work);
227 }
228 }
229 dir_entry = readdir(dir_handle);
230 }
231 closedir(dir_handle);
232 }
233
234 /**
235 * Parses files in a stream, either read from STDIN, or a filename. Writes the
236 * results to a stream, either STDOUT, or a filename.
237 *
238 * @param in_filename Filename to read from. Set to NULL to use STDIN.
239 * @param out_filename Filename to write to. Set to NULL to use STDOUT.
240 * @param options Detox options.
241 */
242 void parse_inline(char *in_filename, char *out_filename, options_t *options)
243 {
244 FILE *in_fp;
245 FILE *out_fp;
246 size_t buf_size;
247 size_t padding;
248
249 char *base;
250 char *hold;
251 char *seek;
252 char *work;
253 int err;
254 int has_newline;
255 int remaining;
256
257 if (in_filename != NULL) {
258 if (!(in_fp = fopen(in_filename, "r"))) {
259 fprintf(stderr, "%s: %s\n", in_filename, strerror(errno));
260 return;
261 }
262 } else {
263 in_fp = stdin;
264 }
265
266 if (out_filename != NULL) {
267 if (!(out_fp = fopen(out_filename, "w"))) {
268 fprintf(stderr, "%s: %s\n", out_filename, strerror(errno));
269 return;
270 }
271 } else {
272 out_fp = stdout;
273 }
274
275 buf_size = INLINE_BUF_SIZE;
276 padding = INLINE_BUF_PADDING;
277 base = wrapped_malloc(buf_size);
278
279 while (fgets(base, buf_size - padding, in_fp)) {
280
281 hold = strrchr(base, '\n');
282
283 if (hold == NULL) {
284
285 #ifdef DEBUG
286 fprintf(stderr, "detox: debug: fgets() didn't find a new line\n");
287 #endif
288
289 //
290 // Check to see if we stopped in the middle of a UTF-8 character.
291 //
292
293 hold = seek = strchr(base, '\0');
294 seek--;
295
296 #ifdef DEBUG
297 if (is_utf_8_cont(*seek)) {
298 fprintf(stderr, "detox: debug: looks like we're in the middle of a UTF-8 character\n");
299 }
300 #endif
301
302 while (is_utf_8_cont(*seek) && (hold - seek) < UTF_8_MAX_LENGTH) {
303 #ifdef DEBUG
304 fprintf(stderr, "detox: debug: at %02x, moving back one\n", (unsigned char) *seek);
305 #endif
306 seek--;
307 }
308
309 #ifdef DEBUG
310 fprintf(stderr, "detox: debug: now at %02x\n", (unsigned char) *seek);
311 #endif
312
313 if (is_utf_8_start(*seek)) {
314 #ifdef DEBUG
315 fprintf(stderr, "detox: debug: at %02x, this is the start of a UTF-8 char, seeking ahead\n", (unsigned char) *seek);
316 #endif
317 remaining = get_utf_8_width(*seek);
318 do {
319 #ifdef DEBUG
320 fprintf(stderr, "detox: debug: at %02x\n", (unsigned char) *seek);
321 #endif
322 seek++;
323 remaining--;
324 } while (remaining > 0 && is_utf_8_cont(*seek));
325
326 #ifdef DEBUG
327 fprintf(stderr, "detox: debug: done with initial seek at %02x\n", (unsigned char) *seek);
328 #endif
329
330 if (remaining > 0 && *seek == '\0') {
331 #ifdef DEBUG
332 fprintf(stderr, "detox: debug: we still need more data\n");
333 #endif
334
335 // try bringing bytes in one at a time
336 while (remaining > 0) {
337 err = fgetc(in_fp);
338 if (err == EOF) {
339 #ifdef DEBUG
340 fprintf(stderr, "detox: debug: hit EOF\n");
341 #endif
342 break;
343 }
344
345 *seek = err;
346
347 #ifdef DEBUG
348 fprintf(stderr, "detox: debug: read %02x\n", (unsigned char) *seek);
349 #endif
350
351 // but if it isn't a UTF-8 continuation byte
352 if (!is_utf_8_cont(*seek)) {
353 #ifdef DEBUG
354 fprintf(stderr, "detox: debug: %02x is not a UTF-8 continuation byte\n", (unsigned char) *seek);
355 #endif
356 // try to push it back on to the buffer
357 err = ungetc(*seek, in_fp);
358 if (err == EOF) {
359 fprintf(stderr, "detox: warning: stream push back failed\n");
360 seek++;
361 }
362 remaining = 0;
363 break;
364 }
365
366 seek++;
367 remaining--;
368 }
369 *seek = '\0';
370 }
371
372 #ifdef DEBUG
373 fprintf(stderr, "detox: debug: done with secondary seek at %02x\n", (unsigned char) *seek);
374 #endif
375 }
376 }
377
378 // if we did fgetc(), and ungetc() failed, this might return
379 // differently from before
380
381 hold = strrchr(base, '\n');
382
383 if (hold == NULL) {
384 has_newline = 0;
385 } else {
386 has_newline = 1;
387 *hold = '\0';
388 }
389
390 work = wrapped_strdup(base);
391
392 if (is_protected(work)) {
393 work = NULL;
394 }
395
396 hold = sequence_run_filters(options->sequence_to_use, work);
397 if (work != NULL) {
398 free(work);
399 }
400 work = hold;
401
402 //
403 // we're using printf("%s", ...) because don't know what is in the
404 // string...
405 //
406
407 if (work != NULL) {
408 fprintf(out_fp, "%s", work);
409 free(work);
410 } else {
411 fprintf(out_fp, "%s", base);
412 }
413
414 if (has_newline) {
415 fprintf(out_fp, "\n");
416 }
417 }
418
419 if (in_filename != NULL) {
420 fclose(in_fp);
421 }
422
423 if (out_filename != NULL) {
424 fclose(out_fp);
425 }
426 }