From leres@ee.lbl.gov  Wed Feb 23 22:14:43 2011
Return-Path: <leres@ee.lbl.gov>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 5A7A01065673
	for <freebsd-gnats-submit@freebsd.org>; Wed, 23 Feb 2011 22:14:43 +0000 (UTC)
	(envelope-from leres@ee.lbl.gov)
Received: from fun.ee.lbl.gov (fun.ee.lbl.gov [IPv6:2001:400:610:102::ca])
	by mx1.freebsd.org (Postfix) with ESMTP id 40A968FC08
	for <freebsd-gnats-submit@freebsd.org>; Wed, 23 Feb 2011 22:14:43 +0000 (UTC)
Received: from ice.ee.lbl.gov (ice.ee.lbl.gov [131.243.2.213])
	(authenticated bits=0)
	by fun.ee.lbl.gov (8.14.4/8.14.4) with ESMTP id p1NMEgnK060454
	(version=TLSv1/SSLv3 cipher=DHE-RSA-CAMELLIA256-SHA bits=256 verify=NO)
	for <freebsd-gnats-submit@freebsd.org>; Wed, 23 Feb 2011 14:14:43 -0800 (PST)
Message-Id: <4D6586D2.7030602@ee.lbl.gov>
Date: Wed, 23 Feb 2011 14:14:42 -0800
From: Craig Leres <leres@ee.lbl.gov>
To: freebsd-gnats-submit@freebsd.org
Subject: [PATCH] lib/libfetch/ftp.c add LIST feature

>Number:         154988
>Category:       kern
>Synopsis:       [libfetch] [patch] lib/libfetch/ftp.c add LIST feature
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    des
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Wed Feb 23 22:20:05 UTC 2011
>Closed-Date:    
>Last-Modified:  Wed Feb 23 22:32:51 UTC 2011
>Originator:     Craig Leres
>Release:        FreeBSD 8.2-RELEASE amd64
>Organization:
Lawrence Berkeley National Laboratory
>Environment:
	FreeBSD hot.ee.lbl.gov 8.2-RELEASE FreeBSD 8.2-RELEASE #0 r8: Tue Feb
22 19:50:59 PST 2011
leres@hot.ee.lbl.gov:/usr/src/8.2-RELEASE/sys/amd64/compile/LBLSMPIPV6
amd64

>Description:
	Currently if you use fetch with a ftp directory, you get
	"File not found." It would be nice if instead this type of
	query returned a directory listing.

>How-To-Repeat:
	% fetch -vv ftp://ftp.freebsd.org/
	scheme:   [ftp]
	user:     []
	password: []
	host:     [ftp.freebsd.org]
	port:     [0]
	document: [/]
	---> ftp.freebsd.org:21
	looking up ftp.freebsd.org
	connecting to ftp.freebsd.org:21
	<<< 220 Welcome to freebsd.isc.org.
	>>> USER anonymous
	<<< 331 Please specify the password.
	>>> PASS leres@hot.ee.lbl.gov
	<<< 230 Login successful.
	>>> PWD
	<<< 257 "/"
	>>> MODE S
	<<< 200 Mode set to S.
	>>> TYPE I
	<<< 200 Switching to Binary mode.
	>>> SIZE
	<<< 550 Could not get file size.
	>>> MODE S
	<<< 200 Mode set to S.
	>>> TYPE I
	<<< 200 Switching to Binary mode.
	setting passive mode
	>>> EPSV
	<<< 229 Entering Extended Passive Mode (|||11219|).
	opening data connection
	initiating transfer
	>>> RETR
	<<< 550 Failed to open file.
	fetch: ftp://ftp.freebsd.org/: File unavailable (e.g., file not found,
no access)

>Fix:
 This is a multi-part message in MIME format.
 --------------050609040308060002090201
 Content-Type: text/plain; charset=ISO-8859-1
 Content-Transfer-Encoding: 7bit
 
 -----BEGIN PGP SIGNED MESSAGE-----
 Hash: SHA1

	The attached patch detects a trailing '/' and issues a ftp
	LIST for directories.

	New example output:

	% fetch -vv ftp://ftp.freebsd.org/
	scheme:   [ftp]
	user:     []
	password: []
	host:     [ftp.freebsd.org]
	port:     [0]
	document: [/]
	---> ftp.freebsd.org:21
	looking up ftp.freebsd.org
	connecting to ftp.freebsd.org:21
	<<< 220 Welcome to freebsd.isc.org.
	>>> USER anonymous
	<<< 331 Please specify the password.
	>>> PASS leres@hot.ee.lbl.gov
	<<< 230 Login successful.
	>>> PWD
	<<< 257 "/"
	>>> MODE S
	<<< 200 Mode set to S.
	>>> TYPE I
	<<< 200 Switching to Binary mode.
	setting passive mode
	>>> EPSV
	<<< 229 Entering Extended Passive Mode (|||38037|).
	opening data connection
	initiating transfer
	>>> LIST
	<<< 150 Here comes the directory listing.
	fetch: ftp://ftp.freebsd.org/: size of remote file is not known
	fetch.out                                               61  B  902 kBps
	Waiting for final status
	<<< 226 Directory send OK.

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1lhtIACgkQWxlAhAje3Ju/awCcDNIQSdUni9QPd2NGdDwdHmfp
Xi4An3GOLNEthjToVm9QumxpNrmvwyyP
=HwGD
-----END PGP SIGNATURE-----

--------------050609040308060002090201
Content-Type: text/plain;
 name="patch-ftp.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
 filename="patch-ftp.c"

--- ftp.c.orig	2011-02-23 14:07:53.000000000 -0800
+++ ftp.c	2011-02-23 14:08:15.000000000 -0800
@@ -777,7 +777,11 @@
 		/* make the server initiate the transfer */
 		if (verbose)
 			fetch_info("initiating transfer");
-		e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
+		if (*filename != '\0')
+			e = ftp_cmd(conn, "%s %.*s", oper,
+			    filenamelen, filename);
+		else
+			e = ftp_cmd(conn, "%s", oper);
 		if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
 			goto ouch;
 
@@ -868,7 +872,11 @@
 		/* make the server initiate the transfer */
 		if (verbose)
 			fetch_info("initiating transfer");
-		e = ftp_cmd(conn, "%s %.*s", oper, filenamelen, filename);
+		if (*filename != '\0')
+			e = ftp_cmd(conn, "%s %.*s", oper,
+			    filenamelen, filename);
+		else
+			e = ftp_cmd(conn, "%s", oper);
 		if (e != FTP_CONNECTION_ALREADY_OPEN && e != FTP_OPEN_DATA_CONNECTION)
 			goto ouch;
 
@@ -1100,6 +1108,7 @@
 {
 	conn_t *conn;
 	int oflag;
+	char *cp;
 
 	/* check if we should use HTTP instead */
 	if (purl && strcasecmp(purl->scheme, SCHEME_HTTP) == 0) {
@@ -1124,6 +1133,18 @@
 	if (ftp_cwd(conn, url->doc) == -1)
 		goto errsock;
 
+	cp = url->doc + strlen(url->doc) - 1;
+	if (cp >= url->doc && *cp == '/') {
+		/* list directory */
+		if (us) {   
+			us->size = -1;
+			us->atime = us->mtime = 0;
+		}
+		/* list the directory */
+		return ftp_transfer(conn, "LIST", url->doc, O_RDONLY,
+			url->offset, flags);
+	}
+
 	/* stat file */
 	if (us && ftp_stat(conn, url->doc, us) == -1
 	    && fetchLastErrCode != FETCH_PROTO


--------------050609040308060002090201
Content-Type: application/octet-stream;
 name="patch-ftp.c.sig"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="patch-ftp.c.sig"

iEYEABECAAYFAk1lhtIACgkQWxlAhAje3JuYRQCfSKGOCml2YpxRUt2iexrv9Xlvt2oAnjco
uj3cyQco/Q21D2w7JBMq6DGG
--------------050609040308060002090201--
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: linimon 
Responsible-Changed-When: Wed Feb 23 22:31:54 UTC 2011 
Responsible-Changed-Why:  
des, is this still your area of interest? 

http://www.freebsd.org/cgi/query-pr.cgi?pr=154988 
>Unformatted:
 
