From e9da526e4cacffff5d62fd8741284e36ca83843e Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 19 Oct 2016 12:04:11 +0200 Subject: Eliminate dialyzer warning about unreachable clause --- lib/inets/src/ftp/ftp.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/inets/src/ftp/ftp.erl b/lib/inets/src/ftp/ftp.erl index 6868b75eff..b19b19f063 100644 --- a/lib/inets/src/ftp/ftp.erl +++ b/lib/inets/src/ftp/ftp.erl @@ -2368,8 +2368,7 @@ activate_ctrl_connection(#state{csock = Socket}) -> self() ! {socket_type(Socket), unwrap_socket(Socket), <<>>}. unwrap_socket({tcp,Socket}) -> Socket; -unwrap_socket({ssl,Socket}) -> Socket; -unwrap_socket(Socket) -> Socket. +unwrap_socket({ssl,Socket}) -> Socket. socket_type({tcp,_Socket}) -> tcp; socket_type({ssl,_Socket}) -> ssl. -- cgit v1.2.3 From 69238f6bc0d309f09a845bea831a98ac18d8e1e9 Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 19 Oct 2016 12:06:11 +0200 Subject: Code cleanups --- lib/inets/src/ftp/ftp.erl | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'lib') diff --git a/lib/inets/src/ftp/ftp.erl b/lib/inets/src/ftp/ftp.erl index b19b19f063..e43ba9e5be 100644 --- a/lib/inets/src/ftp/ftp.erl +++ b/lib/inets/src/ftp/ftp.erl @@ -2221,16 +2221,16 @@ setup_data_connection(#state{mode = active, {ok, Port} = inet:port(LSock), case FtpExt of false -> - {IP1, IP2, IP3, IP4} = IP, - {Port1, Port2} = {Port div 256, Port rem 256}, - send_ctrl_message(State, - mk_cmd("PORT ~w,~w,~w,~w,~w,~w", - [IP1, IP2, IP3, IP4, Port1, Port2])); - true -> - IpAddress = inet_parse:ntoa(IP), - Cmd = mk_cmd("EPRT |1|~s|~p|", [IpAddress, Port]), - send_ctrl_message(State, Cmd) - end, + {IP1, IP2, IP3, IP4} = IP, + {Port1, Port2} = {Port div 256, Port rem 256}, + send_ctrl_message(State, + mk_cmd("PORT ~w,~w,~w,~w,~w,~w", + [IP1, IP2, IP3, IP4, Port1, Port2])); + true -> + IpAddress = inet_parse:ntoa(IP), + Cmd = mk_cmd("EPRT |1|~s|~p|", [IpAddress, Port]), + send_ctrl_message(State, Cmd) + end, activate_ctrl_connection(State), {noreply, State#state{caller = {setup_data_connection, {LSock, Caller}}}} @@ -2377,8 +2377,8 @@ activate_data_connection(#state{dsock = Socket} = State) -> activate_connection(Socket), State. -activate_connection({tcp, Socket}) -> inet:setopts(Socket, [{active, once}]); -activate_connection({ssl, Socket}) -> ssl:setopts(Socket, [{active, once}]). +activate_connection({tcp, Socket}) -> inet:setopts(Socket, [{active, once}]); +activate_connection({ssl, Socket}) -> ssl:setopts(Socket, [{active, once}]). close_ctrl_connection(#state{csock = undefined}) -> ok; close_ctrl_connection(#state{csock = Socket}) -> close_connection(Socket). @@ -2386,7 +2386,7 @@ close_ctrl_connection(#state{csock = Socket}) -> close_connection(Socket). close_data_connection(#state{dsock = undefined}) -> ok; close_data_connection(#state{dsock = Socket}) -> close_connection(Socket). -close_connection({lsock,Socket}) -> gen_tcp:close(Socket); +close_connection({lsock,Socket}) -> gen_tcp:close(Socket); close_connection({tcp, Socket}) -> gen_tcp:close(Socket); close_connection({ssl, Socket}) -> ssl:close(Socket). -- cgit v1.2.3 From e311fd770869478f19d591e4229b09f61934f4f4 Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 19 Oct 2016 12:29:20 +0200 Subject: Declare types and specs --- lib/inets/src/ftp/ftp_progress.erl | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/inets/src/ftp/ftp_progress.erl b/lib/inets/src/ftp/ftp_progress.erl index 68185a222d..a6263e5cd7 100644 --- a/lib/inets/src/ftp/ftp_progress.erl +++ b/lib/inets/src/ftp/ftp_progress.erl @@ -36,11 +36,11 @@ -include_lib("kernel/include/file.hrl"). -record(progress, { - file, % string() - cb_module, % atom() - cb_function, % atom() - init_progress_term, % term() - current_progress_term % term() + file :: string() | 'undefined', + cb_module :: module(), + cb_function :: atom(), + init_progress_term :: term(), + current_progress_term :: term() }). %%%========================================================================= @@ -53,13 +53,15 @@ %% Description: Starts the progress report process unless progress reporting %% should not be performed. %%-------------------------------------------------------------------------- +-type options() :: 'ignore' | {module(), atom(), term()}. +-spec start_link(options()) -> 'ignore' | pid(). start_link(ignore) -> ignore; start_link(Options) -> spawn_link(?MODULE, init, [Options]). %%-------------------------------------------------------------------------- -%% report_progress(Pid, Report) -> _ +%% report_progress(Pid, Report) -> ok %% Pid = pid() %% Report = {local_file, File} | {remote_file, File} | %% {transfer_size, Size} @@ -68,17 +70,23 @@ start_link(Options) -> %% Description: Reports progress to the reporting process that calls the %% user defined callback function. %%-------------------------------------------------------------------------- +-type report() :: {'local_file', string()} | {'remote_file', string()} + | {'transfer_size', non_neg_integer()}. +-spec report(pid(), report()) -> 'ok'. report(Pid, Report) -> - Pid ! {progress_report, Report}. + Pid ! {progress_report, Report}, + ok. %%-------------------------------------------------------------------------- -%% stop(Pid) -> _ +%% stop(Pid) -> ok %% Pid = pid() %% %% Description: %%-------------------------------------------------------------------------- +-spec stop(pid()) -> 'ok'. stop(Pid) -> - Pid ! stop. + Pid ! stop, + ok. %%%========================================================================= %%% Internal functions -- cgit v1.2.3 From 5693b4cddd9a855a3277dc3aa51f22a2f768fe4a Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Wed, 19 Oct 2016 12:39:22 +0200 Subject: Eliminate unmatched return dialyzer warnings The code contained a lot of unmatched return warnings, mostly in function calls that were executed only for their side-effects. Also, there were various places where possible errors were not checked and now they are. This may cause crashes, both in the ftp testsuite (which I have not run) and in situations were errors occur but are ignored. --- lib/inets/src/ftp/ftp.erl | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'lib') diff --git a/lib/inets/src/ftp/ftp.erl b/lib/inets/src/ftp/ftp.erl index e43ba9e5be..d4959238e1 100644 --- a/lib/inets/src/ftp/ftp.erl +++ b/lib/inets/src/ftp/ftp.erl @@ -1096,7 +1096,7 @@ init(Options) -> erlang:monitor(process, Client), %% Make sure inet is started - inet_db:start(), + _ = inet_db:start(), %% Where are we {ok, Dir} = file:get_cwd(), @@ -1106,15 +1106,17 @@ init(Options) -> trace -> dbg:tracer(), dbg:p(all, [call]), - dbg:tpl(ftp, [{'_', [], [{return_trace}]}]), - dbg:tpl(ftp_response, [{'_', [], [{return_trace}]}]), - dbg:tpl(ftp_progress, [{'_', [], [{return_trace}]}]); + {ok, _} = dbg:tpl(ftp, [{'_', [], [{return_trace}]}]), + {ok, _} = dbg:tpl(ftp_response, [{'_', [], [{return_trace}]}]), + {ok, _} = dbg:tpl(ftp_progress, [{'_', [], [{return_trace}]}]), + ok; debug -> dbg:tracer(), dbg:p(all, [call]), - dbg:tp(ftp, [{'_', [], [{return_trace}]}]), - dbg:tp(ftp_response, [{'_', [], [{return_trace}]}]), - dbg:tp(ftp_progress, [{'_', [], [{return_trace}]}]); + {ok, _} = dbg:tp(ftp, [{'_', [], [{return_trace}]}]), + {ok, _} = dbg:tp(ftp_response, [{'_', [], [{return_trace}]}]), + {ok, _} = dbg:tp(ftp_progress, [{'_', [], [{return_trace}]}]), + ok; _ -> %% Keep silent ok @@ -1296,8 +1298,7 @@ handle_call({_,{rmdir, Dir}}, From, #state{chunk = false} = State) -> activate_ctrl_connection(State), {noreply, State#state{client = From}}; -handle_call({_,{type, Type}}, From, #state{chunk = false} - = State) -> +handle_call({_,{type, Type}}, From, #state{chunk = false} = State) -> case Type of ascii -> send_ctrl_message(State, mk_cmd("TYPE A", [])), @@ -1455,7 +1456,7 @@ handle_info({Trpt, Socket, Data}, #state{dsock = {Trpt,Socket}, caller = {recv_file, Fd}} = State0) when Trpt==tcp;Trpt==ssl -> ?DBG('L~p --data ~p ----> ~s~p~n',[?LINE,Socket,Data,State0]), - file_write(binary_to_list(Data), Fd), + ok = file_write(binary_to_list(Data), Fd), progress_report({binary, Data}, State0), State = activate_data_connection(State0), {noreply, State}; @@ -1476,7 +1477,7 @@ handle_info({Trpt, Socket, Data}, #state{dsock = {Trpt,Socket}} = State0) when T handle_info({Cls, Socket}, #state{dsock = {Trpt,Socket}, caller = {recv_file, Fd}} = State) when {Cls,Trpt}=={tcp_closed,tcp} ; {Cls,Trpt}=={ssl_closed,ssl} -> - file_close(Fd), + ok = file_close(Fd), progress_report({transfer_size, 0}, State), activate_ctrl_connection(State), {noreply, State#state{dsock = undefined, data = <<>>}}; @@ -2062,7 +2063,7 @@ handle_ctrl_result({pos_prel, _}, #state{caller = {recv_file, _}} = State0) -> end; handle_ctrl_result({Status, _}, #state{caller = {recv_file, Fd}} = State) -> - file_close(Fd), + ok = file_close(Fd), close_data_connection(State), ctrl_result_response(Status, State#state{dsock = undefined}, {error, epath}); @@ -2338,7 +2339,7 @@ accept_data_connection(#state{mode = passive} = State) -> send_ctrl_message(_S=#state{csock = Socket, verbose = Verbose}, Message) -> verbose(lists:flatten(Message),Verbose,send), ?DBG('<--ctrl ~p ---- ~s~p~n',[Socket,Message,_S]), - send_message(Socket, Message). + ok = send_message(Socket, Message). send_data_message(_S=#state{dsock = Socket}, Message) -> ?DBG('<==data ~p ==== ~s~n~p~n',[Socket,Message,_S]), @@ -2360,12 +2361,13 @@ send_message({ssl, Socket}, Message) -> ssl:send(Socket, Message). activate_ctrl_connection(#state{csock = Socket, ctrl_data = {<<>>, _, _}}) -> - activate_connection(Socket); + ok = activate_connection(Socket); activate_ctrl_connection(#state{csock = Socket}) -> - activate_connection(Socket), + ok = activate_connection(Socket), %% We have already received at least part of the next control message, %% that has been saved in ctrl_data, process this first. - self() ! {socket_type(Socket), unwrap_socket(Socket), <<>>}. + self() ! {socket_type(Socket), unwrap_socket(Socket), <<>>}, + ok. unwrap_socket({tcp,Socket}) -> Socket; unwrap_socket({ssl,Socket}) -> Socket. @@ -2374,7 +2376,7 @@ socket_type({tcp,_Socket}) -> tcp; socket_type({ssl,_Socket}) -> ssl. activate_data_connection(#state{dsock = Socket} = State) -> - activate_connection(Socket), + ok = activate_connection(Socket), State. activate_connection({tcp, Socket}) -> inet:setopts(Socket, [{active, once}]); @@ -2390,17 +2392,17 @@ close_connection({lsock,Socket}) -> gen_tcp:close(Socket); close_connection({tcp, Socket}) -> gen_tcp:close(Socket); close_connection({ssl, Socket}) -> ssl:close(Socket). -%% ------------ FILE HANDELING ---------------------------------------- +%% ------------ FILE HANDLING ---------------------------------------- send_file(#state{tls_upgrading_data_connection = {true, CTRL, _}} = State, Fd) -> {noreply, State#state{tls_upgrading_data_connection = {true, CTRL, ?MODULE, send_file, Fd}}}; send_file(State, Fd) -> case file_read(Fd) of - {ok, N, Bin} when N > 0-> + {ok, N, Bin} when N > 0 -> send_data_message(State, Bin), progress_report({binary, Bin}, State), send_file(State, Fd); {ok, _, _} -> - file_close(Fd), + ok = file_close(Fd), close_data_connection(State), progress_report({transfer_size, 0}, State), activate_ctrl_connection(State), @@ -2507,7 +2509,7 @@ progress_report(stop, #state{progress = ProgressPid}) -> ftp_progress:stop(ProgressPid); progress_report({binary, Data}, #state{progress = ProgressPid}) -> ftp_progress:report(ProgressPid, {transfer_size, size(Data)}); -progress_report(Report, #state{progress = ProgressPid}) -> +progress_report(Report, #state{progress = ProgressPid}) -> ftp_progress:report(ProgressPid, Report). -- cgit v1.2.3 From 96659515747469152efad13819ddb55be2a76f6d Mon Sep 17 00:00:00 2001 From: Kostis Sagonas Date: Fri, 21 Oct 2016 22:04:29 +0200 Subject: Change two calls to file_close/1 to allow {error,einval} --- lib/inets/src/ftp/ftp.erl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'lib') diff --git a/lib/inets/src/ftp/ftp.erl b/lib/inets/src/ftp/ftp.erl index d4959238e1..911f5b71a7 100644 --- a/lib/inets/src/ftp/ftp.erl +++ b/lib/inets/src/ftp/ftp.erl @@ -1475,16 +1475,19 @@ handle_info({Trpt, Socket, Data}, #state{dsock = {Trpt,Socket}} = State0) when T Data/binary>>}}; handle_info({Cls, Socket}, #state{dsock = {Trpt,Socket}, - caller = {recv_file, Fd}} - = State) when {Cls,Trpt}=={tcp_closed,tcp} ; {Cls,Trpt}=={ssl_closed,ssl} -> - ok = file_close(Fd), + caller = {recv_file, Fd}} = State) + when {Cls,Trpt}=={tcp_closed,tcp} ; {Cls,Trpt}=={ssl_closed,ssl} -> + case file_close(Fd) of + ok -> ok; + {error,einval} -> ok + end, progress_report({transfer_size, 0}, State), activate_ctrl_connection(State), {noreply, State#state{dsock = undefined, data = <<>>}}; handle_info({Cls, Socket}, #state{dsock = {Trpt,Socket}, client = From, - caller = recv_chunk} - = State) when {Cls,Trpt}=={tcp_closed,tcp} ; {Cls,Trpt}=={ssl_closed,ssl} -> + caller = recv_chunk} = State) + when {Cls,Trpt}=={tcp_closed,tcp} ; {Cls,Trpt}=={ssl_closed,ssl} -> gen_server:reply(From, ok), {noreply, State#state{dsock = undefined, client = undefined, data = <<>>, caller = undefined, @@ -2063,7 +2066,10 @@ handle_ctrl_result({pos_prel, _}, #state{caller = {recv_file, _}} = State0) -> end; handle_ctrl_result({Status, _}, #state{caller = {recv_file, Fd}} = State) -> - ok = file_close(Fd), + case file_close(Fd) of + ok -> ok; + {error, einval} -> ok + end, close_data_connection(State), ctrl_result_response(Status, State#state{dsock = undefined}, {error, epath}); -- cgit v1.2.3