Skip to content

Commit b85f0d1

Browse files
committed
Convert file_info resources to objects
Besides our general desire to get rid of the legacy resource types, this is particularly appealing for fileinfo, because there are already respective objects. Closes GH-5987.
1 parent 54c8661 commit b85f0d1

13 files changed

+79
-111
lines changed

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ PHP 8.1 UPGRADE NOTES
1919
1. Backward Incompatible Changes
2020
========================================
2121

22+
- Fileinfo:
23+
. The fileinfo functions now accept and return, respectively, finfo objects
24+
instead of resources.
25+
2226
- MySQLi:
2327
. mysqli_fetch_fields() and mysqli_fetch_field_direct() will now always return
2428
zero for max_length. You can compute this information by iterating over the

ext/fileinfo/fileinfo.c

Lines changed: 20 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,12 @@ typedef struct _finfo_object {
5151
zend_object zo;
5252
} finfo_object;
5353

54-
#define FILEINFO_DECLARE_INIT_OBJECT(object) \
55-
zval *object = getThis();
56-
5754
static inline finfo_object *php_finfo_fetch_object(zend_object *obj) {
5855
return (finfo_object *)((char*)(obj) - XtOffsetOf(finfo_object, zo));
5956
}
6057

6158
#define Z_FINFO_P(zv) php_finfo_fetch_object(Z_OBJ_P((zv)))
6259

63-
#define FILEINFO_REGISTER_OBJECT(_object, _ptr) \
64-
{ \
65-
finfo_object *obj; \
66-
obj = Z_FINFO_P(_object); \
67-
obj->ptr = _ptr; \
68-
}
69-
7060
#define FILEINFO_FROM_OBJECT(finfo, object) \
7161
{ \
7262
finfo_object *obj = Z_FINFO_P(object); \
@@ -112,20 +102,6 @@ PHP_FILEINFO_API zend_object *finfo_objects_new(zend_class_entry *class_type)
112102
options, magic_errno(magic), magic_error(magic)); \
113103
RETURN_FALSE; \
114104
}
115-
116-
/* True global resources - no need for thread safety here */
117-
static int le_fileinfo;
118-
/* }}} */
119-
120-
void finfo_resource_destructor(zend_resource *rsrc) /* {{{ */
121-
{
122-
if (rsrc->ptr) {
123-
php_fileinfo *finfo = (php_fileinfo *) rsrc->ptr;
124-
magic_close(finfo->magic);
125-
efree(rsrc->ptr);
126-
rsrc->ptr = NULL;
127-
}
128-
}
129105
/* }}} */
130106

131107
/* {{{ PHP_MINIT_FUNCTION */
@@ -144,8 +120,6 @@ PHP_MINIT_FUNCTION(finfo)
144120
finfo_object_handlers.free_obj = finfo_objects_free;
145121
finfo_object_handlers.clone_obj = NULL;
146122

147-
le_fileinfo = zend_register_list_destructors_ex(finfo_resource_destructor, NULL, "file_info", module_number);
148-
149123
REGISTER_LONG_CONSTANT("FILEINFO_NONE", MAGIC_NONE, CONST_CS|CONST_PERSISTENT);
150124
REGISTER_LONG_CONSTANT("FILEINFO_SYMLINK", MAGIC_SYMLINK, CONST_CS|CONST_PERSISTENT);
151125
REGISTER_LONG_CONSTANT("FILEINFO_MIME", MAGIC_MIME, CONST_CS|CONST_PERSISTENT);
@@ -204,14 +178,14 @@ PHP_MINFO_FUNCTION(fileinfo)
204178
}
205179
/* }}} */
206180

207-
/* {{{ Create a new fileinfo resource. */
181+
/* {{{ Construct a new fileinfo object. */
208182
PHP_FUNCTION(finfo_open)
209183
{
210184
zend_long options = MAGIC_NONE;
211185
char *file = NULL;
212186
size_t file_len = 0;
213187
php_fileinfo *finfo;
214-
FILEINFO_DECLARE_INIT_OBJECT(object)
188+
zval *object = getThis();
215189
char resolved_path[MAXPATHLEN];
216190
zend_error_handling zeh;
217191

@@ -287,30 +261,28 @@ PHP_FUNCTION(finfo_open)
287261
}
288262

289263
if (object) {
264+
finfo_object *obj;
290265
zend_restore_error_handling(&zeh);
291-
FILEINFO_REGISTER_OBJECT(object, finfo);
266+
obj = Z_FINFO_P(object);
267+
obj->ptr = finfo;
292268
} else {
293-
RETURN_RES(zend_register_resource(finfo, le_fileinfo));
269+
zend_object *zobj = finfo_objects_new(finfo_class_entry);
270+
finfo_object *obj = php_finfo_fetch_object(zobj);
271+
obj->ptr = finfo;
272+
RETURN_OBJ(zobj);
294273
}
295274
}
296275
/* }}} */
297276

298-
/* {{{ Close fileinfo resource. */
277+
/* {{{ Close fileinfo object - a NOP. */
299278
PHP_FUNCTION(finfo_close)
300279
{
301-
php_fileinfo *finfo;
302-
zval *zfinfo;
303-
304-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zfinfo) == FAILURE) {
305-
RETURN_THROWS();
306-
}
280+
zval *self;
307281

308-
if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
282+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &self, finfo_class_entry) == FAILURE) {
309283
RETURN_THROWS();
310284
}
311285

312-
zend_list_close(Z_RES_P(zfinfo));
313-
314286
RETURN_TRUE;
315287
}
316288
/* }}} */
@@ -320,22 +292,12 @@ PHP_FUNCTION(finfo_set_flags)
320292
{
321293
zend_long options;
322294
php_fileinfo *finfo;
323-
zval *zfinfo;
324-
FILEINFO_DECLARE_INIT_OBJECT(object)
295+
zval *self;
325296

326-
if (object) {
327-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &options) == FAILURE) {
328-
RETURN_THROWS();
329-
}
330-
FILEINFO_FROM_OBJECT(finfo, object);
331-
} else {
332-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rl", &zfinfo, &options) == FAILURE) {
333-
RETURN_THROWS();
334-
}
335-
if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
336-
RETURN_THROWS();
337-
}
297+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Ol", &self, finfo_class_entry, &options) == FAILURE) {
298+
RETURN_THROWS();
338299
}
300+
FILEINFO_FROM_OBJECT(finfo, self);
339301

340302
FINFO_SET_OPTION(finfo->magic, options)
341303
finfo->options = options;
@@ -354,12 +316,10 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
354316
char *ret_val = NULL, *buffer = NULL;
355317
size_t buffer_len;
356318
php_fileinfo *finfo = NULL;
357-
zval *zfinfo, *zcontext = NULL;
319+
zval *zcontext = NULL;
358320
zval *what;
359321
char mime_directory[] = "directory";
360-
361322
struct magic_set *magic = NULL;
362-
FILEINFO_DECLARE_INIT_OBJECT(object)
363323

364324
if (mimetype_emu) {
365325

@@ -389,19 +349,12 @@ static void _php_finfo_get_type(INTERNAL_FUNCTION_PARAMETERS, int mode, int mime
389349
php_error_docref(NULL, E_WARNING, "Failed to load magic database");
390350
goto common;
391351
}
392-
} else if (object) {
393-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|lr!", &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
394-
RETURN_THROWS();
395-
}
396-
FILEINFO_FROM_OBJECT(finfo, object);
397-
magic = finfo->magic;
398352
} else {
399-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs|lr!", &zfinfo, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
400-
RETURN_THROWS();
401-
}
402-
if ((finfo = (php_fileinfo *)zend_fetch_resource(Z_RES_P(zfinfo), "file_info", le_fileinfo)) == NULL) {
353+
zval *self;
354+
if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|lr!", &self, finfo_class_entry, &buffer, &buffer_len, &options, &zcontext) == FAILURE) {
403355
RETURN_THROWS();
404356
}
357+
FILEINFO_FROM_OBJECT(finfo, self);
405358
magic = finfo->magic;
406359
}
407360

ext/fileinfo/fileinfo.stub.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,21 @@ public function buffer(string $string, int $flags = FILEINFO_NONE, $context = nu
2828
public function set_flags(int $flags) {}
2929
}
3030

31-
/** @return resource|false */
32-
function finfo_open(int $flags = FILEINFO_NONE, string $magic_database = "") {}
31+
function finfo_open(int $flags = FILEINFO_NONE, string $magic_database = ""): finfo|false {}
3332

34-
/**
35-
* @param resource $finfo
36-
*/
37-
function finfo_close($finfo): bool {}
33+
function finfo_close(finfo $finfo): bool {}
3834

39-
/**
40-
* @param resource $finfo
41-
*/
42-
function finfo_set_flags($finfo, int $flags): bool {}
35+
function finfo_set_flags(finfo $finfo, int $flags): bool {}
4336

4437
/**
45-
* @param resource $finfo
4638
* @param resource|null $context
4739
*/
48-
function finfo_file($finfo, string $filename, int $flags = FILEINFO_NONE, $context = null): string|false {}
40+
function finfo_file(finfo $finfo, string $filename, int $flags = FILEINFO_NONE, $context = null): string|false {}
4941

5042
/**
51-
* @param resource $finfo
5243
* @param resource|null $context
5344
*/
54-
function finfo_buffer($finfo, string $string, int $flags = FILEINFO_NONE, $context = null): string|false {}
45+
function finfo_buffer(finfo $finfo, string $string, int $flags = FILEINFO_NONE, $context = null): string|false {}
5546

5647
/**
5748
* @param resource|string $filename

ext/fileinfo/fileinfo_arginfo.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: be858509df27550b51d8a7a51a3629eceb6d0aa6 */
2+
* Stub hash: 1282a20b1d007bbcc9c0d4efe400db43a5450307 */
33

4-
ZEND_BEGIN_ARG_INFO_EX(arginfo_finfo_open, 0, 0, 0)
4+
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_finfo_open, 0, 0, finfo, MAY_BE_FALSE)
55
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
66
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, magic_database, IS_STRING, 0, "\"\"")
77
ZEND_END_ARG_INFO()
88

99
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_finfo_close, 0, 1, _IS_BOOL, 0)
10-
ZEND_ARG_INFO(0, finfo)
10+
ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
1111
ZEND_END_ARG_INFO()
1212

1313
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_finfo_set_flags, 0, 2, _IS_BOOL, 0)
14-
ZEND_ARG_INFO(0, finfo)
14+
ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
1515
ZEND_ARG_TYPE_INFO(0, flags, IS_LONG, 0)
1616
ZEND_END_ARG_INFO()
1717

1818
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_finfo_file, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
19-
ZEND_ARG_INFO(0, finfo)
19+
ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
2020
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
2121
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
2222
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, context, "null")
2323
ZEND_END_ARG_INFO()
2424

2525
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_finfo_buffer, 0, 2, MAY_BE_STRING|MAY_BE_FALSE)
26-
ZEND_ARG_INFO(0, finfo)
26+
ZEND_ARG_OBJ_INFO(0, finfo, finfo, 0)
2727
ZEND_ARG_TYPE_INFO(0, string, IS_STRING, 0)
2828
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
2929
ZEND_ARG_INFO_WITH_DEFAULT_VALUE(0, context, "null")
@@ -33,7 +33,10 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_mime_content_type, 0, 1, MAY_BE_
3333
ZEND_ARG_INFO(0, filename)
3434
ZEND_END_ARG_INFO()
3535

36-
#define arginfo_class_finfo___construct arginfo_finfo_open
36+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo___construct, 0, 0, 0)
37+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, flags, IS_LONG, 0, "FILEINFO_NONE")
38+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, magic_database, IS_STRING, 0, "\"\"")
39+
ZEND_END_ARG_INFO()
3740

3841
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_finfo_file, 0, 0, 1)
3942
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)

ext/fileinfo/tests/bug61964-mb.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ rmdir($dir);
4545
?>
4646
--EXPECTF--
4747
bool(false)%A
48-
resource(%d) of type (file_info)
49-
resource(%d) of type (file_info)
48+
object(finfo)#%d (0) {
49+
}
50+
object(finfo)#%d (0) {
51+
}
5052
bool(false)%A
5153
Warning: finfo_open(): offset `string' invalid in %sbug61964-mb.php on line %d
5254

ext/fileinfo/tests/bug61964.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ rmdir($dir);
4545
?>
4646
--EXPECTF--
4747
bool(false)%A
48-
resource(%d) of type (file_info)
49-
resource(%d) of type (file_info)
48+
object(finfo)#%d (0) {
49+
}
50+
object(finfo)#%d (0) {
51+
}
5052
bool(false)%A
5153
Warning: finfo_open(): offset `string' invalid in %sbug61964.php on line %d
5254

ext/fileinfo/tests/finfo_close_basic.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ unset( $finfo );
2121
?>
2222
--EXPECTF--
2323
*** Testing finfo_close() : basic functionality ***
24-
resource(%d) of type (file_info)
24+
object(finfo)#%d (0) {
25+
}
2526
bool(true)
2627
object(finfo)#%d (%d) {
2728
}

ext/fileinfo/tests/finfo_close_error.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ try {
1919
*** Testing finfo_close() : error conditions ***
2020

2121
-- Testing finfo_close() function with wrong resource type --
22-
finfo_close(): supplied resource is not a valid file_info resource
22+
finfo_close(): Argument #1 ($finfo) must be of type finfo, resource given

ext/fileinfo/tests/finfo_open_001.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ var_dump(finfo_open(FILEINFO_MIME, '/foo/bar/inexistent'));
2020
?>
2121
--EXPECTF--
2222
finfo_open(): Argument #2 ($magic_database) must not contain any null bytes
23-
resource(%d) of type (file_info)
24-
resource(%d) of type (file_info)
23+
object(finfo)#%d (0) {
24+
}
25+
object(finfo)#%d (0) {
26+
}
2527

2628
Warning: finfo_open(%s123): Failed to open stream: No such file or directory in %s on line %d
2729

ext/fileinfo/tests/finfo_open_basic.phpt

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,21 @@ var_dump( new finfo() );
2525
?>
2626
--EXPECTF--
2727
*** Testing finfo_open() : basic functionality ***
28-
resource(%d) of type (file_info)
29-
resource(%d) of type (file_info)
30-
resource(%d) of type (file_info)
31-
resource(%d) of type (file_info)
32-
resource(%d) of type (file_info)
33-
resource(%d) of type (file_info)
34-
resource(%d) of type (file_info)
35-
object(finfo)#%d (%d) {
36-
}
37-
object(finfo)#%d (%d) {
28+
object(finfo)#%d (0) {
29+
}
30+
object(finfo)#%d (0) {
31+
}
32+
object(finfo)#%d (0) {
33+
}
34+
object(finfo)#%d (0) {
35+
}
36+
object(finfo)#%d (0) {
37+
}
38+
object(finfo)#%d (0) {
39+
}
40+
object(finfo)#%d (0) {
41+
}
42+
object(finfo)#%d (0) {
43+
}
44+
object(finfo)#%d (0) {
3845
}

ext/fileinfo/tests/finfo_open_error.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--TEST--
22
Test finfo_open() function : error functionality
33
--SKIPIF--
4-
<?php require_once(__DIR__ . '/skipif.inc');
4+
<?php require_once(__DIR__ . '/skipif.inc')?>
55
--FILE--
66
<?php
77
$magicFile = __DIR__ . DIRECTORY_SEPARATOR . 'magic';
@@ -35,6 +35,7 @@ Warning: finfo_open(): Failed to load magic database at "%sfoobarfile" in %sfinf
3535
bool(false)
3636

3737
Warning: finfo_open(): using regular magic file `%smagic' in %sfinfo_open_error.php on line %d
38-
resource(6) of type (file_info)
38+
object(finfo)#%d (0) {
39+
}
3940
finfo_open(): Argument #1 ($flags) must be of type int, string given
4041
finfo::__construct(): Argument #1 ($flags) must be of type int, string given

ext/fileinfo/tests/finfo_open_variation1.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ var_dump( finfo_open( FILEINFO_DEVICES | FILEINFO_RAW, $magicFile ) );
1616
?>
1717
--EXPECTF--
1818
*** Testing finfo_open() : variations in opening ***
19-
resource(%d) of type (file_info)
20-
resource(%d) of type (file_info)
19+
object(finfo)#%d (0) {
20+
}
21+
object(finfo)#%d (0) {
22+
}

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ static const func_info_t func_infos[] = {
776776
#endif
777777

778778
/* ext/fileinfo */
779-
F1("finfo_open", MAY_BE_FALSE | MAY_BE_RESOURCE),
779+
F1("finfo_open", MAY_BE_FALSE | MAY_BE_OBJECT),
780780
F1("finfo_file", MAY_BE_FALSE | MAY_BE_STRING),
781781
F1("finfo_buffer", MAY_BE_FALSE | MAY_BE_STRING),
782782
F1("mime_content_type", MAY_BE_FALSE | MAY_BE_STRING),

0 commit comments

Comments
 (0)