1%%
2%% %CopyrightBegin%
3%%
4%% Copyright Ericsson AB 2008-2016. All Rights Reserved.
5%%
6%% Licensed under the Apache License, Version 2.0 (the "License");
7%% you may not use this file except in compliance with the License.
8%% You may obtain a copy of the License at
9%%
10%%     http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing, software
13%% distributed under the License is distributed on an "AS IS" BASIS,
14%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15%% See the License for the specific language governing permissions and
16%% limitations under the License.
17%%
18%% %CopyrightEnd%
19%%
20
21%%%-------------------------------------------------------------------
22%%% File    : ignore_cores.erl
23%%% Author  : Rickard Green <rickard.s.green@ericsson.com>
24%%% Description :
25%%%
26%%% Created : 11 Feb 2008 by Rickard Green <rickard.s.green@ericsson.com>
27%%%-------------------------------------------------------------------
28
29-module(ignore_cores).
30
31-include_lib("common_test/include/ct.hrl").
32
33-export([init/1, fini/1, setup/3, setup/4, restore/1, dir/1]).
34
35-record(ignore_cores, {org_cwd,
36		       org_path,
37		       org_pwd_env,
38		       ign_dir = false,
39		       cores_dir = false}).
40
41%%
42%% Takes a testcase config
43%%
44
45init(Config) ->
46    {ok, OrgCWD} = file:get_cwd(),
47    [{ignore_cores,
48      #ignore_cores{org_cwd = OrgCWD,
49		    org_path = code:get_path(),
50		    org_pwd_env = os:getenv("PWD")}}
51     | lists:keydelete(ignore_cores, 1, Config)].
52
53fini(Config) ->
54    #ignore_cores{org_cwd = OrgCWD,
55		  org_path = OrgPath,
56		  org_pwd_env = OrgPWD} = proplists:get_value(ignore_cores, Config),
57    ok = file:set_cwd(OrgCWD),
58    true = code:set_path(OrgPath),
59    case OrgPWD of
60	false -> ok;
61	_ -> true = os:putenv("PWD", OrgPWD)
62    end,
63    lists:keydelete(ignore_cores, 1, Config).
64
65setup(Suite, Testcase, Config) ->
66    setup(Suite, Testcase, Config, false).
67
68setup(Suite, Testcase, Config, SetCwd) when is_atom(Suite),
69                                            is_atom(Testcase),
70                                            is_list(Config) ->
71    #ignore_cores{org_cwd = OrgCWD,
72		  org_path = OrgPath,
73		  org_pwd_env = OrgPWD} = proplists:get_value(ignore_cores, Config),
74    Path = lists:map(fun (".") -> OrgCWD; (Dir) -> Dir end, OrgPath),
75    true = code:set_path(Path),
76    PrivDir = proplists:get_value(priv_dir, Config),
77    IgnDir = filename:join([PrivDir,
78			 atom_to_list(Suite)
79			 ++ "_"
80			 ++ atom_to_list(Testcase)
81			 ++ "_wd"]),
82    ok = file:make_dir(IgnDir),
83    case SetCwd of
84	false ->
85	    ok;
86	_ ->
87	    ok = file:set_cwd(IgnDir),
88	    OrgPWD = case os:getenv("PWD") of
89			 false -> false;
90			 PWD ->
91			     os:putenv("PWD", IgnDir),
92			     PWD
93		     end
94    end,
95    ok = file:write_file(filename:join([IgnDir, "ignore_core_files"]), <<>>),
96    %% cores are dumped in /cores on MacOS X
97    CoresDir = case {os:type(), filelib:is_dir("/cores")} of
98		   {{unix,darwin}, true} ->
99		       filelib:fold_files("/cores",
100					  "^core.*$",
101					  false,
102					  fun (C,Cs) -> [C|Cs] end,
103					  []);
104		   _ ->
105		       false
106	       end,
107    lists:keyreplace(ignore_cores,
108		     1,
109		     Config,
110		     {ignore_cores,
111		      #ignore_cores{org_cwd = OrgCWD,
112				    org_path = OrgPath,
113				    org_pwd_env = OrgPWD,
114				    ign_dir = IgnDir,
115				    cores_dir = CoresDir}}).
116
117restore(Config) ->
118    #ignore_cores{org_cwd = OrgCWD,
119		  org_path = OrgPath,
120		  org_pwd_env = OrgPWD,
121		  ign_dir = IgnDir,
122		  cores_dir = CoresDir} = proplists:get_value(ignore_cores, Config),
123    try
124	case CoresDir of
125	    false ->
126		ok;
127	    _ ->
128		%% Move cores dumped by these testcases in /cores
129		%% to cwd.
130		lists:foreach(fun (C) ->
131				      case lists:member(C, CoresDir) of
132					  true -> ok;
133					  _ ->
134					      Dst = filename:join(
135						      [IgnDir,
136						       filename:basename(C)]),
137					      {ok, _} = file:copy(C, Dst),
138					      file:delete(C)
139				      end
140			      end,
141			      filelib:fold_files("/cores",
142						 "^core.*$",
143						 false,
144						 fun (C,Cs) -> [C|Cs] end,
145						 []))
146	end
147    after
148	catch file:set_cwd(OrgCWD),
149	catch code:set_path(OrgPath),
150	case OrgPWD of
151	    false -> ok;
152	    _ -> catch os:putenv("PWD", OrgPWD)
153	end
154    end.
155
156
157dir(Config) ->
158    #ignore_cores{ign_dir = Dir} = proplists:get_value(ignore_cores, Config),
159    Dir.
160