diff options
-rw-r--r-- | src/rebar_file_utils.erl | 78 |
1 files changed, 45 insertions, 33 deletions
diff --git a/src/rebar_file_utils.erl b/src/rebar_file_utils.erl index 436f2d5..8158312 100644 --- a/src/rebar_file_utils.erl +++ b/src/rebar_file_utils.erl @@ -188,7 +188,16 @@ mv(Source, Dest) -> case filelib:is_dir(Source) of true -> SrcDir = filename:nativename(Source), - DestDir = filename:nativename(Dest), + DestDir = case filelib:is_dir(Dest) of + true -> + %% to simulate unix/posix mv, we have to replicate + %% the same directory movement by moving the whole + %% top-level directory, not just the insides + SrcName = filename:basename(Source), + filename:nativename(filename:join(Dest, SrcName)); + false -> + filename:nativename(Dest) + end, robocopy_dir(SrcDir, DestDir); false -> SrcDir = filename:nativename(filename:dirname(Source)), @@ -208,43 +217,46 @@ mv(Source, Dest) -> %% we do a regular move with robocopy without rename. robocopy_file(SrcDir, DestDir, DestName) ; SrcName =/= DestName-> - %% If we're moving a file and the origin and - %% destination names are different: - %% - mktmp - %% - robocopy source_dir tmp_dir srcname - %% - rename srcname destname (to avoid clobbering) - %% - robocopy tmp_dir dest_dir destname - %% - remove tmp_dir - case ec_file:insecure_mkdtemp() of - {error, _Reason} -> - {error, lists:flatten( - io_lib:format("Failed to move ~s to ~s (tmpdir failed)~n", - [Source, Dest]))}; - TmpPath -> - case robocopy_file(SrcDir, TmpPath, SrcName) of - {error, Reason} -> - {error, Reason}; - ok -> - TmpSrc = filename:join(TmpPath, SrcName), - TmpDst = filename:join(TmpPath, DestName), - case file:rename(TmpSrc, TmpDst) of - {error, _} -> - {error, lists:flatten( - io_lib:format("Failed to move ~s to ~s (via rename)~n", - [Source, Dest]))}; - ok -> - case robocopy_file(TmpPath, DestDir, DestName) of - Err = {error, _} -> Err; - OK -> rm_rf(TmpPath), OK - end - end - end - end + robocopy_mv_and_rename(Source, Dest, SrcDir, SrcName, DestDir, DestName) end end end. +robocopy_mv_and_rename(Source, Dest, SrcDir, SrcName, DestDir, DestName) -> + %% If we're moving a file and the origin and + %% destination names are different: + %% - mktmp + %% - robocopy source_dir tmp_dir srcname + %% - rename srcname destname (to avoid clobbering) + %% - robocopy tmp_dir dest_dir destname + %% - remove tmp_dir + case ec_file:insecure_mkdtemp() of + {error, _Reason} -> + {error, lists:flatten( + io_lib:format("Failed to move ~s to ~s (tmpdir failed)~n", + [Source, Dest]))}; + TmpPath -> + case robocopy_file(SrcDir, TmpPath, SrcName) of + {error, Reason} -> + {error, Reason}; + ok -> + TmpSrc = filename:join(TmpPath, SrcName), + TmpDst = filename:join(TmpPath, DestName), + case file:rename(TmpSrc, TmpDst) of + {error, _} -> + {error, lists:flatten( + io_lib:format("Failed to move ~s to ~s (via rename)~n", + [Source, Dest]))}; + ok -> + case robocopy_file(TmpPath, DestDir, DestName) of + Err = {error, _} -> Err; + OK -> rm_rf(TmpPath), OK + end + end + end + end. + robocopy_file(SrcPath, DestPath, FileName) -> Cmd = ?FMT("robocopy /move /e \"~s\" \"~s\" \"~s\"", [rebar_utils:escape_double_quotes(SrcPath), |