File: adf_cache.c

package info (click to toggle)
unadf 0.7.11a-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,660 kB
  • sloc: ansic: 5,455; cpp: 404; makefile: 127; sh: 78
file content (616 lines) | stat: -rw-r--r-- 18,100 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
863
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 *  ADF Library. (C) 1997-2002 Laurent Clevy
 *
 *  adf_cache.c
 *
 *  $Id$
 *
 *  directory cache code
 *
 *  This file is part of ADFLib.
 *
 *  ADFLib is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  ADFLib is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */ 

#include<stdlib.h>
#include<string.h>
#include<stdint.h>

#include"adf_defs.h"
#include"adf_str.h"
#include"adf_err.h"
#include"Win32/defendian.h"
#include"adf_cache.h"
#include"adf_raw.h"
#include"adf_disk.h"
#include"adf_bitm.h"
#include"adf_util.h"
#include"adf_dir.h"


extern struct Env adfEnv;
/*
freeEntCache(struct CacheEntry *cEntry)
{
    if (cEntry->name!=NULL)
        free(cEntry->name);
    if (cEntry->comm!=NULL)
        free(cEntry->comm);
}
*/

/*
 * adfGetDirEntCache
 *
 * replace 'adfGetDirEnt'. returns a the dir contents based on the dircache list
 */
struct List* adfGetDirEntCache(struct Volume *vol, SECTNUM dir, BOOL recurs)
{
	struct bEntryBlock parent;
	struct bDirCacheBlock dirc;
    int offset, n;
    struct List *cell, *head;
    struct CacheEntry caEntry;
    struct Entry *entry;
    SECTNUM nSect;

    if (adfReadEntryBlock(vol,dir,&parent)!=RC_OK)
        return NULL;

    nSect = parent.extension;

    cell = head = NULL;
    do {
        /* one loop per cache block */
        n = offset = 0;
	    if (adfReadDirCBlock(vol, nSect, &dirc)!=RC_OK)
            return NULL;
        while (n<dirc.recordsNb) {
            /* one loop per record */
            entry = (struct Entry*)malloc(sizeof(struct Entry));
            if (!entry) {
                adfFreeDirList(head);
                return NULL;
            }
            adfGetCacheEntry(&dirc, &offset, &caEntry);

            /* converts a cache entry into a dir entry */
            entry->type = (int)caEntry.type;
            entry->name = strdup(caEntry.name);
            if (entry->name==NULL) {
                free(entry); adfFreeDirList(head);
                return NULL;
            }
            entry->sector = caEntry.header;
            entry->comment = strdup(caEntry.comm);
            if (entry->comment==NULL) {
                free(entry->name); adfFreeDirList(head);
                return NULL;
            }
            entry->size = caEntry.size;
            entry->access = caEntry.protect;
            adfDays2Date( caEntry.days, &(entry->year), &(entry->month), 
                &(entry->days) );
            entry->hour = caEntry.mins/60;
            entry->mins = caEntry.mins`;
            entry->secs = caEntry.ticks/50;

            /* add it into the linked list */
            if (head==NULL)
                head = cell = newCell(NULL, (void*)entry); 
            else
                cell = newCell(cell, (void*)entry); 

            if (cell==NULL) {
                adfFreeEntry(entry);
                adfFreeDirList(head);
                return NULL;
            }

            if (recurs && entry->type==ST_DIR)
                 cell->subdir = adfGetDirEntCache(vol,entry->sector,recurs);

            n  ;
        }
        nSect = dirc.nextDirC;
    }while (nSect!=0);
    
    return head;	
}



/*
 * adfGetCacheEntry
 *
 * Returns a cache entry, starting from the offset p (the index into records[])
 * This offset is updated to the end of the returned entry.
 */
void adfGetCacheEntry(struct bDirCacheBlock *dirc, int *p, struct CacheEntry *cEntry)
{
    int ptr;

    ptr = *p;

/*printf("p=%d\n",ptr);*/

#ifdef LITT_ENDIAN
    cEntry->header = swapLong(dirc->records ptr);
    cEntry->size = swapLong(dirc->records ptr 4);
    cEntry->protect = swapLong(dirc->records ptr 8);
    cEntry->days = swapShort(dirc->records ptr 16);
    cEntry->mins = swapShort(dirc->records ptr 18);
    cEntry->ticks = swapShort(dirc->records ptr 20);
#else
    cEntry->header = Long(dirc->records ptr);
    cEntry->size = Long(dirc->records ptr 4);
    cEntry->protect = Long(dirc->records ptr 8);
    cEntry->days = Short(dirc->records ptr 16);
    cEntry->mins = Short(dirc->records ptr 18);
    cEntry->ticks = Short(dirc->records ptr 20);
#endif
    cEntry->type =(signed char) dirc->records[ptr 22];

    cEntry->nLen = dirc->records[ptr 23];
/*    cEntry->name = (char*)malloc(sizeof(char)*(cEntry->nLen 1));
    if (!cEntry->name)
         return;
*/    memcpy(cEntry->name, dirc->records ptr 24, cEntry->nLen);
    cEntry->name[(int)(cEntry->nLen)]='\0';

    cEntry->cLen = dirc->records[ptr 24 cEntry->nLen];
    if (cEntry->cLen>0) {
/*        cEntry->comm =(char*)malloc(sizeof(char)*(cEntry->cLen 1));
        if (!cEntry->comm) {
            free( cEntry->name ); cEntry->name=NULL;
            return;
        }
*/        memcpy(cEntry->comm,dirc->records ptr 24 cEntry->nLen 1,cEntry->cLen);
    }
        cEntry->comm[(int)(cEntry->cLen)]='\0';
/*printf("cEntry->nLen %d cEntry->cLen %d %s\n",cEntry->nLen,cEntry->cLen,cEntry->name);*/
    *p  = ptr 24 cEntry->nLen 1 cEntry->cLen;

    /* the starting offset of each record must be even (68000 constraint) */ 
    if ((*p%2)!=0)
        *p=(*p) 1;
}


/*
 * adfPutCacheEntry
 *
 * remplaces one cache entry at the p offset, and returns its length
 */
int adfPutCacheEntry( struct bDirCacheBlock *dirc, int *p, struct CacheEntry *cEntry)
{
    int ptr, l;

    ptr = *p;

#ifdef LITT_ENDIAN
    swLong(dirc->records ptr, cEntry->header);
    swLong(dirc->records ptr 4, cEntry->size);
    swLong(dirc->records ptr 8, cEntry->protect);
    swShort(dirc->records ptr 16, cEntry->days);
    swShort(dirc->records ptr 18, cEntry->mins);
    swShort(dirc->records ptr 20, cEntry->ticks);
#else
    memcpy(dirc->records ptr,&(cEntry->header),4);
    memcpy(dirc->records ptr 4,&(cEntry->size),4);
    memcpy(dirc->records ptr 8,&(cEntry->protect),4);
    memcpy(dirc->records ptr 16,&(cEntry->days),2);
    memcpy(dirc->records ptr 18,&(cEntry->mins),2);
    memcpy(dirc->records ptr 20,&(cEntry->ticks),2);
#endif
    dirc->records[ptr 22] =(signed char)cEntry->type;

    dirc->records[ptr 23] = cEntry->nLen;
    memcpy(dirc->records ptr 24, cEntry->name, cEntry->nLen);

    dirc->records[ptr 24 cEntry->nLen] = cEntry->cLen;
    memcpy(dirc->records ptr 24 cEntry->nLen 1, cEntry->comm, cEntry->cLen);

/*puts("adfPutCacheEntry");*/

    l = 25 cEntry->nLen cEntry->cLen;
    if ((l%2)==0)
        return l;
    else {
        dirc->records[ptr l] =(char)0;
        return l 1;
    }

    /* ptr%2 must be == 0, if l%2==0, (ptr l)%2==0 */ 
}


/*
 * adfEntry2CacheEntry
 *
 * converts one dir entry into a cache entry, and return its future length in records[]
 */
int adfEntry2CacheEntry(struct bEntryBlock *entry, struct CacheEntry *newEntry)
{
    int entryLen;

    /* new entry */
    newEntry->header = entry->headerKey;
    if (entry->secType==ST_FILE)
        newEntry->size = entry->byteSize;
    else
        newEntry->size = 0L;
    newEntry->protect = entry->access;
    newEntry->days = (int16_t)entry->days;
    newEntry->mins = (int16_t)entry->mins;
    newEntry->ticks  = (int16_t)entry->ticks;
    newEntry->type = (signed char)entry->secType;
    newEntry->nLen = entry->nameLen;
    memcpy(newEntry->name, entry->name, newEntry->nLen);
    newEntry->name[(int)(newEntry->nLen)] = '\0';
    newEntry->cLen = entry->commLen;
    if (newEntry->cLen>0)
        memcpy(newEntry->comm, entry->comment, newEntry->cLen);

    entryLen = 24 newEntry->nLen 1 newEntry->cLen;

/*printf("entry->name %d entry->comment %d\n",entry->nameLen,entry->commLen);
printf("newEntry->nLen %d newEntry->cLen %d\n",newEntry->nLen,newEntry->cLen);
*/    if ((entryLen%2)==0)
        return entryLen;
    else
        return entryLen 1;
}


/*
 * adfDelFromCache
 *
 * delete one cache entry from its block. don't do 'records garbage collecting'
 */
RETCODE adfDelFromCache(struct Volume *vol, struct bEntryBlock *parent, 
    SECTNUM headerKey)
{
    struct bDirCacheBlock dirc;
    SECTNUM nSect, prevSect;
    struct CacheEntry caEntry;
    int offset, oldOffset, n;
    BOOL found;
    int entryLen;
    int i;
    RETCODE rc = RC_OK;

    prevSect = -1;
	nSect = parent->extension;
    found = FALSE;
    do {
        adfReadDirCBlock(vol, nSect, &dirc);
        offset = 0; n = 0;
        while(n < dirc.recordsNb && !found) {
            oldOffset = offset;
            adfGetCacheEntry(&dirc, &offset, &caEntry);
            found = (caEntry.header==headerKey);
            if (found) {
                entryLen = offset-oldOffset;
                if (dirc.recordsNb>1 || prevSect==-1) {
                    if (n<dirc.recordsNb-1) {
                        /* not the last of the block : switch the following records */
                        for(i=oldOffset; i<(488-entryLen); i  )
                            dirc.records[i] = dirc.records[i entryLen];
                        /* and clear the following bytes */
                        for(i=488-entryLen; i<488; i  )
                            dirc.records[i] = 0;
                    }
                    else {
                        /* the last record of this cache block */
                        for(i=oldOffset; i<offset; i  )
                            dirc.records[i] = 0;
                    }
                    dirc.recordsNb--;
                    if (adfWriteDirCBlock(vol, dirc.headerKey, &dirc)!=RC_OK)
						return -1;
                }
                else {
                    /* dirc.recordsNb ==1 or == 0 , prevSect!=-1 : 
                    * the only record in this dirc block and a previous dirc block exists 
                    */
                    adfSetBlockFree(vol, dirc.headerKey);
                    adfReadDirCBlock(vol, prevSect, &dirc);
                    dirc.nextDirC = 0L;
                    adfWriteDirCBlock(vol, prevSect, &dirc);

                    adfUpdateBitmap(vol);
                }
            }
            n  ;
        }
        prevSect = nSect;
        nSect = dirc.nextDirC;
    }while(nSect!=0 && !found);

    if (!found)
        (*adfEnv.wFct)("adfUpdateCache : entry not found");

    return rc;
}


/*
 * adfAddInCache
 *
 */
RETCODE adfAddInCache(struct Volume *vol, struct bEntryBlock *parent, 
    struct bEntryBlock *entry)
{
    struct bDirCacheBlock dirc, newDirc;
    SECTNUM nSect, nCache;
    struct CacheEntry caEntry, newEntry;
    int offset, n;
    int entryLen;

    entryLen = adfEntry2CacheEntry(entry, &newEntry);
/*printf("adfAddInCache--%4ld - m %8lx M -:d:d 0s "s\n",
    newEntry.header, newEntry.type, newEntry.size, newEntry.protect,
    newEntry.days, newEntry.mins/60, newEntry.mins`, 
	newEntry.ticks/50,
	newEntry.name, newEntry.comm);
*/
    nSect = parent->extension;
    do {
        if (adfReadDirCBlock(vol, nSect, &dirc)!=RC_OK)
            return RC_ERROR;
        offset = 0; n = 0;
/*printf("parent=%4ld\n",dirc.parent);*/
        while(n < dirc.recordsNb) {
            adfGetCacheEntry(&dirc, &offset, &caEntry);
/*printf("*%4ld - m %8lx M -:d:d 0s "s\n",
    caEntry.header, caEntry.type, caEntry.size, caEntry.protect,
    caEntry.days, caEntry.mins/60, caEntry.mins`, 
	caEntry.ticks/50,
	caEntry.name, caEntry.comm);
*/
            n  ;
        }
        
/*        if (offset entryLen<=488) {
            adfPutCacheEntry(&dirc, &offset, &newEntry);
            dirc.recordsNb  ;
            adfWriteDirCBlock(vol, dirc.headerKey, &dirc);
            return rc;
        }*/
        nSect = dirc.nextDirC;
    }while(nSect!=0);

    /* in the last block */
    if (offset entryLen<=488) {
        adfPutCacheEntry(&dirc, &offset, &newEntry);
        dirc.recordsNb  ;
/*printf("entry name=%s\n",newEntry.name);*/
    }
    else {
        /* request one new block free */
        nCache = adfGet1FreeBlock(vol);
        if (nCache==-1) {
           (*adfEnv.wFct)("adfCreateDir : nCache==-1");
           return RC_VOLFULL;
        }

        /* create a new dircache block */
        memset(&newDirc,0,512);
        if (parent->secType==ST_ROOT)
            newDirc.parent = vol->rootBlock;
        else if (parent->secType==ST_DIR)
            newDirc.parent = parent->headerKey;
        else
            (*adfEnv.wFct)("adfAddInCache : unknown secType");
        newDirc.recordsNb = 0L;
        newDirc.nextDirC = 0L;

        adfPutCacheEntry(&dirc, &offset, &newEntry);
        newDirc.recordsNb  ;
        if (adfWriteDirCBlock(vol, nCache, &newDirc)!=RC_OK)
			return RC_ERROR;
        dirc.nextDirC = nCache;
    }
/*printf("dirc.headerKey=%d\n",dirc.headerKey);*/
    if (adfWriteDirCBlock(vol, dirc.headerKey, &dirc)!=RC_OK)
		return RC_ERROR;
/*if (strcmp(entry->name,"file_5u")==0)
dumpBlock(&dirc);
*/
    return RC_OK;
}


/*
 * adfUpdateCache
 *
 */
RETCODE adfUpdateCache(struct Volume *vol, struct bEntryBlock *parent, 
    struct bEntryBlock *entry, BOOL entryLenChg)
{
    struct bDirCacheBlock dirc;
    SECTNUM nSect;
    struct CacheEntry caEntry, newEntry;
    int offset, oldOffset, n;
    BOOL found;
    int i, oLen, nLen;
    int sLen; /* shift length */

    nLen = adfEntry2CacheEntry(entry, &newEntry);

    nSect = parent->extension;
    found = FALSE;
    do {
/*printf("dirc=%d\n",nSect);*/
        if (adfReadDirCBlock(vol, nSect, &dirc)!=RC_OK)
			return RC_ERROR;
        offset = 0; n = 0;
        /* search entry to update with its header_key */
        while(n < dirc.recordsNb && !found) {
            oldOffset = offset;
            /* offset is updated */
            adfGetCacheEntry(&dirc, &offset, &caEntry);
            oLen = offset-oldOffset;
            sLen = oLen-nLen;
/*printf("olen=%d nlen=%d\n",oLen,nLen);*/
            found = (caEntry.header==newEntry.header);
            if (found) {
                if (!entryLenChg || oLen==nLen) {
                    /* same length : remplace the old values */
                    adfPutCacheEntry(&dirc, &oldOffset, &newEntry);
/*if (entryLenChg) puts("oLen==nLen");*/
                    if (adfWriteDirCBlock(vol, dirc.headerKey, &dirc)!=RC_OK)
                        return RC_ERROR;
                }
                else if (oLen>nLen) {
/*puts("oLen>nLen");*/
                    /* the new record is shorter, write it, 
                     * then shift down the following records 
                     */
                    adfPutCacheEntry(&dirc, &oldOffset, &newEntry);
                    for(i=oldOffset nLen; i<(488-sLen); i  )
                        dirc.records[i] = dirc.records[i sLen];
                    /* then clear the following bytes */
                    for(i=488-sLen; i<488; i  )
                        dirc.records[i] = (char)0;

                    if (adfWriteDirCBlock(vol, dirc.headerKey, &dirc)!=RC_OK)
                        return RC_ERROR;
                }
                else {
                    /* the new record is larger */
/*puts("oLen<nLen");*/
                    adfDelFromCache(vol,parent,entry->headerKey);
                    adfAddInCache(vol,parent,entry);
/*puts("oLen<nLen end");*/

                }
            }
            n  ;
        }
        nSect = dirc.nextDirC;
    }while(nSect!=0 && !found);

    if (found) {
        if (adfUpdateBitmap(vol)!=RC_OK)
			return RC_ERROR;
    }
    else
        (*adfEnv.wFct)("adfUpdateCache : entry not found");

    return RC_OK;
}


/*
 * adfCreateEmptyCache
 *
 */
RETCODE adfCreateEmptyCache(struct Volume *vol, struct bEntryBlock *parent, SECTNUM nSect)
{
    struct bDirCacheBlock dirc;
    SECTNUM nCache;

    if (nSect==-1) {
        nCache = adfGet1FreeBlock(vol);
        if (nCache==-1) {
           (*adfEnv.wFct)("adfCreateDir : nCache==-1");
           return RC_VOLFULL;
        }
    }
    else
        nCache = nSect;

    if (parent->extension==0)
		parent->extension = nCache;

    memset(&dirc,0, sizeof(struct bDirCacheBlock));

    if (parent->secType==ST_ROOT)
        dirc.parent = vol->rootBlock;
    else if (parent->secType==ST_DIR)
        dirc.parent = parent->headerKey;
    else {
        (*adfEnv.wFct)("adfCreateEmptyCache : unknown secType");
/*printf("secType=%d\n",parent->secType);*/
    }
        
    dirc.recordsNb = 0;
    dirc.nextDirC = 0;

    if (adfWriteDirCBlock(vol, nCache, &dirc)!=RC_OK)
		return RC_ERROR;

    return RC_OK;
}


/*
 * adfReadDirCBlock
 *
 */
RETCODE adfReadDirCBlock(struct Volume *vol, SECTNUM nSect, struct bDirCacheBlock *dirc)
{
    unsigned char buf[512];

    if (adfReadBlock(vol, nSect, buf)!=RC_OK)
		return RC_ERROR;

    memcpy(dirc,buf,512);
#ifdef LITT_ENDIAN
    swapEndian((unsigned char*)dirc,SWBL_CACHE);
#endif
    if (dirc->checkSum!=adfNormalSum(buf,20,512))
        (*adfEnv.wFct)("adfReadDirCBlock : invalid checksum");
    if (dirc->type!=T_DIRC)
        (*adfEnv.wFct)("adfReadDirCBlock : T_DIRC not found");
    if (dirc->headerKey!=nSect)
        (*adfEnv.wFct)("adfReadDirCBlock : headerKey!=nSect");

    return RC_OK;
}


/*
 * adfWriteDirCblock
 *
 */
RETCODE adfWriteDirCBlock(struct Volume* vol, int32_t nSect, struct bDirCacheBlock* dirc)
{
    unsigned char buf[LOGICAL_BLOCK_SIZE];
    uint32_t newSum;
 
    dirc->type = T_DIRC;
    dirc->headerKey = nSect; 

    memcpy(buf, dirc, LOGICAL_BLOCK_SIZE);
#ifdef LITT_ENDIAN
    swapEndian(buf, SWBL_CACHE);
#endif

    newSum = adfNormalSum(buf, 20, LOGICAL_BLOCK_SIZE);
    swLong(buf 20,newSum);
/*    *(long*)(buf 20) = swapLong((unsigned char*)&newSum);*/

    if (adfWriteBlock(vol, nSect, buf)!=RC_OK)
		return RC_ERROR;
/*puts("adfWriteDirCBlock");*/

    return RC_OK;
}

/*################################################################################*/