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_subdirs). 28 29-include("rebar.hrl"). 30-include_lib("kernel/include/file.hrl"). 31 32-export([preprocess/2]). 33 34%% =================================================================== 35%% Public API 36%% =================================================================== 37 38preprocess(Config, _) -> 39 %% Get the list of subdirs specified in the config (if any). 40 Cwd = rebar_utils:get_cwd(), 41 ListSubdirs = rebar_config:get_local(Config, sub_dirs, []), 42 Subdirs0 = lists:flatmap(fun filelib:wildcard/1, ListSubdirs), 43 case {rebar_config:is_skip_dir(Config, Cwd), Subdirs0} of 44 {true, []} -> 45 {ok, []}; 46 {true, _} -> 47 ?WARN("Ignoring sub_dirs for ~s~n", [Cwd]), 48 {ok, []}; 49 {false, _} -> 50 Check = check_loop(Cwd), 51 ok = lists:foreach(Check, Subdirs0), 52 Subdirs = [filename:join(Cwd, Dir) || Dir <- Subdirs0], 53 {ok, Subdirs} 54 end. 55 56%% =================================================================== 57%% Internal functions 58%% =================================================================== 59 60check_loop(Cwd) -> 61 RebarConfig = filename:join(Cwd, "rebar.config"), 62 fun(Dir0) -> 63 IsSymlink = case file:read_link_info(Dir0) of 64 {ok, #file_info{type=symlink}} -> 65 {true, resolve_symlink(Dir0)}; 66 _ -> 67 {false, Dir0} 68 end, 69 case IsSymlink of 70 {false, Dir="."} -> 71 ?ERROR("infinite loop detected:~nsub_dirs" 72 " entry ~p in ~s~n", [Dir, RebarConfig]); 73 {true, Cwd} -> 74 ?ERROR("infinite loop detected:~nsub_dirs" 75 " entry ~p in ~s is a symlink to \".\"~n", 76 [Dir0, RebarConfig]); 77 _ -> 78 ok 79 end 80 end. 81 82resolve_symlink(Dir0) -> 83 {ok, Dir} = file:read_link(Dir0), 84 Dir. 85