forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spl_directory.c
2783 lines (2339 loc) · 79.8 KB
/
spl_directory.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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Marcus Boerger <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "php.h"
#include "ext/standard/file.h"
#include "ext/standard/php_filestat.h"
#include "ext/standard/flock_compat.h"
#include "ext/standard/scanf.h"
#include "ext/standard/php_string.h" /* For php_basename() */
#include "zend_attributes.h"
#include "zend_exceptions.h"
#include "zend_interfaces.h"
#include "spl_iterators.h"
#include "spl_directory.h"
#include "spl_directory_arginfo.h"
#include "spl_exceptions.h"
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
#define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
/* declare the class handlers */
static zend_object_handlers spl_filesystem_object_handlers;
/* includes handler to validate object state when retrieving methods */
static zend_object_handlers spl_filesystem_object_check_handlers;
/* decalre the class entry */
PHPAPI zend_class_entry *spl_ce_SplFileInfo;
PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
PHPAPI zend_class_entry *spl_ce_GlobIterator;
PHPAPI zend_class_entry *spl_ce_SplFileObject;
PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
/* Object helper */
static inline spl_filesystem_object *spl_filesystem_from_obj(zend_object *obj) /* {{{ */ {
return (spl_filesystem_object*)((char*)(obj) - XtOffsetOf(spl_filesystem_object, std));
}
/* }}} */
/* define an overloaded iterator structure */
typedef struct {
zend_object_iterator intern;
zval current;
void *object;
} spl_filesystem_iterator;
static inline spl_filesystem_iterator* spl_filesystem_object_to_iterator(spl_filesystem_object *obj)
{
spl_filesystem_iterator *it;
it = ecalloc(1, sizeof(spl_filesystem_iterator));
it->object = (void *)obj;
zend_iterator_init(&it->intern);
return it;
}
static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_filesystem_iterator *it)
{
return (spl_filesystem_object*)it->object;
}
#define CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(spl_filesystem_object_pointer) \
if (!(spl_filesystem_object_pointer)->u.file.stream) { \
zend_throw_error(NULL, "Object not initialized"); \
RETURN_THROWS(); \
}
#define CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern) \
if (!(intern)->u.dir.dirp) { \
zend_throw_error(NULL, "Object not initialized"); \
RETURN_THROWS(); \
}
static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
{
if (intern->u.file.current_line) {
zend_string_release_ex(intern->u.file.current_line, /* persistent */ false);
intern->u.file.current_line = NULL;
}
if (!Z_ISUNDEF(intern->u.file.current_zval)) {
zval_ptr_dtor(&intern->u.file.current_zval);
ZVAL_UNDEF(&intern->u.file.current_zval);
}
} /* }}} */
static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
{
spl_filesystem_object *intern = spl_filesystem_from_obj(object);
zend_objects_destroy_object(object);
switch(intern->type) {
case SPL_FS_DIR:
if (intern->u.dir.dirp) {
php_stream_close(intern->u.dir.dirp);
intern->u.dir.dirp = NULL;
}
break;
case SPL_FS_FILE:
if (intern->u.file.stream) {
/*
if (intern->u.file.zcontext) {
zend_list_delref(Z_RESVAL_P(intern->zcontext));
}
*/
if (!intern->u.file.stream->is_persistent) {
php_stream_close(intern->u.file.stream);
} else {
php_stream_pclose(intern->u.file.stream);
}
intern->u.file.stream = NULL;
ZVAL_UNDEF(&intern->u.file.zresource);
}
break;
default:
break;
}
} /* }}} */
static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
{
spl_filesystem_object *intern = spl_filesystem_from_obj(object);
if (intern->oth_handler && intern->oth_handler->dtor) {
intern->oth_handler->dtor(intern);
}
zend_object_std_dtor(&intern->std);
if (intern->path) {
zend_string_release(intern->path);
}
if (intern->file_name) {
zend_string_release(intern->file_name);
}
switch(intern->type) {
case SPL_FS_INFO:
break;
case SPL_FS_DIR:
if (intern->u.dir.sub_path) {
zend_string_release(intern->u.dir.sub_path);
}
break;
case SPL_FS_FILE:
if (intern->u.file.open_mode) {
zend_string_release(intern->u.file.open_mode);
}
if (intern->orig_path) {
zend_string_release(intern->orig_path);
}
spl_filesystem_file_free_line(intern);
break;
}
} /* }}} */
/* {{{ spl_ce_dir_object_new */
/* creates the object by
- allocating memory
- initializing the object members
- storing the object
- setting it's handlers
called from
- clone
- new
*/
static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
{
spl_filesystem_object *intern;
intern = emalloc(sizeof(spl_filesystem_object) + zend_object_properties_size(class_type));
/* Avoid initializing the entirety of spl_filesystem_object.u.dir.entry. */
memset(intern, 0,
MAX(XtOffsetOf(spl_filesystem_object, u.dir.entry),
XtOffsetOf(spl_filesystem_object, u.file.escape) + sizeof(int)));
/* intern->type = SPL_FS_INFO; done by set 0 */
intern->file_class = spl_ce_SplFileObject;
intern->info_class = spl_ce_SplFileInfo;
zend_object_std_init(&intern->std, class_type);
object_properties_init(&intern->std, class_type);
return &intern->std;
}
/* }}} */
PHPAPI zend_string *spl_filesystem_object_get_path(const spl_filesystem_object *intern) /* {{{ */
{
#ifdef HAVE_GLOB
if (intern->type == SPL_FS_DIR && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops)) {
size_t len = 0;
char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
if (len == 0) {
return NULL;
}
return zend_string_init(tmp, len, /* persistent */ false);
}
#endif
if (!intern->path) {
return NULL;
}
return zend_string_copy(intern->path);
} /* }}} */
static zend_result spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
{
if (intern->file_name) {
/* already known */
return SUCCESS;
}
switch (intern->type) {
case SPL_FS_INFO:
case SPL_FS_FILE:
zend_throw_error(NULL, "Object not initialized");
return FAILURE;
case SPL_FS_DIR: {
size_t name_len;
zend_string *path;
char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
path = spl_filesystem_object_get_path(intern);
/* if there is parent path, amend it, otherwise just use the given path as is */
name_len = strlen(intern->u.dir.entry.d_name);
if (!path) {
intern->file_name = zend_string_init(intern->u.dir.entry.d_name, name_len, 0);
return SUCCESS;
}
ZEND_ASSERT(ZSTR_LEN(path) != 0);
intern->file_name = zend_string_concat3(
ZSTR_VAL(path), ZSTR_LEN(path), &slash, 1, intern->u.dir.entry.d_name, name_len);
zend_string_release_ex(path, /* persistent */ false);
break;
}
}
return SUCCESS;
} /* }}} */
/* TODO Make void or have callers check return value */
static bool spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
{
if (intern->file_name) {
/* invalidate */
zend_string_release(intern->file_name);
intern->file_name = NULL;
}
if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
intern->u.dir.entry.d_name[0] = '\0';
return 0;
} else {
return 1;
}
}
/* }}} */
#define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
static inline bool spl_filesystem_is_dot(const char * d_name) /* {{{ */
{
return !strcmp(d_name, ".") || !strcmp(d_name, "..");
}
/* }}} */
/* {{{ spl_filesystem_dir_open */
/* open a directory resource
* Can emit an E_WARNING as it reports errors from php_stream_opendir() */
static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string *path)
{
bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
intern->type = SPL_FS_DIR;
intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context));
if (ZSTR_LEN(path) > 1 && IS_SLASH_AT(ZSTR_VAL(path), ZSTR_LEN(path)-1)) {
intern->path = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path)-1, 0);
} else {
intern->path = zend_string_copy(path);
}
intern->u.dir.index = 0;
if (EG(exception) || intern->u.dir.dirp == NULL) {
intern->u.dir.entry.d_name[0] = '\0';
if (!EG(exception)) {
/* open failed w/out notice (turned to exception due to EH_THROW) */
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
"Failed to open directory \"%s\"", ZSTR_VAL(path));
}
} else {
do {
spl_filesystem_dir_read(intern);
} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
}
}
/* }}} */
/* Can generate E_WARNINGS as we report errors from stream initialized via
* php_stream_open_wrapper_ex() */
static zend_result spl_filesystem_file_open(spl_filesystem_object *intern, bool use_include_path) /* {{{ */
{
zval tmp;
intern->type = SPL_FS_FILE;
php_stat(intern->file_name, FS_IS_DIR, &tmp);
if (Z_TYPE(tmp) == IS_TRUE) {
zend_string_release(intern->u.file.open_mode);
intern->u.file.open_mode = NULL;
intern->file_name = NULL;
zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
return FAILURE;
}
intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
intern->u.file.stream = php_stream_open_wrapper_ex(ZSTR_VAL(intern->file_name), ZSTR_VAL(intern->u.file.open_mode), (use_include_path ? USE_PATH : 0) | REPORT_ERRORS, NULL, intern->u.file.context);
if (!ZSTR_LEN(intern->file_name) || !intern->u.file.stream) {
if (!EG(exception)) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", ZSTR_VAL(intern->file_name));
}
zend_string_release(intern->u.file.open_mode);
intern->u.file.open_mode = NULL;
intern->file_name = NULL; /* until here it is not a copy */
return FAILURE;
}
/* prevent closing the stream outside of SplFileObject */
intern->u.file.stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
/*
if (intern->u.file.zcontext) {
//zend_list_addref(Z_RES_VAL(intern->u.file.zcontext));
Z_ADDREF_P(intern->u.file.zcontext);
}
*/
if (ZSTR_LEN(intern->file_name) > 1 && IS_SLASH_AT(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name)-1)) {
intern->file_name = zend_string_init(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name)-1, 0);
} else {
intern->file_name = zend_string_copy(intern->file_name);
}
intern->orig_path = zend_string_init(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path), 0);
/* avoid reference counting in debug mode, thus do it manually */
ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
/*!!! TODO: maybe bug?
Z_SET_REFCOUNT(intern->u.file.zresource, 1);
*/
intern->u.file.delimiter = ',';
intern->u.file.enclosure = '"';
intern->u.file.escape = (unsigned char) '\\';
intern->u.file.is_escape_default = true;
intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
return SUCCESS;
} /* }}} */
/* {{{ spl_filesystem_object_clone */
/* Local zend_object creation (on stack)
Load the 'other' object
Create a new empty object (See spl_filesystem_object_new)
Open the directory
Clone other members (properties)
*/
static zend_object *spl_filesystem_object_clone(zend_object *old_object)
{
zend_object *new_object;
spl_filesystem_object *intern;
spl_filesystem_object *source;
source = spl_filesystem_from_obj(old_object);
new_object = spl_filesystem_object_new(old_object->ce);
intern = spl_filesystem_from_obj(new_object);
intern->flags = source->flags;
switch (source->type) {
case SPL_FS_INFO:
if (source->path != NULL) {
intern->path = zend_string_copy(source->path);
}
if (source->file_name != NULL) {
intern->file_name = zend_string_copy(source->file_name);
}
break;
case SPL_FS_DIR: {
spl_filesystem_dir_open(intern, source->path);
/* read until we hit the position in which we were before */
bool skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
int index;
for (index = 0; index < source->u.dir.index; ++index) {
do {
spl_filesystem_dir_read(intern);
} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
}
intern->u.dir.index = index;
break;
}
case SPL_FS_FILE:
ZEND_UNREACHABLE();
}
intern->file_class = source->file_class;
intern->info_class = source->info_class;
intern->oth = source->oth;
intern->oth_handler = source->oth_handler;
zend_objects_clone_members(new_object, old_object);
if (intern->oth_handler && intern->oth_handler->clone) {
intern->oth_handler->clone(source, intern);
}
return new_object;
}
/* }}} */
static void spl_filesystem_info_set_filename(spl_filesystem_object *intern, zend_string *path) /* {{{ */
{
size_t path_len;
if (intern->file_name) {
zend_string_release(intern->file_name);
}
path_len = ZSTR_LEN(path);
if (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
do {
path_len--;
} while (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len - 1));
intern->file_name = zend_string_init(ZSTR_VAL(path), path_len, 0);
} else {
intern->file_name = zend_string_copy(path);
}
while (path_len > 1 && !IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
path_len--;
}
if (path_len) {
path_len--;
}
if (intern->path) {
zend_string_release(intern->path);
}
intern->path = zend_string_init(ZSTR_VAL(path), path_len, 0);
} /* }}} */
// TODO Do not pass return_value pointer but actually use value returned by function at call site?
static spl_filesystem_object *spl_filesystem_object_create_info(zend_string *file_path, zend_class_entry *ce, zval *return_value) /* {{{ */
{
spl_filesystem_object *intern;
zval arg1;
ZEND_ASSERT(file_path && ZSTR_LEN(file_path) > 0);
ZEND_ASSERT(ce != NULL);
intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
RETVAL_OBJ(&intern->std);
if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
ZVAL_STR_COPY(&arg1, file_path);
zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
zval_ptr_dtor(&arg1);
} else {
spl_filesystem_info_set_filename(intern, file_path);
}
return intern;
} /* }}} */
static spl_filesystem_object *spl_filesystem_object_create_type(int num_args, spl_filesystem_object *source, int type, zend_class_entry *ce, zval *return_value) /* {{{ */
{
spl_filesystem_object *intern;
bool use_include_path = 0;
zval arg1, arg2;
zend_error_handling error_handling;
switch (source->type) {
case SPL_FS_INFO:
case SPL_FS_FILE:
break;
case SPL_FS_DIR:
if (!source->u.dir.entry.d_name[0]) {
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
return NULL;
}
}
switch (type) {
case SPL_FS_INFO:
ce = ce ? ce : source->info_class;
intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
RETVAL_OBJ(&intern->std);
if (spl_filesystem_object_get_file_name(source) == FAILURE) {
return NULL;
}
if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
ZVAL_STR_COPY(&arg1, source->file_name);
zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
zval_ptr_dtor(&arg1);
} else {
intern->file_name = zend_string_copy(source->file_name);
intern->path = spl_filesystem_object_get_path(source);
}
break;
case SPL_FS_FILE:
{
ce = ce ? ce : source->file_class;
zend_string *open_mode = ZSTR_CHAR('r');
zval *resource = NULL;
if (zend_parse_parameters(num_args, "|Sbr!",
&open_mode, &use_include_path, &resource) == FAILURE
) {
return NULL;
}
intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
RETVAL_OBJ(&intern->std);
if (spl_filesystem_object_get_file_name(source) == FAILURE) {
return NULL;
}
if (ce->constructor->common.scope != spl_ce_SplFileObject) {
ZVAL_STR_COPY(&arg1, source->file_name);
ZVAL_STR_COPY(&arg2, open_mode);
zend_call_method_with_2_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
zval_ptr_dtor(&arg1);
zval_ptr_dtor(&arg2);
} else {
intern->file_name = source->file_name;
intern->path = spl_filesystem_object_get_path(source);
intern->u.file.open_mode = zend_string_copy(open_mode);
intern->u.file.zcontext = resource;
/* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
if (spl_filesystem_file_open(intern, use_include_path) == FAILURE) {
zend_restore_error_handling(&error_handling);
zval_ptr_dtor(return_value);
ZVAL_NULL(return_value);
return NULL;
}
zend_restore_error_handling(&error_handling);
}
break;
}
case SPL_FS_DIR:
zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
return NULL;
}
return NULL;
} /* }}} */
static bool spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
{
return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
}
/* }}} */
static zend_string *spl_filesystem_object_get_pathname(spl_filesystem_object *intern) { /* {{{ */
switch (intern->type) {
case SPL_FS_INFO:
case SPL_FS_FILE:
return intern->file_name;
case SPL_FS_DIR:
if (intern->u.dir.entry.d_name[0]) {
spl_filesystem_object_get_file_name(intern);
return intern->file_name;
}
}
return NULL;
}
/* }}} */
static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *object) /* {{{ */
{
spl_filesystem_object *intern = spl_filesystem_from_obj(object);
zval tmp;
HashTable *debug_info;
zend_string *path_name;
// TODO Do zend_new_array() + zend_hash_copy() trick?
debug_info = zend_array_dup(zend_std_get_properties_ex(&intern->std));
path_name = spl_filesystem_object_get_pathname(intern);
if (path_name) {
ZVAL_STR_COPY(&tmp, path_name);
} else {
ZVAL_EMPTY_STRING(&tmp);
}
/* IMPORTANT: Do not free path_name as spl_filesystem_object_get_pathname()
* updates/sets the intern->file_name and returns the pointer to
* intern->file_name which must remain allocated. */
spl_set_private_debug_info_property(spl_ce_SplFileInfo, "pathName", strlen("pathName"), debug_info, &tmp);
if (intern->file_name) {
zend_string *path = spl_filesystem_object_get_path(intern);
if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
/* +1 to skip the trailing / of the path in the file name */
ZVAL_STRINGL(&tmp, ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1, ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1));
} else {
ZVAL_STR_COPY(&tmp, intern->file_name);
}
if (path) {
zend_string_release_ex(path, /* persistent */ false);
}
spl_set_private_debug_info_property(spl_ce_SplFileInfo, "fileName", strlen("fileName"), debug_info, &tmp);
}
if (intern->type == SPL_FS_DIR) {
#ifdef HAVE_GLOB
if (intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp ,&php_glob_stream_ops)) {
ZVAL_STR_COPY(&tmp, intern->path);
} else {
ZVAL_FALSE(&tmp);
}
spl_set_private_debug_info_property(spl_ce_DirectoryIterator, "glob", strlen("glob"), debug_info, &tmp);
#endif
if (intern->u.dir.sub_path) {
ZVAL_STR_COPY(&tmp, intern->u.dir.sub_path);
} else {
ZVAL_EMPTY_STRING(&tmp);
}
spl_set_private_debug_info_property(spl_ce_RecursiveDirectoryIterator, "subPathName", strlen("subPathName"), debug_info, &tmp);
}
if (intern->type == SPL_FS_FILE) {
ZVAL_STR_COPY(&tmp, intern->u.file.open_mode);
spl_set_private_debug_info_property(spl_ce_SplFileObject, "openMode", strlen("openMode"), debug_info, &tmp);
ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.delimiter));
spl_set_private_debug_info_property(spl_ce_SplFileObject, "delimiter", strlen("delimiter"), debug_info, &tmp);
ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.enclosure));
spl_set_private_debug_info_property(spl_ce_SplFileObject, "enclosure", strlen("enclosure"), debug_info, &tmp);
}
return debug_info;
}
/* }}} */
static zend_function *spl_filesystem_object_get_method_check(zend_object **object, zend_string *method, const zval *key) /* {{{ */
{
spl_filesystem_object *fsobj = spl_filesystem_from_obj(*object);
if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
return NULL;
}
return zend_std_get_method(object, method, key);
}
/* }}} */
#define DIT_CTOR_FLAGS 0x00000001
#define DIT_CTOR_GLOB 0x00000002
static void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long ctor_flags) /* {{{ */
{
spl_filesystem_object *intern;
zend_string *path;
zend_result parsed;
zend_long flags = (ctor_flags & ~DIT_CTOR_FLAGS);
zend_error_handling error_handling;
if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
flags |= SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &path, &flags);
} else {
flags |= SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "P", &path);
}
if (parsed == FAILURE) {
RETURN_THROWS();
}
if (ZSTR_LEN(path) == 0) {
zend_argument_must_not_be_empty_error(1);
RETURN_THROWS();
}
intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
if (intern->path) {
/* object is already initialized */
zend_throw_error(NULL, "Directory object is already initialized");
RETURN_THROWS();
}
intern->flags = flags;
/* spl_filesystem_dir_open() may emit an E_WARNING */
zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
#ifdef HAVE_GLOB
if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && !zend_string_starts_with_literal(path, "glob://")) {
path = zend_strpprintf(0, "glob://%s", ZSTR_VAL(path));
spl_filesystem_dir_open(intern, path);
zend_string_release(path);
} else
#endif
{
spl_filesystem_dir_open(intern, path);
}
zend_restore_error_handling(&error_handling);
}
/* }}} */
/* {{{ Cronstructs a new dir iterator from a path. */
PHP_METHOD(DirectoryIterator, __construct)
{
spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
}
/* }}} */
/* {{{ Rewind dir back to the start */
PHP_METHOD(DirectoryIterator, rewind)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
intern->u.dir.index = 0;
php_stream_rewinddir(intern->u.dir.dirp);
spl_filesystem_dir_read(intern);
}
/* }}} */
/* {{{ Return current dir entry */
PHP_METHOD(DirectoryIterator, key)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
RETURN_LONG(intern->u.dir.index);
}
/* }}} */
/* {{{ Return this (needed for Iterator interface) */
PHP_METHOD(DirectoryIterator, current)
{
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)));
RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
}
/* }}} */
/* {{{ Move to next entry */
PHP_METHOD(DirectoryIterator, next)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
intern->u.dir.index++;
do {
spl_filesystem_dir_read(intern);
} while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
if (intern->file_name) {
zend_string_release(intern->file_name);
intern->file_name = NULL;
}
}
/* }}} */
/* {{{ Seek to the given position */
PHP_METHOD(DirectoryIterator, seek)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
zval retval;
zend_long pos;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pos) == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
if (intern->u.dir.index > pos) {
/* we first rewind */
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_rewind, "rewind", NULL);
}
while (intern->u.dir.index < pos) {
bool valid = false;
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
valid = zend_is_true(&retval);
zval_ptr_dtor(&retval);
if (!valid) {
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", pos);
RETURN_THROWS();
}
zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_next, "next", NULL);
}
} /* }}} */
/* {{{ Check whether dir contains more entries */
PHP_METHOD(DirectoryIterator, valid)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
}
/* }}} */
/* {{{ Return the path */
PHP_METHOD(SplFileInfo, getPath)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
zend_string *path;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
path = spl_filesystem_object_get_path(intern);
if (path) {
RETURN_STR(path);
} else {
RETURN_EMPTY_STRING();
}
}
/* }}} */
/* {{{ Return filename only */
PHP_METHOD(SplFileInfo, getFilename)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
zend_string *path;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
if (!intern->file_name) {
zend_throw_error(NULL, "Object not initialized");
RETURN_THROWS();
}
path = spl_filesystem_object_get_path(intern);
if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
/* +1 to skip the trailing / of the path in the file name */
size_t path_len = ZSTR_LEN(path) + 1;
RETVAL_STRINGL(ZSTR_VAL(intern->file_name) + path_len, ZSTR_LEN(intern->file_name) - path_len);
} else {
RETVAL_STR_COPY(intern->file_name);
}
if (path) {
zend_string_release_ex(path, /* persistent */ false);
}
}
/* }}} */
/* {{{ Return filename of current dir entry */
PHP_METHOD(DirectoryIterator, getFilename)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
RETURN_STRING(intern->u.dir.entry.d_name);
}
/* }}} */
/* {{{ Returns file extension component of path */
PHP_METHOD(SplFileInfo, getExtension)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
char *fname = NULL;
const char *p;
size_t flen;
zend_string *path;
size_t idx;
zend_string *ret;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
if (!intern->file_name) {
zend_throw_error(NULL, "Object not initialized");
RETURN_THROWS();
}
path = spl_filesystem_object_get_path(intern);
if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
fname = ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1;
flen = ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1);
} else {
fname = ZSTR_VAL(intern->file_name);
flen = ZSTR_LEN(intern->file_name);
}
if (path) {
zend_string_release_ex(path, /* persistent */ false);
}
ret = php_basename(fname, flen, NULL, 0);
p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
if (p) {
idx = p - ZSTR_VAL(ret);
RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
zend_string_release_ex(ret, 0);
return;
} else {
zend_string_release_ex(ret, 0);
RETURN_EMPTY_STRING();
}
}
/* }}}*/
/* {{{ Returns the file extension component of path */
PHP_METHOD(DirectoryIterator, getExtension)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
const char *p;
size_t idx;
zend_string *fname;
if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}
CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0);
p = zend_memrchr(ZSTR_VAL(fname), '.', ZSTR_LEN(fname));
if (p) {
idx = p - ZSTR_VAL(fname);
RETVAL_STRINGL(ZSTR_VAL(fname) + idx + 1, ZSTR_LEN(fname) - idx - 1);
zend_string_release_ex(fname, 0);
} else {
zend_string_release_ex(fname, 0);
RETURN_EMPTY_STRING();
}
}
/* }}} */
/* {{{ Returns filename component of path */
PHP_METHOD(SplFileInfo, getBasename)
{
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
char *fname, *suffix = 0;
size_t flen;
size_t slen = 0;
zend_string *path;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
RETURN_THROWS();
}
if (!intern->file_name) {
zend_throw_error(NULL, "Object not initialized");