Skip to content

Commit

Permalink
Dereference symbolic links when copying files from ct data folder
Browse files Browse the repository at this point in the history
When running `rebar3 ct`, `rebar_prv_common_test` copies the data folders
to a different location. If `{name}_SUITE_data` folder contains relative
symbolic links, the symbolic links might stop working since the path
might be different.

A solution is to add the `-L` option to `cp` so it dereferences the
symbolic links and copies the original content in the target file.

This is only done for the data folder but and won't change any code that
previously used `rebar_file_utils:cp_r`.
  • Loading branch information
gbgambitresearch committed Jul 21, 2022
1 parent a2ea0c5 commit 07c8a6a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions apps/rebar/src/rebar_file_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
symlink_or_copy/2,
rm_rf/1,
cp_r/2,
cp_r/3,
mv/2,
delete_each/1,
write_file_if_contents_differ/2,
Expand Down Expand Up @@ -207,9 +208,16 @@ rm_rf(Target) ->
end.

-spec cp_r(list(string()), file:filename()) -> 'ok'.
cp_r([], _Dest) ->
ok;
cp_r(Sources, Dest) ->
cp_r(Sources, Dest, false).

%% @doc Copies files and directories.
%% If Dereference is true, it follows the Sources symbolic link and copies
%% the content
-spec cp_r(list(string()), file:filename(), boolean()) -> 'ok'.
cp_r([], _Dest, _Dereference) ->
ok;
cp_r(Sources, Dest, Dereference) ->
case os:type() of
{unix, Os} ->
EscSources = [rebar_utils:escape_chars(Src) || Src <- Sources],
Expand All @@ -229,8 +237,14 @@ cp_r(Sources, Dest) ->
{ok, []} = rebar_utils:sh(?FMT("mkdir -p ~ts",
[rebar_utils:escape_chars(Dest)]),
[{use_stdout, false}, abort_on_error]),
{ok, []} = rebar_utils:sh(?FMT("cp -Rp ~ts \"~ts\"",
[Source, rebar_utils:escape_double_quotes(Dest)]),

Opt = case Dereference of
true -> "-RpL";
false -> "-Rp"
end,

{ok, []} = rebar_utils:sh(?FMT("cp ~s ~ts \"~ts\"",
[Opt, Source, rebar_utils:escape_double_quotes(Dest)]),
[{use_stdout, true}, abort_on_error]),
ok;
{win32, _} ->
Expand Down
2 changes: 1 addition & 1 deletion apps/rebar/src/rebar_prv_common_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ copy_bare_suites(From, To) ->
DataDirs = lists:filter(fun filelib:is_dir/1,
filelib:wildcard(filename:join([From, "*_SUITE_data"]))),
ok = rebar_file_utils:cp_r(SrcFiles, To),
rebar_file_utils:cp_r(DataDirs, To).
rebar_file_utils:cp_r(DataDirs, To, true).

maybe_copy_spec(State, [App|Apps], Spec) ->
case rebar_file_utils:path_from_ancestor(filename:dirname(Spec), rebar_app_info:dir(App)) of
Expand Down

0 comments on commit 07c8a6a

Please sign in to comment.