您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

477 行
19 KiB

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