diff options
author | Björn Gustavsson <[email protected]> | 2016-09-29 12:08:49 +0200 |
---|---|---|
committer | Björn Gustavsson <[email protected]> | 2016-09-29 12:08:49 +0200 |
commit | 81082ce34fcaa97b0ef66a5e445fdbf2e3b9644c (patch) | |
tree | 50f1d9741ea92b233ed950270a045982aa8963bd /lib/kernel | |
parent | b353bc12df008f9a30c5ec4f71b51ae70a3102d9 (diff) | |
parent | 07b0f4315b079cce7aeef7babdba1bbb686de611 (diff) | |
download | otp-81082ce34fcaa97b0ef66a5e445fdbf2e3b9644c.tar.gz otp-81082ce34fcaa97b0ef66a5e445fdbf2e3b9644c.tar.bz2 otp-81082ce34fcaa97b0ef66a5e445fdbf2e3b9644c.zip |
Merge branch 'nathanl/kernel/write_file-uses-writev/PR-1149/OTP-13909'
* nathanl/kernel/write_file-uses-writev/PR-1149/OTP-13909:
Let file:write_file/3 use writev
Diffstat (limited to 'lib/kernel')
-rw-r--r-- | lib/kernel/src/file.erl | 49 |
1 files changed, 30 insertions, 19 deletions
diff --git a/lib/kernel/src/file.erl b/lib/kernel/src/file.erl index 58b601e456..1971df9038 100644 --- a/lib/kernel/src/file.erl +++ b/lib/kernel/src/file.erl @@ -397,25 +397,36 @@ write_file(Name, Bin) -> Modes :: [mode()], Reason :: posix() | badarg | terminated | system_limit. -write_file(Name, Bin, ModeList) when is_list(ModeList) -> - case make_binary(Bin) of - B when is_binary(B) -> - case open(Name, [binary, write | - lists:delete(binary, - lists:delete(write, ModeList))]) of - {ok, Handle} -> - case write(Handle, B) of - ok -> - close(Handle); - E1 -> - _ = close(Handle), - E1 - end; - E2 -> - E2 - end; - E3 -> - E3 +write_file(Name, IOData, ModeList) when is_list(ModeList) -> + case lists:member(raw, ModeList) of + true -> + %% For backwards compatibility of error messages + try iolist_size(IOData) of + _Size -> do_write_file(Name, IOData, ModeList) + catch + error:Error -> {error, Error} + end; + false -> + case make_binary(IOData) of + Bin when is_binary(Bin) -> + do_write_file(Name, Bin, ModeList); + Error -> + Error + end + end. + +do_write_file(Name, IOData, ModeList) -> + case open(Name, [binary, write | ModeList]) of + {ok, Handle} -> + case write(Handle, IOData) of + ok -> + close(Handle); + E1 -> + _ = close(Handle), + E1 + end; + E2 -> + E2 end. %% Obsolete, undocumented, local node only, don't use!. |