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    | Authors: Marcus Boerger <helly@php.net>                              |
14    +----------------------------------------------------------------------+
15  */
16 
17 #ifndef SPL_DIRECTORY_H
18 #define SPL_DIRECTORY_H
19 
20 #include "php.h"
21 #include "php_spl.h"
22 
23 extern PHPAPI zend_class_entry *spl_ce_SplFileInfo;
24 extern PHPAPI zend_class_entry *spl_ce_DirectoryIterator;
25 extern PHPAPI zend_class_entry *spl_ce_FilesystemIterator;
26 extern PHPAPI zend_class_entry *spl_ce_RecursiveDirectoryIterator;
27 extern PHPAPI zend_class_entry *spl_ce_GlobIterator;
28 extern PHPAPI zend_class_entry *spl_ce_SplFileObject;
29 extern PHPAPI zend_class_entry *spl_ce_SplTempFileObject;
30 
31 PHP_MINIT_FUNCTION(spl_directory);
32 
33 typedef enum {
34 	SPL_FS_INFO, /* must be 0 */
35 	SPL_FS_DIR,
36 	SPL_FS_FILE
37 } SPL_FS_OBJ_TYPE;
38 
39 typedef struct _spl_filesystem_object  spl_filesystem_object;
40 
41 typedef void (*spl_foreign_dtor_t)(spl_filesystem_object *object);
42 typedef void (*spl_foreign_clone_t)(spl_filesystem_object *src, spl_filesystem_object *dst);
43 
44 PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_t *len);
45 
46 typedef struct _spl_other_handler {
47 	spl_foreign_dtor_t     dtor;
48 	spl_foreign_clone_t    clone;
49 } spl_other_handler;
50 
51 /* define an overloaded iterator structure */
52 typedef struct {
53 	zend_object_iterator  intern;
54 	zval                  current;
55 	void                 *object;
56 } spl_filesystem_iterator;
57 
58 struct _spl_filesystem_object {
59 	void               *oth;
60 	const spl_other_handler  *oth_handler;
61 	zend_string        *path;
62 	zend_string        *orig_path;
63 	zend_string        *file_name;
64 	SPL_FS_OBJ_TYPE    type;
65 	zend_long               flags;
66 	zend_class_entry   *file_class;
67 	zend_class_entry   *info_class;
68 	union {
69 		struct {
70 			php_stream         *dirp;
71 			zend_string        *sub_path;
72 			int                index;
73 			int                is_recursive;
74 			zend_function      *func_rewind;
75 			zend_function      *func_next;
76 			zend_function      *func_valid;
77 			php_stream_dirent  entry;
78 		} dir;
79 		struct {
80 			php_stream         *stream;
81 			php_stream_context *context;
82 			zval               *zcontext;
83 			zend_string        *open_mode;
84 			zval               current_zval;
85 			char               *current_line;
86 			size_t             current_line_len;
87 			size_t             max_line_len;
88 			zend_long               current_line_num;
89 			zval               zresource;
90 			zend_function      *func_getCurr;
91 			char               delimiter;
92 			char               enclosure;
93 			int                escape;
94 		} file;
95 	} u;
96 	zend_object        std;
97 };
98 
spl_filesystem_from_obj(zend_object * obj)99 static inline spl_filesystem_object *spl_filesystem_from_obj(zend_object *obj) /* {{{ */ {
100 	return (spl_filesystem_object*)((char*)(obj) - XtOffsetOf(spl_filesystem_object, std));
101 }
102 /* }}} */
103 
104 #define Z_SPLFILESYSTEM_P(zv)  spl_filesystem_from_obj(Z_OBJ_P((zv)))
105 
spl_filesystem_object_to_iterator(spl_filesystem_object * obj)106 static inline spl_filesystem_iterator* spl_filesystem_object_to_iterator(spl_filesystem_object *obj)
107 {
108 	spl_filesystem_iterator    *it;
109 
110 	it = ecalloc(1, sizeof(spl_filesystem_iterator));
111 	it->object = (void *)obj;
112 	zend_iterator_init(&it->intern);
113 	return it;
114 }
115 
spl_filesystem_iterator_to_object(spl_filesystem_iterator * it)116 static inline spl_filesystem_object* spl_filesystem_iterator_to_object(spl_filesystem_iterator *it)
117 {
118 	return (spl_filesystem_object*)it->object;
119 }
120 
121 #define SPL_FILE_OBJECT_DROP_NEW_LINE      0x00000001 /* drop new lines */
122 #define SPL_FILE_OBJECT_READ_AHEAD         0x00000002 /* read on rewind/next */
123 #define SPL_FILE_OBJECT_SKIP_EMPTY         0x00000004 /* skip empty lines */
124 #define SPL_FILE_OBJECT_READ_CSV           0x00000008 /* read via fgetcsv */
125 #define SPL_FILE_OBJECT_MASK               0x0000000F /* read via fgetcsv */
126 
127 #define SPL_FILE_DIR_CURRENT_AS_FILEINFO   0x00000000 /* make RecursiveDirectoryTree::current() return SplFileInfo */
128 #define SPL_FILE_DIR_CURRENT_AS_SELF       0x00000010 /* make RecursiveDirectoryTree::current() return getSelf() */
129 #define SPL_FILE_DIR_CURRENT_AS_PATHNAME   0x00000020 /* make RecursiveDirectoryTree::current() return getPathname() */
130 #define SPL_FILE_DIR_CURRENT_MODE_MASK     0x000000F0 /* mask RecursiveDirectoryTree::current() */
131 #define SPL_FILE_DIR_CURRENT(intern,mode)  ((intern->flags&SPL_FILE_DIR_CURRENT_MODE_MASK)==mode)
132 
133 #define SPL_FILE_DIR_KEY_AS_PATHNAME       0x00000000 /* make RecursiveDirectoryTree::key() return getPathname() */
134 #define SPL_FILE_DIR_KEY_AS_FILENAME       0x00000100 /* make RecursiveDirectoryTree::key() return getFilename() */
135 #define SPL_FILE_DIR_KEY_MODE_MASK         0x00000F00 /* mask RecursiveDirectoryTree::key() */
136 #define SPL_FILE_DIR_KEY(intern,mode)      ((intern->flags&SPL_FILE_DIR_KEY_MODE_MASK)==mode)
137 
138 #define SPL_FILE_DIR_SKIPDOTS              0x00001000 /* Tells whether it should skip dots or not */
139 #define SPL_FILE_DIR_UNIXPATHS             0x00002000 /* Whether to unixify path separators */
140 #define SPL_FILE_DIR_FOLLOW_SYMLINKS       0x00004000 /* make RecursiveDirectoryTree::hasChildren() follow symlinks */
141 #define SPL_FILE_DIR_OTHERS_MASK           0x00007000 /* mask used for get/setFlags */
142 
143 #endif /* SPL_DIRECTORY_H */
144