From 195e1f19b06095f39a4fb0da46dfab2ec5b10e9a Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Thu, 13 Jan 2011 12:36:14 +0100 Subject: Implement file:sendfile Allow Erlang code to use sendfile() where available by wrapping it as file:sendfile/4 and file:sendfile/2. sendfile(2) - Linux man page: "sendfile() copies data between one file descriptor and another. Because this copying is done within the kernel, sendfile() is more efficient than the combination of read(2) and write(2), which would require transferring data to and from user space." --- erts/preloaded/src/prim_file.erl | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'erts/preloaded') diff --git a/erts/preloaded/src/prim_file.erl b/erts/preloaded/src/prim_file.erl index 30b7a5246a..f3f977a30b 100644 --- a/erts/preloaded/src/prim_file.erl +++ b/erts/preloaded/src/prim_file.erl @@ -26,7 +26,8 @@ %% 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]). + write/2, pwrite/2, pwrite/3, read/2, read_line/1, pread/2, pread/3, + copy/3, sendfile/5]). %% Specialized file operations -export([open/1, open/3]). @@ -98,6 +99,7 @@ -define(FILE_READ_LINE, 29). -define(FILE_FDATASYNC, 30). -define(FILE_ADVISE, 31). +-define(FILE_SENDFILE, 32). %% Driver responses -define(FILE_RESP_OK, 0). @@ -539,6 +541,21 @@ write_file(_, _) -> {error, badarg}. +%% Returns {error, Reason} | {ok, BytesCopied} +sendfile(#file_descriptor{module = ?MODULE, data = {Port, _}}, + DestFD, Offset, Bytes, ChunkSize) -> + ok = drv_command(Port, <>), + 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 + end. %%%----------------------------------------------------------------- %%% Functions operating on files without handle to the file. ?DRV. -- cgit v1.2.3