Skip to content

Commit 929fe3f

Browse files
committed
fix: removes leading / from link target
1 parent 9b328f8 commit 929fe3f

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

src/hb_store.erl

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,36 +311,40 @@ sync(FromStore, ToStore) ->
311311
FromStoreOpts = maps:put(<<"resolve">>, false, FromStore),
312312
{ok, Entries} = hb_store:list(FromStore, <<"/">>),
313313
ValidEntries = lists:filter(fun(Key) -> Key =/= <<"lmdb">> end, Entries),
314-
case sync_entries(ValidEntries, <<"">>, FromStoreOpts, ToStore) of
314+
case sync_entries(ValidEntries, <<"/">>, FromStoreOpts, ToStore) of
315315
[] -> ok;
316316
FailedKeyValues -> {error, {sync_failed, FailedKeyValues}}
317317
end.
318318

319319
sync_entries(Entries, ParentDir, FromStore, ToStore) ->
320320
?event({sync_entries, ParentDir, Entries}),
321321
lists:foldl(fun(Key, Acc) ->
322-
Path = <<ParentDir/binary, "/", Key/binary>>,
323-
case type(FromStore, Path) of
322+
NewPath =
323+
case ParentDir of
324+
Bin when Bin == <<"">> orelse Bin == <<"/">> -> Key;
325+
_ -> <<ParentDir/binary, "/", Key/binary>>
326+
end,
327+
case type(FromStore, NewPath) of
324328
Type when Type == simple orelse Type == link ->
325-
case hb_store:read(FromStore, Path) of
329+
case hb_store:read(FromStore, NewPath) of
326330
{ok, Value} when Type == simple ->
327-
case hb_store:write(ToStore, Path, Value) of
331+
case hb_store:write(ToStore, NewPath, Value) of
328332
ok -> Acc;
329-
_Error -> [{Path, Value} | Acc]
333+
_Error -> [{NewPath, Value} | Acc]
330334
end;
331335
{ok, LinkTarget} when Type == link ->
332-
ok = hb_store:make_link(ToStore, LinkTarget, Path),
336+
ok = hb_store:make_link(ToStore, LinkTarget, NewPath),
333337
Acc;
334338
_Error ->
335-
[{Path, undefined} | Acc]
339+
[{NewPath, undefined} | Acc]
336340
end;
337341
composite ->
338-
case hb_store:make_group(ToStore, Path) of
342+
case hb_store:make_group(ToStore, NewPath) of
339343
ok ->
340-
{ok, Entries2} = hb_store:list(FromStore, Path),
341-
Acc ++ sync_entries(Entries2, Path, FromStore, ToStore);
344+
{ok, Entries2} = hb_store:list(FromStore, NewPath),
345+
Acc ++ sync_entries(Entries2, NewPath, FromStore, ToStore);
342346
_Error ->
343-
[{Path, undefined} | Acc]
347+
[{NewPath, undefined} | Acc]
344348
end;
345349
not_found ->
346350
Acc

src/hb_store_fs.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ reset(#{ <<"name">> := DataDir }) ->
4747
%% @doc Read a key from the store, following symlinks as needed.
4848
read(#{<<"resolve">> := false} = Opts, Key) ->
4949
maybe {prefixed_link, LinkTarget} ?= read_path(add_prefix(Opts, Key), false),
50-
{ok, remove_prefix(Opts, LinkTarget)}
50+
case remove_prefix(Opts, LinkTarget) of
51+
<<"/", Path/binary>> -> {ok, Path};
52+
Path -> {ok, Path}
53+
end
5154
end;
5255
read(Opts, Key) ->
5356
read_path(add_prefix(Opts, resolve(Opts, Key)), true).

src/hb_store_lmdb.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -489,9 +489,9 @@ make_link(Opts, Existing, New) when is_list(Existing) ->
489489
ExistingBin = to_path(Existing),
490490
make_link(Opts, ExistingBin, New);
491491
make_link(Opts, Existing, New) ->
492-
% ensure key-value is binary pair
492+
% ensure key-value is binary pair
493493
ExistingBin = hb_util:bin(Existing),
494-
NewBin = maybe <<"/", Path/binary>> ?= hb_util:bin(New), Path end,
494+
NewBin = hb_util:bin(New),
495495
% Ensure parent groups exist for the new link path (like filesystem ensure_dir)
496496
ensure_parent_groups(Opts, NewBin),
497497
write(Opts, NewBin, <<"link:", ExistingBin/binary>>).

0 commit comments

Comments
 (0)