From e0a86868cab4e091042568faa3da96b574917787 Mon Sep 17 00:00:00 2001 From: Tuncer Ayaz Date: Thu, 16 Jun 2011 17:38:48 +0200 Subject: [PATCH] Log if sub_dirs loop is detected --- src/rebar_subdirs.erl | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/rebar_subdirs.erl b/src/rebar_subdirs.erl index b107978..261bdf8 100644 --- a/src/rebar_subdirs.erl +++ b/src/rebar_subdirs.erl @@ -27,6 +27,7 @@ -module(rebar_subdirs). -include("rebar.hrl"). +-include_lib("kernel/include/file.hrl"). -export([preprocess/2]). @@ -37,6 +38,38 @@ preprocess(Config, _) -> %% Get the list of subdirs specified in the config (if any). Cwd = rebar_utils:get_cwd(), - Subdirs = [filename:join(Cwd, Dir) || - Dir <- rebar_config:get_local(Config, sub_dirs, [])], + Subdirs0 = rebar_config:get_local(Config, sub_dirs, []), + Check = check_loop(Cwd), + ok = lists:foreach(Check, Subdirs0), + Subdirs = [filename:join(Cwd, Dir) || Dir <- Subdirs0], {ok, Subdirs}. + +%% =================================================================== +%% Internal functions +%% =================================================================== + +check_loop(Cwd) -> + RebarConfig = filename:join(Cwd, "rebar.config"), + fun(Dir0) -> + IsSymlink = case file:read_link_info(Dir0) of + {ok, #file_info{type=symlink}} -> + {true, resolve_symlink(Dir0)}; + _ -> + {false, Dir0} + end, + case IsSymlink of + {false, Dir="."} -> + ?ERROR("endless loop detected:~nsub_dirs" + " entry ~p in ~s~n", [Dir, RebarConfig]); + {true, Cwd} -> + ?ERROR("endless loop detected:~nsub_dirs" + " entry ~p in ~s is a symlink to \".\"~n", + [Dir0, RebarConfig]); + _ -> + ok + end + end. + +resolve_symlink(Dir0) -> + {ok, Dir} = file:read_link(Dir0), + Dir.