]> granicus.if.org Git - php/commitdiff
MFB: fix #39458
authorNuno Lopes <nlopess@php.net>
Fri, 1 Dec 2006 16:42:48 +0000 (16:42 +0000)
committerNuno Lopes <nlopess@php.net>
Fri, 1 Dec 2006 16:42:48 +0000 (16:42 +0000)
ext/ftp/ftp.c
ext/ftp/tests/bug39458-2.phpt [new file with mode: 0644]
ext/ftp/tests/bug39458.phpt [new file with mode: 0644]
ext/ftp/tests/server.inc

index 29d1c84f1b59808123b38ec722f1cb2bfc3a09a9..60a6b78da2e0e2072263d954837531be03649709 100644 (file)
@@ -1614,10 +1614,15 @@ ftp_genlist(ftpbuf_t *ftp, const char *cmd, const char *path TSRMLS_DC)
        if (!ftp_putcmd(ftp, cmd, path)) {
                goto bail;
        }
-       if (!ftp_getresp(ftp) || (ftp->resp != 150 && ftp->resp != 125)) {
+       if (!ftp_getresp(ftp) || (ftp->resp != 150 && ftp->resp != 125 && ftp->resp != 226)) {
                goto bail;
        }
 
+       /* some servers don't open a ftp-data connection if the directory is empty */
+       if (ftp->resp == 226) {
+               return ecalloc(1, sizeof(char**));
+       }
+
        /* pull data buffer into tmpfile */
        if ((data = data_accept(data, ftp TSRMLS_CC)) == NULL) {
                goto bail;
diff --git a/ext/ftp/tests/bug39458-2.phpt b/ext/ftp/tests/bug39458-2.phpt
new file mode 100644 (file)
index 0000000..4cd2a45
--- /dev/null
@@ -0,0 +1,36 @@
+--TEST--
+Bug #39458: ftp_nlist() returns false on empty directories (other server behaviour)
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+$bug39458=1;
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+var_dump(ftp_nlist($ftp, ''));
+var_dump(ftp_nlist($ftp, 'emptydir'));
+var_dump(ftp_nlist($ftp, 'bogusdir'));
+
+ftp_close($ftp);
+?>
+--EXPECT--
+bool(true)
+array(3) {
+  [0]=>
+  string(5) "file1"
+  [1]=>
+  string(5) "file1"
+  [2]=>
+  string(9) "file
+b0rk"
+}
+array(0) {
+}
+bool(false)
diff --git a/ext/ftp/tests/bug39458.phpt b/ext/ftp/tests/bug39458.phpt
new file mode 100644 (file)
index 0000000..5ea3457
--- /dev/null
@@ -0,0 +1,35 @@
+--TEST--
+Bug #39458: ftp_nlist() returns false on empty directories
+--SKIPIF--
+<?php
+require 'skipif.inc';
+?>
+--FILE--
+<?php
+require 'server.inc';
+
+$ftp = ftp_connect('127.0.0.1', $port);
+if (!$ftp) die("Couldn't connect to the server");
+
+var_dump(ftp_login($ftp, 'user', 'pass'));
+
+var_dump(ftp_nlist($ftp, ''));
+var_dump(ftp_nlist($ftp, 'emptydir'));
+var_dump(ftp_nlist($ftp, 'bogusdir'));
+
+ftp_close($ftp);
+?>
+--EXPECT--
+bool(true)
+array(3) {
+  [0]=>
+  string(5) "file1"
+  [1]=>
+  string(5) "file1"
+  [2]=>
+  string(9) "file
+b0rk"
+}
+array(0) {
+}
+bool(false)
index 7480d5c588d7357ffaba3bbe62c19523deb58ce2..e08eeb438aa5dd3f2df5f40d7c4f24716181c2e9 100644 (file)
@@ -201,6 +201,33 @@ while($buf = fread($s, 4098)) {
                change_dir($m[1]);
                fputs($s, "250 CWD command successful.\r\n");
 
+       } elseif (preg_match("~^NLST(?: ([A-Za-z./]+))?\r\n$~", $buf, $m)) {
+
+               if (isset($m[1]) && $m[1] === 'bogusdir') {
+                       fputs($s, "250 $m[1]: No such file or directory\r\n");
+                       continue;
+               }
+
+               // there are some servers that don't open the ftp-data socket if there's nothing to send
+               if (isset($bug39458) && isset($m[1]) && $m[1] === 'emptydir') {
+                       fputs($s, "226 Transfer complete.\r\n");
+                       continue;
+               }
+
+               fputs($s, "150 File status okay; about to open data connection\r\n");
+
+               if (!$fs = stream_socket_client("tcp://$host:$port")) {
+                       fputs($s, "425 Can't open data connection\r\n");
+                       continue;
+               }
+
+               if (empty($m[1]) || $m[1] !== 'emptydir') {
+                       fputs($fs, "file1\r\nfile1\r\nfile\nb0rk\r\n");
+               }
+
+               fputs($s, "226 Closing data Connection.\r\n");
+               fclose($fs);
+
        } elseif (preg_match("~^MKD ([A-Za-z./]+)\r\n$~", $buf, $m)) {
                if (isset($bug7216)) {
                        fputs($s, "257 OK.\r\n");