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
|
/*
* @(#)telldir.c 1.5 2003/12/30 Connectathon Testsuite
*/
/*
* Ensure that telldir and seekdir cooperate.
* This is done by creating lots of scratch files, walking through with
* telldir, and using the resulting cookies with seekdir.
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include "../tests.h"
static char *usage = "usage: telldir [-d] [-n nfiles]";
/*
* Information kept for each file.
*/
typedef struct {
int inuse; /* is this entry initialized? */
long cookie; /* cookie to reach the file */
int numfiles; /* number of files after this one */
} file_info_t;
static int debug = 0;
/*
* Number of scratch files to create.
*/
static int numfiles = 200;
static char *tdirname = "telldir-test"; /* scratch directory to hold files */
static file_info_t *file_info;
static void alloc_file_info ARGS_((int));
static void check_file_info ARGS_((DIR *));
static void cleanup ARGS_((void));
static DIR *make_files ARGS_((void));
static void save_file_info ARGS_((int, long, int));
static void verify ARGS_((DIR *, int, long, int));
static void walk_dir ARGS_((DIR *));
/*ARGSUSED*/
int
main(argc, argv)
int argc;
char **argv;
{
DIR *dp;
int c;
while ((c = getopt(argc, argv, "dn:")) != EOF)
switch (c) {
case 'd':
debug = 1;
break;
case 'n':
numfiles = atoi(optarg);
break;
case '?':
default:
fprintf(stderr, "%s\n", usage);
exit(1);
break;
}
alloc_file_info(numfiles);
dp = make_files();
walk_dir(dp);
check_file_info(dp);
cleanup();
exit(0);
}
/*
* Create the test directory and scratch files. Exits if there is a
* problem. Returns a pointer to the opened test directory.
*/
static DIR *
make_files()
{
DIR *dp;
char filename[MAXPATHLEN];
int i;
if (mkdir(tdirname, 0777) < 0) {
if (errno != EEXIST) {
fprintf(stderr, "can't create %s: %s\n",
tdirname, strerror(errno));
exit(1);
}
}
for (i = 0; i < numfiles; i++) {
int fd;
sprintf(filename, "%s/%d", tdirname, i);
fd = creat(filename, 0666);
if (fd < 0) {
fprintf(stderr, "can't create %s: %s\n",
filename, strerror(errno));
exit(1);
}
close(fd);
}
dp = opendir(tdirname);
if (dp == NULL) {
fprintf(stderr, "can't open %s: %s\n",
tdirname, strerror(errno));
exit(1);
}
return (dp);
}
/*
* Walk through the test directory and make sure we find all the expected
* files. For each file, save the telldir information.
*/
static void
walk_dir(dp)
DIR *dp;
{
int files_left;
struct dirent *entry;
long cookie;
files_left = numfiles;
while (files_left > 0) {
int filenum;
char *filename;
cookie = telldir(dp);
if (cookie == -1) {
fprintf(stderr,
"warning: cookie = -1, errno=%d (%s)\n",
errno, strerror(errno));
}
errno = 0;
entry = readdir(dp);
if (entry == NULL) {
fprintf(stderr, "error reading %s: %s\n",
tdirname,
errno != 0 ? strerror(errno) :
"premature EOF");
exit(1);
}
filename = entry->d_name;
if (strcmp(filename, ".") == 0 ||
strcmp(filename, "..") == 0)
continue;
filenum = atoi(entry->d_name);
if (filenum < 0 || filenum >= numfiles) {
fprintf(stderr, "Warning: unexpected filename: %s\n",
filename);
continue;
}
save_file_info(filenum, cookie, files_left);
files_left--;
}
}
/*
* Check that a seekdir to each saved cookie behaves as expected.
* 1. we see the expected number of files starting at that cookie.
* 2. we see the expected first file at that cookie.
*/
static void
check_file_info(dp)
DIR *dp;
{
int file;
for (file = 0; file < numfiles; file++) {
file_info_t *ip;
ip = file_info + file;
if (! ip->inuse) {
fprintf(stderr, "no information for file %d\n", file);
exit(1);
}
seekdir(dp, ip->cookie);
verify(dp, file, ip->cookie, ip->numfiles);
}
}
/*
* The given directory should be positioned so that file "file" is next,
* and there should be "files_left" files left in the directory (including
* "file"). Verify that this is true; complain and exit if it isn't.
*/
static void
verify(dp, file, cookie, files_left)
DIR *dp;
int file;
long cookie;
int files_left;
{
struct dirent *entry;
int first_file = 1;
int files_found;
for (files_found = 0; files_found < files_left; files_found++) {
errno = 0;
entry = readdir(dp);
if (entry == NULL) {
char *errmsg = errno != 0 ?
strerror(errno) : NULL;
fprintf(stderr, "entry for %d (cookie %ld):\n",
file, cookie);
fprintf(stderr,
"expected to find %d entries, only found %d\n",
files_left, files_found);
if (errmsg)
fprintf(stderr, "error: %s\n", errmsg);
exit(1);
}
if (first_file) {
int file_read = atoi(entry->d_name);
if (file_read != file) {
fprintf(stderr,
"expected file %d at cookie %ld, ",
file, cookie);
fprintf(stderr, "found %s\n",
entry->d_name);
exit(1);
}
}
first_file = 0;
}
}
/*
* Remove the test directory and its contents.
*/
static void
cleanup()
{
char command[2 * MAXPATHLEN];
sprintf(command, "rm -rf %s", tdirname);
system(command);
}
/*
* Create and initialize the file info array.
*/
static void
alloc_file_info(numfiles)
int numfiles;
{
file_info = calloc(numfiles, sizeof (file_info_t));
if (file_info == NULL) {
fprintf(stderr, "can't allocate file info array: %s\n",
strerror(errno));
exit(1);
}
/* inuse fields zeroed by calloc */
}
/*
* Save the given information in file_info[filenum].
*/
static void
save_file_info(filenum, cookie, files_left)
int filenum;
long cookie;
int files_left;
{
if (debug) {
printf("%d 0x%lx %d\n", filenum, cookie, files_left);
}
file_info[filenum].inuse = 1;
file_info[filenum].cookie = cookie;
file_info[filenum].numfiles = files_left;
}
|