Bug #69473 [Opn]: bug in symlink() prevents relative symlinks on Windows
| From: | email at spikx dot net | Date: | Fri, 17 Apr 2015 07:08:18 +0000 |
| Subject: | Bug #69473 [Opn]: bug in symlink() prevents relative symlinks on Windows | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-192144@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69473&edit=1
ID: 69473
User updated by: email at spikx dot net
Reported by: email at spikx dot net
Summary: bug in symlink() prevents relative symlinks on
Windows
Status: Open
Type: Bug
Package: Win32API related
Operating System: Windows 7
PHP Version: 5.6.8
Block user comment: N
Private report: N
New Comment:
The issue with the CWD is even more problematic than expected. Assume you have a local XAMPP
installation on your Windows system and the aforementioned script is under
C:\xampp\htdocs\symlinktest\index.php
Now, if you execute this script via Apache (e.g. by opening it via your Browser), the CWD (for the
Win32API functions) will actually be
C:\xampp (!)
At least it was in my case. If you execute the script via the command line directly within
C:\xampp\htdocs\symlinktest, then the CWD for the Win32API functions will also be
C:\xampp\htdocs\symlinktest as expected.
Calling chdir(â¦) before the symlink(â¦) call did not change the CWD for the Win32API
function as well.
In my opinion, the
if ((attr = GetFileAttributes(topath)) == INVALID_FILE_ATTRIBUTES) { ... }
should simply be removed - or only applied, if
topath is not a relative path. It's
then the responsibility of the user to make sure the relative path provided in $target
is valid/meaningful. As it is currently, PHP's symlink function only allows you to generate
invalid relative symlinks anyway.
Previous Comments:
------------------------------------------------------------------------
[2015-04-16 20:29:44] email at spikx dot net
Description:
------------
As far as I checked, the following applies to any PHP version since the symlink function was
introduced (in PHP 5.3).
Currently it is impossible under Windows to create relative symbolic links with PHP's symlink
function. It will always fail with the message "Could not fetch file information". The
reason is the following:
At line #169 ( https://github.com/php/php-src/blob/master/ext/standard/link_win32.c#L169
) the function checks, if the $target ("topath" in the C function) actually exists:
if ((attr = GetFileAttributes(topath)) == INVALID_FILE_ATTRIBUTES) { ... }
However, the Win32API function "GetFileAttributes" expects the path to be either absolute
or relative **to the CWD**! This means that this check will always fail, if you want to use a
relative symlink, since the relative path of the symlink is relative to the $source's
directory, not relative to the CWD.
Test script:
---------------
<?php
mkdir('foo1');
mkdir('foo2');
symlink( '../foo2', 'foo1/foo3' );
Expected result:
----------------
/foo1 should contain a directory symlink to ../foo2 (i.e. the /foo2 directory in the root folder)
Actual result:
--------------
Under Windows:
Warning: symlink(): Could not fetch file information(error 2) in index.php on line 6
Under Linux:
foo1 contains a directory symlink to ../foo2 (i.e. the foo2 directory in CWD)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69473&edit=1