2 Copyright © 1995-2016, The AROS Development Team. All rights reserved.
7 #include <proto/exec.h>
10 #include <exec/memory.h>
12 #include <aros/debug.h>
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
;
23 struct FileInfoBlock fib
;
30 size
= (dnum
+ (fnum
* 32)) * (sizeof(struct ExAllData
) + 32);
31 eac
= AllocDosObject(DOS_EXALLCONTROL
, &ti
);
34 printf("\nFailed to allocated dos object!\n");
38 ead
= AllocVec(size
, MEMF_ANY
| MEMF_CLEAR
);
41 FreeDosObject(DOS_EXALLCONTROL
, eac
);
42 printf("\nFailed to allocated memory!\n");
45 lock
= Lock(startpath
, SHARED_LOCK
);
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
);
57 if (fib
.fib_DirEntryType
!= ST_USERDIR
)
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
);
68 if (ExAll(lock
, ead
, size
, ED_TYPE
, eac
))
71 kprintf("entries = %ld\n", eac
->eac_Entries
);
72 ExAllEnd(lock
, ead
, size
, ED_TYPE
, eac
);
75 FreeDosObject(DOS_EXALLCONTROL
, eac
);
76 printf("\nNot enough memory for %s when doing ExamineAll()!\n",
78 printf("I/O Error is %ld\n", (long)error
);
79 PrintFault(error
, NULL
);
84 if (error
== ERROR_NO_MORE_ENTRIES
)
88 FreeDosObject(DOS_EXALLCONTROL
, eac
);
90 printf("\nExAll() returned error on %s!\n", startpath
);
91 printf("I/O Error is %ld\n", (long)error
);
92 PrintFault(error
, NULL
);
95 if (eac
->eac_Entries
== 0)
101 AddPart(dpath
, startpath
, 512);
102 AddPart(dpath
, next
->ed_Name
, 512);
103 if (next
->ed_Type
== ST_FILE
)
106 if (!DeleteFile(dpath
))
109 FreeDosObject(DOS_EXALLCONTROL
, eac
);
111 printf("\nFailed to delete file %s\n", dpath
);
112 printf("I/O Error is %ld\n", (long)error
);
113 PrintFault(error
, NULL
);
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
);
130 FreeDosObject(DOS_EXALLCONTROL
, eac
);
132 printf("\nFailed to identify %s - it is no directory or file!\n",
136 next
= next
->ed_Next
;
138 FreeDosObject(DOS_EXALLCONTROL
, eac
);
141 if (!DeleteFile(startpath
))
144 printf("\nFailed to delete directory %s\n", startpath
);
145 printf("I/O Error is %ld\n", (long)error
);
146 PrintFault(error
, NULL
);
153 int deleteAll(char *startpath
, int depth
, int dnum
, int fnum
)
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)
170 int specificParentCheck(BPTR lock
, BPTR dlock
, char *path
)
175 plock
= ParentDir(dlock
);
179 printf("\nFailed to get parent of %s!\n", path
);
180 printf("I/O Error is %ld\n", (long)error
);
181 PrintFault(error
, NULL
);
184 if (!SameLock(lock
, plock
))
187 printf("\nParent of %s is not correct!\n", path
);
194 int checkParent(char *startpath
, int depth
, int dnum
, int fnum
, int size
)
207 lock
= Lock(startpath
, SHARED_LOCK
);
211 printf("\nFailed to get lock on %s!\n", startpath
);
212 printf("I/O Error is %ld\n", (long)error
);
213 PrintFault(error
, NULL
);
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
);
226 printf("\nFailed to get lock on %s!\n", path
);
227 printf("I/O Error is %ld\n", (long)error
);
228 PrintFault(error
, NULL
);
231 if (specificParentCheck(lock
, dlock
, path
) != 0)
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
);
248 printf("\nFailed to get lock on %s!\n", fpath
);
249 printf("I/O Error is %ld\n", (long)error
);
250 PrintFault(error
, NULL
);
253 if (specificParentCheck(dlock
, flock
, fpath
) != 0)
263 if (checkParent(path
, depth
- 1, dnum
- 1,
264 fnum
== 0 ? fnum
: fnum
- 1, size
) != 0)
271 int checkFile(char *path
, int depth
, int dnum
, int fnum
, int size
)
274 unsigned int buffer
[512];
278 fh
= Open(path
, MODE_OLDFILE
);
282 printf("\nFailed to open file %s!\n", path
);
283 printf("I/O Error is %ld\n", (long)error
);
284 PrintFault(error
, NULL
);
287 for (i
= 0; i
< (size
/ 512); i
++)
289 if (Read(fh
, buffer
, 512) != 512)
292 printf("\nFailed to read from file %s\n", path
);
293 printf("I/O Error is %ld\n", (long)error
);
294 PrintFault(error
, NULL
);
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
,
305 printf("Expected: %08x %08x %0x %08x\n", depth
, dnum
, fnum
,
307 printf("Got : %08x %08x %0x %08x\n", buffer
[j
+ 0],
308 buffer
[j
+ 1], buffer
[j
+ 2], buffer
[j
+ 3]);
317 int checkFiles(char *startpath
, int depth
, int dnum
, int fnum
, int size
)
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)
340 if (checkFiles(path
, depth
- 1, dnum
- 1,
341 fnum
== 0 ? fnum
: fnum
- 1, size
) != 0)
347 int writeFile(char *path
, int size
, int depth
, int dnum
, int fnum
)
350 unsigned int buffer
[512];
354 fh
= Open(path
, MODE_NEWFILE
);
358 printf("\nFailed to create file %s!\n", path
);
359 printf("I/O Error is %ld\n", (long)error
);
360 PrintFault(error
, NULL
);
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)
376 printf("Failed to write to file %s\n", path
);
377 printf("I/O Error is %ld\n", (long)error
);
378 PrintFault(error
, NULL
);
383 // printf("Verifying ...");
384 if (checkFile(path
, size
, depth
, dnum
, fnum
) != 0)
390 int createFiles(char *startpath
, int depth
, int dnum
, int fnum
, int size
)
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)
413 if (createFiles(path
, depth
- 1, dnum
- 1,
414 fnum
== 0 ? fnum
: fnum
- 1, size
) != 0)
420 int checkDirs(char *startpath
, int depth
, int num
)
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
);
439 printf("\nFailed locking %s!\n", path
);
440 printf("I/O Error is %ld\n", (long)error
);
441 PrintFault(error
, NULL
);
446 if (checkDirs(path
, depth
- 1, num
- 1) != 0)
452 int createDirs(char *startpath
, int depth
, int num
)
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
);
471 printf("\nFailed to create %s!\n", path
);
472 printf("I/O Error is %ld\n", (long)error
);
473 PrintFault(error
, NULL
);
478 if (createDirs(path
, depth
- 1, num
- 1) != 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
,
488 if (checkFiles(startpath
, depth
, dnum
, fnum
, size
) != 0)
494 int getDiskInfo(char *device
, struct InfoData
*id
)
498 lock
= Lock(device
, SHARED_LOCK
);
501 printf("Failed to get lock on %s!\n", device
);
509 int fileTest(char *startpath
, int depth
, int dnum
, int fnum
, int isize
)
511 int size
= isize
* 1024;
516 for (i
= 0; i
< 5; i
++)
518 printf("Creating %d files per depth with size of %d bytes ...",
520 if (createFiles(startpath
, depth
, dnum
, fnum
, size
) != 0)
525 while (csize
>= 1024)
527 if (verifyFiles(startpath
, depth
, dnum
, cfnum
, csize
) != 0)
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) */
554 printf("Usage: %s <device>\n", argv
[0]);
558 if (getDiskInfo(argv
[1], &sid
) != 0)
560 printf("Directory test\n");
561 printf("==============\n");
562 printf("Creating directories ...");
563 if (createDirs(argv
[1], depth
, dnum
) != 0)
566 printf("Checking directories ...");
567 if (checkDirs(argv
[1], depth
, dnum
) != 0)
571 printf("File test\n");
572 printf("=========\n");
573 if (fileTest(argv
[1], depth
, dnum
, fnum
, isize
) != 0)
576 printf("Doing a parent check ...");
577 if (checkParent(argv
[1], depth
, dnum
, fnum
,) != 0)
581 if (getDiskInfo(argv
[1], &mid
) != 0)
584 printf("Deleting files and directories created by this test ...");
585 if (deleteAll(argv
[1], depth
, dnum
, fnum
) != 0)
589 printf("Used blocks before test: %ld\n", (long)sid
.id_NumBlocksUsed
);
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
);