2 Copyright © 1995-2014, 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
) {
17 struct ExAllControl
*eac
;
18 struct TagItem ti
={TAG_DONE
};
19 struct ExAllData
*ead
;
20 struct ExAllData
*next
;
22 struct FileInfoBlock fib
;
29 size
=(dnum
+(fnum
*32))*(sizeof(struct ExAllData
)+32);
30 eac
= AllocDosObject(DOS_EXALLCONTROL
, &ti
);
33 printf("\nFailed to allocated dos object!\n");
37 ead
= AllocVec(size
, MEMF_ANY
| MEMF_CLEAR
);
40 FreeDosObject(DOS_EXALLCONTROL
, eac
);
41 printf("\nFailed to allocated memory!\n");
44 lock
= Lock(startpath
, SHARED_LOCK
);
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
);
56 if (fib
.fib_DirEntryType
!= ST_USERDIR
)
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
);
67 if (ExAll(lock
, ead
, size
, ED_TYPE
, eac
) != 0)
70 kprintf("entries = %ld\n", eac
->eac_Entries
);
71 ExAllEnd(lock
, ead
, size
, ED_TYPE
, eac
);
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
);
82 if (error
== ERROR_NO_MORE_ENTRIES
)
86 FreeDosObject(DOS_EXALLCONTROL
, eac
);
88 printf("\nExAll() returned error on %s!\n", startpath
);
89 printf("I/O Error is %ld\n", (long)error
);
90 PrintFault(error
, NULL
);
93 if (eac
->eac_Entries
== 0)
99 AddPart(dpath
, startpath
, 512);
100 AddPart(dpath
, next
->ed_Name
, 512);
101 if (next
->ed_Type
== ST_FILE
)
104 if (!DeleteFile(dpath
))
107 FreeDosObject(DOS_EXALLCONTROL
, eac
);
109 printf("\nFailed to delete file %s\n", dpath
);
110 printf("I/O Error is %ld\n", (long)error
);
111 PrintFault(error
, NULL
);
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
);
127 FreeDosObject(DOS_EXALLCONTROL
, eac
);
129 printf("\nFailed to identify %s - it is no directory or file!\n", dpath
);
132 next
= next
->ed_Next
;
134 FreeDosObject(DOS_EXALLCONTROL
, eac
);
137 if (!DeleteFile(startpath
))
140 printf("\nFailed to delete directory %s\n", startpath
);
141 printf("I/O Error is %ld\n", (long)error
);
142 PrintFault(error
, NULL
);
149 int deleteAll(char *startpath
, int depth
, int dnum
, int fnum
) {
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)
165 int specificParentCheck(BPTR lock
, BPTR dlock
, char *path
) {
169 plock
= ParentDir(dlock
);
173 printf("\nFailed to get parent of %s!\n", path
);
174 printf("I/O Error is %ld\n", (long)error
);
175 PrintFault(error
, NULL
);
178 if (!SameLock(lock
, plock
))
181 printf("\nParent of %s is not correct!\n", path
);
188 int checkParent(char *startpath
, int depth
, int dnum
, int fnum
, int size
) {
200 lock
= Lock(startpath
, SHARED_LOCK
);
204 printf("\nFailed to get lock on %s!\n", startpath
);
205 printf("I/O Error is %ld\n", (long)error
);
206 PrintFault(error
, NULL
);
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
);
219 printf("\nFailed to get lock on %s!\n", path
);
220 printf("I/O Error is %ld\n", (long)error
);
221 PrintFault(error
, NULL
);
224 if (specificParentCheck(lock
, dlock
, path
) != 0)
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
);
241 printf("\nFailed to get lock on %s!\n", fpath
);
242 printf("I/O Error is %ld\n", (long)error
);
243 PrintFault(error
, NULL
);
246 if (specificParentCheck(dlock
, flock
, fpath
) != 0)
256 if (checkParent(path
, depth
-1, dnum
-1, fnum
== 0 ? fnum
: fnum
-1, size
) != 0)
263 int checkFile(char *path
, int depth
, int dnum
, int fnum
, int size
) {
265 unsigned int buffer
[512];
269 fh
= Open(path
, MODE_OLDFILE
);
273 printf("\nFailed to open file %s!\n", path
);
274 printf("I/O Error is %ld\n", (long)error
);
275 PrintFault(error
, NULL
);
278 for (i
=0;i
<(size
/512);i
++)
280 if (Read(fh
, buffer
, 512) != 512)
283 printf("\nFailed to read from file %s\n", path
);
284 printf("I/O Error is %ld\n", (long)error
);
285 PrintFault(error
, NULL
);
288 for (j
=0;j
<(512>>4); j
+=4)
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]);
308 int checkFiles(char *startpath
, int depth
, int dnum
, int fnum
, int size
) {
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)
330 if (checkFiles(path
, depth
-1, dnum
-1, fnum
== 0 ? fnum
: fnum
-1, size
) != 0)
336 int writeFile(char *path
, int size
, int depth
, int dnum
, int fnum
) {
338 unsigned int buffer
[512];
342 fh
= Open(path
, MODE_NEWFILE
);
346 printf("\nFailed to create file %s!\n", path
);
347 printf("I/O Error is %ld\n", (long)error
);
348 PrintFault(error
, NULL
);
351 for (i
=0;i
<(512>>4); i
+=4)
358 for (i
=0;i
<(size
/512);i
++)
360 if (Write(fh
, buffer
, 512) != 512)
364 printf("Failed to write to file %s\n", path
);
365 printf("I/O Error is %ld\n", (long)error
);
366 PrintFault(error
, NULL
);
371 // printf("Verifying ...");
372 if (checkFile(path
, size
, depth
, dnum
, fnum
) != 0)
378 int createFiles(char *startpath
, int depth
, int dnum
, int fnum
, int size
) {
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)
400 if (createFiles(path
, depth
-1, dnum
-1, fnum
== 0 ? fnum
: fnum
-1, size
) != 0)
406 int checkDirs(char *startpath
, int depth
, int num
) {
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
);
424 printf("\nFailed locking %s!\n",path
);
425 printf("I/O Error is %ld\n", (long)error
);
426 PrintFault(error
, NULL
);
431 if (checkDirs(path
, depth
-1, num
-1) != 0)
437 int createDirs(char *startpath
, int depth
, int num
) {
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
);
455 printf("\nFailed to create %s!\n", path
);
456 printf("I/O Error is %ld\n", (long)error
);
457 PrintFault(error
, NULL
);
462 if (createDirs(path
, depth
-1, num
-1) != 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)
476 int getDiskInfo(char *device
, struct InfoData
*id
) {
479 lock
= Lock(device
, SHARED_LOCK
);
482 printf("Failed to get lock on %s!\n", device
);
490 int fileTest(char *startpath
, int depth
, int dnum
, int fnum
, int isize
) {
498 printf("Creating %d files per depth with size of %d bytes ...", fnum
, size
);
499 if (createFiles(startpath
, depth
, dnum
, fnum
, size
) != 0)
506 if (verifyFiles(startpath
, depth
, dnum
, cfnum
, csize
) != 0)
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) */
532 printf("Usage: %s <device>\n", argv
[0]);
536 if (getDiskInfo(argv
[1], &sid
) != 0)
538 printf("Directory test\n");
539 printf("==============\n");
540 printf("Creating directories ...");
541 if (createDirs(argv
[1], depth
, dnum
) != 0)
544 printf("Checking directories ...");
545 if (checkDirs(argv
[1], depth
, dnum
) != 0)
549 printf("File test\n");
550 printf("=========\n");
551 if (fileTest(argv
[1], depth
, dnum
, fnum
, isize
) != 0)
554 printf("Doing a parent check ...");
555 if (checkParent(argv
[1], depth
, dnum
, fnum
,) != 0)
559 if (getDiskInfo(argv
[1], &mid
) != 0)
562 printf("Deleting files and directories created by this test ...");
563 if (deleteAll(argv
[1], depth
, dnum
, fnum
) != 0)
567 printf("Used blocks before test: %ld\n", (long)sid
.id_NumBlocksUsed
);
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
);