-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwincache_fcache.c
1167 lines (924 loc) · 31.6 KB
/
wincache_fcache.c
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
/*
+----------------------------------------------------------------------------------------------+
| Windows Cache for PHP |
+----------------------------------------------------------------------------------------------+
| Copyright (c) 2009, Microsoft Corporation. All rights reserved. |
| Copyright (c) 2024, Hao Lu. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without modification, are |
| permitted provided that the following conditions are met: |
| - Redistributions of source code must retain the above copyright notice, this list of |
| conditions and the following disclaimer. |
| - Redistributions in binary form must reproduce the above copyright notice, this list of |
| conditions and the following disclaimer in the documentation and/or other materials provided |
| with the distribution. |
| - Neither the name of the Microsoft Corporation nor the names of its contributors may be |
| used to endorse or promote products derived from this software without specific prior written|
| permission. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE|
| GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| OF THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------------------------+
| Module: wincache_fcache.c |
+----------------------------------------------------------------------------------------------+
| Author: Kanwaljeet Singla <[email protected]> |
| Updated: Eric Stenson <[email protected]> |
| Hao Lu <[email protected]> |
+----------------------------------------------------------------------------------------------+
*/
#include "precomp.h"
#define READAHEAD_MAXIMUM 0x10000000
static int read_file_security(fcache_context * pfcache, const char * filename, unsigned char ** ppsec);
static int read_file_content(HANDLE hFile, unsigned int filesize, void ** ppbuffer);
/* internal helper functions */
void fcache_init_php_handle(fcache_handle *fhandle);
/* Globals */
unsigned short gfcacheid = 1;
/* Private methods */
static int read_file_security(fcache_context * pfcache, const char * filename, unsigned char ** ppsec)
{
int result = NONFATAL;
SECURITY_INFORMATION sec_info = 0;
unsigned int desc_length = 0;
unsigned char * psec_desc = NULL;
dprintdecorate("start read_file_security");
_ASSERT(pfcache != NULL);
_ASSERT(filename != NULL);
_ASSERT(ppsec != NULL);
*ppsec = NULL;
sec_info |= OWNER_SECURITY_INFORMATION;
sec_info |= GROUP_SECURITY_INFORMATION;
sec_info |= DACL_SECURITY_INFORMATION;
/* Get size of security buffer. Call is expected to fail */
if(GetFileSecurity(filename, sec_info, NULL, 0, &desc_length))
{
goto Finished;
}
psec_desc = (unsigned char *)alloc_smalloc(pfcache->palloc, desc_length);
if(psec_desc == NULL)
{
result = FATAL_OUT_OF_SMEMORY;
goto Finished;
}
if(!GetFileSecurity(filename, sec_info, (PSECURITY_DESCRIPTOR)psec_desc, desc_length, &desc_length))
{
goto Finished;
}
*ppsec = psec_desc;
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintverbose("failure %d in read_file_security", result);
if(psec_desc != NULL)
{
alloc_sfree(pfcache->palloc, psec_desc);
psec_desc = NULL;
}
}
dprintdecorate("end read_file_security");
return result;
}
static int read_file_content(HANDLE hFile, unsigned int filesize, void ** ppbuffer)
{
int result = NONFATAL;
HRESULT hr = S_OK;
void * pbuffer = NULL;
size_t coffset = 0;
unsigned int readahead = 0;
char * pvread = NULL;
OVERLAPPED Overlapped;
zend_bool breturn;
dprintverbose("start read_file_content");
_ASSERT(hFile != NULL);
_ASSERT(filesize > 0);
_ASSERT(ppbuffer != NULL);
pbuffer = *ppbuffer;
_ASSERT(pbuffer != NULL);
ZeroMemory(&Overlapped, sizeof(OVERLAPPED));
pvread = (char *)pbuffer;
while(coffset < filesize)
{
Overlapped.Offset = (DWORD)coffset;
if(filesize - coffset > READAHEAD_MAXIMUM)
{
readahead = READAHEAD_MAXIMUM;
}
else
{
/*
* we've validated that the difference is safe to cast to
* a 32-bit value before making this assignment.
*/
readahead = (unsigned int)(filesize - coffset);
}
breturn = ReadFile(hFile, pvread, readahead, &readahead, &Overlapped);
if(!breturn)
{
hr = HRESULT_FROM_WIN32(GetLastError());
if(hr != HRESULT_FROM_WIN32(ERROR_IO_PENDING))
{
error_setlasterror();
result = FATAL_FCACHE_READFILE;
goto Finished;
}
hr = S_OK;
breturn = GetOverlappedResult(hFile, &Overlapped, &readahead, TRUE);
if(!breturn)
{
error_setlasterror();
result = FATAL_FCACHE_READFILE;
goto Finished;
}
}
coffset += readahead;
pvread += readahead;
}
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in read_file_content", result);
}
dprintverbose("end read_file_content");
return result;
}
/* Public methods */
int fcache_create(fcache_context ** ppfcache)
{
int result = NONFATAL;
fcache_context * pfcache = NULL;
dprintverbose("start fcache_create");
_ASSERT(ppfcache != NULL);
*ppfcache = NULL;
pfcache = (fcache_context *)alloc_pemalloc(sizeof(fcache_context));
if(pfcache == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
pfcache->id = gfcacheid++;
pfcache->islocal = 0;
pfcache->cachekey = 0;
pfcache->hinitdone = NULL;
pfcache->maxfsize = 0;
pfcache->memaddr = NULL;
pfcache->header = NULL;
pfcache->pfilemap = NULL;
pfcache->prwlock = NULL;
pfcache->palloc = NULL;
*ppfcache = pfcache;
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in fcache_create", result);
}
dprintverbose("end fcache_create");
return result;
}
void fcache_destroy(fcache_context * pfcache)
{
dprintverbose("start fcache_destroy");
if(pfcache != NULL)
{
alloc_pefree(pfcache);
pfcache = NULL;
}
dprintverbose("end fcache_destroy");
return;
}
int fcache_initialize(fcache_context * pfcache, unsigned short islocal, unsigned short cachekey, unsigned int cachesize, unsigned int maxfsize)
{
int result = NONFATAL;
size_t size = 0;
fcache_header * header = NULL;
unsigned short mapclass = FILEMAP_MAP_SRANDOM;
unsigned short locktype = LOCK_TYPE_SHARED;
unsigned char isfirst = 1;
unsigned char islocked = 0;
unsigned int initmemory = 0;
DWORD ret = 0;
dprintverbose("start fcache_initialize");
_ASSERT(pfcache != NULL);
_ASSERT(cachekey != 0);
_ASSERT(cachesize >= FCACHE_SIZE_MINIMUM && cachesize <= FCACHE_SIZE_MAXIMUM);
_ASSERT(maxfsize >= FILE_SIZE_MINIMUM && maxfsize <= FILE_SIZE_MAXIMUM);
/* Initialize memory map to store code files */
result = filemap_create(&pfcache->pfilemap);
if(FAILED(result))
{
goto Finished;
}
pfcache->cachekey = cachekey;
if(islocal)
{
mapclass = FILEMAP_MAP_LRANDOM;
locktype = LOCK_TYPE_LOCAL;
pfcache->islocal = islocal;
}
/* Create xread xwrite lock for the filecache */
result = lock_create(&pfcache->prwlock);
if(FAILED(result))
{
goto Finished;
}
result = lock_initialize(pfcache->prwlock, "FILECONTENT_CACHE", cachekey, locktype, NULL);
if(FAILED(result))
{
goto Finished;
}
result = utils_create_init_event(pfcache->prwlock->nameprefix, "FCACHE_INIT", &pfcache->hinitdone, &isfirst);
if (FAILED(result))
{
result = FATAL_FCACHE_INIT_EVENT;
goto Finished;
}
islocked = 1;
/* shmfilepath = NULL to use page file for shared memory */
result = filemap_initialize(pfcache->pfilemap, FILEMAP_TYPE_FILECONTENT, cachekey, mapclass, cachesize, isfirst, NULL);
if(FAILED(result))
{
goto Finished;
}
pfcache->memaddr = (char *)pfcache->pfilemap->mapaddr;
size = filemap_getsize(pfcache->pfilemap);
initmemory = (pfcache->pfilemap->existing == 0);
/* Create allocator for filecache segment */
result = alloc_create(&pfcache->palloc);
if(FAILED(result))
{
goto Finished;
}
/* initmemory = 1 for all page file backed shared memory allocators */
result = alloc_initialize(pfcache->palloc, islocal, "FILECONTENT_SEGMENT", cachekey, pfcache->memaddr, size, 1);
if(FAILED(result))
{
goto Finished;
}
/* Get memory for cache header */
pfcache->header = (fcache_header *)alloc_get_cacheheader(pfcache->palloc, sizeof(fcache_header), CACHE_TYPE_FILECONTENT);
if(pfcache->header == NULL)
{
result = FATAL_FCACHE_INITIALIZE;
goto Finished;
}
header = pfcache->header;
/* Initialize the fcache_header if its not initialized already */
if(islocal || isfirst)
{
if (initmemory)
{
/* No need to get a write lock as other processes */
/* are blocked waiting for hinitdone event */
header->mapcount = 1;
header->itemcount = 0;
header->hitcount = 0;
header->misscount = 0;
ReleaseMutex(pfcache->hinitdone);
islocked = 0;
}
}
else
{
/* Increment the mapcount */
InterlockedIncrement(&header->mapcount);
}
/* Keep maxfsize in fcache_context */
pfcache->maxfsize = maxfsize * 1024;
Finished:
if (islocked)
{
ReleaseMutex(pfcache->hinitdone);
islocked = 0;
}
if(FAILED(result))
{
dprintimportant("failure %d in fcache_initialize", result);
if(pfcache->palloc != NULL)
{
alloc_terminate(pfcache->palloc);
alloc_destroy(pfcache->palloc);
pfcache->palloc = NULL;
}
if(pfcache->pfilemap != NULL)
{
filemap_terminate(pfcache->pfilemap);
filemap_destroy(pfcache->pfilemap);
pfcache->pfilemap = NULL;
}
if(pfcache->prwlock != NULL)
{
lock_terminate(pfcache->prwlock);
lock_destroy(pfcache->prwlock);
pfcache->prwlock = NULL;
}
if(pfcache->hinitdone != NULL)
{
CloseHandle(pfcache->hinitdone);
pfcache->hinitdone = NULL;
}
}
dprintverbose("end fcache_initialize");
return result;
}
void fcache_terminate(fcache_context * pfcache)
{
dprintverbose("start fcache_terminate");
if(pfcache != NULL)
{
if(pfcache->header != NULL)
{
InterlockedDecrement(&pfcache->header->mapcount);
pfcache->header = NULL;
}
if(pfcache->palloc != NULL)
{
alloc_terminate(pfcache->palloc);
alloc_destroy(pfcache->palloc);
pfcache->palloc = NULL;
}
if(pfcache->pfilemap != NULL)
{
filemap_terminate(pfcache->pfilemap);
filemap_destroy(pfcache->pfilemap);
pfcache->pfilemap = NULL;
}
if(pfcache->prwlock != NULL)
{
lock_terminate(pfcache->prwlock);
lock_destroy(pfcache->prwlock);
pfcache->prwlock = NULL;
}
if(pfcache->hinitdone != NULL)
{
CloseHandle(pfcache->hinitdone);
pfcache->hinitdone = NULL;
}
}
dprintverbose("end fcache_terminate");
return;
}
int fcache_createval(fcache_context * pfcache, const char * filename, fcache_value ** ppvalue)
{
int result = NONFATAL;
fcache_value * pvalue = NULL;
HANDLE hFile = INVALID_HANDLE_VALUE;
void * buffer = NULL;
unsigned int filesize = 0;
unsigned char * psec = NULL;
unsigned int openflags = 0;
unsigned int attributes = 0;
unsigned int fileflags = 0;
BY_HANDLE_FILE_INFORMATION bhfi = {0};
dprintverbose("start fcache_createval");
_ASSERT(pfcache != NULL);
_ASSERT(filename != NULL);
_ASSERT(ppvalue != NULL);
*ppvalue = NULL;
/* Allocate memory for cache entry in shared memory */
pvalue = (fcache_value *)alloc_smalloc(pfcache->palloc, sizeof(fcache_value));
if(pvalue == NULL)
{
result = FATAL_OUT_OF_SMEMORY;
goto Finished;
}
pvalue->file_sec = 0;
pvalue->file_content = 0;
/* Open the file in shared read mode */
openflags |= FILE_ATTRIBUTE_ENCRYPTED;
openflags |= FILE_FLAG_OVERLAPPED;
openflags |= FILE_FLAG_BACKUP_SEMANTICS;
openflags |= FILE_FLAG_SEQUENTIAL_SCAN;
hFile = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, openflags, NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
error_setlasterror();
result = FATAL_FCACHE_CREATEFILE;
goto Finished;
}
if(!GetFileInformationByHandle(hFile, &bhfi))
{
result = FATAL_FCACHE_BYHANDLE_INFO;
goto Finished;
}
attributes = bhfi.dwFileAttributes;
if(attributes & FILE_ATTRIBUTE_DIRECTORY)
{
fileflags |= FILE_IS_FOLDER;
}
else
{
/* Dont cache files bigger than maxfsize */
if(bhfi.nFileSizeHigh > 0 || bhfi.nFileSizeLow > pfcache->maxfsize)
{
result = WARNING_FCACHE_TOOBIG;
goto Finished;
}
/* File size is just the low file size */
filesize = bhfi.nFileSizeLow;
/* Allocate memory for entire file in shared memory */
buffer = alloc_smalloc(pfcache->palloc, filesize);
if(buffer == NULL)
{
result = FATAL_OUT_OF_SMEMORY;
goto Finished;
}
/* If filesize is greater than 0, read file content */
if(filesize > 0)
{
result = read_file_content(hFile, filesize, &buffer);
if(FAILED(result))
{
goto Finished;
}
}
}
/* Read security information */
result = read_file_security(pfcache, filename, &psec);
if(FAILED(result))
{
goto Finished;
}
fileflags |= FILE_IS_RUNAWARE;
fileflags |= FILE_IS_WUNAWARE;
pvalue->file_size = filesize;
pvalue->file_sec = psec - pfcache->memaddr;
pvalue->file_content = ((buffer == NULL) ? 0 : ((char *)buffer - pfcache->memaddr));
pvalue->file_flags = fileflags;
pvalue->is_deleted = 0;
pvalue->hitcount = 0;
pvalue->refcount = 0;
InterlockedIncrement(&pfcache->header->itemcount);
InterlockedIncrement(&pfcache->header->misscount);
/* Decrement the hitcounts here. They will be incremented*/
/* back in refinc so that hitcount remains the same */
InterlockedDecrement(&pfcache->header->hitcount);
InterlockedDecrement(&pvalue->hitcount);
*ppvalue = pvalue;
_ASSERT(SUCCEEDED(result));
Finished:
if(hFile != INVALID_HANDLE_VALUE)
{
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
if(FAILED(result))
{
dprintverbose("failure %d in fcache_createval", result);
if(buffer != NULL)
{
alloc_sfree(pfcache->palloc, buffer);
buffer = NULL;
}
if(psec != NULL)
{
alloc_sfree(pfcache->palloc, psec);
psec = NULL;
}
if(pvalue != NULL)
{
pvalue->file_sec = 0;
pvalue->file_content = 0;
alloc_sfree(pfcache->palloc, pvalue);
pvalue = NULL;
}
}
dprintverbose("end fcache_createval");
return result;
}
void fcache_destroyval(fcache_context * pfcache, fcache_value * pvalue)
{
dprintverbose("start fcache_destroyval");
_ASSERT(pvalue->refcount == 0);
if(pvalue != NULL)
{
if(pvalue->file_sec != 0)
{
alloc_sfree(pfcache->palloc, pfcache->memaddr + pvalue->file_sec);
pvalue->file_sec = 0;
}
if(pvalue->file_content != 0)
{
alloc_sfree(pfcache->palloc, pfcache->memaddr + pvalue->file_content);
pvalue->file_content = 0;
}
alloc_sfree(pfcache->palloc, pvalue);
pvalue = NULL;
InterlockedDecrement(&pfcache->header->itemcount);
}
dprintverbose("end fcache_destroyval");
return;
}
int fcache_useval(fcache_context * pcache, const char * filename, fcache_value * pvalue, zend_file_handle ** pphandle)
{
int result = NONFATAL;
int allocated = 0;
zend_file_handle * phandle = NULL;
fcache_handle * fhandle = NULL;
dprintverbose("start fcache_useval");
_ASSERT(filename != NULL);
_ASSERT(pvalue != NULL);
_ASSERT(pphandle != NULL);
if(*pphandle == NULL)
{
phandle = (zend_file_handle *)alloc_emalloc(sizeof(zend_file_handle));
if(phandle == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
dprintverbose("alloc'd new zend_file_handle %p", phandle);
allocated = 1;
ZeroMemory(phandle, sizeof(zend_file_handle));
}
else
{
phandle = *pphandle;
dprintverbose("using passed in zend_file_handle %p", phandle);
#if ZEND_MODULE_API_NO >= 20190902 /* PHP 7.4 release */
/* It's a stream handle, so we need to zero out the buf & len values */
phandle->buf = 0;
phandle->len = 0;
#endif
}
/* Allocate memory for fcache_handle. Release memory in fcache_closer */
fhandle = (fcache_handle *)alloc_emalloc(sizeof(fcache_handle));
if(fhandle == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
phandle->opened_path = zend_string_init(filename, strlen(filename), 0);
#if ZEND_MODULE_API_NO >= 20210902 /* PHP 8.1 release */
phandle->filename = zend_string_copy(phandle->opened_path);
#else
phandle->filename = (char *)filename;
phandle->free_filename = 0;
#endif
ZeroMemory(&phandle->handle.stream, sizeof(zend_stream));
phandle->handle.stream.reader = (zend_stream_reader_t)fcache_reader;
phandle->handle.stream.closer = (zend_stream_closer_t)fcache_closer;
phandle->handle.stream.fsizer = (zend_stream_fsizer_t)fcache_fsizer;
fhandle->pfcache = pcache;
fhandle->pfvalue = pvalue;
fhandle->map = (void *)(pcache->memaddr + pvalue->file_content);
fhandle->buf = pcache->memaddr + pvalue->file_content;
fhandle->len = pvalue->file_size;
fhandle->pos = 0;
/* Init the php_handle in the fcache_handle object */
fcache_init_php_handle(fhandle);
phandle->handle.stream.handle = fhandle;
phandle->type = ZEND_HANDLE_STREAM;
if (allocated == 1)
{
*pphandle = phandle;
}
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in fcache_useval", result);
if(fhandle != NULL)
{
alloc_efree(fhandle);
fhandle = NULL;
}
if(phandle != NULL && allocated == 1)
{
alloc_efree(phandle);
phandle = NULL;
}
}
dprintverbose("end fcache_useval");
return result;
}
fcache_value * fcache_getvalue(fcache_context * pfcache, size_t offset)
{
_ASSERT(pfcache != NULL);
return ((offset != 0) ? ((fcache_value *)(pfcache->memaddr + offset)) : NULL);
}
size_t fcache_getoffset(fcache_context * pfcache, fcache_value * pvalue)
{
_ASSERT(pfcache != NULL);
return ((pvalue != NULL) ? ((char *)pvalue - pfcache->memaddr) : 0);
}
void fcache_refinc(fcache_context * pfcache, fcache_value * pvalue)
{
_ASSERT(pfcache != NULL);
_ASSERT(pvalue != NULL);
InterlockedIncrement(&pvalue->refcount);
InterlockedIncrement(&pvalue->hitcount);
InterlockedIncrement(&pfcache->header->hitcount);
return;
}
void fcache_refdec(fcache_context * pfcache, fcache_value * pvalue)
{
_ASSERT(pfcache != NULL);
_ASSERT(pvalue != NULL);
InterlockedDecrement(&pvalue->refcount);
if(InterlockedCompareExchange(&pvalue->is_deleted, 1, 1) == 1 &&
InterlockedCompareExchange(&pvalue->refcount, 0, 0) == 0)
{
fcache_destroyval(pfcache, pvalue);
pvalue = NULL;
}
return;
}
int fcache_getinfo(fcache_value * pvalue, fcache_entry_info ** ppinfo)
{
int result = NONFATAL;
fcache_entry_info * pinfo = NULL;
dprintverbose("start fcache_getinfo");
_ASSERT(pvalue != NULL);
_ASSERT(ppinfo != NULL);
*ppinfo = NULL;
pinfo = (fcache_entry_info *)alloc_emalloc(sizeof(fcache_entry_info));
if(pinfo == NULL)
{
result = FATAL_OUT_OF_LMEMORY;
goto Finished;
}
pinfo->filesize = pvalue->file_size;
pinfo->hitcount = pvalue->hitcount;
*ppinfo = pinfo;
_ASSERT(SUCCEEDED(result));
Finished:
if(FAILED(result))
{
dprintimportant("failure %d in fcache_getinfo", result);
}
dprintverbose("end fcache_getinfo");
return result;
}
void fcache_freeinfo(fcache_entry_info * pinfo)
{
if(pinfo != NULL)
{
alloc_efree(pinfo);
pinfo = NULL;
}
return;
}
size_t fcache_fsizer(void * handle)
{
size_t size = 0;
fcache_handle * fhandle = NULL;
dprintverbose("start fcache_fsizer");
_ASSERT(handle != NULL);
fhandle = (fcache_handle *)handle;
size = fhandle->len;
dprintverbose("end fcache_fsizer");
return size;
}
size_t fcache_reader(void * handle, char * buf, size_t length)
{
size_t size = 0;
fcache_handle * fhandle = NULL;
fcache_value * pvalue = NULL;
size_t bleft = 0;
dprintverbose("start fcache_reader");
_ASSERT(handle != NULL);
_ASSERT(buf != NULL);
fhandle = (fcache_handle *)handle;
pvalue = fhandle->pfvalue;
_ASSERT(fhandle->len == pvalue->file_size);
/* Read either length bytes or the number of bytes left */
bleft = fhandle->len - fhandle->pos;
if(bleft > length)
{
size = length;
}
else
{
size = bleft;
}
/* Copy characters from file buffer to buffer */
/* and update current position and buffer pointer */
memcpy_s(buf, size, fhandle->buf, size);
fhandle->pos += size;
fhandle->buf += size;
dprintverbose("end fcache_reader");
return size;
}
void fcache_closer(void * handle)
{
fcache_handle * fhandle = NULL;
dprintverbose("start fcache_closer");
_ASSERT(handle != NULL);
/* Just decrement the refcount. Scavenger will remove the entry */
fhandle = (fcache_handle *)handle;
fcache_refdec(fhandle->pfcache, fhandle->pfvalue);
/* Free memory allocated for fcache_handle */
alloc_efree(fhandle);
fhandle = handle = NULL;
dprintverbose("end fcache_closer");
return;
}
void fcache_runtest()
{
int result = NONFATAL;
fcache_context * pfcache = NULL;
fcache_value * pvalue = NULL;
unsigned char islocal = 0;
unsigned int cachesize = 32;
unsigned int maxfsize = 250;
char * filename = "testfile.php";
dprintverbose("*** STARTING FCACHE TESTS ***");
result = fcache_create(&pfcache);
if(FAILED(result))
{
goto Finished;
}
result = fcache_initialize(pfcache, islocal, 57, cachesize, maxfsize);
if(FAILED(result))
{
goto Finished;
}
_ASSERT(pfcache->id != 0);
_ASSERT(pfcache->maxfsize == maxfsize * 1024);
_ASSERT(pfcache->header != NULL);
_ASSERT(pfcache->pfilemap != NULL);
_ASSERT(pfcache->prwlock != NULL);
_ASSERT(pfcache->palloc != NULL);
_ASSERT(pfcache->header->itemcount == 0);
_ASSERT(pfcache->header->hitcount == 0);
_ASSERT(pfcache->header->misscount == 0);
Finished:
if(pfcache != NULL)
{
fcache_terminate(pfcache);
fcache_destroy(pfcache);
pfcache = NULL;
}
dprintverbose("*** ENDING FCACHE TESTS ***");
return;
}
/*
* php_stream_ops functions
*/
size_t wincache_stream_write(php_stream *stream, const char *buf, size_t count)
{
/* ignore writes */
return 0;
}
size_t wincache_stream_read(php_stream *stream, char *buf, size_t count)
{
size_t toread = 0;
toread = stream->writepos - stream->readpos;
if (toread > count)
{
toread = count;
}
if (count == 0)
{
return 0;
}
memcpy_s(buf, toread, stream->readbuf + stream->readpos, toread);
stream->readpos += toread;
stream->position = stream->readpos;