From 73d474be62a1a7e6a85525d7eeed94fdd8d9fc4e Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Fri, 1 Dec 2006 16:42:48 +0000 Subject: [PATCH] MFB: fix #39458 --- ext/ftp/ftp.c | 7 ++++++- ext/ftp/tests/bug39458-2.phpt | 36 +++++++++++++++++++++++++++++++++++ ext/ftp/tests/bug39458.phpt | 35 ++++++++++++++++++++++++++++++++++ ext/ftp/tests/server.inc | 27 ++++++++++++++++++++++++++ 4 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 ext/ftp/tests/bug39458-2.phpt create mode 100644 ext/ftp/tests/bug39458.phpt diff --git a/ext/ftp/ftp.c b/ext/ftp/ftp.c index 29d1c84f1b..60a6b78da2 100644 --- a/ext/ftp/ftp.c +++ b/ext/ftp/ftp.c @@ -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 index 0000000000..4cd2a45236 --- /dev/null +++ b/ext/ftp/tests/bug39458-2.phpt @@ -0,0 +1,36 @@ +--TEST-- +Bug #39458: ftp_nlist() returns false on empty directories (other server behaviour) +--SKIPIF-- + +--FILE-- + +--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 index 0000000000..5ea345776c --- /dev/null +++ b/ext/ftp/tests/bug39458.phpt @@ -0,0 +1,35 @@ +--TEST-- +Bug #39458: ftp_nlist() returns false on empty directories +--SKIPIF-- + +--FILE-- + +--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/server.inc b/ext/ftp/tests/server.inc index 7480d5c588..e08eeb438a 100644 --- a/ext/ftp/tests/server.inc +++ b/ext/ftp/tests/server.inc @@ -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"); -- 2.40.0