From ad79d55a5ca54a87a070d00ac07567ef3bfdb114 Mon Sep 17 00:00:00 2001 From: yuyi Date: Fri, 8 Jan 2021 03:21:47 +0800 Subject: [PATCH] ftp: fix an error (#7930) --- vlib/io/buffered_reader.v | 6 +----- vlib/net/ftp/ftp.v | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/vlib/io/buffered_reader.v b/vlib/io/buffered_reader.v index 6cb0f7238e..dc4c9b4d72 100644 --- a/vlib/io/buffered_reader.v +++ b/vlib/io/buffered_reader.v @@ -32,9 +32,6 @@ pub fn new_buffered_reader(o BufferedReaderConfig) &BufferedReader { // read fufills the Reader interface pub fn (mut r BufferedReader) read(mut buf []byte) ?int { - if r.end_of_stream { - return none - } // read data out of the buffer if we dont have any if r.needs_fill() { if !r.fill_buffer() { @@ -53,7 +50,7 @@ fn (mut r BufferedReader) fill_buffer() bool { if r.end_of_stream { // we know we have already reached the end of stream // so return early - return false + return true } r.offset = 0 r.len = 0 @@ -117,4 +114,3 @@ pub fn (mut r BufferedReader) read_line() ?string { r.offset = i } } - diff --git a/vlib/net/ftp/ftp.v b/vlib/net/ftp/ftp.v index 4e0950f019..17346ce7a3 100644 --- a/vlib/net/ftp/ftp.v +++ b/vlib/net/ftp/ftp.v @@ -43,8 +43,15 @@ mut: } fn (mut dtp DTP) read() ?[]byte { - mut data := []byte{len: 1024} - dtp.reader.read(mut data) ? + mut data := []byte{} + mut buf := []byte{len: 1024} + for { + len := dtp.reader.read(mut buf) or { break } + if len == 0 { + break + } + data << buf[..len] + } return data } @@ -163,22 +170,22 @@ pub fn (mut ftp FTP) cd(dir string) ? { } } -fn new_dtp(msg string) ?DTP { +fn new_dtp(msg string) ?&DTP { if !is_dtp_message_valid(msg) { return error('Bad message') } ip, port := get_host_ip_from_dtp_message(msg) - conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') } - dtp := DTP{ - conn: conn - reader: io.new_buffered_reader(reader: io.make_reader(conn)) + mut dtp := &DTP{ ip: ip port: port } + conn := net.dial_tcp('$ip:$port') or { return error('Cannot connect to the data channel') } + dtp.conn = conn + dtp.reader = io.new_buffered_reader(reader: io.make_reader(dtp.conn)) return dtp } -fn (mut ftp FTP) pasv() ?DTP { +fn (mut ftp FTP) pasv() ?&DTP { ftp.write('PASV') ? code, data := ftp.read() ? $if debug {