Merge pull request #368 from eproxus/handle-more-characters-in-paths

Escape more characters in path (fix #367)
This commit is contained in:
Fred Hebert 2014-10-14 08:11:20 -04:00
commit 9c7dae7e25
2 changed files with 61 additions and 57 deletions

View file

@ -44,7 +44,7 @@
rm_rf(Target) -> rm_rf(Target) ->
case os:type() of case os:type() of
{unix, _} -> {unix, _} ->
EscTarget = escape_spaces(Target), EscTarget = escape_path(Target),
{ok, []} = rebar_utils:sh(?FMT("rm -rf ~s", [EscTarget]), {ok, []} = rebar_utils:sh(?FMT("rm -rf ~s", [EscTarget]),
[{use_stdout, false}, abort_on_error]), [{use_stdout, false}, abort_on_error]),
ok; ok;
@ -63,7 +63,7 @@ cp_r([], _Dest) ->
cp_r(Sources, Dest) -> cp_r(Sources, Dest) ->
case os:type() of case os:type() of
{unix, _} -> {unix, _} ->
EscSources = [escape_spaces(Src) || Src <- Sources], EscSources = [escape_path(Src) || Src <- Sources],
SourceStr = string:join(EscSources, " "), SourceStr = string:join(EscSources, " "),
{ok, []} = rebar_utils:sh(?FMT("cp -R ~s \"~s\"", {ok, []} = rebar_utils:sh(?FMT("cp -R ~s \"~s\"",
[SourceStr, Dest]), [SourceStr, Dest]),
@ -78,8 +78,8 @@ cp_r(Sources, Dest) ->
mv(Source, Dest) -> mv(Source, Dest) ->
case os:type() of case os:type() of
{unix, _} -> {unix, _} ->
EscSource = escape_spaces(Source), EscSource = escape_path(Source),
EscDest = escape_spaces(Dest), EscDest = escape_path(Dest),
{ok, []} = rebar_utils:sh(?FMT("mv ~s ~s", [EscSource, EscDest]), {ok, []} = rebar_utils:sh(?FMT("mv ~s ~s", [EscSource, EscDest]),
[{use_stdout, false}, abort_on_error]), [{use_stdout, false}, abort_on_error]),
ok; ok;
@ -190,5 +190,5 @@ cp_r_win32(Source,Dest) ->
end, filelib:wildcard(Source)), end, filelib:wildcard(Source)),
ok. ok.
escape_spaces(Str) -> escape_path(Str) ->
re:replace(Str, " ", "\\\\ ", [global, {return, list}]). re:replace(Str, "([ ()?])", "\\\\&", [global, {return, list}]).

View file

@ -35,9 +35,14 @@
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
-define(TMP_DIR, "tmp_file_utils"). -define(TMP_DIR, "tmp_file_utils").
-define(DIR_TREE, [{d,"source",[{f,"file1"},
{f,"file2"}]}, -define(SRC, "source dir?").
{d,"dest",[]}]). -define(DST, "dest (dir)").
-define(FILE1, "file 1").
-define(FILE2, "file(2)").
-define(DIR_TREE, [{d,?SRC,[{f,?FILE1},
{f,?FILE2}]},
{d,?DST,[]}]).
-define(FILE_CONTENT, <<"1234567890">>). -define(FILE_CONTENT, <<"1234567890">>).
%% ==================================================================== %% ====================================================================
@ -57,7 +62,7 @@ delete_each_test_() ->
rebar_file_utils:delete_each(file_list()) rebar_file_utils:delete_each(file_list())
end, end,
fun teardown/1, fun teardown/1,
[assert_files_not_in("source", file_list())]}. [assert_files_not_in(?SRC, file_list())]}.
%% ==================================================================== %% ====================================================================
%% rm_rf tests %% rm_rf tests
@ -68,20 +73,20 @@ rm_rf_wildcard_test_() ->
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source","file*"])) rebar_file_utils:rm_rf(filename:join([?TMP_DIR,?SRC,"file*"]))
end, end,
fun teardown/1, fun teardown/1,
[assert_files_not_in("source", file_list())]}. [assert_files_not_in(?SRC, file_list())]}.
rm_rf_dir_test_() -> rm_rf_dir_test_() ->
{"rm_rf removes directory tree", {"rm_rf removes directory tree",
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:rm_rf(filename:join([?TMP_DIR,"source"])) rebar_file_utils:rm_rf(filename:join([?TMP_DIR,?SRC]))
end, end,
fun teardown/1, fun teardown/1,
[?_assertNot(filelib:is_dir(filename:join([?TMP_DIR,"source"])))]}. [?_assertNot(filelib:is_dir(filename:join([?TMP_DIR,?SRC])))]}.
%% ==================================================================== %% ====================================================================
%% cp_r tests %% cp_r tests
@ -92,47 +97,47 @@ cp_r_file_to_file_test_() ->
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,?SRC,?FILE1])],
filename:join([?TMP_DIR,"dest","new_file"])) filename:join([?TMP_DIR,?DST,"new_file"]))
end, end,
fun teardown/1, fun teardown/1,
[?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","new_file"])))]}. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,?DST,"new_file"])))]}.
cp_r_file_to_dir_test_() -> cp_r_file_to_dir_test_() ->
{"cp_r copies a file to directory", {"cp_r copies a file to directory",
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,?SRC,?FILE1])],
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","file1"])))]}. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,?DST,?FILE1])))]}.
cp_r_dir_to_dir_test_() -> cp_r_dir_to_dir_test_() ->
{"cp_r copies a directory to directory", {"cp_r copies a directory to directory",
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,?SRC])],
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))), [?_assert(filelib:is_dir(filename:join([?TMP_DIR,?DST,?SRC]))),
assert_files_in("dest/source", assert_files_in("dest/source",
[filename:join([?TMP_DIR,"dest","source",F]) || [filename:join([?TMP_DIR,?DST,?SRC,F]) ||
F <- ["file1","file2"]])]}. F <- [?FILE1,?FILE2]])]}.
cp_r_wildcard_file_to_dir_test_() -> cp_r_wildcard_file_to_dir_test_() ->
{"cp_r copies wildcard files to directory", {"cp_r copies wildcard files to directory",
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","*1"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,?SRC,"*1"])],
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","file1"])))]}. [?_assert(filelib:is_regular(filename:join([?TMP_DIR,?DST,?FILE1])))]}.
cp_r_wildcard_dir_to_dir_test_() -> cp_r_wildcard_dir_to_dir_test_() ->
{"cp_r copies wildcard directory to directory", {"cp_r copies wildcard directory to directory",
@ -140,45 +145,45 @@ cp_r_wildcard_dir_to_dir_test_() ->
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"sour*"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,"sour*"])],
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assert(filelib:is_dir(filename:join([?TMP_DIR,"dest","source"]))), [?_assert(filelib:is_dir(filename:join([?TMP_DIR,?DST,?SRC]))),
assert_files_in("dest/source", assert_files_in("dest/source",
[filename:join([?TMP_DIR,"dest","source",F]) || [filename:join([?TMP_DIR,?DST,?SRC,F]) ||
F <- ["file1","file2"]])]}. F <- [?FILE1,?FILE2]])]}.
cp_r_overwrite_file_test_() -> cp_r_overwrite_file_test_() ->
{"cp_r overwrites destination file", {"cp_r overwrites destination file",
setup, setup,
fun() -> fun() ->
setup(), setup(),
ok = file:write_file(filename:join([?TMP_DIR,"dest","file1"]), ok = file:write_file(filename:join([?TMP_DIR,?DST,?FILE1]),
<<"test">>), <<"test">>),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source","file1"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,?SRC,?FILE1])],
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assertMatch({ok,?FILE_CONTENT}, [?_assertMatch({ok,?FILE_CONTENT},
file:read_file( file:read_file(
filename:join([?TMP_DIR,"dest","file1"])))]}. filename:join([?TMP_DIR,?DST,?FILE1])))]}.
cp_r_overwrite_dir_test_() -> cp_r_overwrite_dir_test_() ->
{"cp_r overwrites destination file (xcopy case on win32)", {"cp_r overwrites destination file (xcopy case on win32)",
setup, setup,
fun() -> fun() ->
setup(), setup(),
ok = file:make_dir(filename:join([?TMP_DIR,"dest","source"])), ok = file:make_dir(filename:join([?TMP_DIR,?DST,?SRC])),
ok = file:write_file( ok = file:write_file(
filename:join([?TMP_DIR,"dest","source","file1"]), filename:join([?TMP_DIR,?DST,?SRC,?FILE1]),
<<"test">>), <<"test">>),
rebar_file_utils:cp_r([filename:join([?TMP_DIR,"source"])], rebar_file_utils:cp_r([filename:join([?TMP_DIR,?SRC])],
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assertMatch({ok,?FILE_CONTENT}, [?_assertMatch({ok,?FILE_CONTENT},
file:read_file( file:read_file(
filename:join([?TMP_DIR,"dest","source","file1"])))]}. filename:join([?TMP_DIR,?DST,?SRC,?FILE1])))]}.
cp_r_overwrite_file_fail_test_() -> cp_r_overwrite_file_fail_test_() ->
{"cp_r fails to fs permission errors (file:copy/2 case on win32)", {"cp_r fails to fs permission errors (file:copy/2 case on win32)",
@ -186,15 +191,15 @@ cp_r_overwrite_file_fail_test_() ->
fun() -> fun() ->
setup(), setup(),
ok = file:write_file( ok = file:write_file(
filename:join([?TMP_DIR,"dest","file1"]),<<"test">>), filename:join([?TMP_DIR,?DST,?FILE1]),<<"test">>),
ok = file:change_mode( ok = file:change_mode(
filename:join([?TMP_DIR,"dest","file1"]),0) filename:join([?TMP_DIR,?DST,?FILE1]),0)
end, end,
fun teardown/1, fun teardown/1,
[?_assertThrow(rebar_abort, [?_assertThrow(rebar_abort,
rebar_file_utils:cp_r( rebar_file_utils:cp_r(
[filename:join([?TMP_DIR,"source","file1"])], [filename:join([?TMP_DIR,?SRC,?FILE1])],
filename:join([?TMP_DIR,"dest"])))]}. filename:join([?TMP_DIR,?DST])))]}.
cp_r_overwrite_dir_fail_test_() -> cp_r_overwrite_dir_fail_test_() ->
{"cp_r fails to fs permission error (xcopy case on win32)", {"cp_r fails to fs permission error (xcopy case on win32)",
@ -202,50 +207,49 @@ cp_r_overwrite_dir_fail_test_() ->
fun() -> fun() ->
setup(), setup(),
ok = file:make_dir( ok = file:make_dir(
filename:join([?TMP_DIR,"dest","source"])), filename:join([?TMP_DIR,?DST,?SRC])),
ok = file:write_file( ok = file:write_file(
filename:join([?TMP_DIR,"dest","source","file1"]), filename:join([?TMP_DIR,?DST,?SRC,?FILE1]),
<<"test">>), <<"test">>),
ok = file:change_mode( ok = file:change_mode(
filename:join([?TMP_DIR,"dest","source","file1"]),0) filename:join([?TMP_DIR,?DST,?SRC,?FILE1]),0)
end, end,
fun teardown/1, fun teardown/1,
[?_assertThrow(rebar_abort, [?_assertThrow(rebar_abort,
rebar_file_utils:cp_r( rebar_file_utils:cp_r(
[filename:join([?TMP_DIR,"source"])], [filename:join([?TMP_DIR,?SRC])],
filename:join([?TMP_DIR,"dest"])))]}. filename:join([?TMP_DIR,?DST])))]}.
mv_file_test_() -> mv_file_test_() ->
{"move a file to folder", {"move a file to folder",
setup, setup,
fun() -> fun() ->
setup(), setup(),
rebar_file_utils:mv(filename:join([?TMP_DIR,"source","file1"]), rebar_file_utils:mv(filename:join([?TMP_DIR,?SRC,?FILE1]),
filename:join([?TMP_DIR,"dest"])) filename:join([?TMP_DIR,?DST]))
end, end,
fun teardown/1, fun teardown/1,
[?_assert(filelib:is_regular(filename:join([?TMP_DIR,"dest","file1"]))), [?_assert(filelib:is_regular(filename:join([?TMP_DIR,?DST,?FILE1]))),
?_assertNot(filelib:is_regular( ?_assertNot(filelib:is_regular(
filename:join([?TMP_DIR,"source","file1"])))]}. filename:join([?TMP_DIR,?SRC,?FILE1])))]}.
%% ==================================================================== %% ====================================================================
%% Utilities %% Utilities
%% ==================================================================== %% ====================================================================
file_list() -> file_list() ->
[filename:join([?TMP_DIR,"source",F]) || F <- ["file1","file2"]]. [filename:join([?TMP_DIR,?SRC,F]) || F <- [?FILE1,?FILE2]].
%% ==================================================================== %% ====================================================================
%% Setup and Teardown %% Setup and Teardown
%% ==================================================================== %% ====================================================================
setup() -> setup() ->
file:make_dir(?TMP_DIR),
make_dir_tree(?TMP_DIR,?DIR_TREE). make_dir_tree(?TMP_DIR,?DIR_TREE).
make_dir_tree(Parent, [{d,Dir,Contents} | Rest]) -> make_dir_tree(Parent, [{d,Dir,Contents} | Rest]) ->
NewDir = filename:join(Parent,Dir), NewDir = filename:join(Parent,Dir),
ok = file:make_dir(NewDir), ok = filelib:ensure_dir(filename:join(NewDir, "tmp")),
ok = make_dir_tree(NewDir,Contents), ok = make_dir_tree(NewDir,Contents),
ok = make_dir_tree(Parent,Rest); ok = make_dir_tree(Parent,Rest);
make_dir_tree(Parent, [{f,File} | Rest]) -> make_dir_tree(Parent, [{f,File} | Rest]) ->