1 /*
2  *
3  *   Copyright (c) International Business Machines  Corp., 2002
4  *
5  *   This program is free software;  you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation; either version 2 of the License, or
8  *   (at your option) any later version.
9  *
10  *   This program is distributed in the hope that it will be useful,
11  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  *   the GNU General Public License for more details.
14  *
15  *   You should have received a copy of the GNU General Public License
16  *   along with this program;  if not, write to the Free Software
17  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /* 10/31/2002   Port to LTP     robbiew@us.ibm.com */
21 /* 06/30/2001   Port to Linux   nsharoff@us.ibm.com */
22 
23 /*
24  * NAME
25  *	test_func64.c - nftw64() calls these functions.
26  */
27 
28 #include "nftw64.h"
29 
30 extern pathdata pathdat[];
31 extern struct list mnem[], badlist[];
32 extern char *dirlist[NDIRLISTENTS];
33 extern const char *rw_fs_name;
34 extern int npathdats, ngoods, nbads, nmnem, visit, next_fd[4];
35 extern FILE *temp;
36 
37 /*
38  * Calling function should free the dirlist array.
39  */
40 int
41 test_func1(const char *path_name, const struct stat64 *stat_pointer,
42 	   int ftw_integer, struct FTW *ftwp)
43 {
44 	char *s;
45 	const char *p;
46 	temp = stderr;
47 
48 	if ((s = malloc((size_t)(strlen((char *)path_name) + 1)))
49 	    == NULL) {
50 		perror("malloc in test_func1");
51 		return 999;
52 	}
53 
54 	if ((p = strstr(path_name, NFTW)) != NULL) {
55 		p += strlen(NFTW);
56 	} else {
57 		p = path_name;
58 	}
59 
60 	(void)strcpy(s, p);
61 	dirlist[visit++] = s;
62 
63 #ifdef DEBUG
64 	fprintf(temp, "INFO: Call to fn() at %s\n", path_name);
65 #endif
66 
67 	if (visit >= NDIRLISTENTS) {
68 		fprintf(temp, "ERROR: Too many paths traversed\n");
69 		return 999;
70 	}
71 	return 0;
72 }
73 
74 int
75 test_func3(const char *path_name, const struct stat64 *stat_pointer,
76 	   int ftw_integer, struct FTW *ftwp)
77 {
78 	visit++;
79 	temp = stderr;
80 #ifdef DEBUG
81 	fprintf(temp, "INFO: Call to fn() at %s\n", path_name);
82 #endif
83 
84 	if (visit >= NDIRLISTENTS) {
85 		fprintf(temp, "ERROR: Too many paths traversed\n");
86 		return 999;
87 	}
88 
89 	if (strcmp(path_name, "./tmp/data/dirl/dir_right.1/dir_right.2/right.3")
90 	    == 0) {
91 		fprintf(temp,
92 			"ERROR: Target of right.3 was already reported so this file should not be\n");
93 		return 999;
94 	}
95 
96 	return 0;
97 }
98 
99 int
100 test_func4(const char *path_name, const struct stat64 *stat_pointer,
101 	   int ftw_integer, struct FTW *ftwp)
102 {
103 	visit++;
104 	do_info(path_name);
105 
106 	/* Stop traversal once directory is visited. */
107 	if (strcmp(path_name, "./tmp/data/d777") == 0)
108 		return 999;
109 	return 0;
110 }
111 
112 int
113 test_func5(const char *path_name, const struct stat64 *stat_pointer,
114 	   int ftw_integer, struct FTW *ftwp)
115 {
116 	char pathcwd[PATH_MAX];
117 
118 	temp = stderr;
119 	if (ftw_integer == FTW_D)
120 		return (0);
121 
122 	if (getcwd(pathcwd, sizeof(pathcwd)) == NULL) {
123 		perror("getcwd");
124 		return 998;
125 	}
126 
127 	if (strstr(path_name, pathcwd) == 0) {
128 		fprintf(temp, "ERROR: For file %s cwd is %s\n", path_name,
129 			pathcwd);
130 		return 999;
131 	}
132 
133 	return (0);
134 }
135 
136 int
137 test_func7(const char *path_name, const struct stat64 *stat_pointer,
138 	   int ftw_integer, struct FTW *ftwp)
139 {
140 	int i, found;
141 	const char *p;
142 
143 	temp = stderr;
144 	do_info(path_name);
145 
146 	if ((p = strstr(path_name, NFTW)) != NULL) {
147 		p += strlen(NFTW);
148 	} else {
149 		p = path_name;
150 	}
151 
152 	for (found = i = 0; i < nbads; i++) {
153 		if (strcmp(p, badlist[i].s) == 0) {
154 			found++;
155 			break;
156 		}
157 	}
158 
159 	if (!found) {
160 		fprintf(temp, "ERROR: Should not have traversed %s\n",
161 			path_name);
162 		return 999;
163 	}
164 	return 0;
165 }
166 
167 int
168 test_func8(const char *path_name, const struct stat64 *stat_pointer,
169 	   int ftw_integer, struct FTW *ftwp)
170 {
171 	int i;
172 	const char *p;
173 	struct stat st_buf;
174 
175 	temp = stderr;
176 	do_info(path_name);
177 
178 	if ((p = strstr(path_name, NFTW)) != NULL) {
179 		p += strlen(NFTW);
180 	} else {
181 		p = path_name;
182 	}
183 
184 	for (i = 0; i < nbads; i++) {
185 		if (ftw_integer == FTW_D || ftw_integer == FTW_F ||
186 		    ftw_integer == FTW_SL) {
187 			if ((((ftw_integer == FTW_D) || (ftw_integer ==
188 							 FTW_F)) ?
189 			     stat(path_name, &st_buf) : lstat(path_name,
190 							      &st_buf)) == -1) {
191 				perror("stat");
192 				return 999;
193 			}
194 
195 			if (st_buf.st_dev != stat_pointer->st_dev) {
196 				fprintf(temp,
197 					"ERROR: st_dev members do not match for %s\n",
198 					path_name);
199 				return 999;
200 			}
201 
202 			if (st_buf.st_ino != stat_pointer->st_ino) {
203 				fprintf(temp,
204 					"ERROR: st_ino members do not match for %s\n",
205 					path_name);
206 				return 999;
207 			}
208 
209 			if (st_buf.st_mode != stat_pointer->st_mode) {
210 				fprintf(temp,
211 					"ERROR: st_mode members do not match for %s\n",
212 					path_name);
213 				return 999;
214 			}
215 
216 			if (st_buf.st_nlink != stat_pointer->st_nlink) {
217 				fprintf(temp,
218 					"ERROR: st_nlink members d o not match for %s\n",
219 					path_name);
220 				return 999;
221 			}
222 
223 			if (st_buf.st_uid != stat_pointer->st_uid) {
224 				fprintf(temp,
225 					"ERROR: st_uid members do not match for %s\n",
226 					path_name);
227 				return 999;
228 			}
229 
230 			if (st_buf.st_gid != stat_pointer->st_gid) {
231 				fprintf(temp,
232 					"ERROR: st_gid members do not match for %s\n",
233 					path_name);
234 				return 999;
235 			}
236 
237 			if (st_buf.st_size != stat_pointer->st_size) {
238 				fprintf(temp,
239 					"ERROR: st_size members do not match for %s\n",
240 					path_name);
241 				return 999;
242 			}
243 		}
244 
245 	}
246 	return 0;
247 }
248 
249 int
250 test_func9(const char *path_name, const struct stat64 *stat_pointer,
251 	   int ftw_integer, struct FTW *ftwp)
252 {
253 	int i;
254 	const char *p;
255 
256 	do_info(path_name);
257 
258 	temp = stderr;
259 	if ((p = strstr(path_name, NFTW)) != NULL) {
260 		p += strlen(NFTW);
261 	} else {
262 		p = path_name;
263 	}
264 
265 	for (i = 0; i < nbads; i++) {
266 		if (strcmp(p, badlist[i].s) == 0) {
267 
268 			if (ftw_integer == FTW_F) {
269 				if (ftw_integer != badlist[i].i) {
270 					fprintf(temp,
271 						"ERROR: Bad thrid arg to fn () for %s\n",
272 						path_name);
273 					fprintf(temp, "       Expected %s\n",
274 						ftw_mnemonic(badlist[i].i));
275 					fprintf(temp, "       Received %s\n",
276 						ftw_mnemonic(ftw_integer));
277 					return 999;
278 				}
279 			}
280 		}
281 	}
282 	return 0;
283 }
284 
285 int
286 test_func10(const char *path_name, const struct stat64 *stat_pointer,
287 	    int ftw_integer, struct FTW *ftwp)
288 {
289 	int i;
290 	const char *p;
291 
292 	temp = stderr;
293 	do_info(path_name);
294 
295 	if ((p = strstr(path_name, NFTW)) != NULL) {
296 		p += strlen(NFTW);
297 	} else {
298 		p = path_name;
299 	}
300 
301 	for (i = 0; i < nbads; i++) {
302 		if (strcmp(p, badlist[i].s) == 0) {
303 			if (ftw_integer == FTW_D) {
304 				if (ftw_integer != badlist[i].i) {
305 					fprintf(temp,
306 						"ERROR: Bad third arg to fn () for %s\n",
307 						path_name);
308 					fprintf(temp, "       Expected %s\n",
309 						ftw_mnemonic(badlist[i].i));
310 					fprintf(temp, "       Received %s\n",
311 						ftw_mnemonic(ftw_integer));
312 					return 999;
313 				}
314 			}
315 		}
316 	}
317 	return 0;
318 }
319 
320 int
321 test_func11(const char *path_name, const struct stat64 *stat_pointer,
322 	    int ftw_integer, struct FTW *ftwp)
323 {
324 	int i;
325 	const char *p;
326 
327 	temp = stderr;
328 	do_info(path_name);
329 
330 	if ((p = strstr(path_name, NFTW)) != NULL) {
331 		p += strlen(NFTW);
332 	} else {
333 		p = path_name;
334 	}
335 
336 	for (i = 0; i < nbads; i++) {
337 		if (strcmp(p, badlist[i].s) == 0) {
338 			if (ftw_integer == FTW_DP) {
339 				if (ftw_integer != badlist[i].i) {
340 					fprintf(temp,
341 						"ERROR: Bad third arg to fn () for %s\n",
342 						path_name);
343 					fprintf(temp, "       Expected %s\n",
344 						ftw_mnemonic(badlist[i].i));
345 					fprintf(temp, "       Received %s\n",
346 						ftw_mnemonic(ftw_integer));
347 					return 999;
348 				}
349 			}
350 		}
351 	}
352 	return 0;
353 }
354 
355 int
356 test_func12(const char *path_name, const struct stat64 *stat_pointer,
357 	    int ftw_integer, struct FTW *ftwp)
358 {
359 	int i;
360 	const char *p;
361 
362 	temp = stderr;
363 	do_info(path_name);
364 
365 	if ((p = strstr(path_name, NFTW)) != NULL) {
366 		p += strlen(NFTW);
367 	} else {
368 		p = path_name;
369 	}
370 
371 	for (i = 0; i < nbads; i++) {
372 		if (strcmp(p, badlist[i].s) == 0) {
373 			if (ftw_integer == FTW_SL) {
374 				if (ftw_integer != badlist[i].i) {
375 					fprintf(temp,
376 						"ERROR: Bad third arg to fn() for %s.  Expected %s, Received %s\n",
377 						path_name,
378 						ftw_mnemonic(badlist[i].i),
379 						ftw_mnemonic(ftw_integer));
380 					return 999;
381 				}
382 			}
383 		}
384 	}
385 	return 0;
386 }
387 
388 int
389 test_func13(const char *path_name, const struct stat64 *stat_pointer,
390 	    int ftw_integer, struct FTW *ftwp)
391 {
392 	int i;
393 	const char *p;
394 
395 	temp = stderr;
396 	do_info(path_name);
397 
398 	if ((p = strstr(path_name, NFTW)) != NULL) {
399 		p += strlen(NFTW);
400 	} else {
401 		p = path_name;
402 	}
403 
404 	for (i = 0; i < nbads; i++) {
405 		if (strcmp(p, badlist[i].s) == 0) {
406 
407 			if (ftw_integer == FTW_SLN) {
408 				if (ftw_integer != badlist[i].i) {
409 					fprintf(temp,
410 						"ERROR: Bad third arg to fn() for %s\n",
411 						path_name);
412 					fprintf(temp, "       Expected %s\n",
413 						ftw_mnemonic(badlist[i].i));
414 					fprintf(temp, "       Received %s\n",
415 						ftw_mnemonic(ftw_integer));
416 					return 999;
417 				}
418 			}
419 		}
420 	}
421 	return 0;
422 }
423 
424 int
425 test_func14(const char *path_name, const struct stat64 *stat_pointer,
426 	    int ftw_integer, struct FTW *ftwp)
427 {
428 	int i;
429 	const char *p;
430 
431 	temp = stderr;
432 	do_info(path_name);
433 
434 	if ((p = strstr(path_name, NFTW)) != NULL) {
435 		p += strlen(NFTW);
436 	} else {
437 		p = path_name;
438 	}
439 
440 	for (i = 0; i < nbads; i++) {
441 		if (strcmp(p, badlist[i].s) == 0) {
442 
443 			if (ftw_integer == FTW_DNR) {
444 				if (ftw_integer != badlist[i].i) {
445 					fprintf(temp,
446 						"ERROR: Bad third arg to fn() for %s\n",
447 						path_name);
448 					fprintf(temp, "       Expected %s\n",
449 						ftw_mnemonic(badlist[i].i));
450 					fprintf(temp, "       Received %s\n",
451 						ftw_mnemonic(ftw_integer));
452 					return 999;
453 				}
454 			}
455 		}
456 	}
457 	return 0;
458 }
459 
460 int
461 test_func15(const char *path_name, const struct stat64 *stat_pointer,
462 	    int ftw_integer, struct FTW *ftwp)
463 {
464 
465 	temp = stderr;
466 	do_info(path_name);
467 	if (strcmp(path_name, "./tmp/data/d666/errs") == 0) {
468 		if (ftw_integer != FTW_NS) {
469 			fprintf(temp,
470 				"ERROR: FTW_NS not passed for file in unsearchable dir\n");
471 			return 999;
472 		}
473 	}
474 	return 0;
475 }
476 
477 int
478 test_func16(const char *path_name, const struct stat64 *stat_pointer,
479 	    int ftw_integer, struct FTW *ftwp)
480 {
481 	const char *p;
482 	temp = stderr;
483 
484 	if ((p = strstr(path_name, NFTW2)) != NULL) {
485 		p += strlen(NFTW2) + 1;
486 	} else {
487 		p = path_name;
488 	}
489 
490 	if (ftwp->level != getlev(p)) {
491 		fprintf(temp, "ERROR: Incorrect value of level for %s\n",
492 			path_name);
493 		fprintf(temp, "       Expected %d, received %d\n",
494 			getlev(p), ftwp->level);
495 		return 999;
496 	}
497 	if (ftwp->base != getbase(path_name)) {
498 		fprintf(temp, "ERROR: Incorrect value of base for %s\n",
499 			path_name);
500 		fprintf(temp, "       Expected %d, received %d\n",
501 			getbase(path_name), ftwp->base);
502 		return 999;
503 	}
504 	return 0;
505 }
506 
507 int
508 test_func17(const char *path_name, const struct stat64 *stat_pointer,
509 	    int ftw_integer, struct FTW *ftwp)
510 {
511 	do_info(path_name);
512 
513 	if (ftw_integer == FTW_SL) {
514 		visit++;
515 		return 999;
516 	}
517 	return 0;
518 }
519 
520 int
521 test_func18(const char *path_name, const struct stat64 *stat_pointer,
522 	    int ftw_integer, struct FTW *ftwp)
523 {
524 	do_info(path_name);
525 	if (ftw_integer == FTW_SLN) {
526 		visit++;
527 		return 999;
528 	}
529 	return 0;
530 }
531 
532 int
533 test_func19(const char *path_name, const struct stat64 *stat_pointer,
534 	    int ftw_integer, struct FTW *ftwp)
535 {
536 	do_info(path_name);
537 	temp = stderr;
538 	visit++;
539 	if (ftw_integer == FTW_DNR) {
540 		if (strcmp(path_name, "./tmp/data/d333") == 0) {
541 			return 0;
542 		} else {
543 			fprintf(temp,
544 				"ERROR: When FTW_DNR is passed to the function fn the\n");
545 			fprintf(temp,
546 				"       descendants of the directory should not have\n");
547 			fprintf(temp, "       Been processed\n");
548 			return 999;
549 		}
550 	} else {
551 		fprintf(temp,
552 			"ERROR: Directory has read permission or FTW_DNR was not passed to fn\n");
553 		return 999;
554 	}
555 }
556 
557 int
558 test_func20(const char *path_name, const struct stat64 *stat_pointer,
559 	    int ftw_integer, struct FTW *ftwp)
560 {
561 	return 0;
562 }
563 
564 int
565 test_func21(const char *path_name, const struct stat64 *stat_pointer,
566 	    int ftw_integer, struct FTW *ftwp)
567 {
568 	int fd;
569 
570 	do_info(path_name);
571 	temp = stderr;
572 	/* get next file descriptor available */
573 	if ((fd = open(path_name, O_RDONLY)) == -1) {
574 		perror("open");
575 		return 999;
576 	}
577 
578 	if (close(fd) == -1) {
579 		perror("close");
580 		return 999;
581 	}
582 
583 	if ((fd != next_fd[0]) && (fd != next_fd[1])) {
584 		fprintf(temp,
585 			"ERROR: Expected next fd available to be %d (none used) or %d (1 used)\n",
586 			next_fd[0], next_fd[1]);
587 		fprintf(temp, "       Next fd available is %d\n", fd);
588 		return 999;
589 	}
590 	return 0;
591 }
592 
593 int
594 test_func22(const char *path_name, const struct stat64 *stat_pointer,
595 	    int ftw_integer, struct FTW *ftwp)
596 {
597 	int fd;
598 	int i;
599 
600 	do_info(path_name);
601 	/* get next file descriptor available */
602 	if ((fd = open(path_name, O_RDONLY)) == -1) {
603 		perror("open");
604 		return 999;
605 	}
606 
607 	if (close(fd) == -1) {
608 		perror("close");
609 		return 999;
610 	}
611 
612 	for (i = 0; i <= ftwp->level + 1; i++) {
613 		if (fd == next_fd[i])
614 			return 0;
615 	}
616 
617 	fprintf(temp,
618 		"ERROR: At the start of the traversal the next four fds were: %d, %d, %d, and %d\n",
619 		next_fd[0], next_fd[1], next_fd[2], next_fd[3]);
620 	fprintf(temp, "       Traversing level %d the next fd is %d\n",
621 		ftwp->level, fd);
622 	return 999;
623 }
624 
625 int
626 test_func23(const char *path_name, const struct stat64 *stat_pointer,
627 	    int ftw_integer, struct FTW *ftwp)
628 {
629 	visit++;
630 	temp = stderr;
631 	do_info(path_name);
632 
633 	if (ftw_integer == FTW_F) {
634 
635 #ifdef DEBUG
636 		fprintf(temp,
637 			"INFO: fn() returning non-zero after traversal of %d objects\n",
638 			visit);
639 #endif
640 
641 		return 999;
642 	}
643 
644 	return 0;
645 }
646