Skip to content

Commit

Permalink
Do not dereference symbolic links in win32 when using cp_r
Browse files Browse the repository at this point in the history
The current behaviour:
- When `Source` is a folder, `robocopy` is used. It follows symbolic
  links and copies the original data, creating a raw file

- When `Source` is a file/symbolic link, `file:copy` is used. It copies
  the raw data creating a raw file.

The change in this commit will use `/sl`  when copying files
from Source folder and `file:make_symlink` when Source is a
symbolic link on Windows.

If the `[{dereference, true}]` option is given, it will dereference the
files and copy the content instead of a symbolic link

This change has been added so the Windows copy behaves more unix one.
  • Loading branch information
gonzalobf committed Jul 25, 2022
1 parent 2ddde5c commit 3993362
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 56 deletions.
62 changes: 45 additions & 17 deletions apps/rebar/src/rebar_file_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ win32_symlink_or_copy(Source, Target) ->
[{use_stdout, false}, return_on_error]),
case win32_mklink_ok(Res, Target) of
true -> ok;
false -> cp_r_win32(Source, drop_last_dir_from_path(Target))
false -> cp_r_win32(Source, drop_last_dir_from_path(Target), [])
end.

%% @private specifically pattern match against the output
Expand Down Expand Up @@ -251,7 +251,7 @@ cp_r(Sources, Dest, Options) ->
[{use_stdout, true}, abort_on_error]),
ok;
{win32, _} ->
lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest,Options) end, Sources),
ok
end.

Expand Down Expand Up @@ -548,9 +548,24 @@ delete_each_dir_win32([Dir | Rest]) ->
[{use_stdout, false}, return_on_error]),
delete_each_dir_win32(Rest).

xcopy_win32(Source,Dest)->
xcopy_win32(Source,Dest, Options)->
%% "xcopy \"~ts\" \"~ts\" /q /y /e 2> nul", Changed to robocopy to
%% handle long names. May have issues with older windows.

CopySubdirectories = "/e",
DontFollow = "/sl",

Opt = [CopySubdirectories],
% By default Windows follows symbolic links except if the "/sl" options is given.
% Add "/sl" for default so it doesn't follow symbolic links and behaves more like unix
OptStr = case proplists:get_value(dereference, Options, false) of
true ->
string:join(Opt, " ");
false ->
% Default option
string:join([DontFollow|Opt], " ")
end,

Cmd = case filelib:is_dir(Source) of
true ->
%% For robocopy, copying /a/b/c/ to /d/e/f/ recursively does not
Expand All @@ -559,14 +574,16 @@ xcopy_win32(Source,Dest)->
%% must manually add the last fragment of a directory to the `Dest`
%% in order to properly replicate POSIX platforms
NewDest = filename:join([Dest, filename:basename(Source)]),
?FMT("robocopy \"~ts\" \"~ts\" /e 1> nul",
?FMT("robocopy \"~ts\" \"~ts\" ~s 1> nul",
[rebar_utils:escape_double_quotes(filename:nativename(Source)),
rebar_utils:escape_double_quotes(filename:nativename(NewDest))]);
rebar_utils:escape_double_quotes(filename:nativename(NewDest)),
OptStr]);
false ->
?FMT("robocopy \"~ts\" \"~ts\" \"~ts\" /e 1> nul",
?FMT("robocopy \"~ts\" \"~ts\" \"~ts\" ~s 1> nul",
[rebar_utils:escape_double_quotes(filename:nativename(filename:dirname(Source))),
rebar_utils:escape_double_quotes(filename:nativename(Dest)),
rebar_utils:escape_double_quotes(filename:basename(Source))])
rebar_utils:escape_double_quotes(filename:basename(Source)),
OptStr])
end,
Res = rebar_utils:sh(Cmd,
[{use_stdout, false}, return_on_error]),
Expand All @@ -578,21 +595,32 @@ xcopy_win32(Source,Dest)->
[Source, Dest]))}
end.

cp_r_win32({true, SourceDir}, {true, DestDir}) ->
cp_r_win32({true, SourceDir}, {true, DestDir}, Options) ->
%% from directory to directory
ok = case file:make_dir(DestDir) of
{error, eexist} -> ok;
Other -> Other
end,
ok = xcopy_win32(SourceDir, DestDir);
cp_r_win32({false, Source} = S,{true, DestDir}) ->
ok = xcopy_win32(SourceDir, DestDir, Options);
cp_r_win32({false, Source} = S,{true, DestDir}, Options) ->
%% from file to directory
cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
cp_r_win32({false, Source},{false, Dest}) ->
cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))}, Options);
cp_r_win32({false, Source},{false, Dest}, Options) ->
%% from file to file
{ok,_} = file:copy(Source, Dest),
case file:read_link(Source) of
{ok, OriginalFile} -> case proplists:get_value(dereference, Options, false) of
true ->
{ok, _} = file:copy(Source, Dest),
ok;
false ->
file:make_symlink(OriginalFile, Dest)
end;
_ ->
{ok, _} = file:copy(Source, Dest),
ok
end,
ok;
cp_r_win32({true, SourceDir}, {false, DestDir}) ->
cp_r_win32({true, SourceDir}, {false, DestDir}, Options) ->
case filelib:is_regular(DestDir) of
true ->
%% From directory to file? This shouldn't happen
Expand All @@ -604,16 +632,16 @@ cp_r_win32({true, SourceDir}, {false, DestDir}) ->
%% So let's attempt to create this directory
case ensure_dir(DestDir) of
ok ->
ok = xcopy_win32(SourceDir, DestDir);
ok = xcopy_win32(SourceDir, DestDir, Options);
{error, Reason} ->
{error, lists:flatten(
io_lib:format("Unable to create dir ~p: ~p\n",
[DestDir, Reason]))}
end
end;
cp_r_win32(Source,Dest) ->
cp_r_win32(Source, Dest, Options) ->
Dst = {filelib:is_dir(Dest), Dest},
lists:foreach(fun(Src) ->
ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst, Options)
end, filelib:wildcard(Source)),
ok.
113 changes: 74 additions & 39 deletions apps/rebar/test/rebar_file_utils_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -390,64 +390,99 @@ cp_r_copies_files(Config) ->
% Checks that the files in src/ are copied to dest/
PrivDir = ?config(priv_dir, Config),
BaseDir = mk_base_dir(PrivDir, cp_r_copies_files),
#{
src_list := SrcList,
dest_dir := DestDir,
dest_file := DestFile,
dest_sym := DestSym,
dest_subdir := DestSubDir
} = create_dir_tree_helper(BaseDir),
rebar_file_utils:cp_r(SrcList, DestDir),
{SrcList, DestDir} = create_dir_tree_helper(BaseDir),

rebar_file_utils:cp_r(SrcList, DestDir, []),

% dest/file
DestFile = filename:join(DestDir, "file"),
?assert(filelib:is_file(DestFile)),
?assertEqual({ok, <<"hello">>}, file:read_file(DestFile)),
% dest/symbolic
DestSym = filename:join(DestDir, "symbolic"),
{ok, #file_info{type = symlink}} = file:read_link_info(DestSym),
?assertEqual({ok, <<"hello">>}, file:read_file(DestSym)),
?assert(filelib:is_dir(DestSubDir)).
% dest/sub_dir
DestSubDir = filename:join(DestDir, "sub_dir"),
?assert(filelib:is_dir(DestSubDir)),
% dest/sub_dir/sub_file
DestSubFile = filename:join(DestSubDir, "sub_file"),
?assert(filelib:is_file(DestSubFile)),
?assertEqual({ok, <<"hello">>}, file:read_file(DestSubFile)),
% dest/sub_dir/sub_symbolic
DestSubSymbolic = filename:join(DestSubDir, "sub_symbolic"),
{ok, #file_info{type = symlink}} = file:read_link_info(DestSubSymbolic),
?assertEqual({ok, <<"hello">>}, file:read_file(DestSubSymbolic)).

cp_r_dereferences_symbolic_links(Config) ->
% Checks that the files in src/ are copied to dest/ AND symbolic file has been dereference
PrivDir = ?config(priv_dir, Config),
BaseDir = mk_base_dir(PrivDir, cp_r_dereferences_symbolic_links),
#{
src_list := SrcList,
dest_dir := DestDir,
dest_file := DestFile,
dest_sym := DestSym,
dest_subdir := DestSubDir
} = create_dir_tree_helper(BaseDir),
{SrcList, DestDir} = create_dir_tree_helper(BaseDir),

rebar_file_utils:cp_r(SrcList, DestDir, [{dereference, true}]),

% dest/file
DestFile = filename:join(DestDir, "file"),
?assert(filelib:is_file(DestFile)),
?assertEqual({ok, <<"hello">>}, file:read_file(DestFile)),
% dest/symbolic
% At this point symbolic is not anymore a symbolic link but a regular file
DestSym = filename:join(DestDir, "symbolic"),
{ok, #file_info{type = regular}} = file:read_link_info(DestSym),
?assertEqual({ok, <<"hello">>}, file:read_file(DestSym)),
?assert(filelib:is_dir(DestSubDir)).
% dest/sub_dir
DestSubDir = filename:join(DestDir, "sub_dir"),
?assert(filelib:is_dir(DestSubDir)),
% dest/sub_dir/sub_file
DestSubFile = filename:join(DestSubDir, "sub_file"),
?assert(filelib:is_file(DestSubFile)),
?assertEqual({ok, <<"hello">>}, file:read_file(DestSubFile)),
% dest/sub_dir/sub_symbolic
% At this point sub_symbolic is not anymore a symbolic link but a regular file
DestSubSymbolic = filename:join(DestSubDir, "sub_symbolic"),
{ok, #file_info{type = regular}} = file:read_link_info(DestSubSymbolic),
?assertEqual({ok, <<"hello">>}, file:read_file(DestSubSymbolic)).

create_dir_tree_helper(BaseDir) ->
% Give a directory path, it creates the tree directory below and returns a map
% with the expected paths in dest/ aftercopying them
% with the expected paths in dest/ aftercopying them.
%
% sub_dir is it needed so try all the win32 copy options
%
% |-- src
% |-- file
% |-- sub_dir
% |-- symbolic -> file
% |-- sub_dir
% |-- sub_file
% |-- sub_symbolic
% |-- dest
SrcDir = filename:join(BaseDir, "src"),
ec_file:mkdir_p(SrcDir),
Dir = [
{folder, ["src"]},
{folder, ["dest"]},
{file, ["src", "file"], <<"hello">>},
{symbolic, ["src", "symbolic"], ["src", "file"]},
{folder, ["src", "sub_dir"]},
{file, ["src", "sub_dir", "sub_file"], <<"hello">>},
{symbolic, ["src", "sub_dir", "sub_symbolic"], ["src", "file"]}
],
ok = create_by_type(Dir, BaseDir),
SrcList = [
filename:join([BaseDir,"src", File])
|| File <- ["file", "symbolic", "sub_dir"]
],
DestDir = filename:join(BaseDir, "dest"),
ec_file:mkdir_p(DestDir),
CreatePaths = fun(Name) ->
{filename:join(SrcDir, Name), filename:join(DestDir, Name)}
end,
{SrcFile, DestFile} = CreatePaths("file"),
file:write_file(SrcFile, <<"hello">>),
{SrcSubDir, DestSubDir} = CreatePaths("sub_dir"),
ec_file:mkdir_p(SrcSubDir),
{SrcSym, DestSym} = CreatePaths("symbolic"),
file:make_symlink(SrcFile, SrcSym),
#{
src_list => [SrcFile, SrcSubDir, SrcSym],
dest_dir => DestDir,
dest_file => DestFile,
dest_sym => DestSym,
dest_subdir => DestSubDir
}.
{SrcList, DestDir}.

create_by_type([], _) ->
ok;
create_by_type([{folder, Path} |Rest], BaseDir) ->
ok = ec_file:mkdir_p(filename:join([BaseDir | Path])),
create_by_type(Rest, BaseDir);
create_by_type([{file, Path, Content} |Rest], BaseDir) ->
ok = file:write_file(filename:join([BaseDir | Path]), Content),
create_by_type(Rest, BaseDir);
create_by_type([{symbolic, Path, TargetFile} |Rest], BaseDir) ->
Target = filename:join([BaseDir | TargetFile]),
Symbolic = filename:join([BaseDir | Path]),
ok = file:make_symlink(Target, Symbolic),
create_by_type(Rest, BaseDir).

0 comments on commit 3993362

Please sign in to comment.