-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.c
More file actions
1107 lines (1003 loc) · 26.5 KB
/
Copy pathcache.c
File metadata and controls
1107 lines (1003 loc) · 26.5 KB
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "file_sys.h"
#include "global.h"
#include "Location.h"
#include "cache.h"
#define MAGIC_NUM 0x20040824l /* needs to get updated in case the format of *
* the cache.idx file changes */
static const char base32[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
typedef struct s_cache_item * CACHEITEM;
typedef struct s_cache_node * CACHENODE;
struct s_cache_item {
CACHENODE Node;
ULONG NodeHash;
CACHEITEM NodeNext;
LOCATION Location;
CACHEITEM NextItem;
CACHEITEM PrevItem;
size_t Reffs;
long Ident;
CACHEOBJ Object;
size_t Size;
long Date;
long Expires;
void (*dtor)(void*);
char Cached[10]; /* enough for 'ABCD.xyz' */
};
struct s_cache_node {
CACHENODE BackNode;
UWORD NodeMask;
unsigned Level4x :8;
unsigned Filled :8;
union {
CACHENODE Node;
CACHEITEM Item;
} Array[16];
};
#define item_isMem( citem ) ((citem)->dtor != NULL)
static CACHEITEM __cache_beg = NULL;
static CACHEITEM __cache_end = NULL;
#define CACHE_MAX ((size_t)100 *1024)
static size_t __cache_mem_use = 0;
static size_t __cache_mem_max = 0;
static size_t __cache_mem_num = 0;
static size_t __cache_dsk_use = 0;
static size_t __cache_dsk_max = 0;
static size_t __cache_dsk_num = 0;
static size_t __cache_dsk_lim = 0;
static WORD __cache_changed = FALSE;
static LOCATION __cache_dir = NULL;
static char * __cache_path = NULL;
static char * __cache_file = NULL;
static long __cache_fid = 1;
/* functions for cache directory handling */
static BOOL cache_flush (CACHEITEM);
static void cache_build (void);
static BOOL cache_remove (long num, long use);
/* search tree handling */
static CACHEITEM * tree_slot (LOCATION);
static CACHEITEM tree_item (LOCATION);
static CACHENODE tree_insert (CACHEITEM, LOCATION);
static void tree_remove (CACHEITEM);
/*******************************************************************************
*
* General setup and information.
*/
/*============================================================================*/
void
cache_setup (const char * dir, size_t mem_max, size_t dsk_max, size_t dsk_lim)
{
if (!dir && !mem_max && !dsk_max && !dsk_lim) {
#ifdef DEBUG
puts ("Setting cache defaults:");
#endif
if (!__cache_mem_max) {
mem_max = CACHE_MAX;
#ifdef DEBUG
printf (" Memory: %lu bytes\n", mem_max);
#endif
}
if (!__cache_dir) {
#ifdef DEBUG
puts (" Disk: (disabled)");
#endif
} else if (!__cache_dsk_max) {
dsk_max = 2L*1024*1024;
dsk_lim = 200;
#ifdef DEBUG
printf (" Disk: %lu bytes, %lu files.\n", dsk_max, dsk_lim);
#endif
}
}
if (mem_max) {
if ((long)(__cache_mem_max = (long)Malloc (-1) /2) < 0) {
__cache_mem_max = 0;
} else if (__cache_mem_max > mem_max) {
__cache_mem_max = mem_max;
}
#ifdef DEBUG
printf ("cache mem %lu\n", __cache_mem_max);
#endif
}
if (dsk_max) {
__cache_dsk_max = dsk_max;
__cache_dsk_lim = (dsk_lim ? dsk_lim : 500);
}
if (dir && *dir && !__cache_dir) {
size_t plen = strlen (dir);
if ((__cache_path = malloc (plen +14)) != NULL) {
memcpy (__cache_path, dir, plen);
if (__cache_path[plen-1] != '/' && __cache_path[plen-1] != '\\') {
__cache_path[plen++] = (strchr (__cache_path, '/') ? '/' : '\\');
}
__cache_file = __cache_path + plen;
strcpy (__cache_file, "cache.idx");
if ((__cache_dir = new_location (__cache_path, NULL)) != NULL) {
cache_build();
}
}
}
if (__cache_dir && __cache_dsk_max
&& (__cache_dsk_num > __cache_dsk_lim ||
__cache_dsk_use > __cache_dsk_max)) {
BOOL update = cache_remove (__cache_dsk_num - __cache_dsk_lim,
__cache_dsk_use - __cache_dsk_max);
if (update) {
cache_flush (NULL);
}
}
}
/*============================================================================*/
const char *
cache_DirInfo(void)
{
return (__cache_dir ? location_Path (__cache_dir, NULL) : NULL);
}
/******************************************************************************/
/*----------------------------------------------------------------------------*/
static CACHEITEM
create_item (LOCATION loc, CACHEOBJ object, size_t size, void (*dtor)(void*))
{
CACHEITEM citem;
if (dtor) {
if (!object) {
#ifdef DEBUG
puts ("create_item(): mem item without object!");
#endif
return NULL;
}
} else {
if (object) {
#ifdef DEBUG
puts ("create_item(): disk item with object!");
#endif
return NULL;
}
}
citem = malloc (sizeof (struct s_cache_item));
if (citem && tree_insert (citem, loc)) {
citem->Location = location_share (loc);
citem->Reffs = 1;
citem->Ident = 0;
citem->Object = object;
citem->Size = size;
citem->Date = 0;
citem->Expires = 0;
citem->dtor = dtor;
if (item_isMem (citem)) {
__cache_mem_num++;
__cache_mem_use += size;
} else {
__cache_dsk_num++;
__cache_dsk_use += size;
}
if (__cache_beg) __cache_beg->PrevItem = citem;
else __cache_end = citem;
citem->PrevItem = NULL;
citem->NextItem = __cache_beg;
__cache_beg = citem;
citem->Cached[0] = '\0';
} else if (citem) { /* memory exhausted */
free (citem);
citem = NULL;
}
return citem;
}
/*----------------------------------------------------------------------------*/
static void
destroy_item (CACHEITEM citem)
{
tree_remove (citem);
if (citem->PrevItem) citem->PrevItem->NextItem = citem->NextItem;
else __cache_beg = citem->NextItem;
if (citem->NextItem) citem->NextItem->PrevItem = citem->PrevItem;
else __cache_end = citem->PrevItem;
if (item_isMem (citem)) {
if (citem->Object) {
(*citem->dtor)(citem->Object);
}
__cache_mem_num--;
__cache_mem_use -= citem->Size;
} else { /* citem is (probably) on disk */
if (citem->Cached[0]) {
strcpy (__cache_file, citem->Cached);
unlink (__cache_path);
__cache_changed = TRUE;
}
free_location ((LOCATION*)&citem->Object);
__cache_dsk_num--;
__cache_dsk_use -= citem->Size;
}
free_location (&citem->Location);
free (citem);
}
/*----------------------------------------------------------------------------*/
static BOOL
item_reorder (CACHEITEM citem)
{
if (!citem->PrevItem) {
return FALSE; /* already at the begin */
}
citem->PrevItem->NextItem = citem->NextItem;
if (citem->NextItem) citem->NextItem->PrevItem = citem->PrevItem;
else __cache_end = citem->PrevItem;
__cache_beg->PrevItem = citem;
citem->PrevItem = NULL;
citem->NextItem = __cache_beg;
__cache_beg = citem;
return (__cache_changed = TRUE);
}
/*----------------------------------------------------------------------------*/
static BOOL
cache_throw (long size)
{
CACHEITEM citem = __cache_end;
#ifdef DEBUG
BOOL single;
if (size > 0) {
printf ("cache_throw(%li):\n", size);
single = FALSE;
} else {
single = TRUE;
}
#endif
while (citem) {
CACHEITEM prev = citem->PrevItem;
if (!citem->Reffs && item_isMem (citem)) {
#ifdef DEBUG
if (single) {
printf ("cache_throw(): %li '%s'\n",
citem->Size, citem->Location->File);
} else {
printf ("%7lu '%s'\n", citem->Size, citem->Location->File);
}
#endif
size -= citem->Size;
destroy_item (citem);
if (size <= 0) return TRUE;
}
citem = prev;
}
#ifdef DEBUG
if (single) {
puts ("cache_throw(): giving up");
} else {
puts (" ... giving up");
}
#endif
return FALSE;
}
/*============================================================================*/
CACHED
cache_insert (LOCATION loc, long ident, long lc_ident,
CACHEOBJ * object, size_t size, void (*dtor)(void*))
{
CACHEITEM citem;
if (!__cache_mem_max) {
cache_setup (NULL,0,0,0);
}
if (__cache_mem_use + size > __cache_mem_max) {
cache_throw (__cache_mem_use + size - __cache_mem_max);
}
if ((citem = create_item (loc, *object, size, dtor)) == NULL) {
return NULL;
}
citem->Ident = ident;
*object = NULL;
if (lc_ident) {
CACHEITEM nitem = citem->NodeNext;
while (nitem) {
if (nitem->Cached[0]) {
if (nitem->Ident) {
#ifdef DEBUG
printf ("cache_insert(%s): ident already set.\n", loc->FullName);
#endif
}
nitem->Ident = lc_ident;
__cache_changed = TRUE;
break;
}
nitem = nitem->NodeNext;
}
if (!nitem) {
#ifdef DEBUG
printf ("cache_insert(%s): local not found.\n", loc->FullName);
#endif
}
}
return citem->Object;
}
/*----------------------------------------------------------------------------*/
static LOCATION
cache_location (CACHEITEM citem)
{
if (!citem->Object && citem->Cached[0]) {
citem->Object = new_location (citem->Cached, __cache_dir);
}
return citem->Object;
}
/*============================================================================*/
CACHED
cache_lookup (LOCATION loc, long ident, long * opt_found)
{
CACHEITEM * p_array = tree_slot (loc);
CACHEITEM * p_cache = p_array;
CACHEITEM * p_found = NULL;
CACHEITEM citem;
if (opt_found) {
*opt_found = 0;
}
while ((citem = *p_cache) != NULL) {
if (location_equal (loc, citem->Location)) {
if (ident ? ident == citem->Ident : citem->Cached[0] != '\0') {
p_found = p_cache;
break;
} else if (opt_found /*&& citem->Ident*/) {
if (citem->Cached[0] == '\0') {
p_found = p_cache;
} else {
*opt_found = citem->Ident;
}
}
}
p_cache = &citem->NodeNext;
}
if (p_found) {
citem = *p_found;
if (p_found != p_array) {
*p_found = citem->NodeNext;
citem->NodeNext = *p_array;
*p_array = citem;
}
item_reorder (citem);
if (opt_found) {
*opt_found = citem->Ident;
}
return (ident ? citem->Object : (CACHED)cache_location (citem));
}
return NULL;
}
/*============================================================================*/
CACHED
cache_bound (CACHED cached, LOCATION * exchange)
{
CACHEITEM citem = __cache_beg;
while (citem) {
if (cached == citem->Object) {
citem->Reffs++;
if (exchange && *exchange != citem->Location) {
free_location (exchange);
*exchange = location_share (citem->Location);
}
return citem->Object;
}
citem = citem->NextItem;
}
return NULL;
}
/*============================================================================*/
CACHEOBJ
cache_release (CACHED * p_object, BOOL erase)
{
CACHEOBJ object = NULL;
CACHED cached = *p_object;
if (cached) {
CACHEITEM citem = __cache_beg;
while (citem) {
if (citem->Object == cached) {
if ((!citem->Reffs || !--citem->Reffs) && erase) {
if (item_isMem (citem)) {
destroy_item (citem);
} else if (!citem->Cached[0]) {
#ifdef DEBUG
puts ("cache_release(): item is busy!");
#endif
} else {
destroy_item (citem);
cache_flush (NULL);
}
}
*p_object = NULL;
break;
}
citem = citem->NextItem;
}
}
if (__cache_mem_use > __cache_mem_max) {
cache_throw (__cache_mem_use - __cache_mem_max);
}
return object;
}
/*============================================================================*/
size_t
cache_clear (CACHED this_n_all)
{
size_t num = 0;
size_t dsk = 0;
CACHEITEM citem = __cache_beg;
while (citem) {
CACHEITEM next = citem->NextItem;
if (!citem->Reffs && (item_isMem (citem) || citem->Cached[0])
&& (!this_n_all || this_n_all == citem->Object)) {
if (!item_isMem (citem)) {
if (!citem->Cached[0]) {
citem = next;
continue; /* skip this, busy item */
}
dsk++;
}
destroy_item (citem);
num++;
if (this_n_all) break;
}
citem = next;
}
if (dsk) {
cache_flush (NULL);
}
return num;
}
/*============================================================================*/
size_t
cache_info (size_t * size, CACHEINF * p_info)
{
size_t num = __cache_mem_num + __cache_dsk_num;
if (p_info) {
CACHEINF info = (num ? malloc (sizeof(struct s_cache_info) * num) : NULL);
if ((*p_info = info) != NULL) {
size_t n = num;
CACHEITEM citem = __cache_beg;
do {
info->Source = citem->Location;
info->Ident = (item_isMem (citem) ?citem->Ident : 0ul);
info->Used = citem->Reffs;
info->Cached = (citem->Cached[0] ? citem->Cached : NULL);
info->Local = (item_isMem (citem) ? NULL : citem->Object);
info->Date = citem->Date;
info->Expires = citem->Expires;
info->Object = citem->Object;
info->Size = citem->Size;
citem = citem->NextItem;
info++;
} while (--n);
}
}
if (size) {
*size = __cache_mem_use /*+ __cache_dsk_use*/;
}
return num;
}
/*============================================================================*/
CRESULT
cache_exclusive (LOCATION loc)
{
CACHEITEM citem = tree_item (loc);
CRESULT res;
if (citem) {
res = (!citem->Object ? CR_BUSY : CR_LOCAL);
#ifdef DEBUG
printf ("cache_exclusive(%s): %s\n",
loc->FullName, (res == CR_BUSY ? "busy" : "found"));
#endif
} else {
create_item (loc, NULL, 0uL, (void(*)(void*))0);
res = CR_NONE;
#ifdef DEBUG
printf ("cache_exclusive(%s) set\n", loc->FullName);
#endif
}
return res;
}
/*============================================================================*/
void
cache_abort (LOCATION loc)
{
CACHEITEM citem = tree_item (loc);
if (!citem) {
#ifdef DEBUG
printf ("cache_abort(%s): not found!\n", loc->FullName);
#endif
/*>>>>>>>>>> DEBUG */
} else if (citem->Object || citem->Cached[0]) {
#ifdef DEBUG
printf ("cache_abort(%s): not busy!\n", loc->FullName);
#endif
} else if (citem->Size) {
#ifdef DEBUG
printf ("cache_abort(%s): has size %lu!\n", loc->FullName, citem->Size);
#endif
/*<<<<<<<<<< DEBUG */
} else {
#ifdef DEBUG
printf ("cache_abort(%s)\n", loc->FullName);
#endif
destroy_item (citem);
}
}
/*============================================================================*/
LOCATION
cache_assign (LOCATION src, void * data, size_t size,
const char * type, long date, long expires)
{
LOCATION loc = NULL;
CACHEITEM citem = tree_item (src);
if (!citem) {
#ifdef DEBUG
printf ("cache_assign(%s): not found!\n", src->FullName);
#endif
} else if (citem->Object) {
#ifdef DEBUG
printf ("cache_assign(%s): already in use!\n", src->FullName);
#endif
} else {
if (__cache_dir) {
char buf[1024];
int fh;
char * p = citem->Cached;
short n = 15; /* 5bit * (4 -1) = one million possible files, */
do { /* should be enough */
*(p++) = base32[(__cache_fid >>n) & 0x1F];
} while ((n -= 5) >= 0);
if (type && *type) {
*(p++) = '.';
strcpy (p, type);
} else {
*p = '\0';
}
if (!__cache_dsk_max) {
cache_setup (NULL,0,0,0);
}
if (__cache_dsk_num >= __cache_dsk_lim ||
__cache_dsk_use + size > __cache_dsk_max) {
cache_remove (__cache_dsk_num +1 - __cache_dsk_lim,
__cache_dsk_use + size - __cache_dsk_max);
}
loc = new_location (citem->Cached, __cache_dir);
location_FullName (loc, buf, sizeof(buf));
if ((fh = open (buf, O_RDWR|O_CREAT|O_TRUNC|O_RAW, 0666)) >= 0) {
write (fh, data, size);
close (fh);
__cache_fid++;
citem->Object = loc;
citem->Size = size;
citem->Date = date;
citem->Expires = expires;
citem->Reffs--;
__cache_dsk_use += size;
cache_flush (citem);
} else {
free_location (&loc);
}
}
if (!loc) { /* either no cache dir set or file couldn't be written */
citem->Cached[0] = '\0';
destroy_item (citem);
}
}
return loc;
}
/*============================================================================*/
void
cache_expires (LOCATION loc, long date)
{
CACHEITEM citem = tree_item (loc);
if (!citem) {
#ifdef DEBUG
printf ("cache_expires(%s): not found!\n", loc->FullName);
#endif
} else if (date > 0 || !citem->Expires) {
citem->Expires = date;
__cache_changed = TRUE;
}
}
/*============================================================================*/
CRESULT
cache_query (LOCATION loc, long ident, CACHEINF info)
{
CACHEITEM citem = *tree_slot (loc);
CACHEITEM found = NULL;
CRESULT res_d = CR_NONE, res_m = CR_NONE;
info->Source = NULL;
info->Ident = 0;
info->Used = 0;
info->Cached = NULL;
info->Local = NULL;
info->Date = 0;
info->Expires = 0;
info->Object = NULL;
info->Size = 0;
while (citem) {
if (location_equal (loc, citem->Location)) {
if (!item_isMem (citem)) {
if (citem->Cached[0]) {
found = citem;
info->Cached = citem->Cached;
info->Local = cache_location (citem);
info->Date = citem->Date;
info->Expires = citem->Expires;
res_d = CR_LOCAL;
} else {
res_d = CR_BUSY;
}
if (!info->Ident) {
info->Ident = citem->Ident;
}
if (!ident || !info->Source) {
info->Source = citem->Location;
}
if (!ident || (res_m == CR_MATCH)) break;
} else if (ident == citem->Ident || !info->Object) {
info->Object = citem->Object;
info->Ident = citem->Ident;
info->Size = citem->Size;
if (ident != citem->Ident) {
if (!ident || !info->Source) {
info->Source = citem->Location;
}
res_m = CR_FOUND;
} else {
info->Source = citem->Location;
res_m = CR_MATCH;
if (res_d) break;
}
}
}
citem = citem->NodeNext;
}
if (found) {
item_reorder (found);
}
return (res_d | res_m);
}
/*******************************************************************************
*
* Functions for cache directory handling.
*/
/*------------------------------------------------------------------------------
* Write the index.cfg file.
*/
static BOOL
cache_flush (CACHEITEM citem)
{
FILE * file;
BOOL single;
if (citem && !__cache_changed) {
file = fopen (__cache_dir->FullName, "rb+");
single = TRUE;
} else {
citem = __cache_end;
file = fopen (__cache_dir->FullName, "wb");
single = FALSE;
}
if (!file) {
#ifdef DEBUG
puts ("cache_flush(): open failed.");
#endif
return FALSE;
}
if (!__cache_dsk_num) {
__cache_fid = 1;
}
fprintf (file, "%08lX:%08lX\n", MAGIC_NUM, __cache_fid);
if (single) {
fseek (file, 0, SEEK_END);
} else {
location_wrIdx (NULL, NULL); /* reset location database */
}
while (citem) {
if (!item_isMem (citem) && citem->Cached[0]) {
if (location_wrIdx (file, citem->Location)) {
fprintf (file, "$%08lX$%08lX$%08lX$%08lX$/%s\n", citem->Ident,
citem->Size, citem->Date, citem->Expires, citem->Cached);
if (single) break;
}
} else if (single) {
#ifdef DEBUG
puts ("cache_flush(): invalid item.");
#endif
break;
}
citem = citem->PrevItem;
}
fclose (file);
__cache_changed = FALSE;
return TRUE;
}
/*------------------------------------------------------------------------------
* Exit handler
*/
static void
_exit_flush (void)
{
CACHEITEM citem = __cache_beg;
time_t locl = time (NULL);
while (citem && citem->Object) {
CACHEITEM next = citem->NextItem;
if (!item_isMem(citem) && citem->Expires && citem->Expires < locl) {
destroy_item (citem);
}
citem = next;
}
if (__cache_changed == TRUE) {
cache_flush (NULL);
}
if (mem_TidyUp) { /* usefull for debugging */
citem = __cache_beg;
while (citem) {
CACHEITEM next = citem->NextItem;
if (!item_isMem (citem)) {
citem->Cached[0] = '\0';
}
destroy_item (citem);
citem = next;
}
free_location (&__cache_dir);
}
}
/*----------------------------------------------------------------------------*/
static long
read_hex (char ** ptr)
{
char * p = (**ptr == '$' ? *ptr + 1 : NULL);
long n = (p ? strtoul (p, &p, 16) : -2);
if (p > *ptr +1 && *p == '$') *ptr = p;
return n;
}
/*----------------------------------------------------------------------------*/
static void
clear_dir (void)
{
DTA * old = Fgetdta(), dta;
Fsetdta (&dta);
strcpy (__cache_file, "*.*");
if (Fsfirst (__cache_path, 0x0000) == E_OK) do {
if ((strchr (base32, toupper (dta.d_fname[0])) &&
strchr (base32, toupper (dta.d_fname[1])) &&
strchr (base32, toupper (dta.d_fname[2])) &&
strchr (base32, toupper (dta.d_fname[3])) &&
(!dta.d_fname[4] || (dta.d_fname[4] == '.')))
|| stricmp (dta.d_fname, "cache.idx") == 0) {
strcpy (__cache_file, dta.d_fname);
Fdelete (__cache_path);
}
} while (Fsnext() == E_OK);
Fsetdta (old);
}
/*------------------------------------------------------------------------------
* Read the index.cfg file and create all cache structures.
*/
static void
cache_build (void)
{
FILE * file = fopen (__cache_path, "rb");
if (file) {
char hdr[20] = "", * p = hdr;
if (!(fgets (hdr, (int)sizeof(hdr) -1, file)
&& strtoul (p, &p, 16) == MAGIC_NUM && *(p++) == ':'
&& (__cache_fid = strtoul (p, &p, 16)) > 0 && p == hdr +17
&& (hdr[17] == '\n' || hdr[17] == '\r'))) {
__cache_fid = 0l;
fclose (file);
file = NULL;
if (hdr[0]) {
hwUi_info ("cache::build()", " \n"
"The cache is out of date and will be cleared now.\n"
"This might need some time.\n");
}
clear_dir();
}
}
if (file) {
time_t locl = time (NULL);
while (!feof (file)) {
char buf[1024];
LOCATION loc = location_rdIdx (file);
if (!fgets (buf, (int)sizeof(buf) -1, file)) {
if (loc) {
#ifdef DEBUG
puts ("cache_setup(): idx truncated.");
#endif
free_location (&loc);
}
} else {
char * ptr = buf;
long ident = read_hex (&ptr);
long size = read_hex (&ptr);
long date = read_hex (&ptr);
long expr = read_hex (&ptr);
size_t len = strlen (++ptr);
while (len && isspace (ptr[len-1])) ptr[--len] = '\0';
if (size < 0 || date < 0 || expr < -1) {
#ifdef DEBUG
puts ("cache_setup(): idx corrupted.");
#endif
free_location (&loc);
break;
} else if (!loc) { /* outdated or invalid */
strcpy (__cache_file, ptr +1);
unlink (__cache_path);
__cache_changed = TRUE;
} else if (expr && expr <= locl) { /* Expired */
free_location (&loc);
strcpy (__cache_file, ptr +1);
unlink (__cache_path);
__cache_changed = TRUE;
} else {
CACHEITEM item = create_item (loc, NULL, size,(void(*)(void*))0);
if (!item) {
#ifdef DEBUG
puts ("cache_setup(): create failed.");
#endif
free_location (&loc);
break;
} else {
strcpy (item->Cached, ptr +1);
item->Ident = ident;
item->Date = date;
item->Expires = expr;
item->Reffs--;
}
}
}
}
fclose (file);
} else { /* !file */
__cache_changed = TRUE;
}
if (__cache_changed) {
if (!cache_flush (NULL)) {
free_location (&__cache_dir);
__cache_changed = FALSE;
} else if (!mem_TidyUp) {
atexit (_exit_flush);
}
}
if (mem_TidyUp) { /* usefull for debugging */
atexit (_exit_flush);
}
}
/*------------------------------------------------------------------------------
* Delete files to meet the cache limits.
*/
static BOOL
cache_remove (long num, long use)
{
long cnt = 0;
CACHEITEM citem = __cache_end;
while (citem) {
CACHEITEM prev = citem->PrevItem;
if (!item_isMem (citem) && !citem->Reffs && citem->Cached[0]) {
use -= citem->Size;
num--;
destroy_item (citem);
cnt++;
if (num <= 0 && use <= 0) break;
}
citem = prev;
}
return (cnt > 0);
}
/*******************************************************************************
*
* Search tree handling (sorted by LOCATION)
*/
/* root node, take care that ALL elements are zero-ed at startup! */
static struct s_cache_node __tree_base = {
NULL, 0x0000, 0, 0,
{ {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL},
{NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL}, {NULL} }
};
/*----------------------------------------------------------------------------*/
static CACHENODE
tree_node (ULONG hash, UWORD * p_idx)
{
CACHENODE node = &__tree_base;
UWORD idx = hash & 0xF;
while (node->NodeMask & (1 << idx)) { /* go through the tree */
node = node->Array[idx].Node;
idx = (hash >>= 4) & 0xF;
}