Skip to content

Commit 675ec53

Browse files
committed
Example case with dir functions
1 parent c2c441f commit 675ec53

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

ext/standard/dir.c

Lines changed: 7 additions & 6 deletions
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
@@ -282,7 +283,7 @@ PHP_FUNCTION(chroot)
282283

283284
ret = chroot(str);
284285
if (ret != 0) {
285-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
286+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
286287
RETURN_FALSE;
287288
}
288289

@@ -291,7 +292,7 @@ PHP_FUNCTION(chroot)
291292
ret = chdir("/");
292293

293294
if (ret != 0) {
294-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
295+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
295296
RETURN_FALSE;
296297
}
297298

@@ -317,7 +318,7 @@ PHP_FUNCTION(chdir)
317318
ret = VCWD_CHDIR(str);
318319

319320
if (ret != 0) {
320-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
321+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
321322
RETURN_FALSE;
322323
}
323324

@@ -420,12 +421,12 @@ PHP_FUNCTION(glob)
420421
ZEND_PARSE_PARAMETERS_END();
421422

422423
if (pattern_len >= MAXPATHLEN) {
423-
php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
424+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
424425
RETURN_FALSE;
425426
}
426427

427428
if ((GLOB_AVAILABLE_FLAGS & flags) != flags) {
428-
php_error_docref(NULL, E_WARNING, "At least one of the passed flags is invalid or not supported on this platform");
429+
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");
429430
RETURN_FALSE;
430431
}
431432

@@ -564,7 +565,7 @@ PHP_FUNCTION(scandir)
564565
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
565566
}
566567
if (n < 0) {
567-
php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
568+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "(errno %d): %s", errno, strerror(errno));
568569
RETURN_FALSE;
569570
}
570571

Lines changed: 38 additions & 0 deletions
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)