Fixed warning while generated AutoDoc documentation: NewList() is not in Exec.
[AROS.git] / test / fstest.c
blobfa70fde03cdc1999c7c78ba238359e1d51ef0259
1 #include <proto/dos.h>
2 #include <proto/exec.h>
3 #include <dos/dos.h>
4 #include <dos/exall.h>
5 #include <exec/memory.h>
6 #include <stdio.h>
7 #include <aros/debug.h>
9 #define REAL_DELETE 1
11 int deleteDirContents(char *startpath, int depth, int dnum, int fnum) {
12 struct ExAllControl *eac;
13 struct TagItem ti={TAG_DONE};
14 struct ExAllData *ead;
15 struct ExAllData *next;
16 BPTR lock;
17 struct FileInfoBlock fib;
18 char dpath[512];
19 int size;
21 if (dnum<1)
22 dnum = 1;
23 size=(dnum+(fnum*32))*(sizeof(struct ExAllData)+32);
24 eac = AllocDosObject(DOS_EXALLCONTROL, &ti);
25 if (eac == NULL)
27 printf("\nFailed to allocated dos object!\n");
28 return 1;
30 eac->eac_LastKey = 0;
31 ead = AllocVec(size, MEMF_ANY | MEMF_CLEAR);
32 if (ead == NULL)
34 FreeDosObject(DOS_EXALLCONTROL, eac);
35 printf("\nFailed to allocated memory!\n");
36 return 1;
38 lock = Lock(startpath, SHARED_LOCK);
39 if (lock == BNULL)
41 FreeVec(ead);
42 FreeDosObject(DOS_EXALLCONTROL, eac);
43 printf("\nFailed to lock %s!\n", startpath);
44 printf("I/O Error is %ld\n", (long)IoErr());
45 PrintFault(IoErr(), NULL);
46 return 1;
48 Examine(lock, &fib);
49 if (fib.fib_DirEntryType != ST_USERDIR)
51 UnLock(lock);
52 FreeVec(ead);
53 FreeDosObject(DOS_EXALLCONTROL, eac);
54 printf("\nEntry %s is not directory!\n", startpath);
55 printf("I/O Error is %ld\n", (long)IoErr());
56 PrintFault(IoErr(), NULL);
57 return 1;
59 if (ExAll(lock, ead, size, ED_TYPE, eac) != 0)
61 kprintf("entries = %ld\n", eac->eac_Entries);
62 ExAllEnd(lock, ead, size, ED_TYPE, eac);
63 UnLock(lock);
64 FreeVec(ead);
65 FreeDosObject(DOS_EXALLCONTROL, eac);
66 printf("\nNot enough memory for %s when doing ExamineAll()!\n", startpath);
67 printf("I/O Error is %ld\n", (long)IoErr());
68 PrintFault(IoErr(), NULL);
69 return 1;
71 UnLock(lock);
72 if (IoErr() != ERROR_NO_MORE_ENTRIES)
74 FreeDosObject(DOS_EXALLCONTROL, eac);
75 FreeVec(ead);
76 printf("\nExAll() returned error on %s!\n", startpath);
77 printf("I/O Error is %ld\n", (long)IoErr());
78 PrintFault(IoErr(), NULL);
79 return 1;
81 if (eac->eac_Entries == 0)
82 next = 0;
83 else
84 next = ead;
85 while (next != NULL)
87 AddPart(dpath, startpath, 512);
88 AddPart(dpath, next->ed_Name, 512);
89 if (next->ed_Type == ST_FILE)
91 #if REAL_DELETE
92 if (DeleteFile(dpath) == 0)
94 FreeDosObject(DOS_EXALLCONTROL, eac);
95 FreeVec(ead);
96 printf("\nFailed to delete file %s\n", dpath);
97 printf("I/O Error is %ld\n", (long)IoErr());
98 PrintFault(IoErr(), NULL);
99 return 1;
101 #endif
103 else if (next->ed_Type == ST_USERDIR)
105 if (deleteDirContents(dpath, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1) != 0)
107 FreeDosObject(DOS_EXALLCONTROL, eac);
108 FreeVec(ead);
109 return 1;
112 else
114 FreeDosObject(DOS_EXALLCONTROL, eac);
115 FreeVec(ead);
116 printf("\nFailed to identify %s - it is no directory or file!\n", dpath);
117 return 1;
119 next = next->ed_Next;
121 FreeDosObject(DOS_EXALLCONTROL, eac);
122 FreeVec(ead);
123 #if REAL_DELETE
124 if (DeleteFile(startpath) == 0)
126 printf("\nFailed to delete directory %s\n", startpath);
127 printf("I/O Error is %ld\n", (long)IoErr());
128 PrintFault(IoErr(), NULL);
129 return 1;
131 #endif
132 return 0;
135 int deleteAll(char *startpath, int depth, int dnum, int fnum) {
136 int i;
137 char name[32];
138 char path[512];
140 for (i=0; i<dnum; i++)
142 sprintf(name, "d-%03d-%03d", depth, i);
143 AddPart(path, startpath, 512);
144 AddPart(path, name, 512);
145 if (deleteDirContents(path, depth, dnum, fnum) != 0)
146 return 1;
148 return 0;
151 int specificParentCheck(BPTR lock, BPTR dlock, char *path) {
152 BPTR plock;
154 plock = ParentDir(dlock);
155 if (plock == BNULL)
157 printf("\nFailed to get parent of %s!\n", path);
158 printf("I/O Error is %ld\n", (long)IoErr());
159 PrintFault(IoErr(), NULL);
160 return 1;
162 if (!SameLock(lock, plock))
164 UnLock(plock);
165 printf("\nParent of %s is not correct!\n", path);
166 return 1;
168 UnLock(plock);
169 return 0;
172 int checkParent(char *startpath, int depth, int dnum, int fnum, int size) {
173 BPTR lock;
174 BPTR dlock;
175 BPTR flock;
176 char name[32];
177 char path[512];
178 char fpath[512];
179 int i,j;
181 if (dnum<1)
182 dnum = 1;
183 lock = Lock(startpath, SHARED_LOCK);
184 if (lock == BNULL)
186 printf("\nFailed to get lock on %s!\n", startpath);
187 printf("I/O Error is %ld\n", (long)IoErr());
188 PrintFault(IoErr(), NULL);
189 return 1;
191 for (i=0; i<dnum; i++)
193 sprintf(name, "d-%03d-%03d", depth, i);
194 AddPart(path, startpath, 512);
195 AddPart(path, name, 512);
196 dlock = Lock(path, SHARED_LOCK);
197 if (dlock == BNULL)
199 UnLock(lock);
200 printf("\nFailed to get lock on %s!\n", path);
201 printf("I/O Error is %ld\n", (long)IoErr());
202 PrintFault(IoErr(), NULL);
203 return 1;
205 if (specificParentCheck(lock, dlock, path) != 0)
207 UnLock(lock);
208 UnLock(dlock);
209 return 1;
211 for (j=0; j<fnum; j++)
213 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
214 AddPart(fpath, path, 512);
215 AddPart(fpath, name, 512);
216 flock = Lock(fpath, SHARED_LOCK);
217 if (flock == BNULL)
219 UnLock(lock);
220 UnLock(dlock);
221 printf("\nFailed to get lock on %s!\n", fpath);
222 printf("I/O Error is %ld\n", (long)IoErr());
223 PrintFault(IoErr(), NULL);
224 return 1;
226 if (specificParentCheck(dlock, flock, fpath) != 0)
228 UnLock(lock);
229 UnLock(flock);
230 UnLock(dlock);
231 return 1;
233 UnLock(flock);
235 if (depth>0)
236 if (checkParent(path, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1, size) != 0)
237 return 1;
239 UnLock(lock);
240 return 0;
243 int checkFile(char *path, int depth, int dnum, int fnum, int size) {
244 BPTR fh;
245 unsigned int buffer[512];
246 int i,j;
248 fh = Open(path, MODE_OLDFILE);
249 if (fh == BNULL)
251 printf("\nFailed to open file %s!\n", path);
252 printf("I/O Error is %ld\n", (long)IoErr());
253 PrintFault(IoErr(), NULL);
254 return 1;
256 for (i=0;i<(size/512);i++)
258 if (Read(fh, buffer, 512) != 512)
260 printf("\nFailed to read from file %s\n", path);
261 printf("I/O Error is %ld\n", (long)IoErr());
262 PrintFault(IoErr(), NULL);
263 return 1;
265 for (j=0;j<(512>>4); j+=4)
267 if (
268 (buffer[j+0] != depth) ||
269 (buffer[j+1] != dnum) ||
270 (buffer[j+2] != fnum) ||
271 (buffer[j+3] != size)
274 printf("\nFailed to verify file %s at offset %d\n", path, j*4);
275 printf("Expected: %08x %08x %0x %08x\n", depth, dnum, fnum, size);
276 printf("Got : %08x %08x %0x %08x\n", buffer[j+0], buffer[j+1], buffer[j+2], buffer[j+3]);
277 return 1;
281 Close(fh);
282 return 0;
285 int checkFiles(char *startpath, int depth, int dnum, int fnum, int size) {
286 int i,j;
287 char name[32];
288 char path[512];
289 char fpath[512];
291 if (dnum<1)
292 dnum = 1;
293 for (i=0; i<dnum; i++)
295 sprintf(name, "d-%03d-%03d", depth, i);
296 AddPart(path, startpath, 512);
297 AddPart(path, name, 512);
298 for (j=0; j<fnum; j++)
300 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
301 AddPart(fpath, path, 512);
302 AddPart(fpath, name, 512);
303 if (checkFile(fpath, depth, dnum, fnum, size) != 0)
304 return 1;
306 if (depth>0)
307 if (checkFiles(path, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1, size) != 0)
308 return 1;
310 return 0;
313 int writeFile(char *path, int size, int depth, int dnum, int fnum) {
314 BPTR fh;
315 unsigned int buffer[512];
316 int i;
318 fh = Open(path, MODE_NEWFILE);
319 if (fh == BNULL)
321 printf("\nFailed to create file %s!\n", path);
322 printf("I/O Error is %ld\n", (long)IoErr());
323 PrintFault(IoErr(), NULL);
324 return 1;
326 for (i=0;i<(512>>4); i+=4)
328 buffer[i+0] = depth;
329 buffer[i+1] = dnum;
330 buffer[i+2] = fnum;
331 buffer[i+3] = size;
333 for (i=0;i<(size/512);i++)
335 if (Write(fh, buffer, 512) != 512)
337 Close(fh);
338 printf("Failed to write to file %s\n", path);
339 printf("I/O Error is %ld\n", (long)IoErr());
340 PrintFault(IoErr(), NULL);
341 return 1;
344 Close(fh);
345 // printf("Verifying ...");
346 if (checkFile(path, size, depth, dnum, fnum) != 0)
347 return 1;
348 // printf("done\n");
349 return 0;
352 int createFiles(char *startpath, int depth, int dnum, int fnum, int size) {
353 int i,j;
354 char name[32];
355 char path[512];
356 char fpath[512];
358 if (dnum<1)
359 dnum = 1;
360 for (i=0; i<dnum; i++)
362 sprintf(name, "d-%03d-%03d", depth, i);
363 AddPart(path, startpath, 512);
364 AddPart(path, name, 512);
365 for (j=0; j<fnum; j++)
367 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
368 AddPart(fpath, path, 512);
369 AddPart(fpath, name, 512);
370 if (writeFile(fpath, size, depth, dnum, fnum) != 0)
371 return 1;
373 if (depth>0)
374 if (createFiles(path, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1, size) != 0)
375 return 1;
377 return 0;
380 int checkDirs(char *startpath, int depth, int num) {
381 BPTR dir;
382 int i;
383 char name[32];
384 char path[512];
386 if (num<1)
387 num = 1;
388 for (i=0; i<num; i++)
390 sprintf(name, "d-%03d-%03d", depth, i);
391 AddPart(path, startpath, 512);
392 AddPart(path, name, 512);
393 dir = Lock(path, SHARED_LOCK);
394 if (dir == BNULL)
396 printf("\nFailed locking %s!\n",path);
397 printf("I/O Error is %ld\n", (long)IoErr());
398 PrintFault(IoErr(), NULL);
399 return 1;
401 UnLock(dir);
402 if (depth>0)
403 if (checkDirs(path, depth-1, num-1) != 0)
404 return 1;
406 return 0;
409 int createDirs(char *startpath, int depth, int num) {
410 BPTR dir;
411 int i;
412 char name[32];
413 char path[512];
415 if (num<1)
416 num = 1;
417 for (i=0; i<num; i++)
419 sprintf(name, "d-%03d-%03d", depth, i);
420 AddPart(path, startpath, 512);
421 AddPart(path, name, 512);
422 dir = CreateDir(path);
423 if (dir == BNULL)
425 printf("\nFailed to create %s!\n", path);
426 printf("I/O Error is %ld\n", (long)IoErr());
427 PrintFault(IoErr(), NULL);
428 return 1;
430 UnLock(dir);
431 if (depth>0)
432 if (createDirs(path, depth-1, num-1) != 0)
433 return 1;
435 return 0;
438 int verifyFiles(char *startpath, int depth, int dnum, int fnum, int size) {
439 printf("Verifying %d files per depth with size of %d bytes ...", fnum, size);
440 if (checkFiles(startpath, depth, dnum, fnum, size) != 0)
441 return 1;
442 printf("done\n");
443 return 0;
446 int getDiskInfo(char *device, struct InfoData *id) {
447 BPTR lock;
449 lock = Lock(device, SHARED_LOCK);
450 if (lock == BNULL)
452 printf("Failed to get lock on %s!\n", device);
453 return 1;
455 Info(lock, id);
456 UnLock(lock);
457 return 0;
460 int fileTest(char *startpath, int depth, int dnum, int fnum, int isize) {
461 int size=isize*1024;
462 int csize;
463 int cfnum;
464 int i;
466 for (i=0;i<5;i++)
468 printf("Creating %d files per depth with size of %d bytes ...", fnum, size);
469 if (createFiles(startpath, depth, dnum, fnum, size) != 0)
470 return 1;
471 printf("done\n");
472 csize = size;
473 cfnum = fnum;
474 while (csize>=1024)
476 if (verifyFiles(startpath, depth, dnum, cfnum, csize) != 0)
477 return 1;
478 csize = csize>>3;
479 cfnum = cfnum<<1;
481 size = size<<3;
482 fnum = fnum>>1;
484 return 0;
487 #define TEST 1
488 #define TEST_PARENT 0
490 int main(int argc, char *argv[]) {
491 int isize=1; /* initial size in 1024 byte */
492 int depth=10; /* directory depth */
493 int dnum=6; /* number of directories per depth*/
494 int fnum=16; /* number of files per depth (the bigger the files the lesser are created) */
495 struct InfoData sid;
496 struct InfoData mid;
497 struct InfoData eid;
498 int nomid=0;
500 if (argc<2)
502 printf("Usage: %s <device>\n", argv[0]);
503 return 1;
505 #if TEST
506 if (getDiskInfo(argv[1], &sid) != 0)
507 return 1;
508 printf("Directory test\n");
509 printf("==============\n");
510 printf("Creating directories ...");
511 if (createDirs(argv[1], depth, dnum) != 0)
512 return 1;
513 printf("done\n");
514 printf("Checking directories ...");
515 if (checkDirs(argv[1], depth, dnum) != 0)
516 return 1;
517 printf("done\n");
518 printf("\n\n");
519 printf("File test\n");
520 printf("=========\n");
521 if (fileTest(argv[1], depth, dnum, fnum, isize) != 0)
522 return 1;
523 #if TEST_PARENT
524 printf("Doing a parent check ...");
525 if (checkParent(argv[1], depth, dnum, fnum,) != 0)
526 return 1;
527 printf("done\n");
528 #endif
529 if (getDiskInfo(argv[1], &mid) != 0)
530 nomid=1;
531 #endif
532 printf("Deleting files and directories created by this test ...");
533 if (deleteAll(argv[1], depth, dnum, fnum) != 0)
534 return 1;
535 printf("done\n");
536 #if TEST
537 printf("Used blocks before test: %ld\n", (long)sid.id_NumBlocksUsed);
538 if (nomid == 0)
539 printf("Used blocks using test: %ld\n", (long)mid.id_NumBlocksUsed);
540 if (getDiskInfo(argv[1], &eid) == 0)
541 printf("Used blocks after test: %ld\n", (long)eid.id_NumBlocksUsed);
542 #endif
543 return 0;