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.

527 line
21 KiB

15 年之前
15 年之前
15 年之前
12 年之前
15 年之前
15 年之前
12 年之前
15 年之前
7 年之前
15 年之前
12 年之前
14 年之前
14 年之前
14 年之前
  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. format_error/1,
  31. symlink_or_copy/2,
  32. rm_rf/1,
  33. cp_r/2,
  34. mv/2,
  35. delete_each/1,
  36. write_file_if_contents_differ/2,
  37. write_file_if_contents_differ/3,
  38. system_tmpdir/0,
  39. system_tmpdir/1,
  40. reset_dir/1,
  41. touch/1,
  42. path_from_ancestor/2,
  43. canonical_path/1,
  44. resolve_link/1,
  45. split_dirname/1,
  46. ensure_dir/1]).
  47. -include("rebar.hrl").
  48. -include_lib("providers/include/providers.hrl").
  49. -include_lib("kernel/include/file.hrl").
  50. %% ===================================================================
  51. %% Public API
  52. %% ===================================================================
  53. try_consult(File) ->
  54. case file:consult(File) of
  55. {ok, Terms} ->
  56. Terms;
  57. {error, enoent} ->
  58. [];
  59. {error, Reason} ->
  60. throw(?PRV_ERROR({bad_term_file, File, Reason}))
  61. end.
  62. -spec consult_config(rebar_state:t(), string()) -> [[tuple()]].
  63. consult_config(State, Filename) ->
  64. Fullpath = filename:join(rebar_dir:root_dir(State), Filename),
  65. ?DEBUG("Loading configuration from ~p", [Fullpath]),
  66. Config = case try_consult(Fullpath) of
  67. [T] -> T;
  68. [] -> []
  69. end,
  70. JoinedConfig = lists:flatmap(
  71. fun (SubConfig) when is_list(SubConfig) ->
  72. case lists:suffix(".config", SubConfig) of
  73. %% since consult_config returns a list in a list we take the head here
  74. false -> hd(consult_config(State, SubConfig ++ ".config"));
  75. true -> hd(consult_config(State, SubConfig))
  76. end;
  77. (Entry) -> [Entry]
  78. end, Config),
  79. %% Backwards compatibility
  80. [JoinedConfig].
  81. format_error({bad_term_file, AppFile, Reason}) ->
  82. io_lib:format("Error reading file ~ts: ~ts", [AppFile, file:format_error(Reason)]).
  83. symlink_or_copy(Source, Target) ->
  84. Link = case os:type() of
  85. {win32, _} ->
  86. Source;
  87. _ ->
  88. rebar_dir:make_relative_path(Source, Target)
  89. end,
  90. case file:make_symlink(Link, Target) of
  91. ok ->
  92. ok;
  93. {error, eexist} ->
  94. exists;
  95. {error, _} ->
  96. case os:type() of
  97. {win32, _} ->
  98. S = unicode:characters_to_list(Source),
  99. T = unicode:characters_to_list(Target),
  100. case filelib:is_dir(S) of
  101. true ->
  102. win32_symlink_or_copy(S, T);
  103. false ->
  104. cp_r([S], T)
  105. end;
  106. _ ->
  107. case filelib:is_dir(Target) of
  108. true ->
  109. ok;
  110. false ->
  111. cp_r([Source], Target)
  112. end
  113. end
  114. end.
  115. %% @private Compatibility function for windows
  116. win32_symlink_or_copy(Source, Target) ->
  117. Res = rebar_utils:sh(
  118. ?FMT("cmd /c mklink /j \"~ts\" \"~ts\"",
  119. [rebar_utils:escape_double_quotes(filename:nativename(Target)),
  120. rebar_utils:escape_double_quotes(filename:nativename(Source))]),
  121. [{use_stdout, false}, return_on_error]),
  122. case win32_mklink_ok(Res, Target) of
  123. true -> ok;
  124. false -> cp_r_win32(Source, drop_last_dir_from_path(Target))
  125. end.
  126. %% @private specifically pattern match against the output
  127. %% of the windows 'mklink' shell call; different values from
  128. %% what win32_ok/1 handles
  129. win32_mklink_ok({ok, _}, _) ->
  130. true;
  131. win32_mklink_ok({error,{1,"Local NTFS volumes are required to complete the operation.\n"}}, _) ->
  132. false;
  133. win32_mklink_ok({error,{1,"Cannot create a file when that file already exists.\n"}}, Target) ->
  134. % File or dir is already in place; find if it is already a symlink (true) or
  135. % if it is a directory (copy-required; false)
  136. is_symlink(Target);
  137. win32_mklink_ok(_, _) ->
  138. false.
  139. %% @private
  140. is_symlink(Filename) ->
  141. {ok, Info} = file:read_link_info(Filename),
  142. Info#file_info.type == symlink.
  143. %% @private
  144. %% drops the last 'node' of the filename, presumably the last dir such as 'src'
  145. %% this is because cp_r_win32/2 automatically adds the dir name, to appease
  146. %% robocopy and be more uniform with POSIX
  147. drop_last_dir_from_path([]) ->
  148. [];
  149. drop_last_dir_from_path(Path) ->
  150. case lists:droplast(filename:split(Path)) of
  151. [] -> [];
  152. Dirs -> filename:join(Dirs)
  153. end.
  154. %% @doc Remove files and directories.
  155. %% Target is a single filename, directoryname or wildcard expression.
  156. -spec rm_rf(string()) -> 'ok'.
  157. rm_rf(Target) ->
  158. case os:type() of
  159. {unix, _} ->
  160. EscTarget = rebar_utils:escape_chars(Target),
  161. {ok, []} = rebar_utils:sh(?FMT("rm -rf ~ts", [EscTarget]),
  162. [{use_stdout, false}, abort_on_error]),
  163. ok;
  164. {win32, _} ->
  165. Filelist = filelib:wildcard(Target),
  166. Dirs = [F || F <- Filelist, filelib:is_dir(F)],
  167. Files = Filelist -- Dirs,
  168. ok = delete_each(Files),
  169. ok = delete_each_dir_win32(Dirs),
  170. ok
  171. end.
  172. -spec cp_r(list(string()), file:filename()) -> 'ok'.
  173. cp_r([], _Dest) ->
  174. ok;
  175. cp_r(Sources, Dest) ->
  176. case os:type() of
  177. {unix, _} ->
  178. EscSources = [rebar_utils:escape_chars(Src) || Src <- Sources],
  179. SourceStr = rebar_string:join(EscSources, " "),
  180. % ensure destination exists before copying files into it
  181. {ok, []} = rebar_utils:sh(?FMT("mkdir -p ~ts",
  182. [rebar_utils:escape_chars(Dest)]),
  183. [{use_stdout, false}, abort_on_error]),
  184. {ok, []} = rebar_utils:sh(?FMT("cp -Rp ~ts \"~ts\"",
  185. [SourceStr, rebar_utils:escape_double_quotes(Dest)]),
  186. [{use_stdout, false}, abort_on_error]),
  187. ok;
  188. {win32, _} ->
  189. lists:foreach(fun(Src) -> ok = cp_r_win32(Src,Dest) end, Sources),
  190. ok
  191. end.
  192. -spec mv(string(), file:filename()) -> 'ok'.
  193. mv(Source, Dest) ->
  194. case os:type() of
  195. {unix, _} ->
  196. EscSource = rebar_utils:escape_chars(Source),
  197. EscDest = rebar_utils:escape_chars(Dest),
  198. case rebar_utils:sh(?FMT("mv ~ts ~ts", [EscSource, EscDest]),
  199. [{use_stdout, false}, abort_on_error]) of
  200. {ok, []} ->
  201. ok;
  202. {ok, Warning} ->
  203. ?WARN("mv: ~p", [Warning]),
  204. ok
  205. end;
  206. {win32, _} ->
  207. case filelib:is_dir(Source) of
  208. true ->
  209. SrcDir = filename:nativename(Source),
  210. DestDir = case filelib:is_dir(Dest) of
  211. true ->
  212. %% to simulate unix/posix mv, we have to replicate
  213. %% the same directory movement by moving the whole
  214. %% top-level directory, not just the insides
  215. SrcName = filename:basename(Source),
  216. filename:nativename(filename:join(Dest, SrcName));
  217. false ->
  218. filename:nativename(Dest)
  219. end,
  220. robocopy_dir(SrcDir, DestDir);
  221. false ->
  222. SrcDir = filename:nativename(filename:dirname(Source)),
  223. SrcName = filename:basename(Source),
  224. DestDir = filename:nativename(filename:dirname(Dest)),
  225. DestName = filename:basename(Dest),
  226. IsDestDir = filelib:is_dir(Dest),
  227. if IsDestDir ->
  228. %% if basename and target name are different because
  229. %% we move to a directory, then just move there.
  230. %% Similarly, if they are the same but we're going to
  231. %% a directory, let's just do that directly.
  232. FullDestDir = filename:nativename(Dest),
  233. robocopy_file(SrcDir, FullDestDir, SrcName)
  234. ; SrcName =:= DestName ->
  235. %% if basename and target name are the same and both are files,
  236. %% we do a regular move with robocopy without rename.
  237. robocopy_file(SrcDir, DestDir, DestName)
  238. ; SrcName =/= DestName->
  239. robocopy_mv_and_rename(Source, Dest, SrcDir, SrcName, DestDir, DestName)
  240. end
  241. end
  242. end.
  243. robocopy_mv_and_rename(Source, Dest, SrcDir, SrcName, DestDir, DestName) ->
  244. %% If we're moving a file and the origin and
  245. %% destination names are different:
  246. %% - mktmp
  247. %% - robocopy source_dir tmp_dir srcname
  248. %% - rename srcname destname (to avoid clobbering)
  249. %% - robocopy tmp_dir dest_dir destname
  250. %% - remove tmp_dir
  251. case ec_file:insecure_mkdtemp() of
  252. {error, _Reason} ->
  253. {error, lists:flatten(
  254. io_lib:format("Failed to move ~ts to ~ts (tmpdir failed)~n",
  255. [Source, Dest]))};
  256. TmpPath ->
  257. case robocopy_file(SrcDir, TmpPath, SrcName) of
  258. {error, Reason} ->
  259. {error, Reason};
  260. ok ->
  261. TmpSrc = filename:join(TmpPath, SrcName),
  262. TmpDst = filename:join(TmpPath, DestName),
  263. case file:rename(TmpSrc, TmpDst) of
  264. {error, _} ->
  265. {error, lists:flatten(
  266. io_lib:format("Failed to move ~ts to ~ts (via rename)~n",
  267. [Source, Dest]))};
  268. ok ->
  269. case robocopy_file(TmpPath, DestDir, DestName) of
  270. Err = {error, _} -> Err;
  271. OK -> rm_rf(TmpPath), OK
  272. end
  273. end
  274. end
  275. end.
  276. robocopy_file(SrcPath, DestPath, FileName) ->
  277. Cmd = ?FMT("robocopy /move /e \"~ts\" \"~ts\" \"~ts\"",
  278. [rebar_utils:escape_double_quotes(SrcPath),
  279. rebar_utils:escape_double_quotes(DestPath),
  280. rebar_utils:escape_double_quotes(FileName)]),
  281. Res = rebar_utils:sh(Cmd, [{use_stdout, false}, return_on_error]),
  282. case win32_ok(Res) of
  283. false ->
  284. {error, lists:flatten(
  285. io_lib:format("Failed to move ~ts to ~ts~n",
  286. [filename:join(SrcPath, FileName),
  287. filename:join(DestPath, FileName)]))};
  288. true ->
  289. ok
  290. end.
  291. robocopy_dir(Source, Dest) ->
  292. Cmd = ?FMT("robocopy /move /e \"~ts\" \"~ts\"",
  293. [rebar_utils:escape_double_quotes(Source),
  294. rebar_utils:escape_double_quotes(Dest)]),
  295. Res = rebar_utils:sh(Cmd,
  296. [{use_stdout, false}, return_on_error]),
  297. case win32_ok(Res) of
  298. true -> ok;
  299. false ->
  300. {error, lists:flatten(
  301. io_lib:format("Failed to move ~ts to ~ts~n",
  302. [Source, Dest]))}
  303. end.
  304. win32_ok({ok, _}) -> true;
  305. win32_ok({error, {Rc, _}}) when Rc<9; Rc=:=16 -> true;
  306. win32_ok(_) -> false.
  307. delete_each([]) ->
  308. ok;
  309. delete_each([File | Rest]) ->
  310. case file:delete(File) of
  311. ok ->
  312. delete_each(Rest);
  313. {error, enoent} ->
  314. delete_each(Rest);
  315. {error, Reason} ->
  316. ?ERROR("Failed to delete file ~ts: ~p\n", [File, Reason]),
  317. ?FAIL
  318. end.
  319. %% @doc backwards compat layer to pre-utf8 support
  320. write_file_if_contents_differ(Filename, Bytes) ->
  321. write_file_if_contents_differ(Filename, Bytes, raw).
  322. %% @doc let the user pick the encoding required; there are no good
  323. %% heuristics for data encoding
  324. write_file_if_contents_differ(Filename, Bytes, raw) ->
  325. write_file_if_contents_differ_(Filename, iolist_to_binary(Bytes));
  326. write_file_if_contents_differ(Filename, Bytes, utf8) ->
  327. write_file_if_contents_differ_(Filename, unicode:characters_to_binary(Bytes, utf8)).
  328. %% @private compare raw strings and check contents
  329. write_file_if_contents_differ_(Filename, ToWrite) ->
  330. case file:read_file(Filename) of
  331. {ok, ToWrite} ->
  332. ok;
  333. {ok, _} ->
  334. file:write_file(Filename, ToWrite, [raw]);
  335. {error, _} ->
  336. file:write_file(Filename, ToWrite, [raw])
  337. end.
  338. %% returns an os appropriate tmpdir given a path
  339. -spec system_tmpdir() -> file:filename().
  340. system_tmpdir() -> system_tmpdir([]).
  341. -spec system_tmpdir(PathComponents) -> file:filename() when
  342. PathComponents :: [file:name()].
  343. system_tmpdir(PathComponents) ->
  344. Tmp = case erlang:system_info(system_architecture) of
  345. "win32" ->
  346. "./tmp";
  347. _SysArch ->
  348. "/tmp"
  349. end,
  350. filename:join([Tmp|PathComponents]).
  351. %% recursively removes a directory and then recreates the same
  352. %% directory but empty
  353. -spec reset_dir(Path) -> ok | {error, Reason} when
  354. Path :: file:name(),
  355. Reason :: file:posix().
  356. reset_dir(Path) ->
  357. %% delete the directory if it exists
  358. _ = ec_file:remove(Path, [recursive]),
  359. %% recreate the directory
  360. ensure_dir(Path).
  361. %% Linux touch but using erlang functions to work in bot *nix os and
  362. %% windows
  363. -spec touch(Path) -> ok | {error, Reason} when
  364. Path :: file:name(),
  365. Reason :: file:posix().
  366. touch(Path) ->
  367. {ok, A} = file:read_file_info(Path),
  368. ok = file:write_file_info(Path, A#file_info{mtime = calendar:local_time(),
  369. atime = calendar:local_time()}).
  370. %% for a given path return the path relative to a base directory
  371. -spec path_from_ancestor(string(), string()) -> {ok, string()} | {error, badparent}.
  372. path_from_ancestor(Target, To) ->
  373. path_from_ancestor_(filename:split(canonical_path(Target)),
  374. filename:split(canonical_path(To))).
  375. path_from_ancestor_([Part|Target], [Part|To]) -> path_from_ancestor_(Target, To);
  376. path_from_ancestor_([], []) -> {ok, ""};
  377. path_from_ancestor_(Target, []) -> {ok, filename:join(Target)};
  378. path_from_ancestor_(_, _) -> {error, badparent}.
  379. %% reduce a filepath by removing all incidences of `.' and `..'
  380. -spec canonical_path(string()) -> string().
  381. canonical_path(Dir) ->
  382. Canon = canonical_path([], filename:split(filename:absname(Dir))),
  383. filename:nativename(Canon).
  384. canonical_path([], []) -> filename:absname("/");
  385. canonical_path(Acc, []) -> filename:join(lists:reverse(Acc));
  386. canonical_path(Acc, ["."|Rest]) -> canonical_path(Acc, Rest);
  387. canonical_path([_|Acc], [".."|Rest]) -> canonical_path(Acc, Rest);
  388. canonical_path([], [".."|Rest]) -> canonical_path([], Rest);
  389. canonical_path(Acc, [Component|Rest]) -> canonical_path([Component|Acc], Rest).
  390. %% @doc returns canonical target of path if path is a link, otherwise returns path
  391. -spec resolve_link(string()) -> string().
  392. resolve_link(Path) ->
  393. case file:read_link(Path) of
  394. {ok, Target} ->
  395. canonical_path(filename:absname(Target, filename:dirname(Path)));
  396. {error, _} -> Path
  397. end.
  398. %% @doc splits a path into dirname and basename
  399. -spec split_dirname(string()) -> {string(), string()}.
  400. split_dirname(Path) ->
  401. {filename:dirname(Path), filename:basename(Path)}.
  402. -spec ensure_dir(filelib:dirname_all()) -> ok | {error, file:posix()}.
  403. ensure_dir(Path) ->
  404. filelib:ensure_dir(filename:join(Path, "fake_file")).
  405. %% ===================================================================
  406. %% Internal functions
  407. %% ===================================================================
  408. delete_each_dir_win32([]) -> ok;
  409. delete_each_dir_win32([Dir | Rest]) ->
  410. {ok, []} = rebar_utils:sh(?FMT("rd /q /s \"~ts\"",
  411. [rebar_utils:escape_double_quotes(filename:nativename(Dir))]),
  412. [{use_stdout, false}, return_on_error]),
  413. delete_each_dir_win32(Rest).
  414. xcopy_win32(Source,Dest)->
  415. %% "xcopy \"~ts\" \"~ts\" /q /y /e 2> nul", Changed to robocopy to
  416. %% handle long names. May have issues with older windows.
  417. Cmd = case filelib:is_dir(Source) of
  418. true ->
  419. %% For robocopy, copying /a/b/c/ to /d/e/f/ recursively does not
  420. %% create /d/e/f/c/*, but rather copies all files to /d/e/f/*.
  421. %% The usage we make here expects the former, not the later, so we
  422. %% must manually add the last fragment of a directory to the `Dest`
  423. %% in order to properly replicate POSIX platforms
  424. NewDest = filename:join([Dest, filename:basename(Source)]),
  425. ?FMT("robocopy \"~ts\" \"~ts\" /e 1> nul",
  426. [rebar_utils:escape_double_quotes(filename:nativename(Source)),
  427. rebar_utils:escape_double_quotes(filename:nativename(NewDest))]);
  428. false ->
  429. ?FMT("robocopy \"~ts\" \"~ts\" \"~ts\" /e 1> nul",
  430. [rebar_utils:escape_double_quotes(filename:nativename(filename:dirname(Source))),
  431. rebar_utils:escape_double_quotes(filename:nativename(Dest)),
  432. rebar_utils:escape_double_quotes(filename:basename(Source))])
  433. end,
  434. Res = rebar_utils:sh(Cmd,
  435. [{use_stdout, false}, return_on_error]),
  436. case win32_ok(Res) of
  437. true -> ok;
  438. false ->
  439. {error, lists:flatten(
  440. io_lib:format("Failed to copy ~ts to ~ts~n",
  441. [Source, Dest]))}
  442. end.
  443. cp_r_win32({true, SourceDir}, {true, DestDir}) ->
  444. %% from directory to directory
  445. ok = case file:make_dir(DestDir) of
  446. {error, eexist} -> ok;
  447. Other -> Other
  448. end,
  449. ok = xcopy_win32(SourceDir, DestDir);
  450. cp_r_win32({false, Source} = S,{true, DestDir}) ->
  451. %% from file to directory
  452. cp_r_win32(S, {false, filename:join(DestDir, filename:basename(Source))});
  453. cp_r_win32({false, Source},{false, Dest}) ->
  454. %% from file to file
  455. {ok,_} = file:copy(Source, Dest),
  456. ok;
  457. cp_r_win32({true, SourceDir}, {false, DestDir}) ->
  458. case filelib:is_regular(DestDir) of
  459. true ->
  460. %% From directory to file? This shouldn't happen
  461. {error, lists:flatten(
  462. io_lib:format("Cannot copy dir (~p) to file (~p)\n",
  463. [SourceDir, DestDir]))};
  464. false ->
  465. %% Specifying a target directory that doesn't currently exist.
  466. %% So let's attempt to create this directory
  467. case ensure_dir(DestDir) of
  468. ok ->
  469. ok = xcopy_win32(SourceDir, DestDir);
  470. {error, Reason} ->
  471. {error, lists:flatten(
  472. io_lib:format("Unable to create dir ~p: ~p\n",
  473. [DestDir, Reason]))}
  474. end
  475. end;
  476. cp_r_win32(Source,Dest) ->
  477. Dst = {filelib:is_dir(Dest), Dest},
  478. lists:foreach(fun(Src) ->
  479. ok = cp_r_win32({filelib:is_dir(Src), Src}, Dst)
  480. end, filelib:wildcard(Source)),
  481. ok.