2 * ADF Library. (C) 1997-1999 Laurent Clevy
19 extern unsigned long bitMask
[32];
21 extern struct Env adfEnv
;
27 RETCODE
adfUpdateBitmap(struct Volume
*vol
)
30 struct bRootBlock root
;
32 /*printf("adfUpdateBitmap\n");*/
34 if (adfReadRootBlock(vol
, vol
->rootBlock
,&root
)!=RC_OK
)
37 root
.bmFlag
= BM_INVALID
;
38 if (adfWriteRootBlock(vol
,vol
->rootBlock
,&root
)!=RC_OK
)
41 for(i
=0; i
<vol
->bitmapSize
; i
++)
42 if (vol
->bitmapBlocksChg
[i
]) {
43 if (adfWriteBitmapBlock(vol
, vol
->bitmapBlocks
[i
], vol
->bitmapTable
[i
])!=RC_OK
)
45 vol
->bitmapBlocksChg
[i
] = FALSE
;
48 root
.bmFlag
= BM_VALID
;
49 adfTime2AmigaTime(adfGiveCurrentTime(),&(root
.days
),&(root
.mins
),&(root
.ticks
));
50 if (adfWriteRootBlock(vol
,vol
->rootBlock
,&root
)!=RC_OK
)
61 long adfCountFreeBlocks(struct Volume
* vol
)
67 for(j
=vol
->firstBlock
+2; j
<=(vol
->lastBlock
- vol
->firstBlock
); j
++)
68 if ( adfIsBlockFree(vol
,j
) )
79 RETCODE
adfReadBitmap(struct Volume
* vol
, long nBlock
, struct bRootBlock
* root
)
83 struct bBitmapExtBlock bmExt
;
85 mapSize
= nBlock
/ (127*32);
86 if ( (nBlock
%(127*32))!=0 )
88 vol
->bitmapSize
= mapSize
;
90 vol
->bitmapTable
= (struct bBitmapBlock
**) malloc(sizeof(struct bBitmapBlock
*)*mapSize
);
91 if (!vol
->bitmapTable
) {
92 (*adfEnv
.eFct
)("adfReadBitmap : malloc, vol->bitmapTable");
95 vol
->bitmapBlocks
= (SECTNUM
*) malloc(sizeof(SECTNUM
)*mapSize
);
96 if (!vol
->bitmapBlocks
) {
97 free(vol
->bitmapTable
);
98 (*adfEnv
.eFct
)("adfReadBitmap : malloc, vol->bitmapBlocks");
101 vol
->bitmapBlocksChg
= (BOOL
*) malloc(sizeof(BOOL
)*mapSize
);
102 if (!vol
->bitmapBlocksChg
) {
103 free(vol
->bitmapTable
); free(vol
->bitmapBlocks
);
104 (*adfEnv
.eFct
)("adfReadBitmap : malloc, vol->bitmapBlocks");
107 for(i
=0; i
<mapSize
; i
++) {
108 vol
->bitmapBlocksChg
[i
] = FALSE
;
110 vol
->bitmapTable
[i
] = (struct bBitmapBlock
*)malloc(sizeof(struct bBitmapBlock
));
111 if (!vol
->bitmapTable
[i
]) {
112 free(vol
->bitmapBlocksChg
); free(vol
->bitmapBlocks
);
114 free(vol
->bitmapTable
[j
]);
115 free(vol
->bitmapTable
);
116 (*adfEnv
.eFct
)("adfReadBitmap : malloc, vol->bitmapBlocks");
122 /* bitmap pointers in rootblock : 0 <= i <BM_SIZE */
123 while(i
<BM_SIZE
&& root
->bmPages
[i
]!=0) {
124 vol
->bitmapBlocks
[j
] = nSect
= root
->bmPages
[i
];
125 if ( !isSectNumValid(vol
,nSect
) ) {
126 (*adfEnv
.wFct
)("adfReadBitmap : sector out of range");
129 if (adfReadBitmapBlock(vol
, nSect
, vol
->bitmapTable
[j
])!=RC_OK
) {
137 /* bitmap pointers in bitmapExtBlock, j <= mapSize */
138 if (adfReadBitmapExtBlock(vol
, nSect
, &bmExt
)!=RC_OK
) {
143 while(i
<127 && j
<mapSize
) {
144 nSect
= bmExt
.bmPages
[i
];
145 if ( !isSectNumValid(vol
,nSect
) )
146 (*adfEnv
.wFct
)("adfReadBitmap : sector out of range");
147 vol
->bitmapBlocks
[j
] = nSect
;
149 if (adfReadBitmapBlock(vol
, nSect
, vol
->bitmapTable
[j
])!=RC_OK
) {
155 nSect
= bmExt
.nextBlock
;
166 BOOL
adfIsBlockFree(struct Volume
* vol
, SECTNUM nSect
)
168 int sectOfMap
= nSect
-2;
169 int block
= sectOfMap
/(127*32);
170 int indexInMap
= (sectOfMap
/32)%127;
172 /*printf("sect=%d block=%d ind=%d, ",sectOfMap,block,indexInMap);
173 printf("bit=%d, ",sectOfMap%32);
174 printf("bitm=%x, ",bitMask[ sectOfMap%32]);
175 printf("res=%x, ",vol->bitmapTable[ block ]->map[ indexInMap ]
176 & bitMask[ sectOfMap%32 ]);
178 return ( (vol
->bitmapTable
[ block
]->map
[ indexInMap
]
179 & bitMask
[ sectOfMap
%32 ])!=0 );
187 void adfSetBlockFree(struct Volume
* vol
, SECTNUM nSect
)
189 unsigned long oldValue
;
190 int sectOfMap
= nSect
-2;
191 int block
= sectOfMap
/(127*32);
192 int indexInMap
= (sectOfMap
/32)%127;
194 /*printf("sect=%d block=%d ind=%d, ",sectOfMap,block,indexInMap);
195 printf("bit=%d, ",sectOfMap%32);
196 *printf("bitm=%x, ",bitMask[ sectOfMap%32]);*/
198 oldValue
= vol
->bitmapTable
[ block
]->map
[ indexInMap
];
199 /*printf("old=%x, ",oldValue);*/
200 vol
->bitmapTable
[ block
]->map
[ indexInMap
]
201 = oldValue
| bitMask
[ sectOfMap
%32 ];
202 /*printf("new=%x, ",vol->bitmapTable[ block ]->map[ indexInMap ]);*/
204 vol
->bitmapBlocksChg
[ block
] = TRUE
;
212 void adfSetBlockUsed(struct Volume
* vol
, SECTNUM nSect
)
214 unsigned long oldValue
;
215 int sectOfMap
= nSect
-2;
216 int block
= sectOfMap
/(127*32);
217 int indexInMap
= (sectOfMap
/32)%127;
219 oldValue
= vol
->bitmapTable
[ block
]->map
[ indexInMap
];
221 vol
->bitmapTable
[ block
]->map
[ indexInMap
]
222 = oldValue
& (~bitMask
[ sectOfMap
%32 ]);
223 vol
->bitmapBlocksChg
[ block
] = TRUE
;
231 SECTNUM
adfGet1FreeBlock(struct Volume
*vol
) {
233 if (!adfGetFreeBlocks(vol
,1,block
))
243 BOOL
adfGetFreeBlocks(struct Volume
* vol
, int nbSect
, SECTNUM
* sectList
)
247 long block
= vol
->rootBlock
;
251 //printf("lastblock=%ld\n",vol->lastBlock);
252 while( i
<nbSect
&& !endSearch
) {
253 if ( adfIsBlockFree(vol
, block
) ) {
257 /* if ( block==vol->lastBlock )
258 block = vol->firstBlock+2;*/
259 if ( (block
+vol
->firstBlock
)==vol
->lastBlock
)
264 if (block
== vol
->rootBlock
)
270 for(j
=0; j
<nbSect
; j
++)
271 adfSetBlockUsed( vol
, sectList
[j
] );
280 * create bitmap structure in vol
282 RETCODE
adfCreateBitmap(struct Volume
*vol
)
284 long nBlock
, mapSize
;
287 nBlock
= vol
->lastBlock
- vol
->firstBlock
+1 - 2;
289 mapSize
= nBlock
/ (127*32);
290 if ( (nBlock
%(127*32))!=0 )
292 vol
->bitmapSize
= mapSize
;
294 vol
->bitmapTable
= (struct bBitmapBlock
**)malloc( sizeof(struct bBitmapBlock
*)*mapSize
);
295 if (!vol
->bitmapTable
) {
296 (*adfEnv
.eFct
)("adfCreateBitmap : malloc, vol->bitmapTable");
300 vol
->bitmapBlocksChg
= (BOOL
*) malloc(sizeof(BOOL
)*mapSize
);
301 if (!vol
->bitmapBlocksChg
) {
302 free(vol
->bitmapTable
);
303 (*adfEnv
.eFct
)("adfCreateBitmap : malloc, vol->bitmapBlocksChg");
307 vol
->bitmapBlocks
= (SECTNUM
*) malloc(sizeof(SECTNUM
)*mapSize
);
308 if (!vol
->bitmapBlocks
) {
309 free(vol
->bitmapTable
); free(vol
->bitmapBlocksChg
);
310 (*adfEnv
.eFct
)("adfCreateBitmap : malloc, vol->bitmapBlocks");
314 for(i
=0; i
<mapSize
; i
++) {
315 vol
->bitmapTable
[i
] = (struct bBitmapBlock
*)malloc(sizeof(struct bBitmapBlock
));
316 if (!vol
->bitmapTable
[i
]) {
317 free(vol
->bitmapTable
); free(vol
->bitmapBlocksChg
);
319 free(vol
->bitmapTable
[j
]);
320 free(vol
->bitmapTable
);
321 (*adfEnv
.eFct
)("adfCreateBitmap : malloc");
326 for(i
=vol
->firstBlock
+2; i
<=(vol
->lastBlock
- vol
->firstBlock
); i
++)
327 adfSetBlockFree(vol
, i
);
336 * write ext blocks and bitmap
338 * uses vol->bitmapSize,
340 RETCODE
adfWriteNewBitmap(struct Volume
*vol
)
342 struct bBitmapExtBlock bitme
;
343 SECTNUM
*bitExtBlock
;
348 struct bRootBlock root
;
350 sectList
=(SECTNUM
*)malloc(sizeof(SECTNUM
)*vol
->bitmapSize
);
352 (*adfEnv
.eFct
)("adfCreateBitmap : sectList");
356 if (!adfGetFreeBlocks(vol
, vol
->bitmapSize
, sectList
)) {
361 if (adfReadRootBlock(vol
, vol
->rootBlock
, &root
)!=RC_OK
) {
366 n
= min( vol
->bitmapSize
, BM_SIZE
);
368 root
.bmPages
[i
] = vol
->bitmapBlocks
[i
] = sectList
[i
];
372 /* for devices with more than 25*127 blocks == hards disks */
373 if (vol
->bitmapSize
>BM_SIZE
) {
375 nExtBlock
= (vol
->bitmapSize
-BM_SIZE
)/127;
376 if ((vol
->bitmapSize
-BM_SIZE
)%127)
379 bitExtBlock
=(SECTNUM
*)malloc(sizeof(SECTNUM
)*nExtBlock
);
382 adfEnv
.eFct("adfWriteNewBitmap : malloc failed");
386 if (!adfGetFreeBlocks(vol
, nExtBlock
, bitExtBlock
)) {
387 free(sectList
); free(bitExtBlock
);
392 root
.bmExt
= bitExtBlock
[ k
];
393 while( nBlock
<vol
->bitmapSize
) {
395 while( i
<127 && nBlock
<vol
->bitmapSize
) {
396 bitme
.bmPages
[i
] = vol
->bitmapBlocks
[nBlock
] = sectList
[i
];
401 bitme
.nextBlock
= bitExtBlock
[ k
+1 ];
404 if (adfWriteBitmapExtBlock(vol
, bitExtBlock
[ k
], &bitme
)!=RC_OK
) {
405 free(sectList
); free(bitExtBlock
);
415 if (adfWriteRootBlock(vol
,vol
->rootBlock
,&root
)!=RC_OK
)
427 adfReadBitmapBlock(struct Volume
* vol
, SECTNUM nSect
, struct bBitmapBlock
* bitm
)
429 unsigned char buf
[LOGICAL_BLOCK_SIZE
];
431 //printf("bitmap %ld\n",nSect);
432 if (adfReadBlock(vol
, nSect
, buf
)!=RC_OK
)
435 memcpy(bitm
, buf
, LOGICAL_BLOCK_SIZE
);
437 /* big to little = 68000 to x86 */
438 swapEndian((unsigned char*)bitm
, SWBL_BITMAP
);
441 if (bitm
->checkSum
!=adfNormalSum(buf
,0,LOGICAL_BLOCK_SIZE
))
442 (*adfEnv
.wFct
)("adfReadBitmapBlock : invalid checksum");
449 * adfWriteBitmapBlock
454 adfWriteBitmapBlock(struct Volume
* vol
, SECTNUM nSect
, struct bBitmapBlock
* bitm
)
456 unsigned char buf
[LOGICAL_BLOCK_SIZE
];
457 unsigned long newSum
;
459 memcpy(buf
,bitm
,LOGICAL_BLOCK_SIZE
);
462 swapEndian(buf
, SWBL_BITMAP
);
465 newSum
= adfNormalSum(buf
, 0, LOGICAL_BLOCK_SIZE
);
468 /* dumpBlock((unsigned char*)buf);*/
469 if (adfWriteBlock(vol
, nSect
, (unsigned char*)buf
)!=RC_OK
)
477 * adfReadBitmapExtBlock
482 adfReadBitmapExtBlock(struct Volume
* vol
, SECTNUM nSect
, struct bBitmapExtBlock
* bitme
)
484 unsigned char buf
[LOGICAL_BLOCK_SIZE
];
486 if (adfReadBlock(vol
, nSect
, buf
)!=RC_OK
)
489 memcpy(bitme
, buf
, LOGICAL_BLOCK_SIZE
);
491 swapEndian((unsigned char*)bitme
, SWBL_BITMAP
);
499 * adfWriteBitmapExtBlock
503 adfWriteBitmapExtBlock(struct Volume
* vol
, SECTNUM nSect
, struct bBitmapExtBlock
* bitme
)
505 unsigned char buf
[LOGICAL_BLOCK_SIZE
];
507 memcpy(buf
,bitme
, LOGICAL_BLOCK_SIZE
);
510 swapEndian(buf
, SWBL_BITMAPE
);
513 /* dumpBlock((unsigned char*)buf);*/
514 if (adfWriteBlock(vol
, nSect
, (unsigned char*)buf
)!=RC_OK
)
525 void adfFreeBitmap(struct Volume
* vol
)
529 for(i
=0; i
<vol
->bitmapSize
; i
++)
530 free(vol
->bitmapTable
[i
]);
533 free(vol
->bitmapTable
);
534 vol
->bitmapTable
= 0;
536 free(vol
->bitmapBlocks
);
537 vol
->bitmapBlocks
= 0;
539 free(vol
->bitmapBlocksChg
);
540 vol
->bitmapBlocksChg
= 0;
544 /*#######################################################################################*/