From b2b0a463bd09792bbf015974b8bdcf52bd4ae4df Mon Sep 17 00:00:00 2001 From: Ryan Zezeski Date: Tue, 8 Mar 2011 22:18:54 -0500 Subject: [PATCH] no-range PUT should truncate file If a Range header is _not_ specified in a PUT request then truncate the file so that the operation essentially becomes equal to overwriting a file on a typical filesystem. --- src/luwak_wm_file.erl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/luwak_wm_file.erl b/src/luwak_wm_file.erl index d4bae84..9c33030 100644 --- a/src/luwak_wm_file.erl +++ b/src/luwak_wm_file.erl @@ -505,7 +505,14 @@ accept_doc_body(RD, Ctx=#ctx{key=K, client=C}) -> accept_streambody(RD, #ctx{handle={ok, H}, client=C}) -> Stream = luwak_put_stream:start_link(C, H, 0, 1000), Size = luwak_file:get_default_block_size(), - accept_streambody1(Stream, 0, wrq:stream_req_body(RD, Size)). + {H2, Count} = accept_streambody1(Stream, 0, wrq:stream_req_body(RD, Size)), + H2Len = luwak_file:length(C, H2), + %% truncate will fail if passed a Start >= the length of the file + if Count < H2Len -> + {ok, _} = luwak_io:truncate(C, H2, Count), + true; + true -> true + end. accept_streambody1(Stream, Count0, {Data, Next}) -> Count = Count0+size(Data), @@ -514,7 +521,8 @@ accept_streambody1(Stream, Count0, {Data, Next}) -> accept_streambody1(Stream, Count, Next()); Next =:= done -> luwak_put_stream:close(Stream), - true + {ok, File} = luwak_put_stream:status(Stream, ?TIMEOUT_DEFAULT), + {File, Count} end. %% @spec extract_content_type(reqdata()) ->