1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2010, 2011, 2012, 2013 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix ftp-client)
20 #:use-module (srfi srfi-1)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-11)
23 #:use-module (srfi srfi-26)
24 #:use-module (srfi srfi-31)
25 #:use-module (rnrs io ports)
26 #:use-module (rnrs bytevectors)
27 #:use-module (ice-9 match)
28 #:use-module (ice-9 regex)
29 #:use-module (ice-9 rdelim)
30 #:export (ftp-connection?
31 ftp-connection-addrinfo
42 ;;; Simple FTP client (RFC 959).
46 ;; TODO: Use SRFI-3{4,5} error conditions.
48 (define-record-type <ftp-connection>
49 (%make-ftp-connection socket addrinfo)
51 (socket ftp-connection-socket)
52 (addrinfo ftp-connection-addrinfo))
55 (make-regexp "^([0-9]{3}) (.+)$"))
57 (define (%ftp-listen port)
58 (let loop ((line (read-line port)))
59 (cond ((eof-object? line) (values line #f))
60 ((regexp-exec %ftp-ready-rx line)
63 (values (string->number (match:substring match 1))
64 (match:substring match 2))))
66 (loop (read-line port))))))
68 (define (%ftp-command command expected-code port)
69 (format port "~A~A~A" command (string #\return) (string #\newline))
70 (let-values (((code message) (%ftp-listen port)))
71 (if (eqv? code expected-code)
73 (throw 'ftp-error port command code message))))
75 (define (%ftp-login user pass port)
76 (let ((command (string-append "USER " user (string #\newline))))
77 (display command port)
78 (let-values (((code message) (%ftp-listen port)))
81 ((331) (%ftp-command (string-append "PASS " pass) 230 port))
82 (else (throw 'ftp-error port command code message))))))
84 (define* (ftp-open host #:optional (port 21))
85 "Open an FTP connection to HOST on PORT (a service-identifying string,
86 or a TCP port number), and return it."
87 ;; Use 21 as the default PORT instead of "ftp", to avoid depending on
88 ;; libc's NSS, which is not available during bootstrap.
90 (catch 'getaddrinfo-error
94 (if (number? port) (number->string port) port)
95 (if (number? port) AI_NUMERICSERV 0)))
97 (let loop ((addresses addresses))
98 (let* ((ai (car addresses))
99 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
100 (addrinfo:protocol ai))))
104 (connect s (addrinfo:addr ai))
106 (let-values (((code message) (%ftp-listen s)))
109 ;;(%ftp-command "OPTS UTF8 ON" 200 s)
110 (%ftp-login "anonymous" "guix@example.com" s)
111 (%make-ftp-connection s ai))
113 (format (current-error-port)
114 "FTP to `~a' failed: ~A: ~A~%"
120 ;; Connection failed, so try one of the other addresses.
122 (if (null? addresses)
124 (loop (cdr addresses))))))))
125 (lambda (key errcode)
126 (format (current-error-port) "failed to resolve `~a': ~a~%"
127 host (gai-strerror errcode))
130 (define (ftp-close conn)
131 (close (ftp-connection-socket conn)))
133 (define (ftp-chdir conn dir)
134 (%ftp-command (string-append "CWD " dir) 250
135 (ftp-connection-socket conn)))
137 (define (ftp-size conn file)
138 "Return the size in bytes of FILE."
139 (let ((message (%ftp-command (string-append "SIZE " file) 213
140 (ftp-connection-socket conn))))
141 (string->number (string-trim-both message))))
143 (define (ftp-pasv conn)
145 (make-regexp "([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+),([0-9]+)"))
147 (let ((message (%ftp-command "PASV" 227 (ftp-connection-socket conn))))
148 (cond ((regexp-exec %pasv-rx message)
151 (+ (* (string->number (match:substring match 5)) 256)
152 (string->number (match:substring match 6)))))
154 (throw 'ftp-error conn "PASV" 227 message)))))
156 (define (address-with-port sa port)
157 "Return a socket-address object based on SA, but with PORT."
158 (let ((fam (sockaddr:fam sa))
159 (addr (sockaddr:addr sa)))
160 (cond ((= fam AF_INET)
161 (make-socket-address fam addr port))
163 (make-socket-address fam addr port
164 (sockaddr:flowinfo sa)
165 (sockaddr:scopeid sa)))
168 (define* (ftp-list conn #:optional directory)
170 (ftp-chdir conn directory))
172 (let* ((port (ftp-pasv conn))
173 (ai (ftp-connection-addrinfo conn))
174 (s (socket (addrinfo:fam ai) (addrinfo:socktype ai)
175 (addrinfo:protocol ai))))
176 (connect s (address-with-port (addrinfo:addr ai) port))
182 (%ftp-command "LIST" 150 (ftp-connection-socket conn))
184 (let loop ((line (read-line s))
186 (cond ((eof-object? line) (reverse result))
187 ((regexp-exec %ftp-ready-rx line)
190 (let ((code (string->number (match:substring match 1))))
193 (throw 'ftp-error conn "LIST" code)))))
196 (match (reverse (string-tokenize line))
197 ((file _ ... permissions)
198 (let ((type (case (string-ref permissions 0)
201 (cons (list file type) result)))
203 (cons (cons file 'file) result))))))))
206 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
208 (throw 'ftp-error conn "LIST" code message)))))))
210 (define* (ftp-retr conn file #:optional directory)
211 "Retrieve FILE from DIRECTORY (or, if omitted, the current directory) from
212 FTP connection CONN. Return a binary port to that file. The returned port
213 must be closed before CONN can be used for other purposes."
215 (ftp-chdir conn directory))
217 ;; Ask for "binary mode".
218 (%ftp-command "TYPE I" 200 (ftp-connection-socket conn))
220 (let* ((port (ftp-pasv conn))
221 (ai (ftp-connection-addrinfo conn))
222 (s (with-fluids ((%default-port-encoding #f))
223 (socket (addrinfo:fam ai) (addrinfo:socktype ai)
224 (addrinfo:protocol ai)))))
227 (let-values (((code message) (%ftp-listen (ftp-connection-socket conn))))
229 (throw 'ftp-error conn "LIST" code message))))
231 (connect s (address-with-port (addrinfo:addr ai) port))
234 (%ftp-command (string-append "RETR " file)
235 150 (ftp-connection-socket conn))
237 (make-custom-binary-input-port "FTP RETR port"
238 (rec (read! bv start count)
239 (match (get-bytevector-n! s bv
243 ;; Nothing available yet, so try
244 ;; again. This is important because
245 ;; the return value of `read!' makes
246 ;; it impossible to distinguish
247 ;; between "not yet" and "EOF".
248 (read! bv start count))
250 #f #f ; no get/set position
253 ;;; ftp-client.scm ends here