Humble script for creating the port's snapshots.
[AROS.git] / test / fstest.c
blobe198c40252bfd047a01681c00ba675816197ba2e
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <proto/dos.h>
7 #include <proto/exec.h>
8 #include <dos/dos.h>
9 #include <dos/exall.h>
10 #include <exec/memory.h>
11 #include <stdio.h>
12 #include <aros/debug.h>
14 #define REAL_DELETE 1
16 int deleteDirContents(char *startpath, int depth, int dnum, int fnum) {
17 struct ExAllControl *eac;
18 struct TagItem ti={TAG_DONE};
19 struct ExAllData *ead;
20 struct ExAllData *next;
21 BPTR lock;
22 struct FileInfoBlock fib;
23 char dpath[512];
24 int size;
25 LONG error;
27 if (dnum<1)
28 dnum = 1;
29 size=(dnum+(fnum*32))*(sizeof(struct ExAllData)+32);
30 eac = AllocDosObject(DOS_EXALLCONTROL, &ti);
31 if (eac == NULL)
33 printf("\nFailed to allocated dos object!\n");
34 return 1;
36 eac->eac_LastKey = 0;
37 ead = AllocVec(size, MEMF_ANY | MEMF_CLEAR);
38 if (ead == NULL)
40 FreeDosObject(DOS_EXALLCONTROL, eac);
41 printf("\nFailed to allocated memory!\n");
42 return 1;
44 lock = Lock(startpath, SHARED_LOCK);
45 if (lock == BNULL)
47 error = IoErr();
48 FreeVec(ead);
49 FreeDosObject(DOS_EXALLCONTROL, eac);
50 printf("\nFailed to lock %s!\n", startpath);
51 printf("I/O Error is %ld\n", (long)error);
52 PrintFault(error, NULL);
53 return 1;
55 Examine(lock, &fib);
56 if (fib.fib_DirEntryType != ST_USERDIR)
58 error = IoErr();
59 UnLock(lock);
60 FreeVec(ead);
61 FreeDosObject(DOS_EXALLCONTROL, eac);
62 printf("\nEntry %s is not directory!\n", startpath);
63 printf("I/O Error is %ld\n", (long)error);
64 PrintFault(error, NULL);
65 return 1;
67 if (ExAll(lock, ead, size, ED_TYPE, eac) != 0)
69 error = IoErr();
70 kprintf("entries = %ld\n", eac->eac_Entries);
71 ExAllEnd(lock, ead, size, ED_TYPE, eac);
72 UnLock(lock);
73 FreeVec(ead);
74 FreeDosObject(DOS_EXALLCONTROL, eac);
75 printf("\nNot enough memory for %s when doing ExamineAll()!\n", startpath);
76 printf("I/O Error is %ld\n", (long)error);
77 PrintFault(error, NULL);
78 return 1;
80 error = IoErr();
81 UnLock(lock);
82 if (error == ERROR_NO_MORE_ENTRIES)
83 error = 0;
84 else
86 FreeDosObject(DOS_EXALLCONTROL, eac);
87 FreeVec(ead);
88 printf("\nExAll() returned error on %s!\n", startpath);
89 printf("I/O Error is %ld\n", (long)error);
90 PrintFault(error, NULL);
91 return 1;
93 if (eac->eac_Entries == 0)
94 next = 0;
95 else
96 next = ead;
97 while (next != NULL)
99 AddPart(dpath, startpath, 512);
100 AddPart(dpath, next->ed_Name, 512);
101 if (next->ed_Type == ST_FILE)
103 #if REAL_DELETE
104 if (!DeleteFile(dpath))
106 error = IoErr();
107 FreeDosObject(DOS_EXALLCONTROL, eac);
108 FreeVec(ead);
109 printf("\nFailed to delete file %s\n", dpath);
110 printf("I/O Error is %ld\n", (long)error);
111 PrintFault(error, NULL);
112 return 1;
114 #endif
116 else if (next->ed_Type == ST_USERDIR)
118 if (deleteDirContents(dpath, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1) != 0)
120 FreeDosObject(DOS_EXALLCONTROL, eac);
121 FreeVec(ead);
122 return 1;
125 else
127 FreeDosObject(DOS_EXALLCONTROL, eac);
128 FreeVec(ead);
129 printf("\nFailed to identify %s - it is no directory or file!\n", dpath);
130 return 1;
132 next = next->ed_Next;
134 FreeDosObject(DOS_EXALLCONTROL, eac);
135 FreeVec(ead);
136 #if REAL_DELETE
137 if (!DeleteFile(startpath))
139 error = IoErr();
140 printf("\nFailed to delete directory %s\n", startpath);
141 printf("I/O Error is %ld\n", (long)error);
142 PrintFault(error, NULL);
143 return 1;
145 #endif
146 return 0;
149 int deleteAll(char *startpath, int depth, int dnum, int fnum) {
150 int i;
151 char name[32];
152 char path[512];
154 for (i=0; i<dnum; i++)
156 sprintf(name, "d-%03d-%03d", depth, i);
157 AddPart(path, startpath, 512);
158 AddPart(path, name, 512);
159 if (deleteDirContents(path, depth, dnum, fnum) != 0)
160 return 1;
162 return 0;
165 int specificParentCheck(BPTR lock, BPTR dlock, char *path) {
166 BPTR plock;
167 LONG error;
169 plock = ParentDir(dlock);
170 if (plock == BNULL)
172 error = IoErr();
173 printf("\nFailed to get parent of %s!\n", path);
174 printf("I/O Error is %ld\n", (long)error);
175 PrintFault(error, NULL);
176 return 1;
178 if (!SameLock(lock, plock))
180 UnLock(plock);
181 printf("\nParent of %s is not correct!\n", path);
182 return 1;
184 UnLock(plock);
185 return 0;
188 int checkParent(char *startpath, int depth, int dnum, int fnum, int size) {
189 BPTR lock;
190 BPTR dlock;
191 BPTR flock;
192 char name[32];
193 char path[512];
194 char fpath[512];
195 int i,j;
196 LONG error;
198 if (dnum<1)
199 dnum = 1;
200 lock = Lock(startpath, SHARED_LOCK);
201 if (lock == BNULL)
203 error = IoErr();
204 printf("\nFailed to get lock on %s!\n", startpath);
205 printf("I/O Error is %ld\n", (long)error);
206 PrintFault(error, NULL);
207 return 1;
209 for (i=0; i<dnum; i++)
211 sprintf(name, "d-%03d-%03d", depth, i);
212 AddPart(path, startpath, 512);
213 AddPart(path, name, 512);
214 dlock = Lock(path, SHARED_LOCK);
215 if (dlock == BNULL)
217 error = IoErr();
218 UnLock(lock);
219 printf("\nFailed to get lock on %s!\n", path);
220 printf("I/O Error is %ld\n", (long)error);
221 PrintFault(error, NULL);
222 return 1;
224 if (specificParentCheck(lock, dlock, path) != 0)
226 UnLock(lock);
227 UnLock(dlock);
228 return 1;
230 for (j=0; j<fnum; j++)
232 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
233 AddPart(fpath, path, 512);
234 AddPart(fpath, name, 512);
235 flock = Lock(fpath, SHARED_LOCK);
236 if (flock == BNULL)
238 error = IoErr();
239 UnLock(lock);
240 UnLock(dlock);
241 printf("\nFailed to get lock on %s!\n", fpath);
242 printf("I/O Error is %ld\n", (long)error);
243 PrintFault(error, NULL);
244 return 1;
246 if (specificParentCheck(dlock, flock, fpath) != 0)
248 UnLock(lock);
249 UnLock(flock);
250 UnLock(dlock);
251 return 1;
253 UnLock(flock);
255 if (depth>0)
256 if (checkParent(path, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1, size) != 0)
257 return 1;
259 UnLock(lock);
260 return 0;
263 int checkFile(char *path, int depth, int dnum, int fnum, int size) {
264 BPTR fh;
265 unsigned int buffer[512];
266 int i,j;
267 LONG error;
269 fh = Open(path, MODE_OLDFILE);
270 if (fh == BNULL)
272 error = IoErr();
273 printf("\nFailed to open file %s!\n", path);
274 printf("I/O Error is %ld\n", (long)error);
275 PrintFault(error, NULL);
276 return 1;
278 for (i=0;i<(size/512);i++)
280 if (Read(fh, buffer, 512) != 512)
282 error = IoErr();
283 printf("\nFailed to read from file %s\n", path);
284 printf("I/O Error is %ld\n", (long)error);
285 PrintFault(error, NULL);
286 return 1;
288 for (j=0;j<(512>>4); j+=4)
290 if (
291 (buffer[j+0] != depth) ||
292 (buffer[j+1] != dnum) ||
293 (buffer[j+2] != fnum) ||
294 (buffer[j+3] != size)
297 printf("\nFailed to verify file %s at offset %d\n", path, j*4);
298 printf("Expected: %08x %08x %0x %08x\n", depth, dnum, fnum, size);
299 printf("Got : %08x %08x %0x %08x\n", buffer[j+0], buffer[j+1], buffer[j+2], buffer[j+3]);
300 return 1;
304 Close(fh);
305 return 0;
308 int checkFiles(char *startpath, int depth, int dnum, int fnum, int size) {
309 int i,j;
310 char name[32];
311 char path[512];
312 char fpath[512];
314 if (dnum<1)
315 dnum = 1;
316 for (i=0; i<dnum; i++)
318 sprintf(name, "d-%03d-%03d", depth, i);
319 AddPart(path, startpath, 512);
320 AddPart(path, name, 512);
321 for (j=0; j<fnum; j++)
323 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
324 AddPart(fpath, path, 512);
325 AddPart(fpath, name, 512);
326 if (checkFile(fpath, depth, dnum, fnum, size) != 0)
327 return 1;
329 if (depth>0)
330 if (checkFiles(path, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1, size) != 0)
331 return 1;
333 return 0;
336 int writeFile(char *path, int size, int depth, int dnum, int fnum) {
337 BPTR fh;
338 unsigned int buffer[512];
339 int i;
340 LONG error;
342 fh = Open(path, MODE_NEWFILE);
343 if (fh == BNULL)
345 error = IoErr();
346 printf("\nFailed to create file %s!\n", path);
347 printf("I/O Error is %ld\n", (long)error);
348 PrintFault(error, NULL);
349 return 1;
351 for (i=0;i<(512>>4); i+=4)
353 buffer[i+0] = depth;
354 buffer[i+1] = dnum;
355 buffer[i+2] = fnum;
356 buffer[i+3] = size;
358 for (i=0;i<(size/512);i++)
360 if (Write(fh, buffer, 512) != 512)
362 error = IoErr();
363 Close(fh);
364 printf("Failed to write to file %s\n", path);
365 printf("I/O Error is %ld\n", (long)error);
366 PrintFault(error, NULL);
367 return 1;
370 Close(fh);
371 // printf("Verifying ...");
372 if (checkFile(path, size, depth, dnum, fnum) != 0)
373 return 1;
374 // printf("done\n");
375 return 0;
378 int createFiles(char *startpath, int depth, int dnum, int fnum, int size) {
379 int i,j;
380 char name[32];
381 char path[512];
382 char fpath[512];
384 if (dnum<1)
385 dnum = 1;
386 for (i=0; i<dnum; i++)
388 sprintf(name, "d-%03d-%03d", depth, i);
389 AddPart(path, startpath, 512);
390 AddPart(path, name, 512);
391 for (j=0; j<fnum; j++)
393 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
394 AddPart(fpath, path, 512);
395 AddPart(fpath, name, 512);
396 if (writeFile(fpath, size, depth, dnum, fnum) != 0)
397 return 1;
399 if (depth>0)
400 if (createFiles(path, depth-1, dnum-1, fnum == 0 ? fnum : fnum-1, size) != 0)
401 return 1;
403 return 0;
406 int checkDirs(char *startpath, int depth, int num) {
407 BPTR dir;
408 int i;
409 char name[32];
410 char path[512];
411 LONG error;
413 if (num<1)
414 num = 1;
415 for (i=0; i<num; i++)
417 sprintf(name, "d-%03d-%03d", depth, i);
418 AddPart(path, startpath, 512);
419 AddPart(path, name, 512);
420 dir = Lock(path, SHARED_LOCK);
421 if (dir == BNULL)
423 error = IoErr();
424 printf("\nFailed locking %s!\n",path);
425 printf("I/O Error is %ld\n", (long)error);
426 PrintFault(error, NULL);
427 return 1;
429 UnLock(dir);
430 if (depth>0)
431 if (checkDirs(path, depth-1, num-1) != 0)
432 return 1;
434 return 0;
437 int createDirs(char *startpath, int depth, int num) {
438 BPTR dir;
439 int i;
440 char name[32];
441 char path[512];
442 LONG error;
444 if (num<1)
445 num = 1;
446 for (i=0; i<num; i++)
448 sprintf(name, "d-%03d-%03d", depth, i);
449 AddPart(path, startpath, 512);
450 AddPart(path, name, 512);
451 dir = CreateDir(path);
452 if (dir == BNULL)
454 error = IoErr();
455 printf("\nFailed to create %s!\n", path);
456 printf("I/O Error is %ld\n", (long)error);
457 PrintFault(error, NULL);
458 return 1;
460 UnLock(dir);
461 if (depth>0)
462 if (createDirs(path, depth-1, num-1) != 0)
463 return 1;
465 return 0;
468 int verifyFiles(char *startpath, int depth, int dnum, int fnum, int size) {
469 printf("Verifying %d files per depth with size of %d bytes ...", fnum, size);
470 if (checkFiles(startpath, depth, dnum, fnum, size) != 0)
471 return 1;
472 printf("done\n");
473 return 0;
476 int getDiskInfo(char *device, struct InfoData *id) {
477 BPTR lock;
479 lock = Lock(device, SHARED_LOCK);
480 if (lock == BNULL)
482 printf("Failed to get lock on %s!\n", device);
483 return 1;
485 Info(lock, id);
486 UnLock(lock);
487 return 0;
490 int fileTest(char *startpath, int depth, int dnum, int fnum, int isize) {
491 int size=isize*1024;
492 int csize;
493 int cfnum;
494 int i;
496 for (i=0;i<5;i++)
498 printf("Creating %d files per depth with size of %d bytes ...", fnum, size);
499 if (createFiles(startpath, depth, dnum, fnum, size) != 0)
500 return 1;
501 printf("done\n");
502 csize = size;
503 cfnum = fnum;
504 while (csize>=1024)
506 if (verifyFiles(startpath, depth, dnum, cfnum, csize) != 0)
507 return 1;
508 csize = csize>>3;
509 cfnum = cfnum<<1;
511 size = size<<3;
512 fnum = fnum>>1;
514 return 0;
517 #define TEST 1
518 #define TEST_PARENT 0
520 int main(int argc, char *argv[]) {
521 int isize=1; /* initial size in 1024 byte */
522 int depth=10; /* directory depth */
523 int dnum=6; /* number of directories per depth*/
524 int fnum=16; /* number of files per depth (the bigger the files the lesser are created) */
525 struct InfoData sid;
526 struct InfoData mid;
527 struct InfoData eid;
528 int nomid=0;
530 if (argc<2)
532 printf("Usage: %s <device>\n", argv[0]);
533 return 1;
535 #if TEST
536 if (getDiskInfo(argv[1], &sid) != 0)
537 return 1;
538 printf("Directory test\n");
539 printf("==============\n");
540 printf("Creating directories ...");
541 if (createDirs(argv[1], depth, dnum) != 0)
542 return 1;
543 printf("done\n");
544 printf("Checking directories ...");
545 if (checkDirs(argv[1], depth, dnum) != 0)
546 return 1;
547 printf("done\n");
548 printf("\n\n");
549 printf("File test\n");
550 printf("=========\n");
551 if (fileTest(argv[1], depth, dnum, fnum, isize) != 0)
552 return 1;
553 #if TEST_PARENT
554 printf("Doing a parent check ...");
555 if (checkParent(argv[1], depth, dnum, fnum,) != 0)
556 return 1;
557 printf("done\n");
558 #endif
559 if (getDiskInfo(argv[1], &mid) != 0)
560 nomid=1;
561 #endif
562 printf("Deleting files and directories created by this test ...");
563 if (deleteAll(argv[1], depth, dnum, fnum) != 0)
564 return 1;
565 printf("done\n");
566 #if TEST
567 printf("Used blocks before test: %ld\n", (long)sid.id_NumBlocksUsed);
568 if (nomid == 0)
569 printf("Used blocks using test: %ld\n", (long)mid.id_NumBlocksUsed);
570 if (getDiskInfo(argv[1], &eid) == 0)
571 printf("Used blocks after test: %ld\n", (long)eid.id_NumBlocksUsed);
572 #endif
573 return 0;