ftp: document all public methods (#14408)

master
Larpon 2022-05-16 07:52:12 +02:00 committed by GitHub
parent 02e026e298
commit 5068b8b293
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 0 deletions

View File

@ -66,6 +66,7 @@ mut:
buffer_size int buffer_size int
} }
// new returns an `FTP` instance.
pub fn new() FTP { pub fn new() FTP {
mut f := FTP{ mut f := FTP{
conn: 0 conn: 0
@ -101,6 +102,7 @@ fn (mut zftp FTP) read() ?(int, string) {
return code, data return code, data
} }
// connect establishes an FTP connection to the host at `ip` port 21.
pub fn (mut zftp FTP) connect(ip string) ?bool { pub fn (mut zftp FTP) connect(ip string) ?bool {
zftp.conn = net.dial_tcp('$ip:21')? zftp.conn = net.dial_tcp('$ip:21')?
zftp.reader = io.new_buffered_reader(reader: zftp.conn) zftp.reader = io.new_buffered_reader(reader: zftp.conn)
@ -111,6 +113,7 @@ pub fn (mut zftp FTP) connect(ip string) ?bool {
return false return false
} }
// login sends the "USER `user`" and "PASS `passwd`" commands to the remote host.
pub fn (mut zftp FTP) login(user string, passwd string) ?bool { pub fn (mut zftp FTP) login(user string, passwd string) ?bool {
zftp.write('USER $user') or { zftp.write('USER $user') or {
$if debug { $if debug {
@ -138,11 +141,13 @@ pub fn (mut zftp FTP) login(user string, passwd string) ?bool {
return false return false
} }
// close closes the FTP connection.
pub fn (mut zftp FTP) close() ? { pub fn (mut zftp FTP) close() ? {
zftp.write('QUIT')? zftp.write('QUIT')?
zftp.conn.close()? zftp.conn.close()?
} }
// pwd returns the current working directory on the remote host for the logged in user.
pub fn (mut zftp FTP) pwd() ?string { pub fn (mut zftp FTP) pwd() ?string {
zftp.write('PWD')? zftp.write('PWD')?
_, data := zftp.read()? _, data := zftp.read()?
@ -153,6 +158,7 @@ pub fn (mut zftp FTP) pwd() ?string {
return data return data
} }
// cd changes the current working directory to the specified remote directory `dir`.
pub fn (mut zftp FTP) cd(dir string) ? { pub fn (mut zftp FTP) cd(dir string) ? {
zftp.write('CWD $dir') or { return } zftp.write('CWD $dir') or { return }
mut code, mut data := zftp.read()? mut code, mut data := zftp.read()?
@ -201,6 +207,7 @@ fn (mut zftp FTP) pasv() ?&DTP {
return dtp return dtp
} }
// dir returns a list of the files in the current working directory.
pub fn (mut zftp FTP) dir() ?[]string { pub fn (mut zftp FTP) dir() ?[]string {
mut dtp := zftp.pasv() or { return error('Cannot establish data connection') } mut dtp := zftp.pasv() or { return error('Cannot establish data connection') }
zftp.write('LIST')? zftp.write('LIST')?
@ -227,6 +234,7 @@ pub fn (mut zftp FTP) dir() ?[]string {
return dir return dir
} }
// get retrieves `file` from the remote host.
pub fn (mut zftp FTP) get(file string) ?[]u8 { pub fn (mut zftp FTP) get(file string) ?[]u8 {
mut dtp := zftp.pasv() or { return error('Cannot stablish data connection') } mut dtp := zftp.pasv() or { return error('Cannot stablish data connection') }
zftp.write('RETR $file')? zftp.write('RETR $file')?