You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

598 regels
24 KiB

15 jaren geleden
15 jaren geleden
15 jaren geleden
9 jaren geleden
12 jaren geleden
15 jaren geleden
15 jaren geleden
12 jaren geleden
15 jaren geleden
6 jaren geleden
6 jaren geleden
7 jaren geleden
15 jaren geleden
12 jaren geleden
15 jaren geleden
15 jaren geleden
14 jaren geleden
14 jaren geleden
14 jaren geleden
  1. %% -*- erlang-indent-level: 4;indent-tabs-mode: nil -*-
  2. %% ex: ts=4 sw=4 et
  3. %% -------------------------------------------------------------------
  4. %%
  5. %% rebar: Erlang Build Tools
  6. %%
  7. %% Copyright (c) 2009 Dave Smith (dizzyd@dizzyd.com)
  8. %%
  9. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  10. %% of this software and associated documentation files (the "Software"), to deal
  11. %% in the Software without restriction, including without limitation the rights
  12. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. %% copies of the Software, and to permit persons to whom the Software is
  14. %% furnished to do so, subject to the following conditions:
  15. %%
  16. %% The above copyright notice and this permission notice shall be included in
  17. %% all copies or substantial portions of the Software.
  18. %%
  19. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. %% THE SOFTWARE.
  26. %% -------------------------------------------------------------------
  27. -module(rebar_file_utils).
  28. -export([try_consult/1,
  29. consult_config/2,
  30. consult_config_terms/2,
  31. format_error/1,
  32. symlink_or_copy/2,
  33. rm_rf/1,
  34. cp_r/2,
  35. mv/2,
  36. delete_each/1,
  37. write_file_if_contents_differ/2,
  38. write_file_if_contents_differ/3,
  39. system_tmpdir/0,
  40. system_tmpdir/1,
  41. reset_dir/1,
  42. touch/1,
  43. path_from_ancestor/2,
  44. canonical_path/1,
  45. absolute_path/1,
  46. normalized_path/1,
  47. normalize_relative_path/1,
  48. resolve_link/1,
  49. split_dirname/1,
  50. ensure_dir/1]).
  51. -include("rebar.hrl").
  52. -include_lib("providers/include/providers.hrl").
  53. -include_lib("kernel/include/file.hrl").
  54. %% ===================================================================
  55. %% Public API
  56. %% ===================================================================
  57. try_consult(File) ->
  58. case file:consult(File) of
  59. {ok, Terms} ->
  60. Terms;
  61. {error, enoent} ->
  62. [];
  63. {error, Reason} ->
  64. throw(?PRV_ERROR({bad_term_file, File, Reason}))
  65. end.
  66. %% @doc Parse a sys.config file and return the configuration terms
  67. %% for all its potentially nested configs.
  68. -spec consult_config(rebar_state:t(), string()) -> [[tuple()]].
  69. consult_config(State, Filename) ->
  70. Fullpath = filename:join(rebar_dir:root_dir(State), Filename),
  71. ?DEBUG("Loading configuration from ~p", [Fullpath]),
  72. Config = case try_consult(Fullpath) of
  73. [T] -> T;
  74. [] -> []
  75. end,
  76. consult_config_terms(State, Config).
  77. %% @doc From a parsed sys.config file, expand all the terms to include
  78. %% its potential nested configs. It is also possible that no sub-terms
  79. %% (i.e. the config file does not refer to "some/other/file.config")
  80. %% that the input term is returned as-is.
  81. %%
  82. %% This function is added mostly to help with variable substitution
  83. %% and evaluation of 'sys.config.src' files, giving a way to handle
  84. %% expansion that is separate from regular config handling.
  85. -spec consult_config_terms(rebar_state:t(), [tuple()]) -> [[tuple()]].
  86. consult_config_terms(State, Config) ->
  87. JoinedConfig = lists:flatmap(
  88. fun (SubConfig) when is_list(SubConfig) ->
  89. case lists:suffix(".config", SubConfig) of
  90. %% since consult_config returns a list in a list we take the head here
  91. false -> hd(consult_config(State, SubConfig ++ ".config"));
  92. true -> hd(consult_config(State, SubConfig))
  93. end;
  94. (Entry) -> [Entry]
  95. end, Config),
  96. %% Backwards compatibility
  97. [JoinedConfig].
  98. format_error({bad_term_file, AppFile, Reason}) ->
  99. io_lib:format("Error reading file ~ts: ~ts", [AppFile, file:format_error(Reason)]).
  100. symlink_or_copy(Source, Target) ->
  101. Link = case os:type() of
  102. {win32, _} ->
  103. Source;
  104. _ ->
  105. rebar_dir:make_relative_path(Source, Target)
  106. end,
  107. case file:make_symlink(Link, Target) of
  108. ok ->
  109. ok;
  110. {error, eexist} ->
  111. exists;
  112. {error, _} ->
  113. case os:type() of
  114. {win32, _} ->
  115. S = unicode:characters_to_list(Source),
  116. T = unicode:characters_to_list(Target),
  117. case filelib:is_dir(S) of
  118. true ->
  119. win32_symlink_or_copy(S, T);
  120. false ->
  121. cp_r([S], T)
  122. end;
  123. _ ->
  124. case filelib:is_dir(Target) of
  125. true ->
  126. ok;
  127. false ->
  128. cp_r([Source], Target)
  129. end
  130. end
  131. end.
  132. %% @private Compatibility function for windows
  133. win32_symlink_or_copy(Source, Target) ->
  134. Res = rebar_utils:sh(
  135. ?FMT("cmd /c mklink /j \"~ts\" \"~ts\"",
  136. [rebar_utils:escape_double_quotes(filename:nativename(Target)),
  137. rebar_utils:escape_double_quotes(filename:nativename(Source))]),
  138. [{use_stdout, false}, return_on_error]),
  139. case win32_mklink_ok(Res, Target) of
  140. true -> ok;
  141. false -> cp_r_win32(Source, drop_last_dir_from_path(Target))
  142. end.
  143. %% @private specifically pattern match against the output
  144. %% of the windows 'mklink' shell call; different values from
  145. %% what win32_ok/1 handles
  146. win32_mklink_ok({ok, _}, _) ->
  147. true;
  148. win32_mklink_ok({error,{1,"Local NTFS volumes are required to complete the operation.\n"}}, _) ->
  149. false;
  150. win32_mklink_ok({error,{1,"Cannot create a file when that file already exists.\n"}}, Target) ->
  151. % File or dir is already in place; find if it is already a symlink (true) or
  152. % if it is a directory (copy-required; false)
  153. is_symlink(Target);
  154. win32_mklink_ok(_, _) ->
  155. false.
  156. %% @private
  157. is_symlink(Filename) ->
  158. {ok, Info} = file:read_link_info(Filename),
  159. Info#file_info.type == symlink.
  160. %% @private
  161. %% drops the last 'node' of the filename, presumably the last dir such as 'src'
  162. %% this is because cp_r_win32/2 automatically adds the dir name, to appease
  163. %% robocopy and be more uniform with POSIX
  164. drop_last_dir_from_path([]) ->
  165. [];
  166. drop_last_dir_from_path(Path) ->
  167. case lists:droplast(filename:split(Path)) of
  168. [] -> [];
  169. Dirs -> filename:join(Dirs)
  170. end.
  171. %% @doc Remove files and directories.
  172. %% Target is a single filename, directoryname or wildcard expression.
  173. -spec rm_rf(string()) -> 'ok'.
  174. rm_rf(Target) ->
  175. case os:type() of
  176. {unix, _} ->
  177. EscTarget = rebar_utils:escape_chars(Target),
  178. {ok, []} = rebar_utils:sh(?FMT("rm -rf ~ts", [EscTarget]),
  179. [{use_stdout, false}, abort_on_error]),
  180. ok;
  181. {win32, _} ->
  182. Filelist = filelib:wildcard(Target),
  183. Dirs = [F || F <- Filelist, filelib:is_dir(F)],
  184. Files = Filelist -- Dirs,
  185. ok = delete_each(Files),
  186. ok = delete_each_dir_win32(Dirs),
  187. ok
  188. end.
  189. -spec cp_r(list(string()), file:filename()) -> 'ok'.
  190. cp_r([], _Dest) ->
  191. ok;
  192. cp_r(Sources, Dest) ->
  193. case os:type() of
  194. {unix, Os} ->
  195. EscSources = [rebar_utils:escape_chars(Src) || Src <- Sources],
  196. SourceStr = rebar_string:join(EscSources, " "),
  197. % On darwin the following cp command will cp everything inside
  198. % target vs target and everything inside, so we chop the last char
  199. % off if it is a '/'
  200. Source = case {Os == darwin, lists:last(SourceStr) == $/} of
  201. {true, true} ->
  202. rebar_string:trim(SourceStr, trailing, "/");
  203. {true, false} ->
  204. SourceStr;
  205. {false, _} ->
  206. SourceStr
  207. end,
  208. % ensure destination exists before copying files into it
  209. {ok, []} = rebar_utils:sh(?FMT("mkdir -p ~ts",
  210. [rebar_utils:escape_chars(Dest)]),
  211. [{use_stdout, false}, abort_on_error]),
  212. {ok, []} = rebar_utils:sh(?FMT("cp -Rp ~ts \"~ts\"",
  213. [Source, rebar_utils:escape_double_quotes(Dest)]),
  214. [{use_stdout, true}, abort_on_error]),
  215. ok;
  216. {win32, _} ->
  217. lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
  218. ok
  219. end.
  220. -spec mv(string(), file:filename()) -> 'ok'.
  221. mv(Source, Dest) ->
  222. case os:type() of
  223. {unix, _} ->
  224. EscSource = rebar_utils:escape_chars(Source),
  225. EscDest = rebar_utils:escape_chars(Dest),
  226. case rebar_utils:sh(?FMT("mv ~ts ~ts", [EscSource, EscDest]),
  227. [{use_stdout, false}, abort_on_error]) of
  228. {ok, []} ->
  229. ok;
  230. {ok, Warning} ->
  231. ?WARN("mv: ~p", [Warning]),
  232. ok
  233. end;
  234. {win32, _} ->
  235. case filelib:is_dir(Source) of
  236. true ->
  237. SrcDir = filename:nativename(Source),
  238. DestDir = case filelib:is_dir(Dest) of
  239. true ->
  240. %% to simulate unix/posix mv, we have to replicate
  241. %% the same directory movement by moving the whole
  242. %% top-level directory, not just the insides
  243. SrcName = filename:basename(Source),
  244. filename:nativename(filename:join(Dest, SrcName));
  245. false ->
  246. filename:nativename(Dest)
  247. end,
  248. robocopy_dir(SrcDir, DestDir);
  249. false ->
  250. SrcDir = filename:nativename(filename:dirname(Source)),
  251. SrcName = filename:basename(Source),
  252. DestDir = filename:nativename(filename:dirname(Dest)),
  253. DestName = filename:basename(Dest),
  254. IsDestDir = filelib:is_dir(Dest),
  255. if IsDestDir ->
  256. %% if basename and target name are different because
  257. %% we move to a directory, then just move there.
  258. %% Similarly, if they are the same but we're going to
  259. %% a directory, let's just do that directly.
  260. FullDestDir = filename:nativename(Dest),
  261. robocopy_file(SrcDir, FullDestDir, SrcName)
  262. ; SrcName =:= DestName ->
  263. %% if basename and target name are the same and both are files,
  264. %% we do a regular move with robocopy without rename.
  265. robocopy_file(SrcDir, DestDir, DestName)
  266. ; SrcName =/= DestName->
  267. robocopy_mv_and_rename(Source, Dest, SrcDir, SrcName, DestDir, DestName)
  268. end
  269. end
  270. end.
  271. robocopy_mv_and_rename(Source, Dest, SrcDir, SrcName, DestDir, DestName) ->
  272. %% If we're moving a file and the origin and
  273. %% destination names are different:
  274. %% - mktmp
  275. %% - robocopy source_dir tmp_dir srcname
  276. %% - rename srcname destname (to avoid clobbering)
  277. %% - robocopy tmp_dir dest_dir destname
  278. %% - remove tmp_dir
  279. case ec_file:insecure_mkdtemp() of
  280. {error, _Reason} ->
  281. {error, lists:flatten(
  282. io_lib:format("Failed to move ~ts to ~ts (tmpdir failed)~n",
  283. [Source, Dest]))};
  284. TmpPath ->
  285. case robocopy_file(SrcDir, TmpPath, SrcName) of
  286. {error, Reason} ->
  287. {error, Reason};
  288. ok ->
  289. TmpSrc = filename:join(TmpPath, SrcName),
  290. TmpDst = filename:join(TmpPath, DestName),
  291. case file:rename(TmpSrc, TmpDst) of
  292. {error, _} ->
  293. {error, lists:flatten(
  294. io_lib:format("Failed to move ~ts to ~ts (via rename)~n",
  295. [Source, Dest]))};
  296. ok ->
  297. case robocopy_file(TmpPath, DestDir, DestName) of
  298. Err = {error, _} -> Err;
  299. OK -> rm_rf(TmpPath), OK
  300. end
  301. end
  302. end
  303. end.
  304. robocopy_file(SrcPath, DestPath, FileName) ->
  305. Cmd = ?FMT("robocopy /move /e \"~ts\" \"~ts\" \"~ts\" 1> nul",
  306. [rebar_utils:escape_double_quotes(SrcPath),
  307. rebar_utils:escape_double_quotes(DestPath),
  308. rebar_utils:escape_double_quotes(FileName)]),
  309. Res = rebar_utils:sh(Cmd, [{use_stdout, false}, return_on_error]),
  310. case win32_ok(Res) of
  311. false ->
  312. {error, lists:flatten(
  313. io_lib:format("Failed to move ~ts to ~ts~n",
  314. [filename:join(SrcPath, FileName),
  315. filename:join(DestPath, FileName)]))};
  316. true ->
  317. ok
  318. end.
  319. robocopy_dir(Source, Dest) ->
  320. Cmd = ?FMT("robocopy /move /e \"~ts\" \"~ts\" 1> nul",
  321. [rebar_utils:escape_double_quotes(Source),
  322. rebar_utils:escape_double_quotes(Dest)]),
  323. Res = rebar_utils:sh(Cmd,
  324. [{use_stdout, false}, return_on_error]),
  325. case win32_ok(Res) of
  326. true -> ok;
  327. false ->
  328. {error, lists:flatten(
  329. io_lib:format("Failed to move ~ts to ~ts~n",
  330. [Source, Dest]))}
  331. end.
  332. win32_ok({ok, _}) -> true;
  333. win32_ok({error, {Rc, _}}) when Rc<9; Rc=:=16 -> true;
  334. win32_ok(_) -> false.
  335. delete_each([]) ->
  336. ok;
  337. delete_each([File | Rest]) ->
  338. case file:delete(File) of
  339. ok ->
  340. delete_each(Rest);
  341. {error, enoent} ->
  342. delete_each(Rest);
  343. {error, Reason} ->
  344. ?ERROR("Failed to delete file ~ts: ~p\n", [File, Reason]),
  345. ?ABORT
  346. end.
  347. %% @doc backwards compat layer to pre-utf8 support
  348. write_file_if_contents_differ(Filename, Bytes) ->
  349. write_file_if_contents_differ(Filename, Bytes, raw).
  350. %% @doc let the user pick the encoding required; there are no good
  351. %% heuristics for data encoding
  352. write_file_if_contents_differ(Filename, Bytes, raw) ->
  353. write_file_if_contents_differ_(Filename, iolist_to_binary(Bytes));
  354. write_file_if_contents_differ(Filename, Bytes, utf8) ->
  355. write_file_if_contents_differ_(Filename, unicode:characters_to_binary(Bytes, utf8)).
  356. %% @private compare raw strings and check contents
  357. write_file_if_contents_differ_(Filename, ToWrite) ->
  358. case file:read_file(Filename) of
  359. {ok, ToWrite} ->
  360. ok;
  361. {ok, _} ->
  362. file:write_file(Filename, ToWrite, [raw]);
  363. {error, _} ->
  364. file:write_file(Filename, ToWrite, [raw])
  365. end.
  366. %% returns an os appropriate tmpdir given a path
  367. -spec system_tmpdir() -> file:filename().
  368. system_tmpdir() -> system_tmpdir([]).
  369. -spec system_tmpdir(PathComponents) -> file:filename() when
  370. PathComponents :: [file:name()].
  371. system_tmpdir(PathComponents) ->
  372. Tmp = case erlang:system_info(system_architecture) of
  373. "win32" ->
  374. "./tmp";
  375. _SysArch ->
  376. "/tmp"
  377. end,
  378. filename:join([Tmp|PathComponents]).
  379. %% recursively removes a directory and then recreates the same
  380. %% directory but empty
  381. -spec reset_dir(Path) -> ok | {error, Reason} when
  382. Path :: file:name(),
  383. Reason :: file:posix().
  384. reset_dir(Path) ->
  385. %% delete the directory if it exists
  386. _ = ec_file:remove(Path, [recursive]),
  387. %% recreate the directory
  388. ensure_dir(Path).
  389. %% Linux touch but using erlang functions to work in bot *nix os and
  390. %% windows
  391. -spec touch(Path) -> ok | {error, Reason} when
  392. Path :: file:name(),
  393. Reason :: file:posix().
  394. touch(Path) ->
  395. {ok, A} = file:read_file_info(Path),
  396. ok = file:write_file_info(Path, A#file_info{mtime = calendar:local_time(),
  397. atime = calendar:local_time()}).
  398. %% for a given path return the path relative to a base directory
  399. -spec path_from_ancestor(string(), string()) -> {ok, string()} | {error, badparent}.
  400. path_from_ancestor(Target, To) ->
  401. path_from_ancestor_(filename:split(canonical_path(Target)),
  402. filename:split(canonical_path(To))).
  403. path_from_ancestor_([Part|Target], [Part|To]) -> path_from_ancestor_(Target, To);
  404. path_from_ancestor_([], []) -> {ok, ""};
  405. path_from_ancestor_(Target, []) -> {ok, filename:join(Target)};
  406. path_from_ancestor_(_, _) -> {error, badparent}.
  407. %% reduce a filepath by removing all incidences of `.' and `..'
  408. -spec canonical_path(string()) -> string().
  409. canonical_path(Dir) ->
  410. Canon = canonical_path([], filename:split(filename:absname(Dir))),
  411. filename:nativename(Canon).
  412. canonical_path([], []) -> filename:absname("/");
  413. canonical_path(Acc, []) -> filename:join(lists:reverse(Acc));
  414. canonical_path(Acc, ["."|Rest]) -> canonical_path(Acc, Rest);
  415. canonical_path([_|Acc], [".."|Rest]) -> canonical_path(Acc, Rest);
  416. canonical_path([], [".."|Rest]) -> canonical_path([], Rest);
  417. canonical_path(Acc, [Component|Rest]) -> canonical_path([Component|Acc], Rest).
  418. %% @doc make a path absolute
  419. -spec absolute_path(file:filename()) -> file:filename().
  420. absolute_path(Path) ->
  421. case filename:pathtype(Path) of
  422. absolute ->
  423. Path;
  424. relative ->
  425. {ok, Dir} = file:get_cwd(),
  426. filename:join([Dir, Path]);
  427. volumerelative ->
  428. [Letter, $: | _] = filename:nativename(filename:absname(Path)),
  429. Volume = [Letter, $:],
  430. {ok, Dir} = file:get_cwd(Volume),
  431. Volume ++ filename:join([Dir, Path])
  432. end.
  433. %% @doc normalizing a path removes all of the `..' and the
  434. %% `.' segments it may contain.
  435. -spec normalized_path(file:filename()) -> file:filename().
  436. normalized_path(Path) ->
  437. AbsPath = absolute_path(Path),
  438. Components = filename:split(AbsPath),
  439. normalized_path(Components, []).
  440. %% @private drops path fragments for normalization
  441. -spec normalized_path([string()], [string()]) -> file:filename().
  442. normalized_path([], NormalizedPath) ->
  443. filename:join(lists:reverse(NormalizedPath));
  444. normalized_path([H|T], NormalizedPath) ->
  445. case H of
  446. "." when NormalizedPath == [], T == [] -> normalized_path(T, ["."]);
  447. "." -> normalized_path(T, NormalizedPath);
  448. ".." when NormalizedPath == [] -> normalized_path(T, [".."]);
  449. ".." when hd(NormalizedPath) =/= ".." -> normalized_path(T, tl(NormalizedPath));
  450. _ -> normalized_path(T, [H|NormalizedPath])
  451. end.
  452. %% @doc normalizes relative paths so that ./a/b/c/ => a/b/c
  453. -spec normalize_relative_path(string()) -> file:filename().
  454. normalize_relative_path(Path) ->
  455. normalized_path(filename:split(Path), []).
  456. %% @doc returns canonical target of path if path is a link, otherwise returns path
  457. -spec resolve_link(string()) -> string().
  458. resolve_link(Path) ->
  459. case file:read_link(Path) of
  460. {ok, Target} ->
  461. canonical_path(filename:absname(Target, filename:dirname(Path)));
  462. {error, _} -> Path
  463. end.
  464. %% @doc splits a path into dirname and basename
  465. -spec split_dirname(string()) -> {string(), string()}.
  466. split_dirname(Path) ->
  467. {filename:dirname(Path), filename:basename(Path)}.
  468. -spec ensure_dir(filelib:dirname_all()) -> ok | {error, file:posix()}.
  469. ensure_dir(Path) ->
  470. filelib:ensure_dir(filename:join(Path, "fake_file")).
  471. %% ===================================================================
  472. %% Internal functions
  473. %% ===================================================================
  474. delete_each_dir_win32([]) -> ok;
  475. delete_each_dir_win32([Dir | Rest]) ->
  476. {ok, []} = rebar_utils:sh(?FMT("rd /q /s \"~ts\"",
  477. [rebar_utils:escape_double_quotes(filename:nativename(Dir))]),
  478. [{use_stdout, false}, return_on_error]),
  479. delete_each_dir_win32(Rest).
  480. xcopy_win32(Source,Dest)->
  481. %% "xcopy \"~ts\" \"~ts\" /q /y /e 2> nul", Changed to robocopy to
  482. %% handle long names. May have issues with older windows.
  483. Cmd = case filelib:is_dir(Source) of
  484. true ->
  485. %% For robocopy, copying /a/b/c/ to /d/e/f/ recursively does not
  486. %% create /d/e/f/c/*, but rather copies all files to /d/e/f/*.
  487. %% The usage we make here expects the former, not the later, so we
  488. %% must manually add the last fragment of a directory to the `Dest`
  489. %% in order to properly replicate POSIX platforms
  490. NewDest = filename:join([Dest, filename:basename(Source)]),
  491. ?FMT("robocopy \"~ts\" \"~ts\" /e 1> nul",
  492. [rebar_utils:escape_double_quotes(filename:nativename(Source)),
  493. rebar_utils:escape_double_quotes(filename:nativename(NewDest))]);
  494. false ->
  495. ?FMT("robocopy \"~ts\" \"~ts\" \"~ts\" /e 1> nul",
  496. [rebar_utils:escape_double_quotes(filename:nativename(filename:dirname(Source))),
  497. rebar_utils:escape_double_quotes(filename:nativename(Dest)),
  498. rebar_utils:escape_double_quotes(filename:basename(Source))])
  499. end,
  500. Res = rebar_utils:sh(Cmd,
  501. [{use_stdout, false}, return_on_error]),
  502. case win32_ok(Res) of
  503. true -> ok;
  504. false ->
  505. {error, lists:flatten(
  506. io_lib:format("Failed to copy ~ts to ~ts~n",
  507. [Source, Dest]))}
  508. end.
  509. cp_r_win32({true, SourceDir}, {true, DestDir}) ->
  510. %% from directory to directory
  511. ok = case file:make_dir(DestDir) of
  512. {error, eexist} -> ok;
  513. Other -> Other
  514. end,
  515. ok = xcopy_win32(SourceDir, DestDir);
  516. cp_r_win32({false, Source} = S,{true, DestDir}) ->
  517. %% from file to directory
  518. cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
  519. cp_r_win32({false, Source},{false, Dest}) ->
  520. %% from file to file
  521. {ok,_} = file:copy(Source, Dest),
  522. ok;
  523. cp_r_win32({true, SourceDir}, {false, DestDir}) ->
  524. case filelib:is_regular(DestDir) of
  525. true ->
  526. %% From directory to file? This shouldn't happen
  527. {error, lists:flatten(
  528. io_lib:format("Cannot copy dir (~p) to file (~p)\n",
  529. [SourceDir, DestDir]))};
  530. false ->
  531. %% Specifying a target directory that doesn't currently exist.
  532. %% So let's attempt to create this directory
  533. case ensure_dir(DestDir) of
  534. ok ->
  535. ok = xcopy_win32(SourceDir, DestDir);
  536. {error, Reason} ->
  537. {error, lists:flatten(
  538. io_lib:format("Unable to create dir ~p: ~p\n",
  539. [DestDir, Reason]))}
  540. end
  541. end;
  542. cp_r_win32(Source,Dest) ->
  543. Dst = {filelib:is_dir(Dest), Dest},
  544. lists:foreach(fun(Src) ->
  545. ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
  546. end, filelib:wildcard(Source)),
  547. ok.