death to register
[openafs.git] / src / dir / test / dtest.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #define PAGESIZE 2048
11 #include <afsconfig.h>
12 #include <afs/param.h>
13
14
15 #ifdef AFS_NT40_ENV
16 #include <fcntl.h>
17 #include <windows.h>
18 #else
19 #include <sys/file.h>
20 #endif
21 #include <stdio.h>
22 #include <errno.h>
23
24 long fidCounter = 0;
25
26 typedef struct dirhandle {
27     int fd;
28     int uniq;
29 } dirhandle;
30 int Uniq;
31
32 void
33 Usage(void)
34 {
35     printf("Usage: dtest <command [args]>, where command is one of:\n");
36     printf("-l file - lists directory in file\n");
37     printf("-c file - checks directory in file\n");
38     printf("-s ifile ofile - salvages directory in ifile, places in ofile\n");
39     printf
40         ("-f file name count - creates count names based on \"name\" in directory in file\n");
41     printf("-d file name - delete name from directory in file\n");
42     printf("-r file name - lookup name in directory\n");
43     printf("-a file name - add name to directory in file\n");
44     exit(1);
45 }
46
47 main(argc, argv)
48      char **argv;
49 {
50     DInit(600);
51     argc--;
52     argv++;
53
54     if (argc == 0)
55         Usage();
56
57     switch ((*argv++)[1]) {
58     case 'l':
59         ListDir(*argv);
60         break;
61     case 'c':
62         CheckDir(*argv);
63         break;
64     case 's':
65         SalvageDir(*argv, argv[1]);
66         break;
67     case 'f':
68         CRTest(*argv, argv[1], atoi(argv[2]));
69         break;
70     case 'd':
71         DelTest(*argv, argv[1]);
72         break;
73     case 'r':
74         LookupDir(*argv, argv[1]);
75         break;
76     case 'a':
77         AddEntry(*argv, argv[1]);
78         break;
79     default:
80         Usage();
81     }
82     exit(0);
83 }
84
85 LookupDir(dname, ename)
86      char *dname, *ename;
87 {
88     dirhandle dir;
89     long fid[3], code;
90
91     OpenDir(dname, &dir);
92     code = Lookup(&dir, ename, fid);
93     if (code)
94         printf("lookup code %d\n", code);
95     else {
96         printf("Found fid %d.%d for file '%s'\n", fid[1], fid[2], ename);
97     }
98     DFlush();
99 }
100
101 AddEntry(dname, ename)
102      char *dname, *ename;
103 {
104     dirhandle dir;
105     long fid[3], code;
106
107     fid[1] = fidCounter++;
108     fid[2] = 3;
109     OpenDir(dname, &dir);
110     code = Create(&dir, ename, fid);
111     if (code)
112         printf("create code %d\n", code);
113     DFlush();
114 }
115
116 ListDir(name)
117      char *name;
118 {
119     extern ListEntry();
120     dirhandle dir;
121     OpenDir(name, &dir);
122     EnumerateDir(&dir, ListEntry, 0);
123 }
124
125 ListEntry(handle, name, vnode, unique)
126      long handle, vnode, unique;
127      char *name;
128 {
129     printf("%s\t%d\t%d\n", name, vnode, unique);
130 }
131
132 CheckDir(name)
133      char *name;
134 {
135     dirhandle dir;
136     OpenDir(name, &dir);
137     if (DirOK(&dir))
138         printf("Directory ok.\n");
139     else
140         printf("Directory bad\n");
141 }
142
143 SalvageDir(iname, oname)
144      char *iname, *oname;
145 {
146     dirhandle in, out;
147     long myFid[3], parentFid[3];
148     OpenDir(iname, &in);
149     if (Lookup(in, ".", myFid) || Lookup(in, "..", parentFid)) {
150         printf("Lookup of \".\" and/or \"..\" failed: ");
151         printf("%d %d %d %d\n", myFid[1], myFid[2], parentFid[1],
152                parentFid[2]);
153         printf("Directory cannnot be salvaged\n");
154     }
155     CreateDir(oname, &out);
156     DirSalvage(&in, &out, myFid[1], myFid[2], parentFid[1], parentFid[2]);
157     DFlush();
158 }
159
160 DelTest(dname, ename)
161      char *dname;
162      char *ename;
163 {
164     dirhandle dir;
165     long code;
166
167     OpenDir(dname, &dir);
168     code = Delete(&dir, ename);
169     if (code)
170         printf("delete code is %d\n", code);
171     DFlush();
172 }
173
174 CRTest(dname, ename, count)
175      char *dname;
176      char *ename;
177      int count;
178 {
179     char tbuffer[200];
180     long i, code;
181     long fid[3];
182     dirhandle dir;
183
184     CreateDir(dname, &dir);
185     memset(fid, 0, sizeof(fid));
186     MakeDir(&dir, fid, fid);
187     for (i = 0; i < count; i++) {
188         sprintf(tbuffer, "%s%d", ename, i);
189         fid[1] = fidCounter++;
190         fid[2] = count;
191         code = Create(&dir, tbuffer, &fid);
192         if (i % 100 == 0) {
193             printf("#");
194             fflush(stdout);
195         }
196         if (code) {
197             printf("code for '%s' is %d\n", tbuffer, code);
198             return;
199         }
200     }
201     DFlush();
202 }
203
204 OpenDir(name, dir)
205      char *name;
206      dirhandle *dir;
207 {
208     dir->fd = open(name, O_RDWR, 0666);
209     if (dir->fd == -1) {
210         printf("Couldn't open %s\n", name);
211         exit(1);
212     }
213     dir->uniq = ++Uniq;
214 }
215
216 CreateDir(name, dir)
217      char *name;
218      dirhandle *dir;
219 {
220     dir->fd = open(name, O_CREAT | O_RDWR | O_TRUNC, 0666);
221     dir->uniq = ++Uniq;
222     if (dir->fd == -1) {
223         printf("Couldn't create %s\n", name);
224         exit(1);
225     }
226 }
227
228 ReallyRead(dir, block, data)
229      dirhandle *dir;
230      int block;
231      char *data;
232 {
233     int code;
234     if (lseek(dir->fd, block * PAGESIZE, 0) == -1)
235         return errno;
236     code = read(dir->fd, data, PAGESIZE);
237     if (code < 0)
238         return errno;
239     if (code != PAGESIZE)
240         return EIO;
241     return 0;
242 }
243
244 ReallyWrite(dir, block, data)
245      dirhandle *dir;
246      int block;
247      char *data;
248 {
249     int code;
250     if (lseek(dir->fd, block * PAGESIZE, 0) == -1)
251         return errno;
252     code = write(dir->fd, data, PAGESIZE);
253     if (code < 0)
254         return errno;
255     if (code != PAGESIZE)
256         return EIO;
257     return 0;
258 }
259
260 FidZap(dir)
261      dirhandle *dir;
262 {
263     dir->fd = -1;
264 }
265
266 int
267 FidZero(afid)
268      long *afid;
269 {
270     *afid = 0;
271 }
272
273 FidEq(dir1, dir2)
274      dirhandle *dir1, *dir2;
275 {
276     return (dir1->uniq == dir2->uniq);
277 }
278
279 int
280 FidVolEq(afid, bfid)
281      long *afid, *bfid;
282 {
283     return 1;
284 }
285
286 FidCpy(todir, fromdir)
287      dirhandle *todir, *fromdir;
288 {
289     *todir = *fromdir;
290 }
291
292 Die(msg)
293      char *msg;
294 {
295     printf("Something died with this message:  %s\n", msg);
296 }
297
298 Log(a, b, c, d, e, f, g, h, i, j, k, l, m, n)
299 {
300     printf(a, b, c, d, e, f, g, h, i, j, k, l, m, n);
301 }