Skip to content

Commit 3bfac30

Browse files
committed
Example case with dir functions
1 parent a4a806e commit 3bfac30

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

ext/standard/dir.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "php_string.h"
2424
#include "php_scandir.h"
2525
#include "basic_functions.h"
26+
#include "io_exceptions.h"
2627
#include "dir_arginfo.h"
2728

2829
#if HAVE_UNISTD_H
@@ -278,7 +279,7 @@ PHP_FUNCTION(chroot)
278279

279280
ret = chroot(str);
280281
if (ret != 0) {
281-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
282+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
282283
RETURN_FALSE;
283284
}
284285

@@ -287,7 +288,7 @@ PHP_FUNCTION(chroot)
287288
ret = chdir("/");
288289

289290
if (ret != 0) {
290-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
291+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
291292
RETURN_FALSE;
292293
}
293294

@@ -313,7 +314,7 @@ PHP_FUNCTION(chdir)
313314
ret = VCWD_CHDIR(str);
314315

315316
if (ret != 0) {
316-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
317+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
317318
RETURN_FALSE;
318319
}
319320

@@ -415,12 +416,12 @@ PHP_FUNCTION(glob)
415416
ZEND_PARSE_PARAMETERS_END();
416417

417418
if (pattern_len >= MAXPATHLEN) {
418-
php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
419+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
419420
RETURN_FALSE;
420421
}
421422

422423
if ((GLOB_AVAILABLE_FLAGS & flags) != flags) {
423-
php_error_docref(NULL, E_WARNING, "At least one of the passed flags is invalid or not supported on this platform");
424+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "At least one of the passed flags is invalid or not supported on this platform");
424425
RETURN_FALSE;
425426
}
426427

@@ -558,7 +559,7 @@ PHP_FUNCTION(scandir)
558559
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
559560
}
560561
if (n < 0) {
561-
php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
562+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "(errno %d): %s", errno, strerror(errno));
562563
RETURN_FALSE;
563564
}
564565

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
Test scandir() function error conditions with throw on error declare enabled - Non-existent directory
3+
--SKIPIF--
4+
<?php
5+
if (substr(PHP_OS, 0, 3) == 'WIN') {
6+
die('skip.. Not valid for Windows');
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
12+
declare(throw_on_error=1);
13+
14+
echo "*** Testing scandir() : error conditions ***\n";
15+
$directory = __DIR__ . '/idonotexist';
16+
17+
echo "\n-- Pass scandir() an absolute path that does not exist --\n";
18+
try {
19+
var_dump(scandir($directory));
20+
} catch (\FileSystemError $e) {
21+
echo $e->getMessage() . \PHP_EOL;
22+
}
23+
24+
echo "\n-- Pass scandir() a relative path that does not exist --\n";
25+
try {
26+
var_dump(scandir('/idonotexist'));
27+
} catch (\FileSystem $e) {
28+
echo $e->getMessage() . \PHP_EOL;
29+
}
30+
?>
31+
--EXPECT--
32+
*** Testing scandir() : error conditions ***
33+
34+
-- Pass scandir() an absolute path that does not exist --
35+
(errno 2): No such file or directory
36+
37+
-- Pass scandir() a relative path that does not exist --
38+
(errno 2): No such file or directory

0 commit comments

Comments
 (0)