Return a file size string when possible.

This takes the advice from
  http://files.stairways.com/other/ftp-list-specs-info.txt
further: search for the date by an explicit occurrence of a known month
name.  This means that we won't see files with bad names (they'd be
filtered out of the result), but the filtered out entries are ones that
would not be usable with `ftp-make-file-seconds'.

When the month is found, and the entry is a file, look for a number
preceding the month, and if found, return it as the file size string.
This is a minor change in the API.  (But it's probably better to either
revise it further, or eventually make it irrelevant by exposing the
interesting functionality via `net/url'.)
This commit is contained in:
Eli Barzilay 2011-08-05 01:08:21 -04:00
parent 4daaa84636
commit 6a1336e75e
2 changed files with 20 additions and 9 deletions

View File

@ -159,7 +159,9 @@
#"250" void (void))) #"250" void (void)))
(define re:dir-line (define re:dir-line
#rx"^(.)......... .* ([A-Z].* .* [0-9][0-9]:?[0-9][0-9]) (.*)$") (regexp (string-append
"^(.)(.*) ((?i:jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)"
" .* [0-9][0-9]:?[0-9][0-9]) (.*)$")))
(define (ftp-directory-list tcp-ports) (define (ftp-directory-list tcp-ports)
(define tcp-data (establish-data-connection tcp-ports)) (define tcp-data (establish-data-connection tcp-ports))
@ -173,9 +175,15 @@
(ftp-connection-out tcp-ports) (ftp-connection-out tcp-ports)
#"226" print-msg (void)) #"226" print-msg (void))
(for*/list ([l (in-list lines)] (for*/list ([l (in-list lines)]
[m (in-value (regexp-match re:dir-line l))] [m (in-value (cond [(regexp-match re:dir-line l) => cdr]
[else #f]))]
#:when m) #:when m)
(cdr m))) (define size (cond [(and (equal? "-" (car m))
(regexp-match #rx"([0-9]+) *$" (cadr m)))
=> cadr]
[else #f]))
(define r `(,(car m) ,@(cddr m)))
(if size `(,@r ,size) r)))
(define (ftp-download-file tcp-ports folder filename) (define (ftp-download-file tcp-ports folder filename)
;; Save the file under the name tmp.file, rename it once download is ;; Save the file under the name tmp.file, rename it once download is

View File

@ -46,13 +46,16 @@ Returns a list of files and directories in the current directory of
the server, assuming that the server provides directory information in the server, assuming that the server provides directory information in
the quasi-standard Unix format. the quasi-standard Unix format.
Each file or directory is represented by a list of three strings. The Each file or directory is represented by a list of three or four
first string is either @racket["-"], @racket["d"], or @racket["l"], strings. The first string is either @racket["-"], @racket["d"], or
depending on whether the items is a file, directory, or link, @racket["l"], depending on whether the items is a file, directory, or
respectively. The second item is the file's date; to convert this link, respectively. The second item is the file's date; to convert this
value to seconds consistent with @racket[file-seconds], pass the date value to seconds consistent with @racket[file-seconds], pass the date
string to @racket[ftp-make-file-seconds], below. The third string is string to @racket[ftp-make-file-seconds]. The third string is the name
the name of the file or directory. of the file or directory. If the item is a file (the first string is
@racket["-"]), and if the line that the server replied with has a size
in the expected place, then a fourth string containing this size is
included.
Warning: the FTP protocol has no specification for the reply format, so Warning: the FTP protocol has no specification for the reply format, so
this information can be unreliable.} this information can be unreliable.}