]> granicus.if.org Git - php/commitdiff
MFH: Fix path lookup when include_path is used
authorEtienne Kneuss <colder@php.net>
Tue, 20 May 2008 21:46:51 +0000 (21:46 +0000)
committerEtienne Kneuss <colder@php.net>
Tue, 20 May 2008 21:46:51 +0000 (21:46 +0000)
ext/spl/spl_directory.c
ext/spl/spl_directory.h
ext/spl/tests/fileobject_004.phpt [new file with mode: 0644]

index b50e577dde290f09caca6f4b8157e7a9b946fb15..c0ddb9f1d4296460ada13dd043e7a6b97110fc6b 100755 (executable)
@@ -109,6 +109,9 @@ static void spl_filesystem_object_free_storage(void *object TSRMLS_DC) /* {{{ */
                        if (intern->u.file.open_mode) {
                                efree(intern->u.file.open_mode);
                        }
+                       if (intern->orig_path) {
+                               efree(intern->orig_path);
+                       }
                }
                spl_filesystem_file_free_line(intern TSRMLS_CC);
                break;
@@ -257,6 +260,8 @@ static int spl_filesystem_file_open(spl_filesystem_object *intern, int use_inclu
                intern->file_name_len--;
        }
 
+       intern->orig_path = estrndup(intern->u.file.stream->orig_path, strlen(intern->u.file.stream->orig_path));
+
        intern->file_name = estrndup(intern->file_name, intern->file_name_len);
        intern->u.file.open_mode = estrndup(intern->u.file.open_mode, intern->u.file.open_mode_len);
 
@@ -985,14 +990,22 @@ SPL_METHOD(SplFileInfo, getRealPath)
 {
        spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
        char buff[MAXPATHLEN];
+       char *filename;
 
        php_set_error_handling(EH_THROW, spl_ce_RuntimeException TSRMLS_CC);
 
        if (intern->type == SPL_FS_DIR && !intern->file_name && intern->u.dir.entry.d_name[0]) {
                spl_filesystem_object_get_file_name(intern TSRMLS_CC);
        }
+       
+       if (intern->orig_path) {
+               filename = intern->orig_path;
+       } else { 
+               filename = intern->file_name;
+       }
+
 
-       if (intern->file_name && VCWD_REALPATH(intern->file_name, buff)) {
+       if (filename && VCWD_REALPATH(filename, buff)) {
 #ifdef ZTS
                if (VCWD_ACCESS(buff, F_OK)) {
                        RETVAL_FALSE;
@@ -1866,6 +1879,8 @@ SPL_METHOD(SplFileObject, __construct)
        spl_filesystem_object *intern = (spl_filesystem_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
        zend_bool use_include_path = 0;
        char *p1, *p2;
+       char *tmp_path;
+       int   tmp_path_len;
 
        php_set_error_handling(EH_THROW, spl_ce_RuntimeException TSRMLS_CC);
 
@@ -1881,18 +1896,29 @@ SPL_METHOD(SplFileObject, __construct)
        }
        
        if (spl_filesystem_file_open(intern, use_include_path, 0 TSRMLS_CC) == SUCCESS) {
-               p1 = strrchr(intern->file_name, '/');
+               tmp_path_len = strlen(intern->u.file.stream->orig_path);
+
+               if (tmp_path_len && IS_SLASH_AT(intern->u.file.stream->orig_path, tmp_path_len-1)) {
+                       tmp_path_len--;
+               }
+
+               tmp_path = estrndup(intern->u.file.stream->orig_path, tmp_path_len);
+
+               p1 = strrchr(tmp_path, '/');
 #if defined(PHP_WIN32) || defined(NETWARE)
-               p2 = strrchr(intern->file_name, '\\');
+               p2 = strrchr(tmp_path, '\\');
 #else
                p2 = 0;
 #endif
                if (p1 || p2) {
-                       intern->_path_len = (p1 > p2 ? p1 : p2) - intern->file_name;
+                       intern->_path_len = (p1 > p2 ? p1 : p2) - tmp_path;
                } else {
                        intern->_path_len = 0;
                }
-               intern->_path = estrndup(intern->file_name, intern->_path_len);
+
+               efree(tmp_path);
+
+               intern->_path = estrndup(intern->u.file.stream->orig_path, intern->_path_len);
        }
 
        php_set_error_handling(EH_NORMAL, NULL TSRMLS_CC);
index bffa0f36864999514e8fd2aff2bd507b498f4e6a..eecfbe1f683d16796e3e9dec306f376be56578b9 100755 (executable)
@@ -65,6 +65,7 @@ struct _spl_filesystem_object {
        spl_other_handler  *oth_handler;
        char               *_path;
        int                _path_len;
+       char               *orig_path;
        char               *file_name;
        int                file_name_len;
        SPL_FS_OBJ_TYPE    type;
diff --git a/ext/spl/tests/fileobject_004.phpt b/ext/spl/tests/fileobject_004.phpt
new file mode 100644 (file)
index 0000000..f71fc87
--- /dev/null
@@ -0,0 +1,24 @@
+--TEST--
+SPL: SplFileObject realpath and include_path
+--SKIPIF--
+<?php if (!extension_loaded("spl")) print "skip"; ?>
+--FILE--
+<?php
+
+set_include_path('tests');
+
+chdir(dirname(dirname(__FILE__))); // ext/spl
+
+
+$fo = new SplFileObject('fileobject_004.phpt', 'r', true);
+
+var_dump($fo->getPath());
+var_dump($fo->getFilename());
+var_dump($fo->getRealPath());
+?>
+==DONE==
+--EXPECTF--
+string(%d) "%sspl%stests"
+string(19) "fileobject_004.phpt"
+string(%d) "%sspl%stests%sfileobject_004.phpt"
+==DONE==