Skip to content

Commit

Permalink
Example case with dir functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias committed Mar 4, 2021
1 parent 37c5ff4 commit d3a8b91
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
13 changes: 7 additions & 6 deletions ext/standard/dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "php_string.h"
#include "php_scandir.h"
#include "basic_functions.h"
#include "io_exceptions.h"
#include "dir_arginfo.h"

#if HAVE_UNISTD_H
Expand Down Expand Up @@ -278,7 +279,7 @@ PHP_FUNCTION(chroot)

ret = chroot(str);
if (ret != 0) {
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
RETURN_FALSE;
}

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

if (ret != 0) {
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
RETURN_FALSE;
}

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

if (ret != 0) {
php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno);
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "%s (errno %d)", strerror(errno), errno);
RETURN_FALSE;
}

Expand Down Expand Up @@ -415,12 +416,12 @@ PHP_FUNCTION(glob)
ZEND_PARSE_PARAMETERS_END();

if (pattern_len >= MAXPATHLEN) {
php_error_docref(NULL, E_WARNING, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "Pattern exceeds the maximum allowed length of %d characters", MAXPATHLEN);
RETURN_FALSE;
}

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

Expand Down Expand Up @@ -558,7 +559,7 @@ PHP_FUNCTION(scandir)
n = php_stream_scandir(dirn, &namelist, context, (void *) php_stream_dirent_alphasortr);
}
if (n < 0) {
php_error_docref(NULL, E_WARNING, "(errno %d): %s", errno, strerror(errno));
php_exception_or_warning_docref(NULL, zend_ce_filesystem_error, "(errno %d): %s", errno, strerror(errno));
RETURN_FALSE;
}

Expand Down
38 changes: 38 additions & 0 deletions ext/standard/tests/dir/scandir_error2_throw_on_error_declare.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--TEST--
Test scandir() function error conditions with throw on error declare enabled - Non-existent directory
--SKIPIF--
<?php
if (substr(PHP_OS, 0, 3) == 'WIN') {
die('skip.. Not valid for Windows');
}
?>
--FILE--
<?php

declare(throw_on_error=1);

echo "*** Testing scandir() : error conditions ***\n";
$directory = __DIR__ . '/idonotexist';

echo "\n-- Pass scandir() an absolute path that does not exist --\n";
try {
var_dump(scandir($directory));
} catch (\FileSystemError $e) {
echo $e->getMessage() . \PHP_EOL;
}

echo "\n-- Pass scandir() a relative path that does not exist --\n";
try {
var_dump(scandir('/idonotexist'));
} catch (\FileSystem $e) {
echo $e->getMessage() . \PHP_EOL;
}
?>
--EXPECT--
*** Testing scandir() : error conditions ***

-- Pass scandir() an absolute path that does not exist --
(errno 2): No such file or directory

-- Pass scandir() a relative path that does not exist --
(errno 2): No such file or directory

0 comments on commit d3a8b91

Please sign in to comment.