-
Notifications
You must be signed in to change notification settings - Fork 7.7k
/
streams.c
executable file
·1871 lines (1522 loc) · 47.4 KB
/
streams.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
/*
+----------------------------------------------------------------------+
| PHP Version 4 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2002 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 2.02 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available at through the world-wide-web at |
| http://www.php.net/license/2_02.txt. |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: |
| Wez Furlong (wez@thebrainroom.com) |
| Borrowed code from: |
| Rasmus Lerdorf <rasmus@lerdorf.on.ca> |
| Jim Winstead <jimw@php.net> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#define _GNU_SOURCE
#include "php.h"
#include "php_globals.h"
#include "php_network.h"
#include "php_open_temporary_file.h"
#include "ext/standard/file.h"
#include "ext/standard/basic_functions.h" /* for BG(mmap_file) (not strictly required) */
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <fcntl.h>
#ifndef MAP_FAILED
#define MAP_FAILED ((void *) -1)
#endif
#define CHUNK_SIZE 8192
#ifdef PHP_WIN32
#define EWOULDBLOCK WSAEWOULDBLOCK
#else
#include "build-defs.h"
#endif
#define STREAM_DEBUG 0
#define STREAM_WRAPPER_PLAIN_FILES ((php_stream_wrapper*)-1)
/* {{{ some macros to help track leaks */
#if ZEND_DEBUG
#define emalloc_rel_orig(size) \
( __php_stream_call_depth == 0 \
? _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_RELAY_CC) \
: _emalloc((size) ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC) )
#define erealloc_rel_orig(ptr, size) \
( __php_stream_call_depth == 0 \
? _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_RELAY_CC) \
: _erealloc((ptr), (size), 0 ZEND_FILE_LINE_CC ZEND_FILE_LINE_ORIG_RELAY_CC) )
#define pemalloc_rel_orig(size, persistent) ((persistent) ? malloc((size)) : emalloc_rel_orig((size)))
#define perealloc_rel_orig(ptr, size, persistent) ((persistent) ? realloc((ptr), (size)) : erealloc_rel_orig((ptr), (size)))
#else
# define pemalloc_rel_orig(size, persistent) pemalloc((size), (persistent))
# define perealloc_rel_orig(ptr, size, persistent) perealloc((ptr), (size), (persistent))
# define emalloc_rel_orig(size) emalloc((size))
#endif
/* }}} */
static HashTable url_stream_wrappers_hash;
/* allocate a new stream for a particular ops */
PHPAPI php_stream *_php_stream_alloc(php_stream_ops *ops, void *abstract, int persistent, const char *mode STREAMS_DC TSRMLS_DC) /* {{{ */
{
php_stream *ret;
ret = (php_stream*) pemalloc_rel_orig(sizeof(php_stream), persistent);
memset(ret, 0, sizeof(php_stream));
#if STREAM_DEBUG
fprintf(stderr, "stream_alloc: %s:%p\n", ops->label, ret);
#endif
ret->ops = ops;
ret->abstract = abstract;
ret->is_persistent = persistent;
ret->rsrc_id = ZEND_REGISTER_RESOURCE(NULL, ret, php_file_le_stream());
strlcpy(ret->mode, mode, sizeof(ret->mode));
return ret;
}
/* }}} */
PHPAPI int _php_stream_free(php_stream *stream, int close_options TSRMLS_DC) /* {{{ */
{
int ret = 1;
#if STREAM_DEBUG
fprintf(stderr, "stream_free: %s:%p in_free=%d opts=%08x\n", stream->ops->label, stream, stream->in_free, close_options);
#endif
if (stream->in_free)
return 1;
stream->in_free++;
_php_stream_flush(stream, 1 TSRMLS_CC);
if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) == 0) {
/* Remove entry from the resource list */
zend_list_delete(stream->rsrc_id);
}
if (close_options & PHP_STREAM_FREE_CALL_DTOR) {
if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FOPENCOOKIE) {
/* calling fclose on an fopencookied stream will ultimately
call this very same function. If we were called via fclose,
the cookie_closer unsets the fclose_stdiocast flags, so
we can be sure that we only reach here when PHP code calls
php_stream_free.
Lets let the cookie code clean it all up.
*/
stream->in_free = 0;
return fclose(stream->stdiocast);
}
ret = stream->ops->close(stream, close_options & PHP_STREAM_FREE_PRESERVE_HANDLE ? 0 : 1 TSRMLS_CC);
stream->abstract = NULL;
/* tidy up any FILE* that might have been fdopened */
if (stream->fclose_stdiocast == PHP_STREAM_FCLOSE_FDOPEN && stream->stdiocast) {
fclose(stream->stdiocast);
stream->stdiocast = NULL;
}
}
if (close_options & PHP_STREAM_FREE_RELEASE_STREAM) {
while (stream->filterhead) {
php_stream_filter_remove_head(stream, 1);
}
if (stream->wrapper && stream->wrapper->wops && stream->wrapper->wops->stream_closer) {
stream->wrapper->wops->stream_closer(stream->wrapper, stream TSRMLS_CC);
stream->wrapper = NULL;
}
if (stream->wrapperdata) {
zval_ptr_dtor(&stream->wrapperdata);
stream->wrapperdata = NULL;
}
#if ZEND_DEBUG
if ((close_options & PHP_STREAM_FREE_RSRC_DTOR) && (stream->__exposed == 0) && (EG(error_reporting) & E_WARNING)) {
/* it leaked: Lets deliberately NOT pefree it so that the memory manager shows it
* as leaked; it will log a warning, but lets help it out and display what kind
* of stream it was. */
char leakbuf[512];
snprintf(leakbuf, sizeof(leakbuf), __FILE__ "(%d) : Stream of type '%s' 0x%08X (path:%s) was not closed\n", __LINE__, stream->ops->label, (unsigned int)stream, stream->__orig_path);
STR_FREE(stream->__orig_path);
# if defined(PHP_WIN32)
OutputDebugString(leakbuf);
# else
fprintf(stderr, leakbuf);
# endif
} else {
STR_FREE(stream->__orig_path);
pefree(stream, stream->is_persistent);
}
#else
pefree(stream, stream->is_persistent);
#endif
}
return ret;
}
/* }}} */
static HashTable stream_filters_hash;
PHPAPI int php_stream_filter_register_factory(const char *filterpattern, php_stream_filter_factory *factory TSRMLS_DC)
{
return zend_hash_add(&stream_filters_hash, (char*)filterpattern, strlen(filterpattern), factory, sizeof(*factory), NULL);
}
PHPAPI int php_stream_filter_unregister_factory(const char *filterpattern TSRMLS_DC)
{
return zend_hash_del(&stream_filters_hash, (char*)filterpattern, strlen(filterpattern));
}
/* We allow very simple pattern matching for filter factories:
* if "charset.utf-8/sjis" is requested, we search first for an exact
* match. If that fails, we try "charset.*".
* This means that we don't need to clog up the hashtable with a zillion
* charsets (for example) but still be able to provide them all as filters */
PHPAPI php_stream_filter *php_stream_filter_create(const char *filtername, const char *filterparams, int filterparamslen, int persistent TSRMLS_DC)
{
php_stream_filter_factory *factory;
php_stream_filter *filter = NULL;
int n;
char *period;
n = strlen(filtername);
if (SUCCESS == zend_hash_find(&stream_filters_hash, (char*)filtername, n, (void**)&factory)) {
filter = factory->create_filter(filtername, filterparams, filterparamslen, persistent TSRMLS_CC);
} else if ((period = strchr(filtername, '.'))) {
/* try a wildcard */
char wildname[128];
PHP_STRLCPY(wildname, filtername, sizeof(wildname) - 1, period-filtername + 1);
strcat(wildname, "*");
if (SUCCESS == zend_hash_find(&stream_filters_hash, wildname, strlen(wildname), (void**)&factory)) {
filter = factory->create_filter(filtername, filterparams, filterparamslen, persistent TSRMLS_CC);
}
}
if (filter == NULL) {
/* TODO: these need correct docrefs */
if (factory == NULL)
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to locate filter \"%s\"", filtername);
else
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to create or locate filter \"%s\"", filtername);
}
return filter;
}
PHPAPI php_stream_filter *_php_stream_filter_alloc(php_stream_filter_ops *fops, void *abstract, int persistent STREAMS_DC TSRMLS_DC)
{
php_stream_filter *filter;
filter = (php_stream_filter*) pemalloc_rel_orig(sizeof(php_stream_filter), persistent);
memset(filter, 0, sizeof(php_stream_filter));
filter->fops = fops;
filter->abstract = abstract;
filter->is_persistent = persistent;
return filter;
}
PHPAPI void php_stream_filter_free(php_stream_filter *filter TSRMLS_DC)
{
if (filter->fops->dtor)
filter->fops->dtor(filter TSRMLS_CC);
pefree(filter, filter->is_persistent);
}
PHPAPI void php_stream_filter_prepend(php_stream *stream, php_stream_filter *filter)
{
filter->next = stream->filterhead;
filter->prev = NULL;
if (stream->filterhead) {
stream->filterhead->prev = filter;
} else {
stream->filtertail = filter;
}
stream->filterhead = filter;
filter->stream = stream;
}
PHPAPI void php_stream_filter_append(php_stream *stream, php_stream_filter *filter)
{
filter->prev = stream->filtertail;
filter->next = NULL;
if (stream->filtertail) {
stream->filtertail->next = filter;
} else {
stream->filterhead = filter;
}
stream->filtertail = filter;
filter->stream = stream;
}
PHPAPI php_stream_filter *php_stream_filter_remove(php_stream *stream, php_stream_filter *filter, int call_dtor TSRMLS_DC)
{
assert(stream == filter->stream);
if (filter->prev) {
filter->prev->next = filter->next;
} else {
stream->filterhead = filter->next;
}
if (filter->next) {
filter->next->prev = filter->prev;
} else {
stream->filtertail = filter->prev;
}
if (call_dtor) {
php_stream_filter_free(filter TSRMLS_CC);
return NULL;
}
return filter;
}
/* {{{ generic stream operations */
PHPAPI size_t _php_stream_read(php_stream *stream, char *buf, size_t size TSRMLS_DC)
{
if (stream->filterhead)
return stream->filterhead->fops->read(stream, stream->filterhead, buf, size TSRMLS_CC);
return stream->ops->read(stream, buf, size TSRMLS_CC);
}
PHPAPI int _php_stream_eof(php_stream *stream TSRMLS_DC)
{
/* we define our stream reading function so that it
must return EOF when an EOF condition occurs, when
working in unbuffered mode and called with these args */
if (stream->filterhead)
return stream->filterhead->fops->eof(stream, stream->filterhead TSRMLS_CC);
return stream->ops->read(stream, NULL, 0 TSRMLS_CC) == EOF ? 1 : 0;
}
PHPAPI int _php_stream_putc(php_stream *stream, int c TSRMLS_DC)
{
unsigned char buf = c;
if (php_stream_write(stream, &buf, 1) > 0) {
return 1;
}
return EOF;
}
PHPAPI int _php_stream_getc(php_stream *stream TSRMLS_DC)
{
char buf;
if (php_stream_read(stream, &buf, 1) > 0) {
return buf & 0xff;
}
return EOF;
}
PHPAPI int _php_stream_puts(php_stream *stream, char *buf TSRMLS_DC)
{
int len;
char newline[2] = "\n"; /* is this OK for Win? */
len = strlen(buf);
if (len > 0 && php_stream_write(stream, buf, len) && php_stream_write(stream, newline, 1)) {
return 1;
}
return 0;
}
PHPAPI int _php_stream_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
{
memset(ssb, 0, sizeof(*ssb));
/* if the stream was wrapped, allow the wrapper to stat it */
if (stream->wrapper && stream->wrapper->wops->stream_stat != NULL) {
return stream->wrapper->wops->stream_stat(stream->wrapper, stream, ssb TSRMLS_CC);
}
/* if the stream doesn't directly support stat-ing, return with failure.
* We could try and emulate this by casting to a FD and fstat-ing it,
* but since the fd might not represent the actual underlying content
* this would give bogus results. */
if (stream->ops->stat == NULL) {
return -1;
}
return stream->ops->stat(stream, ssb TSRMLS_CC);
}
PHPAPI char *_php_stream_gets(php_stream *stream, char *buf, size_t maxlen TSRMLS_DC)
{
if (maxlen == 0)
return NULL;
if (stream->filterhead || stream->ops->gets == NULL) {
/* unbuffered fgets - performance not so good! */
char *c = buf;
/* TODO: look at error returns? */
while (--maxlen > 0 && php_stream_read(stream, buf, 1) == 1 && *buf++ != '\n')
;
*buf = '\0';
return c == buf && maxlen > 0 ? NULL : c;
} else if (stream->ops->gets) {
return stream->ops->gets(stream, buf, maxlen TSRMLS_CC);
}
/* should not happen */
return NULL;
}
PHPAPI int _php_stream_flush(php_stream *stream, int closing TSRMLS_DC)
{
if (stream->filterhead)
stream->filterhead->fops->flush(stream, stream->filterhead, closing TSRMLS_CC);
if (stream->ops->flush) {
return stream->ops->flush(stream TSRMLS_CC);
}
return 0;
}
PHPAPI size_t _php_stream_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
assert(stream);
if (buf == NULL || count == 0 || stream->ops->write == NULL)
return 0;
if (stream->filterhead) {
return stream->filterhead->fops->write(stream, stream->filterhead, buf, count TSRMLS_CC);
} else {
return stream->ops->write(stream, buf, count TSRMLS_CC);
}
}
PHPAPI off_t _php_stream_tell(php_stream *stream TSRMLS_DC)
{
off_t ret = -1;
if (stream->ops->seek) {
ret = stream->ops->seek(stream, 0, SEEK_CUR TSRMLS_CC);
}
return ret;
}
PHPAPI int _php_stream_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
{
if (stream->ops->seek) {
if (stream->filterhead)
stream->filterhead->fops->flush(stream, stream->filterhead, 0 TSRMLS_CC);
return stream->ops->seek(stream, offset, whence TSRMLS_CC);
}
/* emulate forward moving seeks with reads */
if (whence == SEEK_CUR && offset > 0) {
char tmp[1024];
while(offset >= sizeof(tmp)) {
if (php_stream_read(stream, tmp, sizeof(tmp)) == 0)
return -1;
offset -= sizeof(tmp);
}
if (offset) {
if (php_stream_read(stream, tmp, offset) == 0)
return -1;
}
return 0;
}
php_error_docref(NULL TSRMLS_CC, E_WARNING, "streams of type %s do not support seeking", stream->ops->label);
return -1;
}
PHPAPI int _php_stream_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
{
if (stream->ops->set_option)
return stream->ops->set_option(stream, option, value, ptrparam TSRMLS_CC);
return -1;
}
PHPAPI size_t _php_stream_passthru(php_stream * stream STREAMS_DC TSRMLS_DC)
{
size_t bcount = 0;
int ready = 0;
char buf[8192];
#ifdef HAVE_MMAP
int fd;
#endif
#ifdef HAVE_MMAP
if (!php_stream_is(stream, PHP_STREAM_IS_SOCKET)
&& stream->filterhead == NULL
&& SUCCESS == php_stream_cast(stream, PHP_STREAM_AS_FD, (void*)&fd, 0))
{
struct stat sbuf;
off_t off;
void *p;
size_t len;
fstat(fd, &sbuf);
if (sbuf.st_size > sizeof(buf)) {
off = php_stream_tell(stream);
len = sbuf.st_size - off;
p = mmap(0, len, PROT_READ, MAP_SHARED, fd, off);
if (p != (void *) MAP_FAILED) {
BG(mmap_file) = p;
BG(mmap_len) = len;
PHPWRITE(p, len);
BG(mmap_file) = NULL;
munmap(p, len);
bcount += len;
ready = 1;
}
}
}
#endif
if(!ready) {
int b;
while ((b = php_stream_read(stream, buf, sizeof(buf))) > 0) {
PHPWRITE(buf, b);
bcount += b;
}
}
return bcount;
}
PHPAPI size_t _php_stream_copy_to_mem(php_stream *src, char **buf, size_t maxlen, int persistent STREAMS_DC TSRMLS_DC)
{
size_t ret = 0;
char *ptr;
size_t len = 0, max_len;
int step = CHUNK_SIZE;
int min_room = CHUNK_SIZE / 4;
#if HAVE_MMAP
int srcfd;
#endif
if (buf)
*buf = NULL;
if (maxlen == 0)
return 0;
if (maxlen == PHP_STREAM_COPY_ALL)
maxlen = 0;
#if HAVE_MMAP
/* try and optimize the case where we are copying from the start of a plain file.
* We could probably make this work in more situations, but I don't trust the stdio
* buffering layer.
* */
if ( php_stream_is(src, PHP_STREAM_IS_STDIO) &&
src->filterhead == NULL &&
php_stream_tell(src) == 0 &&
SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0))
{
struct stat sbuf;
if (fstat(srcfd, &sbuf) == 0) {
void *srcfile;
#if STREAM_DEBUG
fprintf(stderr, "mmap attempt: maxlen=%d filesize=%d\n", maxlen, sbuf.st_size);
#endif
if (maxlen > sbuf.st_size || maxlen == 0)
maxlen = sbuf.st_size;
#if STREAM_DEBUG
fprintf(stderr, "mmap attempt: will map maxlen=%d\n", maxlen);
#endif
srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0);
if (srcfile != (void*)MAP_FAILED) {
*buf = pemalloc_rel_orig(maxlen + 1, persistent);
if (*buf) {
memcpy(*buf, srcfile, maxlen);
(*buf)[maxlen] = '\0';
ret = maxlen;
}
munmap(srcfile, maxlen);
return ret;
}
}
/* fall through - we might be able to copy in smaller chunks */
}
#endif
ptr = *buf = pemalloc_rel_orig(step, persistent);
max_len = step;
while((ret = php_stream_read(src, ptr, max_len - len))) {
len += ret;
if (len + min_room >= max_len) {
*buf = perealloc_rel_orig(*buf, max_len + step, persistent);
max_len += step;
ptr = *buf + len;
}
}
if (len) {
*buf = perealloc_rel_orig(*buf, len + 1, persistent);
(*buf)[len] = '\0';
} else {
pefree(*buf, persistent);
*buf = NULL;
}
return len;
}
PHPAPI size_t _php_stream_copy_to_stream(php_stream *src, php_stream *dest, size_t maxlen STREAMS_DC TSRMLS_DC)
{
char buf[CHUNK_SIZE];
size_t readchunk;
size_t haveread = 0;
size_t didread;
#if HAVE_MMAP
int srcfd;
#endif
if (maxlen == 0)
return 0;
if (maxlen == PHP_STREAM_COPY_ALL)
maxlen = 0;
#if HAVE_MMAP
/* try and optimize the case where we are copying from the start of a plain file.
* We could probably make this work in more situations, but I don't trust the stdio
* buffering layer.
* */
if ( php_stream_is(src, PHP_STREAM_IS_STDIO) &&
src->filterhead == NULL &&
php_stream_tell(src) == 0 &&
SUCCESS == php_stream_cast(src, PHP_STREAM_AS_FD, (void**)&srcfd, 0))
{
struct stat sbuf;
if (fstat(srcfd, &sbuf) == 0) {
void *srcfile;
/* in the event that the source file is 0 bytes, return 1 to indicate success
* because opening the file to write had already created a copy */
if(sbuf.st_size ==0)
return 1;
if (maxlen > sbuf.st_size || maxlen == 0)
maxlen = sbuf.st_size;
srcfile = mmap(NULL, maxlen, PROT_READ, MAP_SHARED, srcfd, 0);
if (srcfile != (void*)MAP_FAILED) {
haveread = php_stream_write(dest, srcfile, maxlen);
munmap(srcfile, maxlen);
return haveread;
}
}
/* fall through - we might be able to copy in smaller chunks */
}
#endif
while(1) {
readchunk = sizeof(buf);
if (maxlen && (maxlen - haveread) < readchunk)
readchunk = maxlen - haveread;
didread = php_stream_read(src, buf, readchunk);
if (didread) {
/* extra paranoid */
size_t didwrite, towrite;
char *writeptr;
towrite = didread;
writeptr = buf;
haveread += didread;
while(towrite) {
didwrite = php_stream_write(dest, writeptr, towrite);
if (didwrite == 0)
return 0; /* error */
towrite -= didwrite;
writeptr += didwrite;
}
} else {
if (maxlen == 0) {
return haveread;
} else {
return 0; /* error */
}
}
if (maxlen - haveread == 0) {
break;
}
}
return haveread;
}
/* }}} */
/* {{{ ------- STDIO stream implementation -------*/
typedef struct {
FILE *file;
int is_process_pipe; /* use pclose instead of fclose */
int is_pipe; /* don't try and seek */
#if HAVE_FLUSHIO
char last_op;
#endif
} php_stdio_stream_data;
PHPAPI php_stream *_php_stream_fopen_temporary_file(const char *dir, const char *pfx, char **opened_path STREAMS_DC TSRMLS_DC)
{
FILE *fp = php_open_temporary_file(dir, pfx, opened_path TSRMLS_CC);
if (fp) {
php_stream *stream = php_stream_fopen_from_file_rel(fp, "wb");
if (stream) {
return stream;
}
fclose(fp);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "unable to allocate stream");
return NULL;
}
return NULL;
}
PHPAPI php_stream *_php_stream_fopen_tmpfile(int dummy STREAMS_DC TSRMLS_DC)
{
FILE *fp;
php_stream *stream;
fp = tmpfile();
if (fp == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "tmpfile(): %s", strerror(errno));
return NULL;
}
stream = php_stream_fopen_from_file_rel(fp, "r+");
if (stream == NULL) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "tmpfile(): %s", strerror(errno));
fclose(fp);
return NULL;
}
return stream;
}
PHPAPI php_stream *_php_stream_fopen_from_file(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
{
php_stdio_stream_data *self;
#ifdef S_ISFIFO
int fd;
#endif
self = emalloc_rel_orig(sizeof(*self));
self->file = file;
self->is_pipe = 0;
self->is_process_pipe = 0;
#ifdef S_ISFIFO
/* detect if this is a pipe */
fd = fileno(file);
if (fd >= 0) {
struct stat sb;
self->is_pipe = (fstat(fd, &sb) == 0 && S_ISFIFO(sb.st_mode)) ? 1 : 0;
}
#endif
return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
}
PHPAPI php_stream *_php_stream_fopen_from_pipe(FILE *file, const char *mode STREAMS_DC TSRMLS_DC)
{
php_stdio_stream_data *self;
self = emalloc_rel_orig(sizeof(*self));
self->file = file;
self->is_pipe = 1;
self->is_process_pipe = 1;
return php_stream_alloc_rel(&php_stream_stdio_ops, self, 0, mode);
}
static size_t php_stdiop_write(php_stream *stream, const char *buf, size_t count TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
#if HAVE_FLUSHIO
if (!data->is_pipe && data->last_op == 'r') {
fseek(data->file, 0, SEEK_CUR);
}
data->last_op = 'w';
#endif
return fwrite(buf, 1, count, data->file);
}
static size_t php_stdiop_read(php_stream *stream, char *buf, size_t count TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
if (buf == NULL && count == 0) {
/* check for EOF condition */
if (feof(data->file)) {
return EOF;
}
return 0;
}
#if HAVE_FLUSHIO
if (!data->is_pipe && data->last_op == 'w')
fseek(data->file, 0, SEEK_CUR);
data->last_op = 'r';
#endif
return fread(buf, 1, count, data->file);
}
static int php_stdiop_close(php_stream *stream, int close_handle TSRMLS_DC)
{
int ret;
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
if (close_handle) {
if (data->is_process_pipe) {
ret = pclose(data->file);
} else {
ret = fclose(data->file);
}
} else {
ret = 0;
}
/* STDIO streams are never persistent! */
efree(data);
return ret;
}
static int php_stdiop_flush(php_stream *stream TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
return fflush(data->file);
}
static int php_stdiop_seek(php_stream *stream, off_t offset, int whence TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
if (data->is_pipe) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "cannot seek on a pipe");
return -1;
}
if (offset == 0 && whence == SEEK_CUR)
return ftell(data->file);
return fseek(data->file, offset, whence);
}
static char *php_stdiop_gets(php_stream *stream, char *buf, size_t size TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*)stream->abstract;
assert(data != NULL);
#if HAVE_FLUSHIO
if (!data->is_pipe && data->last_op == 'w') {
fseek(data->file, 0, SEEK_CUR);
}
data->last_op = 'r';
#endif
return fgets(buf, size, data->file);
}
static int php_stdiop_cast(php_stream *stream, int castas, void **ret TSRMLS_DC)
{
int fd;
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
assert(data != NULL);
switch (castas) {
case PHP_STREAM_AS_STDIO:
if (ret) {
*ret = data->file;
}
return SUCCESS;
case PHP_STREAM_AS_FD:
fd = fileno(data->file);
if (fd < 0) {
return FAILURE;
}
if (ret) {
fflush(data->file);
*ret = (void*)fd;
}
return SUCCESS;
default:
return FAILURE;
}
}
static int php_stdiop_stat(php_stream *stream, php_stream_statbuf *ssb TSRMLS_DC)
{
int fd;
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
assert(data != NULL);
fd = fileno(data->file);
return fstat(fd, &ssb->sb);
}
static int php_stdiop_set_option(php_stream *stream, int option, int value, void *ptrparam TSRMLS_DC)
{
php_stdio_stream_data *data = (php_stdio_stream_data*) stream->abstract;
size_t size;
int fd;
#ifdef O_NONBLOCK
/* FIXME: make this work for win32 */
int flags;
int oldval;
#endif
switch(option) {
case PHP_STREAM_OPTION_BLOCKING:
fd = fileno(data->file);
if (fd == -1)
return -1;
#ifdef O_NONBLOCK
flags = fcntl(fd, F_GETFL, 0);
oldval = (flags & O_NONBLOCK) ? 0 : 1;
if (value)
flags ^= O_NONBLOCK;
else
flags |= O_NONBLOCK;
if (-1 == fcntl(fd, F_SETFL, flags))
return -1;
return oldval;
#else
return -1; /* not yet implemented */
#endif
case PHP_STREAM_OPTION_BUFFER:
if (ptrparam)
size = *(size_t *)ptrparam;
else
size = BUFSIZ;
switch(value) {
case PHP_STREAM_BUFFER_NONE:
return setvbuf(data->file, NULL, _IONBF, 0);
case PHP_STREAM_BUFFER_LINE:
return setvbuf(data->file, NULL, _IOLBF, size);
case PHP_STREAM_BUFFER_FULL:
return setvbuf(data->file, NULL, _IOFBF, size);
default:
return -1;
}
break;
default:
return -1;
}
}
PHPAPI php_stream_ops php_stream_stdio_ops = {
php_stdiop_write, php_stdiop_read,
php_stdiop_close, php_stdiop_flush,
"STDIO",
php_stdiop_seek,
php_stdiop_gets, php_stdiop_cast,
php_stdiop_stat,
php_stdiop_set_option
};
/* }}} */
/* {{{ php_stream_fopen_with_path */
PHPAPI php_stream *_php_stream_fopen_with_path(char *filename, char *mode, char *path, char **opened_path STREAMS_DC TSRMLS_DC)
{