Skip to content

Commit

Permalink
(erlang#1743): store the etags in cache files
Browse files Browse the repository at this point in the history
  • Loading branch information
Laszlo Toth authored and Laszlo Toth committed Apr 10, 2018
1 parent cb743f7 commit 22a4bce
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/rebar_pkg_resource.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ download(TmpDir, Pkg={pkg, Name, Vsn, _Hash}, State) ->
CachePath = filename:join(PackageDir, Package),
case rebar_utils:url_append_path(CDN, filename:join(?REMOTE_PACKAGE_DIR, Package)) of
{ok, Url} ->
cached_download(TmpDir, CachePath, Pkg, Url, etag(CachePath), State);
cached_download(TmpDir, CachePath, Pkg, Url, etag(CachePath), State, CachePath);
_ ->
{fetch_fail, Name, Vsn}
end.

cached_download(TmpDir, CachePath, Pkg={pkg, Name, Vsn, _Hash}, Url, ETag, State) ->
cached_download(TmpDir, CachePath, Pkg={pkg, Name, Vsn, _Hash}, Url, ETag, State, CachePath) ->
case request(Url, ETag) of
{ok, cached} ->
?INFO("Version cached at ~ts is up to date, reusing it", [CachePath]),
serve_from_cache(TmpDir, CachePath, Pkg, State);
{ok, Body, NewETag} ->
?INFO("Downloaded package, caching at ~ts", [CachePath]),
store_etag_in_cache(CachePath, NewETag),
serve_from_download(TmpDir, CachePath, Pkg, NewETag, Body, State);
error when ETag =/= false ->
store_etag_in_cache(CachePath, ETag),
?INFO("Download error, using cached file at ~ts", [CachePath]),
serve_from_cache(TmpDir, CachePath, Pkg, State);
error ->
Expand Down Expand Up @@ -130,10 +132,9 @@ request(Url, ETag) ->
end.

etag(Path) ->
case file:read_file(Path) of
{ok, Binary} ->
<<X:128/big-unsigned-integer>> = crypto:hash(md5, Binary),
rebar_string:lowercase(lists:flatten(io_lib:format("~32.16.0b", [X])));
case file:read_file(etag_path(Path)) of
{ok, Bin} ->
binary_to_list(Bin);
{error, _} ->
false
end.
Expand Down Expand Up @@ -216,3 +217,9 @@ version_pad([Major, Minor, Patch]) ->
{list_to_integer(Major), list_to_integer(Minor), list_to_integer(Patch)};
version_pad([Major, Minor, Patch | _]) ->
{list_to_integer(Major), list_to_integer(Minor), list_to_integer(Patch)}.

store_etag_in_cache(Path, ETag) ->
ok = file:write_file(etag_path(Path), ETag).

etag_path(Path) ->
string:join([Path, ".etag"], []).

0 comments on commit 22a4bce

Please sign in to comment.