Skip to content

Commit b99f92c

Browse files
committed
sendfile fix
1 parent d5de5e4 commit b99f92c

File tree

3 files changed

+50
-55
lines changed

3 files changed

+50
-55
lines changed

src/osiris_file.erl

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123

124124
-optional_callbacks([write/2]).
125125

126+
-define(DEFAULT_FILE, osiris_file_default).
127+
126128
-spec advise(Handle, Offset, Length, Advise) -> ok | {error, Reason} when
127129
Handle :: file_handle(),
128130
Offset :: integer(),
@@ -133,7 +135,7 @@
133135
advise({Mod, Handle}, Offset, Length, Advise) ->
134136
Mod:advise(Handle, Offset, Length, Advise);
135137
advise(Handle, Offset, Length, Advise) ->
136-
file:advise(Handle, Offset, Length, Advise).
138+
?DEFAULT_FILE:advise(Handle, Offset, Length, Advise).
137139

138140

139141
-spec close(Handle) -> ok | {error, Reason} when
@@ -143,7 +145,7 @@ advise(Handle, Offset, Length, Advise) ->
143145
close({Mod, Handle}) ->
144146
Mod:close(Handle);
145147
close(Handle) ->
146-
file:close(Handle).
148+
?DEFAULT_FILE:close(Handle).
147149

148150

149151
-spec copy(Source, Destination) -> {ok, BytesCopied} | {error, Reason} when
@@ -155,7 +157,7 @@ close(Handle) ->
155157
Reason :: file:posix() | badarg | terminated.
156158
%% TODO
157159
copy(Source, Destination) ->
158-
file:copy(Source, Destination).
160+
?DEFAULT_FILE:copy(Source, Destination).
159161

160162
-spec del_dir(Dir) -> ok | {error, Reason} when
161163
Dir :: file:name_all(),
@@ -191,7 +193,7 @@ prim_delete(File) ->
191193
Reason :: file:posix().
192194
%% Only used for local files
193195
ensure_dir(Dir) ->
194-
filelib:ensure_dir(Dir).
196+
?DEFAULT_FILE:ensure_dir(Dir).
195197

196198

197199
-spec list_dir(Dir) -> {ok, Filenames} | {error, Reason} when
@@ -211,7 +213,7 @@ list_dir(Dir) ->
211213
Reason :: file:posix() | badarg.
212214
%% Only used for the local segment file, no need to change it.
213215
make_dir(Dir) ->
214-
file:make_dir(Dir).
216+
?DEFAULT_FILE:make_dir(Dir).
215217

216218

217219
-spec open(File, Modes) -> {ok, file_handle()} | {error, Reason} when
@@ -223,7 +225,7 @@ open(File, Options) ->
223225
case lists:member(write, Options) of
224226
true ->
225227
%% We do not use tiered storage for writes
226-
file:open(File, Options);
228+
?DEFAULT_FILE:open(File, Options);
227229
false ->
228230
%% Here we will get the correct Mod based on config/manifest file etc.
229231
Mod = get_mod(File),
@@ -241,7 +243,7 @@ open(File, Options) ->
241243
position({Mod, Handle}, Position) ->
242244
Mod:position(Handle, Position);
243245
position(Handle, Position) ->
244-
file:position(Handle, Position).
246+
?DEFAULT_FILE:position(Handle, Position).
245247

246248

247249
-spec pread(Handle, Location, Number) ->
@@ -270,7 +272,7 @@ pread(Handle, Position, Size) ->
270272
read({Mod, Handle}, Size) ->
271273
Mod:read(Handle, Size);
272274
read(Handle, Size) ->
273-
file:read(Handle, Size).
275+
?DEFAULT_FILE:read(Handle, Size).
274276

275277

276278
-spec read_file_info(File) -> {ok, FileInfo} | {error, Reason} when
@@ -283,20 +285,20 @@ read_file_info(File) ->
283285
Mod:read_file_info(File).
284286

285287

286-
-spec sendfile(Handle, Socket, Offset, Bytes, Opts) ->
287-
{ok, non_neg_integer()} | {error, inet:posix() |
288-
closed | badarg | not_owner} when
288+
-spec sendfile(Transport, Handle, Socket, Offset, Bytes) ->
289+
ok | {error, inet:posix() |
290+
closed | badarg | not_owner} when
291+
Transport :: tcp | ssl,
289292
Handle :: file_handle(),
290293
Socket :: inet:socket() | socket:socket() |
291294
fun ((iolist()) -> ok | {error, inet:posix() | closed}),
292295
Offset :: non_neg_integer(),
293-
Bytes :: non_neg_integer(),
294-
Opts :: [sendfile_option()].
296+
Bytes :: non_neg_integer().
295297

296-
sendfile({Mod, Handle}, Socket, Offset, Length, Options) ->
297-
Mod:sendfile(Handle, Socket, Offset, Length, Options);
298-
sendfile(Handle, Socket, Offset, Length, Options) ->
299-
file:sendfile(Handle, Socket, Offset, Length, Options).
298+
sendfile(Transport, {Mod, Handle}, Socket, Offset, Length) ->
299+
Mod:sendfile(Transport, Handle, Socket, Offset, Length);
300+
sendfile(Transport, Handle, Socket, Offset, Length) ->
301+
?DEFAULT_FILE:sendfile(Transport, Handle, Socket, Offset, Length).
300302

301303

302304
-spec truncate(Handle) -> ok | {error, Reason} when
@@ -306,7 +308,7 @@ sendfile(Handle, Socket, Offset, Length, Options) ->
306308
truncate({Mod, Handle}) ->
307309
Mod:truncate(Handle);
308310
truncate(Handle) ->
309-
file:truncate(Handle).
311+
?DEFAULT_FILE:truncate(Handle).
310312

311313

312314
-spec write(Handle, Bytes) -> ok | {error, Reason} when
@@ -316,7 +318,7 @@ truncate(Handle) ->
316318
write({Mod, Handle}, Data) ->
317319
Mod:write(Handle, Data);
318320
write(Handle, Data) ->
319-
file:write(Handle, Data).
321+
?DEFAULT_FILE:write(Handle, Data).
320322

321323
%% -spec try_write(module(), term(), iodata()) ->
322324
%% ok | {error, term()}.
@@ -329,25 +331,23 @@ write(Handle, Data) ->
329331
%% end.
330332

331333

334+
%% TODO code below just hack to make it work for now.
332335
-spec get_mod() -> module().
333336

334337
get_mod() ->
335-
get_mod(file).
338+
?DEFAULT_FILE;
336339

337-
get_mod(file) ->
338-
%% TODO. This will figure out the correct module to use, based on
339-
%% info in the magical manifest file.
340-
file;
341340
get_mod(prim_file) ->
342341
%% Just temporary solutin till I figure out why
343342
%% we even use prim_file?
344-
prim_file;
343+
%% prim_file;
344+
?DEFAULT_FILE;
345345
get_mod(File) ->
346346
case filelib:is_file(File) of
347347
true ->
348-
file;
348+
?DEFAULT_FILE;
349349
false ->
350-
application:get_env(osiris, io_segment_module, file)
350+
application:get_env(osiris, io_segment_module, ?DEFAULT_FILE)
351351
end.
352352

353353

@@ -356,7 +356,7 @@ get_mod(prim_file, File) ->
356356
%% we even use prim_file?
357357
case filelib:is_file(File) of
358358
true ->
359-
prim_file;
359+
?DEFAULT_FILE;
360360
false ->
361-
application:get_env(osiris, io_segment_module, prim_file)
361+
application:get_env(osiris, io_segment_module, ?DEFAULT_FILE)
362362
end.

src/osiris_file_default.erl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%% Really need a better name...
12
-module(osiris_file_default).
23
-behaviour(osiris_file).
34

@@ -61,8 +62,25 @@ read(Handle, Size) ->
6162
read_file_info(File) ->
6263
prim_file:read_file_info(File).
6364

64-
sendfile(Handle, Socket, Offset, Length, Options) ->
65-
file:sendfile(Handle, Socket, Offset, Length, Options).
65+
sendfile(_Transport, _Handle, _Sock, _Pos, 0) ->
66+
ok;
67+
sendfile(tcp = Transport, Handle, Sock, Pos, ToSend) ->
68+
case file:sendfile(Handle, Sock, Pos, ToSend, []) of
69+
{ok, 0} ->
70+
%% TODO add counter for this?
71+
sendfile(Transport, Handle, Sock, Pos, ToSend);
72+
{ok, BytesSent} ->
73+
sendfile(Transport, Handle, Sock, Pos + BytesSent, ToSend - BytesSent);
74+
{error, _} = Err ->
75+
Err
76+
end;
77+
sendfile(ssl, Handle, Sock, Pos, ToSend) ->
78+
case file:pread(Handle, Pos, ToSend) of
79+
{ok, Data} ->
80+
ssl:send(Sock, Data);
81+
{error, _} = Err ->
82+
Err
83+
end.
6684

6785
truncate(Handle) ->
6886
file:truncate(Handle).

src/osiris_log.erl

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,7 +1696,7 @@ send_file(Sock,
16961696
_ = Callback(Header, ToSend + byte_size(HeaderData)),
16971697
case send(Transport, Sock, HeaderData) of
16981698
ok ->
1699-
case sendfile(Transport, Fd, Sock,
1699+
case osiris_file:sendfile(Transport, Fd, Sock,
17001700
Pos + ?HEADER_SIZE_B + ToSkip, ToSend) of
17011701
ok ->
17021702
State = State1#?MODULE{mode = Read},
@@ -2447,29 +2447,6 @@ max_segment_size_reached(
24472447
CurrentSizeBytes >= MaxSizeBytes orelse
24482448
CurrentSizeChunks >= MaxSizeChunks.
24492449

2450-
%%TODO This should be fully up to the backend.
2451-
sendfile(_Transport, _Fd, _Sock, _Pos, 0) ->
2452-
ok;
2453-
sendfile(tcp = Transport, Fd, Sock, Pos, ToSend) ->
2454-
case osiris_file:sendfile(Fd, Sock, Pos, ToSend, []) of
2455-
ok ->
2456-
ok;
2457-
{ok, 0} ->
2458-
%% TODO add counter for this?
2459-
sendfile(Transport, Fd, Sock, Pos, ToSend);
2460-
{ok, BytesSent} ->
2461-
sendfile(Transport, Fd, Sock, Pos + BytesSent, ToSend - BytesSent);
2462-
{error, _} = Err ->
2463-
Err
2464-
end;
2465-
sendfile(ssl, Fd, Sock, Pos, ToSend) ->
2466-
case osiris_file:pread(Fd, Pos, ToSend) of
2467-
{ok, Data} ->
2468-
ssl:send(Sock, Data);
2469-
{error, _} = Err ->
2470-
Err
2471-
end.
2472-
24732450
send(tcp, Sock, Data) ->
24742451
gen_tcp:send(Sock, Data);
24752452
send(ssl, Sock, Data) ->

0 commit comments

Comments
 (0)