Skip to content

Commit 76791e9

Browse files
authored
Use win32 glob implementation on all platforms (#18164)
* Move glob to main/ from win32/ In preparation to make the Win32 reimplementation the standard cross-platform one. Currently, it doesn't do that and just passes through the original glob implementation. We could consider also having an option to use the standard glob for systems that have a sufficient one. * Enable building with win32 glob on non-windows Kind of broken. We're namespacing the function and struct, but not yet the GLOB_* defines. There are a lot of places callers check if i.e. NOMATCH is defined that would likely become redundant. Currently it also has php_glob and #defines glob php_glob (etc.) - I suspect doing the opposite and changing the callers would make more sense, just doing MVP to geet it to build (even if it fails tests). * Massive first pass at conversion to internal glob Have not tested yet. the big things are: - Should be invisible to userland PHP code. - A lot of :%s/GLOB_/PHP_GLOB_/g; the diff can be noisy as a result, especially in comments. - Prefixes everything with PHP_ to avoid conflicts with system glob in case it gets included transitively. - A lot of weird shared definitions that were sprawled out to other headers are now included in php_glob.h. - A lot of (but not yet all cases) of HAVE_GLOB are removed, since we can always fall back to php_glob. - Using the system glob is not wired up yet; it'll need more shim ifdefs for each flag type than just glob_t/glob/globfree defs. * Fix inclusion of GLOB_ONLYDIR This is a GNU extension, but we don't need to implement it, as the GNU implementation is flawed enough that callers have to manually filter it anyways; just provide a stub definition for the constant. We could consideer implementing this properly later. For now, fixes the basic glob constant tests. * Remove HAVE_GLOBs We now always have a glob implementation that works. HAVE_GLOB should only be used to check if we have a system implementation, for if we decide to wrap the system implementation instead. * We don't need to care about being POSIXly correct for internal glob * Check for reallocarray Ideally temporary until GH-17433. * Forgot to move this file from win32/ to main/ * Check for issetugid (BSD function) * Allow using the system glob with --enable-system-glob * Style fix after removing ifdef * Remove empty case for system glob
1 parent 4eef2bc commit 76791e9

24 files changed

+395
-463
lines changed

Zend/Optimizer/zend_func_infos.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,9 +518,7 @@ static const func_info_t func_infos[] = {
518518
F1("getcwd", MAY_BE_STRING|MAY_BE_FALSE),
519519
F1("readdir", MAY_BE_STRING|MAY_BE_FALSE),
520520
F1("scandir", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
521-
#if defined(HAVE_GLOB)
522521
F1("glob", MAY_BE_ARRAY|MAY_BE_ARRAY_KEY_LONG|MAY_BE_ARRAY_OF_STRING|MAY_BE_FALSE),
523-
#endif
524522
F1("exec", MAY_BE_STRING|MAY_BE_FALSE),
525523
F1("system", MAY_BE_STRING|MAY_BE_FALSE),
526524
F1("escapeshellcmd", MAY_BE_STRING),

configure.ac

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,7 @@ AC_CHECK_FUNCS(m4_normalize([
557557
getwd
558558
glob
559559
gmtime_r
560+
issetugid
560561
lchown
561562
localtime_r
562563
memcntl
@@ -571,6 +572,7 @@ AC_CHECK_FUNCS(m4_normalize([
571572
poll
572573
pthread_jit_write_protect_np
573574
putenv
575+
reallocarray
574576
scandir
575577
setenv
576578
setitimer
@@ -591,6 +593,17 @@ AC_CHECK_FUNCS(m4_normalize([
591593
vasprintf
592594
]))
593595

596+
PHP_ARG_ENABLE([system-glob],
597+
[whether to use the system glob function],
598+
[AS_HELP_STRING([--enable-system-glob],
599+
[Use the system glob function instead of the PHP provided replacement.])],
600+
[no],
601+
[no])
602+
603+
AS_VAR_IF([PHP_SYSTEM_GLOB], [yes],
604+
[AC_DEFINE([PHP_SYSTEM_GLOB], [1],
605+
[Define to 1 if PHP will use the system glob function instead of php_glob.])])
606+
594607
AC_CHECK_FUNC([inet_ntop],, [AC_MSG_FAILURE([Required inet_ntop not found.])])
595608
AC_CHECK_FUNC([inet_pton],, [AC_MSG_FAILURE([Required inet_pton not found.])])
596609

@@ -1631,6 +1644,7 @@ PHP_ADD_SOURCES([main], m4_normalize([
16311644
php_content_types.c
16321645
php_ini_builder.c
16331646
php_ini.c
1647+
php_glob.c
16341648
php_odbc_utils.c
16351649
php_open_temporary_file.c
16361650
php_scandir.c

ext/ffi/ffi.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@
4545
#endif
4646
#endif
4747

48-
#ifdef HAVE_GLOB
49-
#ifdef PHP_WIN32
50-
#include "win32/glob.h"
51-
#else
52-
#include <glob.h>
53-
#endif
54-
#endif
48+
#include "php_glob.h"
5549

5650
#ifndef __BIGGEST_ALIGNMENT__
5751
/* XXX need something better, perhaps with regard to SIMD, etc. */
@@ -5360,16 +5354,15 @@ ZEND_INI_END()
53605354

53615355
static zend_result zend_ffi_preload_glob(const char *filename) /* {{{ */
53625356
{
5363-
#ifdef HAVE_GLOB
5364-
glob_t globbuf;
5357+
php_glob_t globbuf;
53655358
int ret;
53665359
unsigned int i;
53675360

5368-
memset(&globbuf, 0, sizeof(glob_t));
5361+
memset(&globbuf, 0, sizeof(globbuf));
53695362

5370-
ret = glob(filename, 0, NULL, &globbuf);
5371-
#ifdef GLOB_NOMATCH
5372-
if (ret == GLOB_NOMATCH || !globbuf.gl_pathc) {
5363+
ret = php_glob(filename, 0, NULL, &globbuf);
5364+
#ifdef PHP_GLOB_NOMATCH
5365+
if (ret == PHP_GLOB_NOMATCH || !globbuf.gl_pathc) {
53735366
#else
53745367
if (!globbuf.gl_pathc) {
53755368
#endif
@@ -5378,20 +5371,13 @@ static zend_result zend_ffi_preload_glob(const char *filename) /* {{{ */
53785371
for(i=0 ; i<globbuf.gl_pathc; i++) {
53795372
zend_ffi *ffi = zend_ffi_load(globbuf.gl_pathv[i], 1);
53805373
if (!ffi) {
5381-
globfree(&globbuf);
5374+
php_globfree(&globbuf);
53825375
return FAILURE;
53835376
}
53845377
efree(ffi);
53855378
}
5386-
globfree(&globbuf);
5379+
php_globfree(&globbuf);
53875380
}
5388-
#else
5389-
zend_ffi *ffi = zend_ffi_load(filename, 1);
5390-
if (!ffi) {
5391-
return FAILURE;
5392-
}
5393-
efree(ffi);
5394-
#endif
53955381

53965382
return SUCCESS;
53975383
}

ext/opcache/zend_accelerator_blacklist.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@
3030
# define REGEX_MODE (REG_EXTENDED|REG_NOSUB)
3131
#endif
3232

33-
#ifdef HAVE_GLOB
34-
#ifdef PHP_WIN32
35-
#include "win32/glob.h"
36-
#else
37-
#include <glob.h>
38-
#endif
39-
#endif
33+
#include "php_glob.h"
4034

4135
#include "ext/pcre/php_pcre.h"
4236

@@ -320,16 +314,15 @@ static void zend_accel_blacklist_loadone(zend_blacklist *blacklist, char *filena
320314

321315
void zend_accel_blacklist_load(zend_blacklist *blacklist, char *filename)
322316
{
323-
#ifdef HAVE_GLOB
324-
glob_t globbuf;
317+
php_glob_t globbuf;
325318
int ret;
326319
unsigned int i;
327320

328-
memset(&globbuf, 0, sizeof(glob_t));
321+
memset(&globbuf, 0, sizeof(globbuf));
329322

330-
ret = glob(filename, 0, NULL, &globbuf);
331-
#ifdef GLOB_NOMATCH
332-
if (ret == GLOB_NOMATCH || !globbuf.gl_pathc) {
323+
ret = php_glob(filename, 0, NULL, &globbuf);
324+
#ifdef PHP_GLOB_NOMATCH
325+
if (ret == PHP_GLOB_NOMATCH || !globbuf.gl_pathc) {
333326
#else
334327
if (!globbuf.gl_pathc) {
335328
#endif
@@ -338,11 +331,8 @@ void zend_accel_blacklist_load(zend_blacklist *blacklist, char *filename)
338331
for(i=0 ; i<globbuf.gl_pathc; i++) {
339332
zend_accel_blacklist_loadone(blacklist, globbuf.gl_pathv[i]);
340333
}
341-
globfree(&globbuf);
334+
php_globfree(&globbuf);
342335
}
343-
#else
344-
zend_accel_blacklist_loadone(blacklist, filename);
345-
#endif
346336
zend_accel_blacklist_update_regexp(blacklist);
347337
}
348338

ext/spl/spl_directory.c

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ static inline bool spl_intern_is_glob(const spl_filesystem_object *intern)
211211

212212
PHPAPI zend_string *spl_filesystem_object_get_path(const spl_filesystem_object *intern) /* {{{ */
213213
{
214-
#ifdef HAVE_GLOB
215214
if (intern->type == SPL_FS_DIR && spl_intern_is_glob(intern)) {
216215
size_t len = 0;
217216
char *tmp = php_glob_stream_get_path(intern->u.dir.dirp, &len);
@@ -220,7 +219,6 @@ PHPAPI zend_string *spl_filesystem_object_get_path(const spl_filesystem_object *
220219
}
221220
return zend_string_init(tmp, len, /* persistent */ false);
222221
}
223-
#endif
224222
if (!intern->path) {
225223
return NULL;
226224
}
@@ -641,14 +639,12 @@ static inline HashTable *spl_filesystem_object_get_debug_info(zend_object *objec
641639
spl_set_private_debug_info_property(spl_ce_SplFileInfo, "fileName", strlen("fileName"), debug_info, &tmp);
642640
}
643641
if (intern->type == SPL_FS_DIR) {
644-
#ifdef HAVE_GLOB
645642
if (spl_intern_is_glob(intern)) {
646643
ZVAL_STR_COPY(&tmp, intern->path);
647644
} else {
648645
ZVAL_FALSE(&tmp);
649646
}
650647
spl_set_private_debug_info_property(spl_ce_DirectoryIterator, "glob", strlen("glob"), debug_info, &tmp);
651-
#endif
652648
if (intern->u.dir.sub_path) {
653649
ZVAL_STR_COPY(&tmp, intern->u.dir.sub_path);
654650
} else {
@@ -721,16 +717,12 @@ static void spl_filesystem_object_construct(INTERNAL_FUNCTION_PARAMETERS, zend_l
721717

722718
/* spl_filesystem_dir_open() may emit an E_WARNING */
723719
zend_replace_error_handling(EH_THROW, spl_ce_UnexpectedValueException, &error_handling);
724-
#ifdef HAVE_GLOB
725720
if (SPL_HAS_FLAG(ctor_flags, DIT_CTOR_GLOB) && !zend_string_starts_with_literal(path, "glob://")) {
726721
path = zend_strpprintf(0, "glob://%s", ZSTR_VAL(path));
727722
spl_filesystem_dir_open(intern, path);
728723
zend_string_release(path);
729-
} else
730-
#endif
731-
{
724+
} else {
732725
spl_filesystem_dir_open(intern, path);
733-
734726
}
735727
zend_restore_error_handling(&error_handling);
736728
}
@@ -1582,7 +1574,6 @@ PHP_METHOD(RecursiveDirectoryIterator, __construct)
15821574
}
15831575
/* }}} */
15841576

1585-
#ifdef HAVE_GLOB
15861577
/* {{{ Cronstructs a new dir iterator from a glob expression (no glob:// needed). */
15871578
PHP_METHOD(GlobIterator, __construct)
15881579
{
@@ -1607,7 +1598,6 @@ PHP_METHOD(GlobIterator, count)
16071598
}
16081599
}
16091600
/* }}} */
1610-
#endif /* HAVE_GLOB */
16111601

16121602
/* {{{ forward declarations to the iterator handlers */
16131603
static void spl_filesystem_dir_it_dtor(zend_object_iterator *iter);
@@ -2782,11 +2772,9 @@ PHP_MINIT_FUNCTION(spl_directory)
27822772
spl_filesystem_object_check_handlers.clone_obj = NULL;
27832773
spl_filesystem_object_check_handlers.get_method = spl_filesystem_object_get_method_check;
27842774

2785-
#ifdef HAVE_GLOB
27862775
spl_ce_GlobIterator = register_class_GlobIterator(spl_ce_FilesystemIterator, zend_ce_countable);
27872776
spl_ce_GlobIterator->create_object = spl_filesystem_object_new;
27882777
spl_ce_GlobIterator->default_object_handlers = &spl_filesystem_object_check_handlers;
2789-
#endif
27902778

27912779
spl_ce_SplFileObject = register_class_SplFileObject(spl_ce_SplFileInfo, spl_ce_RecursiveIterator, spl_ce_SeekableIterator);
27922780
spl_ce_SplFileObject->default_object_handlers = &spl_filesystem_object_check_handlers;

ext/spl/spl_directory.stub.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,15 +207,13 @@ public function getSubPath(): string {}
207207
public function getSubPathname(): string {}
208208
}
209209

210-
#ifdef HAVE_GLOB
211210
class GlobIterator extends FilesystemIterator implements Countable
212211
{
213212
public function __construct(string $pattern, int $flags = FilesystemIterator::KEY_AS_PATHNAME | FilesystemIterator::CURRENT_AS_FILEINFO) {}
214213

215214
/** @tentative-return-type */
216215
public function count(): int {}
217216
}
218-
#endif
219217

220218
class SplFileObject extends SplFileInfo implements RecursiveIterator, SeekableIterator
221219
{

ext/spl/spl_directory_arginfo.h

Lines changed: 2 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/standard/basic_functions.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,7 @@ PHP_MINIT_FUNCTION(basic) /* {{{ */
339339

340340
php_register_url_stream_wrapper("php", &php_stream_php_wrapper);
341341
php_register_url_stream_wrapper("file", &php_plain_files_wrapper);
342-
#ifdef HAVE_GLOB
343342
php_register_url_stream_wrapper("glob", &php_glob_stream_wrapper);
344-
#endif
345343
php_register_url_stream_wrapper("data", &php_stream_rfc2397_wrapper);
346344
php_register_url_stream_wrapper("http", &php_stream_http_wrapper);
347345
php_register_url_stream_wrapper("ftp", &php_stream_ftp_wrapper);

ext/standard/basic_functions.stub.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,13 +2697,11 @@ function readdir($dir_handle = null): string|false {}
26972697
*/
26982698
function scandir(string $directory, int $sorting_order = SCANDIR_SORT_ASCENDING, $context = null): array|false {}
26992699

2700-
#ifdef HAVE_GLOB
27012700
/**
27022701
* @return array<int, string>|false
27032702
* @refcount 1
27042703
*/
27052704
function glob(string $pattern, int $flags = 0): array|false {}
2706-
#endif
27072705

27082706
/* exec.c */
27092707

ext/standard/basic_functions_arginfo.h

Lines changed: 1 addition & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)