Coverage Report

Created: 2025-12-14 06:06

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