diff --git a/src/hex_tarball.erl b/src/hex_tarball.erl index 12de42d..7457b81 100644 --- a/src/hex_tarball.erl +++ b/src/hex_tarball.erl @@ -1,5 +1,5 @@ -module(hex_tarball). --export([create/2, create_docs/1, unpack/2, format_checksum/1, format_error/1]). +-export([create/2, create_docs/1, unpack/2, unpack_docs/2, format_checksum/1, format_error/1]). -ifdef(TEST). -export([do_decode_metadata/1, gzip/1, normalize_requirements/1]). -endif. @@ -134,6 +134,42 @@ unpack(Tarball, Output) -> {error, {tarball, Reason}} end. +%% @doc +%% Unpacks a documentation tarball. +%% +%% Examples: +%% +%% ``` +%% > hex_tarball:unpack_docs(Tarball, memory). +%% {ok,#{checksum => <<...>>, +%% contents => [{"src/foo.erl",<<"-module(foo).">>}], +%% metadata => #{<<"name">> => <<"foo">>, ...}}} +%% +%% > hex_tarball:unpack_docs(Tarball, "path/to/unpack"). +%% {ok,#{checksum => <<...>>, +%% metadata => #{<<"name">> => <<"foo">>, ...}}} +%% ''' +-spec unpack_docs(tarball(), memory) -> + {ok, #{checksum => checksum(), metadata => metadata(), contents => contents()}} | + {error, term()}; + (tarball(), filename()) -> + {ok, #{checksum => checksum(), metadata => metadata()}} | + {error, term()}. +unpack_docs(Tarball, _) when byte_size(Tarball) > ?TARBALL_MAX_SIZE -> + {error, {tarball, too_big}}; + +unpack_docs(Tarball, Output) -> + case hex_erl_tar:extract({binary, Tarball}, [memory]) of + {ok, []} -> + {error, {tarball, empty}}; + + {ok, FileList} -> + do_unpack(maps:from_list(FileList), Output); + + {error, Reason} -> + {error, {tarball, Reason}} + end. + %% @doc %% Returns base16-encoded representation of checksum. -spec format_checksum(checksum()) -> binary().