Coverage Report

Created: 2025-11-16 06:23

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