Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / large-dir.c
1 /*
2 * Copyright (c) 1995, 1996, 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 #include <dirent.h>
46
47 #include <err.h>
48
49 static int
50 creat_files(const char *dirname, int count)
51 {
52 struct stat sb;
53 int i;
54 DIR *d;
55 struct dirent *dp;
56
57 if (mkdir(dirname, 0777) < 0)
58 err(1, "mkdir %s", dirname);
59
60 if (chdir(dirname) < 0)
61 err(1, "chdir %s", dirname);
62 if (stat(".", &sb) < 0)
63 err(1, "stat .");
64 if (sb.st_size != 2048)
65 errx(1, "size != 2048");
66 for (i = 0; i < count; ++i) {
67 char num[17];
68 int fd;
69
70 snprintf(num, sizeof(num), "%d", i);
71
72 fd = open(num, O_CREAT | O_EXCL, 0777);
73 if (fd < 0)
74 err(1, "open %s", num);
75 if (close(fd) < 0)
76 err(1, "close %s", num);
77 }
78 if (stat(".", &sb) < 0)
79 err(1, "stat .");
80
81 d = opendir(".");
82 if (d == NULL)
83 err(1, "opendir .");
84 for (i = -2; i < count; ++i) {
85 char num[17];
86
87 dp = readdir(d);
88 if (dp == NULL)
89 errx(1, "out of entries at %d?", i);
90 if (i == -2)
91 strcpy(num, ".");
92 else if (i == -1)
93 strcpy(num, "..");
94 else
95 snprintf(num, sizeof(num), "%d", i);
96 if (strcmp(num, dp->d_name) != 0)
97 errx(1, "'%s' != '%s'", num, dp->d_name);
98 }
99 if (readdir(d) != NULL)
100 errx(1, "more entries?");
101 closedir(d);
102 for (i = 0; i < count; ++i) {
103 char num[17];
104
105 snprintf(num, sizeof(num), "%d", i);
106
107 if (unlink(num) < 0)
108 err(1, "unlink %s", num);
109 }
110 d = opendir(".");
111 if (d == NULL)
112 err(1, "opendir .");
113 dp = readdir(d);
114 if (dp == NULL || strcmp(dp->d_name, ".") != 0)
115 errx(1, "where's .?");
116 dp = readdir(d);
117 if (dp == NULL || strcmp(dp->d_name, "..") != 0)
118 errx(1, "where's ..?");
119 if (readdir(d) != NULL)
120 errx(1, "even more entries?");
121 closedir(d);
122 if (stat(".", &sb) < 0)
123 err(1, "stat .");
124 #if 0
125 if (sb.st_size != 2048)
126 errx(1, "size != 2048");
127 #endif
128 return 0;
129 }
130
131 static void
132 usage(int ret)
133 {
134 fprintf(stderr, "%s directory number-of-files\n", __progname);
135 exit(ret);
136 }
137
138 int
139 main(int argc, char **argv)
140 {
141 char *ptr;
142 int count;
143
144
145 if (argc != 3)
146 usage(1);
147
148 count = strtol(argv[2], &ptr, 0);
149 if (count == 0 && ptr == argv[2])
150 errx(1, "'%s' not a number", argv[2]);
151
152 return creat_files(argv[1], count);
153 }