Coverage Report

Created: 2026-06-02 06:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/spl/spl_directory.c
Line
Count
Source
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright © The PHP Group and Contributors.                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to the Modified BSD License that is      |
6
   | bundled with this package in the file LICENSE, and is available      |
7
   | through the World Wide Web at <https://www.php.net/license/>.        |
8
   |                                                                      |
9
   | SPDX-License-Identifier: BSD-3-Clause                                |
10
   +----------------------------------------------------------------------+
11
   | Author: Marcus Boerger <helly@php.net>                               |
12
   +----------------------------------------------------------------------+
13
 */
14
15
#ifdef HAVE_CONFIG_H
16
# include "config.h"
17
#endif
18
19
#include "php.h"
20
#include "ext/standard/file.h"
21
#include "ext/standard/php_filestat.h"
22
#include "ext/standard/flock_compat.h"
23
#include "ext/standard/scanf.h"
24
#include "ext/standard/php_string.h" /* For php_basename() */
25
#include "zend_attributes.h"
26
#include "zend_exceptions.h"
27
#include "zend_interfaces.h"
28
29
#include "spl_iterators.h"
30
#include "spl_directory.h"
31
#include "spl_directory_arginfo.h"
32
#include "spl_exceptions.h"
33
#include "spl_functions.h" /* For spl_set_private_debug_info_property() */
34
35
0
#define SPL_HAS_FLAG(flags, test_flag) ((flags & test_flag) ? 1 : 0)
36
37
/* declare the class handlers */
38
static zend_object_handlers spl_filesystem_object_handlers;
39
/* includes handler to validate object state when retrieving methods */
40
static zend_object_handlers spl_filesystem_object_check_handlers;
41
42
/* decalre the class entry */
43
PHPAPI zend_class_entry *spl_ce_SplFileInfo;
44
PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
45
PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
46
PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
47
PHPAPI zend_class_entry *spl_ce_GlobIterator;
48
PHPAPI zend_class_entry *spl_ce_SplFileObject;
49
PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
50
51
0
#define spl_filesystem_from_obj(obj) ZEND_CONTAINER_OF(obj, spl_filesystem_object, std)
52
53
/* define an overloaded iterator structure */
54
typedef struct {
55
  zend_object_iterator  intern;
56
  zval                  current;
57
  void                 *object;
58
} spl_filesystem_iterator;
59
60
static inline spl_filesystem_iterator* spl_filesystem_object_to_iterator(spl_filesystem_object *obj)
61
0
{
62
0
  spl_filesystem_iterator    *it;
63
64
0
  it = ecalloc(1, sizeof(spl_filesystem_iterator));
65
0
  it->object = (void *)obj;
66
0
  zend_iterator_init(&it->intern);
67
0
  return it;
68
0
}
69
70
static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_filesystem_iterator *it)
71
0
{
72
0
  return (spl_filesystem_object*)it->object;
73
0
}
74
75
#define CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(spl_filesystem_object_pointer) \
76
0
  if (!(spl_filesystem_object_pointer)->u.file.stream) { \
77
0
    zend_throw_error(NULL, "Object not initialized"); \
78
0
    RETURN_THROWS(); \
79
0
  }
80
81
#define CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern) \
82
0
  if (!(intern)->u.dir.dirp) { \
83
0
    zend_throw_error(NULL, "Object not initialized"); \
84
0
    RETURN_THROWS(); \
85
0
  }
86
87
static void spl_filesystem_file_free_line(spl_filesystem_object *intern) /* {{{ */
88
0
{
89
0
  if (intern->u.file.current_line) {
90
0
    zend_string_release_ex(intern->u.file.current_line, /* persistent */ false);
91
0
    intern->u.file.current_line = NULL;
92
0
  }
93
0
  if (!Z_ISUNDEF(intern->u.file.current_zval)) {
94
0
    zval_ptr_dtor(&intern->u.file.current_zval);
95
0
    ZVAL_UNDEF(&intern->u.file.current_zval);
96
0
  }
97
0
} /* }}} */
98
99
static void spl_filesystem_object_destroy_object(zend_object *object) /* {{{ */
100
0
{
101
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(object);
102
103
0
  zend_objects_destroy_object(object);
104
105
0
  switch(intern->type) {
106
0
  case SPL_FS_DIR:
107
0
    if (intern->u.dir.dirp) {
108
0
      php_stream_close(intern->u.dir.dirp);
109
0
      intern->u.dir.dirp = NULL;
110
0
    }
111
0
    break;
112
0
  case SPL_FS_FILE:
113
0
    if (intern->u.file.stream) {
114
      /*
115
      if (intern->u.file.zcontext) {
116
         zend_list_delref(Z_RESVAL_P(intern->zcontext));
117
      }
118
      */
119
0
      if (!intern->u.file.stream->is_persistent) {
120
0
        php_stream_close(intern->u.file.stream);
121
0
      } else {
122
0
        php_stream_pclose(intern->u.file.stream);
123
0
      }
124
0
      intern->u.file.stream = NULL;
125
0
      ZVAL_UNDEF(&intern->u.file.zresource);
126
0
    }
127
0
    break;
128
0
  default:
129
0
    break;
130
0
  }
131
0
} /* }}} */
132
133
static void spl_filesystem_object_free_storage(zend_object *object) /* {{{ */
134
0
{
135
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(object);
136
137
0
  if (intern->oth_handler && intern->oth_handler->dtor) {
138
0
    intern->oth_handler->dtor(intern);
139
0
  }
140
141
0
  zend_object_std_dtor(&intern->std);
142
143
0
  if (intern->path) {
144
0
    zend_string_release(intern->path);
145
0
  }
146
0
  if (intern->file_name) {
147
0
    zend_string_release(intern->file_name);
148
0
  }
149
0
  switch(intern->type) {
150
0
  case SPL_FS_INFO:
151
0
    break;
152
0
  case SPL_FS_DIR:
153
0
    if (intern->u.dir.sub_path) {
154
0
      zend_string_release(intern->u.dir.sub_path);
155
0
    }
156
0
    break;
157
0
  case SPL_FS_FILE:
158
0
    if (intern->u.file.open_mode) {
159
0
      zend_string_release(intern->u.file.open_mode);
160
0
    }
161
0
    if (intern->orig_path) {
162
0
      zend_string_release(intern->orig_path);
163
0
    }
164
0
    spl_filesystem_file_free_line(intern);
165
0
    break;
166
0
  }
167
0
} /* }}} */
168
169
/* {{{ spl_ce_dir_object_new */
170
/* creates the object by
171
   - allocating memory
172
   - initializing the object members
173
   - storing the object
174
   - setting it's handlers
175
176
   called from
177
   - clone
178
   - new
179
 */
180
static zend_object *spl_filesystem_object_new(zend_class_entry *class_type)
181
0
{
182
0
  spl_filesystem_object *intern;
183
184
0
  intern = emalloc(sizeof(spl_filesystem_object) + zend_object_properties_size(class_type));
185
  /* Avoid initializing the entirety of spl_filesystem_object.u.dir.entry. */
186
0
  memset(intern, 0,
187
0
    MAX(offsetof(spl_filesystem_object, u.dir.entry),
188
0
      offsetof(spl_filesystem_object, u.file.escape) + sizeof(int)));
189
  /* intern->type = SPL_FS_INFO; done by set 0 */
190
0
  intern->file_class = spl_ce_SplFileObject;
191
0
  intern->info_class = spl_ce_SplFileInfo;
192
193
0
  zend_object_std_init(&intern->std, class_type);
194
0
  object_properties_init(&intern->std, class_type);
195
196
0
  return &intern->std;
197
0
}
198
/* }}} */
199
200
static inline bool spl_intern_is_glob(const spl_filesystem_object *intern)
201
0
{
202
  /* NULL check on `dirp` is necessary as destructors may interfere. */
203
0
  return intern->u.dir.dirp && php_stream_is(intern->u.dir.dirp, &php_glob_stream_ops);
204
0
}
205
206
PHPAPI zend_string *spl_filesystem_object_get_path(const spl_filesystem_object *intern) /* {{{ */
207
0
{
208
0
  if (intern->type == SPL_FS_DIR && spl_intern_is_glob(intern)) {
209
0
    size_t len = 0;
210
0
    char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
211
0
    if (len == 0) {
212
0
      return NULL;
213
0
    }
214
0
    return zend_string_init(tmp, len, /* persistent */ false);
215
0
  }
216
0
  if (!intern->path) {
217
0
    return NULL;
218
0
  }
219
0
  return zend_string_copy(intern->path);
220
0
} /* }}} */
221
222
static zend_result spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
223
0
{
224
0
  if (intern->file_name) {
225
    /* already known */
226
0
    return SUCCESS;
227
0
  }
228
229
0
  switch (intern->type) {
230
0
    case SPL_FS_INFO:
231
0
    case SPL_FS_FILE:
232
0
      zend_throw_error(NULL, "Object not initialized");
233
0
      return FAILURE;
234
0
    case SPL_FS_DIR: {
235
0
      size_t name_len;
236
0
      zend_string *path;
237
0
      char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
238
239
0
      path = spl_filesystem_object_get_path(intern);
240
      /* if there is parent path, amend it, otherwise just use the given path as is */
241
0
      name_len = strlen(intern->u.dir.entry.d_name);
242
0
      if (!path) {
243
0
        intern->file_name = zend_string_init(intern->u.dir.entry.d_name, name_len, 0);
244
0
        return SUCCESS;
245
0
      }
246
247
0
      ZEND_ASSERT(ZSTR_LEN(path) != 0);
248
0
      intern->file_name = zend_string_concat3(
249
0
        ZSTR_VAL(path), ZSTR_LEN(path), &slash, 1, intern->u.dir.entry.d_name, name_len);
250
0
      zend_string_release_ex(path, /* persistent */ false);
251
0
      break;
252
0
    }
253
0
  }
254
0
  return SUCCESS;
255
0
} /* }}} */
256
257
static void spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
258
0
{
259
0
  if (intern->file_name) {
260
    /* invalidate */
261
0
    zend_string_release(intern->file_name);
262
0
    intern->file_name = NULL;
263
0
  }
264
265
0
  if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
266
0
    intern->u.dir.entry.d_name[0] = '\0';
267
0
  }
268
0
}
269
/* }}} */
270
271
0
#define IS_SLASH_AT(zs, pos) (IS_SLASH(zs[pos]))
272
273
static inline bool spl_filesystem_is_dot(const char * d_name) /* {{{ */
274
0
{
275
0
  return !strcmp(d_name, ".") || !strcmp(d_name, "..");
276
0
}
277
/* }}} */
278
279
/* {{{ spl_filesystem_dir_open */
280
/* open a directory resource
281
 * Can emit an E_WARNING as it reports errors from php_stream_opendir() */
282
static void spl_filesystem_dir_open(spl_filesystem_object* intern, zend_string *path)
283
0
{
284
0
  bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
285
286
0
  intern->type = SPL_FS_DIR;
287
0
  intern->u.dir.dirp = php_stream_opendir(ZSTR_VAL(path), REPORT_ERRORS, FG(default_context));
288
289
0
  if (intern->u.dir.dirp) {
290
    /* we prevent potential UAF with conflicting explicit fclose(), relying on the object destructor for this */
291
0
    intern->u.dir.dirp->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
292
0
  }
293
294
0
  if (ZSTR_LEN(path) > 1 && IS_SLASH_AT(ZSTR_VAL(path), ZSTR_LEN(path)-1)) {
295
0
    intern->path = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path)-1, 0);
296
0
  } else {
297
0
    intern->path = zend_string_copy(path);
298
0
  }
299
0
  intern->u.dir.index = 0;
300
301
0
  if (EG(exception) || intern->u.dir.dirp == NULL) {
302
0
    intern->u.dir.entry.d_name[0] = '\0';
303
0
    if (!EG(exception)) {
304
      /* open failed w/out notice (turned to exception due to EH_THROW) */
305
0
      zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
306
0
        "Failed to open directory \"%s\"", ZSTR_VAL(path));
307
0
    }
308
0
  } else {
309
0
    do {
310
0
      spl_filesystem_dir_read(intern);
311
0
    } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
312
0
  }
313
0
}
314
/* }}} */
315
316
/* Can generate E_WARNINGS as we report errors from stream initialized via
317
 * php_stream_open_wrapper_ex() */
318
static zend_result spl_filesystem_file_open(spl_filesystem_object *intern, bool use_include_path) /* {{{ */
319
0
{
320
0
  zval tmp;
321
322
0
  intern->type = SPL_FS_FILE;
323
0
  php_stat(intern->file_name, FS_IS_DIR, &tmp);
324
0
  if (Z_TYPE(tmp) == IS_TRUE) {
325
0
    zend_string_release(intern->u.file.open_mode);
326
0
    intern->u.file.open_mode = NULL;
327
0
    intern->file_name = NULL;
328
0
    zend_throw_exception_ex(spl_ce_LogicException, 0, "Cannot use SplFileObject with directories");
329
0
    return FAILURE;
330
0
  }
331
332
0
  intern->u.file.context = php_stream_context_from_zval(intern->u.file.zcontext, 0);
333
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);
334
335
0
  if (!ZSTR_LEN(intern->file_name) || !intern->u.file.stream) {
336
0
    if (!EG(exception)) {
337
0
      zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot open file '%s'", ZSTR_VAL(intern->file_name));
338
0
    }
339
0
    zend_string_release(intern->u.file.open_mode);
340
0
    intern->u.file.open_mode = NULL;
341
0
    intern->file_name = NULL; /* until here it is not a copy */
342
0
    return FAILURE;
343
0
  }
344
345
  /* prevent closing the stream outside of SplFileObject */
346
0
  intern->u.file.stream->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
347
348
  /*
349
  if (intern->u.file.zcontext) {
350
    //zend_list_addref(Z_RES_VAL(intern->u.file.zcontext));
351
    Z_ADDREF_P(intern->u.file.zcontext);
352
  }
353
  */
354
355
0
  if (ZSTR_LEN(intern->file_name) > 1 && IS_SLASH_AT(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name)-1)) {
356
0
    intern->file_name = zend_string_init(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name)-1, 0);
357
0
  } else {
358
0
    intern->file_name = zend_string_copy(intern->file_name);
359
0
  }
360
361
0
  intern->orig_path = zend_string_init(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path), 0);
362
363
  /* avoid reference counting in debug mode, thus do it manually */
364
0
  ZVAL_RES(&intern->u.file.zresource, intern->u.file.stream->res);
365
  /*!!! TODO: maybe bug?
366
  Z_SET_REFCOUNT(intern->u.file.zresource, 1);
367
  */
368
369
0
  intern->u.file.delimiter = ',';
370
0
  intern->u.file.enclosure = '"';
371
0
  intern->u.file.escape = (unsigned char) '\\';
372
0
  intern->u.file.is_escape_default = true;
373
374
0
  intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
375
376
0
  return SUCCESS;
377
0
} /* }}} */
378
379
/* {{{ spl_filesystem_object_clone */
380
/* Local zend_object creation (on stack)
381
   Load the 'other' object
382
   Create a new empty object (See spl_filesystem_object_new)
383
   Open the directory
384
   Clone other members (properties)
385
 */
386
static zend_object *spl_filesystem_object_clone(zend_object *old_object)
387
0
{
388
0
  zend_object *new_object;
389
0
  spl_filesystem_object *intern;
390
0
  spl_filesystem_object *source;
391
392
0
  source = spl_filesystem_from_obj(old_object);
393
0
  new_object = spl_filesystem_object_new(old_object->ce);
394
0
  intern = spl_filesystem_from_obj(new_object);
395
396
0
  intern->flags = source->flags;
397
398
0
  switch (source->type) {
399
0
    case SPL_FS_INFO:
400
0
      if (source->path != NULL) {
401
0
        intern->path = zend_string_copy(source->path);
402
0
      }
403
0
      if (source->file_name != NULL) {
404
0
        intern->file_name = zend_string_copy(source->file_name);
405
0
      }
406
0
      break;
407
0
    case SPL_FS_DIR: {
408
0
      spl_filesystem_dir_open(intern, source->path);
409
      /* read until we hit the position in which we were before */
410
0
      bool skip_dots = SPL_HAS_FLAG(source->flags, SPL_FILE_DIR_SKIPDOTS);
411
0
      zend_long index;
412
0
      for (index = 0; index < source->u.dir.index; ++index) {
413
0
        do {
414
0
          spl_filesystem_dir_read(intern);
415
0
        } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
416
0
      }
417
0
      intern->u.dir.index = index;
418
0
      break;
419
0
    }
420
0
    case SPL_FS_FILE:
421
0
      ZEND_UNREACHABLE();
422
0
  }
423
424
0
  intern->file_class = source->file_class;
425
0
  intern->info_class = source->info_class;
426
0
  intern->oth = source->oth;
427
0
  intern->oth_handler = source->oth_handler;
428
429
0
  zend_objects_clone_members(new_object, old_object);
430
431
0
  if (intern->oth_handler && intern->oth_handler->clone) {
432
0
    intern->oth_handler->clone(source, intern);
433
0
  }
434
435
0
  return new_object;
436
0
}
437
/* }}} */
438
439
static void spl_filesystem_info_set_filename(spl_filesystem_object *intern, zend_string *path) /* {{{ */
440
0
{
441
0
  size_t path_len;
442
443
0
  if (intern->file_name) {
444
0
    zend_string_release(intern->file_name);
445
0
  }
446
447
0
  path_len = ZSTR_LEN(path);
448
0
  if (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
449
0
    do {
450
0
      path_len--;
451
0
    } while (path_len > 1 && IS_SLASH_AT(ZSTR_VAL(path), path_len - 1));
452
0
    intern->file_name = zend_string_init(ZSTR_VAL(path), path_len, 0);
453
0
  } else {
454
0
    intern->file_name = zend_string_copy(path);
455
0
  }
456
0
  while (path_len > 1 && !IS_SLASH_AT(ZSTR_VAL(path), path_len-1)) {
457
0
    path_len--;
458
0
  }
459
0
  if (path_len) {
460
0
    path_len--;
461
0
  }
462
463
0
  if (intern->path) {
464
0
    zend_string_release(intern->path);
465
0
  }
466
0
  intern->path = zend_string_init(ZSTR_VAL(path), path_len, 0);
467
0
} /* }}} */
468
469
// TODO Do not pass return_value pointer but actually use value returned by function at call site?
470
static spl_filesystem_object *spl_filesystem_object_create_info(zend_string *file_path, zend_class_entry *ce, zval *return_value) /* {{{ */
471
0
{
472
0
  spl_filesystem_object *intern;
473
0
  zval arg1;
474
475
0
  ZEND_ASSERT(file_path && ZSTR_LEN(file_path) > 0);
476
0
  ZEND_ASSERT(ce != NULL);
477
478
0
  intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
479
0
  RETVAL_OBJ(&intern->std);
480
481
0
  if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
482
0
    ZVAL_STR(&arg1, file_path);
483
0
    zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
484
0
  } else {
485
0
    spl_filesystem_info_set_filename(intern, file_path);
486
0
  }
487
488
0
  return intern;
489
0
} /* }}} */
490
491
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) /* {{{ */
492
0
{
493
0
  spl_filesystem_object *intern;
494
0
  bool use_include_path = false;
495
0
  zval arg1, arg2;
496
0
  zend_error_handling error_handling;
497
498
0
  switch (source->type) {
499
0
    case SPL_FS_INFO:
500
0
    case SPL_FS_FILE:
501
0
      break;
502
0
    case SPL_FS_DIR:
503
0
      if (!source->u.dir.entry.d_name[0]) {
504
0
        zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Could not open file");
505
0
        return NULL;
506
0
      }
507
0
  }
508
509
0
  switch (type) {
510
0
    case SPL_FS_INFO:
511
0
      ce = ce ? ce : source->info_class;
512
513
0
      intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
514
0
      RETVAL_OBJ(&intern->std);
515
516
0
      if (spl_filesystem_object_get_file_name(source) == FAILURE) {
517
0
        return NULL;
518
0
      }
519
520
0
      if (ce->constructor->common.scope != spl_ce_SplFileInfo) {
521
0
        ZVAL_STR(&arg1, source->file_name);
522
0
        zend_call_method_with_1_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1);
523
0
      } else {
524
0
        intern->file_name = zend_string_copy(source->file_name);
525
0
        intern->path = spl_filesystem_object_get_path(source);
526
0
      }
527
0
      break;
528
0
    case SPL_FS_FILE:
529
0
    {
530
0
      ce = ce ? ce : source->file_class;
531
532
0
      zend_string *open_mode = ZSTR_CHAR('r');
533
0
      zval *resource = NULL;
534
535
0
      if (zend_parse_parameters(num_args, "|Sbr!",
536
0
        &open_mode, &use_include_path, &resource) == FAILURE
537
0
      ) {
538
0
        return NULL;
539
0
      }
540
541
0
      intern = spl_filesystem_from_obj(spl_filesystem_object_new(ce));
542
0
      RETVAL_OBJ(&intern->std);
543
544
0
      if (spl_filesystem_object_get_file_name(source) == FAILURE) {
545
0
        return NULL;
546
0
      }
547
548
0
      if (ce->constructor->common.scope != spl_ce_SplFileObject) {
549
0
        ZVAL_STR(&arg1, source->file_name);
550
0
        ZVAL_STR(&arg2, open_mode);
551
0
        zend_call_method_with_2_params(Z_OBJ_P(return_value), ce, &ce->constructor, "__construct", NULL, &arg1, &arg2);
552
0
      } else {
553
0
        intern->file_name = source->file_name;
554
0
        intern->path = spl_filesystem_object_get_path(source);
555
0
        intern->u.file.open_mode = zend_string_copy(open_mode);
556
0
        intern->u.file.zcontext = resource;
557
558
        /* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
559
0
        zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
560
0
        if (spl_filesystem_file_open(intern, use_include_path) == FAILURE) {
561
0
          zend_restore_error_handling(&error_handling);
562
0
          zval_ptr_dtor(return_value);
563
0
          ZVAL_NULL(return_value);
564
0
          return NULL;
565
0
        }
566
0
        zend_restore_error_handling(&error_handling);
567
0
      }
568
0
      break;
569
0
    }
570
0
    case SPL_FS_DIR:
571
0
      zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Operation not supported");
572
0
      return NULL;
573
0
  }
574
0
  return NULL;
575
0
} /* }}} */
576
577
static bool spl_filesystem_is_invalid_or_dot(const char * d_name) /* {{{ */
578
0
{
579
0
  return d_name[0] == '\0' || spl_filesystem_is_dot(d_name);
580
0
}
581
/* }}} */
582
583
0
static zend_string *spl_filesystem_object_get_pathname(spl_filesystem_object *intern) { /* {{{ */
584
0
  switch (intern->type) {
585
0
    case SPL_FS_INFO:
586
0
    case SPL_FS_FILE:
587
0
      return intern->file_name;
588
0
    case SPL_FS_DIR:
589
0
      if (intern->u.dir.entry.d_name[0]) {
590
0
        spl_filesystem_object_get_file_name(intern);
591
0
        return intern->file_name;
592
0
      }
593
0
  }
594
0
  return NULL;
595
0
}
596
/* }}} */
597
598
static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *object) /* {{{ */
599
0
{
600
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(object);
601
0
  zval tmp;
602
0
  HashTable *debug_info;
603
0
  zend_string *path_name;
604
605
  // TODO Do zend_new_array() + zend_hash_copy() trick?
606
0
  debug_info = zend_array_dup(zend_std_get_properties_ex(&intern->std));
607
608
0
  path_name = spl_filesystem_object_get_pathname(intern);
609
0
  if (path_name) {
610
0
    ZVAL_STR_COPY(&tmp, path_name);
611
0
  } else {
612
0
    ZVAL_EMPTY_STRING(&tmp);
613
0
  }
614
  /* IMPORTANT: Do not free path_name as spl_filesystem_object_get_pathname()
615
   * updates/sets the intern->file_name and returns the pointer to
616
   * intern->file_name which must remain allocated. */
617
0
  spl_set_private_debug_info_property(spl_ce_SplFileInfo, "pathName", strlen("pathName"), debug_info, &tmp);
618
619
0
  if (intern->file_name) {
620
0
    zend_string *path = spl_filesystem_object_get_path(intern);
621
0
    if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
622
      /* +1 to skip the trailing / of the path in the file name */
623
0
      ZVAL_STRINGL(&tmp, ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1, ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1));
624
0
    } else {
625
0
      ZVAL_STR_COPY(&tmp, intern->file_name);
626
0
    }
627
0
    if (path) {
628
0
      zend_string_release_ex(path, /* persistent */ false);
629
0
    }
630
631
0
    spl_set_private_debug_info_property(spl_ce_SplFileInfo, "fileName", strlen("fileName"), debug_info, &tmp);
632
0
  }
633
0
  if (intern->type == SPL_FS_DIR) {
634
0
    if (spl_intern_is_glob(intern)) {
635
0
      ZVAL_STR_COPY(&tmp, intern->path);
636
0
    } else {
637
0
      ZVAL_FALSE(&tmp);
638
0
    }
639
0
    spl_set_private_debug_info_property(spl_ce_DirectoryIterator, "glob", strlen("glob"), debug_info, &tmp);
640
0
    if (intern->u.dir.sub_path) {
641
0
      ZVAL_STR_COPY(&tmp, intern->u.dir.sub_path);
642
0
    } else {
643
0
      ZVAL_EMPTY_STRING(&tmp);
644
0
    }
645
0
    spl_set_private_debug_info_property(spl_ce_RecursiveDirectoryIterator, "subPathName", strlen("subPathName"), debug_info, &tmp);
646
0
  }
647
0
  if (intern->type == SPL_FS_FILE) {
648
0
    ZVAL_STR_COPY(&tmp, intern->u.file.open_mode);
649
0
    spl_set_private_debug_info_property(spl_ce_SplFileObject, "openMode", strlen("openMode"), debug_info, &tmp);
650
651
0
    ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.delimiter));
652
0
    spl_set_private_debug_info_property(spl_ce_SplFileObject, "delimiter", strlen("delimiter"), debug_info, &tmp);
653
654
0
    ZVAL_STR(&tmp, ZSTR_CHAR((zend_uchar)intern->u.file.enclosure));
655
0
    spl_set_private_debug_info_property(spl_ce_SplFileObject, "enclosure", strlen("enclosure"), debug_info, &tmp);
656
0
  }
657
658
0
  return debug_info;
659
0
}
660
/* }}} */
661
662
static zend_function *spl_filesystem_object_get_method_check(zend_object **object, zend_string *method, const zval *key) /* {{{ */
663
0
{
664
0
  spl_filesystem_object *fsobj = spl_filesystem_from_obj(*object);
665
666
0
  if (fsobj->u.dir.dirp == NULL && fsobj->orig_path == NULL) {
667
0
    zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
668
0
    return NULL;
669
0
  }
670
671
0
  return zend_std_get_method(object, method, key);
672
0
}
673
/* }}} */
674
675
0
#define DIT_CTOR_FLAGS  0x00000001
676
0
#define DIT_CTOR_GLOB   0x00000002
677
678
static void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_long ctor_flags) /* {{{ */
679
0
{
680
0
  spl_filesystem_object *intern;
681
0
  zend_string *path;
682
0
  zend_result parsed;
683
0
  zend_long flags = (ctor_flags & ~DIT_CTOR_FLAGS);
684
0
  zend_error_handling error_handling;
685
686
0
  if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_FLAGS)) {
687
0
    flags |= SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_FILEINFO;
688
0
    parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "P|l", &path, &flags);
689
0
  } else {
690
0
    flags |= SPL_FILE_DIR_KEY_AS_PATHNAME|SPL_FILE_DIR_CURRENT_AS_SELF;
691
0
    parsed = zend_parse_parameters(ZEND_NUM_ARGS(), "P", &path);
692
0
  }
693
0
  if (parsed == FAILURE) {
694
0
    RETURN_THROWS();
695
0
  }
696
697
0
  if (ZSTR_LEN(path) == 0) {
698
0
    zend_argument_must_not_be_empty_error(1);
699
0
    RETURN_THROWS();
700
0
  }
701
702
0
  intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
703
0
  if (intern->path) {
704
    /* object is already initialized */
705
0
    zend_throw_error(NULL, "Directory object is already initialized");
706
0
    RETURN_THROWS();
707
0
  }
708
0
  intern->flags = flags;
709
710
  /* spl_filesystem_dir_open() may emit an E_WARNING */
711
0
  zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
712
0
  if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && !zend_string_starts_with_literal(path, "glob://")) {
713
0
    path = zend_strpprintf(0, "glob://%s", ZSTR_VAL(path));
714
0
    spl_filesystem_dir_open(intern, path);
715
0
    zend_string_release(path);
716
0
  } else {
717
0
    spl_filesystem_dir_open(intern, path);
718
0
  }
719
0
  zend_restore_error_handling(&error_handling);
720
0
}
721
/* }}} */
722
723
/* {{{ Cronstructs a new dir iterator from a path. */
724
PHP_METHOD(DirectoryIterator, __construct)
725
0
{
726
0
  spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
727
0
}
728
/* }}} */
729
730
/* {{{ Rewind dir back to the start */
731
PHP_METHOD(DirectoryIterator, rewind)
732
0
{
733
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
734
735
0
  ZEND_PARSE_PARAMETERS_NONE();
736
737
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
738
0
  intern->u.dir.index = 0;
739
0
  php_stream_rewinddir(intern->u.dir.dirp);
740
0
  spl_filesystem_dir_read(intern);
741
0
}
742
/* }}} */
743
744
/* {{{ Return current dir entry */
745
PHP_METHOD(DirectoryIterator, key)
746
0
{
747
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
748
749
0
  ZEND_PARSE_PARAMETERS_NONE();
750
751
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
752
0
  RETURN_LONG(intern->u.dir.index);
753
0
}
754
/* }}} */
755
756
/* {{{ Return this (needed for Iterator interface) */
757
PHP_METHOD(DirectoryIterator, current)
758
0
{
759
0
  ZEND_PARSE_PARAMETERS_NONE();
760
761
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)));
762
0
  RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
763
0
}
764
/* }}} */
765
766
/* {{{ Move to next entry */
767
PHP_METHOD(DirectoryIterator, next)
768
0
{
769
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
770
0
  bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
771
772
0
  ZEND_PARSE_PARAMETERS_NONE();
773
774
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
775
0
  intern->u.dir.index++;
776
0
  do {
777
0
    spl_filesystem_dir_read(intern);
778
0
  } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
779
0
  if (intern->file_name) {
780
0
    zend_string_release(intern->file_name);
781
0
    intern->file_name = NULL;
782
0
  }
783
0
}
784
/* }}} */
785
786
/* {{{ Seek to the given position */
787
PHP_METHOD(DirectoryIterator, seek)
788
0
{
789
0
  spl_filesystem_object *intern    = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
790
0
  zval retval;
791
0
  zend_long pos;
792
793
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &pos) == FAILURE) {
794
0
    RETURN_THROWS();
795
0
  }
796
797
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
798
0
  if (intern->u.dir.index > pos) {
799
    /* we first rewind */
800
0
    zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_rewind, "rewind", NULL);
801
0
  }
802
803
0
  while (intern->u.dir.index < pos) {
804
0
    bool valid = false;
805
0
    zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_valid, "valid", &retval);
806
0
    valid = zend_is_true(&retval);
807
0
    zval_ptr_dtor(&retval);
808
0
    if (!valid) {
809
0
      zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", pos);
810
0
      RETURN_THROWS();
811
0
    }
812
0
    zend_call_method_with_0_params(Z_OBJ_P(ZEND_THIS), Z_OBJCE_P(ZEND_THIS), &intern->u.dir.func_next, "next", NULL);
813
0
  }
814
0
} /* }}} */
815
816
/* {{{ Check whether dir contains more entries */
817
PHP_METHOD(DirectoryIterator, valid)
818
0
{
819
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
820
821
0
  ZEND_PARSE_PARAMETERS_NONE();
822
823
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
824
0
  RETURN_BOOL(intern->u.dir.entry.d_name[0] != '\0');
825
0
}
826
/* }}} */
827
828
/* {{{ Return the path */
829
PHP_METHOD(SplFileInfo, getPath)
830
0
{
831
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
832
0
  zend_string *path;
833
834
0
  ZEND_PARSE_PARAMETERS_NONE();
835
836
0
    path = spl_filesystem_object_get_path(intern);
837
0
  if (path) {
838
0
    RETURN_STR(path);
839
0
  } else {
840
0
    RETURN_EMPTY_STRING();
841
0
  }
842
0
}
843
/* }}} */
844
845
/* {{{ Return filename only */
846
PHP_METHOD(SplFileInfo, getFilename)
847
0
{
848
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
849
0
  zend_string *path;
850
851
0
  ZEND_PARSE_PARAMETERS_NONE();
852
853
0
  if (!intern->file_name) {
854
0
    zend_throw_error(NULL, "Object not initialized");
855
0
    RETURN_THROWS();
856
0
  }
857
858
0
  path = spl_filesystem_object_get_path(intern);
859
860
0
  if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
861
    /* +1 to skip the trailing / of the path in the file name */
862
0
    size_t path_len = ZSTR_LEN(path) + 1;
863
0
    RETVAL_STRINGL(ZSTR_VAL(intern->file_name) + path_len, ZSTR_LEN(intern->file_name) - path_len);
864
0
  } else {
865
0
    RETVAL_STR_COPY(intern->file_name);
866
0
  }
867
0
  if (path) {
868
0
    zend_string_release_ex(path, /* persistent */ false);
869
0
  }
870
0
}
871
/* }}} */
872
873
/* {{{ Return filename of current dir entry */
874
PHP_METHOD(DirectoryIterator, getFilename)
875
0
{
876
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
877
878
0
  ZEND_PARSE_PARAMETERS_NONE();
879
880
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
881
0
  RETURN_STRING(intern->u.dir.entry.d_name);
882
0
}
883
/* }}} */
884
885
/* {{{ Returns file extension component of path */
886
PHP_METHOD(SplFileInfo, getExtension)
887
0
{
888
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
889
0
  char *fname = NULL;
890
0
  const char *p;
891
0
  size_t flen;
892
0
  zend_string *path;
893
0
  size_t idx;
894
0
  zend_string *ret;
895
896
0
  ZEND_PARSE_PARAMETERS_NONE();
897
898
0
  if (!intern->file_name) {
899
0
    zend_throw_error(NULL, "Object not initialized");
900
0
    RETURN_THROWS();
901
0
  }
902
903
0
  path = spl_filesystem_object_get_path(intern);
904
905
0
  if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
906
0
    fname = ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1;
907
0
    flen = ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1);
908
0
  } else {
909
0
    fname = ZSTR_VAL(intern->file_name);
910
0
    flen = ZSTR_LEN(intern->file_name);
911
0
  }
912
0
  if (path) {
913
0
    zend_string_release_ex(path, /* persistent */ false);
914
0
  }
915
916
0
  ret = php_basename(fname, flen, NULL, 0);
917
918
0
  p = zend_memrchr(ZSTR_VAL(ret), '.', ZSTR_LEN(ret));
919
0
  if (p) {
920
0
    idx = p - ZSTR_VAL(ret);
921
0
    RETVAL_STRINGL(ZSTR_VAL(ret) + idx + 1, ZSTR_LEN(ret) - idx - 1);
922
0
    zend_string_release_ex(ret, 0);
923
0
    return;
924
0
  } else {
925
0
    zend_string_release_ex(ret, 0);
926
0
    RETURN_EMPTY_STRING();
927
0
  }
928
0
}
929
/* }}}*/
930
931
/* {{{ Returns the file extension component of path */
932
PHP_METHOD(DirectoryIterator, getExtension)
933
0
{
934
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
935
0
  const char *p;
936
0
  size_t idx;
937
0
  zend_string *fname;
938
939
0
  ZEND_PARSE_PARAMETERS_NONE();
940
941
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
942
0
  fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), NULL, 0);
943
944
0
  p = zend_memrchr(ZSTR_VAL(fname), '.', ZSTR_LEN(fname));
945
0
  if (p) {
946
0
    idx = p - ZSTR_VAL(fname);
947
0
    RETVAL_STRINGL(ZSTR_VAL(fname) + idx + 1, ZSTR_LEN(fname) - idx - 1);
948
0
    zend_string_release_ex(fname, 0);
949
0
  } else {
950
0
    zend_string_release_ex(fname, 0);
951
0
    RETURN_EMPTY_STRING();
952
0
  }
953
0
}
954
/* }}} */
955
956
/* {{{ Returns filename component of path */
957
PHP_METHOD(SplFileInfo, getBasename)
958
0
{
959
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
960
0
  char *fname, *suffix = 0;
961
0
  size_t flen;
962
0
  size_t slen = 0;
963
0
  zend_string *path;
964
965
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
966
0
    RETURN_THROWS();
967
0
  }
968
969
0
  if (!intern->file_name) {
970
0
    zend_throw_error(NULL, "Object not initialized");
971
0
    RETURN_THROWS();
972
0
  }
973
974
0
  path = spl_filesystem_object_get_path(intern);
975
976
0
  if (path && ZSTR_LEN(path) && ZSTR_LEN(path) < ZSTR_LEN(intern->file_name)) {
977
    /* +1 to skip the trailing / of the path in the file name */
978
0
    fname = ZSTR_VAL(intern->file_name) + ZSTR_LEN(path) + 1;
979
0
    flen = ZSTR_LEN(intern->file_name) - (ZSTR_LEN(path) + 1);
980
0
  } else {
981
0
    fname = ZSTR_VAL(intern->file_name);
982
0
    flen = ZSTR_LEN(intern->file_name);
983
0
  }
984
0
  if (path) {
985
0
    zend_string_release_ex(path, /* persistent */ false);
986
0
  }
987
988
0
  RETURN_STR(php_basename(fname, flen, suffix, slen));
989
0
}
990
/* }}}*/
991
992
/* {{{ Returns filename component of current dir entry */
993
PHP_METHOD(DirectoryIterator, getBasename)
994
0
{
995
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
996
0
  char *suffix = 0;
997
0
  size_t slen = 0;
998
0
  zend_string *fname;
999
1000
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &suffix, &slen) == FAILURE) {
1001
0
    RETURN_THROWS();
1002
0
  }
1003
1004
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
1005
0
  fname = php_basename(intern->u.dir.entry.d_name, strlen(intern->u.dir.entry.d_name), suffix, slen);
1006
1007
0
  RETURN_STR(fname);
1008
0
}
1009
/* }}} */
1010
1011
/* {{{ Return path and filename */
1012
PHP_METHOD(SplFileInfo, getPathname)
1013
0
{
1014
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1015
0
  zend_string *path;
1016
1017
0
  ZEND_PARSE_PARAMETERS_NONE();
1018
0
  path = spl_filesystem_object_get_pathname(intern);
1019
0
  if (path) {
1020
0
    RETURN_STR_COPY(path);
1021
0
  } else {
1022
0
    RETURN_EMPTY_STRING();
1023
0
  }
1024
0
}
1025
/* }}} */
1026
1027
/* {{{ Return getPathname() or getFilename() depending on flags */
1028
PHP_METHOD(FilesystemIterator, key)
1029
0
{
1030
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1031
1032
0
  ZEND_PARSE_PARAMETERS_NONE();
1033
1034
0
  if (SPL_FILE_DIR_KEY(intern, SPL_FILE_DIR_KEY_AS_FILENAME)) {
1035
0
    RETURN_STRING(intern->u.dir.entry.d_name);
1036
0
  } else {
1037
0
    if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1038
0
      RETURN_THROWS();
1039
0
    }
1040
0
    RETURN_STR_COPY(intern->file_name);
1041
0
  }
1042
0
}
1043
/* }}} */
1044
1045
/* {{{ Return getFilename(), getFileInfo() or $this depending on flags */
1046
PHP_METHOD(FilesystemIterator, current)
1047
0
{
1048
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1049
1050
0
  ZEND_PARSE_PARAMETERS_NONE();
1051
1052
0
  if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1053
0
    if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1054
0
      RETURN_THROWS();
1055
0
    }
1056
0
    RETURN_STR_COPY(intern->file_name);
1057
0
  } else if (SPL_FILE_DIR_CURRENT(intern, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1058
0
    if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1059
0
      RETURN_THROWS();
1060
0
    }
1061
0
    spl_filesystem_object_create_type(0, intern, SPL_FS_INFO, NULL, return_value);
1062
0
  } else {
1063
0
    RETURN_OBJ_COPY(Z_OBJ_P(ZEND_THIS));
1064
0
  }
1065
0
}
1066
/* }}} */
1067
1068
/* {{{ Returns true if current entry is '.' or  '..' */
1069
PHP_METHOD(DirectoryIterator, isDot)
1070
0
{
1071
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1072
1073
0
  ZEND_PARSE_PARAMETERS_NONE();
1074
1075
0
  CHECK_DIRECTORY_ITERATOR_IS_INITIALIZED(intern);
1076
0
  RETURN_BOOL(spl_filesystem_is_dot(intern->u.dir.entry.d_name));
1077
0
}
1078
/* }}} */
1079
1080
/* {{{ Constructs a new SplFileInfo from a path. */
1081
/* When the constructor gets called the object is already created
1082
   by the engine, so we must only call 'additional' initializations.
1083
 */
1084
PHP_METHOD(SplFileInfo, __construct)
1085
0
{
1086
0
  spl_filesystem_object *intern;
1087
0
  zend_string *path;
1088
1089
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &path) == FAILURE) {
1090
0
    RETURN_THROWS();
1091
0
  }
1092
1093
0
  intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1094
1095
0
  spl_filesystem_info_set_filename(intern, path);
1096
1097
  /* intern->type = SPL_FS_INFO; already set */
1098
0
}
1099
/* }}} */
1100
1101
/* {{{ FileInfoFunction */
1102
#define FileInfoFunction(func_name, func_num) \
1103
0
PHP_METHOD(SplFileInfo, func_name) \
1104
0
{ \
1105
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)); \
1106
0
  zend_error_handling error_handling; \
1107
0
  ZEND_PARSE_PARAMETERS_NONE(); \
1108
0
  if (spl_filesystem_object_get_file_name(intern) == FAILURE) { \
1109
0
    RETURN_THROWS(); \
1110
0
  } \
1111
0
  zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);\
1112
0
  php_stat(intern->file_name, func_num, return_value); \
1113
0
  zend_restore_error_handling(&error_handling); \
1114
0
}
1115
/* }}} */
1116
1117
/* {{{ Get file permissions */
1118
0
FileInfoFunction(getPerms, FS_PERMS)
1119
/* }}} */
1120
1121
/* {{{ Get file inode */
1122
0
FileInfoFunction(getInode, FS_INODE)
1123
/* }}} */
1124
1125
/* {{{ Get file size */
1126
0
FileInfoFunction(getSize, FS_SIZE)
1127
/* }}} */
1128
1129
/* {{{ Get file owner */
1130
0
FileInfoFunction(getOwner, FS_OWNER)
1131
/* }}} */
1132
1133
/* {{{ Get file group */
1134
0
FileInfoFunction(getGroup, FS_GROUP)
1135
/* }}} */
1136
1137
/* {{{ Get last access time of file */
1138
0
FileInfoFunction(getATime, FS_ATIME)
1139
/* }}} */
1140
1141
/* {{{ Get last modification time of file */
1142
0
FileInfoFunction(getMTime, FS_MTIME)
1143
/* }}} */
1144
1145
/* {{{ Get inode modification time of file */
1146
0
FileInfoFunction(getCTime, FS_CTIME)
1147
/* }}} */
1148
1149
/* {{{ Get file type */
1150
0
FileInfoFunction(getType, FS_TYPE)
1151
/* }}} */
1152
1153
/* {{{ Returns true if file can be written */
1154
0
FileInfoFunction(isWritable, FS_IS_W)
1155
/* }}} */
1156
1157
/* {{{ Returns true if file can be read */
1158
0
FileInfoFunction(isReadable, FS_IS_R)
1159
/* }}} */
1160
1161
/* {{{ Returns true if file is executable */
1162
0
FileInfoFunction(isExecutable, FS_IS_X)
1163
/* }}} */
1164
1165
/* {{{ Returns true if file is a regular file */
1166
0
FileInfoFunction(isFile, FS_IS_FILE)
1167
/* }}} */
1168
1169
/* {{{ Returns true if file is directory */
1170
0
FileInfoFunction(isDir, FS_IS_DIR)
1171
/* }}} */
1172
1173
/* {{{ Returns true if file is symbolic link */
1174
0
FileInfoFunction(isLink, FS_IS_LINK)
1175
/* }}} */
1176
1177
/* {{{ Return the target of a symbolic link */
1178
PHP_METHOD(SplFileInfo, getLinkTarget)
1179
0
{
1180
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1181
0
  ssize_t ret;
1182
0
  char buff[MAXPATHLEN];
1183
1184
0
  ZEND_PARSE_PARAMETERS_NONE();
1185
1186
0
  if (intern->file_name == NULL) {
1187
0
    if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1188
0
      RETURN_THROWS();
1189
0
    }
1190
0
  }
1191
0
#if defined(PHP_WIN32) || defined(HAVE_SYMLINK)
1192
0
  if (intern->file_name == NULL) {
1193
0
    zend_value_error("Filename must not be empty");
1194
0
    RETURN_THROWS();
1195
0
  }
1196
0
  if (!IS_ABSOLUTE_PATH(ZSTR_VAL(intern->file_name), ZSTR_LEN(intern->file_name))) {
1197
0
    char expanded_path[MAXPATHLEN];
1198
0
    if (!expand_filepath_with_mode(ZSTR_VAL(intern->file_name), expanded_path, NULL, 0, CWD_EXPAND )) {
1199
0
      php_error_docref(NULL, E_WARNING, "No such file or directory");
1200
0
      RETURN_FALSE;
1201
0
    }
1202
0
    ret = php_sys_readlink(expanded_path, buff, MAXPATHLEN - 1);
1203
0
  } else {
1204
0
    ret = php_sys_readlink(ZSTR_VAL(intern->file_name), buff,  MAXPATHLEN-1);
1205
0
  }
1206
#else
1207
  ret = -1; /* always fail if not implemented */
1208
#endif
1209
1210
0
  if (ret == -1) {
1211
0
    zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Unable to read link %s, error: %s", ZSTR_VAL(intern->file_name), strerror(errno));
1212
0
    RETVAL_FALSE;
1213
0
  } else {
1214
    /* Append NULL to the end of the string */
1215
0
    buff[ret] = '\0';
1216
1217
0
    RETVAL_STRINGL(buff, ret);
1218
0
  }
1219
0
}
1220
/* }}} */
1221
1222
/* {{{ Return the resolved path */
1223
PHP_METHOD(SplFileInfo, getRealPath)
1224
0
{
1225
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1226
0
  char buff[MAXPATHLEN];
1227
0
  char *filename;
1228
1229
0
  ZEND_PARSE_PARAMETERS_NONE();
1230
1231
0
  if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
1232
0
    if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1233
0
      RETURN_THROWS();
1234
0
    }
1235
0
  }
1236
1237
0
  if (intern->orig_path) {
1238
0
    filename = ZSTR_VAL(intern->orig_path);
1239
0
  } else {
1240
0
    filename = intern->file_name ? ZSTR_VAL(intern->file_name) : NULL;
1241
0
  }
1242
1243
1244
0
  if (filename && VCWD_REALPATH(filename, buff)) {
1245
#ifdef ZTS
1246
    if (VCWD_ACCESS(buff, F_OK)) {
1247
      RETURN_FALSE;
1248
    } else
1249
#endif
1250
0
    RETURN_STRING(buff);
1251
0
  } else {
1252
0
    RETURN_FALSE;
1253
0
  }
1254
0
}
1255
/* }}} */
1256
1257
/* {{{ Open the current file */
1258
PHP_METHOD(SplFileInfo, openFile)
1259
0
{
1260
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1261
1262
0
  spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_FILE, NULL, return_value);
1263
0
}
1264
/* }}} */
1265
1266
/* {{{ Class to use in openFile() */
1267
PHP_METHOD(SplFileInfo, setFileClass)
1268
0
{
1269
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1270
0
  zend_class_entry *ce = spl_ce_SplFileObject;
1271
1272
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
1273
0
    RETURN_THROWS();
1274
0
  }
1275
1276
0
  intern->file_class = ce;
1277
0
}
1278
/* }}} */
1279
1280
/* {{{ Class to use in getFileInfo(), getPathInfo() */
1281
PHP_METHOD(SplFileInfo, setInfoClass)
1282
0
{
1283
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1284
0
  zend_class_entry *ce = spl_ce_SplFileInfo;
1285
1286
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &ce) == FAILURE) {
1287
0
    RETURN_THROWS();
1288
0
  }
1289
1290
0
  intern->info_class = ce;
1291
0
}
1292
/* }}} */
1293
1294
/* {{{ Get/copy file info */
1295
PHP_METHOD(SplFileInfo, getFileInfo)
1296
0
{
1297
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1298
0
  zend_class_entry *ce = intern->info_class;
1299
1300
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C!", &ce) == FAILURE) {
1301
0
    RETURN_THROWS();
1302
0
  }
1303
1304
0
  spl_filesystem_object_create_type(ZEND_NUM_ARGS(), intern, SPL_FS_INFO, ce, return_value);
1305
0
}
1306
/* }}} */
1307
1308
/* {{{ Get/copy file info */
1309
PHP_METHOD(SplFileInfo, getPathInfo)
1310
0
{
1311
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1312
0
  zend_class_entry *ce = NULL;
1313
0
  zend_string *path;
1314
1315
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|C!", &ce) == FAILURE) {
1316
0
    RETURN_THROWS();
1317
0
  }
1318
1319
0
  if (ce == NULL) {
1320
0
    ce = intern->info_class;
1321
0
  } else if (!instanceof_function(ce, spl_ce_SplFileInfo)) {
1322
0
    zend_argument_type_error(1, "must be a class name derived from %s or null, %s given", ZSTR_VAL(spl_ce_SplFileInfo->name), ZSTR_VAL(ce->name));
1323
0
    RETURN_THROWS();
1324
0
  }
1325
1326
0
  path = spl_filesystem_object_get_pathname(intern);
1327
0
  if (path && ZSTR_LEN(path)) {
1328
0
    zend_string *dpath = zend_string_init(ZSTR_VAL(path), ZSTR_LEN(path), 0);
1329
0
    ZSTR_LEN(dpath) = zend_dirname(ZSTR_VAL(dpath), ZSTR_LEN(path));
1330
0
    spl_filesystem_object_create_info(dpath, ce, return_value);
1331
0
    zend_string_release(dpath);
1332
0
  }
1333
0
}
1334
/* }}} */
1335
1336
/* {{{ */
1337
PHP_METHOD(SplFileInfo, __debugInfo)
1338
0
{
1339
0
  ZEND_PARSE_PARAMETERS_NONE();
1340
1341
0
  RETURN_ARR(spl_filesystem_object_get_debug_info(Z_OBJ_P(ZEND_THIS)));
1342
0
} /* }}} */
1343
1344
/* {{{ */
1345
PHP_METHOD(SplFileInfo, _bad_state_ex)
1346
0
{
1347
0
  ZEND_PARSE_PARAMETERS_NONE();
1348
0
  zend_throw_error(NULL, "The parent constructor was not called: the object is in an invalid state");
1349
0
  RETURN_THROWS();
1350
0
}
1351
/* }}} */
1352
1353
/* {{{ Constructs a new dir iterator from a path. */
1354
PHP_METHOD(FilesystemIterator, __construct)
1355
0
{
1356
0
  spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS | SPL_FILE_DIR_SKIPDOTS);
1357
0
}
1358
/* }}} */
1359
1360
/* {{{ Rewind dir back to the start */
1361
PHP_METHOD(FilesystemIterator, rewind)
1362
0
{
1363
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1364
0
  bool skip_dots = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_SKIPDOTS);
1365
1366
0
  ZEND_PARSE_PARAMETERS_NONE();
1367
1368
0
  intern->u.dir.index = 0;
1369
0
  if (intern->u.dir.dirp) {
1370
0
    php_stream_rewinddir(intern->u.dir.dirp);
1371
0
  }
1372
0
  do {
1373
0
    spl_filesystem_dir_read(intern);
1374
0
  } while (skip_dots && spl_filesystem_is_dot(intern->u.dir.entry.d_name));
1375
0
}
1376
/* }}} */
1377
1378
/* {{{ Get handling flags */
1379
PHP_METHOD(FilesystemIterator, getFlags)
1380
0
{
1381
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1382
1383
0
  ZEND_PARSE_PARAMETERS_NONE();
1384
1385
0
  RETURN_LONG(intern->flags & (SPL_FILE_DIR_KEY_MODE_MASK | SPL_FILE_DIR_CURRENT_MODE_MASK | SPL_FILE_DIR_OTHERS_MASK));
1386
0
} /* }}} */
1387
1388
/* {{{ Set handling flags */
1389
PHP_METHOD(FilesystemIterator, setFlags)
1390
0
{
1391
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1392
0
  zend_long flags;
1393
1394
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &flags) == FAILURE) {
1395
0
    RETURN_THROWS();
1396
0
  }
1397
1398
0
  intern->flags &= ~(SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK);
1399
0
  intern->flags |= ((SPL_FILE_DIR_KEY_MODE_MASK|SPL_FILE_DIR_CURRENT_MODE_MASK|SPL_FILE_DIR_OTHERS_MASK) & flags);
1400
0
} /* }}} */
1401
1402
/* {{{ Returns whether current entry is a directory and not '.' or '..' */
1403
PHP_METHOD(RecursiveDirectoryIterator, hasChildren)
1404
0
{
1405
0
  bool allow_links = 0;
1406
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1407
1408
0
  ZEND_PARSE_PARAMETERS_START(0, 1)
1409
0
    Z_PARAM_OPTIONAL
1410
0
    Z_PARAM_BOOL(allow_links)
1411
0
  ZEND_PARSE_PARAMETERS_END();
1412
1413
0
  if (spl_filesystem_is_invalid_or_dot(intern->u.dir.entry.d_name)) {
1414
0
    RETURN_FALSE;
1415
0
  } else {
1416
0
    if (intern->u.dir.entry.d_type == DT_DIR) {
1417
0
      RETURN_TRUE;
1418
0
    } else if (intern->u.dir.entry.d_type == DT_REG) {
1419
0
      RETURN_FALSE;
1420
0
    }
1421
0
    if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1422
0
      RETURN_THROWS();
1423
0
    }
1424
0
    php_stat(intern->file_name, FS_LPERMS, return_value);
1425
0
    if (Z_TYPE_P(return_value) == IS_FALSE) {
1426
0
      return;
1427
0
    } else if (!S_ISLNK(Z_LVAL_P(return_value))) {
1428
0
      RETURN_BOOL(S_ISDIR(Z_LVAL_P(return_value)));
1429
0
    } else {
1430
0
      if (!allow_links
1431
0
       && !(intern->flags & SPL_FILE_DIR_FOLLOW_SYMLINKS)) {
1432
0
        RETURN_FALSE;
1433
0
      }
1434
0
      php_stat(intern->file_name, FS_IS_DIR, return_value);
1435
0
    }
1436
0
    }
1437
0
}
1438
/* }}} */
1439
1440
/* {{{ Returns an iterator for the current entry if it is a directory */
1441
PHP_METHOD(RecursiveDirectoryIterator, getChildren)
1442
0
{
1443
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1444
0
  spl_filesystem_object *subdir;
1445
0
  char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
1446
1447
0
  ZEND_PARSE_PARAMETERS_NONE();
1448
1449
0
  if (spl_filesystem_object_get_file_name(intern) == FAILURE) {
1450
0
    RETURN_THROWS();
1451
0
  }
1452
1453
0
  zval params[2];
1454
0
  ZVAL_STR_COPY(&params[0], intern->file_name);
1455
0
  ZVAL_LONG(&params[1], intern->flags);
1456
1457
0
  zend_result is_initialized = object_init_with_constructor(return_value, Z_OBJCE_P(ZEND_THIS), 2, params, NULL);
1458
0
  zval_ptr_dtor_str(&params[0]);
1459
0
  if (is_initialized == FAILURE) {
1460
0
    RETURN_THROWS();
1461
0
  }
1462
1463
0
  subdir = spl_filesystem_from_obj(Z_OBJ_P(return_value));
1464
0
  if (subdir) {
1465
0
    size_t name_len = strlen(intern->u.dir.entry.d_name);
1466
0
    if (intern->u.dir.sub_path && ZSTR_LEN(intern->u.dir.sub_path)) {
1467
0
      zend_string *sub_path = zend_string_concat3(
1468
0
        ZSTR_VAL(intern->u.dir.sub_path), ZSTR_LEN(intern->u.dir.sub_path),
1469
0
        &slash, 1,
1470
0
        intern->u.dir.entry.d_name, name_len
1471
0
      );
1472
0
      subdir->u.dir.sub_path = sub_path;
1473
0
    } else {
1474
0
      subdir->u.dir.sub_path = zend_string_init(intern->u.dir.entry.d_name, name_len, 0);
1475
0
    }
1476
0
    subdir->info_class = intern->info_class;
1477
0
    subdir->file_class = intern->file_class;
1478
0
    subdir->oth = intern->oth;
1479
0
  }
1480
0
}
1481
/* }}} */
1482
1483
/* {{{ Get sub path */
1484
PHP_METHOD(RecursiveDirectoryIterator, getSubPath)
1485
0
{
1486
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1487
1488
0
  ZEND_PARSE_PARAMETERS_NONE();
1489
1490
0
  if (intern->u.dir.sub_path) {
1491
0
    RETURN_STR_COPY(intern->u.dir.sub_path);
1492
0
  } else {
1493
0
    RETURN_EMPTY_STRING();
1494
0
  }
1495
0
}
1496
/* }}} */
1497
1498
/* {{{ Get sub path and file name */
1499
PHP_METHOD(RecursiveDirectoryIterator, getSubPathname)
1500
0
{
1501
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1502
0
  char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
1503
1504
0
  ZEND_PARSE_PARAMETERS_NONE();
1505
1506
0
  if (intern->u.dir.sub_path) {
1507
0
    RETURN_NEW_STR(strpprintf(0, "%s%c%s", ZSTR_VAL(intern->u.dir.sub_path), slash, intern->u.dir.entry.d_name));
1508
0
  } else {
1509
0
    RETURN_STRING(intern->u.dir.entry.d_name);
1510
0
  }
1511
0
}
1512
/* }}} */
1513
1514
/* {{{ Cronstructs a new dir iterator from a path. */
1515
PHP_METHOD(RecursiveDirectoryIterator, __construct)
1516
0
{
1517
0
  spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS);
1518
0
}
1519
/* }}} */
1520
1521
/* {{{ Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
1522
PHP_METHOD(GlobIterator, __construct)
1523
0
{
1524
0
  spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAM_PASSTHRU, DIT_CTOR_FLAGS|DIT_CTOR_GLOB);
1525
0
}
1526
/* }}} */
1527
1528
/* {{{ Return the number of directories and files found by globbing */
1529
PHP_METHOD(GlobIterator, count)
1530
0
{
1531
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1532
1533
0
  ZEND_PARSE_PARAMETERS_NONE();
1534
1535
0
  if (EXPECTED(spl_intern_is_glob(intern))) {
1536
0
    RETURN_LONG(php_glob_stream_get_count(intern->u.dir.dirp, NULL));
1537
0
  } else {
1538
    /* This can happen by avoiding constructors in specially-crafted code. */
1539
0
    zend_throw_error(NULL, "GlobIterator is not initialized");
1540
0
  }
1541
0
}
1542
/* }}} */
1543
1544
/* {{{ forward declarations to the iterator handlers */
1545
static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter);
1546
static zend_result spl_filesystem_dir_it_valid(zend_object_iterator *iter);
1547
static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter);
1548
static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key);
1549
static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter);
1550
static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter);
1551
1552
/* iterator handler table */
1553
static const zend_object_iterator_funcs spl_filesystem_dir_it_funcs = {
1554
  spl_filesystem_dir_it_dtor,
1555
  spl_filesystem_dir_it_valid,
1556
  spl_filesystem_dir_it_current_data,
1557
  spl_filesystem_dir_it_current_key,
1558
  spl_filesystem_dir_it_move_forward,
1559
  spl_filesystem_dir_it_rewind,
1560
  NULL,
1561
  NULL, /* get_gc */
1562
};
1563
/* }}} */
1564
1565
/* {{{ spl_ce_dir_get_iterator */
1566
static zend_object_iterator *spl_filesystem_dir_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
1567
0
{
1568
0
  spl_filesystem_iterator *iterator;
1569
0
  spl_filesystem_object *dir_object;
1570
1571
0
  if (by_ref) {
1572
0
    zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1573
0
    return NULL;
1574
0
  }
1575
0
  dir_object = spl_filesystem_from_obj(Z_OBJ_P(object));
1576
0
  iterator = spl_filesystem_object_to_iterator(dir_object);
1577
0
  ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1578
0
  iterator->intern.funcs = &spl_filesystem_dir_it_funcs;
1579
  /* ->current must be initialized; rewind doesn't set it and valid
1580
   * doesn't check whether it's set */
1581
0
  iterator->current = *object;
1582
1583
0
  return &iterator->intern;
1584
0
}
1585
/* }}} */
1586
1587
/* {{{ spl_filesystem_dir_it_dtor */
1588
static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter)
1589
0
{
1590
0
  spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1591
0
  zval_ptr_dtor(&iterator->intern.data);
1592
0
}
1593
/* }}} */
1594
1595
/* {{{ spl_filesystem_dir_it_valid */
1596
static zend_result spl_filesystem_dir_it_valid(zend_object_iterator *iter)
1597
0
{
1598
0
  spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1599
1600
0
  return object->u.dir.entry.d_name[0] != '\0' ? SUCCESS : FAILURE;
1601
0
}
1602
/* }}} */
1603
1604
/* {{{ spl_filesystem_dir_it_current_data */
1605
static zval *spl_filesystem_dir_it_current_data(zend_object_iterator *iter)
1606
0
{
1607
0
  spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1608
1609
0
  return &iterator->current;
1610
0
}
1611
/* }}} */
1612
1613
/* {{{ spl_filesystem_dir_it_current_key */
1614
static void spl_filesystem_dir_it_current_key(zend_object_iterator *iter, zval *key)
1615
0
{
1616
0
  spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1617
1618
0
  ZVAL_LONG(key, object->u.dir.index);
1619
0
}
1620
/* }}} */
1621
1622
/* {{{ spl_filesystem_dir_it_move_forward */
1623
static void spl_filesystem_dir_it_move_forward(zend_object_iterator *iter)
1624
0
{
1625
0
  spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1626
1627
0
  object->u.dir.index++;
1628
0
  spl_filesystem_dir_read(object);
1629
0
  if (object->file_name) {
1630
0
    zend_string_release(object->file_name);
1631
0
    object->file_name = NULL;
1632
0
  }
1633
0
}
1634
/* }}} */
1635
1636
/* {{{ spl_filesystem_dir_it_rewind */
1637
static void spl_filesystem_dir_it_rewind(zend_object_iterator *iter)
1638
0
{
1639
0
  spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1640
1641
0
  object->u.dir.index = 0;
1642
0
  if (object->u.dir.dirp) {
1643
0
    php_stream_rewinddir(object->u.dir.dirp);
1644
0
  }
1645
0
  spl_filesystem_dir_read(object);
1646
0
}
1647
/* }}} */
1648
1649
/* {{{ spl_filesystem_tree_it_dtor */
1650
static void spl_filesystem_tree_it_dtor(zend_object_iterator *iter)
1651
0
{
1652
0
  spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1653
0
  zval_ptr_dtor(&iterator->intern.data);
1654
0
  zval_ptr_dtor(&iterator->current);
1655
0
}
1656
/* }}} */
1657
1658
/* {{{ spl_filesystem_tree_it_current_data */
1659
static zval *spl_filesystem_tree_it_current_data(zend_object_iterator *iter)
1660
0
{
1661
0
  spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1662
0
  spl_filesystem_object   *object   = spl_filesystem_iterator_to_object(iterator);
1663
1664
0
  if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_PATHNAME)) {
1665
0
    if (Z_ISUNDEF(iterator->current)) {
1666
0
      if (spl_filesystem_object_get_file_name(object) == FAILURE) {
1667
0
        return NULL;
1668
0
      }
1669
0
      ZVAL_STR_COPY(&iterator->current, object->file_name);
1670
0
    }
1671
0
    return &iterator->current;
1672
0
  } else if (SPL_FILE_DIR_CURRENT(object, SPL_FILE_DIR_CURRENT_AS_FILEINFO)) {
1673
0
    if (Z_ISUNDEF(iterator->current)) {
1674
0
      if (spl_filesystem_object_get_file_name(object) == FAILURE) {
1675
0
        return NULL;
1676
0
      }
1677
0
      spl_filesystem_object_create_type(0, object, SPL_FS_INFO, NULL, &iterator->current);
1678
0
    }
1679
0
    return &iterator->current;
1680
0
  } else {
1681
0
    return &iterator->intern.data;
1682
0
  }
1683
0
}
1684
/* }}} */
1685
1686
/* {{{ spl_filesystem_tree_it_current_key */
1687
static void spl_filesystem_tree_it_current_key(zend_object_iterator *iter, zval *key)
1688
0
{
1689
0
  spl_filesystem_object *object = spl_filesystem_iterator_to_object((spl_filesystem_iterator *)iter);
1690
1691
0
  if (SPL_FILE_DIR_KEY(object, SPL_FILE_DIR_KEY_AS_FILENAME)) {
1692
0
    ZVAL_STRING(key, object->u.dir.entry.d_name);
1693
0
  } else {
1694
0
    if (spl_filesystem_object_get_file_name(object) == FAILURE) {
1695
0
      return;
1696
0
    }
1697
0
    ZVAL_STR_COPY(key, object->file_name);
1698
0
  }
1699
0
}
1700
/* }}} */
1701
1702
/* {{{ spl_filesystem_tree_it_move_forward */
1703
static void spl_filesystem_tree_it_move_forward(zend_object_iterator *iter)
1704
0
{
1705
0
  spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1706
0
  spl_filesystem_object   *object   = spl_filesystem_iterator_to_object(iterator);
1707
0
  bool skip_dots = SPL_HAS_FLAG(object->flags, SPL_FILE_DIR_SKIPDOTS);
1708
1709
0
  object->u.dir.index++;
1710
0
  do {
1711
0
    spl_filesystem_dir_read(object);
1712
0
  } while (skip_dots && spl_filesystem_is_dot(object->u.dir.entry.d_name));
1713
0
  if (object->file_name) {
1714
0
    zend_string_release(object->file_name);
1715
0
    object->file_name = NULL;
1716
0
  }
1717
0
  if (!Z_ISUNDEF(iterator->current)) {
1718
0
    zval_ptr_dtor(&iterator->current);
1719
0
    ZVAL_UNDEF(&iterator->current);
1720
0
  }
1721
0
}
1722
/* }}} */
1723
1724
/* {{{ spl_filesystem_tree_it_rewind */
1725
static void spl_filesystem_tree_it_rewind(zend_object_iterator *iter)
1726
0
{
1727
0
  spl_filesystem_iterator *iterator = (spl_filesystem_iterator *)iter;
1728
0
  spl_filesystem_object   *object   = spl_filesystem_iterator_to_object(iterator);
1729
0
  bool skip_dots = SPL_HAS_FLAG(object->flags, SPL_FILE_DIR_SKIPDOTS);
1730
1731
0
  object->u.dir.index = 0;
1732
0
  if (object->u.dir.dirp) {
1733
0
    php_stream_rewinddir(object->u.dir.dirp);
1734
0
  }
1735
0
  do {
1736
0
    spl_filesystem_dir_read(object);
1737
0
  } while (skip_dots && spl_filesystem_is_dot(object->u.dir.entry.d_name));
1738
0
  if (!Z_ISUNDEF(iterator->current)) {
1739
0
    zval_ptr_dtor(&iterator->current);
1740
0
    ZVAL_UNDEF(&iterator->current);
1741
0
  }
1742
0
}
1743
/* }}} */
1744
1745
/* {{{ iterator handler table */
1746
static const zend_object_iterator_funcs spl_filesystem_tree_it_funcs = {
1747
  spl_filesystem_tree_it_dtor,
1748
  spl_filesystem_dir_it_valid,
1749
  spl_filesystem_tree_it_current_data,
1750
  spl_filesystem_tree_it_current_key,
1751
  spl_filesystem_tree_it_move_forward,
1752
  spl_filesystem_tree_it_rewind,
1753
  NULL,
1754
  NULL, /* get_gc */
1755
};
1756
/* }}} */
1757
1758
/* {{{ spl_ce_dir_get_iterator */
1759
static zend_object_iterator *spl_filesystem_tree_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
1760
0
{
1761
0
  spl_filesystem_iterator *iterator;
1762
0
  spl_filesystem_object *dir_object;
1763
1764
0
  if (by_ref) {
1765
0
    zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
1766
0
    return NULL;
1767
0
  }
1768
0
  dir_object = spl_filesystem_from_obj(Z_OBJ_P(object));
1769
0
  iterator = spl_filesystem_object_to_iterator(dir_object);
1770
1771
0
  ZVAL_OBJ_COPY(&iterator->intern.data, Z_OBJ_P(object));
1772
0
  iterator->intern.funcs = &spl_filesystem_tree_it_funcs;
1773
1774
0
  return &iterator->intern;
1775
0
}
1776
/* }}} */
1777
1778
static ZEND_COLD void spl_filesystem_file_cannot_read(spl_filesystem_object *intern)
1779
0
{
1780
0
  zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot read from file %s", ZSTR_VAL(intern->file_name));
1781
0
}
1782
1783
static zend_result spl_filesystem_file_read_ex(spl_filesystem_object *intern, bool silent, zend_long line_add, bool csv)
1784
0
{
1785
0
  char *buf;
1786
0
  size_t line_len = 0;
1787
1788
0
  spl_filesystem_file_free_line(intern);
1789
1790
0
  if (php_stream_eof(intern->u.file.stream)) {
1791
0
    if (!silent) {
1792
0
      spl_filesystem_file_cannot_read(intern);
1793
0
    }
1794
0
    return FAILURE;
1795
0
  }
1796
1797
0
  if (intern->u.file.max_line_len > 0) {
1798
0
    buf = safe_emalloc((intern->u.file.max_line_len + 1), sizeof(char), 0);
1799
0
    if (php_stream_get_line(intern->u.file.stream, buf, intern->u.file.max_line_len + 1, &line_len) == NULL) {
1800
0
      efree(buf);
1801
0
      buf = NULL;
1802
0
    } else {
1803
0
      buf[line_len] = '\0';
1804
0
    }
1805
0
  } else {
1806
0
    buf = php_stream_get_line(intern->u.file.stream, NULL, 0, &line_len);
1807
0
  }
1808
1809
0
  if (!buf) {
1810
0
    if (!silent) {
1811
0
      spl_filesystem_file_cannot_read(intern);
1812
0
    }
1813
0
    return FAILURE;
1814
0
  }
1815
1816
0
  if (!csv && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
1817
0
    if (line_len > 0 && buf[line_len - 1] == '\n') {
1818
0
      line_len--;
1819
0
      if (line_len > 0 && buf[line_len - 1] == '\r') {
1820
0
        line_len--;
1821
0
      }
1822
0
      buf[line_len] = '\0';
1823
0
    }
1824
0
  }
1825
1826
0
  intern->u.file.current_line = zend_string_init(buf, line_len, /* persistent */ false);
1827
0
  efree(buf);
1828
0
  intern->u.file.current_line_num += line_add;
1829
1830
0
  return SUCCESS;
1831
0
} /* }}} */
1832
1833
static inline zend_result spl_filesystem_file_read(spl_filesystem_object *intern, bool silent, bool csv)
1834
0
{
1835
0
  zend_long line_add = (intern->u.file.current_line) ? 1 : 0;
1836
0
  return spl_filesystem_file_read_ex(intern, silent, line_add, csv);
1837
0
}
1838
1839
static bool is_line_empty(const spl_filesystem_object *intern)
1840
0
{
1841
0
  const char *current_line = ZSTR_VAL(intern->u.file.current_line);
1842
0
  size_t current_line_len = ZSTR_LEN(intern->u.file.current_line);
1843
0
  return current_line_len == 0 || (
1844
0
    SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)
1845
0
    && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)
1846
0
    && (
1847
0
      (current_line_len == 1 && current_line[0] == '\n')
1848
0
      || (current_line_len == 2 && current_line[0] == '\r' && current_line[1] == '\n')
1849
0
    )
1850
0
  );
1851
0
}
1852
1853
static zend_result spl_filesystem_file_read_csv(spl_filesystem_object *intern, char delimiter, char enclosure, int escape, zval *return_value, bool silent) /* {{{ */
1854
0
{
1855
0
  do {
1856
0
    zend_result ret = spl_filesystem_file_read(intern, silent, /* csv */ true);
1857
0
    if (ret != SUCCESS) {
1858
0
      return ret;
1859
0
    }
1860
0
  } while (is_line_empty(intern) && SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY));
1861
1862
  /* We need to duplicate the current line content as php_fgetcsv() will free it.
1863
   * This is because it might reach the end of the line when it's in an enclosure and
1864
   * thus must fetch the next line from the stream */
1865
0
  size_t buf_len = ZSTR_LEN(intern->u.file.current_line);
1866
0
  char *buf = estrndup(ZSTR_VAL(intern->u.file.current_line), buf_len);
1867
1868
0
  if (!Z_ISUNDEF(intern->u.file.current_zval)) {
1869
0
    zval_ptr_dtor(&intern->u.file.current_zval);
1870
0
    ZVAL_UNDEF(&intern->u.file.current_zval);
1871
0
  }
1872
1873
0
  HashTable *values = php_fgetcsv(intern->u.file.stream, delimiter, enclosure, escape, buf_len, buf);
1874
0
  if (values == NULL) {
1875
0
    values = php_bc_fgetcsv_empty_line();
1876
0
  }
1877
0
  ZVAL_ARR(&intern->u.file.current_zval, values);
1878
0
  if (return_value) {
1879
0
    ZVAL_COPY(return_value, &intern->u.file.current_zval);
1880
0
  }
1881
0
  return SUCCESS;
1882
0
}
1883
/* }}} */
1884
1885
static zend_result spl_filesystem_file_read_line_ex(zval * this_ptr, spl_filesystem_object *intern, bool silent) /* {{{ */
1886
0
{
1887
0
  zval retval;
1888
1889
  /* 1) use fgetcsv? 2) overloaded call the function, 3) do it directly */
1890
0
  if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
1891
0
    return spl_filesystem_file_read_csv(intern, intern->u.file.delimiter, intern->u.file.enclosure, intern->u.file.escape, NULL, silent);
1892
0
  }
1893
0
  if (intern->u.file.func_getCurr->common.scope != spl_ce_SplFileObject) {
1894
0
    spl_filesystem_file_free_line(intern);
1895
1896
0
    if (php_stream_eof(intern->u.file.stream)) {
1897
0
      if (!silent) {
1898
0
        spl_filesystem_file_cannot_read(intern);
1899
0
      }
1900
0
      return FAILURE;
1901
0
    }
1902
0
    zend_call_method_with_0_params(Z_OBJ_P(this_ptr), Z_OBJCE_P(this_ptr), &intern->u.file.func_getCurr, "getCurrentLine", &retval);
1903
0
    if (Z_ISUNDEF(retval)) {
1904
0
      return FAILURE;
1905
0
    }
1906
1907
0
    if (Z_TYPE(retval) != IS_STRING) {
1908
0
      zend_type_error("%s::getCurrentLine(): Return value must be of type string, %s returned",
1909
0
        ZSTR_VAL(Z_OBJCE_P(this_ptr)->name), zend_zval_value_name(&retval));
1910
0
      zval_ptr_dtor(&retval);
1911
0
      return FAILURE;
1912
0
    }
1913
1914
0
    if (intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval)) {
1915
0
      intern->u.file.current_line_num++;
1916
0
    }
1917
0
    spl_filesystem_file_free_line(intern);
1918
0
    intern->u.file.current_line = zend_string_copy(Z_STR(retval));
1919
0
    zval_ptr_dtor(&retval);
1920
0
    return SUCCESS;
1921
0
  } else {
1922
0
    return spl_filesystem_file_read(intern, silent, /* csv */ false);
1923
0
  }
1924
0
} /* }}} */
1925
1926
static zend_result spl_filesystem_file_read_line(zval * this_ptr, spl_filesystem_object *intern, bool silent) /* {{{ */
1927
0
{
1928
0
  zend_result ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
1929
1930
0
  while (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_SKIP_EMPTY) && ret == SUCCESS && is_line_empty(intern)) {
1931
0
    spl_filesystem_file_free_line(intern);
1932
0
    ret = spl_filesystem_file_read_line_ex(this_ptr, intern, silent);
1933
0
  }
1934
1935
0
  return ret;
1936
0
}
1937
/* }}} */
1938
1939
static void spl_filesystem_file_rewind(zval * this_ptr, spl_filesystem_object *intern) /* {{{ */
1940
0
{
1941
0
  if (!intern->u.file.stream) {
1942
0
    zend_throw_error(NULL, "Object not initialized");
1943
0
    return;
1944
0
  }
1945
0
  if (-1 == php_stream_rewind(intern->u.file.stream)) {
1946
0
    zend_throw_exception_ex(spl_ce_RuntimeException, 0, "Cannot rewind file %s", ZSTR_VAL(intern->file_name));
1947
0
    return;
1948
0
  }
1949
1950
0
  spl_filesystem_file_free_line(intern);
1951
0
  intern->u.file.current_line_num = 0;
1952
1953
0
  if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
1954
0
    spl_filesystem_file_read_line(this_ptr, intern, true);
1955
0
  }
1956
0
} /* }}} */
1957
1958
/* {{{ Construct a new file object */
1959
PHP_METHOD(SplFileObject, __construct)
1960
0
{
1961
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
1962
0
  zend_string *file_name = NULL;
1963
0
  zend_string *open_mode = ZSTR_CHAR('r');
1964
0
  zval *stream_context = NULL;
1965
0
  bool use_include_path = false;
1966
0
  size_t path_len;
1967
0
  zend_error_handling error_handling;
1968
1969
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "P|Sbr!", &file_name, &open_mode, &use_include_path, &stream_context) == FAILURE) {
1970
0
    RETURN_THROWS();
1971
0
  }
1972
1973
  /* Prevent reinitialization of Object */
1974
0
  if (UNEXPECTED(intern->u.file.stream)) {
1975
0
    zend_throw_error(NULL, "Cannot call constructor twice");
1976
0
    RETURN_THROWS();
1977
0
  }
1978
1979
0
  intern->u.file.open_mode = zend_string_copy(open_mode);
1980
  /* file_name and zcontext are copied by spl_filesystem_file_open() */
1981
0
  intern->file_name = file_name;
1982
0
  intern->u.file.zcontext = stream_context;
1983
1984
  /* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
1985
0
  zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
1986
0
  zend_result retval = spl_filesystem_file_open(intern, use_include_path);
1987
0
  zend_restore_error_handling(&error_handling);
1988
0
  if (retval == FAILURE) {
1989
0
    RETURN_THROWS();
1990
0
  }
1991
1992
0
  path_len = strlen(intern->u.file.stream->orig_path);
1993
1994
0
  if (path_len > 1 && IS_SLASH_AT(intern->u.file.stream->orig_path, path_len-1)) {
1995
0
    path_len--;
1996
0
  }
1997
1998
0
  while (path_len > 1 && !IS_SLASH_AT(intern->u.file.stream->orig_path, path_len-1)) {
1999
0
    path_len--;
2000
0
  }
2001
2002
0
  if (path_len) {
2003
0
    path_len--;
2004
0
  }
2005
2006
0
  intern->path = zend_string_init(intern->u.file.stream->orig_path, path_len, 0);
2007
0
} /* }}} */
2008
2009
/* {{{ Construct a new temp file object */
2010
PHP_METHOD(SplTempFileObject, __construct)
2011
0
{
2012
0
  zend_string *file_name;
2013
0
  zend_long max_memory = PHP_STREAM_MAX_MEM;
2014
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2015
0
  zend_error_handling error_handling;
2016
2017
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &max_memory) == FAILURE) {
2018
0
    RETURN_THROWS();
2019
0
  }
2020
2021
  /* Prevent reinitialization of Object */
2022
0
  if (UNEXPECTED(intern->u.file.stream)) {
2023
0
    zend_throw_error(NULL, "Cannot call constructor twice");
2024
0
    RETURN_THROWS();
2025
0
  }
2026
2027
0
  if (max_memory < 0) {
2028
0
    file_name = ZSTR_INIT_LITERAL("php://memory", 0);
2029
0
  } else if (ZEND_NUM_ARGS()) {
2030
0
    file_name = zend_strpprintf(0, "php://temp/maxmemory:" ZEND_LONG_FMT, max_memory);
2031
0
  } else {
2032
0
    file_name = ZSTR_INIT_LITERAL("php://temp", 0);
2033
0
  }
2034
0
  intern->file_name = file_name;
2035
0
  intern->u.file.open_mode = ZSTR_INIT_LITERAL("wb", 0);
2036
2037
  /* spl_filesystem_file_open() can generate E_WARNINGs which we want to promote to exceptions */
2038
0
  zend_replace_error_handling(EH_THROW, spl_ce_RuntimeException, &error_handling);
2039
0
  if (spl_filesystem_file_open(intern, /* use_include_path */ false) == SUCCESS) {
2040
0
    intern->path = ZSTR_EMPTY_ALLOC();
2041
0
  }
2042
0
  zend_string_release(file_name);
2043
0
  zend_restore_error_handling(&error_handling);
2044
0
} /* }}} */
2045
2046
/* {{{ Rewind the file and read the first line */
2047
PHP_METHOD(SplFileObject, rewind)
2048
0
{
2049
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2050
2051
0
  ZEND_PARSE_PARAMETERS_NONE();
2052
2053
0
  spl_filesystem_file_rewind(ZEND_THIS, intern);
2054
0
} /* }}} */
2055
2056
/* {{{ Return whether end of file is reached */
2057
PHP_METHOD(SplFileObject, eof)
2058
0
{
2059
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2060
2061
0
  ZEND_PARSE_PARAMETERS_NONE();
2062
2063
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2064
2065
0
  RETURN_BOOL(php_stream_eof(intern->u.file.stream));
2066
0
} /* }}} */
2067
2068
/* {{{ Return !eof() */
2069
PHP_METHOD(SplFileObject, valid)
2070
0
{
2071
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2072
2073
0
  ZEND_PARSE_PARAMETERS_NONE();
2074
2075
0
  if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2076
0
    RETURN_BOOL(intern->u.file.current_line || !Z_ISUNDEF(intern->u.file.current_zval));
2077
0
  }
2078
0
  if (!intern->u.file.stream) {
2079
0
    RETURN_FALSE;
2080
0
  }
2081
0
  RETURN_BOOL(!php_stream_eof(intern->u.file.stream));
2082
0
} /* }}} */
2083
2084
/* {{{ Return next line from file */
2085
PHP_METHOD(SplFileObject, fgets)
2086
0
{
2087
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2088
2089
0
  ZEND_PARSE_PARAMETERS_NONE();
2090
2091
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2092
2093
0
  if (intern->u.file.current_line) {
2094
0
    RETVAL_STR_COPY(intern->u.file.current_line);
2095
0
    spl_filesystem_file_free_line(intern);
2096
0
    intern->u.file.current_line_num++;
2097
0
  } else {
2098
0
    if (spl_filesystem_file_read_ex(intern, /* silent */ true, /* line_add */ 1, /* csv */ false) == FAILURE) {
2099
0
      if (php_stream_eof(intern->u.file.stream)) {
2100
0
        RETURN_EMPTY_STRING();
2101
0
      }
2102
0
      spl_filesystem_file_cannot_read(intern);
2103
0
      RETURN_THROWS();
2104
0
    }
2105
0
    RETVAL_STR_COPY(intern->u.file.current_line);
2106
0
    spl_filesystem_file_free_line(intern);
2107
0
  }
2108
0
} /* }}} */
2109
2110
/* {{{ Return current line from file */
2111
PHP_METHOD(SplFileObject, current)
2112
0
{
2113
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2114
2115
0
  ZEND_PARSE_PARAMETERS_NONE();
2116
2117
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2118
2119
0
  if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
2120
0
    spl_filesystem_file_read_line(ZEND_THIS, intern, true);
2121
0
  }
2122
0
  if (intern->u.file.current_line && (!SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV) || Z_ISUNDEF(intern->u.file.current_zval))) {
2123
0
    RETURN_STR_COPY(intern->u.file.current_line);
2124
0
  } else if (!Z_ISUNDEF(intern->u.file.current_zval)) {
2125
0
    ZEND_ASSERT(!Z_ISREF(intern->u.file.current_zval));
2126
0
    ZEND_ASSERT(Z_TYPE(intern->u.file.current_zval) == IS_ARRAY);
2127
0
    RETURN_COPY(&intern->u.file.current_zval);
2128
0
  }
2129
0
  RETURN_FALSE;
2130
0
} /* }}} */
2131
2132
/* {{{ Return line number */
2133
PHP_METHOD(SplFileObject, key)
2134
0
{
2135
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2136
2137
0
  ZEND_PARSE_PARAMETERS_NONE();
2138
2139
  /* Do not read the next line to support correct counting with fgetc()
2140
  if (!intern->u.file.current_line) {
2141
    spl_filesystem_file_read_line(ZEND_THIS, intern);
2142
  } */
2143
0
  RETURN_LONG(intern->u.file.current_line_num);
2144
0
} /* }}} */
2145
2146
/* {{{ Read next line */
2147
PHP_METHOD(SplFileObject, next)
2148
0
{
2149
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2150
2151
0
  ZEND_PARSE_PARAMETERS_NONE();
2152
2153
0
  if (!intern->u.file.current_line && Z_ISUNDEF(intern->u.file.current_zval)) {
2154
0
    if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == FAILURE) {
2155
0
      return;
2156
0
    }
2157
0
  }
2158
2159
0
  spl_filesystem_file_free_line(intern);
2160
0
  if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2161
0
    spl_filesystem_file_read_line(ZEND_THIS, intern, true);
2162
0
  }
2163
0
  intern->u.file.current_line_num++;
2164
0
} /* }}} */
2165
2166
/* {{{ Set file handling flags */
2167
PHP_METHOD(SplFileObject, setFlags)
2168
0
{
2169
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2170
2171
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &intern->flags) == FAILURE) {
2172
0
    RETURN_THROWS();
2173
0
  }
2174
0
} /* }}} */
2175
2176
/* {{{ Get file handling flags */
2177
PHP_METHOD(SplFileObject, getFlags)
2178
0
{
2179
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2180
2181
0
  ZEND_PARSE_PARAMETERS_NONE();
2182
2183
0
  RETURN_LONG(intern->flags & SPL_FILE_OBJECT_MASK);
2184
0
} /* }}} */
2185
2186
/* {{{ Set maximum line length */
2187
PHP_METHOD(SplFileObject, setMaxLineLen)
2188
0
{
2189
0
  zend_long max_len;
2190
2191
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2192
2193
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &max_len) == FAILURE) {
2194
0
    RETURN_THROWS();
2195
0
  }
2196
2197
0
  if (max_len < 0) {
2198
0
    zend_argument_value_error(1, "must be greater than or equal to 0");
2199
0
    RETURN_THROWS();
2200
0
  }
2201
2202
0
  intern->u.file.max_line_len = max_len;
2203
0
} /* }}} */
2204
2205
/* {{{ Get maximum line length */
2206
PHP_METHOD(SplFileObject, getMaxLineLen)
2207
0
{
2208
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2209
2210
0
  ZEND_PARSE_PARAMETERS_NONE();
2211
2212
0
  RETURN_LONG((zend_long)intern->u.file.max_line_len);
2213
0
} /* }}} */
2214
2215
/* {{{ Return false */
2216
PHP_METHOD(SplFileObject, hasChildren)
2217
0
{
2218
0
  ZEND_PARSE_PARAMETERS_NONE();
2219
2220
0
  RETURN_FALSE;
2221
0
} /* }}} */
2222
2223
/* {{{ Read NULL */
2224
PHP_METHOD(SplFileObject, getChildren)
2225
0
{
2226
0
  ZEND_PARSE_PARAMETERS_NONE();
2227
  /* return NULL */
2228
0
} /* }}} */
2229
2230
static int spl_csv_enclosure_param_handling(const zend_string* escape_str, const spl_filesystem_object *intern, uint32_t arg_num)
2231
0
{
2232
0
  if (escape_str == NULL) {
2233
0
    if (intern->u.file.is_escape_default) {
2234
0
      php_error_docref(NULL, E_DEPRECATED, "the $escape parameter must be provided,"
2235
0
        " as its default value will change,"
2236
0
        " either explicitly or via SplFileObject::setCsvControl()");
2237
0
      if (UNEXPECTED(EG(exception))) {
2238
0
        return PHP_CSV_ESCAPE_ERROR;
2239
0
      }
2240
0
    }
2241
0
    return intern->u.file.escape;
2242
0
  } else {
2243
0
    return php_csv_handle_escape_argument(escape_str, arg_num);
2244
0
  }
2245
0
}
2246
2247
/* {{{ Return current line as CSV */
2248
PHP_METHOD(SplFileObject, fgetcsv)
2249
0
{
2250
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2251
0
  char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure;
2252
0
  char *delim = NULL, *enclo = NULL;
2253
0
  size_t d_len = 0, e_len = 0;
2254
0
  zend_string *escape_str = NULL;
2255
2256
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ssS", &delim, &d_len, &enclo, &e_len, &escape_str) == FAILURE) {
2257
0
    RETURN_THROWS();
2258
0
  }
2259
2260
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2261
2262
0
  if (delim) {
2263
0
    if (d_len != 1) {
2264
0
      zend_argument_value_error(1, "must be a single character");
2265
0
      RETURN_THROWS();
2266
0
    }
2267
0
    delimiter = delim[0];
2268
0
  }
2269
0
  if (enclo) {
2270
0
    if (e_len != 1) {
2271
0
      zend_argument_value_error(2, "must be a single character");
2272
0
      RETURN_THROWS();
2273
0
    }
2274
0
    enclosure = enclo[0];
2275
0
  }
2276
0
  int escape_char = spl_csv_enclosure_param_handling(escape_str, intern, 3);
2277
0
  if (escape_char == PHP_CSV_ESCAPE_ERROR) {
2278
0
    RETURN_THROWS();
2279
0
  }
2280
2281
0
  if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape_char, return_value, true) == FAILURE) {
2282
0
    RETURN_FALSE;
2283
0
  }
2284
0
}
2285
/* }}} */
2286
2287
/* {{{ Output a field array as a CSV line */
2288
PHP_METHOD(SplFileObject, fputcsv)
2289
0
{
2290
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2291
0
  char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure;
2292
0
  char *delim = NULL, *enclo = NULL;
2293
0
  size_t d_len = 0, e_len = 0;
2294
0
  zend_long ret;
2295
0
  zval *fields = NULL;
2296
0
  zend_string *escape_str = NULL;
2297
0
  zend_string *eol = NULL;
2298
2299
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|ssSS", &fields, &delim, &d_len, &enclo, &e_len, &escape_str, &eol) == FAILURE) {
2300
0
    RETURN_THROWS();
2301
0
  }
2302
2303
0
  if (delim) {
2304
0
    if (d_len != 1) {
2305
0
      zend_argument_value_error(2, "must be a single character");
2306
0
      RETURN_THROWS();
2307
0
    }
2308
0
    delimiter = delim[0];
2309
0
  }
2310
0
  if (enclo) {
2311
0
    if (e_len != 1) {
2312
0
      zend_argument_value_error(3, "must be a single character");
2313
0
      RETURN_THROWS();
2314
0
    }
2315
0
    enclosure = enclo[0];
2316
0
  }
2317
0
  int escape_char = spl_csv_enclosure_param_handling(escape_str, intern, 4);
2318
0
  if (escape_char == PHP_CSV_ESCAPE_ERROR) {
2319
0
    RETURN_THROWS();
2320
0
  }
2321
2322
0
  ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape_char, eol);
2323
0
  if (ret < 0) {
2324
0
    RETURN_FALSE;
2325
0
  }
2326
0
  RETURN_LONG(ret);
2327
0
}
2328
/* }}} */
2329
2330
/* {{{ Set the delimiter, enclosure and escape character used in fgetcsv */
2331
PHP_METHOD(SplFileObject, setCsvControl)
2332
0
{
2333
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2334
0
  char delimiter = ',', enclosure = '"';
2335
0
  char *delim = NULL, *enclo = NULL;
2336
0
  size_t d_len = 0, e_len = 0;
2337
0
  zend_string *escape_str = NULL;
2338
2339
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ssS", &delim, &d_len, &enclo, &e_len, &escape_str) == FAILURE) {
2340
0
    RETURN_THROWS();
2341
0
  }
2342
2343
0
  if (delim) {
2344
0
    if (d_len != 1) {
2345
0
      zend_argument_value_error(1, "must be a single character");
2346
0
      RETURN_THROWS();
2347
0
    }
2348
0
    delimiter = delim[0];
2349
0
  }
2350
0
  if (enclo) {
2351
0
    if (e_len != 1) {
2352
0
      zend_argument_value_error(2, "must be a single character");
2353
0
      RETURN_THROWS();
2354
0
    }
2355
0
    enclosure = enclo[0];
2356
0
  }
2357
0
  int escape_char = php_csv_handle_escape_argument(escape_str, 3);
2358
0
  if (escape_char == PHP_CSV_ESCAPE_ERROR) {
2359
0
    RETURN_THROWS();
2360
0
  }
2361
0
  if (escape_str != NULL) {
2362
0
    intern->u.file.is_escape_default = false;
2363
0
  }
2364
2365
0
  intern->u.file.delimiter = delimiter;
2366
0
  intern->u.file.enclosure = enclosure;
2367
0
  intern->u.file.escape    = escape_char;
2368
0
}
2369
/* }}} */
2370
2371
/* {{{ Get the delimiter, enclosure and escape character used in fgetcsv */
2372
PHP_METHOD(SplFileObject, getCsvControl)
2373
0
{
2374
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2375
0
  char delimiter[2], enclosure[2], escape[2];
2376
2377
0
  ZEND_PARSE_PARAMETERS_NONE();
2378
2379
0
  array_init(return_value);
2380
2381
0
  delimiter[0] = intern->u.file.delimiter;
2382
0
  delimiter[1] = '\0';
2383
0
  enclosure[0] = intern->u.file.enclosure;
2384
0
  enclosure[1] = '\0';
2385
0
  if (intern->u.file.escape == PHP_CSV_NO_ESCAPE) {
2386
0
    escape[0] = '\0';
2387
0
  } else {
2388
0
    escape[0] = (unsigned char) intern->u.file.escape;
2389
0
    escape[1] = '\0';
2390
0
  }
2391
2392
0
  add_next_index_string(return_value, delimiter);
2393
0
  add_next_index_string(return_value, enclosure);
2394
0
  add_next_index_string(return_value, escape);
2395
0
}
2396
/* }}} */
2397
2398
/* {{{ Portable file locking */
2399
PHP_METHOD(SplFileObject, flock)
2400
0
{
2401
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2402
0
  zval *wouldblock = NULL;
2403
0
  zend_long operation = 0;
2404
2405
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|z", &operation, &wouldblock) == FAILURE) {
2406
0
    RETURN_THROWS();
2407
0
  }
2408
2409
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2410
2411
0
  php_flock_common(intern->u.file.stream, operation, 1, wouldblock, return_value);
2412
0
}
2413
/* }}} */
2414
2415
/* {{{ Flush the file */
2416
PHP_METHOD(SplFileObject, fflush)
2417
0
{
2418
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2419
2420
0
  ZEND_PARSE_PARAMETERS_NONE();
2421
2422
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2423
2424
0
  RETURN_BOOL(!php_stream_flush(intern->u.file.stream));
2425
0
} /* }}} */
2426
2427
/* {{{ Return current file position */
2428
PHP_METHOD(SplFileObject, ftell)
2429
0
{
2430
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2431
0
  zend_long ret;
2432
2433
0
  ZEND_PARSE_PARAMETERS_NONE();
2434
2435
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2436
2437
0
  ret = php_stream_tell(intern->u.file.stream);
2438
2439
0
  if (ret == -1) {
2440
0
    RETURN_FALSE;
2441
0
  } else {
2442
0
    RETURN_LONG(ret);
2443
0
  }
2444
0
} /* }}} */
2445
2446
/* {{{ Seek to a position */
2447
PHP_METHOD(SplFileObject, fseek)
2448
0
{
2449
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2450
0
  zend_long pos, whence = SEEK_SET;
2451
2452
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|l", &pos, &whence) == FAILURE) {
2453
0
    RETURN_THROWS();
2454
0
  }
2455
2456
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2457
2458
0
  spl_filesystem_file_free_line(intern);
2459
0
  RETURN_LONG(php_stream_seek(intern->u.file.stream, pos, (int)whence));
2460
0
} /* }}} */
2461
2462
/* {{{ Get a character from the file */
2463
PHP_METHOD(SplFileObject, fgetc)
2464
0
{
2465
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2466
2467
0
  ZEND_PARSE_PARAMETERS_NONE();
2468
2469
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2470
2471
0
  spl_filesystem_file_free_line(intern);
2472
2473
0
  int result = php_stream_getc(intern->u.file.stream);
2474
2475
0
  if (result == EOF) {
2476
0
    RETURN_FALSE;
2477
0
  }
2478
0
  if (result == '\n') {
2479
0
    intern->u.file.current_line_num++;
2480
0
  }
2481
2482
0
  RETURN_STR(ZSTR_CHAR((zend_uchar)result));
2483
0
} /* }}} */
2484
2485
/* {{{ Output all remaining data from a file pointer */
2486
PHP_METHOD(SplFileObject, fpassthru)
2487
0
{
2488
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2489
2490
0
  ZEND_PARSE_PARAMETERS_NONE();
2491
2492
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2493
2494
0
  RETURN_LONG(php_stream_passthru(intern->u.file.stream));
2495
0
} /* }}} */
2496
2497
/* {{{ Implements a mostly ANSI compatible fscanf() */
2498
PHP_METHOD(SplFileObject, fscanf)
2499
0
{
2500
0
  uint32_t num_varargs = 0;
2501
0
  zend_string *format_str;
2502
0
  zval *varargs= NULL;
2503
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2504
2505
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S*", &format_str, &varargs, &num_varargs) == FAILURE) {
2506
0
    RETURN_THROWS();
2507
0
  }
2508
2509
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2510
2511
  /* Get next line */
2512
0
  if (spl_filesystem_file_read(intern, /* silent */ false, /* csv */ false) == FAILURE) {
2513
0
    RETURN_THROWS();
2514
0
  }
2515
2516
0
  int result = php_sscanf_internal(ZSTR_VAL(intern->u.file.current_line), ZSTR_VAL(format_str), (int)num_varargs, varargs, 0, return_value);
2517
2518
0
  if (SCAN_ERROR_WRONG_PARAM_COUNT == result) {
2519
0
    zend_wrong_param_count();
2520
0
    RETURN_THROWS();
2521
0
  }
2522
0
}
2523
/* }}} */
2524
2525
/* {{{ Binary-safe file write */
2526
PHP_METHOD(SplFileObject, fwrite)
2527
0
{
2528
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2529
0
  char *str;
2530
0
  size_t str_len;
2531
0
  zend_long length = 0;
2532
0
  bool length_is_null = true;
2533
0
  ssize_t written;
2534
2535
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
2536
0
    Z_PARAM_STRING(str, str_len)
2537
0
    Z_PARAM_OPTIONAL
2538
0
    Z_PARAM_LONG_OR_NULL(length, length_is_null)
2539
0
  ZEND_PARSE_PARAMETERS_END();
2540
2541
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2542
2543
0
  if (!length_is_null) {
2544
0
    if (length >= 0) {
2545
0
      str_len = MIN((size_t)length, str_len);
2546
0
    } else {
2547
      /* Negative length given, nothing to write */
2548
0
      str_len = 0;
2549
0
    }
2550
0
  }
2551
0
  if (!str_len) {
2552
0
    RETURN_LONG(0);
2553
0
  }
2554
2555
0
  written = php_stream_write(intern->u.file.stream, str, str_len);
2556
0
  if (written < 0) {
2557
0
    RETURN_FALSE;
2558
0
  }
2559
0
  RETURN_LONG(written);
2560
0
} /* }}} */
2561
2562
PHP_METHOD(SplFileObject, fread)
2563
0
{
2564
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2565
0
  zend_long length = 0;
2566
0
  zend_string *str;
2567
2568
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &length) == FAILURE) {
2569
0
    RETURN_THROWS();
2570
0
  }
2571
2572
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2573
2574
0
  if (length <= 0) {
2575
0
    zend_argument_value_error(1, "must be greater than 0");
2576
0
    RETURN_THROWS();
2577
0
  }
2578
2579
0
  str = php_stream_read_to_str(intern->u.file.stream, length);
2580
0
  if (!str) {
2581
0
    RETURN_FALSE;
2582
0
  }
2583
0
  RETURN_STR(str);
2584
0
}
2585
2586
/* {{{ Stat() on a filehandle */
2587
PHP_METHOD(SplFileObject, fstat)
2588
0
{
2589
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2590
2591
0
  ZEND_PARSE_PARAMETERS_NONE();
2592
2593
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2594
2595
0
  php_fstat(intern->u.file.stream, return_value);
2596
0
}
2597
/* }}} */
2598
2599
/* {{{ Truncate file to 'size' length */
2600
PHP_METHOD(SplFileObject, ftruncate)
2601
0
{
2602
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2603
0
  zend_long size;
2604
2605
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &size) == FAILURE) {
2606
0
    RETURN_THROWS();
2607
0
  }
2608
2609
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2610
2611
0
  if (size < 0) {
2612
0
    zend_argument_value_error(1, "must be greater than or equal to 0");
2613
0
    RETURN_THROWS();
2614
0
  }
2615
2616
2617
0
  if (!php_stream_truncate_supported(intern->u.file.stream)) {
2618
0
    zend_throw_exception_ex(spl_ce_LogicException, 0, "Can't truncate file %s", ZSTR_VAL(intern->file_name));
2619
0
    RETURN_THROWS();
2620
0
  }
2621
2622
0
  RETURN_BOOL(0 == php_stream_truncate_set_size(intern->u.file.stream, size));
2623
0
} /* }}} */
2624
2625
/* {{{ Seek to specified line */
2626
PHP_METHOD(SplFileObject, seek)
2627
0
{
2628
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2629
0
  zend_long line_pos, i;
2630
2631
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &line_pos) == FAILURE) {
2632
0
    RETURN_THROWS();
2633
0
  }
2634
2635
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2636
2637
0
  if (line_pos < 0) {
2638
0
    zend_argument_value_error(1, "must be greater than or equal to 0");
2639
0
    RETURN_THROWS();
2640
0
  }
2641
2642
0
  spl_filesystem_file_rewind(ZEND_THIS, intern);
2643
2644
0
  for (i = 0; i < line_pos; i++) {
2645
0
    if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == FAILURE) {
2646
0
      break;
2647
0
    }
2648
0
  }
2649
0
  if (line_pos > 0 && !SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_AHEAD)) {
2650
0
    intern->u.file.current_line_num++;
2651
0
    spl_filesystem_file_free_line(intern);
2652
0
  }
2653
0
} /* }}} */
2654
2655
PHP_METHOD(SplFileObject, __toString)
2656
0
{
2657
0
  ZEND_PARSE_PARAMETERS_NONE();
2658
2659
0
  spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
2660
2661
0
  CHECK_SPL_FILE_OBJECT_IS_INITIALIZED(intern);
2662
2663
0
  if (!intern->u.file.current_line) {
2664
0
    ZEND_ASSERT(Z_ISUNDEF(intern->u.file.current_zval));
2665
0
    if (spl_filesystem_file_read_line(ZEND_THIS, intern, true) == FAILURE) {
2666
0
      RETURN_EMPTY_STRING();
2667
0
    }
2668
0
  }
2669
2670
0
  RETURN_STR_COPY(intern->u.file.current_line);
2671
0
}
2672
2673
/* {{{ PHP_MINIT_FUNCTION(spl_directory) */
2674
PHP_MINIT_FUNCTION(spl_directory)
2675
2
{
2676
2
  spl_ce_SplFileInfo = register_class_SplFileInfo(zend_ce_stringable);
2677
2
  spl_ce_SplFileInfo->create_object = spl_filesystem_object_new;
2678
2
  spl_ce_SplFileInfo->default_object_handlers = &spl_filesystem_object_handlers;
2679
2680
2
  memcpy(&spl_filesystem_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
2681
2
  spl_filesystem_object_handlers.offset = offsetof(spl_filesystem_object, std);
2682
2
  spl_filesystem_object_handlers.clone_obj = spl_filesystem_object_clone;
2683
2
  spl_filesystem_object_handlers.dtor_obj = spl_filesystem_object_destroy_object;
2684
2
  spl_filesystem_object_handlers.free_obj = spl_filesystem_object_free_storage;
2685
2686
2
  spl_ce_DirectoryIterator = register_class_DirectoryIterator(spl_ce_SplFileInfo, spl_ce_SeekableIterator);
2687
2
  spl_ce_DirectoryIterator->create_object = spl_filesystem_object_new;
2688
2
  spl_ce_DirectoryIterator->get_iterator = spl_filesystem_dir_get_iterator;
2689
2690
2
  spl_ce_FilesystemIterator = register_class_FilesystemIterator(spl_ce_DirectoryIterator);
2691
2
  spl_ce_FilesystemIterator->create_object = spl_filesystem_object_new;
2692
2
  spl_ce_FilesystemIterator->get_iterator = spl_filesystem_tree_get_iterator;
2693
2694
2
  spl_ce_RecursiveDirectoryIterator = register_class_RecursiveDirectoryIterator(spl_ce_FilesystemIterator, spl_ce_RecursiveIterator);
2695
2
  spl_ce_RecursiveDirectoryIterator->create_object = spl_filesystem_object_new;
2696
2697
2
  memcpy(&spl_filesystem_object_check_handlers, &spl_filesystem_object_handlers, sizeof(zend_object_handlers));
2698
2
  spl_filesystem_object_check_handlers.clone_obj = NULL;
2699
2
  spl_filesystem_object_check_handlers.get_method = spl_filesystem_object_get_method_check;
2700
2701
2
  spl_ce_GlobIterator = register_class_GlobIterator(spl_ce_FilesystemIterator, zend_ce_countable);
2702
2
  spl_ce_GlobIterator->create_object = spl_filesystem_object_new;
2703
2
  spl_ce_GlobIterator->default_object_handlers = &spl_filesystem_object_check_handlers;
2704
2705
2
  spl_ce_SplFileObject = register_class_SplFileObject(spl_ce_SplFileInfo, spl_ce_RecursiveIterator, spl_ce_SeekableIterator);
2706
2
  spl_ce_SplFileObject->default_object_handlers = &spl_filesystem_object_check_handlers;
2707
2
  spl_ce_SplFileObject->create_object = spl_filesystem_object_new;
2708
2709
2
  spl_ce_SplTempFileObject = register_class_SplTempFileObject(spl_ce_SplFileObject);
2710
2
  spl_ce_SplTempFileObject->create_object = spl_filesystem_object_new;
2711
2712
2
  return SUCCESS;
2713
2
}
2714
/* }}} */