aboutsummaryrefslogtreecommitdiffstats
path: root/lib/kernel/src
diff options
context:
space:
mode:
authorRaimo Niskanen <[email protected]>2015-11-24 11:57:22 +0100
committerRaimo Niskanen <[email protected]>2015-11-24 11:57:22 +0100
commit6b5ea6ecb4eed204496cf196d98e7453df02c6dd (patch)
tree0c5d1d7de983c7c2a10e9a91e934e0a55965f91c /lib/kernel/src
parentfa2b944f13fc85bdee593e1896c61ca2e3a7def9 (diff)
downloadotp-6b5ea6ecb4eed204496cf196d98e7453df02c6dd.tar.gz
otp-6b5ea6ecb4eed204496cf196d98e7453df02c6dd.tar.bz2
otp-6b5ea6ecb4eed204496cf196d98e7453df02c6dd.zip
Clean up code for file:position/2
Diffstat (limited to 'lib/kernel/src')
-rw-r--r--lib/kernel/src/file_io_server.erl64
1 files changed, 36 insertions, 28 deletions
diff --git a/lib/kernel/src/file_io_server.erl b/lib/kernel/src/file_io_server.erl
index d0ad43afbf..5b2cae6b02 100644
--- a/lib/kernel/src/file_io_server.erl
+++ b/lib/kernel/src/file_io_server.erl
@@ -1,7 +1,7 @@
%%
%% %CopyrightBegin%
%%
-%% Copyright Ericsson AB 2000-2013. All Rights Reserved.
+%% Copyright Ericsson AB 2000-2015. All Rights Reserved.
%%
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
@@ -217,23 +217,23 @@ file_request({allocate, Offset, Length},
file_request({pread,At,Sz},
#state{handle=Handle,buf=Buf,read_mode=ReadMode}=State) ->
case position(Handle, At, Buf) of
- {ok,_Offs} ->
+ {error,_} = Reply ->
+ {error,Reply,State};
+ _ ->
case ?PRIM_FILE:read(Handle, Sz) of
{ok,Bin} when ReadMode =:= list ->
std_reply({ok,binary_to_list(Bin)}, State);
Reply ->
std_reply(Reply, State)
- end;
- Reply ->
- std_reply(Reply, State)
+ end
end;
file_request({pwrite,At,Data},
#state{handle=Handle,buf=Buf}=State) ->
case position(Handle, At, Buf) of
- {ok,_Offs} ->
- std_reply(?PRIM_FILE:write(Handle, Data), State);
- Reply ->
- std_reply(Reply, State)
+ {error,_} = Reply ->
+ {error,Reply,State};
+ _ ->
+ std_reply(?PRIM_FILE:write(Handle, Data), State)
end;
file_request(datasync,
#state{handle=Handle}=State) ->
@@ -257,10 +257,10 @@ file_request(close,
file_request({position,At},
#state{handle=Handle,buf=Buf}=State) ->
case position(Handle, At, Buf) of
- {error, _Reason}=Reply ->
- {error,Reply,State};
- Reply ->
- {reply,Reply,State#state{buf= <<>>}}
+ {error,_} = Reply ->
+ {error,Reply,State};
+ Reply ->
+ std_reply(Reply, State)
end;
file_request(truncate,
#state{handle=Handle}=State) ->
@@ -268,13 +268,14 @@ file_request(truncate,
{error,_Reason}=Reply ->
{stop,normal,Reply,State#state{buf= <<>>}};
Reply ->
- {reply,Reply,State}
+ std_reply(Reply, State)
end;
file_request(Unknown,
#state{}=State) ->
Reason = {request, Unknown},
{error,{error,Reason},State}.
+%% Standard reply and clear buffer
std_reply({error,_}=Reply, State) ->
{error,Reply,State#state{buf= <<>>}};
std_reply(Reply, State) ->
@@ -291,7 +292,7 @@ io_request({put_chars, Enc, Chars},
#state{handle=Handle,buf=Buf}=State) ->
case position(Handle, cur, Buf) of
{error,_}=Reply ->
- {stop,normal,Reply,State#state{buf= <<>>}};
+ {stop,normal,Reply,State};
_ ->
put_chars(Chars, Enc, State#state{buf= <<>>})
end;
@@ -372,23 +373,27 @@ io_request_loop([Request|Tail],
%% I/O request put_chars
%%
put_chars(Chars, latin1, #state{handle=Handle, unic=latin1}=State) ->
+ NewState = State#state{buf = <<>>},
case ?PRIM_FILE:write(Handle, Chars) of
{error,_}=Reply ->
- {stop,normal,Reply,State};
+ {stop,normal,Reply,NewState};
Reply ->
- {reply,Reply,State}
+ {reply,Reply,NewState}
end;
put_chars(Chars, InEncoding, #state{handle=Handle, unic=OutEncoding}=State) ->
+ NewState = State#state{buf = <<>>},
case unicode:characters_to_binary(Chars,InEncoding,OutEncoding) of
Bin when is_binary(Bin) ->
case ?PRIM_FILE:write(Handle, Bin) of
{error,_}=Reply ->
- {stop,normal,Reply,State};
+ {stop,normal,Reply,NewState};
Reply ->
- {reply,Reply,State}
+ {reply,Reply,NewState}
end;
{error,_,_} ->
- {stop,normal,{error,{no_translation, InEncoding, OutEncoding}},State}
+ {stop,normal,
+ {error,{no_translation, InEncoding, OutEncoding}},
+ NewState}
end.
%%
@@ -902,11 +907,14 @@ cbv({utf32,little},_) ->
%% Compensates ?PRIM_FILE:position/2 for the number of bytes
%% we have buffered
-
-position(Handle, cur, Buf) ->
- position(Handle, {cur, 0}, Buf);
-position(Handle, {cur, Offs}, Buf) when is_binary(Buf) ->
- ?PRIM_FILE:position(Handle, {cur, Offs-byte_size(Buf)});
-position(Handle, At, _Buf) ->
- ?PRIM_FILE:position(Handle, At).
-
+position(Handle, At, Buf) ->
+ ?PRIM_FILE:position(
+ Handle,
+ case At of
+ cur ->
+ {cur, -byte_size(Buf)};
+ {cur, Offs} ->
+ {cur, Offs-byte_size(Buf)};
+ _ ->
+ At
+ end).