From 5ba916ef7ac71bd1e7e23b4c87ae6a472f14fd6c Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Fri, 25 Nov 2011 12:03:58 +0100 Subject: Create erlang fallback for sendfile Created erlang fallback for sendfile in gen_tcp and moved sendfile from file to gen_tcp. Also created testcases for testing all different options to sendfile. For info about how sendfile should work see the BSD man pages as they contain a more complete API than other *nixes. --- erts/preloaded/src/prim_file.erl | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) (limited to 'erts/preloaded/src/prim_file.erl') diff --git a/erts/preloaded/src/prim_file.erl b/erts/preloaded/src/prim_file.erl index f3f977a30b..606d7d5aab 100644 --- a/erts/preloaded/src/prim_file.erl +++ b/erts/preloaded/src/prim_file.erl @@ -27,7 +27,7 @@ %% Generic file contents operations -export([open/2, close/1, datasync/1, sync/1, advise/4, position/2, truncate/1, write/2, pwrite/2, pwrite/3, read/2, read_line/1, pread/2, pread/3, - copy/3, sendfile/5]). + copy/3, sendfile/10]). %% Specialized file operations -export([open/1, open/3]). @@ -542,21 +542,49 @@ write_file(_, _) -> %% Returns {error, Reason} | {ok, BytesCopied} +sendfile(_,_,_,_,_,_,_,_,_,_) -> + {error, enotsup}; sendfile(#file_descriptor{module = ?MODULE, data = {Port, _}}, - DestFD, Offset, Bytes, ChunkSize) -> + DestFD, Offset, Bytes, ChunkSize, Headers, Trailers, + Nodiskio, MNowait, Sync) -> + ok = drv_command(Port, <>), + ChunkSize:64, + (get_bit(Nodiskio)):1, + (get_bit(MNowait)):1, + (get_bit(Sync)):1,0:5, + (encode_hdtl(Headers))/binary, + (encode_hdtl(Trailers))/binary>>), Self = self(), %% Should we use a ref()? receive {efile_reply, Self, Port, {ok, _Written}=OKRes}-> OKRes; {efile_reply, Self, Port, {error, _PosixError}=Error}-> - Error; - Unexpected -> - Unexpected + Error end. +get_bit(true) -> + 1; +get_bit(false) -> + 0. + +encode_hdtl(undefined) -> + <<0>>; +encode_hdtl([]) -> + <<0>>; +encode_hdtl(List) -> + encode_hdtl(List,<<>>,0). + +encode_hdtl([], Acc, Cnt) -> + <>; +encode_hdtl([Bin|T], Acc, Cnt) -> + encode_hdtl(T, <<(byte_size(Bin)):32, Bin/binary, Acc/binary>>,Cnt + 1). + + + + + %%%----------------------------------------------------------------- %%% Functions operating on files without handle to the file. ?DRV. %%% -- cgit v1.2.3