diff options
author | John Högberg <[email protected]> | 2017-11-02 21:27:56 +0100 |
---|---|---|
committer | John Högberg <[email protected]> | 2017-11-30 15:44:39 +0100 |
commit | 8e634f0063acb8009c2afb303111a2fd35aad78b (patch) | |
tree | 9f2221d07b699d695e831ed2581753e363ebe80a /lib | |
parent | c9fc20af633bbdc68206ca7d7438cd803793875f (diff) | |
download | otp-8e634f0063acb8009c2afb303111a2fd35aad78b.tar.gz otp-8e634f0063acb8009c2afb303111a2fd35aad78b.tar.bz2 otp-8e634f0063acb8009c2afb303111a2fd35aad78b.zip |
Make file:sendfile/5 follow its documented error behavior
Diffstat (limited to 'lib')
-rw-r--r-- | lib/kernel/src/file.erl | 21 | ||||
-rw-r--r-- | lib/kernel/test/sendfile_SUITE.erl | 36 |
2 files changed, 46 insertions, 11 deletions
diff --git a/lib/kernel/src/file.erl b/lib/kernel/src/file.erl index b5a51c3410..d05199897f 100644 --- a/lib/kernel/src/file.erl +++ b/lib/kernel/src/file.erl @@ -1236,15 +1236,18 @@ sendfile(File, _Sock, _Offet, _Bytes, _Opts) when is_pid(File) -> sendfile(File, Sock, Offset, Bytes, []) -> sendfile(File, Sock, Offset, Bytes, ?MAX_CHUNK_SIZE, [], [], []); sendfile(File, Sock, Offset, Bytes, Opts) -> - ChunkSize0 = proplists:get_value(chunk_size, Opts, ?MAX_CHUNK_SIZE), - ChunkSize = if ChunkSize0 > ?MAX_CHUNK_SIZE -> - ?MAX_CHUNK_SIZE; - true -> ChunkSize0 - end, - %% Support for headers, trailers and options has been removed because the - %% Darwin and BSD API for using it does not play nice with - %% non-blocking sockets. See unix_efile.c for more info. - sendfile(File, Sock, Offset, Bytes, ChunkSize, [], [], Opts). + try proplists:get_value(chunk_size, Opts, ?MAX_CHUNK_SIZE) of + ChunkSize0 when is_integer(ChunkSize0) -> + ChunkSize = erlang:min(ChunkSize0, ?MAX_CHUNK_SIZE), + %% Support for headers, trailers and options has been removed + %% because the Darwin and BSD API for using it does not play nice + %% with non-blocking sockets. See unix_efile.c for more info. + sendfile(File, Sock, Offset, Bytes, ChunkSize, [], [], Opts); + _Other -> + {error, badarg} + catch + error:_ -> {error, badarg} + end. %% sendfile/2 -spec sendfile(Filename, Socket) -> diff --git a/lib/kernel/test/sendfile_SUITE.erl b/lib/kernel/test/sendfile_SUITE.erl index 4872ce53b7..0c0b1cbcb6 100644 --- a/lib/kernel/test/sendfile_SUITE.erl +++ b/lib/kernel/test/sendfile_SUITE.erl @@ -40,7 +40,8 @@ t_sendfile_sendduring/1, t_sendfile_recvduring/1, t_sendfile_closeduring/1, - t_sendfile_crashduring/1]). + t_sendfile_crashduring/1, + t_sendfile_arguments/1]). all() -> [t_sendfile_small, @@ -55,7 +56,8 @@ all() -> t_sendfile_sendduring, t_sendfile_recvduring, t_sendfile_closeduring, - t_sendfile_crashduring]. + t_sendfile_crashduring, + t_sendfile_arguments]. init_per_suite(Config) -> case {os:type(),os:version()} of @@ -371,6 +373,36 @@ t_sendfile_crashduring(Config) -> end end. +t_sendfile_arguments(Config) -> + Filename = proplists:get_value(small_file, Config), + + {ok, Listener} = gen_tcp:listen(0, + [{packet, 0}, {active, false}, {reuseaddr, true}]), + {ok, Port} = inet:port(Listener), + + ErrorCheck = + fun(Reason, Offset, Length, Opts) -> + {ok, Sender} = gen_tcp:connect({127, 0, 0, 1}, Port, + [{packet, 0}, {active, false}]), + {ok, Receiver} = gen_tcp:accept(Listener), + {ok, Fd} = file:open(Filename, [read, raw]), + {error, Reason} = file:sendfile(Fd, Sender, Offset, Length, Opts), + gen_tcp:close(Receiver), + gen_tcp:close(Sender), + file:close(Fd) + end, + + ErrorCheck(einval, -1, 0, []), + ErrorCheck(einval, 0, -1, []), + ErrorCheck(badarg, gurka, 0, []), + ErrorCheck(badarg, 0, gurka, []), + ErrorCheck(badarg, 0, 0, gurka), + ErrorCheck(badarg, 0, 0, [{chunk_size, gurka}]), + + gen_tcp:close(Listener), + + ok. + %% Generic sendfile server code sendfile_send(Send) -> sendfile_send({127,0,0,1},Send). |