|
|
1.1 root 1: /* @(#)subr.c 1.3 90/01/03 NFS Rev 2 Testsuite
2: * 1.6 Lachman ONC Test Suite source
3: *
4: * Useful subroutines shared by all tests
5: */
6:
7: #include <sys/param.h>
8: #ifndef major
9: #include <sys/types.h>
10: #endif
11: #ifdef SVR3
12: #include <sys/sysmacros.h>
13: #include <sys/fs/nfs/time.h>
14: #else
15: #include <sys/time.h>
16: #endif
17: #include <sys/stat.h>
18: #include <stdio.h>
19: #include "tests.h"
20:
21: char *Myname;
22:
23: /*
24: * Build a directory tree "lev" levels deep
25: * with "files" number of files in each directory
26: * and "dirs" fan out. Starts at the current directory.
27: * "fname" and "dname" are the base of the names used for
28: * files and directories.
29: */
30: dirtree(lev, files, dirs, fname, dname, totfiles, totdirs)
31: int lev;
32: int files;
33: int dirs;
34: char *fname;
35: char *dname;
36: int *totfiles;
37: int *totdirs;
38: {
39: int fd;
40: int f, d;
41: char name[MAXPATHLEN];
42:
43: if (lev-- == 0) {
44: return;
45: }
46: for ( f = 0; f < files; f++) {
47: sprintf(name, "%s%d", fname, f);
48: if ((fd = creat(name, 0666)) < 0) {
49: error("creat %s failed", name);
50: exit(1);
51: }
52: (*totfiles)++;
53: if (close(fd) < 0) {
54: error("close %d failed", fd);
55: exit(1);
56: }
57: }
58: for ( d = 0; d < dirs; d++) {
59: sprintf(name, "%s%d", dname, d);
60: if (mkdir(name, 0777) < 0) {
61: error("mkdir %s failed", name);
62: exit(1);
63: }
64: (*totdirs)++;
65: if (chdir(name) < 0) {
66: error("chdir %s failed", name);
67: exit(1);
68: }
69: dirtree(lev, files, dirs, fname, dname, totfiles, totdirs);
70: if (chdir("..") < 0) {
71: error("chdir .. failed");
72: exit(1);
73: }
74: }
75: }
76:
77: /*
78: * Remove a directory tree starting at the current directory.
79: * "fname" and "dname" are the base of the names used for
80: * files and directories to be removed - don't remove anything else!
81: * "files" and "dirs" are used with fname and dname to generate
82: * the file names to remove.
83: *
84: * This routine will fail if, say after removing known files,
85: * the directory is not empty.
86: *
87: * This is used to test the unlink function and to clean up after tests.
88: */
89: rmdirtree(lev, files, dirs, fname, dname, totfiles, totdirs, ignore)
90: int lev;
91: int files;
92: int dirs;
93: char *fname;
94: char *dname;
95: int *totfiles; /* total removed */
96: int *totdirs; /* total removed */
97: int ignore;
98: {
99: int f, d;
100: char name[MAXPATHLEN];
101:
102: if (lev-- == 0) {
103: return;
104: }
105: for ( f = 0; f < files; f++) {
106: sprintf(name, "%s%d", fname, f);
107: if (unlink(name) < 0 && !ignore) {
108: error("unlink %s failed", name);
109: exit(1);
110: }
111: (*totfiles)++;
112: }
113: for ( d = 0; d < dirs; d++) {
114: sprintf(name, "%s%d", dname, d);
115: if (chdir(name) < 0) {
116: if (ignore)
117: continue;
118: error("chdir %s failed", name);
119: exit(1);
120: }
121: rmdirtree(lev, files, dirs, fname, dname, totfiles, totdirs, ignore);
122: if (chdir("..") < 0) {
123: error("chdir .. failed");
124: exit(1);
125: }
126: if (rmdir(name) < 0) {
127: error("rmdir %s failed", name);
128: exit(1);
129: }
130: (*totdirs)++;
131: }
132: }
133:
134: /* VARARGS */
135: error(str, ar1, ar2, ar3, ar4, ar5, ar6, ar7, ar8, ar9)
136: char *str;
137: {
138: #ifdef SVR3
139: char *ret, *getcwd(), path[MAXPATHLEN];
140: #else
141: char *ret, *getwd(), path[MAXPATHLEN];
142: #endif
143:
144: #ifdef SVR3
145: if ((ret = getcwd(path, sizeof(path))) == NULL)
146: fprintf(stderr, "%s: getcwd failed\n", Myname);
147: else
148: fprintf(stderr, "\t%s: (%s) ", Myname, path);
149: #else
150: if ((ret = getwd(path)) == NULL)
151: fprintf(stderr, "%s: getwd failed\n", Myname);
152: else
153: fprintf(stderr, "\t%s: (%s) ", Myname, path);
154: #endif
155:
156: fprintf(stderr, str, ar1, ar2, ar3, ar4, ar5, ar6, ar7, ar8, ar9);
157: if (errno)
158: perror(" ");
159: else
160: fprintf(stderr, "\n");
161: fflush(stderr);
162: if (ret == NULL)
163: exit(1);
164: }
165:
166: static struct timeval ts, te;
167:
168: /*
169: * save current time in struct ts
170: */
171: starttime()
172: {
173:
174: gettimeofday(&ts, (struct timezone *)0);
175: }
176:
177: /*
178: * sets the struct tv to the difference in time between
179: * current time and the time in struct ts.
180: */
181: endtime(tv)
182: struct timeval *tv;
183: {
184:
185: gettimeofday(&te, (struct timezone *)0);
186: if (te.tv_usec < ts.tv_usec) {
187: te.tv_sec--;
188: te.tv_usec += 1000000;
189: }
190: tv->tv_usec = te.tv_usec - ts.tv_usec;
191: tv->tv_sec = te.tv_sec - ts.tv_sec;
192: }
193:
194: /*
195: * Set up and move to a test directory
196: */
197: testdir(dir)
198: char *dir;
199: {
200: struct stat statb;
201: char str[MAXPATHLEN];
202: extern char *getenv();
203:
204: /*
205: * If dir is non-NULL, use that dir. If NULL, first
206: * check for env variable NFSTESTDIR. If that is not
207: * set, use the compiled-in TESTDIR.
208: */
209: if (dir == NULL)
210: if ((dir = getenv("NFSTESTDIR")) == NULL)
211: dir = TESTDIR;
212:
213: if (stat(dir, &statb) == 0) {
214: sprintf(str, "rm -r %s", dir);
215: if (system(str) != 0) {
216: error("can't remove old test directory %s", dir);
217: exit(1);
218: }
219: }
220:
221: if (mkdir(dir, 0777) < 0) {
222: error("can't create test directory %s", dir);
223: exit(1);
224: }
225: if (chdir(dir) < 0) {
226: error("can't chdir to test directory %s", dir);
227: exit(1);
228: }
229: }
230:
231: /*
232: * Move to a test directory
233: */
234: mtestdir(dir)
235: char *dir;
236: {
237: struct stat statb;
238: char str[MAXPATHLEN];
239: char *getenv();
240:
241: /*
242: * If dir is non-NULL, use that dir. If NULL, first
243: * check for env variable NFSTESTDIR. If that is not
244: * set, use the compiled-in TESTDIR.
245: */
246: if (dir == NULL)
247: if ((dir = getenv("NFSTESTDIR")) == NULL)
248: dir = TESTDIR;
249:
250: if (chdir(dir) < 0) {
251: error("can't chdir to test directory %s", dir);
252: return(-1);
253: }
254: return(0);
255: }
256:
257: /*
258: * get parameter at parm, convert to int, and make sure that
259: * it is at least min.
260: */
261: getparm(parm, minimum, label)
262: char *parm, *label;
263: int minimum;
264: {
265: int val;
266:
267: val = atoi(parm);
268: if (val < minimum) {
269: error("Illegal %s parameter %d, must be at least %d",
270: label, val, minimum);
271: exit(1);
272: }
273: return(val);
274: }
275:
276: /*
277: * exit point for successful test
278: */
279: complete()
280: {
281:
282: fprintf(stdout, "\t%s ok.\n", Myname);
283: exit(0);
284: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.