1/*-
2 * Copyright (c) 2012 Jilles Tjoelker
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27/*
28 * Limited test program for nftw() as specified by IEEE Std. 1003.1-2008.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/wait.h>
35
36#include <assert.h>
37#include <err.h>
38#include <errno.h>
39#include <fcntl.h>
40#include <ftw.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <spawn.h>
45#include <unistd.h>
46
47extern char **environ;
48
49static char dir[] = "/tmp/testftw.XXXXXXXXXX";
50static int failures;
51static int ftwflags;
52
53static void
54cleanup(int ustatus __unused)
55{
56	int error, status;
57	pid_t pid, waitres;
58	const char *myargs[5];
59
60	err_set_exit(NULL);
61	myargs[0] = "rm";
62	myargs[1] = "-rf";
63	myargs[2] = "--";
64	myargs[3] = dir;
65	myargs[4] = NULL;
66	error = posix_spawnp(&pid, myargs[0], NULL, NULL,
67	    __DECONST(char **, myargs), environ);
68	if (error != 0)
69		warn("posix_spawnp rm");
70	else {
71		waitres = waitpid(pid, &status, 0);
72		if (waitres != pid)
73			warnx("waitpid rm failed");
74		else if (status != 0)
75			warnx("rm failed");
76	}
77}
78
79static int
80cb(const char *path, const struct stat *st, int type, struct FTW *f)
81{
82
83	switch (type) {
84	case FTW_D:
85		if ((ftwflags & FTW_DEPTH) == 0)
86			return (0);
87		break;
88	case FTW_DP:
89		if ((ftwflags & FTW_DEPTH) != 0)
90			return (0);
91		break;
92	case FTW_SL:
93		if ((ftwflags & FTW_PHYS) != 0)
94			return (0);
95		break;
96	}
97	warnx("unexpected path=%s type=%d f.level=%d\n",
98	    path, type, f->level);
99	failures++;
100	return (0);
101}
102
103int
104main(int argc, char *argv[])
105{
106	int fd;
107
108	if (!mkdtemp(dir))
109		err(2, "mkdtemp");
110
111	err_set_exit(cleanup);
112
113	fd = open(dir, O_DIRECTORY | O_RDONLY);
114	if (fd == -1)
115		err(2, "open %s", dir);
116
117	if (mkdirat(fd, "d1", 0777) == -1)
118		err(2, "mkdirat d1");
119
120	if (symlinkat(dir, fd, "d1/looper") == -1)
121		err(2, "symlinkat looper");
122
123	ftwflags = FTW_PHYS;
124	if (nftw(dir, cb, 10, ftwflags) == -1)
125		err(2, "nftw FTW_PHYS");
126	ftwflags = FTW_PHYS | FTW_DEPTH;
127	if (nftw(dir, cb, 10, ftwflags) == -1)
128		err(2, "nftw FTW_PHYS | FTW_DEPTH");
129	ftwflags = 0;
130	if (nftw(dir, cb, 10, ftwflags) == -1)
131		err(2, "nftw 0");
132	ftwflags = FTW_DEPTH;
133	if (nftw(dir, cb, 10, ftwflags) == -1)
134		err(2, "nftw FTW_DEPTH");
135
136	close(fd);
137
138	printf("PASS nftw()\n");
139
140	cleanup(failures != 0);
141
142	return (failures != 0);
143}
144