fix colour used for gradient mode.
[AROS.git] / test / dos / fstest.c
blobab540f2b00e5f3845841f444ce67e45d5ead39af
1 /*
2 Copyright © 1995-2016, 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)
18 struct ExAllControl *eac;
19 struct TagItem ti = { TAG_DONE };
20 struct ExAllData *ead;
21 struct ExAllData *next;
22 BPTR lock;
23 struct FileInfoBlock fib;
24 char dpath[512];
25 int size;
26 LONG error;
28 if (dnum < 1)
29 dnum = 1;
30 size = (dnum + (fnum * 32)) * (sizeof(struct ExAllData) + 32);
31 eac = AllocDosObject(DOS_EXALLCONTROL, &ti);
32 if (eac == NULL)
34 printf("\nFailed to allocated dos object!\n");
35 return 1;
37 eac->eac_LastKey = 0;
38 ead = AllocVec(size, MEMF_ANY | MEMF_CLEAR);
39 if (ead == NULL)
41 FreeDosObject(DOS_EXALLCONTROL, eac);
42 printf("\nFailed to allocated memory!\n");
43 return 1;
45 lock = Lock(startpath, SHARED_LOCK);
46 if (lock == BNULL)
48 error = IoErr();
49 FreeVec(ead);
50 FreeDosObject(DOS_EXALLCONTROL, eac);
51 printf("\nFailed to lock %s!\n", startpath);
52 printf("I/O Error is %ld\n", (long)error);
53 PrintFault(error, NULL);
54 return 1;
56 Examine(lock, &fib);
57 if (fib.fib_DirEntryType != ST_USERDIR)
59 error = IoErr();
60 UnLock(lock);
61 FreeVec(ead);
62 FreeDosObject(DOS_EXALLCONTROL, eac);
63 printf("\nEntry %s is not directory!\n", startpath);
64 printf("I/O Error is %ld\n", (long)error);
65 PrintFault(error, NULL);
66 return 1;
68 if (ExAll(lock, ead, size, ED_TYPE, eac))
70 error = IoErr();
71 kprintf("entries = %ld\n", eac->eac_Entries);
72 ExAllEnd(lock, ead, size, ED_TYPE, eac);
73 UnLock(lock);
74 FreeVec(ead);
75 FreeDosObject(DOS_EXALLCONTROL, eac);
76 printf("\nNot enough memory for %s when doing ExamineAll()!\n",
77 startpath);
78 printf("I/O Error is %ld\n", (long)error);
79 PrintFault(error, NULL);
80 return 1;
82 error = IoErr();
83 UnLock(lock);
84 if (error == ERROR_NO_MORE_ENTRIES)
85 error = 0;
86 else
88 FreeDosObject(DOS_EXALLCONTROL, eac);
89 FreeVec(ead);
90 printf("\nExAll() returned error on %s!\n", startpath);
91 printf("I/O Error is %ld\n", (long)error);
92 PrintFault(error, NULL);
93 return 1;
95 if (eac->eac_Entries == 0)
96 next = NULL;
97 else
98 next = ead;
99 while (next != NULL)
101 AddPart(dpath, startpath, 512);
102 AddPart(dpath, next->ed_Name, 512);
103 if (next->ed_Type == ST_FILE)
105 #if REAL_DELETE
106 if (!DeleteFile(dpath))
108 error = IoErr();
109 FreeDosObject(DOS_EXALLCONTROL, eac);
110 FreeVec(ead);
111 printf("\nFailed to delete file %s\n", dpath);
112 printf("I/O Error is %ld\n", (long)error);
113 PrintFault(error, NULL);
114 return 1;
116 #endif
118 else if (next->ed_Type == ST_USERDIR)
120 if (deleteDirContents(dpath, depth - 1, dnum - 1,
121 fnum == 0 ? fnum : fnum - 1) != 0)
123 FreeDosObject(DOS_EXALLCONTROL, eac);
124 FreeVec(ead);
125 return 1;
128 else
130 FreeDosObject(DOS_EXALLCONTROL, eac);
131 FreeVec(ead);
132 printf("\nFailed to identify %s - it is no directory or file!\n",
133 dpath);
134 return 1;
136 next = next->ed_Next;
138 FreeDosObject(DOS_EXALLCONTROL, eac);
139 FreeVec(ead);
140 #if REAL_DELETE
141 if (!DeleteFile(startpath))
143 error = IoErr();
144 printf("\nFailed to delete directory %s\n", startpath);
145 printf("I/O Error is %ld\n", (long)error);
146 PrintFault(error, NULL);
147 return 1;
149 #endif
150 return 0;
153 int deleteAll(char *startpath, int depth, int dnum, int fnum)
155 int i;
156 char name[32];
157 char path[512];
159 for (i = 0; i < dnum; i++)
161 sprintf(name, "d-%03d-%03d", depth, i);
162 AddPart(path, startpath, 512);
163 AddPart(path, name, 512);
164 if (deleteDirContents(path, depth, dnum, fnum) != 0)
165 return 1;
167 return 0;
170 int specificParentCheck(BPTR lock, BPTR dlock, char *path)
172 BPTR plock;
173 LONG error;
175 plock = ParentDir(dlock);
176 if (plock == BNULL)
178 error = IoErr();
179 printf("\nFailed to get parent of %s!\n", path);
180 printf("I/O Error is %ld\n", (long)error);
181 PrintFault(error, NULL);
182 return 1;
184 if (!SameLock(lock, plock))
186 UnLock(plock);
187 printf("\nParent of %s is not correct!\n", path);
188 return 1;
190 UnLock(plock);
191 return 0;
194 int checkParent(char *startpath, int depth, int dnum, int fnum, int size)
196 BPTR lock;
197 BPTR dlock;
198 BPTR flock;
199 char name[32];
200 char path[512];
201 char fpath[512];
202 int i, j;
203 LONG error;
205 if (dnum < 1)
206 dnum = 1;
207 lock = Lock(startpath, SHARED_LOCK);
208 if (lock == BNULL)
210 error = IoErr();
211 printf("\nFailed to get lock on %s!\n", startpath);
212 printf("I/O Error is %ld\n", (long)error);
213 PrintFault(error, NULL);
214 return 1;
216 for (i = 0; i < dnum; i++)
218 sprintf(name, "d-%03d-%03d", depth, i);
219 AddPart(path, startpath, 512);
220 AddPart(path, name, 512);
221 dlock = Lock(path, SHARED_LOCK);
222 if (dlock == BNULL)
224 error = IoErr();
225 UnLock(lock);
226 printf("\nFailed to get lock on %s!\n", path);
227 printf("I/O Error is %ld\n", (long)error);
228 PrintFault(error, NULL);
229 return 1;
231 if (specificParentCheck(lock, dlock, path) != 0)
233 UnLock(lock);
234 UnLock(dlock);
235 return 1;
237 for (j = 0; j < fnum; j++)
239 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
240 AddPart(fpath, path, 512);
241 AddPart(fpath, name, 512);
242 flock = Lock(fpath, SHARED_LOCK);
243 if (flock == BNULL)
245 error = IoErr();
246 UnLock(lock);
247 UnLock(dlock);
248 printf("\nFailed to get lock on %s!\n", fpath);
249 printf("I/O Error is %ld\n", (long)error);
250 PrintFault(error, NULL);
251 return 1;
253 if (specificParentCheck(dlock, flock, fpath) != 0)
255 UnLock(lock);
256 UnLock(flock);
257 UnLock(dlock);
258 return 1;
260 UnLock(flock);
262 if (depth > 0)
263 if (checkParent(path, depth - 1, dnum - 1,
264 fnum == 0 ? fnum : fnum - 1, size) != 0)
265 return 1;
267 UnLock(lock);
268 return 0;
271 int checkFile(char *path, int depth, int dnum, int fnum, int size)
273 BPTR fh;
274 unsigned int buffer[512];
275 int i, j;
276 LONG error;
278 fh = Open(path, MODE_OLDFILE);
279 if (fh == BNULL)
281 error = IoErr();
282 printf("\nFailed to open file %s!\n", path);
283 printf("I/O Error is %ld\n", (long)error);
284 PrintFault(error, NULL);
285 return 1;
287 for (i = 0; i < (size / 512); i++)
289 if (Read(fh, buffer, 512) != 512)
291 error = IoErr();
292 printf("\nFailed to read from file %s\n", path);
293 printf("I/O Error is %ld\n", (long)error);
294 PrintFault(error, NULL);
295 return 1;
297 for (j = 0; j < (512 >> 4); j += 4)
299 if ((buffer[j + 0] != depth) ||
300 (buffer[j + 1] != dnum) ||
301 (buffer[j + 2] != fnum) || (buffer[j + 3] != size))
303 printf("\nFailed to verify file %s at offset %d\n", path,
304 j * 4);
305 printf("Expected: %08x %08x %0x %08x\n", depth, dnum, fnum,
306 size);
307 printf("Got : %08x %08x %0x %08x\n", buffer[j + 0],
308 buffer[j + 1], buffer[j + 2], buffer[j + 3]);
309 return 1;
313 Close(fh);
314 return 0;
317 int checkFiles(char *startpath, int depth, int dnum, int fnum, int size)
319 int i, j;
320 char name[32];
321 char path[512];
322 char fpath[512];
324 if (dnum < 1)
325 dnum = 1;
326 for (i = 0; i < dnum; i++)
328 sprintf(name, "d-%03d-%03d", depth, i);
329 AddPart(path, startpath, 512);
330 AddPart(path, name, 512);
331 for (j = 0; j < fnum; j++)
333 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
334 AddPart(fpath, path, 512);
335 AddPart(fpath, name, 512);
336 if (checkFile(fpath, depth, dnum, fnum, size) != 0)
337 return 1;
339 if (depth > 0)
340 if (checkFiles(path, depth - 1, dnum - 1,
341 fnum == 0 ? fnum : fnum - 1, size) != 0)
342 return 1;
344 return 0;
347 int writeFile(char *path, int size, int depth, int dnum, int fnum)
349 BPTR fh;
350 unsigned int buffer[512];
351 int i;
352 LONG error;
354 fh = Open(path, MODE_NEWFILE);
355 if (fh == BNULL)
357 error = IoErr();
358 printf("\nFailed to create file %s!\n", path);
359 printf("I/O Error is %ld\n", (long)error);
360 PrintFault(error, NULL);
361 return 1;
363 for (i = 0; i < (512 >> 4); i += 4)
365 buffer[i + 0] = depth;
366 buffer[i + 1] = dnum;
367 buffer[i + 2] = fnum;
368 buffer[i + 3] = size;
370 for (i = 0; i < (size / 512); i++)
372 if (Write(fh, buffer, 512) != 512)
374 error = IoErr();
375 Close(fh);
376 printf("Failed to write to file %s\n", path);
377 printf("I/O Error is %ld\n", (long)error);
378 PrintFault(error, NULL);
379 return 1;
382 Close(fh);
383 // printf("Verifying ...");
384 if (checkFile(path, size, depth, dnum, fnum) != 0)
385 return 1;
386 // printf("done\n");
387 return 0;
390 int createFiles(char *startpath, int depth, int dnum, int fnum, int size)
392 int i, j;
393 char name[32];
394 char path[512];
395 char fpath[512];
397 if (dnum < 1)
398 dnum = 1;
399 for (i = 0; i < dnum; i++)
401 sprintf(name, "d-%03d-%03d", depth, i);
402 AddPart(path, startpath, 512);
403 AddPart(path, name, 512);
404 for (j = 0; j < fnum; j++)
406 sprintf(name, "f-%03d-%03d-%03d-%08d", depth, i, j, size);
407 AddPart(fpath, path, 512);
408 AddPart(fpath, name, 512);
409 if (writeFile(fpath, size, depth, dnum, fnum) != 0)
410 return 1;
412 if (depth > 0)
413 if (createFiles(path, depth - 1, dnum - 1,
414 fnum == 0 ? fnum : fnum - 1, size) != 0)
415 return 1;
417 return 0;
420 int checkDirs(char *startpath, int depth, int num)
422 BPTR dir;
423 int i;
424 char name[32];
425 char path[512];
426 LONG error;
428 if (num < 1)
429 num = 1;
430 for (i = 0; i < num; i++)
432 sprintf(name, "d-%03d-%03d", depth, i);
433 AddPart(path, startpath, 512);
434 AddPart(path, name, 512);
435 dir = Lock(path, SHARED_LOCK);
436 if (dir == BNULL)
438 error = IoErr();
439 printf("\nFailed locking %s!\n", path);
440 printf("I/O Error is %ld\n", (long)error);
441 PrintFault(error, NULL);
442 return 1;
444 UnLock(dir);
445 if (depth > 0)
446 if (checkDirs(path, depth - 1, num - 1) != 0)
447 return 1;
449 return 0;
452 int createDirs(char *startpath, int depth, int num)
454 BPTR dir;
455 int i;
456 char name[32];
457 char path[512] = "";
458 LONG error;
460 if (num < 1)
461 num = 1;
462 for (i = 0; i < num; i++)
464 sprintf(name, "d-%03d-%03d", depth, i);
465 AddPart(path, startpath, 512);
466 AddPart(path, name, 512);
467 dir = CreateDir(path);
468 if (dir == BNULL)
470 error = IoErr();
471 printf("\nFailed to create %s!\n", path);
472 printf("I/O Error is %ld\n", (long)error);
473 PrintFault(error, NULL);
474 return 1;
476 UnLock(dir);
477 if (depth > 0)
478 if (createDirs(path, depth - 1, num - 1) != 0)
479 return 1;
481 return 0;
484 int verifyFiles(char *startpath, int depth, int dnum, int fnum, int size)
486 printf("Verifying %d files per depth with size of %d bytes ...", fnum,
487 size);
488 if (checkFiles(startpath, depth, dnum, fnum, size) != 0)
489 return 1;
490 printf("done\n");
491 return 0;
494 int getDiskInfo(char *device, struct InfoData *id)
496 BPTR lock;
498 lock = Lock(device, SHARED_LOCK);
499 if (lock == BNULL)
501 printf("Failed to get lock on %s!\n", device);
502 return 1;
504 Info(lock, id);
505 UnLock(lock);
506 return 0;
509 int fileTest(char *startpath, int depth, int dnum, int fnum, int isize)
511 int size = isize * 1024;
512 int csize;
513 int cfnum;
514 int i;
516 for (i = 0; i < 5; i++)
518 printf("Creating %d files per depth with size of %d bytes ...",
519 fnum, size);
520 if (createFiles(startpath, depth, dnum, fnum, size) != 0)
521 return 1;
522 printf("done\n");
523 csize = size;
524 cfnum = fnum;
525 while (csize >= 1024)
527 if (verifyFiles(startpath, depth, dnum, cfnum, csize) != 0)
528 return 1;
529 csize = csize >> 3;
530 cfnum = cfnum << 1;
532 size = size << 3;
533 fnum = fnum >> 1;
535 return 0;
538 #define TEST 1
539 #define TEST_PARENT 0
541 int main(int argc, char *argv[])
543 int isize = 1; /* initial size in 1024 byte */
544 int depth = 10; /* directory depth */
545 int dnum = 6; /* number of directories per depth */
546 int fnum = 16; /* number of files per depth (the bigger the files the fewer are created) */
547 struct InfoData sid;
548 struct InfoData mid;
549 struct InfoData eid;
550 int nomid = 0;
552 if (argc < 2)
554 printf("Usage: %s <device>\n", argv[0]);
555 return 1;
557 #if TEST
558 if (getDiskInfo(argv[1], &sid) != 0)
559 return 1;
560 printf("Directory test\n");
561 printf("==============\n");
562 printf("Creating directories ...");
563 if (createDirs(argv[1], depth, dnum) != 0)
564 return 1;
565 printf("done\n");
566 printf("Checking directories ...");
567 if (checkDirs(argv[1], depth, dnum) != 0)
568 return 1;
569 printf("done\n");
570 printf("\n\n");
571 printf("File test\n");
572 printf("=========\n");
573 if (fileTest(argv[1], depth, dnum, fnum, isize) != 0)
574 return 1;
575 #if TEST_PARENT
576 printf("Doing a parent check ...");
577 if (checkParent(argv[1], depth, dnum, fnum,) != 0)
578 return 1;
579 printf("done\n");
580 #endif
581 if (getDiskInfo(argv[1], &mid) != 0)
582 nomid = 1;
583 #endif
584 printf("Deleting files and directories created by this test ...");
585 if (deleteAll(argv[1], depth, dnum, fnum) != 0)
586 return 1;
587 printf("done\n");
588 #if TEST
589 printf("Used blocks before test: %ld\n", (long)sid.id_NumBlocksUsed);
590 if (nomid == 0)
591 printf("Used blocks using test: %ld\n", (long)mid.id_NumBlocksUsed);
592 if (getDiskInfo(argv[1], &eid) == 0)
593 printf("Used blocks after test: %ld\n", (long)eid.id_NumBlocksUsed);
594 #endif
595 return 0;