Skip to content

Commit 5e7eeaf

Browse files
committed
Example case with dir functions
1 parent bbf56a5 commit 5e7eeaf

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
@@ -280,7 +281,7 @@ PHP_FUNCTION(chroot)
280281

281282
ret = chroot(str);
282283
if (ret != 0) {
283-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
284+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
284285
RETURN_FALSE;
285286
}
286287

@@ -289,7 +290,7 @@ PHP_FUNCTION(chroot)
289290
ret = chdir("/");
290291

291292
if (ret != 0) {
292-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
293+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
293294
RETURN_FALSE;
294295
}
295296

@@ -315,7 +316,7 @@ PHP_FUNCTION(chdir)
315316
ret = VCWD_CHDIR(str);
316317

317318
if (ret != 0) {
318-
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
319+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
319320
RETURN_FALSE;
320321
}
321322

@@ -417,12 +418,12 @@ PHP_FUNCTION(glob)
417418
ZEND_PARSE_PARAMETERS_END();
418419

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

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

@@ -560,7 +561,7 @@ PHP_FUNCTION(scandir)
560561
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
561562
}
562563
if (n < 0) {
563-
php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
564+
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "(errno %d): %s", errno, strerror(errno));
564565
RETURN_FALSE;
565566
}
566567

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)