Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return size when package exceeds max size #103

Merged
merged 1 commit into from Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/hex_tarball.erl
Expand Up @@ -65,11 +65,14 @@ create(Metadata, Files, Config) ->

UncompressedSize = byte_size(ContentsTarball),

case(byte_size(Tarball) > TarballMaxSize) or (UncompressedSize > TarballMaxUncompressedSize) of
true ->
{error, {tarball, too_big}};
case {(byte_size(Tarball) > TarballMaxSize), (UncompressedSize > TarballMaxUncompressedSize)} of
{_, true} ->
{error, {tarball, {too_big_uncompressed, TarballMaxUncompressedSize}}};

false ->
{true, _} ->
{error, {tarball, {too_big_compressed, TarballMaxSize}}};

{false, false} ->
{ok, #{tarball => Tarball, outer_checksum => OuterChecksum, inner_checksum => InnerChecksum}}
end.

Expand Down Expand Up @@ -197,7 +200,11 @@ format_checksum(Checksum) ->
%% Converts an error reason term to a human-readable error message string.
-spec format_error(term()) -> string().
format_error({tarball, empty}) -> "empty tarball";
format_error({tarball, too_big}) -> "tarball is too big";
format_error({tarball, {too_big_uncompressed, Size}}) ->
io_lib:format("package exceeds max uncompressed size ~w ~s", [format_byte_size(Size), "MB"]);
format_error({tarball, {too_big_compressed, Size}}) ->
io_lib:format("package exceeds max compressed size ~w ~s", [format_byte_size(Size), "MB"]);

format_error({tarball, {missing_files, Files}}) -> io_lib:format("missing files: ~p", [Files]);
format_error({tarball, {bad_version, Vsn}}) -> io_lib:format("unsupported version: ~p", [Vsn]);
format_error({tarball, invalid_checksum}) -> "invalid tarball checksum";
Expand All @@ -214,6 +221,9 @@ format_error({checksum_mismatch, ExpectedChecksum, ActualChecksum}) ->
"Actual (base16-encoded): ~s",
[encode_base16(ExpectedChecksum), encode_base16(ActualChecksum)]).

format_byte_size(Size) ->
Size / 1000000.

%%====================================================================
%% Internal functions
%%====================================================================
Expand Down
4 changes: 3 additions & 1 deletion test/hex_tarball_SUITE.erl
Expand Up @@ -23,7 +23,9 @@ too_big_to_create_test(_Config) ->
},
Contents = [{"src/foo.erl", <<"-module(foo).">>}],
Config = maps:put(tarball_max_size, 5100, hex_core:default_config()),
{error, {tarball, too_big}} = hex_tarball:create(Metadata, Contents, Config),
{error, {tarball, {too_big_compressed, 5100}}} = hex_tarball:create(Metadata, Contents, Config),
Config1 = maps:put(tarball_max_uncompressed_size, 100, hex_core:default_config()),
{error, {tarball,{too_big_uncompressed, 100}}} = hex_tarball:create(Metadata, Contents, Config1),
ok.

too_big_to_unpack_test(_Config) ->
Expand Down