#35791 [NEW]: file_exists() does not see removed symlinks

From: Date: Fri, 23 Dec 2005 21:16:09 +0000
Subject: #35791 [NEW]: file_exists() does not see removed symlinks
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-90951@lists.php.net to get a copy of this message
From:             jgmtfia at gmail dot com
Operating system: Linux
PHP version:      5.1.1
PHP Bug Type:     Filesystem function related
Bug description:  file_exists() does not see removed symlinks

Description:
------------
I have narrowed down a PHP problem.  I can demonstrate it with
file_exists().

I have 2 files, A and B, and one symlink C that points to A.
    -rw-r--r--  Dec 23 14:13 A
    -rw-r--r--  Dec 23 14:13 B
    lrwxr-xr-x  Dec 23 14:13 C -> A

When I delete link C from PHP, and then clearstatcache(), file_exists()
returns true for link C.  **It continues to do so until A is removed from
disk**.  Using the Apache2 module, I do multiple page reloads and only
after some time has passed does PHP notice that link C has been removed.

Not a big problem in general, however, if I do the following:
  unlink("C");
  symlink("B", "C");
  file_get_contents("C");
**PHP still reads the contents of A**.  Using the CLI version I have to
run the script again before the contents of B are read.  In Apache I need
to reload multiple times.

I can reproduce this on linux kernels 2.6.10 and 2.6.14.  On ext2 and ext3
filesystems.  The code below executes correctly (passes) with PHP 4.3.10
and fails on 5.1.0b3, 5.1.0 and 5.1.1.

Reproduce code:
---------------
<?php
touch A; ln -s A C;
$FILE = 'C';
$LS = "ls -log $FILE 2>&1";
clearstatcache();

if(!file_exists($FILE))
    echo "$FILE link does not exist, cannot run test.\n";

echo "File '$FILE' exists:\n\t\t".$LS."\n";

if(!unlink($FILE)){
    echo "Unable to delete '$FILE'\n\t\t".$LS."\n";
    exit;
}

clearstatcache();

echo "Deleted '$FILE'\n\t\t".$LS."\n";
if(!file_exists($FILE)){
    echo "(PASS) PHP sees the file as deleted.\n";
    exit;
}

echo "(FAIL) File '$FILE' deleted as seen by OS.  ";
echo "file_exists() still thinks it exists:\n\t\t".$LS."\n";

echo "Deleting 'A'\n";
unlink('A');
clearstatcache();

if(file_exists($FILE)){
    echo "(FAIL) PHP still sees '$FILE' as
existing:\n\t\t".$LS."\n";
    exit;
}

echo "(FAIL) PHP sees file as deleted only after link target deleted.\n";
?>


Expected result:
----------------
~/x$ ./php-4.3.10-15 ./test.php
File 'C' exists:
                lrwxr-xr-x  1 1 Dec 23 14:00 C -> A

Deleted 'C'
                ls: C: No such file or directory

(PASS) PHP sees the file as deleted.


Actual result:
--------------
~/x$ ./php-5.1.1 ./test.php
File 'C' exists:
                lrwxr-xr-x  1 1 Dec 23 14:02 C -> A

Deleted 'C'
                ls: C: No such file or directory

(FAIL) File 'C' deleted as seen by OS.  file_exists() still thinks it 
exists:
                ls: C: No such file or directory

Deleting 'A'
(FAIL) PHP sees file as deleted only after link target deleted.


-- 
Edit bug report at http://bugs.php.net/?id=35791&edit=1
-- 
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=35791&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=35791&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=35791&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=35791&r=fixedcvs
Fixed in release:             http://bugs.php.net/fix.php?id=35791&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=35791&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=35791&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=35791&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=35791&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=35791&r=notwrong
Not enough info:              http://bugs.php.net/fix.php?id=35791&r=notenoughinfo
Submitted twice:              http://bugs.php.net/fix.php?id=35791&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=35791&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=35791&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=35791&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=35791&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=35791&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=35791&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=35791&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=35791&r=mysqlcfg


Thread (22 messages)

« previous php.bugs (#90951) next »