mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
all: change optional to result of io (#16075)
This commit is contained in:
@ -42,7 +42,7 @@ mut:
|
||||
port int
|
||||
}
|
||||
|
||||
fn (mut dtp DTP) read() ?[]u8 {
|
||||
fn (mut dtp DTP) read() ![]u8 {
|
||||
mut data := []u8{}
|
||||
mut buf := []u8{len: 1024}
|
||||
for {
|
||||
@ -75,15 +75,15 @@ pub fn new() FTP {
|
||||
return f
|
||||
}
|
||||
|
||||
fn (mut zftp FTP) write(data string) ?int {
|
||||
fn (mut zftp FTP) write(data string) !int {
|
||||
$if debug {
|
||||
println('FTP.v >>> $data')
|
||||
}
|
||||
return zftp.conn.write('$data\r\n'.bytes())
|
||||
}
|
||||
|
||||
fn (mut zftp FTP) read() ?(int, string) {
|
||||
mut data := zftp.reader.read_line()?
|
||||
fn (mut zftp FTP) read() !(int, string) {
|
||||
mut data := zftp.reader.read_line()!
|
||||
$if debug {
|
||||
println('FTP.v <<< $data')
|
||||
}
|
||||
@ -93,7 +93,7 @@ fn (mut zftp FTP) read() ?(int, string) {
|
||||
code := data[..3].int()
|
||||
if data[3] == `-` {
|
||||
for {
|
||||
data = zftp.reader.read_line()?
|
||||
data = zftp.reader.read_line()!
|
||||
if data[..3].int() == code && data[3] != `-` {
|
||||
break
|
||||
}
|
||||
@ -103,10 +103,10 @@ fn (mut zftp FTP) read() ?(int, string) {
|
||||
}
|
||||
|
||||
// connect establishes an FTP connection to the host at `ip` port 21.
|
||||
pub fn (mut zftp FTP) connect(ip string) ?bool {
|
||||
zftp.conn = net.dial_tcp('$ip:21')?
|
||||
pub fn (mut zftp FTP) connect(ip string) !bool {
|
||||
zftp.conn = net.dial_tcp('$ip:21')!
|
||||
zftp.reader = io.new_buffered_reader(reader: zftp.conn)
|
||||
code, _ := zftp.read()?
|
||||
code, _ := zftp.read()!
|
||||
if code == ftp.connected {
|
||||
return true
|
||||
}
|
||||
@ -114,14 +114,14 @@ pub fn (mut zftp FTP) connect(ip string) ?bool {
|
||||
}
|
||||
|
||||
// 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 {
|
||||
$if debug {
|
||||
println('ERROR sending user')
|
||||
}
|
||||
return false
|
||||
}
|
||||
mut code, _ := zftp.read()?
|
||||
mut code, _ := zftp.read()!
|
||||
if code == ftp.logged_in {
|
||||
return true
|
||||
}
|
||||
@ -134,7 +134,7 @@ pub fn (mut zftp FTP) login(user string, passwd string) ?bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
code, _ = zftp.read()?
|
||||
code, _ = zftp.read()!
|
||||
if code == ftp.logged_in {
|
||||
return true
|
||||
}
|
||||
@ -142,15 +142,15 @@ pub fn (mut zftp FTP) login(user string, passwd string) ?bool {
|
||||
}
|
||||
|
||||
// close closes the FTP connection.
|
||||
pub fn (mut zftp FTP) close() ? {
|
||||
zftp.write('QUIT')?
|
||||
zftp.conn.close()?
|
||||
pub fn (mut zftp FTP) close() ! {
|
||||
zftp.write('QUIT')!
|
||||
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 {
|
||||
zftp.write('PWD')?
|
||||
_, data := zftp.read()?
|
||||
pub fn (mut zftp FTP) pwd() !string {
|
||||
zftp.write('PWD')!
|
||||
_, data := zftp.read()!
|
||||
spl := data.split('"') // "
|
||||
if spl.len >= 2 {
|
||||
return spl[1]
|
||||
@ -159,9 +159,9 @@ pub fn (mut zftp FTP) pwd() ?string {
|
||||
}
|
||||
|
||||
// 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 }
|
||||
mut code, mut data := zftp.read()?
|
||||
mut code, mut data := zftp.read()!
|
||||
match int(code) {
|
||||
ftp.denied {
|
||||
$if debug {
|
||||
@ -169,7 +169,7 @@ pub fn (mut zftp FTP) cd(dir string) ? {
|
||||
}
|
||||
}
|
||||
ftp.complete {
|
||||
code, data = zftp.read()?
|
||||
code, data = zftp.read()!
|
||||
}
|
||||
else {}
|
||||
}
|
||||
@ -178,7 +178,7 @@ pub fn (mut zftp 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')
|
||||
}
|
||||
@ -194,32 +194,32 @@ fn new_dtp(msg string) ?&DTP {
|
||||
return dtp
|
||||
}
|
||||
|
||||
fn (mut zftp FTP) pasv() ?&DTP {
|
||||
zftp.write('PASV')?
|
||||
code, data := zftp.read()?
|
||||
fn (mut zftp FTP) pasv() !&DTP {
|
||||
zftp.write('PASV')!
|
||||
code, data := zftp.read()!
|
||||
$if debug {
|
||||
println('pass: $data')
|
||||
}
|
||||
if code != ftp.passive_mode {
|
||||
return error('pasive mode not allowed')
|
||||
}
|
||||
dtp := new_dtp(data)?
|
||||
dtp := new_dtp(data)!
|
||||
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') }
|
||||
zftp.write('LIST')?
|
||||
code, _ := zftp.read()?
|
||||
zftp.write('LIST')!
|
||||
code, _ := zftp.read()!
|
||||
if code == ftp.denied {
|
||||
return error('`LIST` denied')
|
||||
}
|
||||
if code != ftp.open_data_connection {
|
||||
return error('Data channel empty')
|
||||
}
|
||||
list_dir := dtp.read()?
|
||||
result, _ := zftp.read()?
|
||||
list_dir := dtp.read()!
|
||||
result, _ := zftp.read()!
|
||||
if result != ftp.close_data_connection {
|
||||
println('`LIST` not ok')
|
||||
}
|
||||
@ -235,17 +235,17 @@ pub fn (mut zftp FTP) dir() ?[]string {
|
||||
}
|
||||
|
||||
// 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') }
|
||||
zftp.write('RETR $file')?
|
||||
code, _ := zftp.read()?
|
||||
zftp.write('RETR $file')!
|
||||
code, _ := zftp.read()!
|
||||
if code == ftp.denied {
|
||||
return error('Permission denied')
|
||||
}
|
||||
if code != ftp.open_data_connection {
|
||||
return error('Data connection not ready')
|
||||
}
|
||||
blob := dtp.read()?
|
||||
blob := dtp.read()!
|
||||
dtp.close()
|
||||
return blob
|
||||
}
|
||||
|
Reference in New Issue
Block a user